Reputation: 31
I want to make my own analogue for SSTV and I ran into a problem: when I start recording fast-changing beeps (which are 5 milliseconds long), very strange extra sounds appear
For creating beeps i used wave
, math
and struct
library (solve to creating beeps)
So, when I started this code, I received this file with VERY strange sounds: (click to hear)
import math
import wave
import struct
audio = []
sample_rate = 44100.0
def append_sinewave(freq=440.0, duration_milliseconds=500): # i removed volume from example
global audio
num_samples = duration_milliseconds * (sample_rate / 1000.0)
for x in range(int(num_samples)):
#audio.append(volume * math.sin(2 * math.pi * freq * ( x / sample_rate )))
audio.append(math.sin(2 * math.pi * freq * ( x / sample_rate )))
return
def save_wav(file_name):
wav_file=wave.open(file_name,"w")
nchannels = 1
sampwidth = 2
nframes = len(audio)
comptype = "NONE"
compname = "not compressed"
wav_file.setparams((nchannels, sampwidth, sample_rate, nframes, comptype, compname))
for sample in audio:
wav_file.writeframes(struct.pack('h', int( sample * 32767.0 )))
wav_file.close()
return
for i in range(1000): # creating sound
append_sinewave(freq=400+5*i, duration_milliseconds=5)
save_wav("output.wav")
Also, when I increasing duration of beeps (see code below) this strange sounds disappeared (click to hear)
for i in range(20): # more duration fixes the problem
append_sinewave(freq=400+250*i, duration_milliseconds=250)
save_wav("output.wav")
How to fix it?
Upvotes: 0
Views: 36
Reputation: 31
For solve my problem, I made the beginning of the wave from the previous frequency, and and because of this, the very claps that spoiled the sound disappeared. Solution in the code:
# we need to edit only append_sinewave function
audio = []
sample_rate = 44100.0
last_phase = 0 # a variable to save the phase between beeps
def append_sinewave(freq=440.0, duration_milliseconds=500):
global audio, last_phase
num_samples = int(duration_milliseconds * (sample_rate / 1000.0))
# adding a signal while continuing the phase
for x in range(num_samples):
phase = last_phase + 2 * math.pi * freq * (x / sample_rate)
sample = math.sin(phase)
audio.append(sample)
# save the phase so that the next frequency continues the wave
last_phase += 2 * math.pi * freq * (num_samples / sample_rate)
last_phase %= 2 * math.pi # normalizing
return
Thanks to OysterShucker for idea
Upvotes: 1