Sound_Queue.h (1266B)
1 2// Simple sound queue for synchronous sound handling in SDL 3 4// Copyright (C) 2005 Shay Green. MIT license. 5 6#ifndef SOUND_QUEUE_H 7#define SOUND_QUEUE_H 8 9#include <SDL.h> 10 11// Simple SDL sound wrapper that has a synchronous interface 12class Sound_Queue { 13public: 14 Sound_Queue(); 15 ~Sound_Queue(); 16 17 // Initialize with specified sample rate and channel count. 18 // Returns NULL on success, otherwise error string. 19 const char* start( long sample_rate, int chan_count = 1 ); 20 21 // Number of samples in buffer waiting to be played 22 int sample_count() const; 23 24 // Write samples to buffer and block until enough space is available 25 typedef short sample_t; 26 void write( const sample_t*, int count, bool sync = true ); 27 28 // Pointer to samples currently playing (for showing waveform display) 29 sample_t const* currently_playing() const { return currently_playing_; } 30 31 // Stop audio output 32 void stop(); 33 34private: 35 enum { buf_size = 2048 }; 36 enum { buf_count = 3 }; 37 sample_t* volatile bufs; 38 SDL_sem* volatile free_sem; 39 sample_t* volatile currently_playing_; 40 int volatile read_buf; 41 int write_buf; 42 int write_pos; 43 bool sound_open; 44 bool sync_output; 45 46 sample_t* buf( int index ); 47 void fill_buffer( Uint8*, int ); 48 static void fill_buffer_( void*, Uint8*, int ); 49}; 50 51#endif