| 1 | |
| 2 | // Multi-channel sound buffer interface, and basic mono and stereo buffers |
| 3 | |
| 4 | // Blip_Buffer 0.3.3. Copyright (C) 2003-2005 Shay Green. GNU LGPL license. |
| 5 | |
| 6 | #ifndef MULTI_BUFFER_H |
| 7 | #define MULTI_BUFFER_H |
| 8 | |
| 9 | #include "Blip_Buffer.h" |
| 10 | |
| 11 | // Multi_Buffer is an interface to one or more Blip_Buffers mapped to one or |
| 12 | // more channels consisting of left, center, and right buffers. |
| 13 | class Multi_Buffer { |
| 14 | public: |
| 15 | Multi_Buffer( int samples_per_frame ); |
| 16 | virtual ~Multi_Buffer() { } |
| 17 | |
| 18 | // Set the number of channels available |
| 19 | virtual blargg_err_t set_channel_count( int ); |
| 20 | |
| 21 | // Get indexed channel, from 0 to channel count - 1 |
| 22 | struct channel_t { |
| 23 | Blip_Buffer* center; |
| 24 | Blip_Buffer* left; |
| 25 | Blip_Buffer* right; |
| 26 | }; |
| 27 | virtual channel_t channel( int index ) = 0; |
| 28 | |
| 29 | // See Blip_Buffer.h |
| 30 | // to do: rename to set_sample_rate |
| 31 | virtual blargg_err_t sample_rate( long rate, int msec = blip_default_length ) = 0; |
| 32 | virtual void clock_rate( long ) = 0; |
| 33 | virtual void bass_freq( int ) = 0; |
| 34 | virtual void clear() = 0; |
| 35 | long sample_rate() const; |
| 36 | |
| 37 | // Length of buffer, in milliseconds |
| 38 | int length() const; |
| 39 | |
| 40 | // See Blip_Buffer.h. For optimal operation, pass false for 'added_stereo' |
| 41 | // if nothing was added to the left and right buffers of any channel for |
| 42 | // this time frame. |
| 43 | virtual void end_frame( blip_time_t, bool added_stereo = true ) = 0; |
| 44 | |
| 45 | // Number of samples per output frame (1 = mono, 2 = stereo) |
| 46 | int samples_per_frame() const; |
| 47 | |
| 48 | // See Blip_Buffer.h |
| 49 | virtual long read_samples( blip_sample_t*, long ) = 0; |
| 50 | virtual long samples_avail() const = 0; |
| 51 | |
| 52 | private: |
| 53 | // noncopyable |
| 54 | Multi_Buffer( const Multi_Buffer& ); |
| 55 | Multi_Buffer& operator = ( const Multi_Buffer& ); |
| 56 | |
| 57 | long sample_rate_; |
| 58 | int length_; |
| 59 | int const samples_per_frame_; |
| 60 | }; |
| 61 | |
| 62 | // Mono_Buffer uses a single buffer and outputs mono samples. |
| 63 | class Mono_Buffer : public Multi_Buffer { |
| 64 | Blip_Buffer buf; |
| 65 | public: |
| 66 | Mono_Buffer(); |
| 67 | ~Mono_Buffer(); |
| 68 | |
| 69 | // Buffer used for all channels |
| 70 | Blip_Buffer* center(); |
| 71 | |
| 72 | // See Multi_Buffer |
| 73 | blargg_err_t sample_rate( long rate, int msec = blip_default_length ); |
| 74 | using Multi_Buffer::sample_rate; |
| 75 | void clock_rate( long ); |
| 76 | void bass_freq( int ); |
| 77 | void clear(); |
| 78 | channel_t channel( int ); |
| 79 | void end_frame( blip_time_t, bool unused = true ); |
| 80 | long samples_avail() const; |
| 81 | long read_samples( blip_sample_t*, long ); |
| 82 | }; |
| 83 | |
| 84 | // Stereo_Buffer uses three buffers (one for center) and outputs stereo sample pairs. |
| 85 | class Stereo_Buffer : public Multi_Buffer { |
| 86 | public: |
| 87 | Stereo_Buffer(); |
| 88 | ~Stereo_Buffer(); |
| 89 | |
| 90 | // Buffers used for all channels |
| 91 | Blip_Buffer* center(); |
| 92 | Blip_Buffer* left(); |
| 93 | Blip_Buffer* right(); |
| 94 | |
| 95 | // See Multi_Buffer |
| 96 | blargg_err_t sample_rate( long, int msec = blip_default_length ); |
| 97 | using Multi_Buffer::sample_rate; |
| 98 | void clock_rate( long ); |
| 99 | void bass_freq( int ); |
| 100 | void clear(); |
| 101 | channel_t channel( int index ); |
| 102 | void end_frame( blip_time_t, bool added_stereo = true ); |
| 103 | |
| 104 | long samples_avail() const; |
| 105 | long read_samples( blip_sample_t*, long ); |
| 106 | |
| 107 | private: |
| 108 | enum { buf_count = 3 }; |
| 109 | Blip_Buffer bufs [buf_count]; |
| 110 | channel_t chan; |
| 111 | bool stereo_added; |
| 112 | bool was_stereo; |
| 113 | |
| 114 | void mix_stereo( blip_sample_t*, long ); |
| 115 | void mix_mono( blip_sample_t*, long ); |
| 116 | }; |
| 117 | |
| 118 | |
| 119 | // End of public interface |
| 120 | |
| 121 | inline blargg_err_t Multi_Buffer::sample_rate( long rate, int msec ) |
| 122 | { |
| 123 | sample_rate_ = rate; |
| 124 | length_ = msec; |
| 125 | return blargg_success; |
| 126 | } |
| 127 | |
| 128 | inline int Multi_Buffer::samples_per_frame() const { return samples_per_frame_; } |
| 129 | |
| 130 | inline Blip_Buffer* Stereo_Buffer::left() { return &bufs [1]; } |
| 131 | |
| 132 | inline Blip_Buffer* Stereo_Buffer::center() { return &bufs [0]; } |
| 133 | |
| 134 | inline Blip_Buffer* Stereo_Buffer::right() { return &bufs [2]; } |
| 135 | |
| 136 | inline long Stereo_Buffer::samples_avail() const { return bufs [0].samples_avail() * 2; } |
| 137 | |
| 138 | inline Stereo_Buffer::channel_t Stereo_Buffer::channel( int index ) { return chan; } |
| 139 | |
| 140 | inline long Multi_Buffer::sample_rate() const { return sample_rate_; } |
| 141 | |
| 142 | inline int Multi_Buffer::length() const { return length_; } |
| 143 | |
| 144 | inline Blip_Buffer* Mono_Buffer::center() { return &buf; } |
| 145 | |
| 146 | inline void Mono_Buffer::clock_rate( long rate ) { buf.clock_rate( rate ); } |
| 147 | |
| 148 | inline void Mono_Buffer::clear() { buf.clear(); } |
| 149 | |
| 150 | inline void Mono_Buffer::bass_freq( int freq ) { buf.bass_freq( freq ); } |
| 151 | |
| 152 | inline long Mono_Buffer::read_samples( blip_sample_t* p, long s ) { return buf.read_samples( p, s ); } |
| 153 | |
| 154 | inline long Mono_Buffer::samples_avail() const { return buf.samples_avail(); } |
| 155 | |
| 156 | #endif |
| 157 | |
| 158 | |