2010-04-03 47 views
4

我正在使用python播放正弦音調。基調是基於計算機的內部時間在幾分鐘內,但我想同時播放一個基於第二個的協調或雙重聲音。使用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') 

回答

1

怎麼樣在你的代碼如下最小的變化...:

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') 
+0

非常感謝你這似乎做 – Alex 2010-04-03 21:46:04