public void ChannelStep(int cycles) { double cpuSpeedFactor = _spu.Device.Cpu.SpeedFactor; if (!Active || !SoundEnabled || double.IsNaN(cpuSpeedFactor) || double.IsInfinity(cpuSpeedFactor) || cpuSpeedFactor < 0.5) { return; } int sampleRate = ChannelOutput.SampleRate; double timeDelta = (cycles / GameBoyCpu.OfficialClockFrequency) / cpuSpeedFactor; int sampleCount = (int)(timeDelta * sampleRate) * 2; float[] buffer = new float[sampleCount]; double interval = 1.0 / Frequency; int intervalSampleCount = (int)(interval * sampleRate); for (int i = 0; i < buffer.Length; i += 2) { _coordinate++; if (_coordinate >= intervalSampleCount) { _top = !_top; _coordinate = 0; } int waveRamCoordinate = (int)(_coordinate / (double)intervalSampleCount * _waveRam.Length); int waveDataSample = _top ? (_waveRam[waveRamCoordinate] & 0xF) : ((_waveRam[waveRamCoordinate] >> 4) & 0xF); float sample = ChannelVolume * OutputLevel * (waveDataSample - 7) / 15f; _spu.WriteToSoundBuffer(ChannelNumber, buffer, i, sample); } ChannelOutput.BufferSoundSamples(buffer, 0, buffer.Length); }
public void ChannelStep(int cycles) { double cpuSpeedFactor = _spu.Device.Cpu.SpeedFactor; if (!Active || double.IsNaN(cpuSpeedFactor) || double.IsInfinity(cpuSpeedFactor) || cpuSpeedFactor < 0.5) { return; } UpdateVolume(cycles); double ratio = DividingRatio == 0 ? 0.5 : DividingRatio; double frequency = 524288 / ratio / Math.Pow(2, ShiftClockFrequency + 1) * 2; int sampleRate = ChannelOutput.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 += 2) { float sample = (float)(ChannelVolume * (_volume / 15.0) * _currentValue); _spu.WriteToSoundBuffer(ChannelNumber, buffer, i, sample); _coordinate += timeDelta; if (_coordinate >= (1 / frequency) * 2) { _coordinate -= (1 / frequency) * 2; _currentValue = _random.NextDouble(); } } if (UseSoundLength) { _length -= timeDelta; } } ChannelOutput.BufferSoundSamples(buffer, 0, buffer.Length); }
public virtual void ChannelStep(int cycles) { double cpuSpeedFactor = _spu.Device.Cpu.SpeedFactor; if (!Active || double.IsNaN(cpuSpeedFactor) || double.IsInfinity(cpuSpeedFactor) || cpuSpeedFactor < 0.5) { return; } UpdateVolume(cycles); double realFrequency = 131072.0 / (2048.0 - Frequency); int sampleRate = ChannelOutput.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 += 2) { float sample = (float)(ChannelVolume * (_volume / 15.0) // Volume adjustments. * Math.Sign(Math.Sin(2 * Math.PI * realFrequency * _coordinate / sampleRate))); // Square wave formula _spu.WriteToSoundBuffer(ChannelNumber, buffer, i, sample); _coordinate = (_coordinate + 1) % sampleRate; } if (UseSoundLength) { _length -= timeDelta; } } ChannelOutput.BufferSoundSamples(buffer, 0, buffer.Length); }