Emulation.Common - cleanup SpeexResampler, and DCFilter

This commit is contained in:
adelikat 2017-04-27 11:27:26 -05:00
parent 2f17f16019
commit ee842fbde3
3 changed files with 69 additions and 82 deletions

View File

@ -71,14 +71,14 @@ namespace BizHawk.Emulation.Common
{
for (int i = 0; i < length; i += 2)
{
int L = samplesin[i] << 12;
int R = samplesin[i + 1] << 12;
int l = samplesin[i] << 12;
int r = samplesin[i + 1] << 12;
_accumL -= _accumL >> _depth;
_accumR -= _accumR >> _depth;
_accumL += L - _latchL;
_accumR += R - _latchR;
_latchL = L;
_latchR = R;
_accumL += l - _latchL;
_accumR += r - _latchR;
_latchL = l;
_latchR = r;
int bigL = _accumL >> 12;
int bigR = _accumR >> 12;

View File

@ -1,6 +1,8 @@
using System;
using System.Runtime.InteropServices;
// ReSharper disable StyleCop.SA1300
// ReSharper disable InconsistentNaming
namespace BizHawk.Emulation.Common
{
/// <summary>
@ -8,6 +10,15 @@ namespace BizHawk.Emulation.Common
/// </summary>
public class SpeexResampler : IDisposable, ISoundProvider
{
// to accept an ISyncSoundProvder input
private readonly ISoundProvider _input;
// function to call to dispatch output
private readonly Action<short[], int> _drainer;
// TODO: this size is roughly based on how big you can make the buffer before the snes resampling (32040.5 -> 44100) gets screwed up
private readonly short[] _inbuf = new short[512]; // [8192]; // [512];
private static class LibSpeexDSP
{
public const int QUALITY_MAX = 10;
@ -70,7 +81,6 @@ namespace BizHawk.Emulation.Common
/// <param name="in_len">Number of input samples in the input buffer. Returns the number of samples processed</param>
/// <param name="outp">Output buffer</param>
/// <param name="out_len">Size of the output buffer. Returns the number of samples written</param>
/// <returns></returns>
[DllImport("libspeexdsp.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern RESAMPLER_ERR speex_resampler_process_float(IntPtr st, uint channel_index, float[] inp, ref uint in_len, float[] outp, ref uint out_len);
@ -83,7 +93,6 @@ namespace BizHawk.Emulation.Common
/// <param name="in_len">Number of input samples in the input buffer. Returns the number of samples processed</param>
/// <param name="outp">Output buffer</param>
/// <param name="out_len">Size of the output buffer. Returns the number of samples written</param>
/// <returns></returns>
[DllImport("libspeexdsp.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern RESAMPLER_ERR speex_resampler_process_int(IntPtr st, uint channel_index, short[] inp, ref uint in_len, short[] outp, ref uint out_len);
@ -95,7 +104,6 @@ namespace BizHawk.Emulation.Common
/// <param name="in_len">Number of input samples in the input buffer. Returns the number of samples processed. This is all per-channel.</param>
/// <param name="outp">Output buffer</param>
/// <param name="out_len">Size of the output buffer. Returns the number of samples written. This is all per-channel.</param>
/// <returns></returns>
[DllImport("libspeexdsp.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern RESAMPLER_ERR speex_resampler_process_interleaved_float(IntPtr st, float[] inp, ref uint in_len, float[] outp, ref uint out_len);
@ -107,7 +115,6 @@ namespace BizHawk.Emulation.Common
/// <param name="in_len">Number of input samples in the input buffer. Returns the number of samples processed. This is all per-channel.</param>
/// <param name="outp">Output buffer</param>
/// <param name="out_len">Size of the output buffer. Returns the number of samples written. This is all per-channel.</param>
/// <returns></returns>
[DllImport("libspeexdsp.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern RESAMPLER_ERR speex_resampler_process_interleaved_int(IntPtr st, short[] inp, ref uint in_len, short[] outp, ref uint out_len);
@ -117,7 +124,6 @@ namespace BizHawk.Emulation.Common
/// <param name="st">Resampler state</param>
/// <param name="in_rate">Input sampling rate (integer number of Hz).</param>
/// <param name="out_rate">Output sampling rate (integer number of Hz).</param>
/// <returns></returns>
[DllImport("libspeexdsp.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern RESAMPLER_ERR speex_resampler_set_rate(IntPtr st, uint in_rate, uint out_rate);
@ -138,7 +144,6 @@ namespace BizHawk.Emulation.Common
/// <param name="ratio_den">Denominator of the sampling rate ratio</param>
/// <param name="in_rate">Input sampling rate rounded to the nearest integer (in Hz).</param>
/// <param name="out_rate">Output sampling rate rounded to the nearest integer (in Hz).</param>
/// <returns></returns>
[DllImport("libspeexdsp.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern RESAMPLER_ERR speex_resampler_set_rate_frac(IntPtr st, uint ratio_num, uint ratio_den, uint in_rate, uint out_rate);
@ -229,7 +234,6 @@ namespace BizHawk.Emulation.Common
/// is the same for the first frame).
/// </summary>
/// <param name="st">Resampler state</param>
/// <returns></returns>
[DllImport("libspeexdsp.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern RESAMPLER_ERR speex_resampler_skip_zeroes(IntPtr st);
@ -237,7 +241,6 @@ namespace BizHawk.Emulation.Common
/// Reset a resampler so a new (unrelated) stream can be processed.
/// </summary>
/// <param name="st">Resampler state</param>
/// <returns></returns>
[DllImport("libspeexdsp.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern RESAMPLER_ERR speex_resampler_reset_mem(IntPtr st);
@ -250,37 +253,21 @@ namespace BizHawk.Emulation.Common
public static extern string speex_resampler_strerror(RESAMPLER_ERR err);
}
/// <summary>
/// opaque pointer to state
/// </summary>
private IntPtr st = IntPtr.Zero;
// opaque pointer to state
private IntPtr _st = IntPtr.Zero;
/// <summary>
/// function to call to dispatch output
/// </summary>
private readonly Action<short[], int> drainer;
// TODO: this size is roughly based on how big you can make the buffer before the snes resampling (32040.5 -> 44100) gets screwed up
private short[] inbuf = new short[512]; //[8192]; // [512];
private short[] outbuf;
private short[] _outbuf;
// for sync
private short[] outbuf2 = new short[16];
private int outbuf2pos = 0;
private short[] _outbuf2 = new short[16];
private int _outbuf2pos = 0;
// to accept an ISyncSoundProvder input
private readonly ISoundProvider input;
/// <summary>
/// in buffer position in samples (not sample pairs)
/// </summary>
private int inbufpos = 0;
// in buffer position in samples (not sample pairs)
private int _inbufpos;
/// <summary>
/// throw an exception based on error state
/// </summary>
/// <param name="e"></param>
private static void CheckError(LibSpeexDSP.RESAMPLER_ERR e)
{
switch (e)
@ -299,14 +286,15 @@ namespace BizHawk.Emulation.Common
}
/// <summary>
/// Initializes a new instance of the <see cref="SpeexResampler"/> class
/// </summary>
/// <param name="quality">0 to 10</param>
/// <param name="rationum">numerator of srate change ratio (inrate / outrate)</param>
/// <param name="ratioden">demonenator of srate change ratio (inrate / outrate)</param>
/// <param name="rationum">numerator of sample rate change ratio (inrate / outrate)</param>
/// <param name="ratioden">denominator of sample rate change ratio (inrate / outrate)</param>
/// <param name="sratein">sampling rate in, rounded to nearest hz</param>
/// <param name="srateout">sampling rate out, rounded to nearest hz</param>
/// <param name="drainer">function which accepts output as produced. if null, act as an ISyncSoundProvider</param>
/// <param name="input">source to take input from when output is requested. if null, no autofetching</param>
/// <param name="drainer">function which accepts output as produced. if null, act as an <seealso cref="ISoundProvider"/></param>
/// <param name="input">source to take input from when output is requested. if null, no auto-fetching</param>
public SpeexResampler(int quality, uint rationum, uint ratioden, uint sratein, uint srateout, Action<short[], int> drainer = null, ISoundProvider input = null)
{
if (drainer != null && input != null)
@ -315,30 +303,30 @@ namespace BizHawk.Emulation.Common
}
LibSpeexDSP.RESAMPLER_ERR err = LibSpeexDSP.RESAMPLER_ERR.SUCCESS;
st = LibSpeexDSP.speex_resampler_init_frac(2, rationum, ratioden, sratein, srateout, quality, ref err);
_st = LibSpeexDSP.speex_resampler_init_frac(2, rationum, ratioden, sratein, srateout, quality, ref err);
if (st == IntPtr.Zero)
if (_st == IntPtr.Zero)
{
throw new Exception("LibSpeexDSP returned null!");
}
CheckError(err);
this.drainer = drainer ?? InternalDrain;
this.input = input;
_drainer = drainer ?? InternalDrain;
_input = input;
outbuf = new short[inbuf.Length * ratioden / rationum / 2 * 2 + 128];
_outbuf = new short[(_inbuf.Length * ratioden / rationum / 2 * 2) + 128];
}
/// <summary>change sampling rate on the fly</summary>
/// <param name="rationum">numerator of srate change ratio (inrate / outrate)</param>
/// <param name="ratioden">demonenator of srate change ratio (inrate / outrate)</param>
/// <param name="rationum">numerator of sample rate change ratio (inrate / outrate)</param>
/// <param name="ratioden">denominator of sample rate change ratio (inrate / outrate)</param>
/// <param name="sratein">sampling rate in, rounded to nearest hz</param>
/// <param name="srateout">sampling rate out, rounded to nearest hz</param>
public void ChangeRate(uint rationum, uint ratioden, uint sratein, uint srateout)
{
CheckError(LibSpeexDSP.speex_resampler_set_rate_frac(st, rationum, ratioden, sratein, srateout));
outbuf = new short[inbuf.Length * ratioden / rationum / 2 * 2 + 128];
CheckError(LibSpeexDSP.speex_resampler_set_rate_frac(_st, rationum, ratioden, sratein, srateout));
_outbuf = new short[(_inbuf.Length * ratioden / rationum / 2 * 2) + 128];
}
/// <summary>
@ -346,10 +334,10 @@ namespace BizHawk.Emulation.Common
/// </summary>
public void EnqueueSample(short left, short right)
{
inbuf[inbufpos++] = left;
inbuf[inbufpos++] = right;
_inbuf[_inbufpos++] = left;
_inbuf[_inbufpos++] = right;
if (inbufpos == inbuf.Length)
if (_inbufpos == _inbuf.Length)
{
Flush();
}
@ -365,52 +353,51 @@ namespace BizHawk.Emulation.Common
int numused = 0;
while (numused < nsamp)
{
int shortstocopy = Math.Min(inbuf.Length - inbufpos, (nsamp - numused) * 2);
int shortstocopy = Math.Min(_inbuf.Length - _inbufpos, (nsamp - numused) * 2);
Buffer.BlockCopy(userbuf, numused * 2 * sizeof(short), inbuf, inbufpos * sizeof(short), shortstocopy * sizeof(short));
inbufpos += shortstocopy;
Buffer.BlockCopy(userbuf, numused * 2 * sizeof(short), _inbuf, _inbufpos * sizeof(short), shortstocopy * sizeof(short));
_inbufpos += shortstocopy;
numused += shortstocopy / 2;
if (inbufpos == inbuf.Length)
if (_inbufpos == _inbuf.Length)
{
Flush();
}
}
}
/// <summary>
/// flush as many input samples as possible, generating output samples right now
/// </summary>
public void Flush()
{
uint inal = (uint)inbufpos / 2;
uint inal = (uint)_inbufpos / 2;
uint outal = (uint)outbuf.Length / 2;
uint outal = (uint)_outbuf.Length / 2;
LibSpeexDSP.speex_resampler_process_interleaved_int(st, inbuf, ref inal, outbuf, ref outal);
LibSpeexDSP.speex_resampler_process_interleaved_int(_st, _inbuf, ref inal, _outbuf, ref outal);
// reset inbuf
if (inal != inbufpos / 2)
if (inal != _inbufpos / 2)
{
throw new Exception("Speexresampler didn't eat the whole array?");
}
inbufpos = 0;
_inbufpos = 0;
//Buffer.BlockCopy(inbuf, (int)inal * 2 * sizeof(short), inbuf, 0, inbufpos - (int)inal * 2);
//inbufpos -= (int)inal * 2;
////Buffer.BlockCopy(inbuf, (int)inal * 2 * sizeof(short), inbuf, 0, inbufpos - (int)inal * 2);
////inbufpos -= (int)inal * 2;
// dispatch outbuf
drainer(outbuf, (int)outal);
_drainer(_outbuf, (int)outal);
}
public void Dispose()
{
if (st != IntPtr.Zero)
if (_st != IntPtr.Zero)
{
LibSpeexDSP.speex_resampler_destroy(st);
st = IntPtr.Zero;
LibSpeexDSP.speex_resampler_destroy(_st);
_st = IntPtr.Zero;
GC.SuppressFinalize(this);
}
}
@ -422,36 +409,36 @@ namespace BizHawk.Emulation.Common
private void InternalDrain(short[] buf, int nsamp)
{
if (outbuf2pos + nsamp * 2 > outbuf2.Length)
if (_outbuf2pos + (nsamp * 2) > _outbuf2.Length)
{
short[] newbuf = new short[outbuf2pos + nsamp * 2];
Buffer.BlockCopy(outbuf2, 0, newbuf, 0, outbuf2pos * sizeof(short));
outbuf2 = newbuf;
short[] newbuf = new short[_outbuf2pos + (nsamp * 2)];
Buffer.BlockCopy(_outbuf2, 0, newbuf, 0, _outbuf2pos * sizeof(short));
_outbuf2 = newbuf;
}
Buffer.BlockCopy(buf, 0, outbuf2, outbuf2pos * sizeof(short), nsamp * 2 * sizeof(short));
outbuf2pos += nsamp * 2;
Buffer.BlockCopy(buf, 0, _outbuf2, _outbuf2pos * sizeof(short), nsamp * 2 * sizeof(short));
_outbuf2pos += nsamp * 2;
}
public void GetSamplesSync(out short[] samples, out int nsamp)
{
if (input != null)
if (_input != null)
{
short[] sampin;
int nsampin;
input.GetSamplesSync(out sampin, out nsampin);
_input.GetSamplesSync(out sampin, out nsampin);
EnqueueSamples(sampin, nsampin);
}
Flush();
nsamp = outbuf2pos / 2;
samples = outbuf2;
outbuf2pos = 0;
nsamp = _outbuf2pos / 2;
samples = _outbuf2;
_outbuf2pos = 0;
}
public void DiscardSamples()
{
outbuf2pos = 0;
_outbuf2pos = 0;
}
public bool CanProvideAsync => false;
@ -472,4 +459,3 @@ namespace BizHawk.Emulation.Common
}
}
}

View File

@ -28,6 +28,7 @@
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=CGB/@EntryIndexedValue">CGB</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=CRC/@EntryIndexedValue">CRC</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=DB/@EntryIndexedValue">DB</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=DC/@EntryIndexedValue">DC</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=DMG/@EntryIndexedValue">DMG</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=GB/@EntryIndexedValue">GB</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=GBA/@EntryIndexedValue">GBA</s:String>