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 <SDL2/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* init( 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 );
27
28private:
29 enum { buf_size = 2048 };
30 enum { buf_count = 3 };
31 sample_t* volatile bufs;
32 SDL_sem* volatile free_sem;
33 int volatile read_buf;
34 int write_buf;
35 int write_pos;
36 bool sound_open;
37
38 sample_t* buf( int index );
39 void fill_buffer( Uint8*, int );
40 static void fill_buffer_( void*, Uint8*, int );
41};
42
43#endif
44
45