如何从原始的16位,44100hz,立体声LPCM获得正确的声音(立体声)

时间:2012-07-13 02:12:49

标签: audio spotify naudio

我正在使用libspotify检索音乐以播放某些音频库。 spotify音乐应该是原始的16bit,44100hz,立体声LPCM。我一直试图和NAudio一起演奏音乐,但不幸的是它不是立体声音响。

来自spotify docs:样本以整数形式传递,请参阅sp_audioformat。一帧包含与通道相同数量的样本。即交错是在样本层面。

以下代码从文件中播放单声道的歌曲。该文件是spotify音乐数据的副本。

有人可以引导我走向立体声解决方案。它可能是任何音频库,最好是在.NET中。

using (var waveOutDevice = new WaveOut())
{

    using (var pcmStream = new FileStream(PcmFile, FileMode.Open))
    {
        WaveStream waveStream = null;
        try
        {
            const int sampleRate = 44100;
            const int channels = 2;
            var waveFormat = WaveFormat.CreateCustomFormat(WaveFormatEncoding.Pcm, sampleRate * channels, 1, sampleRate*2*channels, channels, 16);
            waveStream = new RawSourceWaveStream(pcmStream, waveFormat);

            waveOutDevice.Init(waveStream);
            waveOutDevice.Play();

            Thread.Sleep(5000); //Listen to 5 secs of music

        }
        finally
        {
            waveOutDevice.Stop();
            if (waveStream != null) waveStream.Close();
        }
    }
}

CreateCustomFormat的Signatur是 public static NAudio.Wave.WaveFormat CreateCustomFormat(NAudio.Wave.WaveFormatEncoding tag,int sampleRate,int channels,int averageBytesPerSecond,int blockAlign,int bitsPerSample)

2 个答案:

答案 0 :(得分:1)

这是立体声:

const int bitsPerSample = 16;
int blockAlign = (channels * (bitsPerSample / 8));
int averageBytesPerSecond = sampleRate * blockAlign;
var waveFormat = WaveFormat.CreateCustomFormat(WaveFormatEncoding.Pcm, sampleRate, channels, averageBytesPerSecond, blockAlign, bitsPerSample);

答案 1 :(得分:0)

也许您将错误的值传递给 CreateCustomFormat 。请尝试以下方法:

WaveFormat.CreateCustomFormat(WaveFormatEncoding.Pcm, sampleRate, channels, sampleRate*channels, 1, 8);