[Re] Audio Output

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 Output

OK 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

Full code listing attached
<<decoder_example.c>>
-------------- next part --------------
A non-text attachment was scrubbed…
Name: decoder_example.c
Type: application/octet-stream
Size: 16409 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20020830/0d7e7b4b/attachment.obj

When I do a printf for the size of len (right at the start of callback
before I do anything to len) I get the value 44112. However when I do a
memcpy I copy the length of data that is in my buffer. This is always a lot
less than 44112. Would this cause me to get jerky sound playback that is
playing too slow. I am using Visual C++. I have been told that len should
be 8192.

Some help please.

Thanks,
Paul> ----- Original Message -----

From: Paul Newton [mailto:Paul.Newton@sli-institute.ac.uk]
Sent: 30 August 2002 11:55
To: 'sdl at libsdl.org
Subject: [SDL] [Re] Audio Output

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 Output

OK 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


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

I have finally got this to work.

Thanks for all your help,
Paul> ----- Original Message -----

From: Paul Newton [mailto:Paul.Newton@sli-institute.ac.uk]
Sent: 30 August 2002 13:54
To: 'sdl at libsdl.org
Subject: RE: [SDL] [Re] Audio Output

When I do a printf for the size of len (right at the start of callback
before I do anything to len) I get the value 44112. However when I do a
memcpy I copy the length of data that is in my buffer. This is always a lot
less than 44112. Would this cause me to get jerky sound playback that is
playing too slow. I am using Visual C++. I have been told that len should
be 8192.

Some help please.

Thanks,
Paul

-----Original Message-----
From: Paul Newton [mailto:Paul.Newton@sli-institute.ac.uk]
Sent: 30 August 2002 11:55
To: 'sdl at libsdl.org
Subject: [SDL] [Re] Audio Output

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 Output

OK 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


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

I have got my code working with SDL audio. I am using Visual C++ 6.0

The sound output is fine when I have a C program (decoder_example.c from Ogg
Vorbis web site). Howver when I use the code in a GUI which I am working on
(using the Microsoft Foundation Classes) I get a lot of unwanted
interference in the playback. Does anybody know why this should be the
case. I can send someone the workspace if they want to investigate the
code.

Thanks,
Paul

Hi

I’m also presently trying to play an Ogg file with SDL on Win32. If you
still have that workspace, I’d like to have it please.

Thanks
Alexandre Leduc

Paul Newton wrote:> I have got my code working with SDL audio. I am using Visual C++ 6.0

The sound output is fine when I have a C program (decoder_example.c from Ogg
Vorbis web site). Howver when I use the code in a GUI which I am working on
(using the Microsoft Foundation Classes) I get a lot of unwanted
interference in the playback. Does anybody know why this should be the
case. I can send someone the workspace if they want to investigate the
code.

Thanks,
Paul

Although I can’t help you with the MFC stuff. I’m just curious about the
code that makes the OGG files play.

Alexandre Leduc

Alex Leduc wrote:> Hi

I’m also presently trying to play an Ogg file with SDL on Win32. If you
still have that workspace, I’d like to have it please.

Thanks
Alexandre Leduc

Paul Newton wrote:

I have got my code working with SDL audio. I am using Visual C++ 6.0

The sound output is fine when I have a C program (decoder_example.c
from Ogg
Vorbis web site). Howver when I use the code in a GUI which I am
working on
(using the Microsoft Foundation Classes) I get a lot of unwanted
interference in the playback. Does anybody know why this should be the
case. I can send someone the workspace if they want to investigate the
code.

Thanks,
Paul