I used soundfile with success with aiff files but I would like to generate sounds with definite fourier components.
I collected several codes online and put them together to create an aiff file and try to read it back. The code I came to is shown below. I know I am very close but I seem to get lost in the binary encoding of an aiff file. I tried many ways using h (short integer with a varying amplitude of 32767 to the sine function and taking an int()) and with the f (float) which gives something like the sound I am looking for but with a lot of distortion. Any help appreciated! (I work on macosx catalina and python 3.8, the encoding is little-endian so I used < in the struct statement).
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)