python - 如何生成带有蜂鸣声的 WAV 文件?

标签 python audio wav

有没有一种方法可以在 python 中生成连续不断的蜂鸣声并将其导出到 WAV 文件中?

最佳答案

我根据对上一个问题的回答,添加了很多评论。希望这能说明问题。您可能想要引入一个 for 循环来控制蜂鸣声的数量和增加的音量。

#!/usr/bin/python 
# based on : www.daniweb.com/code/snippet263775.html
import math
import wave
import struct

# Audio will contain a long list of samples (i.e. floating point numbers describing the
# waveform).  If you were working with a very long sound you'd want to stream this to
# disk instead of buffering it all in memory list this.  But most sounds will fit in 
# memory.
audio = []
sample_rate = 44100.0


def append_silence(duration_milliseconds=500):
    """
    Adding silence is easy - we add zeros to the end of our array
    """
    num_samples = duration_milliseconds * (sample_rate / 1000.0)

    for x in range(int(num_samples)): 
        audio.append(0.0)

    return


def append_sinewave(
        freq=440.0, 
        duration_milliseconds=500, 
        volume=1.0):
    """
    The sine wave generated here is the standard beep.  If you want something
    more aggresive you could try a square or saw tooth waveform.   Though there
    are some rather complicated issues with making high quality square and
    sawtooth waves... which we won't address here :) 
    """ 

    global audio # using global variables isn't cool.

    num_samples = duration_milliseconds * (sample_rate / 1000.0)

    for x in range(int(num_samples)):
        audio.append(volume * math.sin(2 * math.pi * freq * ( x / sample_rate )))

    return


def save_wav(file_name):
    # Open up a wav file
    wav_file=wave.open(file_name,"w")

    # wav params
    nchannels = 1

    sampwidth = 2

    # 44100 is the industry standard sample rate - CD quality.  If you need to
    # save on file size you can adjust it downwards. The stanard for low quality
    # is 8000 or 8kHz.
    nframes = len(audio)
    comptype = "NONE"
    compname = "not compressed"
    wav_file.setparams((nchannels, sampwidth, sample_rate, nframes, comptype, compname))

    # WAV files here are using short, 16 bit, signed integers for the 
    # sample size.  So we multiply the floating point data we have by 32767, the
    # maximum value for a short integer.  NOTE: It is theortically possible to
    # use the floating point -1.0 to 1.0 data directly in a WAV file but not
    # obvious how to do that using the wave module in python.
    for sample in audio:
        wav_file.writeframes(struct.pack('h', int( sample * 32767.0 )))

    wav_file.close()

    return


append_sinewave(volume=0.25)
append_silence()
append_sinewave(volume=0.5)
append_silence()
append_sinewave()
save_wav("output.wav")

关于python - 如何生成带有蜂鸣声的 WAV 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33879523/

相关文章:

python - 如何在 Python 3 中将具有属性的对象转换为不带 "_"的 JSON?

ios - 音量快速变化会导致 AVAudioPlayer 出现伪影

java - Clip.LOOP_CONTINUOUSLY 不起作用

python - 如何使用 gdcm 和 Python 解压压缩的 DICOM 图像?

python - “int”对象不是可迭代的 GIS 脚本

java - 如何使用绝对路径从 Java 运行 Python 文件?

c# - C# 中的频率分析器

python - 在Python中进行实时音频处理有什么方便的方法吗?

ios - 将 float 组转换为Swift文件Wav

android - 如何使用 AudioCodec 重新采样音频?