aiff ファイルでサウンドファイルを使用して成功しましたが、明確なフーリエ コンポーネントでサウンドを生成したいと考えています。
オンラインでいくつかのコードを収集し、それらをまとめて aiff ファイルを作成し、それを読み戻そうとしました。私がたどり着いたコードを以下に示します。私は非常に近いことを知っていますが、aiff ファイルのバイナリ エンコーディングで迷子になっているようです。h (正弦関数に対して 32767 の可変振幅を持ち、int() を取る短い整数) と、探している音のようなものを与える f (float) を使用して多くの方法を試しましたが、多くの歪みがあります。どんな助けでも大歓迎です!(私は macosx catalina と python 3.8 で作業しています。エンコーディングはリトルエンディアンなので、構造体ステートメントで < を使用しました)。
import aifc,struct
import numpy as np
import soundfile as sf
import sounddevice as sd
sampleRate = 44100 # hertz
seconds = 3 # seconds
frequency = 440.0 # hertz
obj = aifc.open('sound.aif','w')
obj.setnchannels(1) # mono
obj.setsampwidth(2)
obj.setframerate(sampleRate)
t = np.linspace(0, seconds, seconds * sampleRate, False)
value=[]
for i in t:
value.append(np.sin(i*frequency*2*np.pi))
# This is the line that troubles me...
data = struct.pack('<'+'f'*len(value), *value)
plt.figure()
plt.plot(t[0:100],value[0:100])
plt.show()
obj.writeframes( data )
obj.close()
obj = aifc.open('sound.aif','r')
print( "Number of channels",obj.getnchannels())
print ( "Sample width",obj.getsampwidth())
print ( "Frame rate.",obj.getframerate())
print ("Number of frames",obj.getnframes())
print ( "parameters:",obj.getparams())
obj.close()
data, fs = sf.read('sound.aif')
sd.play(data, fs, blocking=True)