4

次のコードを使用して、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)

それで、私は疑問に思っていました - どうすればこのような異なる音色を連結して曲を作ることができますか?

4

2 に答える 2

4

numpy.concatenate を使用してこれを行うことができます (既に投稿されているように)。連結軸も指定する必要があります。非常に低い率を使用して説明します。

from scipy.io.wavfile import write
from numpy import linspace,sin,pi,int16,concatenate

def note(freq, len, amp=1, rate=5):
 t = linspace(0,len,len*rate)
 data = sin(2*pi*freq*t)*amp
 return data.astype(int16) # two byte integers

tone1 = note(440,2,amp=10)
tone2 = note(140,2,amp=10)
print tone1
print tone2
print concatenate((tone2,tone1),axis=1)

#output:
[ 0 -9 -3  8  6 -6 -8  3  9  0]
[ 0  6  9  8  3 -3 -8 -9 -6  0]
[ 0  6  9  8  3 -3 -8 -9 -6  0  0 -9 -3  8  6 -6 -8  3  9  0]
于 2012-03-20T21:10:56.933 に答える
0

numpy.linspacenumpy 配列を作成します。トーンを連結するには、対応する配列を連結する必要があります。このために、少しグーグルで調べてみると、Numpy が便利な名前のnumpy.concatenatefunctionを提供していることがわかります。

于 2012-03-20T20:57:59.683 に答える