我正在编写一个 C# 模拟器,并决定使用 CScore 输出 PCM。当样本大小(每个通道)为一个字节时,声音输出正确,但是当我将样本大小增加到 16 位时,声音非常嘈杂。
与该问题相关的一个问题是如何解释这 2 个字节(它们有符号吗?先高字节?)
这大致就是我正在做的事情:
首先,我生成这样的样本
public void GenerateSamples(int sampleCount)
{
while(sampleCount > 0)
{
--sampleCount;
for(int c = 0; c < _numChannels; ++c)
{
_buffer[_sampleIndex++] = _outputValue;
}
// The amount of ticks in a sample
_tickCounter -= APU.MinimumTickThreshold;
if(_tickCounter < 0)
{
_tickCounter = _tickThreshold;
_up = !_up;
// Replicating signed behaviour
_outputValue = (short)(_up ? 32767 : -32768);
}
}
}
这将生成一个简单的方波,其频率由_tickThreshold 确定。如果 _buffer 是字节数组,则声音是正确的。我想用短裤输出它,因为它使我能够使用签名样本并简单地添加多个通道以混合它们。
这就是我输出声音的方式。
for(int i = 0; i < sampleCount; ++i)
{
for(int c = 0; c < _numChannels; ++c)
{
short sample = _channel.Buffer[_channelSampleIndex++];
// Outputting the samples the other way around doesn't output
// sound for me
_buffer[_sampleIndex++] = (byte)sample;
_buffer[_sampleIndex++] = (byte)(sample >> 8);
}
}
我使用的 WaveFormat 是这样确定的:
_waveFormat = new WaveFormat(_apu.SampleRate, // 44000
_apu.SampleSize * 8, // 16
_apu.NumChannels); // 2
我很确定我遗漏了一些明显的东西,但我已经调试了一段时间,似乎并没有查明问题出在哪里。
谢谢