vrshoot

view src/audio/stream.h @ 0:b2f14e535253

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