Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play stereo tone in android

Similar topics on this question include only playing tone in mono where the left and right frequencies are the same.

My question is: how to generate a stereo tone such that the left channel has a different frequency than the right channel?

I thought of pre-recording .wav files but recording many .wav files and putting it on res folder is not a good idea.

i come across the SoundPool and AudioTrack class but I need a snippet showing the different frequencies of the left and right channel stored as buffer before playing the tone.

Or is there other ways? Please provide the desired snippet.

like image 945
Neigyl R. Noval Avatar asked Dec 12 '11 17:12

Neigyl R. Noval


2 Answers

I have never tried this:

  1. Initialize your left and right frequencies
    //playback rate (1.0 = normal playback, range 0.5 to 2.0)
    float lFrequency = 1.0;
    float rFrequency = 1.0;

  2. Initialize a SoundPool object
    SoundPool sp = SoundPool(2, AudioManager.STREAM_MUSIC, 0);

  3. Load your track twice (load function)
    int sLeft = sp.load(mContext, R.raw.yourAudioFileId, 1);
    int sRight = sp.load(mContext, R.raw.yourAudioFileId, 1);

  4. Play the 2 sounds (one on Left and one on Right) using different rates (play function)
    sp.play (sLeft, 1.0, 0.0, 0, 0, lFrequency);
    sp.play (sRight, 0.0, 1.0, 0, 0, rFrequency);

like image 196
Sherif elKhatib Avatar answered Sep 22 '22 03:09

Sherif elKhatib


From my understanding, if you use SoundPool it will play in stereo without any special configuration. From the documentation: "The SoundPool library uses the MediaPlayer service to decode the audio into a raw 16-bit PCM mono or stereo stream" so as long as the file you play is in Stereo to begin with, it should play that way.

like image 25
Wookie1120 Avatar answered Sep 20 '22 03:09

Wookie1120