加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 03:34:16 所属栏目:百科 来源:网络整理
导读:我想在固定时间创建一系列低音和高音哔声.例如: 150 ms时的高音嘟嘟声 低音提示音在151毫秒 200 ms时低音提示音 250 ms处的高音嘟嘟声 有没有办法在Ruby或Python中执行此操作?我真的不关心输出编码是什么(.wav,.mp3,.ogg,等等),但我确实想创建一个输出文件
我想在固定时间创建一系列低音和高音哔声.例如:

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

有没有办法在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)

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读