[sdlsound] Writing decoded audio to stdout

Kevin Nowaczyk beakerboy99 at yahoo.com
Tue Feb 15 22:00:14 EST 2005


Ryan,
Thanks for the code! I transfered it in and it kind of
works. There are things which seem to not be working
as I'd expect.  When I redirect stdout to the audio
device I hear static.  The other thing is if I
redirect stdout to a file, the file is always the size
of one buffer.  It seems like the the audio sample is
never refreshed.  I've tried to get some help from the
Doxygen API refernece on the website, but I'm new to
the world of SDL and had a hard time following what
needed to be done.  

I've included a small c++ file with your code.  If you
compile and run it with an audiofile name as a
commandline parameter and redirect stdout to either
audio or a file, you should see what I'm talking
about.

#include <SDL_sound.h>
#include <iostream>

class AudioPlayer
{
  public:
    AudioPlayer()
      {
        Sound_Init();
      }

    ~AudioPlayer()
      {
        Sound_Quit();
      }

    void play (char*);
};

void AudioPlayer::play (char* fname)
  {
    Sound_Sample* sample = Sound_NewSampleFromFile
(fname, NULL, 1024*64);
    if (!sample) 
      {
        std::cerr << "Error opening " << fname << ": "
<< Sound_GetError () << std::endl; 
        return;
      }
    do 
      {
        Uint32 br = Sound_Decode(sample);
        if (br > 0)  // write this block of decoded
data to stdout.
          std::cout.write((const char *)
sample->buffer, sample->buffer_size);
        if (sample->flags & SOUND_SAMPLEFLAG_ERROR) 
          {
            std::cerr << "Error decoding " << fname <<
": " << Sound_GetError() << std::endl;
            break;
          }
      } while (sample->flags & SOUND_SAMPLEFLAG_EOF);
    
    Sound_FreeSample(sample);
  }

main(int argc,char** argv)
{
  if (argc == 2)
    {
      char* filename = argv[1];
      AudioPlayer player;
      player.play(filename);

    }
  else
   {
     std::cout << "please provide a filename" <<
std::endl;
   }

}


Thanks for any help you can give me,
Kevin Nowaczyk

--- "Ryan C. Gordon" <icculus at clutteredmind.org>
wrote:

> 
> >     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();
>       return;
>   }
> 
>   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();
>           break;
>       }
>   } while (sound->flags & SOUND_SAMPLEFLAG_EOF);
> 
>   Sound_FreeSample(sample);
> 
> --ryan.
> 
> 


=====
Manage your PEZ collection online.  Visit PezBase at http://pezbase.beakerboy.com


		
__________________________________ 
Do you Yahoo!? 
Yahoo! Mail - Easier than ever with enhanced search. Learn more.
http://info.mail.yahoo.com/mail_250



More information about the sdlsound mailing list