From 210d415e3d79280f51a8d8fffb3f0764b2cc3988 Mon Sep 17 00:00:00 2001 From: goyuken Date: Sun, 9 Dec 2012 03:13:47 +0000 Subject: [PATCH] Add Blargg's "blip_buf" as an unmanaged dll. license is LGPL; if I like the way it works, I'll rewrite it with MIT code. Implement it as the new resampling output track for NES. Small (~3%)? speedup. Should sound better, especially when the emulator can't quite keep speed. --- BizHawk.Emulation/BizHawk.Emulation.csproj | 1 + .../Consoles/Nintendo/NES/APU.cs | 134 +++-- .../Consoles/Nintendo/NES/Core.cs | 80 ++- .../Consoles/Nintendo/NES/NES.cs | 2 +- .../Sound/Utilities/BlipBuffer.cs | 140 +++++ BizHawk.MultiClient/Sound.cs | 3 + BizHawk.MultiClient/output/dll/blip_buf.dll | Bin 0 -> 25935 bytes blip_buf/Makefile | 9 + blip_buf/blip_buf.c | 344 ++++++++++++ blip_buf/blip_buf.h | 72 +++ blip_buf/blip_buf.txt | 250 +++++++++ blip_buf/changes.txt | 39 ++ blip_buf/license.txt | 504 ++++++++++++++++++ blip_buf/makefile_orig | 22 + blip_buf/readme.txt | 62 +++ 15 files changed, 1592 insertions(+), 70 deletions(-) create mode 100644 BizHawk.Emulation/Sound/Utilities/BlipBuffer.cs create mode 100644 BizHawk.MultiClient/output/dll/blip_buf.dll create mode 100644 blip_buf/Makefile create mode 100644 blip_buf/blip_buf.c create mode 100644 blip_buf/blip_buf.h create mode 100644 blip_buf/blip_buf.txt create mode 100644 blip_buf/changes.txt create mode 100644 blip_buf/license.txt create mode 100644 blip_buf/makefile_orig create mode 100644 blip_buf/readme.txt diff --git a/BizHawk.Emulation/BizHawk.Emulation.csproj b/BizHawk.Emulation/BizHawk.Emulation.csproj index 6f6a10cd86..7e0118d411 100644 --- a/BizHawk.Emulation/BizHawk.Emulation.csproj +++ b/BizHawk.Emulation/BizHawk.Emulation.csproj @@ -502,6 +502,7 @@ + diff --git a/BizHawk.Emulation/Consoles/Nintendo/NES/APU.cs b/BizHawk.Emulation/Consoles/Nintendo/NES/APU.cs index d98d62b147..6177c40212 100644 --- a/BizHawk.Emulation/Consoles/Nintendo/NES/APU.cs +++ b/BizHawk.Emulation/Consoles/Nintendo/NES/APU.cs @@ -33,12 +33,17 @@ namespace BizHawk.Emulation.Consoles.Nintendo public bool EnableNoise = false; public bool EnableDMC = true; + public bool recalculate = false; + NES nes; public APU(NES nes, APU old, bool pal) { this.nes = nes; dmc = new DMCUnit(this, pal); - noise = new NoiseUnit(pal); + noise = new NoiseUnit(this, pal); + triangle = new TriangleUnit(this); + pulse[0] = new PulseUnit(this, 0); + pulse[1] = new PulseUnit(this, 1); if (old != null) { EnableSquare1 = old.EnableSquare1; @@ -75,8 +80,9 @@ namespace BizHawk.Emulation.Consoles.Nintendo class PulseUnit { - public PulseUnit(int unit) { this.unit = unit; } + public PulseUnit(APU apu, int unit) { this.unit = unit; this.apu = apu; } public int unit; + APU apu; //reg0 int duty_cnt, env_loop, env_constant, env_cnt_value; @@ -251,26 +257,35 @@ namespace BizHawk.Emulation.Consoles.Nintendo //reload timer timer_counter = timer_raw_reload_value; } - + + int newsample; + if (duty_value) //high state of duty cycle { - sample = env_output; + newsample = env_output; if (swp_silence) - sample = env_output >> 1; //(a little biasing hack here) + newsample = env_output >> 1; //(a little biasing hack here) if (len_cnt == 0) //length counter is 0 - sample = env_output >> 1; //silenced (a little biasing hack here) + newsample = env_output >> 1; //silenced (a little biasing hack here) } else - sample = env_output >> 1; //duty cycle is 0, silenced. + newsample = env_output >> 1; //duty cycle is 0, silenced. - sample -= env_output >> 1; //unbias + newsample -= env_output >> 1; //unbias + if (newsample != sample) + { + apu.recalculate = true; + sample = newsample; + } } } class NoiseUnit { + APU apu; + //reg0 (sweep) int env_cnt_value, env_loop, env_constant; @@ -292,8 +307,9 @@ namespace BizHawk.Emulation.Consoles.Nintendo int[] NOISE_TABLE; - public NoiseUnit(bool pal) + public NoiseUnit(APU apu, bool pal) { + this.apu = apu; NOISE_TABLE = pal ? NOISE_TABLE_PAL : NOISE_TABLE_NTSC; } @@ -413,9 +429,15 @@ namespace BizHawk.Emulation.Consoles.Nintendo } //unbiasing is rolled in here - if (len_cnt == 0) sample = 0; - else if (noise_bit) sample = -env_output; - else sample = env_output; + int newsample; + if (len_cnt == 0) newsample = 0; + else if (noise_bit) newsample = -env_output; + else newsample = env_output; + if (newsample != sample) + { + apu.recalculate = true; + sample = newsample; + } } } @@ -433,6 +455,9 @@ namespace BizHawk.Emulation.Consoles.Nintendo int seq = 15; public int sample; + APU apu; + public TriangleUnit(APU apu) { this.apu = apu; } + public void SyncState(Serializer ser) { ser.Sync("linear_counter_reload", ref linear_counter_reload); @@ -500,6 +525,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo //is clocked in frame counter. if (en) { + int newsample; if (timer > 0) timer--; if (timer == 0) { @@ -507,16 +533,21 @@ namespace BizHawk.Emulation.Consoles.Nintendo timer = timer_cnt_reload; } if(CFG_DECLICK) - sample = TRIANGLE_TABLE[(seq+8)&0x1F]; + newsample = TRIANGLE_TABLE[(seq + 8) & 0x1F]; else - sample = TRIANGLE_TABLE[seq]; + newsample = TRIANGLE_TABLE[seq]; //special hack: frequently, games will use the maximum frequency triangle in order to mute it //apparently this results in the DAC for the triangle wave outputting a steady level at about 7.5 //so we'll emulate it at the digital level - if (timer_cnt_reload == 1) sample = 8; + if (timer_cnt_reload == 1) newsample = 8; - sample -= 8; //unbias + newsample -= 8; //unbias + if (newsample != sample) + { + apu.recalculate = true; + sample = newsample; + } } } @@ -640,6 +671,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo out_deltacounter -= 2; } //apu.nes.LogLine("dmc out sample: {0}", out_deltacounter); + apu.recalculate = true; } //The right shift register is clocked. @@ -721,6 +753,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo case 1: out_deltacounter = val & 0x7F; //apu.nes.LogLine("~~ out_deltacounter set to {0}", out_deltacounter); + apu.recalculate = true; break; case 2: user_address = 0xC000 | (val << 6); @@ -775,8 +808,8 @@ namespace BizHawk.Emulation.Consoles.Nintendo SyncIRQ(); } - PulseUnit[] pulse = { new PulseUnit(0), new PulseUnit(1) }; - TriangleUnit triangle = new TriangleUnit(); + PulseUnit[] pulse = new PulseUnit[2]; + TriangleUnit triangle; NoiseUnit noise; //= new NoiseUnit(); DMCUnit dmc; @@ -1041,17 +1074,32 @@ namespace BizHawk.Emulation.Consoles.Nintendo //we want the changes to affect it on the *next* cycle. } - int loopy = 0; - public const int DECIMATIONFACTOR = 20; - const int QUEUESIZE = 1789773 / DECIMATIONFACTOR; //1 second, should be enough - public QuickQueue squeue = new QuickQueue(QUEUESIZE); + //int loopy = 0; + //public const int DECIMATIONFACTOR = 20; + //const int QUEUESIZE = 1789773 / DECIMATIONFACTOR; //1 second, should be enough + //public QuickQueue squeue = new QuickQueue(QUEUESIZE); + + public struct Delta + { + public uint time; + public int value; + public Delta(uint time, int value) + { + this.time = time; + this.value = value; + } + } + public List dlist = new List(); + + public uint sampleclock = 0; + + int oldmix = 0; + void EmitSample() { - //here we throw out 19/20 of the samples, for an easy speedup... blech. - loopy++; - if (loopy == DECIMATIONFACTOR) + if (recalculate) { - loopy = 0; + recalculate = false; int s_pulse0 = pulse[0].sample; int s_pulse1 = pulse[1].sample; @@ -1075,24 +1123,24 @@ namespace BizHawk.Emulation.Consoles.Nintendo //this needs to leave enough headroom for straying DC bias due to the DMC unit getting stuck outputs. smb3 is bad about that. int mix = (int)(50000 * output); - //more properly correct - //float pulse_out, tnd_out; - //if (s_pulse0 == 0 && s_pulse1 == 0) - // pulse_out = 0; - //else pulse_out = 95.88f / ((8128.0f / (s_pulse0 + s_pulse1)) + 100.0f); - //if (s_tri == 0 && s_noise == 0 && s_dmc == 0) - // tnd_out = 0; - //else tnd_out = 159.79f / (1 / ((s_tri / 8227.0f) + (s_noise / 12241.0f * NOISEADJUST) + (s_dmc / 22638.0f)) + 100); - //float output = pulse_out + tnd_out; - //output = output * 2 - 1; - //this needs to leave enough headroom for straying DC bias due to the DMC unit getting stuck outputs. smb3 is bad about that. - //int mix = (int)(20000 * output); - - - - if (squeue.Count < QUEUESIZE) - squeue.Enqueue((short)mix); + dlist.Add(new Delta(sampleclock, mix - oldmix)); + oldmix = mix; } + //more properly correct + //float pulse_out, tnd_out; + //if (s_pulse0 == 0 && s_pulse1 == 0) + // pulse_out = 0; + //else pulse_out = 95.88f / ((8128.0f / (s_pulse0 + s_pulse1)) + 100.0f); + //if (s_tri == 0 && s_noise == 0 && s_dmc == 0) + // tnd_out = 0; + //else tnd_out = 159.79f / (1 / ((s_tri / 8227.0f) + (s_noise / 12241.0f * NOISEADJUST) + (s_dmc / 22638.0f)) + 100); + //float output = pulse_out + tnd_out; + //output = output * 2 - 1; + //this needs to leave enough headroom for straying DC bias due to the DMC unit getting stuck outputs. smb3 is bad about that. + //int mix = (int)(20000 * output); + + + sampleclock++; } } //class APU diff --git a/BizHawk.Emulation/Consoles/Nintendo/NES/Core.cs b/BizHawk.Emulation/Consoles/Nintendo/NES/Core.cs index 18b52d891d..5e4318dc54 100644 --- a/BizHawk.Emulation/Consoles/Nintendo/NES/Core.cs +++ b/BizHawk.Emulation/Consoles/Nintendo/NES/Core.cs @@ -62,56 +62,84 @@ namespace BizHawk.Emulation.Consoles.Nintendo } } - class MagicSoundProvider : ISoundProvider, IDisposable + class MagicSoundProvider : ISoundProvider, ISyncSoundProvider, IDisposable { - Sound.Utilities.SpeexResampler resampler; - ISoundProvider output; + Sound.Utilities.BlipBuffer blip; NES nes; + const int blipbuffsize = 4096; + public MagicSoundProvider(NES nes, uint infreq) { this.nes = nes; - var actualMetaspu = new Sound.MetaspuSoundProvider(Sound.ESynchMethod.ESynchMethod_V); + + blip = new Sound.Utilities.BlipBuffer(blipbuffsize); + blip.SetRates(infreq, 44100); + + //var actualMetaspu = new Sound.MetaspuSoundProvider(Sound.ESynchMethod.ESynchMethod_V); //1.789773mhz NTSC - resampler = new Sound.Utilities.SpeexResampler(2, infreq, 44100 * APU.DECIMATIONFACTOR, infreq, 44100, actualMetaspu.buffer.enqueue_samples); - output = new Sound.Utilities.DCFilter(actualMetaspu); + //resampler = new Sound.Utilities.SpeexResampler(2, infreq, 44100 * APU.DECIMATIONFACTOR, infreq, 44100, actualMetaspu.buffer.enqueue_samples); + //output = new Sound.Utilities.DCFilter(actualMetaspu); } - Random r = new Random(); public void GetSamples(short[] samples) { - if (nes.apu.squeue.Count == 0) - return; - var monosampbuf = nes.apu.squeue.ToArray(2); - nes.apu.squeue.Clear(); - if (monosampbuf.Length > 0) - { - var stereosampbuf = new short[monosampbuf.Length * 2]; - for (int i = 0; i < monosampbuf.Length; i++) - { - stereosampbuf[i * 2 + 0] = monosampbuf[i]; - stereosampbuf[i * 2 + 1] = monosampbuf[i]; - } - resampler.EnqueueSamples(stereosampbuf, monosampbuf.Length); - resampler.Flush(); - output.GetSamples(samples); - } + Console.WriteLine("Sync: {0}", nes.apu.dlist.Count); + int nsamp = samples.Length / 2; + if (nsamp > blipbuffsize) // oh well. + nsamp = blipbuffsize; + uint targetclock = (uint)blip.ClocksNeeded(nsamp); + uint actualclock = nes.apu.sampleclock; + foreach (var d in nes.apu.dlist) + blip.AddDelta(d.time * targetclock / actualclock, d.value); + nes.apu.dlist.Clear(); + blip.EndFrame(targetclock); + nes.apu.sampleclock = 0; + + blip.ReadSamples(samples, nsamp, true); + // duplicate to stereo + for (int i = 0; i < nsamp * 2; i += 2) + samples[i + 1] = samples[i]; //mix in the cart's extra sound circuit nes.board.ApplyCustomAudio(samples); } + public void GetSamples(out short[] samples, out int nsamp) + { + Console.WriteLine("ASync: {0}", nes.apu.dlist.Count); + foreach (var d in nes.apu.dlist) + blip.AddDelta(d.time, d.value); + nes.apu.dlist.Clear(); + blip.EndFrame(nes.apu.sampleclock); + nes.apu.sampleclock = 0; + + nsamp = blip.SamplesAvailable(); + samples = new short[nsamp * 2]; + + blip.ReadSamples(samples, nsamp, true); + // duplicate to stereo + for (int i = 0; i < nsamp * 2; i += 2) + samples[i + 1] = samples[i]; + + nes.board.ApplyCustomAudio(samples); + } + public void DiscardSamples() { - output.DiscardSamples(); + nes.apu.dlist.Clear(); + nes.apu.sampleclock = 0; } public int MaxVolume { get; set; } public void Dispose() { - resampler.Dispose(); - resampler = null; + if (blip != null) + { + blip.Dispose(); + blip = null; + } } } MagicSoundProvider magicSoundProvider; diff --git a/BizHawk.Emulation/Consoles/Nintendo/NES/NES.cs b/BizHawk.Emulation/Consoles/Nintendo/NES/NES.cs index 3cd692a9df..69b905af5e 100644 --- a/BizHawk.Emulation/Consoles/Nintendo/NES/NES.cs +++ b/BizHawk.Emulation/Consoles/Nintendo/NES/NES.cs @@ -224,7 +224,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo MyVideoProvider videoProvider; public IVideoProvider VideoProvider { get { return videoProvider; } } public ISoundProvider SoundProvider { get { return magicSoundProvider; } } - public ISyncSoundProvider SyncSoundProvider { get { return new FakeSyncSound(magicSoundProvider, CoreOutputComm.VsyncRate > 55 ? 734 : 882); } } + public ISyncSoundProvider SyncSoundProvider { get { return magicSoundProvider; } } public bool StartAsyncSound() { return true; } public void EndAsyncSound() { } diff --git a/BizHawk.Emulation/Sound/Utilities/BlipBuffer.cs b/BizHawk.Emulation/Sound/Utilities/BlipBuffer.cs new file mode 100644 index 0000000000..4ab0549e64 --- /dev/null +++ b/BizHawk.Emulation/Sound/Utilities/BlipBuffer.cs @@ -0,0 +1,140 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Runtime.InteropServices; + +namespace BizHawk.Emulation.Sound.Utilities +{ + /// + /// wrapper around blargg's unmanaged blip_buf + /// + public class BlipBuffer : IDisposable + { + // this is transitional only. if the band-limited synthesis idea works out, i'll + // make a managed MIT implementation + + static class BlipBufDll + { + /** Creates new buffer that can hold at most sample_count samples. Sets rates + so that there are blip_max_ratio clocks per sample. Returns pointer to new + buffer, or NULL if insufficient memory. */ + [DllImport("blip_buf.dll", CallingConvention = CallingConvention.Cdecl)] + public static extern IntPtr blip_new(int sample_count); + + /** Sets approximate input clock rate and output sample rate. For every + clock_rate input clocks, approximately sample_rate samples are generated. */ + [DllImport("blip_buf.dll", CallingConvention = CallingConvention.Cdecl)] + public static extern void blip_set_rates(IntPtr context, double clock_rate, double sample_rate); + + /** Maximum clock_rate/sample_rate ratio. For a given sample_rate, + clock_rate must not be greater than sample_rate*blip_max_ratio. */ + public const int blip_max_ratio = 1 << 20; + + /** Clears entire buffer. Afterwards, blip_samples_avail() == 0. */ + [DllImport("blip_buf.dll", CallingConvention = CallingConvention.Cdecl)] + public static extern void blip_clear(IntPtr context); + + /** Adds positive/negative delta into buffer at specified clock time. */ + [DllImport("blip_buf.dll", CallingConvention = CallingConvention.Cdecl)] + public static extern void blip_add_delta(IntPtr context, uint clock_time, int delta); + + /** Same as blip_add_delta(), but uses faster, lower-quality synthesis. */ + [DllImport("blip_buf.dll", CallingConvention = CallingConvention.Cdecl)] + public static extern void blip_add_delta_fast(IntPtr context, uint clock_time, int delta); + + /** Length of time frame, in clocks, needed to make sample_count additional + samples available. */ + [DllImport("blip_buf.dll", CallingConvention = CallingConvention.Cdecl)] + public static extern int blip_clocks_needed(IntPtr context, int sample_count); + + /** Maximum number of samples that can be generated from one time frame. */ + public const int blip_max_frame = 4000; + + /** Makes input clocks before clock_duration available for reading as output + samples. Also begins new time frame at clock_duration, so that clock time 0 in + the new time frame specifies the same clock as clock_duration in the old time + frame specified. Deltas can have been added slightly past clock_duration (up to + however many clocks there are in two output samples). */ + [DllImport("blip_buf.dll", CallingConvention = CallingConvention.Cdecl)] + public static extern void blip_end_frame(IntPtr context, uint clock_duration); + + /** Number of buffered samples available for reading. */ + [DllImport("blip_buf.dll", CallingConvention = CallingConvention.Cdecl)] + public static extern int blip_samples_avail(IntPtr context); + + /** Reads and removes at most 'count' samples and writes them to 'out'. If + 'stereo' is true, writes output to every other element of 'out', allowing easy + interleaving of two buffers into a stereo sample stream. Outputs 16-bit signed + samples. Returns number of samples actually read. */ + [DllImport("blip_buf.dll", CallingConvention = CallingConvention.Cdecl)] + public static extern int blip_read_samples(IntPtr context, short[] @out, int count, int stereo); + + /** Frees buffer. No effect if NULL is passed. */ + [DllImport("blip_buf.dll", CallingConvention = CallingConvention.Cdecl)] + public static extern void blip_delete(IntPtr context); + } + + IntPtr context; + + public BlipBuffer(int sample_count) + { + context = BlipBufDll.blip_new(sample_count); + if (context == IntPtr.Zero) + throw new Exception("blip_new returned NULL!"); + } + + public void Dispose() + { + BlipBufDll.blip_delete(context); + context = IntPtr.Zero; + } + + public void SetRates(double clock_rate, double sample_rate) + { + BlipBufDll.blip_set_rates(context, clock_rate, sample_rate); + } + + public const int MaxRatio = BlipBufDll.blip_max_ratio; + + public void Clear() + { + BlipBufDll.blip_clear(context); + } + + + public void AddDelta(uint clock_time, int delta) + { + BlipBufDll.blip_add_delta(context, clock_time, delta); + } + + public void AddDeltaFast(uint clock_time, int delta) + { + BlipBufDll.blip_add_delta_fast(context, clock_time, delta); + } + + public int ClocksNeeded(int sample_count) + { + return BlipBufDll.blip_clocks_needed(context, sample_count); + } + + public const int MaxFrame = BlipBufDll.blip_max_frame; + + public void EndFrame(uint clock_duration) + { + BlipBufDll.blip_end_frame(context, clock_duration); + } + + public int SamplesAvailable() + { + return BlipBufDll.blip_samples_avail(context); + } + + public int ReadSamples(short[] output, int count, bool stereo) + { + if (output.Length < count * (stereo ? 2 : 1)) + throw new ArgumentOutOfRangeException(); + return BlipBufDll.blip_read_samples(context, output, count, stereo ? 1 : 0); + } + } +} diff --git a/BizHawk.MultiClient/Sound.cs b/BizHawk.MultiClient/Sound.cs index 8d4f5a4dcd..006aa24f77 100644 --- a/BizHawk.MultiClient/Sound.cs +++ b/BizHawk.MultiClient/Sound.cs @@ -166,6 +166,9 @@ namespace BizHawk.MultiClient return; samples = new short[samplesNeeded]; samplesProvided = samplesNeeded; + + if (asyncsoundProvider != null) asyncsoundProvider.DiscardSamples(); + if (syncsoundProvider != null) syncsoundProvider.DiscardSamples(); } else if (syncsoundProvider != null) { diff --git a/BizHawk.MultiClient/output/dll/blip_buf.dll b/BizHawk.MultiClient/output/dll/blip_buf.dll new file mode 100644 index 0000000000000000000000000000000000000000..a26adfc124d3a268fac92653b80ba88fe9f1e3e6 GIT binary patch literal 25935 zcmeHv3w%`7o&T9fFoci^5EN^~QAZ6G(is>+A^{Q-UK%8Xpb?OpNoE3($-|im4~uPZ z2xL0MW?S9WuDZC=t?RmAcd5lnB_SY>wTk%SrQ)-}prTksz})@*&bfE)+?k2~$!9

=|{2`5P9r&j!w;4+Q zbY=O{TEDf?*RaIrsgyW<)+&$H*HmwnP;tQaC@=`4^FWutV^dcR$@qD zty{ZFnUk@rjEo&;1FjxqFkZ|ufMiDotTG)XWva8*Dt-+(>1{x!lBW**MdjHL<90D- z6?8;E^hmsDWNa)EHZ!&teELjJF}9TO`brxa84s!1aK9U9q_CSpkf<}Kco=Z z3caIKB`dAWcDn*D0F$Y~j!e472rOXsj%=Smv=fg4R?=DZ^-hKEfp9f8yG0nyBX%X>l@w(is+jO|Gx8uHT~ zvyF=G-gL%-yGAVRYI~aMZQHM-B6xCiXpzktUSxBI0ybMXV6%r-+p@x|ZMng7hCneY z$v`lNoVM=vfrOvhavQPZq5k8?tr}?1|(1X+m1+7)x2Z2%sOFblK7$78PH_@F; z26za8lSvJ;ZBuD$75a9t*$&_A3pfB8MJ#@UuDq?y{P0rh3>fiv z8p@qumoGYYPl1{DQ$@y}0-C~__Y_bcussFn4=VTs(w6%0Ow`&ctd5S*+ozuh9SH8q zSSWQtrDU2uNVR6D2BX4c`~pa+X}>jGZcD~+xa1ita6tJ6C;^gA4CU=oS55mZ(9;RL zg2PFTh^}KLwvuB3Tk+wf0BVQ#z4^}D$G_Z_WLet`4sRYlyxKOh25~TQYibPz^Wl!e zUR&ndt8G@4Bf}+pfY?GQp#|m;Di{#9x4qm4KYnwOE$hu2^L~wrdFnBjD zJKTNb1c{l}wDj;k^>R?K(-3@xabCe)&V`G++K;_+cwd|b3FNm0p=@)g>x7@ecsVJ&J15T1`04xyLpyn# zb-tSsZb}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UQ$i1&mT~~{la;Tm0G3P>z$1^ ztC)J`1zb4%+*sfk%hVq&_7IzCGo|Teg|nwgQ>V?Cbu)$=U^f<(NhP!9m5Unvpn?8} z2ndIuu|H72@Och?e}ImHh%4p1h^Y7@XFfz-95qp|qtBWMn0CNKp%=eLpd2F+Fx_Dj z0n!e+#MH$p7xLVydI>K_B{zNzf&38#s>PWC1r+eYXhLC<;zgyw=V`>J{^)3mJmU&hQN=>|mPz_=gLGt2k`* z$Ym;uj(N4vG@il-YnOZCiKOz9d9>72xvH13=ua%hkYwCVKy;J4cDcKD99@dYn|5=t z7I#WTA8-;mK|B)6_KQGf+1xSNIpfEV8_y*DQH}yf4zVwre{=aPgm&q?Y4eNcNM+MX z<`ijxO~0UxkIB$sLV9ueoT#v`t!tDLaLr!faV^Wn5Q+Q24gmR}&}$AgIH%|%E(H`H z(fs*wlStV4qm<%U<=UZ@0y>g{-wqLHH6$0pf*w38?D{+rLu3gL<#2v*K%<`rs3MxF zR>q8bNULf1c00woC1$7Ur7jV09*ViJHXl|h&Kad(~$i(VpQguUJU9E7xz5zeM z;PD~Ah@{SSM}trDH&yvp;nTl5N%^@2(P{YTg6;~Ux{*U_6F|gnLW=E)=pTjPeZYqn zA8+1(-;J|D5`H71I^tx0;D@;Ajwo?QCUO@H!1MZOb^4A#M*E0IgK~eR z#Z}a?r~*tU&@2WbN!%xm(Ml)~#V0fsz{d@KopT}H9UW{u%v?)6QkBauy2dY#c1bve zwWPkzQ!l!btErx5lB8LM<;qU(yGA5U%Dg8biH}ZIy&~PvB!8+c!nEp0omkY?yFDwx zLT!r*4m~4X;M0%TC{E?!tDO2$K+4^OgvHkDNn~%L6K6aqgg3+%R1M}BN0Wy%CCLPs z7g0tzbf$H9g22OOAfm5s9!qrO4r)aPc +#include +#include +#include + +/* Library Copyright (C) 2003-2009 Shay Green. This library 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 +version 2.1 of the License, or (at your option) any later version. This +library is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more +details. You should have received a copy of the GNU Lesser General Public +License along with this module; if not, write to the Free Software Foundation, +Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ + +#if defined (BLARGG_TEST) && BLARGG_TEST + #include "blargg_test.h" +#endif + +/* Equivalent to ULONG_MAX >= 0xFFFFFFFF00000000. +Avoids constants that don't fit in 32 bits. */ +#if ULONG_MAX/0xFFFFFFFF > 0xFFFFFFFF + typedef unsigned long fixed_t; + enum { pre_shift = 32 }; + +#elif defined(ULLONG_MAX) + typedef unsigned long long fixed_t; + enum { pre_shift = 32 }; + +#else + typedef unsigned fixed_t; + enum { pre_shift = 0 }; + +#endif + +enum { time_bits = pre_shift + 20 }; + +static fixed_t const time_unit = (fixed_t) 1 << time_bits; + +enum { bass_shift = 9 }; /* affects high-pass filter breakpoint frequency */ +enum { end_frame_extra = 2 }; /* allows deltas slightly after frame length */ + +enum { half_width = 8 }; +enum { buf_extra = half_width*2 + end_frame_extra }; +enum { phase_bits = 5 }; +enum { phase_count = 1 << phase_bits }; +enum { delta_bits = 15 }; +enum { delta_unit = 1 << delta_bits }; +enum { frac_bits = time_bits - pre_shift }; + +/* We could eliminate avail and encode whole samples in offset, but that would +limit the total buffered samples to blip_max_frame. That could only be +increased by decreasing time_bits, which would reduce resample ratio accuracy. +*/ + +/** Sample buffer that resamples to output rate and accumulates samples +until they're read out */ +struct blip_t +{ + fixed_t factor; + fixed_t offset; + int avail; + int size; + int integrator; +}; + +typedef int buf_t; + +/* probably not totally portable */ +#define SAMPLES( buf ) ((buf_t*) ((buf) + 1)) + +/* Arithmetic (sign-preserving) right shift */ +#define ARITH_SHIFT( n, shift ) \ + ((n) >> (shift)) + +enum { max_sample = +32767 }; +enum { min_sample = -32768 }; + +#define CLAMP( n ) \ + {\ + if ( (short) n != n )\ + n = ARITH_SHIFT( n, 16 ) ^ max_sample;\ + } + +static void check_assumptions( void ) +{ + int n; + + #if INT_MAX < 0x7FFFFFFF || UINT_MAX < 0xFFFFFFFF + #error "int must be at least 32 bits" + #endif + + assert( (-3 >> 1) == -2 ); /* right shift must preserve sign */ + + n = max_sample * 2; + CLAMP( n ); + assert( n == max_sample ); + + n = min_sample * 2; + CLAMP( n ); + assert( n == min_sample ); + + assert( blip_max_ratio <= time_unit ); + assert( blip_max_frame <= (fixed_t) -1 >> time_bits ); +} + +blip_t* blip_new( int size ) +{ + blip_t* m; + assert( size >= 0 ); + + m = (blip_t*) malloc( sizeof *m + (size + buf_extra) * sizeof (buf_t) ); + if ( m ) + { + m->factor = time_unit / blip_max_ratio; + m->size = size; + blip_clear( m ); + check_assumptions(); + } + return m; +} + +void blip_delete( blip_t* m ) +{ + if ( m != NULL ) + { + /* Clear fields in case user tries to use after freeing */ + memset( m, 0, sizeof *m ); + free( m ); + } +} + +void blip_set_rates( blip_t* m, double clock_rate, double sample_rate ) +{ + double factor = time_unit * sample_rate / clock_rate; + m->factor = (fixed_t) factor; + + /* Fails if clock_rate exceeds maximum, relative to sample_rate */ + assert( 0 <= factor - m->factor && factor - m->factor < 1 ); + + /* Avoid requiring math.h. Equivalent to + m->factor = (int) ceil( factor ) */ + if ( m->factor < factor ) + m->factor++; + + /* At this point, factor is most likely rounded up, but could still + have been rounded down in the floating-point calculation. */ +} + +void blip_clear( blip_t* m ) +{ + /* We could set offset to 0, factor/2, or factor-1. 0 is suitable if + factor is rounded up. factor-1 is suitable if factor is rounded down. + Since we don't know rounding direction, factor/2 accommodates either, + with the slight loss of showing an error in half the time. Since for + a 64-bit factor this is years, the halving isn't a problem. */ + + m->offset = m->factor / 2; + m->avail = 0; + m->integrator = 0; + memset( SAMPLES( m ), 0, (m->size + buf_extra) * sizeof (buf_t) ); +} + +int blip_clocks_needed( const blip_t* m, int samples ) +{ + fixed_t needed; + + /* Fails if buffer can't hold that many more samples */ + assert( samples >= 0 && m->avail + samples <= m->size ); + + needed = (fixed_t) samples * time_unit; + if ( needed < m->offset ) + return 0; + + return (needed - m->offset + m->factor - 1) / m->factor; +} + +void blip_end_frame( blip_t* m, unsigned t ) +{ + fixed_t off = t * m->factor + m->offset; + m->avail += off >> time_bits; + m->offset = off & (time_unit - 1); + + /* Fails if buffer size was exceeded */ + assert( m->avail <= m->size ); +} + +int blip_samples_avail( const blip_t* m ) +{ + return m->avail; +} + +static void remove_samples( blip_t* m, int count ) +{ + buf_t* buf = SAMPLES( m ); + int remain = m->avail + buf_extra - count; + m->avail -= count; + + memmove( &buf [0], &buf [count], remain * sizeof buf [0] ); + memset( &buf [remain], 0, count * sizeof buf [0] ); +} + +int blip_read_samples( blip_t* m, short out [], int count, int stereo ) +{ + assert( count >= 0 ); + + if ( count > m->avail ) + count = m->avail; + + if ( count ) + { + int const step = stereo ? 2 : 1; + buf_t const* in = SAMPLES( m ); + buf_t const* end = in + count; + int sum = m->integrator; + do + { + /* Eliminate fraction */ + int s = ARITH_SHIFT( sum, delta_bits ); + + sum += *in++; + + CLAMP( s ); + + *out = s; + out += step; + + /* High-pass filter */ + sum -= s << (delta_bits - bass_shift); + } + while ( in != end ); + m->integrator = sum; + + remove_samples( m, count ); + } + + return count; +} + +/* Things that didn't help performance on x86: + __attribute__((aligned(128))) + #define short int + restrict +*/ + +/* Sinc_Generator( 0.9, 0.55, 4.5 ) */ +static short const bl_step [phase_count + 1] [half_width] = +{ +{ 43, -115, 350, -488, 1136, -914, 5861,21022}, +{ 44, -118, 348, -473, 1076, -799, 5274,21001}, +{ 45, -121, 344, -454, 1011, -677, 4706,20936}, +{ 46, -122, 336, -431, 942, -549, 4156,20829}, +{ 47, -123, 327, -404, 868, -418, 3629,20679}, +{ 47, -122, 316, -375, 792, -285, 3124,20488}, +{ 47, -120, 303, -344, 714, -151, 2644,20256}, +{ 46, -117, 289, -310, 634, -17, 2188,19985}, +{ 46, -114, 273, -275, 553, 117, 1758,19675}, +{ 44, -108, 255, -237, 471, 247, 1356,19327}, +{ 43, -103, 237, -199, 390, 373, 981,18944}, +{ 42, -98, 218, -160, 310, 495, 633,18527}, +{ 40, -91, 198, -121, 231, 611, 314,18078}, +{ 38, -84, 178, -81, 153, 722, 22,17599}, +{ 36, -76, 157, -43, 80, 824, -241,17092}, +{ 34, -68, 135, -3, 8, 919, -476,16558}, +{ 32, -61, 115, 34, -60, 1006, -683,16001}, +{ 29, -52, 94, 70, -123, 1083, -862,15422}, +{ 27, -44, 73, 106, -184, 1152,-1015,14824}, +{ 25, -36, 53, 139, -239, 1211,-1142,14210}, +{ 22, -27, 34, 170, -290, 1261,-1244,13582}, +{ 20, -20, 16, 199, -335, 1301,-1322,12942}, +{ 18, -12, -3, 226, -375, 1331,-1376,12293}, +{ 15, -4, -19, 250, -410, 1351,-1408,11638}, +{ 13, 3, -35, 272, -439, 1361,-1419,10979}, +{ 11, 9, -49, 292, -464, 1362,-1410,10319}, +{ 9, 16, -63, 309, -483, 1354,-1383, 9660}, +{ 7, 22, -75, 322, -496, 1337,-1339, 9005}, +{ 6, 26, -85, 333, -504, 1312,-1280, 8355}, +{ 4, 31, -94, 341, -507, 1278,-1205, 7713}, +{ 3, 35, -102, 347, -506, 1238,-1119, 7082}, +{ 1, 40, -110, 350, -499, 1190,-1021, 6464}, +{ 0, 43, -115, 350, -488, 1136, -914, 5861} +}; + +/* Shifting by pre_shift allows calculation using unsigned int rather than +possibly-wider fixed_t. On 32-bit platforms, this is likely more efficient. +And by having pre_shift 32, a 32-bit platform can easily do the shift by +simply ignoring the low half. */ + +void blip_add_delta( blip_t* m, unsigned time, int delta ) +{ + unsigned fixed = (unsigned) ((time * m->factor + m->offset) >> pre_shift); + buf_t* out = SAMPLES( m ) + m->avail + (fixed >> frac_bits); + + int const phase_shift = frac_bits - phase_bits; + int phase = fixed >> phase_shift & (phase_count - 1); + short const* in = bl_step [phase]; + short const* rev = bl_step [phase_count - phase]; + + int interp = fixed >> (phase_shift - delta_bits) & (delta_unit - 1); + int delta2 = (delta * interp) >> delta_bits; + delta -= delta2; + + /* Fails if buffer size was exceeded */ + assert( out <= &SAMPLES( m ) [m->size + end_frame_extra] ); + + out [0] += in[0]*delta + in[half_width+0]*delta2; + out [1] += in[1]*delta + in[half_width+1]*delta2; + out [2] += in[2]*delta + in[half_width+2]*delta2; + out [3] += in[3]*delta + in[half_width+3]*delta2; + out [4] += in[4]*delta + in[half_width+4]*delta2; + out [5] += in[5]*delta + in[half_width+5]*delta2; + out [6] += in[6]*delta + in[half_width+6]*delta2; + out [7] += in[7]*delta + in[half_width+7]*delta2; + + in = rev; + out [ 8] += in[7]*delta + in[7-half_width]*delta2; + out [ 9] += in[6]*delta + in[6-half_width]*delta2; + out [10] += in[5]*delta + in[5-half_width]*delta2; + out [11] += in[4]*delta + in[4-half_width]*delta2; + out [12] += in[3]*delta + in[3-half_width]*delta2; + out [13] += in[2]*delta + in[2-half_width]*delta2; + out [14] += in[1]*delta + in[1-half_width]*delta2; + out [15] += in[0]*delta + in[0-half_width]*delta2; +} + +void blip_add_delta_fast( blip_t* m, unsigned time, int delta ) +{ + unsigned fixed = (unsigned) ((time * m->factor + m->offset) >> pre_shift); + buf_t* out = SAMPLES( m ) + m->avail + (fixed >> frac_bits); + + int interp = fixed >> (frac_bits - delta_bits) & (delta_unit - 1); + int delta2 = delta * interp; + + /* Fails if buffer size was exceeded */ + assert( out <= &SAMPLES( m ) [m->size + end_frame_extra] ); + + out [7] += delta * delta_unit - delta2; + out [8] += delta2; +} diff --git a/blip_buf/blip_buf.h b/blip_buf/blip_buf.h new file mode 100644 index 0000000000..52703643c4 --- /dev/null +++ b/blip_buf/blip_buf.h @@ -0,0 +1,72 @@ +/** \file +Sample buffer that resamples from input clock rate to output sample rate */ + +/* blip_buf 1.1.0 */ +#ifndef BLIP_BUF_H +#define BLIP_BUF_H + +#ifdef __cplusplus + extern "C" { +#endif + +/** First parameter of most functions is blip_t*, or const blip_t* if nothing +is changed. */ +typedef struct blip_t blip_t; + +/** Creates new buffer that can hold at most sample_count samples. Sets rates +so that there are blip_max_ratio clocks per sample. Returns pointer to new +buffer, or NULL if insufficient memory. */ +blip_t* blip_new( int sample_count ); + +/** Sets approximate input clock rate and output sample rate. For every +clock_rate input clocks, approximately sample_rate samples are generated. */ +void blip_set_rates( blip_t*, double clock_rate, double sample_rate ); + +enum { /** Maximum clock_rate/sample_rate ratio. For a given sample_rate, +clock_rate must not be greater than sample_rate*blip_max_ratio. */ +blip_max_ratio = 1 << 20 }; + +/** Clears entire buffer. Afterwards, blip_samples_avail() == 0. */ +void blip_clear( blip_t* ); + +/** Adds positive/negative delta into buffer at specified clock time. */ +void blip_add_delta( blip_t*, unsigned int clock_time, int delta ); + +/** Same as blip_add_delta(), but uses faster, lower-quality synthesis. */ +void blip_add_delta_fast( blip_t*, unsigned int clock_time, int delta ); + +/** Length of time frame, in clocks, needed to make sample_count additional +samples available. */ +int blip_clocks_needed( const blip_t*, int sample_count ); + +enum { /** Maximum number of samples that can be generated from one time frame. */ +blip_max_frame = 4000 }; + +/** Makes input clocks before clock_duration available for reading as output +samples. Also begins new time frame at clock_duration, so that clock time 0 in +the new time frame specifies the same clock as clock_duration in the old time +frame specified. Deltas can have been added slightly past clock_duration (up to +however many clocks there are in two output samples). */ +void blip_end_frame( blip_t*, unsigned int clock_duration ); + +/** Number of buffered samples available for reading. */ +int blip_samples_avail( const blip_t* ); + +/** Reads and removes at most 'count' samples and writes them to 'out'. If +'stereo' is true, writes output to every other element of 'out', allowing easy +interleaving of two buffers into a stereo sample stream. Outputs 16-bit signed +samples. Returns number of samples actually read. */ +int blip_read_samples( blip_t*, short out [], int count, int stereo ); + +/** Frees buffer. No effect if NULL is passed. */ +void blip_delete( blip_t* ); + + +/* Deprecated */ +typedef blip_t blip_buffer_t; + +#ifdef __cplusplus + } +#endif + +#endif diff --git a/blip_buf/blip_buf.txt b/blip_buf/blip_buf.txt new file mode 100644 index 0000000000..f7a33cc87b --- /dev/null +++ b/blip_buf/blip_buf.txt @@ -0,0 +1,250 @@ +blip_buf 1.1.0 +-------------- +Author : Shay Green +Website : http://www.slack.net/~ant/ +License : GNU Lesser General Public License (LGPL) + + +Contents +-------- +* Overview +* Buffer creation +* Waveform generation +* Time frames +* Complex waveforms +* Sample buffering +* Thanks + + +Overview +-------- +This library resamples audio waveforms from input clock rate to output +sample rate. Usage follows this general pattern: + +* Create buffer with blip_new(). +* Set clock rate and sample rate with blip_set_rates(). +* Waveform generation loop: + - Generate several clocks of waveform with blip_add_delta(). + - End time frame with blip_end_frame(). + - Read samples from buffer with blip_read_samples(). +* Free buffer with blip_delete(). + + +Buffer creation +--------------- +Before synthesis, a buffer must be created with blip_new(). Its size is +the maximum number of unread samples it can hold. For most uses, this +can be 1/10 the sample rate or less, since samples will usually be read +out immediately after being generated. + +After the buffer is created, the input clock rate and output sample rate +must be set with blip_set_rates(). This determines how many input clocks +there are per second, and how many output samples are generated per +second. + +If the compiler supports a 64-bit integer type, then the input-output +ratio is stored very accurately. If the compiler only supports a 32-bit +integer type, then the ratio is stored with only 20 fraction bits, so +some ratios cannot be represented exactly (for example, sample +rate=48000 and clock rate=48001). The ratio is internally rounded up, so +there will never be fewer than 'sample rate' samples per second. Having +too many per second is generally better than having too few. + + +Waveform generation +------------------- +Waveforms are generated at the input clock rate. Consider a simple +square wave with 8 clocks per cycle (4 clocks high, 4 clocks low): + + |<-- 8 clocks ->| + +5| ._._._._ ._._._._ ._._._._ ._._ + | | | | | | | | + Amp 0|._._._._ | | | | | | + | | | | | | | + -5| ._._._._ ._._._._ ._._._._ + * . . . * . . . * . . . * . . . * . . . * . . . * . . . * . + Time 0 4 8 12 16 20 24 28 + +The wave changes amplitude at time points 0, 4, 8, 12, 16, etc. + +The following generates the amplitude at every clock of above waveform +at the input clock rate: + + int wave [30]; + + for ( int i = 4; i < 30; ++i ) + { + if ( i % 8 < 4 ) + wave [i] = -5; + else + wave [i] = +5; + } + +Without this library, the wave array would then need to be resampled +from the input clock rate to the output sample rate. This library does +this resampling internally, so it won't be discussed further; waveform +generation code can focus entirely on the input clocks. + +Rather than specify the amplitude at every clock, this library merely +needs to know the points where the amplitude CHANGES, referred to as a +delta. The time of a delta is specified with a clock count. The deltas +for this square wave are shown below the time points they occur at: + + +5| ._._._._ ._._._._ ._._._._ ._._ + | | | | | | | | + Amp 0|._._._._ | | | | | | + | | | | | | | + -5| ._._._._ ._._._._ ._._._._ + * . . . * . . . * . . . * . . . * . . . * . . . * . . . * . + Time 0 4 8 12 16 20 24 28 + Delta +5 -10 +10 -10 +10 -10 +10 + +The following calls generate the above waveform: + + blip_add_delta( blip, 4, +5 ); + blip_add_delta( blip, 8, -10 ); + blip_add_delta( blip, 12, +10 ); + blip_add_delta( blip, 16, -10 ); + blip_add_delta( blip, 20, +10 ); + blip_add_delta( blip, 24, -10 ); + blip_add_delta( blip, 28, +10 ); + +In the examples above, the amplitudes are small for clarity. The 16-bit +sample range is -32768 to +32767, so actual waveform amplitudes would +need to be in the thousands to be audible (for example, -5000 to +5000). + +This library allows waveform generation code to pay NO attention to the +output sample rate. It can focus ENTIRELY on the essence of the +waveform: the points where its amplitude changes. Since these points can +be efficiently generated in a loop, synthesis is efficient. Sound chip +emulation code can be structured to allow full accuracy down to a single +clock, with the emulated CPU being able to simply tell the sound chip to +"emulate from wherever you left off, up to clock time T within the +current time frame". + + +Time frames +----------- +Since time keeps increasing, if left unchecked, at some point it would +overflow the range of an integer. This library's solution to the problem +is to break waveform generation into time frames of moderate length. +Clock counts within a time frame are thus relative to the beginning of +the frame, where 0 is the beginning of the frame. When a time frame of +length T is ended, what was at time T in the old time frame is now at +time 0 in the new time frame. Breaking the above waveform into time +frames of 10 clocks each looks like this: + + +5| ._._._._ ._._._._ ._._._._ ._._ + | | | | | | | | + Amp 0|._._._._ | | | | | | + | | | | | | | + -5| ._._._._ ._._._._ ._._._._ + * . . . * . . . * . . . * . . . * . . . * . . . * . . . * . + Time |0 4 8 | 2 6 |0 4 8 | + | first time frame | second time frame | third time frame | + |<--- 10 clocks --->|<--- 10 clocks --->|<--- 10 clocks --->| + +The following calls generate the above waveform. After they execute, the +first 30 clocks of the waveform will have been resampled and be +available as output samples for reading with blip_read_samples(). + + blip_add_delta( blip, 4, +5 ); + blip_add_delta( blip, 8, -10 ); + blip_end_frame( blip, 10 ); + + blip_add_delta( blip, 2, +10 ); + blip_add_delta( blip, 6, -10 ); + blip_end_frame( blip, 10 ); + + blip_add_delta( blip, 0, +10 ); + blip_add_delta( blip, 4, -10 ); + blip_add_delta( blip, 8, +10 ); + blip_end_frame( blip, 10 ); + ... + +Time frames can be a convenient length, and the length can vary from one +frame to the next. Once a time frame is ended, the resulting output +samples become available for reading immediately, and no more deltas can +be added to it. + +There is a limit of about 4000 output samples per time frame. The number +of clocks depends on the clock rate. At common sample rates, this allows +time frames of at least 1/15 second, plenty for most uses. This limit +allows increased resampling ratio accuracy. + +In an emulator, it is usually convenient to have audio time frames +correspond to video frames, where the CPU's clock counter is reset at +the beginning of each video frame and thus can be used directly as the +relative clock counts for audio time frames. + + +Complex waveforms +----------------- +Any sort of waveform can be generated, not just a square wave. For +example, a saw-like wave: + + +5| ._._._._ ._._._._ ._._ + | | | | | | + Amp 0|._._._._ | ._._._._ | ._._._._ + | | | | | + -5| ._._._._ ._._._._ + * . . . * . . . * . . . * . . . * . . . * . . . * . . . * . + Time 0 4 8 12 16 20 24 28 + Delta +5 -10 +5 +5 -10 +5 +5 + +Code to generate above waveform: + + blip_add_delta( blip, 4, +5 ); + blip_add_delta( blip, 8, -10 ); + blip_add_delta( blip, 12, +5 ); + blip_add_delta( blip, 16, +5 ); + blip_add_delta( blip, 20, +10 ); + blip_add_delta( blip, 24, +5 ); + blip_add_delta( blip, 28, +5 ); + +Similarly, multiple waveforms can be added within a time frame without +problem. It doesn't matter what order they're added, because all the +library needs are the deltas. The synthesis code doesn't need to know +all the waveforms at once either; it can calculate and add the deltas +for each waveform individually. Deltas don't need to be added in +chronological order either. + + +Sample buffering +---------------- +Sample buffering is very flexible. Once a time frame is ended, the +resampled waveforms become output samples that are immediately made +available for reading with blip_read_samples(). They don't have to be +read immediately; they can be allowed to accumulate in the buffer, with +each time frame appending more samples to the buffer. When reading, some +or all of the samples in can be read out, with the remaining unread +samples staying in the buffer for later. Usually a program will +immediately read all available samples after ending a time frame and +play them immediately. In some systems, a program needs samples in +fixed-length blocks; in that case, it would keep generating time frames +until some number of samples are available, then read only that many, +even if slightly more were available in the buffer. + +In some systems, one wants to run waveform generation for exactly the +number of clocks necessary to generate some desired number of output +samples, and no more. In that case, use blip_clocks_needed( blip, N ) to +find out how many clocks are needed to generate N additional samples. +Ending a time frame with this value will result in exactly N more +samples becoming available for reading. + + +Thanks +------ +Thanks to Jsr (FamiTracker author), the Mednafen team (multi-system +emulator), ShizZie (Nhes GMB author), Marcel van Tongeren, Luke Molnar +(UberNES author), Fredrick Meunier (Fuse contributor) for using and +giving feedback for another similar library. Thanks to Disch for his +interest and discussions about the synthesis algorithm itself, and for +writing his own implementation of it (Schpune) rather than just using +mine. Thanks to Xodnizel for Festalon, whose sound quality got me +interested in video game sound emulation in the first place, and where I +first came up with the algorithm while optimizing its brute-force +filter. + +-- +Shay Green diff --git a/blip_buf/changes.txt b/blip_buf/changes.txt new file mode 100644 index 0000000000..0c50861a9f --- /dev/null +++ b/blip_buf/changes.txt @@ -0,0 +1,39 @@ +Blip_buf Change Log +------------------- + +blip_buf 1.1.0 (2009-10-06) +-------------- +* Changed library name to blip_buf, to make it easier to find uses of on +the Web. + +* Increased internal input-output ratio accuracy greatly when compiler +supports 64-bit integer type. + +* Renamed blip_buffer_t to blip_t, for consistency with function names. +Old name is deprecated. + +* Reduced high-pass so that a bit more bass comes through. + +* Made header file minimally Doxygen-compatible. + +* Added unit test suite. + +* Added assertions to catch more precondition violations (negative +values, etc.) + +* blip_new() sets default rate to blip_max_ratio clocks per sample. + +* blip_delete() can now accept NULL, as originally claimed. + +* blip_clocks_needed() now properly returns zero when passed zero. + +* blip_add_delta_fast() now adds delta at same place blip_add_delta()'s +center of delta would be, rather than one sample later as before. + +* blip_add_delta() and blip_add_delta_fast() allow adding slightly past +buffer size, as originally claimed. + + +blip 1.0.0 (2008-12-08) +---------- +* First release diff --git a/blip_buf/license.txt b/blip_buf/license.txt new file mode 100644 index 0000000000..5faba9d48c --- /dev/null +++ b/blip_buf/license.txt @@ -0,0 +1,504 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library 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 + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! + + diff --git a/blip_buf/makefile_orig b/blip_buf/makefile_orig new file mode 100644 index 0000000000..dbf51fa2e1 --- /dev/null +++ b/blip_buf/makefile_orig @@ -0,0 +1,22 @@ +all: demo demo_chip demo_fixed demo_stereo demo.c demo_chip.c demo_fixed.c demo_stereo.c blip_buf.c + +demo: demo.c blip_buf.c + $(CC) -o demo demo.c blip_buf.c wave_writer.c + +demo_chip: demo_chip.c blip_buf.c + $(CC) -o demo_chip demo_chip.c blip_buf.c wave_writer.c + +demo_fixed: demo_fixed.c blip_buf.c + $(CC) -o demo_fixed demo_fixed.c blip_buf.c wave_writer.c + +demo_stereo: demo_stereo.c blip_buf.c + $(CC) -o demo_stereo demo_stereo.c blip_buf.c wave_writer.c + +demo_sdl: demo_sdl.c blip_buf.c + $(CC) -o demo_sdl $(shell sdl-config --cflags) $(shell sdl-config --libs) demo_sdl.c blip_buf.c + +test: blip_buf.c blip_buf.h + $(CXX) -o test -DBLARGG_TEST -I . -I tests/tester blip_buf.c tests/*.cpp tests/tester/*.cpp + +clean: + -$(RM) demo demo_chip demo_fixed demo_stereo demo_sdl diff --git a/blip_buf/readme.txt b/blip_buf/readme.txt new file mode 100644 index 0000000000..4709d3f053 --- /dev/null +++ b/blip_buf/readme.txt @@ -0,0 +1,62 @@ +blip_buf 1.1.0: Band-Limited Audio Buffer +----------------------------------------- +Blip_buf is a small waveform synthesis library meant for use in classic +video game sound chip emulation. It greatly simplifies sound chip +emulation code by handling all the details of resampling. The emulator +merely sets the input clock rate and output sample rate, adds waveforms +by specifying the clock times where their amplitude changes, then reads +the resulting output samples. For a more full-features synthesis +library, get Blip_Buffer. + +* Simple C interface and usage. +* Several code examples, including simple sound chip emulator. +* Uses fast, high-quality band-limited resampling algorithm (BLEP). +* Output is low-pass and high-pass filtered and clamped to 16-bit range. +* Supports mono, stereo, and multi-channel synthesis. + +Author : Shay Green +Website : http://www.slack.net/~ant/ +License : GNU Lesser General Public License (LGPL) +Language: C + + +Getting Started +--------------- +Build the demos by typing "make" at the command-line. If that doesn't +work, manually build a program from demo.c, blip_buf.c, and +wave_writer.c. Run "demo", which should record a square wave sweep to +"out.wav". The others demo advanced topics, and their source code can be +used as a starting point for experimentation (after making any changes, +just type "make" to do any necessary recompilation). + +To build the SDL demo, type "make demo_sdl" at the command-line, or +manually build a program from demo_sdl.c, blip_buf.c, and the SDL +multimedia library. + +See blip_buf.h for reference and blip_buf.txt for documentation. + + +Files +----- +readme.txt Essential information +blip_buf.txt Library documentation +license.txt GNU Lesser General Public License + +makefile Builds demos (type "make" at command-line) + +demo.c Generates square wave sweep +demo_stereo.c Generates stereo sound using two blip buffers +demo_fixed.c Works in fixed-point time rather than clocks +demo_sdl.c Plays sound live using SDL multimedia library +demo_chip.c Emulates sound hardware and plays back log.txt +demo_log.txt Log of sound hardware writes for demo_chip.c +wave_writer.h Simple wave sound file writer used by demos +wave_writer.c + +blip_buf.h Library header and reference (Doxygen-enabled) +blip_buf.c source code + +tests/ Unit tests (build with "make test") + +-- +Shay Green