次のコードを使用して、2 秒間続く 440 Hz のトーンを含む wav ファイルを生成します。
from scipy.io.wavfile import write
from numpy import linspace,sin,pi,int16
def note(freq, len, amp=1, rate=44100):
t = linspace(0,len,len*rate)
data = sin(2*pi*freq*t)*amp
return data.astype(int16) # two byte integers
tone = note(440,2,amp=10000)
write('440hzAtone.wav',44100,tone) # writing the sound to a file
Pythonで実際に曲を生成するために、ノートメソッドに基づいてコードを変更できるかどうか疑問に思っていました。
2 つの異なるトーンを追加してみましたが、予想どおり 2 つのトーンが同時に再生され、ダイヤル トーンのようなサウンドが作成されました。
tone1 = note(440,2,amp=10000)
tone2 = note(480,2,amp=10000)
tone = tone1+tone2
write('440hzAtone.wav',44100,tone)
2 つのトーンを掛け合わせることも試みましたが、これでは静電気が発生するだけです。
また、さまざまな長さのトーンをジャンル化して追加しようとしましたが、これにより、次のように例外が発生します。
tone1 = note(440,2,amp=10000)
tone2 = note(480,1,amp=10000)
tone = tone1+tone2
write('440hzAtone.wav',44100,tone)
原因:
ValueError: operands could not be broadcast together with shapes (88200) (44100)
それで、私は疑問に思っていました - どうすればこのような異なる音色を連結して曲を作ることができますか?