【问题标题】:CScore output PCM bigger than one byteCScore 输出 PCM 大于一字节
【发布时间】:2016-03-06 17:35:07
【问题描述】:

我正在编写一个 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

我很确定我遗漏了一些明显的东西,但我已经调试了一段时间,似乎并没有确定问题出在哪里。

谢谢

【问题讨论】:

    标签: c# audio pcm gameboy cscore


    【解决方案1】:

    这里的耻辱。 问题是我没有考虑到现在我需要生成一半的样本量(CScore 要求的是字节数,而不是样本量)。 在我的示例中,我必须将 sampleCount 变量除以 sampleSize 以生成正确的声音量。

    噪音的出现是因为我没有将额外的样本与来自 CScore 的下一个读取调用同步(我正在动态生成声音,而不是预先缓冲它。这样我就不会因为额外的样本而引入延迟)。

    我发现了这个问题:SampleToPcm16.cs

    【讨论】:

    • 感谢分享。您可以将自己的答案标记为正确答案 ;-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多