From 1fb05abc662bc7c1147ca27cf4c94065f22edc0c Mon Sep 17 00:00:00 2001 From: Washi Date: Sat, 16 Sep 2017 17:26:09 +0200 Subject: [PATCH] Sound channel 1 sfx improvements --- Emux.GameBoy/Audio/SquareChannel.cs | 19 +++++++++++-------- Emux.GameBoy/Audio/SquareSweepChannel.cs | 16 +++++++++++++--- Emux/Audio/GameBoyAudioMixer.cs | 2 +- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/Emux.GameBoy/Audio/SquareChannel.cs b/Emux.GameBoy/Audio/SquareChannel.cs index 523f57d..de528a6 100644 --- a/Emux.GameBoy/Audio/SquareChannel.cs +++ b/Emux.GameBoy/Audio/SquareChannel.cs @@ -24,7 +24,7 @@ namespace Emux.GameBoy.Audio _spu = spu; } - public byte NR0 + public virtual byte NR0 { get { return _nr0; } set { _nr0 = value; } @@ -167,22 +167,25 @@ namespace Emux.GameBoy.Audio const float maxAmplitude = 0.05f; - double realFrequency = 131072.0 / (2048.0 - Frequency) / 2; + double realFrequency = 131072.0 / (2048.0 - Frequency); int sampleRate = ChannelOutput.SampleRate; - double timeDelta = (cycles / GameBoyCpu.OfficialClockFrequency) / cpuSpeedFactor * 2; - int sampleCount = (int) (timeDelta * sampleRate); + double timeDelta = (cycles / GameBoyCpu.OfficialClockFrequency) / cpuSpeedFactor; + int sampleCount = (int)(timeDelta * sampleRate * 2); float[] buffer = new float[sampleCount]; if (!UseSoundLength || _length >= 0) { - for (int i = 0; i < buffer.Length; i++) + for (int i = 0; i < buffer.Length; i += 2) { - buffer[i] = (float) (maxAmplitude * (_volume / 15.0) // Volume adjustments. - * Math.Sign(Math.Sin(2 * Math.PI * realFrequency * _coordinate / sampleRate))); // Square wave formula + float sample = (float)(maxAmplitude * (_volume / 15.0) // Volume adjustments. + * Math.Sign(Math.Sin(2 * Math.PI * realFrequency * _coordinate / sampleRate))); // Square wave formula + buffer[i] = sample; + if (i < buffer.Length - 1) + buffer[i + 1] = sample; _coordinate = (_coordinate + 1) % sampleRate; } - if(UseSoundLength) + if (UseSoundLength) _length -= timeDelta; } diff --git a/Emux.GameBoy/Audio/SquareSweepChannel.cs b/Emux.GameBoy/Audio/SquareSweepChannel.cs index 6fcbd5f..178f8be 100644 --- a/Emux.GameBoy/Audio/SquareSweepChannel.cs +++ b/Emux.GameBoy/Audio/SquareSweepChannel.cs @@ -14,9 +14,19 @@ namespace Emux.GameBoy.Audio _spu = spu; } + public override byte NR0 + { + get { return base.NR0; } + set + { + base.NR0 = value; + _frequencySweepClock = 0; + } + } + public float SweepFrequency { - get { return (NR0 >> 4) / 128f; } + get { return ((NR0 >> 4) & 7) / 128f; } } public bool SweepIncrease @@ -33,10 +43,10 @@ namespace Emux.GameBoy.Audio { if (SweepShiftCount > 0 && _spu.Device.Cpu.SpeedFactor > 0.5) { - double timeDelta = (cycles / GameBoyCpu.OfficialClockFrequency) / _spu.Device.Cpu.SpeedFactor * 2; + double timeDelta = (cycles / GameBoyCpu.OfficialClockFrequency) / _spu.Device.Cpu.SpeedFactor; _frequencySweepClock += timeDelta; - double sweepInterval = 1.0 / SweepFrequency; + double sweepInterval = SweepFrequency; while (_frequencySweepClock >= sweepInterval) { _frequencySweepClock -= sweepInterval; diff --git a/Emux/Audio/GameBoyAudioMixer.cs b/Emux/Audio/GameBoyAudioMixer.cs index 5595729..fd11e32 100644 --- a/Emux/Audio/GameBoyAudioMixer.cs +++ b/Emux/Audio/GameBoyAudioMixer.cs @@ -105,7 +105,7 @@ namespace Emux.Audio { lock (this) { - if (IsRecording) + if (!IsRecording) throw new InvalidOperationException("Cannot stop a recording when a recording is not happening."); try {