如何使用 NAudio C# 更改播放速度

发布于 2024-11-18 17:47:45 字数 95 浏览 4 评论 0原文

我正在实现一个媒体播放器并使用 NAudio 来播放我的文件。有什么方法可以在播放声音时提高播放速度(2 倍或 4 倍)。代码示例将不胜感激。

提前致谢 干杯。

I am implementing a Media Player and using NAudio to play my files. Is there any way to increase the playback speed like (2X or 4X) while the sound is playing. Code samples will be appreciated.

Thanks in Advance
Cheers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黯然#的苍凉 2024-11-25 17:47:45

NAudio 不包含用于更改音频播放速度的现成组件。但是,如果您创建自己的派生 WaveStream / IWaveProvider 并自己实现加速算法,则这是可能的。获得 2 倍或 4 倍速度提升的最简单方法就是丢弃样本。但是,质量不会很好(会引入文物),因此是否可以选择该选项取决于您的需求。

我自己曾经在 NAudio 中实现过可变播放速度,但不幸的是我无法在这里分享代码,因为它不是开源的。不过,Yuval Naveh 在他的 PracticeSharp 应用程序中实现了可变播放速度,该应用程序使用 NAudio,所以你可能想看看他是如何做到的(我认为他是通过包装 SoundTouch 来实现的) 。

NAudio does not include a ready-made component to change the speed of audio playback. However, it is possible if you create your own derived WaveStream / IWaveProvider and implement a speedup algorithm yourself. The simplest way to get a 2x or 4x speed increase is just to throw away samples. However, the quality will not be good (artefacts will be introduced), so it depends on your needs as to whether you can go with that option or not.

I have implemented variable playback speed in NAudio once myself but unfortunately I can't share the code here as it is not open source. Yuval Naveh, however has implemented variable playback speed as part of his PracticeSharp application, which uses NAudio, so you might want to check out how he has done it (I think he achieves it by wrapping SoundTouch).

任性一次 2024-11-25 17:47:45

我最近向 ispy 添加了变速播放 - 它使用 naudio 进行播放。 PlaybackRate 是一个双精度值 - 设置它可以减慢或加快音频速度:

if (WaveOutProvider != null)
{
    if (Math.Abs(PlaybackRate - 1) > double.Epsilon)
    {
    //resample audio if playback speed changed
    var newRate = Convert.ToInt32(_waveProvider.WaveFormat.SampleRate/PlaybackRate);
    var wf = new WaveFormat(newRate, 16, _waveProvider.WaveFormat.Channels);
    var resampleInputMemoryStream = new MemoryStream(data) {Position = 0};

    WaveStream ws = new RawSourceWaveStream(resampleInputMemoryStream, _waveProvider.WaveFormat);
    var wfcs = new WaveFormatConversionStream(wf,ws) {Position = 0};
    var b = new byte[ws.WaveFormat.AverageBytesPerSecond];

    int bo = wfcs.Read(b, 0, ws.WaveFormat.AverageBytesPerSecond);
    while (bo > 0)
    {
        WaveOutProvider.AddSamples(b, 0, bo);
        bo = wfcs.Read(b, 0, ws.WaveFormat.AverageBytesPerSecond);
    }
    wfcs.Dispose();
    ws.Dispose();

    }
    else
    {
    WaveOutProvider.AddSamples(data, 0, data.Length);    
    }

}

I've recently added variable speed playback to ispy - that uses naudio for playback. PlaybackRate is a double - set it to slow down or speed up audio:

if (WaveOutProvider != null)
{
    if (Math.Abs(PlaybackRate - 1) > double.Epsilon)
    {
    //resample audio if playback speed changed
    var newRate = Convert.ToInt32(_waveProvider.WaveFormat.SampleRate/PlaybackRate);
    var wf = new WaveFormat(newRate, 16, _waveProvider.WaveFormat.Channels);
    var resampleInputMemoryStream = new MemoryStream(data) {Position = 0};

    WaveStream ws = new RawSourceWaveStream(resampleInputMemoryStream, _waveProvider.WaveFormat);
    var wfcs = new WaveFormatConversionStream(wf,ws) {Position = 0};
    var b = new byte[ws.WaveFormat.AverageBytesPerSecond];

    int bo = wfcs.Read(b, 0, ws.WaveFormat.AverageBytesPerSecond);
    while (bo > 0)
    {
        WaveOutProvider.AddSamples(b, 0, bo);
        bo = wfcs.Read(b, 0, ws.WaveFormat.AverageBytesPerSecond);
    }
    wfcs.Dispose();
    ws.Dispose();

    }
    else
    {
    WaveOutProvider.AddSamples(data, 0, data.Length);    
    }

}
灼痛 2024-11-25 17:47:45

我已经完成了 PracticeSharp,他们正在实现飞行速度(在音频播放时)。但我的要求不符合。
如果有任何简单的解决方案可以在 Speed on Fly 上工作,请提供,例如,像这样的 VolumeSampleProvider 是否有任何用于速度更改的类。

I have gone throughthe PracticeSharp they are implementing the Speed on Fly(while Audio Playing). but my requirement didn't match.
If any simple solution to work on Speed on Fly please provide, for eample for volume the VolumeSampleProvider like this is there any class for Speed change.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文