[sdlsound] Writing decoded audio to stdout

Ryan C. Gordon icculus at clutteredmind.org
Wed Feb 9 16:26:57 EST 2005

>     if (sample->flags & SOUND_SAMPLEFLAG_ERROR)

You don't have to check ERROR here...you won't get a new Sound_Sample 
with an error state...this flag should be checked after Sound_Decode(), 
if you didn't get as many bytes as you requested (in which case it's 
either an ERROR or an EOF).

>     Uint8 stream1[decode_buffersize];
>     while (bw < decode_buffersize)

This is a bug. You decode enough to fill a single buffer and then quit.

This looks like you cut-and-pasted from playsound.c, which is probably 
over-complicated for what you want to do...besides all the extra crap it 
does, it also has to account for having to feed the audio device 
incrementally over time from a buffer that might have more or less data 
available...when going to disk or a pipe, you can just dump as fast as 
you can without tapdancing.

  const char *fname = p.string().c_str();
  Sound_Sample * sample = Sound_NewSampleFromFile (fname, NULL, 1024*64);
  if (!sample) {
      cerr << "Error opening " << fname << ": " << Sound_GetError();

  do {
      Uint32 br = Sound_Decode(sample);
      if (br > 0)  // write this block of decoded data to stdout.
          cout.write((const char *) sample->buffer, sample->buffer_size);

      if (sample->flags & SOUND_SAMPLEFLAG_ERROR) {
          cerr << "Error decoding " << fname << ": " << Sound_GetError();
  } while (sound->flags & SOUND_SAMPLEFLAG_EOF);



More information about the sdlsound mailing list