且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

更改声音文件的速度

更新时间:2023-02-13 23:30:24

有两种方法可以加快声音文件的播放速度:

There are two options to speed up the playback of a sound file:


  • 提高采样率

  • 减少单位时间的采样数。

在这两种方法中,播放速度的提高都会使声音的音高发生相应的变化。

In either of these methods, the increase in playback speed will have a corresponding change in the pitch of the sound.

采样率

提高采样率将提高声音的播放速度。例如,从22 KHz的采样率变为44 KHz,将使播放声音的速度是原始声音的两倍。在这种方法中,原始采样数据保持不变-只需更改音频播放设置。

Increasing the sample rate will increase the playback speed of the sound. For example, going from a 22 KHz sampling rate to 44 KHz will make the playback sound twice as fast as the original. In this method, the original sampling data is unaltered -- only the audio playback settings need to be changed.

减少单位时间的采样数

在这种方法中,回放采样率保持不变,但样本数量减少了-有些样本被扔掉了。

In this method, the playback sampling rate is kept constant, but the number of samples are reduced -- some of the samples are thrown out.

使声音播放速度是原始速度的两倍的天真方法是删除所有其他采样,并以原始播放采样率进行播放。

The naive approach to make the playback of the sound be twice the speed of the original is to remove every other sample, and playback at the original playback sampling rate.

但是,使用这种方法会丢失一些信息,并且我希望音频中会引入一些伪像,所以这不是最理想的方法。

However, with this approach, some of the information will be lost, and I would expect that some artifacts will be introduced to the audio, so it's not the most desirable approach.

尽管我自己还没有尝试过,但是对样本进行平均以创建新样本的想法是一个很好的起点。这似乎意味着,不仅可以舍弃音频信息,还可以通过平均过程将其保留到一定程度。

Although I haven't tried it myself, the idea of averaging the samples to create a new sample to be a good approach to start with. This would seem to mean that rather than just throwing out audio information, it can be "preserved" to an extent by the averaging process.

,这是一段伪代码,可以使播放速度提高一倍:

As a rough idea of the process, here's a piece of pseudocode to double the speed of playback:

original_samples = [0, 0.1, 0.2, 0.3, 0.4, 0.5]

def faster(samples):
    new_samples = []
    for i = 0 to samples.length:
        if i is even:
            new_samples.add(0.5 * (samples[i] + samples[i+1]))
    return new_samples

faster_samples = faster(original_samples)

我还发布了一个问题的答案 ,在其中我详细介绍了可以执行的一些基本音频操作,因此也许也很有趣。

I've also posted an answered to the a question "Getting started with programmatic audio" where I went into some details about some basic audio manipulation that can be performed, so maybe that might be of interest as well.