* Removed a couple files that were not being used.
* Cleanups found with clang's -Weverything (and shutting it up about a
lot of things that were ridiculous, as well as ignoring some of the
warnings still coming up).
| ... | ... |
@@ -167,8 +167,7 @@ void Blip_Synth_Fast_::volume_unit(double new_unit) |
| 167 | 167 |
this->delta_factor = static_cast<int>(new_unit * (1L << blip_sample_bits) + 0.5); |
| 168 | 168 |
} |
| 169 | 169 |
|
| 170 |
-#if !BLIP_BUFFER_FAST |
|
| 171 |
- |
|
| 170 |
+#ifndef BLIP_BUFFER_FAST |
|
| 172 | 171 |
Blip_Synth_::Blip_Synth_(short *p, int w) : impulses(p), width(w) |
| 173 | 172 |
{
|
| 174 | 173 |
this->volume_unit_ = 0.0; |
* [2SF] Used more up-to-date asmjit, despite the ugly looking code.
| ... | ... |
@@ -5,6 +5,7 @@ |
| 5 | 5 |
#include <cmath> |
| 6 | 6 |
#include <cstring> |
| 7 | 7 |
#include "Blip_Buffer.h" |
| 8 |
+#include "XSFCommon.h" |
|
| 8 | 9 |
|
| 9 | 10 |
/* Copyright (C) 2003-2007 Shay Green. This module is free software; you |
| 10 | 11 |
can redistribute it and/or modify it under the terms of the GNU Lesser |
| ... | ... |
@@ -63,7 +64,7 @@ void Blip_Buffer::clear(int entire_buffer) |
| 63 | 64 |
void Blip_Buffer::set_sample_rate(long new_rate, long msec) |
| 64 | 65 |
{
|
| 65 | 66 |
// start with maximum length that resampled time can represent |
| 66 |
- long new_size = (ULONG_MAX >> BLIP_BUFFER_ACCURACY) - blip_buffer_extra_ - 64; |
|
| 67 |
+ long new_size = (std::numeric_limits<unsigned long>::max() >> BLIP_BUFFER_ACCURACY) - blip_buffer_extra_ - 64; |
|
| 67 | 68 |
if (msec != blip_max_length) |
| 68 | 69 |
{
|
| 69 | 70 |
long s = (new_rate * (msec + 1) + 999) / 1000; |
| ... | ... |
@@ -289,7 +290,7 @@ void Blip_Synth_::treble_eq(const blip_eq_t &eq) |
| 289 | 290 |
|
| 290 | 291 |
// volume might require rescaling |
| 291 | 292 |
double vol = this->volume_unit_; |
| 292 |
- if (vol) |
|
| 293 |
+ if (!fEqual(vol, 0.0)) |
|
| 293 | 294 |
{
|
| 294 | 295 |
this->volume_unit_ = 0.0; |
| 295 | 296 |
this->volume_unit(vol); |
| ... | ... |
@@ -298,7 +299,7 @@ void Blip_Synth_::treble_eq(const blip_eq_t &eq) |
| 298 | 299 |
|
| 299 | 300 |
void Blip_Synth_::volume_unit(double new_unit) |
| 300 | 301 |
{
|
| 301 |
- if (new_unit != this->volume_unit_) |
|
| 302 |
+ if (!fEqual(new_unit, this->volume_unit_)) |
|
| 302 | 303 |
{
|
| 303 | 304 |
// use default eq if it hasn't been set yet |
| 304 | 305 |
if (!this->kernel_unit) |
| ... | ... |
@@ -60,7 +60,7 @@ void Blip_Buffer::clear(int entire_buffer) |
| 60 | 60 |
} |
| 61 | 61 |
} |
| 62 | 62 |
|
| 63 |
-void Blip_Buffer::set_sample_rate(long new_rate, int msec) |
|
| 63 |
+void Blip_Buffer::set_sample_rate(long new_rate, long msec) |
|
| 64 | 64 |
{
|
| 65 | 65 |
// start with maximum length that resampled time can represent |
| 66 | 66 |
long new_size = (ULONG_MAX >> BLIP_BUFFER_ACCURACY) - blip_buffer_extra_ - 64; |
| ... | ... |
@@ -372,7 +372,7 @@ void Blip_Buffer::mix_samples(const blip_sample_t *in, long count) |
| 372 | 372 |
auto out = &this->buffer_[(this->offset_ >> BLIP_BUFFER_ACCURACY) + blip_widest_impulse_ / 2]; |
| 373 | 373 |
|
| 374 | 374 |
static const int sample_shift = blip_sample_bits - 16; |
| 375 |
- int prev = 0; |
|
| 375 |
+ int32_t prev = 0; |
|
| 376 | 376 |
while (count--) |
| 377 | 377 |
{
|
| 378 | 378 |
int32_t s = static_cast<int32_t>(*in++) << sample_shift; |
| ... | ... |
@@ -264,7 +264,7 @@ void Blip_Synth_::treble_eq(const blip_eq_t &eq) |
| 264 | 264 |
fimpulse[blip_res + half_size + i] = fimpulse[blip_res + half_size - 1 - i]; |
| 265 | 265 |
|
| 266 | 266 |
// starts at 0 |
| 267 |
- std::fill_n(&fimpulse[0], blip_res, 0.0f); |
|
| 267 |
+ std::fill_n(&fimpulse[0], static_cast<unsigned>(blip_res), 0.0f); |
|
| 268 | 268 |
|
| 269 | 269 |
// find rescale factor |
| 270 | 270 |
double total = std::accumulate(&fimpulse[blip_res], &fimpulse[blip_res + half_size], 0.0); |
| ... | ... |
@@ -264,7 +264,7 @@ void Blip_Synth_::treble_eq(const blip_eq_t &eq) |
| 264 | 264 |
fimpulse[blip_res + half_size + i] = fimpulse[blip_res + half_size - 1 - i]; |
| 265 | 265 |
|
| 266 | 266 |
// starts at 0 |
| 267 |
- std::fill(&fimpulse[0], &fimpulse[blip_res], 0.0f); |
|
| 267 |
+ std::fill_n(&fimpulse[0], blip_res, 0.0f); |
|
| 268 | 268 |
|
| 269 | 269 |
// find rescale factor |
| 270 | 270 |
double total = std::accumulate(&fimpulse[blip_res], &fimpulse[blip_res + half_size], 0.0); |
| ... | ... |
@@ -1,10 +1,9 @@ |
| 1 | 1 |
// Blip_Buffer 0.4.1. http://www.slack.net/~ant/ |
| 2 | 2 |
|
| 3 |
+#include <limits> |
|
| 3 | 4 |
#include <numeric> |
| 4 |
-#include <cassert> |
|
| 5 | 5 |
#include <cmath> |
| 6 | 6 |
#include <cstring> |
| 7 |
-#include <cstdlib> |
|
| 8 | 7 |
#include "Blip_Buffer.h" |
| 9 | 8 |
|
| 10 | 9 |
/* Copyright (C) 2003-2007 Shay Green. This module is free software; you |
| ... | ... |
@@ -22,7 +21,7 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 22 | 21 |
|
| 23 | 22 |
Blip_Buffer::Blip_Buffer() |
| 24 | 23 |
{
|
| 25 |
- this->factor_ = static_cast<uint32_t>(LONG_MAX); |
|
| 24 |
+ this->factor_ = static_cast<uint32_t>(std::numeric_limits<long>::max()); |
|
| 26 | 25 |
this->buffer_.clear(); |
| 27 | 26 |
this->buffer_size_ = 0; |
| 28 | 27 |
this->sample_rate_ = 0; |
| ... | ... |
@@ -71,7 +70,7 @@ void Blip_Buffer::set_sample_rate(long new_rate, int msec) |
| 71 | 70 |
if (s < new_size) |
| 72 | 71 |
new_size = s; |
| 73 | 72 |
else |
| 74 |
- assert(0); // fails if requested buffer length exceeds limit |
|
| 73 |
+ assert(false); // fails if requested buffer length exceeds limit |
|
| 75 | 74 |
} |
| 76 | 75 |
|
| 77 | 76 |
if (this->buffer_size_ != new_size) |
| ... | ... |
@@ -131,7 +130,7 @@ blip_time_t Blip_Buffer::count_clocks(long count) const |
| 131 | 130 |
{
|
| 132 | 131 |
if (!this->factor_) |
| 133 | 132 |
{
|
| 134 |
- assert(0); // sample rate and clock rates must be set first |
|
| 133 |
+ assert(false); // sample rate and clock rates must be set first |
|
| 135 | 134 |
return 0; |
| 136 | 135 |
} |
| 137 | 136 |
|
| ... | ... |
@@ -149,8 +148,8 @@ void Blip_Buffer::remove_samples(long count) |
| 149 | 148 |
|
| 150 | 149 |
// copy remaining samples to beginning and clear old samples |
| 151 | 150 |
long remain = this->samples_avail() + blip_buffer_extra_; |
| 152 |
- memmove(&this->buffer_[0], &this->buffer_[count], remain * sizeof(this->buffer_[0])); |
|
| 153 |
- memset(&this->buffer_[remain], 0, count * sizeof(this->buffer_[0])); |
|
| 151 |
+ memmove(&this->buffer_[0], &this->buffer_[count], remain * sizeof(buf_t_)); |
|
| 152 |
+ memset(&this->buffer_[remain], 0, count * sizeof(buf_t_)); |
|
| 154 | 153 |
} |
| 155 | 154 |
} |
| 156 | 155 |
|
| ... | ... |
@@ -159,8 +158,7 @@ void Blip_Buffer::remove_samples(long count) |
| 159 | 158 |
Blip_Synth_Fast_::Blip_Synth_Fast_() |
| 160 | 159 |
{
|
| 161 | 160 |
this->buf = nullptr; |
| 162 |
- this->last_amp = 0; |
|
| 163 |
- this->delta_factor = 0; |
|
| 161 |
+ this->last_amp = this->delta_factor = 0; |
|
| 164 | 162 |
} |
| 165 | 163 |
|
| 166 | 164 |
void Blip_Synth_Fast_::volume_unit(double new_unit) |
| ... | ... |
@@ -175,8 +173,7 @@ Blip_Synth_::Blip_Synth_(short *p, int w) : impulses(p), width(w) |
| 175 | 173 |
this->volume_unit_ = 0.0; |
| 176 | 174 |
this->kernel_unit = 0; |
| 177 | 175 |
this->buf = nullptr; |
| 178 |
- this->last_amp = 0; |
|
| 179 |
- this->delta_factor = 0; |
|
| 176 |
+ this->last_amp = this->delta_factor = 0; |
|
| 180 | 177 |
} |
| 181 | 178 |
|
| 182 | 179 |
#ifndef M_PI |
| ... | ... |
@@ -193,10 +190,10 @@ static void gen_sinc(float *out, int count, double oversample, double treble, do |
| 193 | 190 |
if (treble > 5.0) |
| 194 | 191 |
treble = 5.0; |
| 195 | 192 |
|
| 196 |
- double const maxh = 4096.0; |
|
| 197 |
- double const rolloff = std::pow(10.0, 1.0 / (maxh * 20.0) * treble / (1.0 - cutoff)); |
|
| 198 |
- double const pow_a_n = std::pow(rolloff, maxh - maxh * cutoff); |
|
| 199 |
- double const to_angle = M_PI / 2 / maxh / oversample; |
|
| 193 |
+ static const double maxh = 4096.0; |
|
| 194 |
+ double rolloff = std::pow(10.0, 1.0 / (maxh * 20.0) * treble / (1.0 - cutoff)); |
|
| 195 |
+ double pow_a_n = std::pow(rolloff, maxh - maxh * cutoff); |
|
| 196 |
+ double to_angle = M_PI / 2 / maxh / oversample; |
|
| 200 | 197 |
for (int i = 0; i < count; ++i) |
| 201 | 198 |
{
|
| 202 | 199 |
double angle = ((i - count) * 2 + 1) * to_angle; |
| ... | ... |
@@ -226,10 +223,10 @@ void blip_eq_t::generate(float *out, int count) const |
| 226 | 223 |
|
| 227 | 224 |
gen_sinc(out, count, blip_res * oversample, this->treble, cutoff); |
| 228 | 225 |
|
| 229 |
- // apply (half of) hann window |
|
| 226 |
+ // apply (half of) hamming window |
|
| 230 | 227 |
double to_fraction = M_PI / (count - 1); |
| 231 | 228 |
for (int i = count; i--; ) |
| 232 |
- out[i] *= 0.5f * (1.0f - static_cast<float>(std::cos(i * to_fraction))); |
|
| 229 |
+ out[i] *= 0.54f - 0.46f * static_cast<float>(std::cos(i * to_fraction)); |
|
| 233 | 230 |
} |
| 234 | 231 |
|
| 235 | 232 |
void Blip_Synth_::adjust_impulse() |
| ... | ... |
@@ -245,7 +242,7 @@ void Blip_Synth_::adjust_impulse() |
| 245 | 242 |
if (p == p2) |
| 246 | 243 |
error /= 2; // phase = 0.5 impulse uses same half for both sides |
| 247 | 244 |
this->impulses[size - blip_res + p] += static_cast<short>(error); |
| 248 |
- //printf("error: %ld\n", error);
|
|
| 245 |
+ //printf( "error: %ld\n", error ); |
|
| 249 | 246 |
} |
| 250 | 247 |
|
| 251 | 248 |
//for (int i = blip_res; i--; printf("\n"))
|
| ... | ... |
@@ -336,7 +333,7 @@ void Blip_Synth_::volume_unit(double new_unit) |
| 336 | 333 |
} |
| 337 | 334 |
} |
| 338 | 335 |
this->delta_factor = static_cast<int>(std::floor(factor + 0.5)); |
| 339 |
- //printf("delta_factor: %d, kernel_unit: %d\n", this->delta_factor, this->kernel_unit);
|
|
| 336 |
+ //printf( "delta_factor: %d, kernel_unit: %d\n", delta_factor, kernel_unit ); |
|
| 340 | 337 |
} |
| 341 | 338 |
} |
| 342 | 339 |
#endif |
| ... | ... |
@@ -349,10 +346,10 @@ long Blip_Buffer::read_samples(blip_sample_t *out_, long max_samples, bool stere |
| 349 | 346 |
|
| 350 | 347 |
if (count) |
| 351 | 348 |
{
|
| 352 |
- int bass = BLIP_READER_BASS(*this); |
|
| 349 |
+ int bass = BLIP_READER_BASS(this); |
|
| 353 | 350 |
BLIP_READER_BEGIN(reader, *this); |
| 354 | 351 |
BLIP_READER_ADJ_(reader, count); |
| 355 |
- auto out = out_ + count; |
|
| 352 |
+ auto out = &out_[count]; |
|
| 356 | 353 |
int32_t offset = static_cast<int32_t>(-count); |
| 357 | 354 |
|
| 358 | 355 |
do |
| ... | ... |
@@ -1,12 +1,11 @@ |
| 1 | 1 |
// Blip_Buffer 0.4.1. http://www.slack.net/~ant/ |
| 2 | 2 |
|
| 3 |
-#include "Blip_Buffer.h" |
|
| 4 |
- |
|
| 5 | 3 |
#include <numeric> |
| 6 | 4 |
#include <cassert> |
| 7 | 5 |
#include <cmath> |
| 8 | 6 |
#include <cstring> |
| 9 | 7 |
#include <cstdlib> |
| 8 |
+#include "Blip_Buffer.h" |
|
| 10 | 9 |
|
| 11 | 10 |
/* Copyright (C) 2003-2007 Shay Green. This module is free software; you |
| 12 | 11 |
can redistribute it and/or modify it under the terms of the GNU Lesser |
| ... | ... |
@@ -23,7 +22,7 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | 22 |
|
| 24 | 23 |
Blip_Buffer::Blip_Buffer() |
| 25 | 24 |
{
|
| 26 |
- this->factor_ = LONG_MAX; |
|
| 25 |
+ this->factor_ = static_cast<uint32_t>(LONG_MAX); |
|
| 27 | 26 |
this->buffer_.clear(); |
| 28 | 27 |
this->buffer_size_ = 0; |
| 29 | 28 |
this->sample_rate_ = 0; |
| ... | ... |
@@ -62,7 +61,7 @@ void Blip_Buffer::clear(int entire_buffer) |
| 62 | 61 |
} |
| 63 | 62 |
} |
| 64 | 63 |
|
| 65 |
-Blip_Buffer::blargg_err_t Blip_Buffer::set_sample_rate(long new_rate, int msec) |
|
| 64 |
+void Blip_Buffer::set_sample_rate(long new_rate, int msec) |
|
| 66 | 65 |
{
|
| 67 | 66 |
// start with maximum length that resampled time can represent |
| 68 | 67 |
long new_size = (ULONG_MAX >> BLIP_BUFFER_ACCURACY) - blip_buffer_extra_ - 64; |
| ... | ... |
@@ -92,8 +91,6 @@ Blip_Buffer::blargg_err_t Blip_Buffer::set_sample_rate(long new_rate, int msec) |
| 92 | 91 |
this->bass_freq(this->bass_freq_); |
| 93 | 92 |
|
| 94 | 93 |
this->clear(); |
| 95 |
- |
|
| 96 |
- return 0; // success |
|
| 97 | 94 |
} |
| 98 | 95 |
|
| 99 | 96 |
blip_resampled_time_t Blip_Buffer::clock_rate_factor(long rate) const |
| ... | ... |
@@ -153,7 +150,7 @@ void Blip_Buffer::remove_samples(long count) |
| 153 | 150 |
// copy remaining samples to beginning and clear old samples |
| 154 | 151 |
long remain = this->samples_avail() + blip_buffer_extra_; |
| 155 | 152 |
memmove(&this->buffer_[0], &this->buffer_[count], remain * sizeof(this->buffer_[0])); |
| 156 |
- memset(&this->buffer_[0] + remain, 0, count * sizeof(this->buffer_[0])); |
|
| 153 |
+ memset(&this->buffer_[remain], 0, count * sizeof(this->buffer_[0])); |
|
| 157 | 154 |
} |
| 158 | 155 |
} |
| 159 | 156 |
|
| ... | ... |
@@ -182,8 +179,9 @@ Blip_Synth_::Blip_Synth_(short *p, int w) : impulses(p), width(w) |
| 182 | 179 |
this->delta_factor = 0; |
| 183 | 180 |
} |
| 184 | 181 |
|
| 185 |
-#undef M_PI |
|
| 182 |
+#ifndef M_PI |
|
| 186 | 183 |
static const double M_PI = 3.1415926535897932384626433832795029; |
| 184 |
+#endif |
|
| 187 | 185 |
|
| 188 | 186 |
static void gen_sinc(float *out, int count, double oversample, double treble, double cutoff) |
| 189 | 187 |
{
|
| ... | ... |
@@ -251,7 +249,7 @@ void Blip_Synth_::adjust_impulse() |
| 251 | 249 |
} |
| 252 | 250 |
|
| 253 | 251 |
//for (int i = blip_res; i--; printf("\n"))
|
| 254 |
- //for (int j = 0; j < width / 2; ++j) |
|
| 252 |
+ //for (int j = 0; j < this->width / 2; ++j) |
|
| 255 | 253 |
//printf("%5ld,", this->impulses[j * blip_res + i + 1]);
|
| 256 | 254 |
} |
| 257 | 255 |
|
| ... | ... |
@@ -259,7 +257,7 @@ void Blip_Synth_::treble_eq(const blip_eq_t &eq) |
| 259 | 257 |
{
|
| 260 | 258 |
float fimpulse[blip_res / 2 * (blip_widest_impulse_ - 1) + blip_res * 2]; |
| 261 | 259 |
|
| 262 |
- static const int half_size = blip_res / 2 * (this->width - 1); |
|
| 260 |
+ int half_size = blip_res / 2 * (this->width - 1); |
|
| 263 | 261 |
eq.generate(&fimpulse[blip_res], half_size); |
| 264 | 262 |
|
| 265 | 263 |
int i; |
| ... | ... |
@@ -376,7 +374,7 @@ void Blip_Buffer::mix_samples(const blip_sample_t *in, long count) |
| 376 | 374 |
{
|
| 377 | 375 |
auto out = &this->buffer_[(this->offset_ >> BLIP_BUFFER_ACCURACY) + blip_widest_impulse_ / 2]; |
| 378 | 376 |
|
| 379 |
- int sample_shift = blip_sample_bits - 16; |
|
| 377 |
+ static const int sample_shift = blip_sample_bits - 16; |
|
| 380 | 378 |
int prev = 0; |
| 381 | 379 |
while (count--) |
| 382 | 380 |
{
|
(As an aside, blargg's code style makes me go blarg myself.)
| ... | ... |
@@ -2,11 +2,11 @@ |
| 2 | 2 |
|
| 3 | 3 |
#include "Blip_Buffer.h" |
| 4 | 4 |
|
| 5 |
-#include <assert.h> |
|
| 6 |
-#include <limits.h> |
|
| 7 |
-#include <string.h> |
|
| 8 |
-#include <stdlib.h> |
|
| 9 |
-#include <math.h> |
|
| 5 |
+#include <numeric> |
|
| 6 |
+#include <cassert> |
|
| 7 |
+#include <cmath> |
|
| 8 |
+#include <cstring> |
|
| 9 |
+#include <cstdlib> |
|
| 10 | 10 |
|
| 11 | 11 |
/* Copyright (C) 2003-2007 Shay Green. This module is free software; you |
| 12 | 12 |
can redistribute it and/or modify it under the terms of the GNU Lesser |
| ... | ... |
@@ -21,167 +21,139 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 21 | 21 |
|
| 22 | 22 |
// TODO: use scoped for variables in treble_eq() |
| 23 | 23 |
|
| 24 |
-#ifdef BLARGG_ENABLE_OPTIMIZER |
|
| 25 |
- #include BLARGG_ENABLE_OPTIMIZER |
|
| 26 |
-#endif |
|
| 27 |
- |
|
| 28 |
-int const silent_buf_size = 1; // size used for Silent_Blip_Buffer |
|
| 29 |
- |
|
| 30 | 24 |
Blip_Buffer::Blip_Buffer() |
| 31 | 25 |
{
|
| 32 |
- factor_ = LONG_MAX; |
|
| 33 |
- buffer_ = 0; |
|
| 34 |
- buffer_size_ = 0; |
|
| 35 |
- sample_rate_ = 0; |
|
| 36 |
- bass_shift_ = 0; |
|
| 37 |
- clock_rate_ = 0; |
|
| 38 |
- bass_freq_ = 16; |
|
| 39 |
- length_ = 0; |
|
| 26 |
+ this->factor_ = LONG_MAX; |
|
| 27 |
+ this->buffer_.clear(); |
|
| 28 |
+ this->buffer_size_ = 0; |
|
| 29 |
+ this->sample_rate_ = 0; |
|
| 30 |
+ this->bass_shift_ = 0; |
|
| 31 |
+ this->clock_rate_ = 0; |
|
| 32 |
+ this->bass_freq_ = 16; |
|
| 33 |
+ this->length_ = 0; |
|
| 40 | 34 |
|
| 41 | 35 |
// assumptions code makes about implementation-defined features |
| 42 |
- #ifndef NDEBUG |
|
| 43 |
- // right shift of negative value preserves sign |
|
| 44 |
- buf_t_ i = -0x7FFFFFFE; |
|
| 45 |
- assert( (i >> 1) == -0x3FFFFFFF ); |
|
| 46 |
- |
|
| 47 |
- // casting to short truncates to 16 bits and sign-extends |
|
| 48 |
- i = 0x18000; |
|
| 49 |
- assert( (short) i == -0x8000 ); |
|
| 50 |
- #endif |
|
| 36 |
+#ifndef NDEBUG |
|
| 37 |
+ // right shift of negative value preserves sign |
|
| 38 |
+ buf_t_ i = -0x7FFFFFFE; |
|
| 39 |
+ assert((i >> 1) == -0x3FFFFFFF); |
|
| 40 |
+ |
|
| 41 |
+ // casting to short truncates to 16 bits and sign-extends |
|
| 42 |
+ i = 0x18000; |
|
| 43 |
+ assert(static_cast<short>(i) == -0x8000); |
|
| 44 |
+#endif |
|
| 51 | 45 |
|
| 52 |
- clear(); |
|
| 46 |
+ this->clear(); |
|
| 53 | 47 |
} |
| 54 | 48 |
|
| 55 | 49 |
Blip_Buffer::~Blip_Buffer() |
| 56 | 50 |
{
|
| 57 |
- if ( buffer_size_ != silent_buf_size ) |
|
| 58 |
- free( buffer_ ); |
|
| 59 | 51 |
} |
| 60 | 52 |
|
| 61 |
-Silent_Blip_Buffer::Silent_Blip_Buffer() |
|
| 53 |
+void Blip_Buffer::clear(int entire_buffer) |
|
| 62 | 54 |
{
|
| 63 |
- factor_ = 0; |
|
| 64 |
- buffer_ = buf; |
|
| 65 |
- buffer_size_ = silent_buf_size; |
|
| 66 |
- clear(); |
|
| 67 |
-} |
|
| 68 |
- |
|
| 69 |
-void Blip_Buffer::clear( int entire_buffer ) |
|
| 70 |
-{
|
|
| 71 |
- offset_ = 0; |
|
| 72 |
- reader_accum_ = 0; |
|
| 73 |
- modified_ = 0; |
|
| 74 |
- if ( buffer_ ) |
|
| 55 |
+ this->offset_ = 0; |
|
| 56 |
+ this->reader_accum_ = 0; |
|
| 57 |
+ this->modified_ = nullptr; |
|
| 58 |
+ if (!this->buffer_.empty()) |
|
| 75 | 59 |
{
|
| 76 |
- long count = (entire_buffer ? buffer_size_ : samples_avail()); |
|
| 77 |
- memset( buffer_, 0, (count + blip_buffer_extra_) * sizeof (buf_t_) ); |
|
| 60 |
+ long count = entire_buffer ? this->buffer_size_ : this->samples_avail(); |
|
| 61 |
+ memset(&this->buffer_[0], 0, (count + blip_buffer_extra_) * sizeof(buf_t_)); |
|
| 78 | 62 |
} |
| 79 | 63 |
} |
| 80 | 64 |
|
| 81 |
-Blip_Buffer::blargg_err_t Blip_Buffer::set_sample_rate( long new_rate, int msec ) |
|
| 65 |
+Blip_Buffer::blargg_err_t Blip_Buffer::set_sample_rate(long new_rate, int msec) |
|
| 82 | 66 |
{
|
| 83 |
- if ( buffer_size_ == silent_buf_size ) |
|
| 84 |
- {
|
|
| 85 |
- assert( 0 ); |
|
| 86 |
- return "Internal (tried to resize Silent_Blip_Buffer)"; |
|
| 87 |
- } |
|
| 88 |
- |
|
| 89 | 67 |
// start with maximum length that resampled time can represent |
| 90 | 68 |
long new_size = (ULONG_MAX >> BLIP_BUFFER_ACCURACY) - blip_buffer_extra_ - 64; |
| 91 |
- if ( msec != blip_max_length ) |
|
| 69 |
+ if (msec != blip_max_length) |
|
| 92 | 70 |
{
|
| 93 | 71 |
long s = (new_rate * (msec + 1) + 999) / 1000; |
| 94 |
- if ( s < new_size ) |
|
| 72 |
+ if (s < new_size) |
|
| 95 | 73 |
new_size = s; |
| 96 | 74 |
else |
| 97 |
- assert( 0 ); // fails if requested buffer length exceeds limit |
|
| 75 |
+ assert(0); // fails if requested buffer length exceeds limit |
|
| 98 | 76 |
} |
| 99 | 77 |
|
| 100 |
- if ( buffer_size_ != new_size ) |
|
| 101 |
- {
|
|
| 102 |
- void* p = realloc( buffer_, (new_size + blip_buffer_extra_) * sizeof *buffer_ ); |
|
| 103 |
- if ( !p ) |
|
| 104 |
- return "Out of memory"; |
|
| 105 |
- buffer_ = (buf_t_*) p; |
|
| 106 |
- } |
|
| 78 |
+ if (this->buffer_size_ != new_size) |
|
| 79 |
+ this->buffer_.resize(new_size + blip_buffer_extra_); |
|
| 107 | 80 |
|
| 108 |
- buffer_size_ = new_size; |
|
| 109 |
- assert( buffer_size_ != silent_buf_size ); // size should never happen to match this |
|
| 81 |
+ this->buffer_size_ = new_size; |
|
| 110 | 82 |
|
| 111 | 83 |
// update things based on the sample rate |
| 112 |
- sample_rate_ = new_rate; |
|
| 113 |
- length_ = new_size * 1000 / new_rate - 1; |
|
| 114 |
- if ( msec ) |
|
| 115 |
- assert( length_ == msec ); // ensure length is same as that passed in |
|
| 84 |
+ this->sample_rate_ = new_rate; |
|
| 85 |
+ this->length_ = new_size * 1000 / new_rate - 1; |
|
| 86 |
+ if (msec) |
|
| 87 |
+ assert(this->length_ == msec); // ensure length is same as that passed in |
|
| 116 | 88 |
|
| 117 | 89 |
// update these since they depend on sample rate |
| 118 |
- if ( clock_rate_ ) |
|
| 119 |
- clock_rate( clock_rate_ ); |
|
| 120 |
- bass_freq( bass_freq_ ); |
|
| 90 |
+ if (this->clock_rate_) |
|
| 91 |
+ this->clock_rate(this->clock_rate_); |
|
| 92 |
+ this->bass_freq(this->bass_freq_); |
|
| 121 | 93 |
|
| 122 |
- clear(); |
|
| 94 |
+ this->clear(); |
|
| 123 | 95 |
|
| 124 | 96 |
return 0; // success |
| 125 | 97 |
} |
| 126 | 98 |
|
| 127 |
-blip_resampled_time_t Blip_Buffer::clock_rate_factor( long rate ) const |
|
| 99 |
+blip_resampled_time_t Blip_Buffer::clock_rate_factor(long rate) const |
|
| 128 | 100 |
{
|
| 129 |
- double ratio = (double) sample_rate_ / rate; |
|
| 130 |
- blip_long factor = (blip_long) floor( ratio * (1L << BLIP_BUFFER_ACCURACY) + 0.5 ); |
|
| 131 |
- assert( factor > 0 || !sample_rate_ ); // fails if clock/output ratio is too large |
|
| 132 |
- return (blip_resampled_time_t) factor; |
|
| 101 |
+ double ratio = static_cast<double>(this->sample_rate_) / rate; |
|
| 102 |
+ int32_t factor = static_cast<int32_t>(std::floor(ratio * (1L << BLIP_BUFFER_ACCURACY) + 0.5)); |
|
| 103 |
+ assert(factor > 0 || !this->sample_rate_); // fails if clock/output ratio is too large |
|
| 104 |
+ return static_cast<blip_resampled_time_t>(factor); |
|
| 133 | 105 |
} |
| 134 | 106 |
|
| 135 |
-void Blip_Buffer::bass_freq( int freq ) |
|
| 107 |
+void Blip_Buffer::bass_freq(int freq) |
|
| 136 | 108 |
{
|
| 137 |
- bass_freq_ = freq; |
|
| 109 |
+ this->bass_freq_ = freq; |
|
| 138 | 110 |
int shift = 31; |
| 139 |
- if ( freq > 0 ) |
|
| 111 |
+ if (freq > 0) |
|
| 140 | 112 |
{
|
| 141 | 113 |
shift = 13; |
| 142 |
- long f = (freq << 16) / sample_rate_; |
|
| 143 |
- while ( (f >>= 1) && --shift ) { }
|
|
| 114 |
+ long f = (freq << 16) / this->sample_rate_; |
|
| 115 |
+ while ((f >>= 1) && --shift); |
|
| 144 | 116 |
} |
| 145 |
- bass_shift_ = shift; |
|
| 117 |
+ this->bass_shift_ = shift; |
|
| 146 | 118 |
} |
| 147 | 119 |
|
| 148 |
-void Blip_Buffer::end_frame( blip_time_t t ) |
|
| 120 |
+void Blip_Buffer::end_frame(blip_time_t t) |
|
| 149 | 121 |
{
|
| 150 |
- offset_ += t * factor_; |
|
| 151 |
- assert( samples_avail() <= (long) buffer_size_ ); // fails if time is past end of buffer |
|
| 122 |
+ this->offset_ += t * this->factor_; |
|
| 123 |
+ assert(this->samples_avail() <= static_cast<long>(this->buffer_size_)); // fails if time is past end of buffer |
|
| 152 | 124 |
} |
| 153 | 125 |
|
| 154 |
-long Blip_Buffer::count_samples( blip_time_t t ) const |
|
| 126 |
+long Blip_Buffer::count_samples(blip_time_t t) const |
|
| 155 | 127 |
{
|
| 156 |
- blip_resampled_time_t last_sample = resampled_time( t ) >> BLIP_BUFFER_ACCURACY; |
|
| 157 |
- blip_resampled_time_t first_sample = offset_ >> BLIP_BUFFER_ACCURACY; |
|
| 158 |
- return long (last_sample - first_sample); |
|
| 128 |
+ auto last_sample = this->resampled_time(t) >> BLIP_BUFFER_ACCURACY; |
|
| 129 |
+ auto first_sample = this->offset_ >> BLIP_BUFFER_ACCURACY; |
|
| 130 |
+ return static_cast<long>(last_sample - first_sample); |
|
| 159 | 131 |
} |
| 160 | 132 |
|
| 161 |
-blip_time_t Blip_Buffer::count_clocks( long count ) const |
|
| 133 |
+blip_time_t Blip_Buffer::count_clocks(long count) const |
|
| 162 | 134 |
{
|
| 163 |
- if ( !factor_ ) |
|
| 135 |
+ if (!this->factor_) |
|
| 164 | 136 |
{
|
| 165 |
- assert( 0 ); // sample rate and clock rates must be set first |
|
| 137 |
+ assert(0); // sample rate and clock rates must be set first |
|
| 166 | 138 |
return 0; |
| 167 | 139 |
} |
| 168 | 140 |
|
| 169 |
- if ( count > buffer_size_ ) |
|
| 170 |
- count = buffer_size_; |
|
| 171 |
- blip_resampled_time_t time = (blip_resampled_time_t) count << BLIP_BUFFER_ACCURACY; |
|
| 172 |
- return (blip_time_t) ((time - offset_ + factor_ - 1) / factor_); |
|
| 141 |
+ if (count > this->buffer_size_) |
|
| 142 |
+ count = this->buffer_size_; |
|
| 143 |
+ auto time = static_cast<blip_resampled_time_t>(count) << BLIP_BUFFER_ACCURACY; |
|
| 144 |
+ return static_cast<blip_time_t>((time - this->offset_ + this->factor_ - 1) / this->factor_); |
|
| 173 | 145 |
} |
| 174 | 146 |
|
| 175 |
-void Blip_Buffer::remove_samples( long count ) |
|
| 147 |
+void Blip_Buffer::remove_samples(long count) |
|
| 176 | 148 |
{
|
| 177 |
- if ( count ) |
|
| 149 |
+ if (count) |
|
| 178 | 150 |
{
|
| 179 |
- remove_silence( count ); |
|
| 151 |
+ this->remove_silence(count); |
|
| 180 | 152 |
|
| 181 | 153 |
// copy remaining samples to beginning and clear old samples |
| 182 |
- long remain = samples_avail() + blip_buffer_extra_; |
|
| 183 |
- memmove( buffer_, buffer_ + count, remain * sizeof *buffer_ ); |
|
| 184 |
- memset( buffer_ + remain, 0, count * sizeof *buffer_ ); |
|
| 154 |
+ long remain = this->samples_avail() + blip_buffer_extra_; |
|
| 155 |
+ memmove(&this->buffer_[0], &this->buffer_[count], remain * sizeof(this->buffer_[0])); |
|
| 156 |
+ memset(&this->buffer_[0] + remain, 0, count * sizeof(this->buffer_[0])); |
|
| 185 | 157 |
} |
| 186 | 158 |
} |
| 187 | 159 |
|
| ... | ... |
@@ -189,277 +161,229 @@ void Blip_Buffer::remove_samples( long count ) |
| 189 | 161 |
|
| 190 | 162 |
Blip_Synth_Fast_::Blip_Synth_Fast_() |
| 191 | 163 |
{
|
| 192 |
- buf = 0; |
|
| 193 |
- last_amp = 0; |
|
| 194 |
- delta_factor = 0; |
|
| 164 |
+ this->buf = nullptr; |
|
| 165 |
+ this->last_amp = 0; |
|
| 166 |
+ this->delta_factor = 0; |
|
| 195 | 167 |
} |
| 196 | 168 |
|
| 197 |
-void Blip_Synth_Fast_::volume_unit( double new_unit ) |
|
| 169 |
+void Blip_Synth_Fast_::volume_unit(double new_unit) |
|
| 198 | 170 |
{
|
| 199 |
- delta_factor = int (new_unit * (1L << blip_sample_bits) + 0.5); |
|
| 171 |
+ this->delta_factor = static_cast<int>(new_unit * (1L << blip_sample_bits) + 0.5); |
|
| 200 | 172 |
} |
| 201 | 173 |
|
| 202 | 174 |
#if !BLIP_BUFFER_FAST |
| 203 | 175 |
|
| 204 |
-Blip_Synth_::Blip_Synth_( short* p, int w ) : |
|
| 205 |
- impulses( p ), |
|
| 206 |
- width( w ) |
|
| 176 |
+Blip_Synth_::Blip_Synth_(short *p, int w) : impulses(p), width(w) |
|
| 207 | 177 |
{
|
| 208 |
- volume_unit_ = 0.0; |
|
| 209 |
- kernel_unit = 0; |
|
| 210 |
- buf = 0; |
|
| 211 |
- last_amp = 0; |
|
| 212 |
- delta_factor = 0; |
|
| 178 |
+ this->volume_unit_ = 0.0; |
|
| 179 |
+ this->kernel_unit = 0; |
|
| 180 |
+ this->buf = nullptr; |
|
| 181 |
+ this->last_amp = 0; |
|
| 182 |
+ this->delta_factor = 0; |
|
| 213 | 183 |
} |
| 214 | 184 |
|
| 215 |
-#undef PI |
|
| 216 |
-#define PI 3.1415926535897932384626433832795029 |
|
| 185 |
+#undef M_PI |
|
| 186 |
+static const double M_PI = 3.1415926535897932384626433832795029; |
|
| 217 | 187 |
|
| 218 |
-static void gen_sinc( float* out, int count, double oversample, double treble, double cutoff ) |
|
| 188 |
+static void gen_sinc(float *out, int count, double oversample, double treble, double cutoff) |
|
| 219 | 189 |
{
|
| 220 |
- if ( cutoff >= 0.999 ) |
|
| 190 |
+ if (cutoff >= 0.999) |
|
| 221 | 191 |
cutoff = 0.999; |
| 222 | 192 |
|
| 223 |
- if ( treble < -300.0 ) |
|
| 193 |
+ if (treble < -300.0) |
|
| 224 | 194 |
treble = -300.0; |
| 225 |
- if ( treble > 5.0 ) |
|
| 195 |
+ if (treble > 5.0) |
|
| 226 | 196 |
treble = 5.0; |
| 227 | 197 |
|
| 228 | 198 |
double const maxh = 4096.0; |
| 229 |
- double const rolloff = pow( 10.0, 1.0 / (maxh * 20.0) * treble / (1.0 - cutoff) ); |
|
| 230 |
- double const pow_a_n = pow( rolloff, maxh - maxh * cutoff ); |
|
| 231 |
- double const to_angle = PI / 2 / maxh / oversample; |
|
| 232 |
- for ( int i = 0; i < count; i++ ) |
|
| 199 |
+ double const rolloff = std::pow(10.0, 1.0 / (maxh * 20.0) * treble / (1.0 - cutoff)); |
|
| 200 |
+ double const pow_a_n = std::pow(rolloff, maxh - maxh * cutoff); |
|
| 201 |
+ double const to_angle = M_PI / 2 / maxh / oversample; |
|
| 202 |
+ for (int i = 0; i < count; ++i) |
|
| 233 | 203 |
{
|
| 234 | 204 |
double angle = ((i - count) * 2 + 1) * to_angle; |
| 235 |
- double c = rolloff * cos( (maxh - 1.0) * angle ) - cos( maxh * angle ); |
|
| 236 |
- double cos_nc_angle = cos( maxh * cutoff * angle ); |
|
| 237 |
- double cos_nc1_angle = cos( (maxh * cutoff - 1.0) * angle ); |
|
| 238 |
- double cos_angle = cos( angle ); |
|
| 205 |
+ double c = rolloff * std::cos((maxh - 1.0) * angle) - std::cos(maxh * angle); |
|
| 206 |
+ double cos_nc_angle = std::cos(maxh * cutoff * angle); |
|
| 207 |
+ double cos_nc1_angle = std::cos((maxh * cutoff - 1.0) * angle); |
|
| 208 |
+ double cos_angle = std::cos(angle); |
|
| 239 | 209 |
|
| 240 | 210 |
c = c * pow_a_n - rolloff * cos_nc1_angle + cos_nc_angle; |
| 241 | 211 |
double d = 1.0 + rolloff * (rolloff - cos_angle - cos_angle); |
| 242 | 212 |
double b = 2.0 - cos_angle - cos_angle; |
| 243 | 213 |
double a = 1.0 - cos_angle - cos_nc_angle + cos_nc1_angle; |
| 244 | 214 |
|
| 245 |
- out [i] = (float) ((a * d + c * b) / (b * d)); // a / b + c / d |
|
| 215 |
+ out[i] = static_cast<float>((a * d + c * b) / (b * d)); // a / b + c / d |
|
| 246 | 216 |
} |
| 247 | 217 |
} |
| 248 | 218 |
|
| 249 |
-void blip_eq_t::generate( float* out, int count ) const |
|
| 219 |
+void blip_eq_t::generate(float *out, int count) const |
|
| 250 | 220 |
{
|
| 251 | 221 |
// lower cutoff freq for narrow kernels with their wider transition band |
| 252 | 222 |
// (8 points->1.49, 16 points->1.15) |
| 253 | 223 |
double oversample = blip_res * 2.25 / count + 0.85; |
| 254 |
- double half_rate = sample_rate * 0.5; |
|
| 255 |
- if ( cutoff_freq ) |
|
| 256 |
- oversample = half_rate / cutoff_freq; |
|
| 257 |
- double cutoff = rolloff_freq * oversample / half_rate; |
|
| 224 |
+ double half_rate = this->sample_rate * 0.5; |
|
| 225 |
+ if (this->cutoff_freq) |
|
| 226 |
+ oversample = half_rate / this->cutoff_freq; |
|
| 227 |
+ double cutoff = this->rolloff_freq * oversample / half_rate; |
|
| 258 | 228 |
|
| 259 |
- gen_sinc( out, count, blip_res * oversample, treble, cutoff ); |
|
| 229 |
+ gen_sinc(out, count, blip_res * oversample, this->treble, cutoff); |
|
| 260 | 230 |
|
| 261 |
- // apply (half of) hamming window |
|
| 262 |
- double to_fraction = PI / (count - 1); |
|
| 263 |
- for ( int i = count; i--; ) |
|
| 264 |
- out [i] *= 0.54f - 0.46f * (float) cos( i * to_fraction ); |
|
| 231 |
+ // apply (half of) hann window |
|
| 232 |
+ double to_fraction = M_PI / (count - 1); |
|
| 233 |
+ for (int i = count; i--; ) |
|
| 234 |
+ out[i] *= 0.5f * (1.0f - static_cast<float>(std::cos(i * to_fraction))); |
|
| 265 | 235 |
} |
| 266 | 236 |
|
| 267 | 237 |
void Blip_Synth_::adjust_impulse() |
| 268 | 238 |
{
|
| 269 | 239 |
// sum pairs for each phase and add error correction to end of first half |
| 270 |
- int const size = impulses_size(); |
|
| 271 |
- for ( int p = blip_res; p-- >= blip_res / 2; ) |
|
| 240 |
+ int size = this->impulses_size(); |
|
| 241 |
+ for (int p = blip_res; p-- >= blip_res / 2; ) |
|
| 272 | 242 |
{
|
| 273 | 243 |
int p2 = blip_res - 2 - p; |
| 274 |
- long error = kernel_unit; |
|
| 275 |
- for ( int i = 1; i < size; i += blip_res ) |
|
| 276 |
- {
|
|
| 277 |
- error -= impulses [i + p ]; |
|
| 278 |
- error -= impulses [i + p2]; |
|
| 279 |
- } |
|
| 280 |
- if ( p == p2 ) |
|
| 244 |
+ long error = this->kernel_unit; |
|
| 245 |
+ for (int i = 1; i < size; i += blip_res) |
|
| 246 |
+ error -= this->impulses[i + p] + this->impulses[i + p2]; |
|
| 247 |
+ if (p == p2) |
|
| 281 | 248 |
error /= 2; // phase = 0.5 impulse uses same half for both sides |
| 282 |
- impulses [size - blip_res + p] += (short) error; |
|
| 283 |
- //printf( "error: %ld\n", error ); |
|
| 249 |
+ this->impulses[size - blip_res + p] += static_cast<short>(error); |
|
| 250 |
+ //printf("error: %ld\n", error);
|
|
| 284 | 251 |
} |
| 285 | 252 |
|
| 286 |
- //for ( int i = blip_res; i--; printf( "\n" ) ) |
|
| 287 |
- // for ( int j = 0; j < width / 2; j++ ) |
|
| 288 |
- // printf( "%5ld,", impulses [j * blip_res + i + 1] ); |
|
| 253 |
+ //for (int i = blip_res; i--; printf("\n"))
|
|
| 254 |
+ //for (int j = 0; j < width / 2; ++j) |
|
| 255 |
+ //printf("%5ld,", this->impulses[j * blip_res + i + 1]);
|
|
| 289 | 256 |
} |
| 290 | 257 |
|
| 291 |
-void Blip_Synth_::treble_eq( blip_eq_t const& eq ) |
|
| 258 |
+void Blip_Synth_::treble_eq(const blip_eq_t &eq) |
|
| 292 | 259 |
{
|
| 293 |
- float fimpulse [blip_res / 2 * (blip_widest_impulse_ - 1) + blip_res * 2]; |
|
| 260 |
+ float fimpulse[blip_res / 2 * (blip_widest_impulse_ - 1) + blip_res * 2]; |
|
| 294 | 261 |
|
| 295 |
- int const half_size = blip_res / 2 * (width - 1); |
|
| 296 |
- eq.generate( &fimpulse [blip_res], half_size ); |
|
| 262 |
+ static const int half_size = blip_res / 2 * (this->width - 1); |
|
| 263 |
+ eq.generate(&fimpulse[blip_res], half_size); |
|
| 297 | 264 |
|
| 298 | 265 |
int i; |
| 299 | 266 |
|
| 300 | 267 |
// need mirror slightly past center for calculation |
| 301 |
- for ( i = blip_res; i--; ) |
|
| 302 |
- fimpulse [blip_res + half_size + i] = fimpulse [blip_res + half_size - 1 - i]; |
|
| 268 |
+ for (i = blip_res; i--; ) |
|
| 269 |
+ fimpulse[blip_res + half_size + i] = fimpulse[blip_res + half_size - 1 - i]; |
|
| 303 | 270 |
|
| 304 | 271 |
// starts at 0 |
| 305 |
- for ( i = 0; i < blip_res; i++ ) |
|
| 306 |
- fimpulse [i] = 0.0f; |
|
| 272 |
+ std::fill(&fimpulse[0], &fimpulse[blip_res], 0.0f); |
|
| 307 | 273 |
|
| 308 | 274 |
// find rescale factor |
| 309 |
- double total = 0.0; |
|
| 310 |
- for ( i = 0; i < half_size; i++ ) |
|
| 311 |
- total += fimpulse [blip_res + i]; |
|
| 275 |
+ double total = std::accumulate(&fimpulse[blip_res], &fimpulse[blip_res + half_size], 0.0); |
|
| 312 | 276 |
|
| 313 |
- //double const base_unit = 44800.0 - 128 * 18; // allows treble up to +0 dB |
|
| 314 |
- //double const base_unit = 37888.0; // allows treble to +5 dB |
|
| 315 |
- double const base_unit = 32768.0; // necessary for blip_unscaled to work |
|
| 277 |
+ //static const double base_unit = 44800.0 - 128 * 18; // allows treble up to +0 dB |
|
| 278 |
+ //static const double base_unit = 37888.0; // allows treble to +5 dB |
|
| 279 |
+ static const double base_unit = 32768.0; // necessary for blip_unscaled to work |
|
| 316 | 280 |
double rescale = base_unit / 2 / total; |
| 317 |
- kernel_unit = (long) base_unit; |
|
| 281 |
+ this->kernel_unit = static_cast<long>(base_unit); |
|
| 318 | 282 |
|
| 319 | 283 |
// integrate, first difference, rescale, convert to int |
| 320 | 284 |
double sum = 0.0; |
| 321 | 285 |
double next = 0.0; |
| 322 |
- int const size = this->impulses_size(); |
|
| 323 |
- for ( i = 0; i < size; i++ ) |
|
| 286 |
+ int size = this->impulses_size(); |
|
| 287 |
+ for (i = 0; i < size; ++i) |
|
| 324 | 288 |
{
|
| 325 |
- impulses [i] = (short) (int) floor( (next - sum) * rescale + 0.5 ); |
|
| 326 |
- sum += fimpulse [i]; |
|
| 327 |
- next += fimpulse [i + blip_res]; |
|
| 289 |
+ this->impulses[i] = static_cast<short>(std::floor((next - sum) * rescale + 0.5)); |
|
| 290 |
+ sum += fimpulse[i]; |
|
| 291 |
+ next += fimpulse[i + blip_res]; |
|
| 328 | 292 |
} |
| 329 |
- adjust_impulse(); |
|
| 293 |
+ this->adjust_impulse(); |
|
| 330 | 294 |
|
| 331 | 295 |
// volume might require rescaling |
| 332 |
- double vol = volume_unit_; |
|
| 333 |
- if ( vol ) |
|
| 296 |
+ double vol = this->volume_unit_; |
|
| 297 |
+ if (vol) |
|
| 334 | 298 |
{
|
| 335 |
- volume_unit_ = 0.0; |
|
| 336 |
- volume_unit( vol ); |
|
| 299 |
+ this->volume_unit_ = 0.0; |
|
| 300 |
+ this->volume_unit(vol); |
|
| 337 | 301 |
} |
| 338 | 302 |
} |
| 339 | 303 |
|
| 340 |
-void Blip_Synth_::volume_unit( double new_unit ) |
|
| 304 |
+void Blip_Synth_::volume_unit(double new_unit) |
|
| 341 | 305 |
{
|
| 342 |
- if ( new_unit != volume_unit_ ) |
|
| 306 |
+ if (new_unit != this->volume_unit_) |
|
| 343 | 307 |
{
|
| 344 | 308 |
// use default eq if it hasn't been set yet |
| 345 |
- if ( !kernel_unit ) |
|
| 346 |
- treble_eq( -8.0 ); |
|
| 309 |
+ if (!this->kernel_unit) |
|
| 310 |
+ this->treble_eq(-8.0); |
|
| 347 | 311 |
|
| 348 |
- volume_unit_ = new_unit; |
|
| 349 |
- double factor = new_unit * (1L << blip_sample_bits) / kernel_unit; |
|
| 312 |
+ this->volume_unit_ = new_unit; |
|
| 313 |
+ double factor = new_unit * (1L << blip_sample_bits) / this->kernel_unit; |
|
| 350 | 314 |
|
| 351 |
- if ( factor > 0.0 ) |
|
| 315 |
+ if (factor > 0.0) |
|
| 352 | 316 |
{
|
| 353 | 317 |
int shift = 0; |
| 354 | 318 |
|
| 355 | 319 |
// if unit is really small, might need to attenuate kernel |
| 356 |
- while ( factor < 2.0 ) |
|
| 320 |
+ while (factor < 2.0) |
|
| 357 | 321 |
{
|
| 358 |
- shift++; |
|
| 322 |
+ ++shift; |
|
| 359 | 323 |
factor *= 2.0; |
| 360 | 324 |
} |
| 361 | 325 |
|
| 362 |
- if ( shift ) |
|
| 326 |
+ if (shift) |
|
| 363 | 327 |
{
|
| 364 |
- kernel_unit >>= shift; |
|
| 365 |
- assert( kernel_unit > 0 ); // fails if volume unit is too low |
|
| 328 |
+ this->kernel_unit >>= shift; |
|
| 329 |
+ assert(this->kernel_unit > 0); // fails if volume unit is too low |
|
| 366 | 330 |
|
| 367 | 331 |
// keep values positive to avoid round-towards-zero of sign-preserving |
| 368 | 332 |
// right shift for negative values |
| 369 | 333 |
long offset = 0x8000 + (1 << (shift - 1)); |
| 370 | 334 |
long offset2 = 0x8000 >> shift; |
| 371 |
- for ( int i = impulses_size(); i--; ) |
|
| 372 |
- impulses [i] = (short) (int) (((impulses [i] + offset) >> shift) - offset2); |
|
| 373 |
- adjust_impulse(); |
|
| 335 |
+ for (int i = this->impulses_size(); i--; ) |
|
| 336 |
+ this->impulses[i] = static_cast<short>(((this->impulses[i] + offset) >> shift) - offset2); |
|
| 337 |
+ this->adjust_impulse(); |
|
| 374 | 338 |
} |
| 375 | 339 |
} |
| 376 |
- delta_factor = (int) floor( factor + 0.5 ); |
|
| 377 |
- //printf( "delta_factor: %d, kernel_unit: %d\n", delta_factor, kernel_unit ); |
|
| 340 |
+ this->delta_factor = static_cast<int>(std::floor(factor + 0.5)); |
|
| 341 |
+ //printf("delta_factor: %d, kernel_unit: %d\n", this->delta_factor, this->kernel_unit);
|
|
| 378 | 342 |
} |
| 379 | 343 |
} |
| 380 | 344 |
#endif |
| 381 | 345 |
|
| 382 |
-long Blip_Buffer::read_samples( blip_sample_t* out_, long max_samples, int stereo ) |
|
| 346 |
+long Blip_Buffer::read_samples(blip_sample_t *out_, long max_samples, bool stereo) |
|
| 383 | 347 |
{
|
| 384 |
- long count = samples_avail(); |
|
| 385 |
- if ( count > max_samples ) |
|
| 348 |
+ long count = this->samples_avail(); |
|
| 349 |
+ if (count > max_samples) |
|
| 386 | 350 |
count = max_samples; |
| 387 | 351 |
|
| 388 |
- if ( count ) |
|
| 352 |
+ if (count) |
|
| 389 | 353 |
{
|
| 390 |
- int const bass = BLIP_READER_BASS( *this ); |
|
| 391 |
- BLIP_READER_BEGIN( reader, *this ); |
|
| 392 |
- BLIP_READER_ADJ_( reader, count ); |
|
| 393 |
- blip_sample_t* BLIP_RESTRICT out = out_ + count; |
|
| 394 |
- blip_long offset = (blip_long) -count; |
|
| 354 |
+ int bass = BLIP_READER_BASS(*this); |
|
| 355 |
+ BLIP_READER_BEGIN(reader, *this); |
|
| 356 |
+ BLIP_READER_ADJ_(reader, count); |
|
| 357 |
+ auto out = out_ + count; |
|
| 358 |
+ int32_t offset = static_cast<int32_t>(-count); |
|
| 395 | 359 |
|
| 396 |
- if ( !stereo ) |
|
| 397 |
- {
|
|
| 398 |
- do |
|
| 399 |
- {
|
|
| 400 |
- blip_long s = BLIP_READER_READ( reader ); |
|
| 401 |
- BLIP_READER_NEXT_IDX_( reader, bass, offset ); |
|
| 402 |
- BLIP_CLAMP( s, s ); |
|
| 403 |
- out [offset] = (blip_sample_t) s; |
|
| 404 |
- } |
|
| 405 |
- while ( ++offset ); |
|
| 406 |
- } |
|
| 407 |
- else |
|
| 360 |
+ do |
|
| 408 | 361 |
{
|
| 409 |
- do |
|
| 410 |
- {
|
|
| 411 |
- blip_long s = BLIP_READER_READ( reader ); |
|
| 412 |
- BLIP_READER_NEXT_IDX_( reader, bass, offset ); |
|
| 413 |
- BLIP_CLAMP( s, s ); |
|
| 414 |
- out [offset * 2] = (blip_sample_t) s; |
|
| 415 |
- } |
|
| 416 |
- while ( ++offset ); |
|
| 417 |
- } |
|
| 362 |
+ int32_t s = BLIP_READER_READ(reader); |
|
| 363 |
+ BLIP_READER_NEXT_IDX_(reader, bass, offset); |
|
| 364 |
+ BLIP_CLAMP(s, s); |
|
| 365 |
+ out[offset * (stereo ? 2 : 1)] = static_cast<blip_sample_t>(s); |
|
| 366 |
+ } while (++offset); |
|
| 418 | 367 |
|
| 419 |
- BLIP_READER_END( reader, *this ); |
|
| 368 |
+ BLIP_READER_END(reader, *this); |
|
| 420 | 369 |
|
| 421 |
- remove_samples( count ); |
|
| 370 |
+ this->remove_samples(count); |
|
| 422 | 371 |
} |
| 423 | 372 |
return count; |
| 424 | 373 |
} |
| 425 | 374 |
|
| 426 |
-void Blip_Buffer::mix_samples( blip_sample_t const* in, long count ) |
|
| 375 |
+void Blip_Buffer::mix_samples(const blip_sample_t *in, long count) |
|
| 427 | 376 |
{
|
| 428 |
- if ( buffer_size_ == silent_buf_size ) |
|
| 429 |
- {
|
|
| 430 |
- assert( 0 ); |
|
| 431 |
- return; |
|
| 432 |
- } |
|
| 433 |
- |
|
| 434 |
- buf_t_* out = buffer_ + (offset_ >> BLIP_BUFFER_ACCURACY) + blip_widest_impulse_ / 2; |
|
| 377 |
+ auto out = &this->buffer_[(this->offset_ >> BLIP_BUFFER_ACCURACY) + blip_widest_impulse_ / 2]; |
|
| 435 | 378 |
|
| 436 |
- int const sample_shift = blip_sample_bits - 16; |
|
| 379 |
+ int sample_shift = blip_sample_bits - 16; |
|
| 437 | 380 |
int prev = 0; |
| 438 |
- while ( count-- ) |
|
| 381 |
+ while (count--) |
|
| 439 | 382 |
{
|
| 440 |
- blip_long s = (blip_long) *in++ << sample_shift; |
|
| 383 |
+ int32_t s = static_cast<int32_t>(*in++) << sample_shift; |
|
| 441 | 384 |
*out += s - prev; |
| 442 | 385 |
prev = s; |
| 443 | 386 |
++out; |
| 444 | 387 |
} |
| 445 | 388 |
*out -= prev; |
| 446 | 389 |
} |
| 447 |
- |
|
| 448 |
-blip_ulong const subsample_mask = (1L << BLIP_BUFFER_ACCURACY) - 1; |
|
| 449 |
- |
|
| 450 |
-void Blip_Buffer::save_state( blip_buffer_state_t* out ) |
|
| 451 |
-{
|
|
| 452 |
- assert( samples_avail() == 0 ); |
|
| 453 |
- out->offset_ = offset_; |
|
| 454 |
- out->reader_accum_ = reader_accum_; |
|
| 455 |
- memcpy( out->buf, &buffer_ [offset_ >> BLIP_BUFFER_ACCURACY], sizeof out->buf ); |
|
| 456 |
-} |
|
| 457 |
- |
|
| 458 |
-void Blip_Buffer::load_state( blip_buffer_state_t const& in ) |
|
| 459 |
-{
|
|
| 460 |
- clear( false ); |
|
| 461 |
- |
|
| 462 |
- offset_ = in.offset_; |
|
| 463 |
- reader_accum_ = in.reader_accum_; |
|
| 464 |
- memcpy( buffer_, in.buf, sizeof in.buf ); |
|
| 465 |
-} |
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,465 @@ |
| 1 |
+// Blip_Buffer 0.4.1. http://www.slack.net/~ant/ |
|
| 2 |
+ |
|
| 3 |
+#include "Blip_Buffer.h" |
|
| 4 |
+ |
|
| 5 |
+#include <assert.h> |
|
| 6 |
+#include <limits.h> |
|
| 7 |
+#include <string.h> |
|
| 8 |
+#include <stdlib.h> |
|
| 9 |
+#include <math.h> |
|
| 10 |
+ |
|
| 11 |
+/* Copyright (C) 2003-2007 Shay Green. This module is free software; you |
|
| 12 |
+can redistribute it and/or modify it under the terms of the GNU Lesser |
|
| 13 |
+General Public License as published by the Free Software Foundation; either |
|
| 14 |
+version 2.1 of the License, or (at your option) any later version. This |
|
| 15 |
+module is distributed in the hope that it will be useful, but WITHOUT ANY |
|
| 16 |
+WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
| 17 |
+FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
| 18 |
+details. You should have received a copy of the GNU Lesser General Public |
|
| 19 |
+License along with this module; if not, write to the Free Software Foundation, |
|
| 20 |
+Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ |
|
| 21 |
+ |
|
| 22 |
+// TODO: use scoped for variables in treble_eq() |
|
| 23 |
+ |
|
| 24 |
+#ifdef BLARGG_ENABLE_OPTIMIZER |
|
| 25 |
+ #include BLARGG_ENABLE_OPTIMIZER |
|
| 26 |
+#endif |
|
| 27 |
+ |
|
| 28 |
+int const silent_buf_size = 1; // size used for Silent_Blip_Buffer |
|
| 29 |
+ |
|
| 30 |
+Blip_Buffer::Blip_Buffer() |
|
| 31 |
+{
|
|
| 32 |
+ factor_ = (blip_ulong)LONG_MAX; |
|
| 33 |
+ buffer_ = 0; |
|
| 34 |
+ buffer_size_ = 0; |
|
| 35 |
+ sample_rate_ = 0; |
|
| 36 |
+ bass_shift_ = 0; |
|
| 37 |
+ clock_rate_ = 0; |
|
| 38 |
+ bass_freq_ = 16; |
|
| 39 |
+ length_ = 0; |
|
| 40 |
+ |
|
| 41 |
+ // assumptions code makes about implementation-defined features |
|
| 42 |
+ #ifndef NDEBUG |
|
| 43 |
+ // right shift of negative value preserves sign |
|
| 44 |
+ buf_t_ i = -0x7FFFFFFE; |
|
| 45 |
+ assert( (i >> 1) == -0x3FFFFFFF ); |
|
| 46 |
+ |
|
| 47 |
+ // casting to short truncates to 16 bits and sign-extends |
|
| 48 |
+ i = 0x18000; |
|
| 49 |
+ assert( (short) i == -0x8000 ); |
|
| 50 |
+ #endif |
|
| 51 |
+ |
|
| 52 |
+ clear(); |
|
| 53 |
+} |
|
| 54 |
+ |
|
| 55 |
+Blip_Buffer::~Blip_Buffer() |
|
| 56 |
+{
|
|
| 57 |
+ if ( buffer_size_ != silent_buf_size ) |
|
| 58 |
+ free( buffer_ ); |
|
| 59 |
+} |
|
| 60 |
+ |
|
| 61 |
+Silent_Blip_Buffer::Silent_Blip_Buffer() |
|
| 62 |
+{
|
|
| 63 |
+ factor_ = 0; |
|
| 64 |
+ buffer_ = buf; |
|
| 65 |
+ buffer_size_ = silent_buf_size; |
|
| 66 |
+ clear(); |
|
| 67 |
+} |
|
| 68 |
+ |
|
| 69 |
+void Blip_Buffer::clear( int entire_buffer ) |
|
| 70 |
+{
|
|
| 71 |
+ offset_ = 0; |
|
| 72 |
+ reader_accum_ = 0; |
|
| 73 |
+ modified_ = 0; |
|
| 74 |
+ if ( buffer_ ) |
|
| 75 |
+ {
|
|
| 76 |
+ long count = (entire_buffer ? buffer_size_ : samples_avail()); |
|
| 77 |
+ memset( buffer_, 0, (count + blip_buffer_extra_) * sizeof (buf_t_) ); |
|
| 78 |
+ } |
|
| 79 |
+} |
|
| 80 |
+ |
|
| 81 |
+Blip_Buffer::blargg_err_t Blip_Buffer::set_sample_rate( long new_rate, int msec ) |
|
| 82 |
+{
|
|
| 83 |
+ if ( buffer_size_ == silent_buf_size ) |
|
| 84 |
+ {
|
|
| 85 |
+ assert( 0 ); |
|
| 86 |
+ return "Internal (tried to resize Silent_Blip_Buffer)"; |
|
| 87 |
+ } |
|
| 88 |
+ |
|
| 89 |
+ // start with maximum length that resampled time can represent |
|
| 90 |
+ long new_size = (ULONG_MAX >> BLIP_BUFFER_ACCURACY) - blip_buffer_extra_ - 64; |
|
| 91 |
+ if ( msec != blip_max_length ) |
|
| 92 |
+ {
|
|
| 93 |
+ long s = (new_rate * (msec + 1) + 999) / 1000; |
|
| 94 |
+ if ( s < new_size ) |
|
| 95 |
+ new_size = s; |
|
| 96 |
+ else |
|
| 97 |
+ assert( 0 ); // fails if requested buffer length exceeds limit |
|
| 98 |
+ } |
|
| 99 |
+ |
|
| 100 |
+ if ( buffer_size_ != new_size ) |
|
| 101 |
+ {
|
|
| 102 |
+ void* p = realloc( buffer_, (new_size + blip_buffer_extra_) * sizeof *buffer_ ); |
|
| 103 |
+ if ( !p ) |
|
| 104 |
+ return "Out of memory"; |
|
| 105 |
+ buffer_ = (buf_t_*) p; |
|
| 106 |
+ } |
|
| 107 |
+ |
|
| 108 |
+ buffer_size_ = new_size; |
|
| 109 |
+ assert( buffer_size_ != silent_buf_size ); // size should never happen to match this |
|
| 110 |
+ |
|
| 111 |
+ // update things based on the sample rate |
|
| 112 |
+ sample_rate_ = new_rate; |
|
| 113 |
+ length_ = new_size * 1000 / new_rate - 1; |
|
| 114 |
+ if ( msec ) |
|
| 115 |
+ assert( length_ == msec ); // ensure length is same as that passed in |
|
| 116 |
+ |
|
| 117 |
+ // update these since they depend on sample rate |
|
| 118 |
+ if ( clock_rate_ ) |
|
| 119 |
+ clock_rate( clock_rate_ ); |
|
| 120 |
+ bass_freq( bass_freq_ ); |
|
| 121 |
+ |
|
| 122 |
+ clear(); |
|
| 123 |
+ |
|
| 124 |
+ return 0; // success |
|
| 125 |
+} |
|
| 126 |
+ |
|
| 127 |
+blip_resampled_time_t Blip_Buffer::clock_rate_factor( long rate ) const |
|
| 128 |
+{
|
|
| 129 |
+ double ratio = (double) sample_rate_ / rate; |
|
| 130 |
+ blip_long factor = (blip_long) floor( ratio * (1L << BLIP_BUFFER_ACCURACY) + 0.5 ); |
|
| 131 |
+ assert( factor > 0 || !sample_rate_ ); // fails if clock/output ratio is too large |
|
| 132 |
+ return (blip_resampled_time_t) factor; |
|
| 133 |
+} |
|
| 134 |
+ |
|
| 135 |
+void Blip_Buffer::bass_freq( int freq ) |
|
| 136 |
+{
|
|
| 137 |
+ bass_freq_ = freq; |
|
| 138 |
+ int shift = 31; |
|
| 139 |
+ if ( freq > 0 ) |
|
| 140 |
+ {
|
|
| 141 |
+ shift = 13; |
|
| 142 |
+ long f = (freq << 16) / sample_rate_; |
|
| 143 |
+ while ( (f >>= 1) && --shift ) { }
|
|
| 144 |
+ } |
|
| 145 |
+ bass_shift_ = shift; |
|
| 146 |
+} |
|
| 147 |
+ |
|
| 148 |
+void Blip_Buffer::end_frame( blip_time_t t ) |
|
| 149 |
+{
|
|
| 150 |
+ offset_ += t * factor_; |
|
| 151 |
+ assert( samples_avail() <= (long) buffer_size_ ); // fails if time is past end of buffer |
|
| 152 |
+} |
|
| 153 |
+ |
|
| 154 |
+long Blip_Buffer::count_samples( blip_time_t t ) const |
|
| 155 |
+{
|
|
| 156 |
+ blip_resampled_time_t last_sample = resampled_time( t ) >> BLIP_BUFFER_ACCURACY; |
|
| 157 |
+ blip_resampled_time_t first_sample = offset_ >> BLIP_BUFFER_ACCURACY; |
|
| 158 |
+ return long (last_sample - first_sample); |
|
| 159 |
+} |
|
| 160 |
+ |
|
| 161 |
+blip_time_t Blip_Buffer::count_clocks( long count ) const |
|
| 162 |
+{
|
|
| 163 |
+ if ( !factor_ ) |
|
| 164 |
+ {
|
|
| 165 |
+ assert( 0 ); // sample rate and clock rates must be set first |
|
| 166 |
+ return 0; |
|
| 167 |
+ } |
|
| 168 |
+ |
|
| 169 |
+ if ( count > buffer_size_ ) |
|
| 170 |
+ count = buffer_size_; |
|
| 171 |
+ blip_resampled_time_t time = (blip_resampled_time_t) count << BLIP_BUFFER_ACCURACY; |
|
| 172 |
+ return (blip_time_t) ((time - offset_ + factor_ - 1) / factor_); |
|
| 173 |
+} |
|
| 174 |
+ |
|
| 175 |
+void Blip_Buffer::remove_samples( long count ) |
|
| 176 |
+{
|
|
| 177 |
+ if ( count ) |
|
| 178 |
+ {
|
|
| 179 |
+ remove_silence( count ); |
|
| 180 |
+ |
|
| 181 |
+ // copy remaining samples to beginning and clear old samples |
|
| 182 |
+ long remain = samples_avail() + blip_buffer_extra_; |
|
| 183 |
+ memmove( buffer_, buffer_ + count, remain * sizeof *buffer_ ); |
|
| 184 |
+ memset( buffer_ + remain, 0, count * sizeof *buffer_ ); |
|
| 185 |
+ } |
|
| 186 |
+} |
|
| 187 |
+ |
|
| 188 |
+// Blip_Synth_ |
|
| 189 |
+ |
|
| 190 |
+Blip_Synth_Fast_::Blip_Synth_Fast_() |
|
| 191 |
+{
|
|
| 192 |
+ buf = 0; |
|
| 193 |
+ last_amp = 0; |
|
| 194 |
+ delta_factor = 0; |
|
| 195 |
+} |
|
| 196 |
+ |
|
| 197 |
+void Blip_Synth_Fast_::volume_unit( double new_unit ) |
|
| 198 |
+{
|
|
| 199 |
+ delta_factor = int (new_unit * (1L << blip_sample_bits) + 0.5); |
|
| 200 |
+} |
|
| 201 |
+ |
|
| 202 |
+#if !BLIP_BUFFER_FAST |
|
| 203 |
+ |
|
| 204 |
+Blip_Synth_::Blip_Synth_( short* p, int w ) : |
|
| 205 |
+ impulses( p ), |
|
| 206 |
+ width( w ) |
|
| 207 |
+{
|
|
| 208 |
+ volume_unit_ = 0.0; |
|
| 209 |
+ kernel_unit = 0; |
|
| 210 |
+ buf = 0; |
|
| 211 |
+ last_amp = 0; |
|
| 212 |
+ delta_factor = 0; |
|
| 213 |
+} |
|
| 214 |
+ |
|
| 215 |
+#undef PI |
|
| 216 |
+#define PI 3.1415926535897932384626433832795029 |
|
| 217 |
+ |
|
| 218 |
+static void gen_sinc( float* out, int count, double oversample, double treble, double cutoff ) |
|
| 219 |
+{
|
|
| 220 |
+ if ( cutoff >= 0.999 ) |
|
| 221 |
+ cutoff = 0.999; |
|
| 222 |
+ |
|
| 223 |
+ if ( treble < -300.0 ) |
|
| 224 |
+ treble = -300.0; |
|
| 225 |
+ if ( treble > 5.0 ) |
|
| 226 |
+ treble = 5.0; |
|
| 227 |
+ |
|
| 228 |
+ double const maxh = 4096.0; |
|
| 229 |
+ double const rolloff = pow( 10.0, 1.0 / (maxh * 20.0) * treble / (1.0 - cutoff) ); |
|
| 230 |
+ double const pow_a_n = pow( rolloff, maxh - maxh * cutoff ); |
|
| 231 |
+ double const to_angle = PI / 2 / maxh / oversample; |
|
| 232 |
+ for ( int i = 0; i < count; i++ ) |
|
| 233 |
+ {
|
|
| 234 |
+ double angle = ((i - count) * 2 + 1) * to_angle; |
|
| 235 |
+ double c = rolloff * cos( (maxh - 1.0) * angle ) - cos( maxh * angle ); |
|
| 236 |
+ double cos_nc_angle = cos( maxh * cutoff * angle ); |
|
| 237 |
+ double cos_nc1_angle = cos( (maxh * cutoff - 1.0) * angle ); |
|
| 238 |
+ double cos_angle = cos( angle ); |
|
| 239 |
+ |
|
| 240 |
+ c = c * pow_a_n - rolloff * cos_nc1_angle + cos_nc_angle; |
|
| 241 |
+ double d = 1.0 + rolloff * (rolloff - cos_angle - cos_angle); |
|
| 242 |
+ double b = 2.0 - cos_angle - cos_angle; |
|
| 243 |
+ double a = 1.0 - cos_angle - cos_nc_angle + cos_nc1_angle; |
|
| 244 |
+ |
|
| 245 |
+ out [i] = (float) ((a * d + c * b) / (b * d)); // a / b + c / d |
|
| 246 |
+ } |
|
| 247 |
+} |
|
| 248 |
+ |
|
| 249 |
+void blip_eq_t::generate( float* out, int count ) const |
|
| 250 |
+{
|
|
| 251 |
+ // lower cutoff freq for narrow kernels with their wider transition band |
|
| 252 |
+ // (8 points->1.49, 16 points->1.15) |
|
| 253 |
+ double oversample = blip_res * 2.25 / count + 0.85; |
|
| 254 |
+ double half_rate = sample_rate * 0.5; |
|
| 255 |
+ if ( cutoff_freq ) |
|
| 256 |
+ oversample = half_rate / cutoff_freq; |
|
| 257 |
+ double cutoff = rolloff_freq * oversample / half_rate; |
|
| 258 |
+ |
|
| 259 |
+ gen_sinc( out, count, blip_res * oversample, treble, cutoff ); |
|
| 260 |
+ |
|
| 261 |
+ // apply (half of) hamming window |
|
| 262 |
+ double to_fraction = PI / (count - 1); |
|
| 263 |
+ for ( int i = count; i--; ) |
|
| 264 |
+ out [i] *= 0.54f - 0.46f * (float) cos( i * to_fraction ); |
|
| 265 |
+} |
|
| 266 |
+ |
|
| 267 |
+void Blip_Synth_::adjust_impulse() |
|
| 268 |
+{
|
|
| 269 |
+ // sum pairs for each phase and add error correction to end of first half |
|
| 270 |
+ int const size = impulses_size(); |
|
| 271 |
+ for ( int p = blip_res; p-- >= blip_res / 2; ) |
|
| 272 |
+ {
|
|
| 273 |
+ int p2 = blip_res - 2 - p; |
|
| 274 |
+ long error = kernel_unit; |
|
| 275 |
+ for ( int i = 1; i < size; i += blip_res ) |
|
| 276 |
+ {
|
|
| 277 |
+ error -= impulses [i + p ]; |
|
| 278 |
+ error -= impulses [i + p2]; |
|
| 279 |
+ } |
|
| 280 |
+ if ( p == p2 ) |
|
| 281 |
+ error /= 2; // phase = 0.5 impulse uses same half for both sides |
|
| 282 |
+ impulses [size - blip_res + p] += (short) error; |
|
| 283 |
+ //printf( "error: %ld\n", error ); |
|
| 284 |
+ } |
|
| 285 |
+ |
|
| 286 |
+ //for ( int i = blip_res; i--; printf( "\n" ) ) |
|
| 287 |
+ // for ( int j = 0; j < width / 2; j++ ) |
|
| 288 |
+ // printf( "%5ld,", impulses [j * blip_res + i + 1] ); |
|
| 289 |
+} |
|
| 290 |
+ |
|
| 291 |
+void Blip_Synth_::treble_eq( blip_eq_t const& eq ) |
|
| 292 |
+{
|
|
| 293 |
+ float fimpulse [blip_res / 2 * (blip_widest_impulse_ - 1) + blip_res * 2]; |
|
| 294 |
+ |
|
| 295 |
+ int const half_size = blip_res / 2 * (width - 1); |
|
| 296 |
+ eq.generate( &fimpulse [blip_res], half_size ); |
|
| 297 |
+ |
|
| 298 |
+ int i; |
|
| 299 |
+ |
|
| 300 |
+ // need mirror slightly past center for calculation |
|
| 301 |
+ for ( i = blip_res; i--; ) |
|
| 302 |
+ fimpulse [blip_res + half_size + i] = fimpulse [blip_res + half_size - 1 - i]; |
|
| 303 |
+ |
|
| 304 |
+ // starts at 0 |
|
| 305 |
+ for ( i = 0; i < blip_res; i++ ) |
|
| 306 |
+ fimpulse [i] = 0.0f; |
|
| 307 |
+ |
|
| 308 |
+ // find rescale factor |
|
| 309 |
+ double total = 0.0; |
|
| 310 |
+ for ( i = 0; i < half_size; i++ ) |
|
| 311 |
+ total += fimpulse [blip_res + i]; |
|
| 312 |
+ |
|
| 313 |
+ //double const base_unit = 44800.0 - 128 * 18; // allows treble up to +0 dB |
|
| 314 |
+ //double const base_unit = 37888.0; // allows treble to +5 dB |
|
| 315 |
+ double const base_unit = 32768.0; // necessary for blip_unscaled to work |
|
| 316 |
+ double rescale = base_unit / 2 / total; |
|
| 317 |
+ kernel_unit = (long) base_unit; |
|
| 318 |
+ |
|
| 319 |
+ // integrate, first difference, rescale, convert to int |
|
| 320 |
+ double sum = 0.0; |
|
| 321 |
+ double next = 0.0; |
|
| 322 |
+ int const size = this->impulses_size(); |
|
| 323 |
+ for ( i = 0; i < size; i++ ) |
|
| 324 |
+ {
|
|
| 325 |
+ impulses [i] = (short) (int) floor( (next - sum) * rescale + 0.5 ); |
|
| 326 |
+ sum += fimpulse [i]; |
|
| 327 |
+ next += fimpulse [i + blip_res]; |
|
| 328 |
+ } |
|
| 329 |
+ adjust_impulse(); |
|
| 330 |
+ |
|
| 331 |
+ // volume might require rescaling |
|
| 332 |
+ double vol = volume_unit_; |
|
| 333 |
+ if ( vol ) |
|
| 334 |
+ {
|
|
| 335 |
+ volume_unit_ = 0.0; |
|
| 336 |
+ volume_unit( vol ); |
|
| 337 |
+ } |
|
| 338 |
+} |
|
| 339 |
+ |
|
| 340 |
+void Blip_Synth_::volume_unit( double new_unit ) |
|
| 341 |
+{
|
|
| 342 |
+ if ( new_unit != volume_unit_ ) |
|
| 343 |
+ {
|
|
| 344 |
+ // use default eq if it hasn't been set yet |
|
| 345 |
+ if ( !kernel_unit ) |
|
| 346 |
+ treble_eq( -8.0 ); |
|
| 347 |
+ |
|
| 348 |
+ volume_unit_ = new_unit; |
|
| 349 |
+ double factor = new_unit * (1L << blip_sample_bits) / kernel_unit; |
|
| 350 |
+ |
|
| 351 |
+ if ( factor > 0.0 ) |
|
| 352 |
+ {
|
|
| 353 |
+ int shift = 0; |
|
| 354 |
+ |
|
| 355 |
+ // if unit is really small, might need to attenuate kernel |
|
| 356 |
+ while ( factor < 2.0 ) |
|
| 357 |
+ {
|
|
| 358 |
+ shift++; |
|
| 359 |
+ factor *= 2.0; |
|
| 360 |
+ } |
|
| 361 |
+ |
|
| 362 |
+ if ( shift ) |
|
| 363 |
+ {
|
|
| 364 |
+ kernel_unit >>= shift; |
|
| 365 |
+ assert( kernel_unit > 0 ); // fails if volume unit is too low |
|
| 366 |
+ |
|
| 367 |
+ // keep values positive to avoid round-towards-zero of sign-preserving |
|
| 368 |
+ // right shift for negative values |
|
| 369 |
+ long offset = 0x8000 + (1 << (shift - 1)); |
|
| 370 |
+ long offset2 = 0x8000 >> shift; |
|
| 371 |
+ for ( int i = impulses_size(); i--; ) |
|
| 372 |
+ impulses [i] = (short) (int) (((impulses [i] + offset) >> shift) - offset2); |
|
| 373 |
+ adjust_impulse(); |
|
| 374 |
+ } |
|
| 375 |
+ } |
|
| 376 |
+ delta_factor = (int) floor( factor + 0.5 ); |
|
| 377 |
+ //printf( "delta_factor: %d, kernel_unit: %d\n", delta_factor, kernel_unit ); |
|
| 378 |
+ } |
|
| 379 |
+} |
|
| 380 |
+#endif |
|
| 381 |
+ |
|
| 382 |
+long Blip_Buffer::read_samples( blip_sample_t* out_, long max_samples, int stereo ) |
|
| 383 |
+{
|
|
| 384 |
+ long count = samples_avail(); |
|
| 385 |
+ if ( count > max_samples ) |
|
| 386 |
+ count = max_samples; |
|
| 387 |
+ |
|
| 388 |
+ if ( count ) |
|
| 389 |
+ {
|
|
| 390 |
+ int const bass = BLIP_READER_BASS( *this ); |
|
| 391 |
+ BLIP_READER_BEGIN( reader, *this ); |
|
| 392 |
+ BLIP_READER_ADJ_( reader, count ); |
|
| 393 |
+ blip_sample_t* BLIP_RESTRICT out = out_ + count; |
|
| 394 |
+ blip_long offset = (blip_long) -count; |
|
| 395 |
+ |
|
| 396 |
+ if ( !stereo ) |
|
| 397 |
+ {
|
|
| 398 |
+ do |
|
| 399 |
+ {
|
|
| 400 |
+ blip_long s = BLIP_READER_READ( reader ); |
|
| 401 |
+ BLIP_READER_NEXT_IDX_( reader, bass, offset ); |
|
| 402 |
+ BLIP_CLAMP( s, s ); |
|
| 403 |
+ out [offset] = (blip_sample_t) s; |
|
| 404 |
+ } |
|
| 405 |
+ while ( ++offset ); |
|
| 406 |
+ } |
|
| 407 |
+ else |
|
| 408 |
+ {
|
|
| 409 |
+ do |
|
| 410 |
+ {
|
|
| 411 |
+ blip_long s = BLIP_READER_READ( reader ); |
|
| 412 |
+ BLIP_READER_NEXT_IDX_( reader, bass, offset ); |
|
| 413 |
+ BLIP_CLAMP( s, s ); |
|
| 414 |
+ out [offset * 2] = (blip_sample_t) s; |
|
| 415 |
+ } |
|
| 416 |
+ while ( ++offset ); |
|
| 417 |
+ } |
|
| 418 |
+ |
|
| 419 |
+ BLIP_READER_END( reader, *this ); |
|
| 420 |
+ |
|
| 421 |
+ remove_samples( count ); |
|
| 422 |
+ } |
|
| 423 |
+ return count; |
|
| 424 |
+} |
|
| 425 |
+ |
|
| 426 |
+void Blip_Buffer::mix_samples( blip_sample_t const* in, long count ) |
|
| 427 |
+{
|
|
| 428 |
+ if ( buffer_size_ == silent_buf_size ) |
|
| 429 |
+ {
|
|
| 430 |
+ assert( 0 ); |
|
| 431 |
+ return; |
|
| 432 |
+ } |
|
| 433 |
+ |
|
| 434 |
+ buf_t_* out = buffer_ + (offset_ >> BLIP_BUFFER_ACCURACY) + blip_widest_impulse_ / 2; |
|
| 435 |
+ |
|
| 436 |
+ int const sample_shift = blip_sample_bits - 16; |
|
| 437 |
+ int prev = 0; |
|
| 438 |
+ while ( count-- ) |
|
| 439 |
+ {
|
|
| 440 |
+ blip_long s = (blip_long) *in++ << sample_shift; |
|
| 441 |
+ *out += s - prev; |
|
| 442 |
+ prev = s; |
|
| 443 |
+ ++out; |
|
| 444 |
+ } |
|
| 445 |
+ *out -= prev; |
|
| 446 |
+} |
|
| 447 |
+ |
|
| 448 |
+blip_ulong const subsample_mask = (1L << BLIP_BUFFER_ACCURACY) - 1; |
|
| 449 |
+ |
|
| 450 |
+void Blip_Buffer::save_state( blip_buffer_state_t* out ) |
|
| 451 |
+{
|
|
| 452 |
+ assert( samples_avail() == 0 ); |
|
| 453 |
+ out->offset_ = offset_; |
|
| 454 |
+ out->reader_accum_ = reader_accum_; |
|
| 455 |
+ memcpy( out->buf, &buffer_ [offset_ >> BLIP_BUFFER_ACCURACY], sizeof out->buf ); |
|
| 456 |
+} |
|
| 457 |
+ |
|
| 458 |
+void Blip_Buffer::load_state( blip_buffer_state_t const& in ) |
|
| 459 |
+{
|
|
| 460 |
+ clear( false ); |
|
| 461 |
+ |
|
| 462 |
+ offset_ = in.offset_; |
|
| 463 |
+ reader_accum_ = in.reader_accum_; |
|
| 464 |
+ memcpy( buffer_, in.buf, sizeof in.buf ); |
|
| 465 |
+} |