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

前端之家收集整理的这篇文章主要介绍了如何使用Ruby或Python创建一系列高音和低音哔声?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在固定时间创建一系列低音和高音哔声.例如:

> 150 ms时的高音嘟嘟声
>低音提示音在151毫秒
> 200 ms时低音提示
> 250 ms处的高音嘟嘟声

有没有办法在RubyPython中执行此操作?我真的不关心输出编码是什么(.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)

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

原文链接:https://www.f2er.com/ruby/264932.html

猜你在找的Ruby相关文章