dbf-halloween2015
diff src/audio/stream.h @ 0:50683c78264e
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 01 Nov 2015 00:09:12 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/audio/stream.h Sun Nov 01 00:09:12 2015 +0200 1.3 @@ -0,0 +1,65 @@ 1.4 +#ifndef STREAM_H_ 1.5 +#define STREAM_H_ 1.6 + 1.7 +#include <pthread.h> 1.8 + 1.9 +#define AUDIO_FFT_BINS 16 1.10 +#define AUDIO_FFT_DOWNSAMPLE 1 1.11 + 1.12 +#define AUDIO_NUM_BUFFERS 8 1.13 +#define AUDIO_BUFFER_MSEC 32 1.14 +// TODO should the sampling rate be hardcoded? 1.15 +#define AUDIO_BUFFER_SAMPLES (AUDIO_BUFFER_MSEC * 44100 / 1000) 1.16 +// TODO unhardcode the channels number 1.17 +#define AUDIO_BUFFER_BYTES (AUDIO_BUFFER_SAMPLES * 2 * 2) 1.18 + 1.19 +#define AUDIO_FFT_SAMPLES (AUDIO_BUFFER_SAMPLES / AUDIO_FFT_DOWNSAMPLE) 1.20 + 1.21 +enum AUDIO_PLAYMODE 1.22 +{ 1.23 + AUDIO_PLAYMODE_ONCE, 1.24 + AUDIO_PLAYMODE_LOOP 1.25 +}; 1.26 + 1.27 +struct AudioStreamBuffer { 1.28 + char samples[AUDIO_BUFFER_BYTES]; 1.29 + 1.30 + int num_samples; 1.31 + int channels; 1.32 + int sample_rate; 1.33 +}; 1.34 + 1.35 +class AudioStream { 1.36 +private: 1.37 + pthread_t play_thread; 1.38 + pthread_mutex_t mutex; 1.39 + 1.40 + float volume, pitch; 1.41 + bool done, loop; 1.42 + unsigned int poll_interval; 1.43 + unsigned int alsrc; 1.44 + 1.45 + virtual bool more_samples(AudioStreamBuffer *buf) = 0; 1.46 + 1.47 +public: 1.48 + void poll_loop(); 1.49 + 1.50 + AudioStream(); 1.51 + virtual ~AudioStream(); 1.52 + 1.53 + virtual bool open(const char *fname); 1.54 + virtual void close(); 1.55 + 1.56 + virtual void set_volume(float vol); 1.57 + virtual float get_volume() const; 1.58 + 1.59 + virtual void set_pitch(float p); 1.60 + virtual float get_pitch() const; 1.61 + 1.62 + virtual void play(AUDIO_PLAYMODE mode); 1.63 + virtual void stop(); 1.64 + 1.65 + virtual void rewind() = 0; 1.66 +}; 1.67 + 1.68 +#endif // AUDIO_STREAM_H_