Example usage for android.media AudioTrack MODE_STREAM

List of usage examples for android.media AudioTrack MODE_STREAM

Introduction

In this page you can find the example usage for android.media AudioTrack MODE_STREAM.

Prototype

int MODE_STREAM

To view the source code for android.media AudioTrack MODE_STREAM.

Click Source Link

Document

Creation mode where audio data is streamed from Java to the native layer as the audio is playing.

Usage

From source file:com.brejza.matt.habmodem.Dsp_service.java

public void startAudio() {
    if (!_enableDecoder)
        return;// w  w  w . j a  v  a2 s  .c o  m

    boolean mic = this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_MICROPHONE);

    System.out.println("isRecording: " + isRecording);
    logEvent("Starting Audio. Mic avaliable: " + mic, false);
    if (!isRecording) {
        isRecording = true;

        buffsize = AudioRecord.getMinBufferSize(8000, AudioFormat.CHANNEL_IN_MONO,
                AudioFormat.ENCODING_PCM_16BIT);
        buffsize = Math.max(buffsize, 3000);

        mRecorder = new AudioRecord(AudioSource.MIC, 8000, AudioFormat.CHANNEL_IN_MONO,
                AudioFormat.ENCODING_PCM_16BIT, buffsize);

        mPlayer = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_OUT_MONO,
                AudioFormat.ENCODING_PCM_16BIT, 2 * buffsize, AudioTrack.MODE_STREAM);

        if (enableEcho) {
            AudioManager manager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            manager.setMode(AudioManager.MODE_IN_CALL);
            manager.setSpeakerphoneOn(true);
        }

        if (mRecorder.getState() != AudioRecord.STATE_INITIALIZED) {

            mRecorder = new AudioRecord(AudioSource.DEFAULT, 8000, AudioFormat.CHANNEL_IN_MONO,
                    AudioFormat.ENCODING_PCM_16BIT, buffsize);

            if (mRecorder.getState() != AudioRecord.STATE_INITIALIZED) {
                logEvent("Error - Could not initialise audio", true);
                return;
            }
            logEvent("Using default audio source", false);
        }

        mRecorder.startRecording();
        System.out.println("STARTING THREAD");
        Thread ct = new captureThread();
        logEvent("Starting Audio Thread.", false);
        setDecoderRunningNotification();
        ct.start();
    }
}

From source file:com.xperia64.timidityae.Globals.java

public static boolean initialize(final Activity a) {
    if (firstRun) {
        final File rootStorage = new File(
                Environment.getExternalStorageDirectory().getAbsolutePath() + "/TimidityAE/");
        if (!rootStorage.exists()) {
            rootStorage.mkdir();/*from w w w.  j ava 2 s  . c o m*/
        }
        File playlistDir = new File(rootStorage.getAbsolutePath() + "/playlists/");
        if (!playlistDir.exists()) {
            playlistDir.mkdir();
        }
        File tcfgDir = new File(rootStorage.getAbsolutePath() + "/timidity/");
        if (!tcfgDir.exists()) {
            tcfgDir.mkdir();
        }
        File sfDir = new File(rootStorage.getAbsolutePath() + "/soundfonts/");
        if (!sfDir.exists()) {
            sfDir.mkdir();
        }
        updateBuffers(updateRates());
        aRate = Integer.parseInt(prefs.getString("tplusRate",
                Integer.toString(AudioTrack.getNativeOutputSampleRate(AudioTrack.MODE_STREAM))));
        buff = Integer.parseInt(prefs.getString("tplusBuff", "192000")); // This is usually a safe number, but should probably do a test or something
        migrateFrom1X(rootStorage);
        final Editor eee = prefs.edit();
        firstRun = false;
        eee.putBoolean("tplusFirstRun", false);
        eee.putString("dataDir", Environment.getExternalStorageDirectory().getAbsolutePath() + "/TimidityAE/");
        if (new File(dataFolder + "/timidity/timidity.cfg").exists()) {
            if (manConfig = !cfgIsAuto(dataFolder + "/timidity/timidity.cfg")) {
                eee.putBoolean("manConfig", true);
            } else {
                eee.putBoolean("manConfig", false);
                ArrayList<String> soundfonts = new ArrayList<String>();
                FileInputStream fstream = null;
                try {
                    fstream = new FileInputStream(dataFolder + "/timidity/timidity.cfg");
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                // Get the object of DataInputStream
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                //Read File Line By Line
                try {
                    br.readLine(); // skip first line
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String line;
                try {
                    while ((line = br.readLine()) != null) {
                        if (line.indexOf("soundfont \"") >= 0 && line.lastIndexOf('"') >= 0) {
                            try {
                                String st = line.substring(line.indexOf("soundfont \"") + 11,
                                        line.lastIndexOf('"'));
                                soundfonts.add(st);
                            } catch (ArrayIndexOutOfBoundsException e1) {
                                e1.printStackTrace();
                            }

                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    eee.putString("tplusSoundfonts", ObjectSerializer.serialize(soundfonts));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            eee.commit();
            return true;
        } else {
            // Should probably check if 8rock11e exists no matter what
            eee.putBoolean("manConfig", false);

            AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {

                ProgressDialog pd;

                @Override
                protected void onPreExecute() {
                    pd = new ProgressDialog(a);
                    pd.setTitle(a.getResources().getString(R.string.extract));
                    pd.setMessage(a.getResources().getString(R.string.extract_sum));
                    pd.setCancelable(false);
                    pd.setIndeterminate(true);
                    pd.show();
                }

                @Override
                protected Void doInBackground(Void... arg0) {

                    if (extract8Rock(a) != 777) {
                        Toast.makeText(a, "Could not extrct default soundfont", Toast.LENGTH_SHORT).show();
                    }
                    return null;
                }

                @Override
                protected void onPostExecute(Void result) {
                    if (pd != null)
                        pd.dismiss();
                    ArrayList<String> tmpConfig = new ArrayList<String>();
                    tmpConfig.add(rootStorage.getAbsolutePath() + "/soundfonts/8Rock11e.sf2");
                    try {
                        eee.putString("tplusSoundfonts", ObjectSerializer.serialize(tmpConfig));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    eee.commit();
                    writeCfg(a, rootStorage.getAbsolutePath() + "/timidity/timidity.cfg", tmpConfig);
                    ((TimidityActivity) a).initCallback();
                }

            };
            task.execute((Void[]) null);
            return false;
        }

    } else {
        return true;
    }
}

From source file:com.cypress.cysmart.RDKEmulatorView.RemoteControlEmulatorFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    /**//from   ww  w  .j a  v a 2s .c  o  m
     * Getting the current orientation of the screen
     * Loading different view for LandScape and portrait
     */
    int currentOrientation = getResources().getConfiguration().orientation;
    if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
        mParentView = inflater.inflate(R.layout.rdk_emulator_view_landscape, container, false);
    } else {
        mParentView = inflater.inflate(R.layout.rdk_emulator_view_portrait, container, false);
    }
    mProgressDialog = new ProgressDialog(getActivity());
    /**
     * Getting the ID's of all Emulator view UI elements
     */
    Button mTrackpadView = (Button) mParentView.findViewById(R.id.trackpad_btn);
    Button mMicrophoneView = (Button) mParentView.findViewById(R.id.microphone_btn);
    mVolumePlusbtn = (ImageButton) mParentView.findViewById(R.id.volume_plus_btn);
    mVolumeMinusBtn = (ImageButton) mParentView.findViewById(R.id.volume_minus_btn);
    mChannelPlusBtn = (ImageButton) mParentView.findViewById(R.id.channel_plus_btn);
    mChannelMinusBtn = (ImageButton) mParentView.findViewById(R.id.channel_minus_btn);
    mLeftBtn = (ImageButton) mParentView.findViewById(R.id.left_btn);
    mRightBtn = (ImageButton) mParentView.findViewById(R.id.right_btn);
    mBackBtn = (ImageButton) mParentView.findViewById(R.id.back_btn);
    mGesturebtn = (ImageButton) mParentView.findViewById(R.id.gesture_btn);
    mExitBtn = (ImageButton) mParentView.findViewById(R.id.exit_btn);
    mPowerBtn = (ImageButton) mParentView.findViewById(R.id.power_btn);
    mRecBtn = (ImageButton) mParentView.findViewById(R.id.record_btn);
    mRecBtn = (ImageButton) mParentView.findViewById(R.id.record_btn);
    /**
     * AudioTrack class initialisation as follows
     *  streamType- AudioManager.STREAM_MUSIC,
     *  sampleRateInHz- 16000,
     *  channelConfig- AudioFormat.CHANNEL_OUT_MONO,
     *  audioFormat-AudioFormat.ENCODING_PCM_16BIT,
     *  bufferSizeInBytes-8000,
     *  mode- AudioTrack.MODE_STREAM
     *
     */
    mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO,
            AudioFormat.ENCODING_PCM_16BIT, BUFFER_SIZE, AudioTrack.MODE_STREAM);
    /**
     * TrackPAd button click listner
     */
    mTrackpadView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            TrackpadEmulatorFragment trackpadService = new TrackpadEmulatorFragment();
            try {
                displayView(trackpadService);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    });
    /**
     * Microphone Button click listner
     */
    mMicrophoneView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            MicrophoneEmulatorFragment microphoneService = new MicrophoneEmulatorFragment();
            microphoneService.create(mservice);
            displayView(microphoneService);
        }
    });
    return mParentView;
}

From source file:com.xperia64.timidityae.Globals.java

public static int[] updateRates() {
    if (prefs != null) {
        int[] values = Globals.validRates(prefs.getString("sdlChanValue", "2").equals("2"),
                /*prefs.getString("tplusBits", "16").equals("16")*/true);
        CharSequence[] hz = new CharSequence[values.length];
        CharSequence[] hzItems = new CharSequence[values.length];
        boolean validRate = false;
        for (int i = 0; i < values.length; i++) {
            hz[i] = Integer.toString(values[i]) + "Hz";
            hzItems[i] = Integer.toString(values[i]);
            if (prefs
                    .getString("tplusRate",
                            Integer.toString(AudioTrack.getNativeOutputSampleRate(AudioTrack.MODE_STREAM)))
                    .equals(hzItems[i])) {
                validRate = true;// w ww .  ja  v  a 2 s .  co  m
                break;
            }
        }

        if (!validRate)
            prefs.edit()
                    .putString("tplusRate",
                            Integer.toString(AudioTrack.getNativeOutputSampleRate(AudioTrack.MODE_STREAM)))
                    .commit();

        return values;
    }
    return null;
}

From source file:com.xperia64.timidityae.Globals.java

public static boolean updateBuffers(int[] rata) {
    if (rata != null) {
        SparseIntArray buffMap = Globals.validBuffers(rata, prefs.getString("sdlChanValue", "2").equals("2"),
                true/*prefs.getString("tplusBits", "16").equals("16")*/);
        int realMin = buffMap.get(Integer.parseInt(prefs.getString("tplusRate",
                Integer.toString(AudioTrack.getNativeOutputSampleRate(AudioTrack.MODE_STREAM)))));
        if (buff < realMin) {
            prefs.edit().putString("tplusBuff", Integer.toString(buff = realMin)).commit();
            return false;
        }/*  ww w .  j  a va 2s  .  co m*/
    }
    return true;
}