如何使用Ruby或Python创建一系列高音和低音哔声?

我想在固定时间创建一系列低音和高音哔声。 例如:

  • 150毫秒的高音嘟嘟声
  • 低音提示音在151毫秒
  • 低音提示音为200 ms
  • 250毫秒时发出高音提示音

有没有办法在Ruby或Python中执行此操作? 我真的不关心输出编码是什么(.wav,.mp3,.ogg,等等),但我确实想创建一个输出文件。

这是Python中的一个函数,用于生成具有单个正弦波的文件:

# based on : www.daniweb.com/code/snippet263775.html import math import wave import struct def make_sine(freq=440, datasize=10000, fname="test.wav", framerate=44100.00): amp=8000.0 # amplitude sine_list=[] for x in range(datasize): sine_list.append(math.sin(2*math.pi * freq * ( x/frate))) # Open up a wav file wav_file=wave.open(fname,"w") # wav params nchannels = 1 sampwidth = 2 framerate = int(frate) nframes=datasize comptype= "NONE" compname= "not compressed" wav_file.setparams((nchannels, sampwidth, framerate, nframes, comptype, compname)) #write on file for s in sine_list: wav_file.writeframes(struct.pack('h', int(s*amp/2))) wav_file.close() frate = 44100.00 #that's the framerate freq=987.0 #that's the frequency, in hertz seconds = 3 #seconds of file data_length = frate*seconds #number of frames fname = "WaveTest2.wav" #name of file make_sine(freq, data_length, fname) 

不是最快的代码…但如果你不需要速度,它将工作正常!

试用Ruby音频文件库 (RAFL)。 它支持:

编写WAV文件时:

以任何采样率写入任意数量的通道以任何幅度或频率生成白噪声,粉红噪声和正弦波

这是该项目的GitHub源代码 。

python:

看起来你需要winsound模块。

具体来说,function:

 winsound.Beep(frequency, duration) 

哪个做你想要的。 但这仅适用于Windows。

有一个叫做beep的软件包可以让你在Linux上做同样的事情。 因此,如果您想要基于Linux的解决方案,请使用subprocess调用beep

以下是Ruby的一些帮助: 为Ruby应用程序添加声音 。 为了实际录制从电脑的扬声器到wav / mp3的嘟嘟声 – 我不知道这是否可能。

如果您需要使用声卡(不是扬声器)发出蜂鸣声,只需使用Ruby附带的Win32API模块即可:

 require 'Win32API' Beep = Win32API.new('kernel32', 'Beep', ['I', 'I'], 'I') Beep.call(1200,150) Beep.call(200,150) Beep.call(300,150) Beep.call(1400,150) 

对于Windows用户:使用gem win32-sound

 # gem install win32-sound require 'win32/sound' include Win32 Sound.play("SystemAsterisk", Sound::ALIAS) # play system asterisk sound Sound.beep(600,200) # play a beep 600 hertz for 200 milliseconds Sound.play("trompet.wav") # play a file from disk