From 04d4880564ddf2f8545af10871404b7858eab8a6 Mon Sep 17 00:00:00 2001 From: nattthebear Date: Fri, 16 Jun 2017 08:19:03 -0400 Subject: [PATCH] pizza: cover your ears --- .../Consoles/Nintendo/Gameboy/LibPizza.cs | 2 + .../Consoles/Nintendo/Gameboy/Pizza.cs | 39 +- waterbox/pizza/blip_buf/blip_buf.c | 344 +++++++++++++++ waterbox/pizza/blip_buf/blip_buf.h | 72 +++ waterbox/pizza/lib/cycles.c | 4 - waterbox/pizza/lib/gameboy.c | 3 - waterbox/pizza/lib/sound.c | 410 ++++-------------- waterbox/pizza/lib/sound.h | 30 -- waterbox/pizza/lib/sound_output.c | 37 ++ waterbox/pizza/lib/sound_output.h | 9 + waterbox/pizza/pizza.c | 9 +- 11 files changed, 592 insertions(+), 367 deletions(-) create mode 100644 waterbox/pizza/blip_buf/blip_buf.c create mode 100644 waterbox/pizza/blip_buf/blip_buf.h create mode 100644 waterbox/pizza/lib/sound_output.c create mode 100644 waterbox/pizza/lib/sound_output.h diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/LibPizza.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/LibPizza.cs index 25ed884cf8..5c2776fa44 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/LibPizza.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/LibPizza.cs @@ -27,7 +27,9 @@ namespace BizHawk.Emulation.Cores.Consoles.Nintendo.Gameboy public class FrameInfo { public IntPtr VideoBuffer; + public IntPtr SoundBuffer; public int Clocks; + public int Samples; public Buttons Keys; } diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Pizza.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Pizza.cs index 85b132b3b8..ff6b74b849 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Pizza.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Pizza.cs @@ -12,7 +12,7 @@ namespace BizHawk.Emulation.Cores.Consoles.Nintendo.Gameboy { [CoreAttributes("Pizza Boy", "Davide Berra", true, false, "c7bc6ee376028b3766de8d7a02e60ab794841f45", "https://github.com/davideberra/emu-pizza/", false)] - public class Pizza : IEmulator, IInputPollable, IVideoProvider, IGameboyCommon + public class Pizza : IEmulator, IInputPollable, IVideoProvider, IGameboyCommon, ISoundProvider { private LibPizza _pizza; private PeRunner _exe; @@ -83,18 +83,21 @@ namespace BizHawk.Emulation.Cores.Consoles.Nintendo.Gameboy public unsafe void FrameAdvance(IController controller, bool render, bool rendersound = true) { fixed (int* vp = _videoBuffer) + fixed (short* sp = _soundBuffer) { var targetClocks = TICKSPERFRAME - _tickOverflow; var frame = new LibPizza.FrameInfo { VideoBuffer = (IntPtr)vp, + SoundBuffer = (IntPtr)sp, Clocks = targetClocks, Keys = GetButtons(controller) }; _pizza.FrameAdvance(frame); _tickOverflow = frame.Clocks - targetClocks; + _numSamples = frame.Samples; Frame++; } } @@ -127,6 +130,40 @@ namespace BizHawk.Emulation.Cores.Consoles.Nintendo.Gameboy public bool DeterministicEmulation { get; private set; } public CoreComm CoreComm { get; } + #region ISoundProvider + + private short[] _soundBuffer = new short[2048]; + private int _numSamples; + + public void SetSyncMode(SyncSoundMode mode) + { + if (mode == SyncSoundMode.Async) + { + throw new NotSupportedException("Async mode is not supported."); + } + } + + public void GetSamplesSync(out short[] samples, out int nsamp) + { + samples = _soundBuffer; + nsamp = _numSamples; + } + + public void GetSamplesAsync(short[] samples) + { + throw new InvalidOperationException("Async mode is not supported."); + } + + public void DiscardSamples() + { + } + + public bool CanProvideAsync => false; + + public SyncSoundMode SyncMode => SyncSoundMode.Sync; + + #endregion + #region IVideoProvider private int[] _videoBuffer = new int[160 * 144]; diff --git a/waterbox/pizza/blip_buf/blip_buf.c b/waterbox/pizza/blip_buf/blip_buf.c new file mode 100644 index 0000000000..1bd3377e92 --- /dev/null +++ b/waterbox/pizza/blip_buf/blip_buf.c @@ -0,0 +1,344 @@ +/* blip_buf 1.1.0. http://www.slack.net/~ant/ */ + +#include "blip_buf.h" + +#include +#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/waterbox/pizza/blip_buf/blip_buf.h b/waterbox/pizza/blip_buf/blip_buf.h new file mode 100644 index 0000000000..e9a5d4cc3b --- /dev/null +++ b/waterbox/pizza/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/waterbox/pizza/lib/cycles.c b/waterbox/pizza/lib/cycles.c index dd3b31271a..19da4ba499 100644 --- a/waterbox/pizza/lib/cycles.c +++ b/waterbox/pizza/lib/cycles.c @@ -222,10 +222,6 @@ void cycles_step() /* channel four */ if (sound.channel_four.cycles_next == cycles.cnt) sound_step_ch4(); - - /* time to generate a sample? */ - if (sound.sample_cycles_next_rounded == cycles.cnt) - sound_step_sample(); /* update timer state */ if (cycles.cnt == timer.next) diff --git a/waterbox/pizza/lib/gameboy.c b/waterbox/pizza/lib/gameboy.c index 048ace5ed7..6fd8fc7977 100644 --- a/waterbox/pizza/lib/gameboy.c +++ b/waterbox/pizza/lib/gameboy.c @@ -39,9 +39,6 @@ char gameboy_inited = 0; void gameboy_init() { - /* init global values */ - // global_init(); - /* init z80 */ z80_init(); diff --git a/waterbox/pizza/lib/sound.c b/waterbox/pizza/lib/sound.c index c6d09a3e79..9573ad1d04 100644 --- a/waterbox/pizza/lib/sound.c +++ b/waterbox/pizza/lib/sound.c @@ -23,6 +23,7 @@ #include "mmu.h" #include "sound.h" #include "utils.h" +#include "sound_output.h" #include #include @@ -31,26 +32,87 @@ /* super variable for audio controller */ sound_t sound; -/* global for output frequency */ -int sound_output_rate = 48000; -int sound_output_rate_fifth = 48; // 48000 / 5; +// functions to connect to blip buf +static int16_t last_sample[8]; + +#define BLIP_LEFT(v,i) if(1){int32_t d = (v)-last_sample[(i)*2];blip_left(d);last_sample[(i)*2] = (v);} +#define BLIP_RIGHT(v,i) if(1){int32_t d = (v)-last_sample[(i)*2+1];blip_right(d);last_sample[(i)*2+1] = (v);} + +static void blip_ch1() +{ + if (sound.channel_one.active) + { + if (sound.nr51->ch1_to_so1) + BLIP_RIGHT(sound.channel_one.sample, 0); + if (sound.nr51->ch1_to_so2) + BLIP_LEFT(sound.channel_one.sample, 0); + } +} +static void blip_ch2() +{ + if (sound.channel_two.active) + { + if (sound.nr51->ch2_to_so1) + BLIP_RIGHT(sound.channel_two.sample, 1); + if (sound.nr51->ch2_to_so2) + BLIP_LEFT(sound.channel_two.sample, 1); + } +} +static void blip_ch3() +{ + if (sound.channel_three.active) + { + uint16_t sample; + uint8_t shift = (sound.nr32->volume_code == 0 ? + 4 : sound.nr32->volume_code - 1); + + /* volume is zero in any case */ + if (shift == 4) + sample = 0; + else + { + /* apply volume change */ + uint8_t idx = sound.channel_three.index; + uint16_t s; + + /* extract current sample */ + if ((idx & 0x01) == 0) + s = (sound.wave_table[idx >> 1] & 0xf0) >> 4; + else + s = sound.wave_table[idx >> 1] & 0x0f; + + /* transform it into signed 16 bit sample */ + sample = ((s * 0x222) >> shift); + } + + + /* not silence? */ + if (sample != 0) + { + if (sound.nr51->ch3_to_so1) + BLIP_RIGHT(sample, 2); + if (sound.nr51->ch3_to_so2) + BLIP_LEFT(sample, 2); + } + } +} +static void blip_ch4() +{ + if (sound.channel_four.active) + { + if (sound.nr51->ch4_to_so1) + BLIP_RIGHT(sound.channel_four.sample, 3); + if (sound.nr51->ch4_to_so2) + BLIP_LEFT(sound.channel_four.sample, 3); + } +} /* internal prototypes */ -size_t sound_available_samples(); void sound_envelope_step(); void sound_length_ctrl_step(); -void sound_push_samples(int16_t l, int16_t r); -void sound_push_sample(int16_t s); -void sound_read_samples(int len, int16_t *buf); -void sound_rebuild_wave(); void sound_sweep_step(); void sound_write_wave(uint16_t a, uint8_t v); -int sound_get_samples() -{ - return SOUND_SAMPLES; // sound_output_rate / 10; -} - void sound_init_pointers() { /* point sound structures to their memory areas */ @@ -92,46 +154,15 @@ void sound_init() /* point sound structures to their memory areas */ sound_init_pointers(); - /* steps length */ - sound.step_int = 4; - sound.step_int1000 = 4000; - - /* buffer stuff */ - sound.buf_wr = 0; - sound.buf_rd = 0; - sound.buf_available = 0; - /* how many cpu cycles we need to emit a 512hz clock (frame sequencer) */ sound.fs_cycles = 4194304 / 512; /* how many cpu cycles to generate a single frame seq clock? */ sound.fs_cycles_next = sound.fs_cycles; - - /* how many cpu cycles to generate a sample */ - sound.sample_cycles = (uint_fast32_t) (((double) 4194304 / - (double) sound_output_rate) * 1000); - - sound.sample_cycles_next = sound.sample_cycles / 1000; - sound.sample_cycles_next_rounded = sound.sample_cycles_next & 0xFFFFFFFC; - - /* no, i'm not empty */ - sound.buf_empty = 0; } void sound_set_speed(char dbl) { - return; - - if (dbl) - { - sound.step_int = 2; - sound.step_int1000 = 2000; - } - else - { - sound.step_int = 4; - sound.step_int1000 = 4000; - } } /* update sound internal state given CPU T-states */ @@ -155,6 +186,10 @@ void sound_step_fs() /* envelope works at 64hz */ if (sound.fs_cycles_idx == 7) sound_envelope_step(); + blip_ch1(); + blip_ch2(); + blip_ch3(); + blip_ch4(); } /* update all channels */ @@ -172,6 +207,7 @@ void sound_step_ch1() /* go back */ sound.channel_one.duty_cycles_next += sound.channel_one.duty_cycles; + blip_ch1(); } void sound_step_ch2() @@ -188,6 +224,7 @@ void sound_step_ch2() /* go back */ sound.channel_two.duty_cycles_next += sound.channel_two.duty_cycles; + blip_ch2(); } void sound_step_ch3() @@ -206,6 +243,7 @@ void sound_step_ch3() /* qty of cpu ticks needed for a wave sample change */ sound.channel_three.cycles = ((2048 - freq) * 2) << global_cpu_double_speed; sound.channel_three.cycles_next += sound.channel_three.cycles; + blip_ch3(); } void sound_step_ch4() @@ -236,109 +274,8 @@ void sound_step_ch4() sound.channel_four.sample = sound.channel_four.volume; /* qty of cpu ticks needed for a wave sample change */ - sound.channel_four.cycles_next += sound.channel_four.period_lfsr; -} - -void sound_step_sample() -{ - uint_fast32_t zum = sound.sample_cycles + sound.sample_cycles_remainder; - - sound.sample_cycles_next += ((zum / 1000) << global_cpu_double_speed); - sound.sample_cycles_next_rounded = - sound.sample_cycles_next & 0xFFFFFFFC; - sound.sample_cycles_remainder = zum % 1000; - - /* update output frame counter */ - sound.frame_counter++; - - /* DAC turned off? */ - if (sound.nr30->dac == 0 && - sound.channel_one.active == 0 && - sound.channel_two.active == 0 && - sound.channel_four.active == 0) - { - sound_push_samples(0, 0); - return; - } - - int16_t sample_left = 0; - int16_t sample_right = 0; - int16_t sample = 0; - - /* time to generate a sample! sum all the fields */ - if (sound.channel_one.active) // && sound.channel_one.sample) - { - /* to the right? */ - if (sound.nr51->ch1_to_so1) - sample_right += sound.channel_one.sample; - - /* to the left? */ - if (sound.nr51->ch1_to_so2) - sample_left += sound.channel_one.sample; - } - - if (sound.channel_two.active) // && sound.channel_two.sample) - { - /* to the right? */ - if (sound.nr51->ch2_to_so1) - sample_right += sound.channel_two.sample; - - /* to the left? */ - if (sound.nr51->ch2_to_so2) - sample_left += sound.channel_two.sample; - } - - if (sound.channel_three.active) - { - uint8_t shift = (sound.nr32->volume_code == 0 ? - 4 : sound.nr32->volume_code - 1); - - /* volume is zero in any case */ - if (shift == 4) - sample = 0; - else - { - /* apply volume change */ - uint8_t idx = sound.channel_three.index; - uint16_t s; - - /* extract current sample */ - if ((idx & 0x01) == 0) - s = (sound.wave_table[idx >> 1] & 0xf0) >> 4; - else - s = sound.wave_table[idx >> 1] & 0x0f; - - /* transform it into signed 16 bit sample */ - sample = ((s * 0x222) >> shift); - } - - - /* not silence? */ - if (sample != 0) - { - /* to the right? */ - if (sound.nr51->ch3_to_so1) - sample_right += sample; - - /* to the left? */ - if (sound.nr51->ch3_to_so2) - sample_left += sample; - } - } - - if (sound.channel_four.active) - { - /* to the right? */ - if (sound.nr51->ch4_to_so1) - sample_right += sound.channel_four.sample; - - /* to the left? */ - if (sound.nr51->ch4_to_so2) - sample_left += sound.channel_four.sample; - } - - /* push the sum of all channels samples */ - sound_push_samples(sample_left, sample_right); + sound.channel_four.cycles_next += sound.channel_four.period_lfsr; + blip_ch4(); } /* update length of channel1 */ @@ -377,116 +314,6 @@ void sound_length_ctrl_step() &sound.channel_four.active); } -void sound_read_buffer(void *userdata, uint8_t *stream, int snd_len) -{ - /* requester snd_len is expressed in byte, */ - /* so divided by to to obtain wanted 16 bits samples */ - sound_read_samples(snd_len / 2, (int16_t *) stream); -} - -void sound_push_samples(int16_t l, int16_t r) -{ - /* store them in tmp buffer */ - sound.buf_tmp[sound.buf_tmp_wr++] = l; - sound.buf_tmp[sound.buf_tmp_wr++] = r; - - if (sound.buf_tmp_wr == SOUND_BUF_TMP_SZ) - { - unsigned int i; - - /* put them in circular shared buffer */ - for (i=0; i sound.buf_wr) - return sound.buf_wr + SOUND_BUF_SZ - sound.buf_rd; - - return sound.buf_wr - sound.buf_rd; -} - -/* read a block of data from circular buffer */ -void sound_read_samples(int to_read, int16_t *buf) -{ - /* not enough samples? read what we got */ - if (sound.buf_available < to_read) - { - /* stop until we got enough samples */ - sound.buf_empty = 1; - - //while (sound.buf_empty && !global_quit) - //pthread_cond_wait(&sound_cond, &sound_mutex); - } - - if (sound.buf_rd + to_read >= SOUND_BUF_SZ) - { - /* overlaps the end of the buffer? copy in 2 phases */ - size_t first_block = SOUND_BUF_SZ - sound.buf_rd; - - memcpy(buf, &sound.buf[sound.buf_rd], first_block * 2); - - memcpy(&buf[first_block], sound.buf, (to_read - first_block) * 2); - - /* set the new read index */ - sound.buf_rd = to_read - first_block; - } - else - { - /* a single memcpy is enough */ - memcpy(buf, &sound.buf[sound.buf_rd], to_read * 2); - - /* update read index */ - sound.buf_rd += to_read; - } - - /* update avaiable samples */ - sound.buf_available -= to_read; -} - /* calc the new frequency by sweep module */ uint_fast32_t sound_sweep_calc() { @@ -745,21 +572,6 @@ uint8_t sound_read_reg(uint16_t a, uint8_t v) } } -void sound_set_output_rate(int freq) -{ - sound_output_rate = freq; - sound_output_rate_fifth = freq / 5; - - double cpu_base_freq = 4194304; - - /* calc cycles needed to generate a sample */ - sound.sample_cycles = (uint_fast32_t) (((double) cpu_base_freq / - (double) sound_output_rate) * 1000); - - sound.sample_cycles_next = sound.sample_cycles / 1000; - sound.sample_cycles_next_rounded = sound.sample_cycles_next & 0xFFFFFFFC; -} - void sound_write_reg(uint16_t a, uint8_t v) { /* when turned off, only write to NR52 (0xFF26) is legit */ @@ -1351,57 +1163,3 @@ void sound_write_wave(uint16_t a, uint8_t v) sound.wave_table[a - 0xFF30] = v; } - -void sound_rebuild_wave() -{ - int sample; - uint8_t shift = (sound.nr32->volume_code == 0 ? - 4 : sound.nr32->volume_code - 1); - int i; - - int min = 0; - int max = 0; - - /* fill wave buffer */ - for (i=0; i<16; i++) - { - /* read higher nibble */ - sample = (sound.wave_table[i] & 0xf0) >> 4; - - /* apply volume change */ - sample >>= shift; - - /* convert to signed int 16 */ - sample = (sample * 0x1111) - 0x8000; - - if (sample < min) - min = sample; - if (sample > max) - max = sample; - - /* save it into rendered wave table */ - sound.channel_three.wave[i * 2] = (int16_t) sample; - - /* do the same with lowest nibble */ - sample = (sound.wave_table[i] & 0x0f); - - /* apply volume change */ - sample >>= shift; - - /* convert to signed int 16 */ - sample = (sample * 0x1111) - 0x8000; - - /* save it into rendered wave table */ - sound.channel_three.wave[(i * 2) + 1] = (int16_t) sample; - - if (sample < min) - min = sample; - if (sample > max) - max = sample; - } - - /* set the new current sample */ - sound.channel_three.sample = - sound.channel_three.wave[sound.channel_three.index]; - -} diff --git a/waterbox/pizza/lib/sound.h b/waterbox/pizza/lib/sound.h index 27a4887107..226b8650bd 100644 --- a/waterbox/pizza/lib/sound.h +++ b/waterbox/pizza/lib/sound.h @@ -20,10 +20,7 @@ #ifndef __SOUND_HDR__ #define __SOUND_HDR__ -#define SOUND_FREQ_MAX 48000 #define SOUND_SAMPLES 4096 -#define SOUND_BUF_SZ (SOUND_SAMPLES * 3) -#define SOUND_BUF_TMP_SZ (SOUND_SAMPLES / 2) typedef struct nr10_s { @@ -278,16 +275,6 @@ typedef struct sound_s /* emulation speed stuff */ uint_fast16_t frame_counter; - /* circular audio buffer stuff */ - uint_fast16_t buf_rd; - uint_fast16_t buf_wr; - uint_fast16_t buf_available; - uint_fast16_t buf_empty; - uint_fast16_t buf_full; - int16_t buf[SOUND_BUF_SZ]; - int16_t buf_tmp[SOUND_BUF_TMP_SZ]; - uint_fast16_t buf_tmp_wr; - /* output rate */ uint_fast32_t output_rate; @@ -295,36 +282,19 @@ typedef struct sound_s uint_fast32_t fs_cycles; uint_fast32_t fs_cycles_idx; uint64_t fs_cycles_next; - uint_fast32_t sample_cycles; - uint_fast32_t sample_cycles_remainder; - uint_fast32_t sample_cycles_next; - uint64_t sample_cycles_next_rounded; - - /* steps length */ - uint_fast32_t step_int; - uint_fast32_t step_int1000; - - uint_fast32_t spare; - uint_fast32_t spare2; - } sound_t; extern sound_t sound; /* prototypes */ -void sound_change_emulation_speed(); -int sound_get_samples(); void sound_init(); -void sound_read_buffer(void *userdata, uint8_t *stream, int snd_len); uint8_t sound_read_reg(uint16_t a, uint8_t v); void sound_set_speed(char dbl); -void sound_set_output_rate(int freq); void sound_step_fs(); void sound_step_ch1(); void sound_step_ch2(); void sound_step_ch3(); void sound_step_ch4(); -void sound_step_sample(); void sound_write_reg(uint16_t a, uint8_t v); #endif diff --git a/waterbox/pizza/lib/sound_output.c b/waterbox/pizza/lib/sound_output.c new file mode 100644 index 0000000000..412dbb2143 --- /dev/null +++ b/waterbox/pizza/lib/sound_output.c @@ -0,0 +1,37 @@ +#include "../blip_buf/blip_buf.h" +#include "sound_output.h" +#include "cycles.h" + +static blip_t* lb; +static blip_t* rb; +static uint64_t startclock; + +#define RELATIVECLOCK (cycles.sampleclock - startclock) + +void blip_left(int delta) +{ + if (delta) + blip_add_delta(lb, RELATIVECLOCK, delta); +} +void blip_right(int delta) +{ + if (delta) + blip_add_delta(rb, RELATIVECLOCK, delta); +} + +void sound_output_init(double clock_rate, double sample_rate) +{ + lb = blip_new(1024); + rb = blip_new(1024); + blip_set_rates(lb, clock_rate, sample_rate); + blip_set_rates(rb, clock_rate, sample_rate); +} +int sound_output_read(int16_t* output) +{ + blip_end_frame(lb, RELATIVECLOCK); + blip_end_frame(rb, RELATIVECLOCK); + startclock = cycles.sampleclock; + int ret = blip_read_samples(lb, output, 2048, 1); + blip_read_samples(rb, output + 1, 2048, 1); + return ret; +} diff --git a/waterbox/pizza/lib/sound_output.h b/waterbox/pizza/lib/sound_output.h new file mode 100644 index 0000000000..91561da49a --- /dev/null +++ b/waterbox/pizza/lib/sound_output.h @@ -0,0 +1,9 @@ +#pragma once + +#include + +void blip_left(int delta); +void blip_right(int delta); + +void sound_output_init(double clock_rate, double sample_rate); +int sound_output_read(int16_t* output); diff --git a/waterbox/pizza/pizza.c b/waterbox/pizza/pizza.c index 066e9d2ea0..007f423d53 100644 --- a/waterbox/pizza/pizza.c +++ b/waterbox/pizza/pizza.c @@ -35,6 +35,7 @@ #include "serial.h" #include "utils.h" #include "mmu.h" +#include "sound_output.h" /* proto */ void cb(); @@ -74,15 +75,14 @@ EXPORT int Init(const void *rom, int romlen) /* init GPU */ gpu_init(&cb); - /* set sound output rate */ - sound_set_output_rate(44100); - /* set rumble cb */ mmu_set_rumble_cb(&rumble_cb); /* get frame buffer reference */ fb = gpu_get_frame_buffer(); + sound_output_init(2 * 1024 * 1024, 44100); + return 1; } @@ -91,7 +91,9 @@ static uint32_t fb32[160 * 144]; typedef struct { uint32_t* vbuff; + int16_t* sbuff; int32_t clocks; // desired(in) actual(out) time to run; 2MHZ + int32_t samples; // actual number of samples produced uint16_t keys; // keypad input } frameinfo_t; @@ -103,6 +105,7 @@ EXPORT void FrameAdvance(frameinfo_t* frame) gameboy_run(current + frame->clocks); frame->clocks = cycles.sampleclock - current; memcpy(frame->vbuff, fb32, 160 * 144 * sizeof(uint32_t)); + frame->samples = sound_output_read(frame->sbuff); } EXPORT int IsCGB(void)