Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play left and right channel separately with AudioTrack?

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.

like image 697
rar Avatar asked Jan 08 '12 23:01

rar


People also ask

How do you separate audio channels in audition?

Separate all channels While dragging a file or selection of clips onto an audio track in a multitrack session, hold down the Alt (Windows) or Option (macOS) key to separate them into separate channels for nonstandardized formats or grouped channels for formats like 5.1.


1 Answers

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.

like image 65
Fakebear Avatar answered Sep 28 '22 14:09

Fakebear