| ... | ... |
@@ -94,7 +94,7 @@ Stereo_Buffer::Stereo_Buffer() : Multi_Buffer(2) |
| 94 | 94 |
|
| 95 | 95 |
Stereo_Buffer::~Stereo_Buffer() { }
|
| 96 | 96 |
|
| 97 |
-void Stereo_Buffer::set_sample_rate(long rate, int msec) |
|
| 97 |
+void Stereo_Buffer::set_sample_rate(long rate, long msec) |
|
| 98 | 98 |
{
|
| 99 | 99 |
this->mixer.samples_read = 0; |
| 100 | 100 |
for (int i = bufs_size; --i >= 0; ) |
| ... | ... |
@@ -14,10 +14,6 @@ details. You should have received a copy of the GNU Lesser General Public |
| 14 | 14 |
License along with this module; if not, write to the Free Software Foundation, |
| 15 | 15 |
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 16 | 16 |
|
| 17 |
-#ifdef BLARGG_ENABLE_OPTIMIZER |
|
| 18 |
-# include BLARGG_ENABLE_OPTIMIZER |
|
| 19 |
-#endif |
|
| 20 |
- |
|
| 21 | 17 |
Multi_Buffer::Multi_Buffer(int spf) : samples_per_frame_(spf) |
| 22 | 18 |
{
|
| 23 | 19 |
this->length_ = 0; |
| ... | ... |
@@ -177,7 +173,7 @@ void Stereo_Mixer::read_pairs(blip_sample_t *out, int count) |
| 177 | 173 |
|
| 178 | 174 |
void Stereo_Mixer::mix_mono(blip_sample_t *out_, int count) |
| 179 | 175 |
{
|
| 180 |
- int bass = BLIP_READER_BASS(*this->bufs[2]); |
|
| 176 |
+ int bass = BLIP_READER_BASS(this->bufs[2]); |
|
| 181 | 177 |
BLIP_READER_BEGIN(center, *this->bufs[2]); |
| 182 | 178 |
BLIP_READER_ADJ_(center, this->samples_read); |
| 183 | 179 |
|
| ... | ... |
@@ -198,16 +194,16 @@ void Stereo_Mixer::mix_mono(blip_sample_t *out_, int count) |
| 198 | 194 |
|
| 199 | 195 |
void Stereo_Mixer::mix_stereo(blip_sample_t *out_, int count) |
| 200 | 196 |
{
|
| 201 |
- auto out = out_ + count * stereo; |
|
| 197 |
+ auto out = &out_[count * stereo]; |
|
| 202 | 198 |
|
| 203 | 199 |
// do left + center and right + center separately to reduce register load |
| 204 | 200 |
auto buf = &this->bufs[2]; |
| 205 |
- while (1) // loop runs twice |
|
| 201 |
+ while (true) // loop runs twice |
|
| 206 | 202 |
{
|
| 207 | 203 |
--buf; |
| 208 | 204 |
--out; |
| 209 | 205 |
|
| 210 |
- int bass = BLIP_READER_BASS(*this->bufs[2]); |
|
| 206 |
+ int bass = BLIP_READER_BASS(this->bufs[2]); |
|
| 211 | 207 |
BLIP_READER_BEGIN(side, **buf); |
| 212 | 208 |
BLIP_READER_BEGIN(center, *this->bufs[2]); |
| 213 | 209 |
|
| ... | ... |
@@ -14,8 +14,6 @@ details. You should have received a copy of the GNU Lesser General Public |
| 14 | 14 |
License along with this module; if not, write to the Free Software Foundation, |
| 15 | 15 |
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 16 | 16 |
|
| 17 |
-#include "blargg_source.h" |
|
| 18 |
- |
|
| 19 | 17 |
#ifdef BLARGG_ENABLE_OPTIMIZER |
| 20 | 18 |
# include BLARGG_ENABLE_OPTIMIZER |
| 21 | 19 |
#endif |
| ... | ... |
@@ -36,31 +34,6 @@ Multi_Buffer::channel_t Multi_Buffer::channel(int /*index*/) |
| 36 | 34 |
return ch; |
| 37 | 35 |
} |
| 38 | 36 |
|
| 39 |
-// Silent_Buffer |
|
| 40 |
- |
|
| 41 |
-Silent_Buffer::Silent_Buffer() : Multi_Buffer(1) // 0 channels would probably confuse |
|
| 42 |
-{
|
|
| 43 |
- // TODO: better to use empty Blip_Buffer so caller never has to check for NULL? |
|
| 44 |
- this->chan.left = this->chan.center = this->chan.right = nullptr; |
|
| 45 |
-} |
|
| 46 |
- |
|
| 47 |
-// Mono_Buffer |
|
| 48 |
- |
|
| 49 |
-Mono_Buffer::Mono_Buffer() : Multi_Buffer(1) |
|
| 50 |
-{
|
|
| 51 |
- this->chan.center = &this->buf; |
|
| 52 |
- this->chan.left = &this->buf; |
|
| 53 |
- this->chan.right = &this->buf; |
|
| 54 |
-} |
|
| 55 |
- |
|
| 56 |
-Mono_Buffer::~Mono_Buffer() { }
|
|
| 57 |
- |
|
| 58 |
-blargg_err_t Mono_Buffer::set_sample_rate(long rate, int msec) |
|
| 59 |
-{
|
|
| 60 |
- RETURN_ERR(this->buf.set_sample_rate(rate, msec)); |
|
| 61 |
- return Multi_Buffer::set_sample_rate(this->buf.sample_rate(), this->buf.length()); |
|
| 62 |
-} |
|
| 63 |
- |
|
| 64 | 37 |
// Tracked_Blip_Buffer |
| 65 | 38 |
|
| 66 | 39 |
Tracked_Blip_Buffer::Tracked_Blip_Buffer() |
| ... | ... |
@@ -86,7 +59,7 @@ uint32_t Tracked_Blip_Buffer::non_silent() const |
| 86 | 59 |
return this->last_non_silence | this->unsettled(); |
| 87 | 60 |
} |
| 88 | 61 |
|
| 89 |
-inline void Tracked_Blip_Buffer::remove_(long n) |
|
| 62 |
+void Tracked_Blip_Buffer::remove_(long n) |
|
| 90 | 63 |
{
|
| 91 | 64 |
if ((this->last_non_silence -= n) < 0) |
| 92 | 65 |
this->last_non_silence = 0; |
| ... | ... |
@@ -125,12 +98,12 @@ Stereo_Buffer::Stereo_Buffer() : Multi_Buffer(2) |
| 125 | 98 |
|
| 126 | 99 |
Stereo_Buffer::~Stereo_Buffer() { }
|
| 127 | 100 |
|
| 128 |
-blargg_err_t Stereo_Buffer::set_sample_rate(long rate, int msec) |
|
| 101 |
+void Stereo_Buffer::set_sample_rate(long rate, int msec) |
|
| 129 | 102 |
{
|
| 130 | 103 |
this->mixer.samples_read = 0; |
| 131 | 104 |
for (int i = bufs_size; --i >= 0; ) |
| 132 |
- RETURN_ERR(this->bufs[i].set_sample_rate(rate, msec)); |
|
| 133 |
- return Multi_Buffer::set_sample_rate(this->bufs[0].sample_rate(), this->bufs[0].length()); |
|
| 105 |
+ this->bufs[i].set_sample_rate(rate, msec); |
|
| 106 |
+ Multi_Buffer::set_sample_rate(this->bufs[0].sample_rate(), this->bufs[0].length()); |
|
| 134 | 107 |
} |
| 135 | 108 |
|
| 136 | 109 |
void Stereo_Buffer::clock_rate(long rate) |
| ... | ... |
@@ -196,7 +169,7 @@ void Stereo_Mixer::read_pairs(blip_sample_t *out, int count) |
| 196 | 169 |
// except that buffer isn't cleared, so caller can encounter |
| 197 | 170 |
// subtle problems and not realize the cause. |
| 198 | 171 |
this->samples_read += count; |
| 199 |
- if (this->bufs[0]->non_silent() || this->bufs[1]->non_silent()) |
|
| 172 |
+ if (this->bufs[0]->non_silent() | this->bufs[1]->non_silent()) |
|
| 200 | 173 |
this->mix_stereo(out, count); |
| 201 | 174 |
else |
| 202 | 175 |
this->mix_mono(out, count); |
| ... | ... |
@@ -238,8 +211,8 @@ void Stereo_Mixer::mix_stereo(blip_sample_t *out_, int count) |
| 238 | 211 |
BLIP_READER_BEGIN(side, **buf); |
| 239 | 212 |
BLIP_READER_BEGIN(center, *this->bufs[2]); |
| 240 | 213 |
|
| 241 |
- BLIP_READER_ADJ_(side, samples_read); |
|
| 242 |
- BLIP_READER_ADJ_(center, samples_read); |
|
| 214 |
+ BLIP_READER_ADJ_(side, this->samples_read); |
|
| 215 |
+ BLIP_READER_ADJ_(center, this->samples_read); |
|
| 243 | 216 |
|
| 244 | 217 |
int offset = -count; |
| 245 | 218 |
do |
| ... | ... |
@@ -256,7 +229,7 @@ void Stereo_Mixer::mix_stereo(blip_sample_t *out_, int count) |
| 256 | 229 |
|
| 257 | 230 |
BLIP_READER_END(side, **buf); |
| 258 | 231 |
|
| 259 |
- if (buf != bufs) |
|
| 232 |
+ if (buf != this->bufs) |
|
| 260 | 233 |
continue; |
| 261 | 234 |
|
| 262 | 235 |
// only end center once |
(As an aside, blargg's code style makes me go blarg myself.)
| ... | ... |
@@ -1,5 +1,6 @@ |
| 1 | 1 |
// Blip_Buffer 0.4.1. http://www.slack.net/~ant/ |
| 2 | 2 |
|
| 3 |
+#include <algorithm> |
|
| 3 | 4 |
#include "Multi_Buffer.h" |
| 4 | 5 |
|
| 5 | 6 |
/* Copyright (C) 2003-2007 Shay Green. This module is free software; you |
| ... | ... |
@@ -16,266 +17,250 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 16 | 17 |
#include "blargg_source.h" |
| 17 | 18 |
|
| 18 | 19 |
#ifdef BLARGG_ENABLE_OPTIMIZER |
| 19 |
- #include BLARGG_ENABLE_OPTIMIZER |
|
| 20 |
+# include BLARGG_ENABLE_OPTIMIZER |
|
| 20 | 21 |
#endif |
| 21 | 22 |
|
| 22 |
-Multi_Buffer::Multi_Buffer( int spf ) : samples_per_frame_( spf ) |
|
| 23 |
+Multi_Buffer::Multi_Buffer(int spf) : samples_per_frame_(spf) |
|
| 23 | 24 |
{
|
| 24 |
- length_ = 0; |
|
| 25 |
- sample_rate_ = 0; |
|
| 26 |
- channels_changed_count_ = 1; |
|
| 27 |
- channel_types_ = 0; |
|
| 28 |
- channel_count_ = 0; |
|
| 29 |
- immediate_removal_ = true; |
|
| 25 |
+ this->length_ = 0; |
|
| 26 |
+ this->sample_rate_ = 0; |
|
| 27 |
+ this->channels_changed_count_ = 1; |
|
| 28 |
+ this->channel_types_ = nullptr; |
|
| 29 |
+ this->channel_count_ = 0; |
|
| 30 |
+ this->immediate_removal_ = true; |
|
| 30 | 31 |
} |
| 31 | 32 |
|
| 32 |
-Multi_Buffer::channel_t Multi_Buffer::channel( int /*index*/ ) |
|
| 33 |
+Multi_Buffer::channel_t Multi_Buffer::channel(int /*index*/) |
|
| 33 | 34 |
{
|
| 34 |
- static channel_t const ch = { 0, 0, 0 };
|
|
| 35 |
+ static const channel_t ch = { 0, 0, 0 };
|
|
| 35 | 36 |
return ch; |
| 36 | 37 |
} |
| 37 | 38 |
|
| 38 | 39 |
// Silent_Buffer |
| 39 | 40 |
|
| 40 |
-Silent_Buffer::Silent_Buffer() : Multi_Buffer( 1 ) // 0 channels would probably confuse |
|
| 41 |
+Silent_Buffer::Silent_Buffer() : Multi_Buffer(1) // 0 channels would probably confuse |
|
| 41 | 42 |
{
|
| 42 | 43 |
// TODO: better to use empty Blip_Buffer so caller never has to check for NULL? |
| 43 |
- chan.left = 0; |
|
| 44 |
- chan.center = 0; |
|
| 45 |
- chan.right = 0; |
|
| 44 |
+ this->chan.left = this->chan.center = this->chan.right = nullptr; |
|
| 46 | 45 |
} |
| 47 | 46 |
|
| 48 | 47 |
// Mono_Buffer |
| 49 | 48 |
|
| 50 |
-Mono_Buffer::Mono_Buffer() : Multi_Buffer( 1 ) |
|
| 49 |
+Mono_Buffer::Mono_Buffer() : Multi_Buffer(1) |
|
| 51 | 50 |
{
|
| 52 |
- chan.center = &buf; |
|
| 53 |
- chan.left = &buf; |
|
| 54 |
- chan.right = &buf; |
|
| 51 |
+ this->chan.center = &this->buf; |
|
| 52 |
+ this->chan.left = &this->buf; |
|
| 53 |
+ this->chan.right = &this->buf; |
|
| 55 | 54 |
} |
| 56 | 55 |
|
| 57 | 56 |
Mono_Buffer::~Mono_Buffer() { }
|
| 58 | 57 |
|
| 59 |
-blargg_err_t Mono_Buffer::set_sample_rate( long rate, int msec ) |
|
| 58 |
+blargg_err_t Mono_Buffer::set_sample_rate(long rate, int msec) |
|
| 60 | 59 |
{
|
| 61 |
- RETURN_ERR( buf.set_sample_rate( rate, msec ) ); |
|
| 62 |
- return Multi_Buffer::set_sample_rate( buf.sample_rate(), buf.length() ); |
|
| 60 |
+ RETURN_ERR(this->buf.set_sample_rate(rate, msec)); |
|
| 61 |
+ return Multi_Buffer::set_sample_rate(this->buf.sample_rate(), this->buf.length()); |
|
| 63 | 62 |
} |
| 64 | 63 |
|
| 65 |
- |
|
| 66 | 64 |
// Tracked_Blip_Buffer |
| 67 | 65 |
|
| 68 | 66 |
Tracked_Blip_Buffer::Tracked_Blip_Buffer() |
| 69 | 67 |
{
|
| 70 |
- last_non_silence = 0; |
|
| 68 |
+ this->last_non_silence = 0; |
|
| 71 | 69 |
} |
| 72 | 70 |
|
| 73 | 71 |
void Tracked_Blip_Buffer::clear() |
| 74 | 72 |
{
|
| 75 |
- last_non_silence = 0; |
|
| 73 |
+ this->last_non_silence = 0; |
|
| 76 | 74 |
Blip_Buffer::clear(); |
| 77 | 75 |
} |
| 78 | 76 |
|
| 79 |
-void Tracked_Blip_Buffer::end_frame( blip_time_t t ) |
|
| 80 |
-{
|
|
| 81 |
- Blip_Buffer::end_frame( t ); |
|
| 82 |
- if ( clear_modified() ) |
|
| 83 |
- last_non_silence = samples_avail() + blip_buffer_extra_; |
|
| 84 |
-} |
|
| 85 |
- |
|
| 86 |
-blip_ulong Tracked_Blip_Buffer::non_silent() const |
|
| 77 |
+void Tracked_Blip_Buffer::end_frame(blip_time_t t) |
|
| 87 | 78 |
{
|
| 88 |
- return last_non_silence | unsettled(); |
|
| 79 |
+ Blip_Buffer::end_frame(t); |
|
| 80 |
+ if (this->clear_modified()) |
|
| 81 |
+ this->last_non_silence = this->samples_avail() + blip_buffer_extra_; |
|
| 89 | 82 |
} |
| 90 | 83 |
|
| 91 |
-inline void Tracked_Blip_Buffer::remove_( long n ) |
|
| 84 |
+uint32_t Tracked_Blip_Buffer::non_silent() const |
|
| 92 | 85 |
{
|
| 93 |
- if ( (last_non_silence -= n) < 0 ) |
|
| 94 |
- last_non_silence = 0; |
|
| 86 |
+ return this->last_non_silence | this->unsettled(); |
|
| 95 | 87 |
} |
| 96 | 88 |
|
| 97 |
-void Tracked_Blip_Buffer::remove_silence( long n ) |
|
| 89 |
+inline void Tracked_Blip_Buffer::remove_(long n) |
|
| 98 | 90 |
{
|
| 99 |
- remove_( n ); |
|
| 100 |
- Blip_Buffer::remove_silence( n ); |
|
| 91 |
+ if ((this->last_non_silence -= n) < 0) |
|
| 92 |
+ this->last_non_silence = 0; |
|
| 101 | 93 |
} |
| 102 | 94 |
|
| 103 |
-void Tracked_Blip_Buffer::remove_samples( long n ) |
|
| 95 |
+void Tracked_Blip_Buffer::remove_silence(long n) |
|
| 104 | 96 |
{
|
| 105 |
- remove_( n ); |
|
| 106 |
- Blip_Buffer::remove_samples( n ); |
|
| 97 |
+ this->remove_(n); |
|
| 98 |
+ Blip_Buffer::remove_silence(n); |
|
| 107 | 99 |
} |
| 108 | 100 |
|
| 109 |
-void Tracked_Blip_Buffer::remove_all_samples() |
|
| 101 |
+void Tracked_Blip_Buffer::remove_samples(long n) |
|
| 110 | 102 |
{
|
| 111 |
- long avail = samples_avail(); |
|
| 112 |
- if ( !non_silent() ) |
|
| 113 |
- remove_silence( avail ); |
|
| 114 |
- else |
|
| 115 |
- remove_samples( avail ); |
|
| 103 |
+ this->remove_(n); |
|
| 104 |
+ Blip_Buffer::remove_samples(n); |
|
| 116 | 105 |
} |
| 117 | 106 |
|
| 118 |
-long Tracked_Blip_Buffer::read_samples( blip_sample_t* out, long count ) |
|
| 107 |
+long Tracked_Blip_Buffer::read_samples(blip_sample_t *out, long count) |
|
| 119 | 108 |
{
|
| 120 |
- count = Blip_Buffer::read_samples( out, count ); |
|
| 121 |
- remove_( count ); |
|
| 109 |
+ count = Blip_Buffer::read_samples(out, count); |
|
| 110 |
+ this->remove_(count); |
|
| 122 | 111 |
return count; |
| 123 | 112 |
} |
| 124 | 113 |
|
| 125 | 114 |
// Stereo_Buffer |
| 126 | 115 |
|
| 127 |
-int const stereo = 2; |
|
| 116 |
+static const int stereo = 2; |
|
| 128 | 117 |
|
| 129 |
-Stereo_Buffer::Stereo_Buffer() : Multi_Buffer( 2 ) |
|
| 118 |
+Stereo_Buffer::Stereo_Buffer() : Multi_Buffer(2) |
|
| 130 | 119 |
{
|
| 131 |
- chan.center = mixer.bufs [2] = &bufs [2]; |
|
| 132 |
- chan.left = mixer.bufs [0] = &bufs [0]; |
|
| 133 |
- chan.right = mixer.bufs [1] = &bufs [1]; |
|
| 134 |
- mixer.samples_read = 0; |
|
| 120 |
+ this->chan.center = this->mixer.bufs[2] = &this->bufs[2]; |
|
| 121 |
+ this->chan.left = this->mixer.bufs[0] = &this->bufs[0]; |
|
| 122 |
+ this->chan.right = this->mixer.bufs[1] = &this->bufs[1]; |
|
| 123 |
+ this->mixer.samples_read = 0; |
|
| 135 | 124 |
} |
| 136 | 125 |
|
| 137 | 126 |
Stereo_Buffer::~Stereo_Buffer() { }
|
| 138 | 127 |
|
| 139 |
-blargg_err_t Stereo_Buffer::set_sample_rate( long rate, int msec ) |
|
| 128 |
+blargg_err_t Stereo_Buffer::set_sample_rate(long rate, int msec) |
|
| 140 | 129 |
{
|
| 141 |
- mixer.samples_read = 0; |
|
| 142 |
- for ( int i = bufs_size; --i >= 0; ) |
|
| 143 |
- RETURN_ERR( bufs [i].set_sample_rate( rate, msec ) ); |
|
| 144 |
- return Multi_Buffer::set_sample_rate( bufs [0].sample_rate(), bufs [0].length() ); |
|
| 130 |
+ this->mixer.samples_read = 0; |
|
| 131 |
+ for (int i = bufs_size; --i >= 0; ) |
|
| 132 |
+ RETURN_ERR(this->bufs[i].set_sample_rate(rate, msec)); |
|
| 133 |
+ return Multi_Buffer::set_sample_rate(this->bufs[0].sample_rate(), this->bufs[0].length()); |
|
| 145 | 134 |
} |
| 146 | 135 |
|
| 147 |
-void Stereo_Buffer::clock_rate( long rate ) |
|
| 136 |
+void Stereo_Buffer::clock_rate(long rate) |
|
| 148 | 137 |
{
|
| 149 |
- for ( int i = bufs_size; --i >= 0; ) |
|
| 150 |
- bufs [i].clock_rate( rate ); |
|
| 138 |
+ for (int i = bufs_size; --i >= 0; ) |
|
| 139 |
+ this->bufs[i].clock_rate(rate); |
|
| 151 | 140 |
} |
| 152 | 141 |
|
| 153 |
-void Stereo_Buffer::bass_freq( int bass ) |
|
| 142 |
+void Stereo_Buffer::bass_freq(int bass) |
|
| 154 | 143 |
{
|
| 155 |
- for ( int i = bufs_size; --i >= 0; ) |
|
| 156 |
- bufs [i].bass_freq( bass ); |
|
| 144 |
+ for (int i = bufs_size; --i >= 0; ) |
|
| 145 |
+ this->bufs[i].bass_freq(bass); |
|
| 157 | 146 |
} |
| 158 | 147 |
|
| 159 | 148 |
void Stereo_Buffer::clear() |
| 160 | 149 |
{
|
| 161 |
- mixer.samples_read = 0; |
|
| 162 |
- for ( int i = bufs_size; --i >= 0; ) |
|
| 163 |
- bufs [i].clear(); |
|
| 150 |
+ this->mixer.samples_read = 0; |
|
| 151 |
+ for (int i = bufs_size; --i >= 0; ) |
|
| 152 |
+ this->bufs[i].clear(); |
|
| 164 | 153 |
} |
| 165 | 154 |
|
| 166 |
-void Stereo_Buffer::end_frame( blip_time_t time ) |
|
| 155 |
+void Stereo_Buffer::end_frame(blip_time_t time) |
|
| 167 | 156 |
{
|
| 168 |
- for ( int i = bufs_size; --i >= 0; ) |
|
| 169 |
- bufs [i].end_frame( time ); |
|
| 157 |
+ for (int i = bufs_size; --i >= 0; ) |
|
| 158 |
+ this->bufs[i].end_frame(time); |
|
| 170 | 159 |
} |
| 171 | 160 |
|
| 172 |
-long Stereo_Buffer::read_samples( blip_sample_t* out, long out_size ) |
|
| 161 |
+long Stereo_Buffer::read_samples(blip_sample_t *out, long out_size) |
|
| 173 | 162 |
{
|
| 174 |
- require( (out_size & 1) == 0 ); // must read an even number of samples |
|
| 175 |
- out_size = min( out_size, samples_avail() ); |
|
| 163 |
+ assert(!(out_size & 1)); // must read an even number of samples |
|
| 164 |
+ out_size = std::min(out_size, this->samples_avail()); |
|
| 176 | 165 |
|
| 177 |
- int pair_count = int (out_size >> 1); |
|
| 178 |
- if ( pair_count ) |
|
| 166 |
+ int pair_count = static_cast<int>(out_size >> 1); |
|
| 167 |
+ if (pair_count) |
|
| 179 | 168 |
{
|
| 180 |
- mixer.read_pairs( out, pair_count ); |
|
| 169 |
+ this->mixer.read_pairs(out, pair_count); |
|
| 181 | 170 |
|
| 182 |
- if ( samples_avail() <= 0 || immediate_removal() ) |
|
| 171 |
+ if (this->samples_avail() <= 0 || this->immediate_removal()) |
|
| 183 | 172 |
{
|
| 184 |
- for ( int i = bufs_size; --i >= 0; ) |
|
| 173 |
+ for (int i = bufs_size; --i >= 0; ) |
|
| 185 | 174 |
{
|
| 186 |
- buf_t& b = bufs [i]; |
|
| 175 |
+ auto &b = this->bufs[i]; |
|
| 187 | 176 |
// TODO: might miss non-silence settling since it checks END of last read |
| 188 |
- if ( !b.non_silent() ) |
|
| 189 |
- b.remove_silence( mixer.samples_read ); |
|
| 177 |
+ if (!b.non_silent()) |
|
| 178 |
+ b.remove_silence(this->mixer.samples_read); |
|
| 190 | 179 |
else |
| 191 |
- b.remove_samples( mixer.samples_read ); |
|
| 180 |
+ b.remove_samples(this->mixer.samples_read); |
|
| 192 | 181 |
} |
| 193 |
- mixer.samples_read = 0; |
|
| 182 |
+ this->mixer.samples_read = 0; |
|
| 194 | 183 |
} |
| 195 | 184 |
} |
| 196 | 185 |
return out_size; |
| 197 | 186 |
} |
| 198 | 187 |
|
| 199 |
- |
|
| 200 | 188 |
// Stereo_Mixer |
| 201 | 189 |
|
| 202 | 190 |
// mixers use a single index value to improve performance on register-challenged processors |
| 203 | 191 |
// offset goes from negative to zero |
| 204 | 192 |
|
| 205 |
-void Stereo_Mixer::read_pairs( blip_sample_t* out, int count ) |
|
| 193 |
+void Stereo_Mixer::read_pairs(blip_sample_t *out, int count) |
|
| 206 | 194 |
{
|
| 207 | 195 |
// TODO: if caller never marks buffers as modified, uses mono |
| 208 | 196 |
// except that buffer isn't cleared, so caller can encounter |
| 209 | 197 |
// subtle problems and not realize the cause. |
| 210 |
- samples_read += count; |
|
| 211 |
- if ( bufs [0]->non_silent() | bufs [1]->non_silent() ) |
|
| 212 |
- mix_stereo( out, count ); |
|
| 198 |
+ this->samples_read += count; |
|
| 199 |
+ if (this->bufs[0]->non_silent() || this->bufs[1]->non_silent()) |
|
| 200 |
+ this->mix_stereo(out, count); |
|
| 213 | 201 |
else |
| 214 |
- mix_mono( out, count ); |
|
| 202 |
+ this->mix_mono(out, count); |
|
| 215 | 203 |
} |
| 216 | 204 |
|
| 217 |
-void Stereo_Mixer::mix_mono( blip_sample_t* out_, int count ) |
|
| 205 |
+void Stereo_Mixer::mix_mono(blip_sample_t *out_, int count) |
|
| 218 | 206 |
{
|
| 219 |
- int const bass = BLIP_READER_BASS( *bufs [2] ); |
|
| 220 |
- BLIP_READER_BEGIN( center, *bufs [2] ); |
|
| 221 |
- BLIP_READER_ADJ_( center, samples_read ); |
|
| 207 |
+ int bass = BLIP_READER_BASS(*this->bufs[2]); |
|
| 208 |
+ BLIP_READER_BEGIN(center, *this->bufs[2]); |
|
| 209 |
+ BLIP_READER_ADJ_(center, this->samples_read); |
|
| 222 | 210 |
|
| 223 |
- typedef blip_sample_t stereo_blip_sample_t [stereo]; |
|
| 224 |
- stereo_blip_sample_t* BLIP_RESTRICT out = (stereo_blip_sample_t*) out_ + count; |
|
| 211 |
+ typedef blip_sample_t stereo_blip_sample_t[stereo]; |
|
| 212 |
+ auto out = reinterpret_cast<stereo_blip_sample_t *>(out_) + count; |
|
| 225 | 213 |
int offset = -count; |
| 226 | 214 |
do |
| 227 | 215 |
{
|
| 228 |
- blargg_long s = BLIP_READER_READ( center ); |
|
| 229 |
- BLIP_READER_NEXT_IDX_( center, bass, offset ); |
|
| 230 |
- BLIP_CLAMP( s, s ); |
|
| 216 |
+ int32_t s = BLIP_READER_READ(center); |
|
| 217 |
+ BLIP_READER_NEXT_IDX_(center, bass, offset); |
|
| 218 |
+ BLIP_CLAMP(s, s); |
|
| 231 | 219 |
|
| 232 |
- out [offset] [0] = (blip_sample_t) s; |
|
| 233 |
- out [offset] [1] = (blip_sample_t) s; |
|
| 234 |
- } |
|
| 235 |
- while ( ++offset ); |
|
| 220 |
+ out[offset][0] = out[offset][1] = static_cast<blip_sample_t>(s); |
|
| 221 |
+ } while (++offset); |
|
| 236 | 222 |
|
| 237 |
- BLIP_READER_END( center, *bufs [2] ); |
|
| 223 |
+ BLIP_READER_END(center, *this->bufs[2]); |
|
| 238 | 224 |
} |
| 239 | 225 |
|
| 240 |
-void Stereo_Mixer::mix_stereo( blip_sample_t* out_, int count ) |
|
| 226 |
+void Stereo_Mixer::mix_stereo(blip_sample_t *out_, int count) |
|
| 241 | 227 |
{
|
| 242 |
- blip_sample_t* BLIP_RESTRICT out = out_ + count * stereo; |
|
| 228 |
+ auto out = out_ + count * stereo; |
|
| 243 | 229 |
|
| 244 | 230 |
// do left + center and right + center separately to reduce register load |
| 245 |
- Tracked_Blip_Buffer* const* buf = &bufs [2]; |
|
| 246 |
- while ( true ) // loop runs twice |
|
| 231 |
+ auto buf = &this->bufs[2]; |
|
| 232 |
+ while (1) // loop runs twice |
|
| 247 | 233 |
{
|
| 248 | 234 |
--buf; |
| 249 | 235 |
--out; |
| 250 | 236 |
|
| 251 |
- int const bass = BLIP_READER_BASS( *bufs [2] ); |
|
| 252 |
- BLIP_READER_BEGIN( side, **buf ); |
|
| 253 |
- BLIP_READER_BEGIN( center, *bufs [2] ); |
|
| 237 |
+ int bass = BLIP_READER_BASS(*this->bufs[2]); |
|
| 238 |
+ BLIP_READER_BEGIN(side, **buf); |
|
| 239 |
+ BLIP_READER_BEGIN(center, *this->bufs[2]); |
|
| 254 | 240 |
|
| 255 |
- BLIP_READER_ADJ_( side, samples_read ); |
|
| 256 |
- BLIP_READER_ADJ_( center, samples_read ); |
|
| 241 |
+ BLIP_READER_ADJ_(side, samples_read); |
|
| 242 |
+ BLIP_READER_ADJ_(center, samples_read); |
|
| 257 | 243 |
|
| 258 | 244 |
int offset = -count; |
| 259 | 245 |
do |
| 260 | 246 |
{
|
| 261 |
- blargg_long s = BLIP_READER_READ_RAW( center ) + BLIP_READER_READ_RAW( side ); |
|
| 247 |
+ int32_t s = BLIP_READER_READ_RAW(center) + BLIP_READER_READ_RAW(side); |
|
| 262 | 248 |
s >>= blip_sample_bits - 16; |
| 263 |
- BLIP_READER_NEXT_IDX_( side, bass, offset ); |
|
| 264 |
- BLIP_READER_NEXT_IDX_( center, bass, offset ); |
|
| 265 |
- BLIP_CLAMP( s, s ); |
|
| 249 |
+ BLIP_READER_NEXT_IDX_(side, bass, offset); |
|
| 250 |
+ BLIP_READER_NEXT_IDX_(center, bass, offset); |
|
| 251 |
+ BLIP_CLAMP(s, s); |
|
| 266 | 252 |
|
| 267 | 253 |
++offset; // before write since out is decremented to slightly before end |
| 268 |
- out [offset * stereo] = (blip_sample_t) s; |
|
| 269 |
- } |
|
| 270 |
- while ( offset ); |
|
| 254 |
+ out[offset * stereo] = static_cast<blip_sample_t>(s); |
|
| 255 |
+ } while (offset); |
|
| 271 | 256 |
|
| 272 |
- BLIP_READER_END( side, **buf ); |
|
| 257 |
+ BLIP_READER_END(side, **buf); |
|
| 273 | 258 |
|
| 274 |
- if ( buf != bufs ) |
|
| 259 |
+ if (buf != bufs) |
|
| 275 | 260 |
continue; |
| 276 | 261 |
|
| 277 | 262 |
// only end center once |
| 278 |
- BLIP_READER_END( center, *bufs [2] ); |
|
| 263 |
+ BLIP_READER_END(center, *this->bufs[2]); |
|
| 279 | 264 |
break; |
| 280 | 265 |
} |
| 281 | 266 |
} |
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,281 @@ |
| 1 |
+// Blip_Buffer 0.4.1. http://www.slack.net/~ant/ |
|
| 2 |
+ |
|
| 3 |
+#include "Multi_Buffer.h" |
|
| 4 |
+ |
|
| 5 |
+/* Copyright (C) 2003-2007 Shay Green. This module is free software; you |
|
| 6 |
+can redistribute it and/or modify it under the terms of the GNU Lesser |
|
| 7 |
+General Public License as published by the Free Software Foundation; either |
|
| 8 |
+version 2.1 of the License, or (at your option) any later version. This |
|
| 9 |
+module is distributed in the hope that it will be useful, but WITHOUT ANY |
|
| 10 |
+WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
| 11 |
+FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
| 12 |
+details. You should have received a copy of the GNU Lesser General Public |
|
| 13 |
+License along with this module; if not, write to the Free Software Foundation, |
|
| 14 |
+Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ |
|
| 15 |
+ |
|
| 16 |
+#include "blargg_source.h" |
|
| 17 |
+ |
|
| 18 |
+#ifdef BLARGG_ENABLE_OPTIMIZER |
|
| 19 |
+ #include BLARGG_ENABLE_OPTIMIZER |
|
| 20 |
+#endif |
|
| 21 |
+ |
|
| 22 |
+Multi_Buffer::Multi_Buffer( int spf ) : samples_per_frame_( spf ) |
|
| 23 |
+{
|
|
| 24 |
+ length_ = 0; |
|
| 25 |
+ sample_rate_ = 0; |
|
| 26 |
+ channels_changed_count_ = 1; |
|
| 27 |
+ channel_types_ = 0; |
|
| 28 |
+ channel_count_ = 0; |
|
| 29 |
+ immediate_removal_ = true; |
|
| 30 |
+} |
|
| 31 |
+ |
|
| 32 |
+Multi_Buffer::channel_t Multi_Buffer::channel( int /*index*/ ) |
|
| 33 |
+{
|
|
| 34 |
+ static channel_t const ch = { 0, 0, 0 };
|
|
| 35 |
+ return ch; |
|
| 36 |
+} |
|
| 37 |
+ |
|
| 38 |
+// Silent_Buffer |
|
| 39 |
+ |
|
| 40 |
+Silent_Buffer::Silent_Buffer() : Multi_Buffer( 1 ) // 0 channels would probably confuse |
|
| 41 |
+{
|
|
| 42 |
+ // TODO: better to use empty Blip_Buffer so caller never has to check for NULL? |
|
| 43 |
+ chan.left = 0; |
|
| 44 |
+ chan.center = 0; |
|
| 45 |
+ chan.right = 0; |
|
| 46 |
+} |
|
| 47 |
+ |
|
| 48 |
+// Mono_Buffer |
|
| 49 |
+ |
|
| 50 |
+Mono_Buffer::Mono_Buffer() : Multi_Buffer( 1 ) |
|
| 51 |
+{
|
|
| 52 |
+ chan.center = &buf; |
|
| 53 |
+ chan.left = &buf; |
|
| 54 |
+ chan.right = &buf; |
|
| 55 |
+} |
|
| 56 |
+ |
|
| 57 |
+Mono_Buffer::~Mono_Buffer() { }
|
|
| 58 |
+ |
|
| 59 |
+blargg_err_t Mono_Buffer::set_sample_rate( long rate, int msec ) |
|
| 60 |
+{
|
|
| 61 |
+ RETURN_ERR( buf.set_sample_rate( rate, msec ) ); |
|
| 62 |
+ return Multi_Buffer::set_sample_rate( buf.sample_rate(), buf.length() ); |
|
| 63 |
+} |
|
| 64 |
+ |
|
| 65 |
+ |
|
| 66 |
+// Tracked_Blip_Buffer |
|
| 67 |
+ |
|
| 68 |
+Tracked_Blip_Buffer::Tracked_Blip_Buffer() |
|
| 69 |
+{
|
|
| 70 |
+ last_non_silence = 0; |
|
| 71 |
+} |
|
| 72 |
+ |
|
| 73 |
+void Tracked_Blip_Buffer::clear() |
|
| 74 |
+{
|
|
| 75 |
+ last_non_silence = 0; |
|
| 76 |
+ Blip_Buffer::clear(); |
|
| 77 |
+} |
|
| 78 |
+ |
|
| 79 |
+void Tracked_Blip_Buffer::end_frame( blip_time_t t ) |
|
| 80 |
+{
|
|
| 81 |
+ Blip_Buffer::end_frame( t ); |
|
| 82 |
+ if ( clear_modified() ) |
|
| 83 |
+ last_non_silence = samples_avail() + blip_buffer_extra_; |
|
| 84 |
+} |
|
| 85 |
+ |
|
| 86 |
+blip_ulong Tracked_Blip_Buffer::non_silent() const |
|
| 87 |
+{
|
|
| 88 |
+ return last_non_silence | unsettled(); |
|
| 89 |
+} |
|
| 90 |
+ |
|
| 91 |
+inline void Tracked_Blip_Buffer::remove_( long n ) |
|
| 92 |
+{
|
|
| 93 |
+ if ( (last_non_silence -= n) < 0 ) |
|
| 94 |
+ last_non_silence = 0; |
|
| 95 |
+} |
|
| 96 |
+ |
|
| 97 |
+void Tracked_Blip_Buffer::remove_silence( long n ) |
|
| 98 |
+{
|
|
| 99 |
+ remove_( n ); |
|
| 100 |
+ Blip_Buffer::remove_silence( n ); |
|
| 101 |
+} |
|
| 102 |
+ |
|
| 103 |
+void Tracked_Blip_Buffer::remove_samples( long n ) |
|
| 104 |
+{
|
|
| 105 |
+ remove_( n ); |
|
| 106 |
+ Blip_Buffer::remove_samples( n ); |
|
| 107 |
+} |
|
| 108 |
+ |
|
| 109 |
+void Tracked_Blip_Buffer::remove_all_samples() |
|
| 110 |
+{
|
|
| 111 |
+ long avail = samples_avail(); |
|
| 112 |
+ if ( !non_silent() ) |
|
| 113 |
+ remove_silence( avail ); |
|
| 114 |
+ else |
|
| 115 |
+ remove_samples( avail ); |
|
| 116 |
+} |
|
| 117 |
+ |
|
| 118 |
+long Tracked_Blip_Buffer::read_samples( blip_sample_t* out, long count ) |
|
| 119 |
+{
|
|
| 120 |
+ count = Blip_Buffer::read_samples( out, count ); |
|
| 121 |
+ remove_( count ); |
|
| 122 |
+ return count; |
|
| 123 |
+} |
|
| 124 |
+ |
|
| 125 |
+// Stereo_Buffer |
|
| 126 |
+ |
|
| 127 |
+int const stereo = 2; |
|
| 128 |
+ |
|
| 129 |
+Stereo_Buffer::Stereo_Buffer() : Multi_Buffer( 2 ) |
|
| 130 |
+{
|
|
| 131 |
+ chan.center = mixer.bufs [2] = &bufs [2]; |
|
| 132 |
+ chan.left = mixer.bufs [0] = &bufs [0]; |
|
| 133 |
+ chan.right = mixer.bufs [1] = &bufs [1]; |
|
| 134 |
+ mixer.samples_read = 0; |
|
| 135 |
+} |
|
| 136 |
+ |
|
| 137 |
+Stereo_Buffer::~Stereo_Buffer() { }
|
|
| 138 |
+ |
|
| 139 |
+blargg_err_t Stereo_Buffer::set_sample_rate( long rate, int msec ) |
|
| 140 |
+{
|
|
| 141 |
+ mixer.samples_read = 0; |
|
| 142 |
+ for ( int i = bufs_size; --i >= 0; ) |
|
| 143 |
+ RETURN_ERR( bufs [i].set_sample_rate( rate, msec ) ); |
|
| 144 |
+ return Multi_Buffer::set_sample_rate( bufs [0].sample_rate(), bufs [0].length() ); |
|
| 145 |
+} |
|
| 146 |
+ |
|
| 147 |
+void Stereo_Buffer::clock_rate( long rate ) |
|
| 148 |
+{
|
|
| 149 |
+ for ( int i = bufs_size; --i >= 0; ) |
|
| 150 |
+ bufs [i].clock_rate( rate ); |
|
| 151 |
+} |
|
| 152 |
+ |
|
| 153 |
+void Stereo_Buffer::bass_freq( int bass ) |
|
| 154 |
+{
|
|
| 155 |
+ for ( int i = bufs_size; --i >= 0; ) |
|
| 156 |
+ bufs [i].bass_freq( bass ); |
|
| 157 |
+} |
|
| 158 |
+ |
|
| 159 |
+void Stereo_Buffer::clear() |
|
| 160 |
+{
|
|
| 161 |
+ mixer.samples_read = 0; |
|
| 162 |
+ for ( int i = bufs_size; --i >= 0; ) |
|
| 163 |
+ bufs [i].clear(); |
|
| 164 |
+} |
|
| 165 |
+ |
|
| 166 |
+void Stereo_Buffer::end_frame( blip_time_t time ) |
|
| 167 |
+{
|
|
| 168 |
+ for ( int i = bufs_size; --i >= 0; ) |
|
| 169 |
+ bufs [i].end_frame( time ); |
|
| 170 |
+} |
|
| 171 |
+ |
|
| 172 |
+long Stereo_Buffer::read_samples( blip_sample_t* out, long out_size ) |
|
| 173 |
+{
|
|
| 174 |
+ require( (out_size & 1) == 0 ); // must read an even number of samples |
|
| 175 |
+ out_size = min( out_size, samples_avail() ); |
|
| 176 |
+ |
|
| 177 |
+ int pair_count = int (out_size >> 1); |
|
| 178 |
+ if ( pair_count ) |
|
| 179 |
+ {
|
|
| 180 |
+ mixer.read_pairs( out, pair_count ); |
|
| 181 |
+ |
|
| 182 |
+ if ( samples_avail() <= 0 || immediate_removal() ) |
|
| 183 |
+ {
|
|
| 184 |
+ for ( int i = bufs_size; --i >= 0; ) |
|
| 185 |
+ {
|
|
| 186 |
+ buf_t& b = bufs [i]; |
|
| 187 |
+ // TODO: might miss non-silence settling since it checks END of last read |
|
| 188 |
+ if ( !b.non_silent() ) |
|
| 189 |
+ b.remove_silence( mixer.samples_read ); |
|
| 190 |
+ else |
|
| 191 |
+ b.remove_samples( mixer.samples_read ); |
|
| 192 |
+ } |
|
| 193 |
+ mixer.samples_read = 0; |
|
| 194 |
+ } |
|
| 195 |
+ } |
|
| 196 |
+ return out_size; |
|
| 197 |
+} |
|
| 198 |
+ |
|
| 199 |
+ |
|
| 200 |
+// Stereo_Mixer |
|
| 201 |
+ |
|
| 202 |
+// mixers use a single index value to improve performance on register-challenged processors |
|
| 203 |
+// offset goes from negative to zero |
|
| 204 |
+ |
|
| 205 |
+void Stereo_Mixer::read_pairs( blip_sample_t* out, int count ) |
|
| 206 |
+{
|
|
| 207 |
+ // TODO: if caller never marks buffers as modified, uses mono |
|
| 208 |
+ // except that buffer isn't cleared, so caller can encounter |
|
| 209 |
+ // subtle problems and not realize the cause. |
|
| 210 |
+ samples_read += count; |
|
| 211 |
+ if ( bufs [0]->non_silent() | bufs [1]->non_silent() ) |
|
| 212 |
+ mix_stereo( out, count ); |
|
| 213 |
+ else |
|
| 214 |
+ mix_mono( out, count ); |
|
| 215 |
+} |
|
| 216 |
+ |
|
| 217 |
+void Stereo_Mixer::mix_mono( blip_sample_t* out_, int count ) |
|
| 218 |
+{
|
|
| 219 |
+ int const bass = BLIP_READER_BASS( *bufs [2] ); |
|
| 220 |
+ BLIP_READER_BEGIN( center, *bufs [2] ); |
|
| 221 |
+ BLIP_READER_ADJ_( center, samples_read ); |
|
| 222 |
+ |
|
| 223 |
+ typedef blip_sample_t stereo_blip_sample_t [stereo]; |
|
| 224 |
+ stereo_blip_sample_t* BLIP_RESTRICT out = (stereo_blip_sample_t*) out_ + count; |
|
| 225 |
+ int offset = -count; |
|
| 226 |
+ do |
|
| 227 |
+ {
|
|
| 228 |
+ blargg_long s = BLIP_READER_READ( center ); |
|
| 229 |
+ BLIP_READER_NEXT_IDX_( center, bass, offset ); |
|
| 230 |
+ BLIP_CLAMP( s, s ); |
|
| 231 |
+ |
|
| 232 |
+ out [offset] [0] = (blip_sample_t) s; |
|
| 233 |
+ out [offset] [1] = (blip_sample_t) s; |
|
| 234 |
+ } |
|
| 235 |
+ while ( ++offset ); |
|
| 236 |
+ |
|
| 237 |
+ BLIP_READER_END( center, *bufs [2] ); |
|
| 238 |
+} |
|
| 239 |
+ |
|
| 240 |
+void Stereo_Mixer::mix_stereo( blip_sample_t* out_, int count ) |
|
| 241 |
+{
|
|
| 242 |
+ blip_sample_t* BLIP_RESTRICT out = out_ + count * stereo; |
|
| 243 |
+ |
|
| 244 |
+ // do left + center and right + center separately to reduce register load |
|
| 245 |
+ Tracked_Blip_Buffer* const* buf = &bufs [2]; |
|
| 246 |
+ while ( true ) // loop runs twice |
|
| 247 |
+ {
|
|
| 248 |
+ --buf; |
|
| 249 |
+ --out; |
|
| 250 |
+ |
|
| 251 |
+ int const bass = BLIP_READER_BASS( *bufs [2] ); |
|
| 252 |
+ BLIP_READER_BEGIN( side, **buf ); |
|
| 253 |
+ BLIP_READER_BEGIN( center, *bufs [2] ); |
|
| 254 |
+ |
|
| 255 |
+ BLIP_READER_ADJ_( side, samples_read ); |
|
| 256 |
+ BLIP_READER_ADJ_( center, samples_read ); |
|
| 257 |
+ |
|
| 258 |
+ int offset = -count; |
|
| 259 |
+ do |
|
| 260 |
+ {
|
|
| 261 |
+ blargg_long s = BLIP_READER_READ_RAW( center ) + BLIP_READER_READ_RAW( side ); |
|
| 262 |
+ s >>= blip_sample_bits - 16; |
|
| 263 |
+ BLIP_READER_NEXT_IDX_( side, bass, offset ); |
|
| 264 |
+ BLIP_READER_NEXT_IDX_( center, bass, offset ); |
|
| 265 |
+ BLIP_CLAMP( s, s ); |
|
| 266 |
+ |
|
| 267 |
+ ++offset; // before write since out is decremented to slightly before end |
|
| 268 |
+ out [offset * stereo] = (blip_sample_t) s; |
|
| 269 |
+ } |
|
| 270 |
+ while ( offset ); |
|
| 271 |
+ |
|
| 272 |
+ BLIP_READER_END( side, **buf ); |
|
| 273 |
+ |
|
| 274 |
+ if ( buf != bufs ) |
|
| 275 |
+ continue; |
|
| 276 |
+ |
|
| 277 |
+ // only end center once |
|
| 278 |
+ BLIP_READER_END( center, *bufs [2] ); |
|
| 279 |
+ break; |
|
| 280 |
+ } |
|
| 281 |
+} |