nuclear@0: #ifndef STREAM_H_ nuclear@0: #define STREAM_H_ nuclear@0: nuclear@0: #include nuclear@0: nuclear@0: #define AUDIO_FFT_BINS 16 nuclear@0: #define AUDIO_FFT_DOWNSAMPLE 1 nuclear@0: nuclear@0: #define AUDIO_NUM_BUFFERS 8 nuclear@0: #define AUDIO_BUFFER_MSEC 32 nuclear@0: // TODO should the sampling rate be hardcoded? nuclear@0: #define AUDIO_BUFFER_SAMPLES (AUDIO_BUFFER_MSEC * 44100 / 1000) nuclear@0: // TODO unhardcode the channels number nuclear@0: #define AUDIO_BUFFER_BYTES (AUDIO_BUFFER_SAMPLES * 2 * 2) nuclear@0: nuclear@0: #define AUDIO_FFT_SAMPLES (AUDIO_BUFFER_SAMPLES / AUDIO_FFT_DOWNSAMPLE) nuclear@0: nuclear@0: enum AUDIO_PLAYMODE nuclear@0: { nuclear@0: AUDIO_PLAYMODE_ONCE, nuclear@0: AUDIO_PLAYMODE_LOOP nuclear@0: }; nuclear@0: nuclear@0: struct AudioStreamBuffer { nuclear@0: char samples[AUDIO_BUFFER_BYTES]; nuclear@0: nuclear@0: int num_samples; nuclear@0: int channels; nuclear@0: int sample_rate; nuclear@0: }; nuclear@0: nuclear@0: class AudioStream { nuclear@0: private: nuclear@0: pthread_t play_thread; nuclear@0: pthread_mutex_t mutex; nuclear@0: nuclear@0: float volume, pitch; nuclear@0: bool done, loop; nuclear@0: unsigned int poll_interval; nuclear@0: unsigned int alsrc; nuclear@0: nuclear@0: virtual bool more_samples(AudioStreamBuffer *buf) = 0; nuclear@0: nuclear@0: public: nuclear@0: void poll_loop(); nuclear@0: nuclear@0: AudioStream(); nuclear@0: virtual ~AudioStream(); nuclear@0: nuclear@0: virtual bool open(const char *fname); nuclear@0: virtual void close(); nuclear@0: nuclear@0: virtual void set_volume(float vol); nuclear@0: virtual float get_volume() const; nuclear@0: nuclear@0: virtual void set_pitch(float p); nuclear@0: virtual float get_pitch() const; nuclear@0: nuclear@0: virtual void play(AUDIO_PLAYMODE mode); nuclear@0: virtual void stop(); nuclear@0: nuclear@0: virtual void rewind() = 0; nuclear@0: }; nuclear@0: nuclear@0: #endif // AUDIO_STREAM_H_