[Openal] Split wav channel
Andrea Mannarà
andrea.mannara at thesis.seac02.it
Wed Jul 29 03:25:18 PDT 2009
---------- Forwarded message ----------
From: Huvber <huvber at gmail.com>
Date: Wed, Jul 29, 2009 at 12:23 PM
Subject: Re: [Openal] Split wav channel
To: Daniel PEACOCK <dpeacock at creativelabs.com>
Cc: openal at opensource.creative.com
On Tue, Jul 28, 2009 at 5:52 PM, Daniel PEACOCK
<dpeacock at creativelabs.com> wrote:
>
> Hi,
>
> Stereo wave files have interleaved data i.e a Left Channel Sample followed by a Right Channel Sample followed by Left, Right etc ... If you want to split the file into two monos then you would do something like this (assuming 16bit samples and that the pointers are all valid and point to enough storage space, etc ...)
>
> ALvoid SplitStereo(ALshort *pStereoData, ALshort *pLeft, ALshort *pRight, ALuint uiNumSamples)
> {
> ALuint i;
>
> for (i = 0; i < uiNumSamples; i++)
> {
> pLeft[ i ] = pStereoData[i*2];
> pRight[ i ] = pStereoData[i*2+1];
> }
> }
>
> If you just want to create a single mono stream from a stereo file, then you need to mix the Left and Right samples together (and watch out for overflows).
>
> ALvoid StereoToMono(ALshort *pStereoData, ALshort *pMonoData, ALuint uiNumSamples)
> {
> ALuint i;
> ALint result;
>
> for (i = 0; i< uiNumSamples; i++)
> {
> result = (ALint)pStereoData[i*2] + (ALint)pStereoData[i*2+1];
> if (result > 32767)
> result = 32767;
> else if (result < -32768)
> result = -32768;
> pMonoData[i] = (ALshort)result;
> }
> }
>
> NOTE : uiNumSamples is the number of 'blocks' or 'frames' and not the count of all the samples in all the channels. e.g. For 16bit stereo data, uiNumSamples = DataSizeInBytes / 4. (divide by 2 because it is 16bit shorts, and divide by 2 again because there are 2 channels).
>
> If you intend to play both mono parts of the stereo file then you should use the vector play call (alSourcePlayv) to keep the parts in sync otherwise you might hear some strange artifacts.
>
> Dan
> Creative Labs (UK) Ltd.
Hi,
Thanks a lot for your help, but I'm still in trouble. I post my code:
ALvoid SplitStereo(ALshort *pStereoData, ALshort *pLeft, ALshort
*pRight, ALuint uiNumSamples)
{
ALuint i;
if(pLeft == NULL)
pLeft = (ALshort *) malloc(uiNumSamples*2);
if(pRight == NULL)
pRight = (ALshort *) malloc(uiNumSamples*2);
for (i = 0; i < uiNumSamples; i++)
{
pLeft[ i ] = pStereoData[i*2];
pRight[ i ] = pStereoData[i*2+1];
}
}
ALenum format;
ALsizei size;
ALfloat freq;
ALboolean loop;
ALuint bufferL;
ALuint bufferR;
ALshort *alDataR=NULL;
ALshort *alDataL = NULL
alDataR =(ALshort *) alutLoadMemoryFromFile ((ALbyte*)&(getUrl(i))[0],
&format,
&size,
&freq);
if( alDataR )
{
if(format == AL_FORMAT_STEREO16)
{
SplitStereo((ALshort*)alDataR,(ALshort*)alDataL,(ALshort*)alDataR,
size/4);
alGenBuffers(1, &bufferL);
alGenBuffers(1, &bufferR);
alBufferData( bufferL, AL_FORMAT_MONO8, alDataL, size/4, freq );
alBufferData( bufferR, AL_FORMAT_MONO8, alDataR, size/4, freq );
}else{
alGenBuffers( 1, &bufferR );
alBufferData( bufferR, format, alDataR, size, freq );
setStorage_id( bufferR);
setL(0);
}
alSourceQueueBuffers( soundId1 , 1, (ALuint*)&(bufferR) );
alSourceQueueBuffers( soundId2 , 1, (ALuint*)&(bufferL) );
alSourcePlay( soundId1 );
alSourcePlay( soundId2 );
}
if the wav is in mono i haven't problem. Otherwise if i use a wav
stereo i have a noise similar to white noise.
Why?
More information about the Openal
mailing list