Audio Ouput with SDl

I was pointed in the direction of SDL fro audio output. I am doing an MSc
project looking at Ogg Vorbis. I am trying to amend one of the example
files given with the source code, decoder_example.c. As supplied it took
input from stdin and output raw PCM to stdout. I have changed the file so
that it takes input from an ogg file but am unsure how to get it to output
to the soundcard. I have tried using SDL but think what I have is a bit of
a mess. It does compile but doesn’t do anything. I think that I have
implemented the SDL calls and the callback function wrongly.

I am not a very experienced programmer so could you please keep all
instructions simple. I have attached my code to this email if somebody
could look at it and tell me how to fix it I would really appreciate it.

Thanks in advance,
Paul Newton

<<decoder_example.c>>
-------------- next part --------------
A non-text attachment was scrubbed…
Name: decoder_example.c
Type: application/octet-stream
Size: 12726 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20020815/77c10abb/attachment.obj

Hmm… I’m getting a seg fault in calls to fread()
within main()… looks like your ogg-vorbis reader
might not be as solid as you thought… ???

P.S. I did look over the rest of the code, and it did
look ok… (I’m not saying it’ll work out of the gate,
just that I didn’t see anything obvious wrong)… You
might want to mutex protect the audio_len and
audio_pos variables, but otherwise, it looked ok…

Hope that helps a little,

-Loren

— Paul Newton <Paul.Newton at sli-institute.ac.uk>
wrote:

I was pointed in the direction of SDL fro audio
output. I am doing an MSc
project looking at Ogg Vorbis. I am trying to amend
one of the example
files given with the source code, decoder_example.c.
As supplied it took
input from stdin and output raw PCM to stdout. I
have changed the file so
that it takes input from an ogg file but am unsure
how to get it to output
to the soundcard. I have tried using SDL but think
what I have is a bit of
a mess. It does compile but doesn’t do anything. I
think that I have
implemented the SDL calls and the callback function
wrongly.

I am not a very experienced programmer so could you
please keep all
instructions simple. I have attached my code to
this email if somebody
could look at it and tell me how to fix it I would
really appreciate it.

Thanks in advance,
Paul Newton

<<decoder_example.c>>

ATTACHMENT part 2 application/octet-stream
name=decoder_example.c__________________________________________________
Do You Yahoo!?
HotJobs - Search Thousands of New Jobs
http://www.hotjobs.com

I am not a very experienced programmer so could you please keep all
instructions simple. I have attached my code to this email if somebody
could look at it and tell me how to fix it I would really appreciate it.

Without looking at the rest of your code:

void fill_audio(void *udata, Uint8 stream, int len)
{
/
Only play if we have data left */
if ( audio_len == 0 )
return;

    /* Mix as much data as possible */
    len = ( len > audio_len ? audio_len : len );
    //SDL_MixAudio(stream, audio_pos, len, SDL_MIX_MAXVOLUME);
    audio_pos += len;
    audio_len -= len;

}

…you aren’t doing anything in the audio callback.

You need to write (len) bytes to the buffer pointed to by (stream) every
time this function is called.

Also, don’t mix against (stream) with SDL_MixAudio, since (I think) the
data in the stream is undefined until you put something there. Use
memcpy() instead. Write silence to the rest of the stream if you don’t
have enough data for it.

(This is like the fourth time I’ve answer this question in two days…does
someone have a good tutorial on SDL audio coding?)

–ryan.

Also, don’t mix against (stream) with SDL_MixAudio, since (I think) the
data in the stream is undefined until you put something there. Use
memcpy() instead. Write silence to the rest of the stream if you don’t
have enough data for it.

Actually, this is safe. The stream is zeroed so that SDL_MixAudio will
work (also so that stream chaining on BeOS will work as advertised).

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment

Actually, this is safe. The stream is zeroed so that SDL_MixAudio will
work (also so that stream chaining on BeOS will work as advertised).

BeOS aside, is that good policy to initial a whole block of memory that is
presumably going to be immediately overwritten by everything except buggy
code?

–ryan.

Actually, this is safe. The stream is zeroed so that SDL_MixAudio will
work (also so that stream chaining on BeOS will work as advertised).

BeOS aside, is that good policy to initial a whole block of memory that is
presumably going to be immediately overwritten by everything except buggy
code?

No, not really. :slight_smile:

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment

I commented that line as I didn’t know if it was neceassary as I thought
that mix was for putting two different sound files together.

Would it be possible for someone to ammend my code so that it played a
vorbis file please. I have been trying to get it to output sound for the
past two days and I am getting nowhere.

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

From: Ryan C. Gordon [mailto:icculus@icculus.org]
Sent: 15 August 2002 16:02
To: sdl at libsdl.org
Subject: Re: [SDL] Audio Ouput with SDl

I am not a very experienced programmer so could you please keep all
instructions simple. I have attached my code to this email if somebody
could look at it and tell me how to fix it I would really appreciate it.

Without looking at the rest of your code:

void fill_audio(void *udata, Uint8 stream, int len)
{
/
Only play if we have data left */
if ( audio_len == 0 )
return;

    /* Mix as much data as possible */
    len = ( len > audio_len ? audio_len : len );
    //SDL_MixAudio(stream, audio_pos, len, SDL_MIX_MAXVOLUME);
    audio_pos += len;
    audio_len -= len;

}

…you aren’t doing anything in the audio callback.

You need to write (len) bytes to the buffer pointed to by (stream) every
time this function is called.

Also, don’t mix against (stream) with SDL_MixAudio, since (I think) the
data in the stream is undefined until you put something there. Use
memcpy() instead. Write silence to the rest of the stream if you don’t
have enough data for it.

(This is like the fourth time I’ve answer this question in two days…does
someone have a good tutorial on SDL audio coding?)

–ryan.


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

I commented that line as I didn’t know if it was neceassary as I thought
that mix was for putting two different sound files together.

It is.

Since you aren’t mixing two sounds, just use memcpy:

     /* Mix as much data as possible */
     len = ( len > audio_len ? audio_len : len );
     memcpy(stream, audio_pos, len);
     audio_pos += len;
     audio_len -= len;

Again, I haven’t looked at the rest.

–ryan.

OK i have used the memcpy commmand and am getting some output but it is very
jerky, as if file is being played very fast. Could somebody check my code
please?

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

From: Ryan C. Gordon [mailto:icculus@icculus.org]
Sent: 16 August 2002 14:25
To: sdl at libsdl.org
Subject: RE: [SDL] Audio Ouput with SDl

I commented that line as I didn’t know if it was neceassary as I thought
that mix was for putting two different sound files together.

It is.

Since you aren’t mixing two sounds, just use memcpy:

     /* Mix as much data as possible */
     len = ( len > audio_len ? audio_len : len );
     memcpy(stream, audio_pos, len);
     audio_pos += len;
     audio_len -= len;

Again, I haven’t looked at the rest.

–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: 12766 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20020819/d4cda3e8/attachment.obj