Sound channel 1 sfx improvements

This commit is contained in:
Washi 2017-09-16 17:26:09 +02:00
parent f5934dc978
commit 1fb05abc66
3 changed files with 25 additions and 12 deletions

View File

@ -24,7 +24,7 @@ namespace Emux.GameBoy.Audio
_spu = spu;
}
public byte NR0
public virtual byte NR0
{
get { return _nr0; }
set { _nr0 = value; }
@ -167,18 +167,21 @@ 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.
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;
}

View File

@ -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;

View File

@ -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
{