[Openal] Split wav channel
Andrea Mannarà
andrea.mannara at thesis.seac02.it
Wed Jul 29 06:09:17 PDT 2009
Ok it's working thanks a lot.
On Wed, Jul 29, 2009 at 12:53 PM, Daniel
PEACOCK<dpeacock at creativelabs.com> wrote:
> Hi Andrea,
>
> I think there are few issues here.
>
> 1. This routine ...
>
> 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];
> }
> }
>
> If you want this function to allocate memory when needed, then you will need
> to pass in pointers to pointers for the left and right buffers. In your code
> alDataL will still be NULL after calling the function - and you will leak
> memory. alDataR will actually be OK, because it is non-NULL on entry (it
> actually points to the stereo interleaved data, i.e same as pStereoData). I
> would expect you to get an AL error when doing alBufferData( bufferL,
> AL_FORMAT_MONO8, alDataL, size/4, freq ); because alDataL is NULL.
>
> 2. The alBufferData calls have 2 other problems ... AL_FORMAT_MONO8 should
> be replaced by AL_FORMAT_MONO16 (we didn't change the bit depth just the
> channel count - this is what is causing the random noise). Also the 4th
> parameter should be the number of bytes not samples - which in this case
> will be size / 2.
>
> Try those modifications and see what happens.
>
> Dan
> Creative Labs (UK) Ltd.
>
> ________________________________
> Notice
> The information in this message is confidential and may be legally
> privileged. It is intended solely for the addressee. Access to this message
> by anyone else is unauthorized. If you are not the intended recipient, any
> disclosure, copying or distribution of the message, or any action taken by
> you in reliance on it, is prohibited and may be unlawful. If you have
> received this message in error, please delete it and contact the sender
> immediately. Thank you.
>
> Creative Labs UK Ltd company number 2658256 registered in England and Wales
> at Belmont Road, Belmont Place, Maidenhead, Berkshire, SL6 6TB
>
> Andrea Mannarà <andrea.mannara at thesis.seac02.it>
>
>
> Andrea Mannarà <andrea.mannara at thesis.seac02.it>
> Sent by: openal-bounces at opensource.creative.com
>
> 07/29/2009 11:25 AM
>
> To
> openal at opensource.creative.com
> cc
>
> Subject
> [Openal] Split wav channel
> ---------- 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?
>
> _______________________________________________
> Openal mailing list
> Openal at opensource.creative.com
> http://opensource.creative.com/mailman/listinfo/openal
>
> ForwardSourceID:NT0006E2AE
>
More information about the Openal
mailing list