gpt4 book ai didi

python - 使用python同时播放两个正弦音

转载 作者:太空宇宙 更新时间:2023-11-03 11:57:34 25 4
gpt4 key购买 nike

我正在使用 python 播放正弦音。音调基于计算机的内部时间(以分钟为单位),但我想同时播放一个以秒为单位的协调或双重声音。

这是我目前所拥有的;有人能指出我正确的方向吗?

from struct import pack
from math import sin, pi
import time

def au_file(name, freq, dur, vol):
fout = open(name, 'wb')
# header needs size, encoding=2, sampling_rate=8000, channel=1
fout.write('.snd' + pack('>5L', 24, 8*dur, 2, 8000, 1))
factor = 2 * pi * freq/8000
# write data
for seg in range(8 * dur):
# sine wave calculations
sin_seg = sin(seg * factor)
fout.write(pack('b', vol * 127 * sin_seg))
fout.close()

t = time.strftime("%S", time.localtime())
ti = time.strftime("%M", time.localtime())
tis = float(t)
tis = tis * 100
tim = float(ti)
tim = tim * 100

if __name__ == '__main__':
au_file(name='timeSound1.au', freq = tim, dur=1000, vol=1.0)

import os
os.startfile('timeSound1.au')

最佳答案

代码中的以下最小更改...:

from struct import pack
from math import sin, pi
import time

def au_file(name, freq, freq1, dur, vol):
fout = open(name, 'wb')
# header needs size, encoding=2, sampling_rate=8000, channel=1
fout.write('.snd' + pack('>5L', 24, 8*dur, 2, 8000, 1))
factor = 2 * pi * freq/8000
factor1 = 2 * pi * freq1/8000
# write data
for seg in range(8 * dur):
# sine wave calculations
sin_seg = sin(seg * factor) + sin(seg * factor1)
fout.write(pack('b', vol * 64 * sin_seg))
fout.close()

t = time.strftime("%S", time.localtime())
ti = time.strftime("%M", time.localtime())
tis = float(t)
tis = tis * 100
tim = float(ti)
tim = tim * 100

if __name__ == '__main__':
au_file(name='timeSound2.au', freq=tim, freq1=tis, dur=1000, vol=1.0)

import os
os.startfile('timeSound2.au')

关于python - 使用python同时播放两个正弦音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2572651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com