音频文件播放速度错误

Audiofiles are played with wrong speed

我在 C# 应用程序中使用 NAudio,我的问题是播放速度取决于采样率。我使用了两个包含 Fs = 44100Hz 和 Fs = 88200 Hz 的 1000Hz 正弦波的 Wav 文件。我用示波器检查了声卡的输出信号。事实证明,我的 Fs = 44100 文件在预期时间的一半后结束,而输出频率加倍 (2kHz)。在其他播放器(例如 windows 媒体播放器、audacity)中使用这些文件时,一切看起来都很好。调试时,我查看了这些文件的波形,一切看起来都很好。我还改变了位分辨率,结果没有区别。我不确定我是不是错过了什么。

我在这个问题首次出现的另一个程序中使用了它。因此,我做了一个基本的小程序,看看我是否已经在更大的应用程序中犯了错误,那里也存在同样的问题。

如果有人能帮助我,那将是非常棒的,非常感谢。 亲切的问候 狮子座

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NAudio.Wave;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        AsioOut asioOut;
        AudioFileReader afr4;
        AudioFileReader afr8;
        AudioFileReader afr0;
        MixingWaveProvider32 mwp;

        int playingID = 0;
        int channelID;
        Boolean audioplaying = false;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            asioOut = new AsioOut();
            afr4 = new AudioFileReader("../wavs/sin1000Hz_1.75s_16bit_44100Hz_ramp50ms.wav");
            afr8 = new AudioFileReader("../wavs/sin1000Hz_1.75s_16bit_88200Hz_ramp50ms.wav");
            afr0 = new AudioFileReader("../wavs/sin14607Hz_180sec_20msRamp_fs96000_24bit_mono_doppelt.wav");
            mwp  = new MixingWaveProvider32();



            afr4.Volume = 1.0f;
            afr8.Volume = 1.0f;

            for (int i = 0; i < asioOut.DriverOutputChannelCount; i++) {
                Console.WriteLine(i + ": " + asioOut.AsioOutputChannelName(i));
                if (asioOut.AsioOutputChannelName(i).Equals("Analog 3 (1)")) {
                    channelID = i;
                }
            }
            asioOut.ChannelOffset = channelID;
            asioOut.Init(mwp);
            asioOut.PlaybackStopped += OnPlaybackStopped;
        }

        //a button to play the Fs = 44100Hz File
        private void button1_Click(object sender, EventArgs e)
        {
            if (audioplaying == false) {
                mwp.AddInputStream(afr4);
                afr4.Position = 0;

                audioplaying = true;
                playingID = 4;

                asioOut.Play();
            }           
        }

        //a button to play the Fs = 88200Hz File
        private void button2_Click(object sender, EventArgs e)
        {
            if (audioplaying == false) {
                mwp.AddInputStream(afr8);
                afr8.Position = 0;

                audioplaying = true;
                playingID = 8;

                asioOut.Play();
            }
        }

        //a button to play just another audiofile
        private void button4_Click(object sender, EventArgs e)
        {
            if (audioplaying == false) {
                mwp.AddInputStream(afr0);
                afr0.Position = 0;

                audioplaying = true;
                playingID = 0;

                asioOut.Play();
            }
        }

        //after playback every Input gets removed again to setup for new playback
        protected virtual void OnPlaybackStopped(object sender, EventArgs e) {            
            if (playingID == 4) {
                mwp.RemoveInputStream(afr4);
            } else if (playingID == 8) {
                mwp.RemoveInputStream(afr8);
            } else {
                mwp.RemoveInputStream(afr0);
            }
            audioplaying = false;

        }

        private void button3_Click(object sender, EventArgs e)
        {
            asioOut.Stop();
        }       
    }
}

您不能使用 MixingWaveProvider32 将具有不同 WaveFormat 的流混合在一起。您需要选择一个 WaveFormat 并将其传递给构造函数。然后你应该只添加具有 WaveFormat 的项目,尽管 MixingWaveProvider32 没有强制执行,当你一次只有一个项目时,就像你正在做的那样