View Full Version : stereo to 2 mono channels, can it be done?

21-02-2008, 06:12 PM
Is it possible to 'demix' a stereo sound buffer to 2 mono channels? That way i could play a streaming stereo .ogg file via 2 virtual speakers in a 3d world. Is there a math trick to get 2 mono channels from the mixed stereo signal? Or is it just impossible.

21-02-2008, 06:41 PM
That'd be awesome. :D

I think it's possible. You have to convert your audio to PCM WAV, and then you have to split up the data. I'm not sure but i think you do it like this:
When you have a 16 bit stereo WAV buffer, you read the first 16 bits and put those in a 16 bit mono buffer. the next 16 bits should be moved to the second mono buffer, the next 2bytes to the first buffer and so on... keep alternating between the two buffers.

I think that'll do the trick. If it works I would love to have the code. :razz: Are you planning to put it on your site?

I googled for it and i think this page (http://www.neurophys.wisc.edu/auditory/riff-format.txt) confirms my assumption. :)

Good luck! ;)

21-02-2008, 06:48 PM
I found this page (http://www.dsprelated.com/showmessage/21294/1.php) too.

21-02-2008, 09:41 PM
Ok i have a first result:

for i:=0 to BufferSize div 2 do
if ( l= true ) then
PByteArray(dataR)^[iR+1]:=PByteArray(data)^[i*2+1] ;
PByteArray(dataL)^[iL+1]:=PByteArray(data)^[i*2+1] ;

alBufferData(Buffer, Format, dataR, BufferSize div 2, FRate);

So to get this further to work i need to have 2 sources with double buffering. Also note the div 2 for the buffersize. And yes the code needs much cleaning up.

I now need an .ogg or .mp3 file with heareable stereo effect to properly test it. My current test files are not suiteable. Does anyone here have such a thing lying around? I have not.

23-02-2008, 12:10 PM
Here is an finished example: http://www.noeska.net/downloads/oggstream.zip .

Try experimenting with the L and R source positions. You need the ogg player example for ogg vorbis units and ogg vorbis dlls. I used .ogg as i found an .ogg file with heareable stereo effect.

23-02-2008, 04:53 PM
Thanks for the code. It seems to work. :)

However i don't find the results satisfactory. I've tried some dance music and there is still a little delay between the two channels. What kind of music did you try? Did you notice any delays? The delay isn't very big (i guess 20ms or so) but it is still noticable. I tried fixing it but i do not have much experience with threading etc.. so i got stuck.

maybe you can test some dance music? Do you think there is any way to solve this? Can we sync the buffers in some way?

23-02-2008, 06:20 PM
I haven been testing with some ryan addams music: http://www.archive.org/details/ra2008-01-22.ra2008-01-22.flac16.sbd
And a whoosh sound effect file.
I also noticed a difference in sound but i did not relate i to a delay in sound. If you turn of either left or right source you will also hear a part of the other source somewhat softer on the other side. I blamed it on that but yours is also a reason.
Unfortunately you can only fill the buffers of one source at one time.

Possible fix: advance source one so it in sync with source 2.
Possible fix2: alSourcePlayv(2,@Sources);

23-02-2008, 07:52 PM
I just tried possible fix 2 and i cannot directly tell whether it's an improvement or not. :? I'm not sure what the code for possible fix 1 should look like.

You should try some dance music (if you have some). The bass-drum should be one clear kick, but it isn't. It's echooing and it all sounds a bit strange.

If you like, i can mail you the ogg i use, so you can hear for yourself (plz PM me your mail-adres). :)

24-02-2008, 10:48 AM
I think the echo you are refering to is in not even caused by not playing sources in sync or not feeding the buffers correctly, but by OpenAL spatializing the sound. E.g. you get feeback from the left channel on the right and the other way. I hear the effect also in the applause in the Ryan Addams ogg files.

The theory with advancing one source is that if one source is 20ms ahead you should ffwd the other source by 20ms or slow down the other source. Also when feeding data to the buffer you could skip the first 20ms of data for the buffer that is behind. But i did not get good results with either method.

For the spatializing, we can make that partially good be setting up the OpenAL environment.

alDistanceModel( AL_INVERSE_DISTANCE );
(Yes there is a nasty typo bug in the openal.pas again.)
And set up sources like:

alSource3f(sourceL, AL_POSITION, -5.5, 0, 0.0);
alSource3f(sourceL, AL_VELOCITY, 0, 0, 0);
alSource3f(sourceL, AL_DIRECTION, 0, 0, 0);
alSourcef(sourceL, AL_ROLLOFF_FACTOR, 1.5);

alSource3f(sourceR, AL_POSITION, 5.5, 0, 0.0);
alSource3f(sourceR, AL_VELOCITY, 0, 0, 0);
alSource3f(sourceR, AL_DIRECTION, 0, 0, 0);
alSourcef(sourceR, AL_ROLLOFF_FACTOR, 1.5);

It is not correct with this, but you can use this to control it a bit.

24-02-2008, 12:46 PM
I think it's a little improvement but i still hear the bass-drum echooing. :(

but by OpenAL spatializing the sound. E.g.

Can you explain it a little more? Is it caused by the time taken by OpenAL to process the buffer for spatializing?

24-02-2008, 02:15 PM
OpenAl is simulating a 3d space where the sources are located within. So when playing something to the right of the listener it is also heard to the left of the listener (although somewhat softer and maybe a bit later) (remember that when lightning strikes the sound comes later).

I have another question. I added an eax effect. But i cannot hear it. I blame it on the realtek soundcard inside my laptop. Can someone here try the latest source?

http://www.noeska.net/downloads/oggstream.zip .

24-02-2008, 03:56 PM
Found out why eax does not work. It does not work on realtek using generic hardware. So i had to use generic software instead.

@chronozphere what audio hardware are you using?

24-02-2008, 06:15 PM
My motherboard includes a SoundMax chip, which delivers basic sound features but not much.

Since i have a MIDI-keyboard and want to get into audio-design, i bought a better soundcard: SoundBlaster Audigy SE. I know it's not the best card out there, but it supports 5.1 surround on vista, and it does more than an average onboard sound-device does.

One day, when i have more money, i'll buy a SoundBlaster X-fi XtremeMusic. It has ASIO 2.0 and MANY advanced options. :razz: