Skip to content

Instantly share code, notes, and snippets.

@nyanpasu64
Created October 16, 2019 10:31
Show Gist options
  • Select an option

  • Save nyanpasu64/cfbfcc845c71258a12d4b79baa74158d to your computer and use it in GitHub Desktop.

Select an option

Save nyanpasu64/cfbfcc845c71258a12d4b79baa74158d to your computer and use it in GitHub Desktop.
blip_buffer 0.4.0 -> 0CC blip_buffer
diff --git a/3rdparty/Blip_Buffer/Blip_Buffer.cpp b/3rdparty/Blip_Buffer/Blip_Buffer.cpp
index cd1f9e3..dc01adb 100644
--- a/3rdparty/Blip_Buffer/Blip_Buffer.cpp
+++ b/3rdparty/Blip_Buffer/Blip_Buffer.cpp
@@ -1,6 +1,8 @@
// Blip_Buffer 0.4.0. http://www.slack.net/~ant/
+#include "../stdafx.h"
+
#include "Blip_Buffer.h"
#include <assert.h>
@@ -9,6 +11,8 @@
#include <stdlib.h>
#include <math.h>
+//#define DITHERING
+
/* Copyright (C) 2003-2006 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser
General Public License as published by the Free Software Foundation; either
@@ -38,12 +42,14 @@ Blip_Buffer::Blip_Buffer()
// assumptions code makes about implementation-defined features
#ifndef NDEBUG
// right shift of negative value preserves sign
- int i = INT_MIN;
- assert( (i >> 1) == INT_MIN / 2 );
+ long i = LONG_MIN;
+ assert( (i >> 1) == LONG_MIN / 2 );
+ i = LONG_MIN;
+ assert( (i >> 31) == -1 );
// casting to smaller signed type truncates bits and extends sign
- long l = (SHRT_MAX + 1) * 5;
- assert( (short) l == SHRT_MIN );
+ i = (SHRT_MAX + 1) * 5;
+ assert( (short) i == SHRT_MIN );
#endif
}
@@ -222,7 +228,9 @@ void blip_eq_t::generate( float* out, int count ) const
// apply (half of) hamming window
double to_fraction = pi / (count - 1);
for ( int i = count; i--; )
- out [i] *= 0.54 - 0.46 * cos( i * to_fraction );
+ out [i] *= 0.53836f - 0.46164f * cosf( float(i * to_fraction) );
+ //out [i] *= 1.0f;
+ //out [i] *= 0.5f * (1.0f - cosf( float(i * to_fraction) ));
}
void Blip_Synth_::adjust_impulse()
@@ -240,7 +248,7 @@ void Blip_Synth_::adjust_impulse()
}
if ( p == p2 )
error /= 2; // phase = 0.5 impulse uses same half for both sides
- impulses [size - blip_res + p] += error;
+ impulses [size - blip_res + p] += (short)error;
//printf( "error: %ld\n", error );
}
@@ -339,6 +347,24 @@ void Blip_Synth_::volume_unit( double new_unit )
}
}
+#ifdef DITHERING
+int dither(long size)
+{
+ static unsigned int lfsr = 0xACE1u;
+ unsigned bit;
+ unsigned period = 0;
+
+ bit = ((lfsr >> 0) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 5) ) & 1;
+ lfsr = (lfsr >> 1) | (bit << 15);
+
+ switch (lfsr % 3) {
+ case 1: return size * 1;
+ case 2: return size * -1;
+ }
+ return 0;
+}
+#endif
+
long Blip_Buffer::read_samples( blip_sample_t* out, long max_samples, int stereo )
{
long count = samples_avail();
@@ -356,7 +382,11 @@ long Blip_Buffer::read_samples( blip_sample_t* out, long max_samples, int stereo
{
for ( long n = count; n--; )
{
+#ifdef DITHERING
+ long s = (accum + dither(1 << sample_shift)) >> sample_shift;
+#else
long s = accum >> sample_shift;
+#endif
accum -= accum >> bass_shift;
accum += *in++;
*out++ = (blip_sample_t) s;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment