7

First, I need to generate 2 sine wave tones on the fly which are of same frequency, but opposite phases and play those back separately into right and left channel in stereo mode on Android. The playback needs to be perfectly in sync so that the sines of left and right channel are "mirrored" (when left channel has, say sample value of 120 the right channel should have -120).

The thing is that I have not found any evidence how this kind of setup would work. Is there a possibility to feed 2 separate tones/samplebuffers to AudioTrack to be played back in left and right channel separately and simultaneously? If not, any other solutions to achieve the end result are much appreciated.

I guess one option would be to use pre-generated stereo wave files and stream those with AudioTrack, but this seems too inflexible for the solution in the works. At the same time, if AudioTrack is able to play back these pre-recorded audio files in "real" stereo mode I would expect the same to be possible with generated sounds as well.

Ry-
  • 218,210
  • 55
  • 464
  • 476
rar
  • 71
  • 1
  • 1
  • 2
  • 2
    Thanks to niko20 in android developers user group: "You dont need a separate buffer for right and left. Stereo audio is interleaved. To ouput stereo you just fill the buffer with left-right- left-right". This is actually working well, tested with AudioTrack – rar Jan 10 '12 at 21:51

2 Answers2

5

AudioTrack has the same method as SoundPool. It call

AudioTrack.setStereoVolume();

If you want to play left/right channel at the same time with different data. You should handle data to meet this target. If you select stereo mode with 16Bit PCM, the data is one byte for left and one byte for right. For example:

left channel want to play: 12, 23, 34, 45

right channel want to play: 54, 43, 32, 21 at the same time with left channel.

data should be generated as: {12, 54, 23, 43, 34, 32, 45, 21}

Then, use AudioTrack.write(data). It will perfectly meet you target.

Fakebear
  • 449
  • 6
  • 15
  • Is this correct? Do I have to put one element of each channel interchanged along the output buffer? – the_moon Apr 14 '14 at 23:05
  • I'm trying to do single channel for mono audio but I've found it is now done by public int setVolume (float gain) Added in API level 21 Sets the specified output gain value on all channels of this track. This API is preferred over setStereoVolume(float, float), as it more gracefully scales down to mono, and up to multi-channel content beyond stereo. So how would I controll it now? – VMMF Apr 28 '16 at 03:22
0

Ok this may work as there are only two samples.

First check the SoundPool out. It is useful when you have tracks that need good performance as they are loaded into memory completely. This does however have its drawbacks the major one being the limit to the size of the tracks being stored in memory. When I first used this I tried putting 2 x 4Mb files into a stream - this was not a good idea.

SoundPool has this function:

setVolume(int streamID, float leftVolume, float rightVolume)

Notice how you can set the volume for left and right for each stream? For stream 1 you can have a maxed out left volume and then for stream 2 a maxed out right volume.

I would setup the streams with the files to play and the volume as desired and then play them back.

A simple tutorial can be found at http://www.vogella.de/articles/AndroidMedia/article.html

pjanssen
  • 1,065
  • 13
  • 35
Graham Smith
  • 25,627
  • 10
  • 46
  • 69
  • OK I understand what you mean, but SoundPool unfortunately does not help me much as I need to generate the sounds in my code and use AudioTrack and MODE_STREAM in order to feed the playing buffer on the fly. AudioTrack supports CHANNEL_OUT_MONO and CHANNEL_OUT_STEREO configurations, but I have not found an option to actually feed different code-generated sounds to left and right channel. – rar Jan 09 '12 at 11:36