0

I have 3 different waves. One of 50 one of 100 and one of 150 hz. The code below is how I am getting my data.

frequency3 = 150
num_samples = 48000
sampling_rate = 48000.0
amplitude = 16000
file = "wave3.wav"
noise_wave = [np.sin(2 * np.pi * frequency3 * x/sampling_rate) for x in range(num_samples)]
nframes=num_samples
comptype="NONE"
compname="not compressed"
nchannels=1
sampwidth=2
wav_file=wave.open(file, 'w')
wav_file.setparams((nchannels, sampwidth, int(sampling_rate), nframes, comptype, compname))
for s in noise_wave:
    wav_file.writeframes(struct.pack('h', int(s*amplitude)))


frame_rate = 48000.0
infile = "wave3.wav"
num_samples = 48000
wav_file = wave.open(infile, 'r')
data3 = wav_file.readframes(num_samples)
wav_file.close()
data3 = struct.unpack('{n}h'.format(n=num_samples), data3)
data3 = np.array(data3)

I am trying to combine that data with the data from the other two waves to form a singular wave.

data = data1 + data2 + data3
plt.subplot(1,1,1)
plt.plot(data[:3000])
plt.title('Entire signal')
plt.show()

The code above gives me a chart that is a correct representation of the combined wave. My question is how do I take the data and create a wave file with it that I can listen to. This is what I tried so far but it has not been working.

outfile = wave.open('total.wav', 'wb')
num_samples = 48000
rate = 48000.0
nchannels=1
outfile.setchannels(nchannels)
sampwidth=2
outfile.setsamplewidth(sampwidth)
outfile.setframerate(rate)
data = struct.pack('{n}h'.format(n=num_samples), data)
outfile.writeframes(data)

It saves it as a wave file but I am unable to listen to it. Any ideas? Thank you

thiccdan69
  • 11
  • 3

0 Answers0