简体   繁体   English

将音频数据写入 WAV 文件的首选方式?

[英]Preferred way to write audio data to a WAV file?

I am trying to write an audio file using python's wave and numpy .我正在尝试使用 python 的wavenumpy编写音频文件。 So far I have the following and it works well:到目前为止,我有以下内容并且效果很好:

import wave
import numpy as np

# set up WAV file parameters
num_channels    = 1                             # mono audio
sample_width    = 1                             # 8 bits(1 byte)/sample
sample_rate     = 44.1e3                        # 44.1k samples/second
frequency       = 440                           # 440 Hz
duration        = 20                            # play for this many seconds
num_samples     = int(sample_rate * duration)   # samples/seconds * seconds

# open WAV file and write data
with wave.open('sine8bit_2.wav', 'w') as wavfile:
    wavfile.setnchannels(num_channels)
    wavfile.setsampwidth(sample_width)
    wavfile.setframerate(sample_rate)

    t = np.linspace(0, duration, num_samples)
    data = (127*np.sin(2*np.pi*frequency*t)).astype(np.int8)
    wavfile.writeframes(data) # or data.tobytes() ??

My issue is that since I am using a high sampling rate, the num_samples variable might quickly become too large (9261000 samples for a 3 minute 30 seconds track say).我的问题是,由于我使用的是高采样率, num_samples变量可能会很快变得太大(比如 3 分 30 秒的轨道有 9261000 个样本)。 Would using a numpy array this large be advisable?使用这么大的 numpy 数组是否可取? Is there a better way of going about this?有没有更好的方法来解决这个问题? Also is use of writeframes(.tobytes()) needed in this case because my code runs fine without it and it seems like extra overhead (especially if the arrays get too large).在这种情况下还需要使用writeframes(.tobytes()) ,因为我的代码在没有它的情况下运行良好,而且看起来像是额外的开销(特别是如果 arrays 变得太大)。

Assuming you are only going to write a sine wave, you could very well create only one period as your data array and write that several times to the .wav file.假设您只打算写入一个正弦波,您可以只创建一个周期作为data数组并将其多次写入.wav文件。

Using the parameters you provided, your data array is 8800 times smaller with that approach.使用您提供的参数,您的data数组使用该方法缩小了 8800 倍。 Its size also no longer depends on the duration of your file!它的大小也不再取决于文件的持续时间!

import wave
import numpy as np

# set up WAV file parameters
num_channels    = 1                             # mono audio
sample_width    = 1                             # 8 bits(1 byte)/sample
sample_rate     = 44.1e3                        # 44.1k samples/second
frequency       = 440                           # 440 Hz
duration        = 20                            # play for this many seconds

# Create a single period of sine wave.
n = round(sample_rate/frequency)
t = np.linspace(0, 1/frequency, n)
data = (127*np.sin(2*np.pi*frequency*t)).astype(np.int8)
periods = round(frequency*duration)

# open WAV file and write data
with wave.open('sine8bit_2.wav', 'w') as wavfile:
    wavfile.setnchannels(num_channels)
    wavfile.setsampwidth(sample_width)
    wavfile.setframerate(sample_rate)

    for _ in range(periods):
        wavfile.writeframes(data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM