vrshoot

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/audio/stream.h	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,79 @@
     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 +struct FFTState;
    1.36 +
    1.37 +class AudioStream {
    1.38 +private:
    1.39 +	pthread_t play_thread;
    1.40 +	pthread_mutex_t mutex;
    1.41 +
    1.42 +	float volume;
    1.43 +	bool done, loop;
    1.44 +	unsigned int poll_interval;
    1.45 +	unsigned int alsrc;
    1.46 +
    1.47 +	int *freqhist;	// this will always point to the fft of the currently playing buffer
    1.48 +
    1.49 +	bool use_fft;
    1.50 +
    1.51 +	virtual bool more_samples(AudioStreamBuffer *buf) = 0;
    1.52 +	virtual void calc_freq(AudioStreamBuffer *buf, int *bins, FFTState *fft);
    1.53 +
    1.54 +public:
    1.55 +	void poll_loop();
    1.56 +
    1.57 +	AudioStream();
    1.58 +	virtual ~AudioStream();
    1.59 +
    1.60 +	virtual void enable_fft();
    1.61 +	virtual void disable_fft();
    1.62 +	virtual bool is_fft_enabled() const;
    1.63 +
    1.64 +	virtual bool open(const char *fname);
    1.65 +	virtual void close();
    1.66 +
    1.67 +	virtual void set_volume(float vol);
    1.68 +	virtual float get_volume() const;
    1.69 +
    1.70 +	virtual void play(AUDIO_PLAYMODE mode);
    1.71 +	virtual void stop();
    1.72 +
    1.73 +	virtual void rewind() = 0;
    1.74 +
    1.75 +	virtual int freq_count(int bin) const;
    1.76 +	virtual float freq_normalized(int bin) const;
    1.77 +
    1.78 +	// frequency range in hertz
    1.79 +	virtual int freq_count(int range_start, int range_end) const;
    1.80 +};
    1.81 +
    1.82 +#endif  // AUDIO_STREAM_H_