如何使用 Ruby 或 Python 创建一系列高低音蜂鸣声? [英] How do I create a series of high- and low-pitch beeps using Ruby or Python?
本文介绍了如何使用 Ruby 或 Python 创建一系列高低音蜂鸣声?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在固定时间创建一系列低音和高音蜂鸣声.例如:
I want to create a series of a low- and high-pitch beeps at fixed times. For example:
- 150 毫秒的高音蜂鸣声
- 在 151 毫秒时发出低沉的哔哔声
- 在 200 毫秒时发出低沉的哔哔声
- 250 毫秒的高音蜂鸣
有没有办法在 Ruby 或 Python 中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg 等等),但我确实想创建一个输出文件.
Is there a way to do this in Ruby or Python? I don't really care what the output encoding is (.wav, .mp3, .ogg, whatever), but I do want to create an output file.
推荐答案
这是 Python 中的一个函数,可以生成一个带有单个正弦波的文件:
Here's a function in Python that makes a file with a single sine wave:
# 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)
不是最快的代码...但如果你不需要速度,它会工作得很好!
Not the fastest code...But if you don't need speed, it will work fine!
这篇关于如何使用 Ruby 或 Python 创建一系列高低音蜂鸣声?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文