NAudio获取音频样本

时间:2016-06-02 03:14:03

标签: c# wpf naudio

我正在使用NAudio从正在播放的歌曲中获取样本,并在歌曲播放时绘制波形。我正在使用AudioFileReaderToSampleProvider将所有样本设为float,然后在播放歌曲时将其绘制为InkCanvas。我的问题是样本似乎与声音不匹配。我也通过使用位于NAudio源代码中的WPF示例中的相同歌曲来验证这一点。在示例中,波形与声音匹配,但在我的应用中它并不是。所以我想知道是否有人可以帮助我找出我正在做什么(或阅读)错误,或者我的绘图逻辑是否错误。

这是我目前的代码:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private ISampleProvider provider;
    private DispatcherTimer timer;
    private AudioFileReader reader;

    private WaveOut waveOut;
    private StylusPointCollection topPoints, bottomPoints;
    private DrawingAttributes attr;

    private double canvasHeight, canvasWidth;
    private int samplesGroupSize;
    private double drawPos = 0;

    private StrokeCollection _WaveformLines;
    public StrokeCollection WaveformLines
    {
        get { return _WaveformLines; } 
        set
        {
            _WaveformLines = value;
            OnPropertyChanged("WaveformLines");
        }
    }

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        reader = new AudioFileReader("C:\\Users\\Agustin\\Desktop\\DragonRider.mp3");
        waveOut = new WaveOut();
        waveOut.Init(reader);

        provider = reader.ToSampleProvider(); //Here I get the samples
        reader.Position = 0; //Go to the position 0 after reading the samples

        canvasHeight = Waveform.ActualHeight;
        canvasWidth = Waveform.ActualWidth;

        WaveformLines = new StrokeCollection();
        topPoints = new StylusPointCollection();
        topPoints.Add(new StylusPoint(0, (canvasHeight / 2)));
        topPoints.Changed += topPoints_Changed;
        bottomPoints = new StylusPointCollection();
        bottomPoints.Add(new StylusPoint(0, (canvasHeight / 2)));
        bottomPoints.Changed += topPoints_Changed;
        WaveformLines.Add(new Stroke(topPoints));
        WaveformLines.Add(new Stroke(bottomPoints));

        attr = new DrawingAttributes();
        attr.Color = Colors.Green;
        attr.Width = 1.5;
        attr.Height = 1;

        timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromMilliseconds(1);
        timer.Tick += timer_Tick;
        timer.Start();
        samplesGroupSize = (int)(timer.Interval.TotalSeconds * reader.WaveFormat.SampleRate); //The value for this is 44.
    }

    private void PlayButton_Click(object sender, RoutedEventArgs e)
    {
        waveOut.Play();
    }
    private void PauseButton_Click(object sender, RoutedEventArgs e)
    {
        waveOut.Pause();
    }

    private void timer_Tick(object sender, EventArgs e)
    {
        if (waveOut.PlaybackState == PlaybackState.Playing)
        {
            TimeLabel.Content = string.Format("Time: {0}", reader.CurrentTime.ToString(@"mm\:ss\:ff")); //NEED TO KEEP WORKING
            float[] samps = new float[samplesGroupSize];
            provider.Read(samps, 0, samps.Length);
            float max = Max(samps);
            float min = Min(samps);
            topPoints.Add(new StylusPoint(drawPos, (canvasHeight / 2) - ((canvasHeight / 2) * max)));
            bottomPoints.Add(new StylusPoint(drawPos, (canvasHeight / 2) - ((canvasHeight / 2) * min)));
            drawPos += 2;
            if (drawPos > canvasWidth)
            {
                WaveformLines.Clear();
                topPoints = new StylusPointCollection();
                topPoints.Add(new StylusPoint(0, (canvasHeight / 2)));
                bottomPoints = new StylusPointCollection();
                bottomPoints.Add(new StylusPoint(0, (canvasHeight / 2)));
                WaveformLines.Add(new Stroke(topPoints));
                WaveformLines.Add(new Stroke(bottomPoints));
                drawPos = 0;
            }
        }
    }

    private float Min(float[] samps)
    {
        float max = samps[0];
        foreach (float s in samps)
        {
            if (s > max)
                max = s;
        }
        return max;
    }
    private float Max(float[] samps)
    {
        float min = samps[0];
        foreach (float s in samps)
        {
            if (s < min)
                min = s;
        }
        return min;
    }

    //I excluded the INotifyPropertyChanged implementation, but in the
    //actual code is located here
}

我知道这个绘图算法不是很好,但我尝试了其他人,他们似乎也没有跟随音频。

感谢。

注意:我知道有类似的问题,但其他问题建议使用我已经使用的AudioFileReaderToSampleProvider之类的内容。我的错误可能更多的是我如何阅读样本,也许我缺少一些字节或者必须跳过一些字节,或者可能是一些我没有设置的缺失属性。

1 个答案:

答案 0 :(得分:0)

您应该认真考虑使用处理读/播的WFP example代码部分和最小/最大计算工作

这将需要你一点努力,但我保证这将是值得的。

然后,您将使用您知道格式正确的数据集,并且可以专注于绘图部分。仔细查看AudioPlayback.cs及其与SampleAggregator.cs的关系。

您还会发现,在读取(和播放)样本时获取自动回调是一种更好的刷新波形图的方法,而不是尝试使用DispatchTimer。它也会让你不再重读波形缓冲区 - 如果可以,你真的想避免这种情况。

编辑:

我测试了您的转换代码,结果float值似乎正确(在-1到1的范围内)。所以我认为问题在于你在WPF中绘制波形的方式。