Ok I have overhauled my code.
My question now is
In the audio callback function, stream is an array of Uint 8. I presume
this is an array of unsigned char.
My problem is that the data I have is of type ogg_int16_t which are signed
short. I have set the format part of the SDL_audioSpec variable to
AUDIO_S16. I have set up my pointers to work with bytes rather than shorts.
Could someone please have a look at my code to see what the problem is?
My full code is attached
Thanks
Paul
Variable declarations
ogg_int16_t convbuffer[4096]; /* take 4k out of the data segment, not the
stack */
int convsize=4096;
ogg_int16_t audioBuffer[BUFFERSIZE];
int audioPosition=0;
int dataPosition=0;
int amountToAddToBuf=0;
int amountInBuf=0;
unsigned int n=BUFFERSIZE;
char flag =0;
char *bufferPointer;
char *convPointer;
My callback function is
void fill_audio(void *udata, Uint8 *stream, int len)
{
int x=0;
int size1=0;
int size2=0;
char *audioPointer;
//Number of bytes currently in the buffer
x = amountInBuf;
//fprintf(stderr, “x %d\n”, x);
if(len < x)
x = len;
size1 = (2*BUFFERSIZE) - audioPosition;
//fprintf(stderr, “size1 %d\n”, size1);
if(size1 < x) //we have to wrap around if true
{
size2 = x - size1;
//fprintf(stderr, “size2 %d\n”, size2);
audioPointer = audioBuffer;
audioPointer += audioPosition;
memcpy(stream, audioPointer, size1);
audioPointer = audioBuffer;
memcpy(stream+size1, audioPointer, size2);
audioPosition = size2;
//fprintf(stderr, “audioPosition in audio %d\n”, audioPosition);
}
else
{
audioPointer = audioBuffer;
audioPointer += audioPosition;
memcpy(stream, audioPointer, x);
audioPosition += x;
//fprintf(stderr, “audioPosition in audio 2 %d\n”, audioPosition);
}
amountInBuf -= x;
// fprintf(stderr, “Amount in Buf audio %d\n”, amountInBuf);
len -= x;
//fprintf(stderr, “len = %d\n”, len);
if(len)
memset(stream + x, ‘\0’, len); //write silence
}
The part where I copy this to my audio buffer is
fprintf(stderr, “Frame = %d\n”, vd.sequence);
fprintf(outputFile, “Data\n”);
fprintf(stderr, “bout = %d\n”, bout);
//Number of bytes to add to buffer. Multiply 2 and by
//number of channels as each sample has 16 bit value
//for each channel
amountToAddToBuf = bout*vi.channels*2;
fprintf(stderr, "amountToAddToBuf = %d\n",
amountToAddToBuf);
SDL_LockAudio();
//Room left in the buffer in bytes
n = (2*BUFFERSIZE) - amountInBuf;
fprintf(stderr, "n = %d\n", n);
if (n < amountToAddToBuf)
amountToAddToBuf = n;
//Consecutive space from current position to end of buffer
length1 = (2*BUFFERSIZE) - dataPosition;
//fprintf(stderr, "length1 = %d\n", length1);
if(length1 < amountToAddToBuf) // we need to wraparound if true
{
//Set pointer to start of convbuffer
convPointer = convbuffer;
//Calculate amount for bottom of buffer
length2 = amountToAddToBuf - length1;
//Copy to top of buffer
memcpy(bufferPointer, convPointer, length1);
//Move to correct part of convbuffer for second copy
convPointer += length1;
//Set bufferPointer to bottom of array
bufferPointer = audioBuffer;
//Copy data to bottom of array
memcpy(bufferPointer, convPointer, length2);
//Update the bufferPointer position
bufferPointer += length2;
//Update the data position
dataPosition = length2;
//fprintf(stderr, "dataPointer %d\n", dataPosition);
amountInBuf += (length1 + length2);
//fprintf(stderr, "Amount in Buf A %d\n\n",
amountInBuf);
}
else //enough space for all the data consecutively
{
//Copy data to audioBuffer
memcpy(bufferPointer, convbuffer, amountToAddToBuf);
//Update the bufferPointer position
bufferPointer += amountToAddToBuf;
//Update the dataPostion
dataPosition += amountToAddToBuf;
//fprintf(stderr, "dataPosition = %d\n", dataPosition);
amountInBuf += amountToAddToBuf;
//fprintf(stderr, "Amount in Buf B %d\n\n", amountInBuf);
}
//fprintf(stderr, "dataPosition %d\n", dataPosition);
//fprintf(stderr, "audioPosition %d\n", audioPosition);
SDL_UnlockAudio();
/*}*/> ----- Original Message -----
From: Paul Newton [mailto:Paul.Newton@sli-institute.ac.uk]
Sent: 29 August 2002 18:52
To: 'sdl at libsdl.org’
Subject: RE: [SDL] Audio OutputOK I have changed my buffers but am back to the clicking sound output again.
Could somebody have a look at the code and see what is wrong with it.
Thanks,
Paul