-1

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.

file = "total.wav"
comptype="NONE"
compname="not compressed"
num_samples = 48000
nchannels=1
sampwidth=2
wav_file = wave.open(file, 'w')
data = struct.pack('{n}h'.format(n=num_samples), data)
data = wav_file.writeframes(num_samples)
wav_file.setparams((nchannels, sampwidth, int(sampling_rate), nframes, comptype, compname))

Any ideas? Thank you

thiccdan69
  • 11
  • 3
  • 1
    Python doesn't just "crash", it gives you a detailed message. Please include that text in your question. – Mark Ransom Oct 24 '22 at 15:50
  • I don't see you iterating over your `frequencies` in the code you posted. You also don't close your wave file. – treuss Oct 24 '22 at 16:01

1 Answers1

-1
  1. You can't use array in sin
  2. Your sample duration now only 1 sec

try this:

import wave
import struct
import numpy as np

num_samples = 48000
frequency = 440 # [440, 330, 220, 110, 528, 756]
sampling_rate = 48000.0
amplitude = 16000
file = "segment.wav"
sine_wave = [0.5 * np.sin(2 * np.pi * x * frequency/sampling_rate) for x in range(5*num_samples)]
nframes=num_samples
comptype="NONE"
compname="not compressed"
nchannels=1
sampwidth=2
with wave.open(file, 'w') as wav_file:
    wav_file.setparams((nchannels, sampwidth, int(sampling_rate), nframes, comptype, compname))
    for s in sine_wave:
        wav_file.writeframes(struct.pack('h', int(s*amplitude)))
  • `numpy.sin` can absolutely operate on arrays – Pranav Hosangadi Oct 24 '22 at 16:50
  • @PranavHosangadi that's not the point of this question – alksndrglk Oct 24 '22 at 17:00
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 27 '22 at 23:25