vrshoot

annotate src/audio/stream.h @ 1:e7ca128b8713

looks nice :)
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Feb 2014 00:35:22 +0200
parents
children
rev   line source
nuclear@0 1 #ifndef STREAM_H_
nuclear@0 2 #define STREAM_H_
nuclear@0 3
nuclear@0 4 #include <pthread.h>
nuclear@0 5
nuclear@0 6 #define AUDIO_FFT_BINS 16
nuclear@0 7 #define AUDIO_FFT_DOWNSAMPLE 1
nuclear@0 8
nuclear@0 9 #define AUDIO_NUM_BUFFERS 8
nuclear@0 10 #define AUDIO_BUFFER_MSEC 32
nuclear@0 11 // TODO should the sampling rate be hardcoded?
nuclear@0 12 #define AUDIO_BUFFER_SAMPLES (AUDIO_BUFFER_MSEC * 44100 / 1000)
nuclear@0 13 // TODO unhardcode the channels number
nuclear@0 14 #define AUDIO_BUFFER_BYTES (AUDIO_BUFFER_SAMPLES * 2 * 2)
nuclear@0 15
nuclear@0 16 #define AUDIO_FFT_SAMPLES (AUDIO_BUFFER_SAMPLES / AUDIO_FFT_DOWNSAMPLE)
nuclear@0 17
nuclear@0 18 enum AUDIO_PLAYMODE
nuclear@0 19 {
nuclear@0 20 AUDIO_PLAYMODE_ONCE,
nuclear@0 21 AUDIO_PLAYMODE_LOOP
nuclear@0 22 };
nuclear@0 23
nuclear@0 24 struct AudioStreamBuffer {
nuclear@0 25 char samples[AUDIO_BUFFER_BYTES];
nuclear@0 26
nuclear@0 27 int num_samples;
nuclear@0 28 int channels;
nuclear@0 29 int sample_rate;
nuclear@0 30 };
nuclear@0 31
nuclear@0 32 struct FFTState;
nuclear@0 33
nuclear@0 34 class AudioStream {
nuclear@0 35 private:
nuclear@0 36 pthread_t play_thread;
nuclear@0 37 pthread_mutex_t mutex;
nuclear@0 38
nuclear@0 39 float volume;
nuclear@0 40 bool done, loop;
nuclear@0 41 unsigned int poll_interval;
nuclear@0 42 unsigned int alsrc;
nuclear@0 43
nuclear@0 44 int *freqhist; // this will always point to the fft of the currently playing buffer
nuclear@0 45
nuclear@0 46 bool use_fft;
nuclear@0 47
nuclear@0 48 virtual bool more_samples(AudioStreamBuffer *buf) = 0;
nuclear@0 49 virtual void calc_freq(AudioStreamBuffer *buf, int *bins, FFTState *fft);
nuclear@0 50
nuclear@0 51 public:
nuclear@0 52 void poll_loop();
nuclear@0 53
nuclear@0 54 AudioStream();
nuclear@0 55 virtual ~AudioStream();
nuclear@0 56
nuclear@0 57 virtual void enable_fft();
nuclear@0 58 virtual void disable_fft();
nuclear@0 59 virtual bool is_fft_enabled() const;
nuclear@0 60
nuclear@0 61 virtual bool open(const char *fname);
nuclear@0 62 virtual void close();
nuclear@0 63
nuclear@0 64 virtual void set_volume(float vol);
nuclear@0 65 virtual float get_volume() const;
nuclear@0 66
nuclear@0 67 virtual void play(AUDIO_PLAYMODE mode);
nuclear@0 68 virtual void stop();
nuclear@0 69
nuclear@0 70 virtual void rewind() = 0;
nuclear@0 71
nuclear@0 72 virtual int freq_count(int bin) const;
nuclear@0 73 virtual float freq_normalized(int bin) const;
nuclear@0 74
nuclear@0 75 // frequency range in hertz
nuclear@0 76 virtual int freq_count(int range_start, int range_end) const;
nuclear@0 77 };
nuclear@0 78
nuclear@0 79 #endif // AUDIO_STREAM_H_