[SDL] Audio Output
Paul Newton
Paul.Newton at sli-institute.ac.uk
Thu Aug 22 03:47:01 PDT 2002
I have had a look at SDL_sound. I do need the decoded waveform. I am
still lost though so I would really appreciate some pointers. In the code
that I had instead of outputting the pcm data to std out using
fwrite(convbuffer,2*vi.channels,bout,outputFile);
I want to output this pcm data to the soundcard.
My understanding is that the pcm data is contained in convbuffer. This is
declared as an array of 4096 elements. I have created a 2D array to act as
a buffer. This is declared as
ogg_int16_t audioBuffer[BUFFERSIZE][4096];
BUFFERSIZE is #define BUFFERSIZE 20
I have setup a SDL_AudioSpec structure
SDL_AudioSpec fmt; //SDL structure definition
For the moment I have set this up as follows
/************************SDL Play**********************************/
/* Set 16-bit stereo audio at 44Khz */
fmt.freq = 44100;
fmt.format = AUDIO_S16SYS;
fmt.channels = 2;
fmt.samples = 4096;
fmt.callback = fill_audio;
fmt.userdata = NULL;
//audio_chunk = &convbuffer;
/* Open the audio device and start playing sound! */
if ( SDL_OpenAudio(&fmt, NULL) < 0 ) {
fprintf(stderr, "Unable to open audio: %s\n", SDL_GetError());
//exit(1);
}
/************************SDL Play**********************************/
I have two variable acting as pointers on my buffer these are
int audioPointer=0;
int dataPointer=BUFFERSIZE/2;
In the decode loop I have th following condition to see whether or not to
decode the next part of the file
if(
(dataPointer >= BUFFERSIZE/2 && audioPointer < BUFFERSIZE/2)
||
(dataPointer < BUFFERSIZE/2 && audioPointer >=
BUFFERSIZE/2)
)
I put this in because I found that the buffer was being filled with data too
quickly and that by the time that the audio got read the data had been
overwritten a few times. This caused the file to produce high frequency
noise which didn't last anywhere near the time of the ogg file.
Instead of writing the pcm data to stdout I do this
/****************Play Audio*************************************/
memcpy(audioBuffer[dataPointer], convbuffer, 1024/*sizeof(convbuffer)*/);
dataPointer = (dataPointer+1)%BUFFERSIZE;
fprintf(stderr, "Data\n");
fprintf(stderr, "Frame %ld\n", (long)(vd.sequence));
//SDL_PauseAudio(0);
/****************Play Audio*************************************/
I have the SDL_PauseAudio(0); at the end of my if condition which contains
the whole of the looped decode part.
My callback function is declared as
/*******************callback function*******************************/
/* The audio function callback takes the following parameters:
stream: A pointer to the audio buffer to be filled
len: The length (in bytes) of the audio buffer
*/
void fill_audio(void *udata, Uint8 *stream, int len)
{
memcpy(stream, audioBuffer[audioPointer], len);
fprintf(stderr, "Audio\n");
audioPointer = (audioPointer+1)%BUFFERSIZE;
}
/*******************callback function*******************************/
The sound output now appaers to go too slowly and is not recognisable as the
ogg file.
Any help would be really appreciated.
I have included the code as an attachment
The ogg file I am using is 841kb in size so if anybody wants that I can send
that to them
Thanks in advance,
Paul
-----Original Message-----
From: Ryan C. Gordon [mailto:icculus at clutteredmind.org]
Sent: 22 August 2002 10:29
To: sdl at libsdl.org
Subject: Re: [SDL] Audio Output
> The goal is simply to get sound output. The project is basically to
develop
> a GUI for this Vorbis. One of my colleagues is trasferring part of the
> Vorbis algorithm to hardwaer and we intend to run that with the hardware
as
> well. First of all though I need to get th e GUI working with the
existing
> software. My plan was just to use the decoder_example code when the user
> hit play. If there is a simper way to get audio output then I would like
to
> know about it. If not then I need a solution as to why what I had was not
> correctly outputting sound.
If you need the decoded audio waveform, look at SDL_sound:
http://icculus.org/SDL_sound/
And the included playsound program that dumps a decoded Ogg file to an
SDL audio callback.
If you don't need the decoded waveform, and only want to play one Ogg at a
time, SDL_mixer makes this a ten-line-or-less program. :)
--ryan.
_______________________________________________
SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl
-------------- next part --------------
A non-text attachment was scrubbed...
Name: decoder_example.c
Type: application/octet-stream
Size: 13133 bytes
Desc: not available
URL: <http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20020822/2199b138/attachment.obj>
More information about the SDL
mailing list