When this runs, there is a clicking sound between pitches. I don't mind the clicking sound too much - it's pleasantly rhythmic. That said...
- I'd like to be able to get rid of this clicking sound when I don't want it.
- Better yet, it would be nice to be able to control the clicking sound in some way - volume, etc.
I have seen this thread, but haven't figured out how to apply it to my problem: How to remove pops from concatented sound data in PyAudio
Any ideas? Thanks for your time!
import numpy
import pyaudio
import math
import random
def sine(frequency, length, rate):
length = int(length * rate)
factor = float(frequency) * (math.pi * 2) / rate
waveform = numpy.sin(numpy.arange(length) * factor)
return waveform
def play_tone(stream, frequency, length, rate=44100):
chunks = []
chunks.append(sine(frequency, length, rate))
chunk = numpy.concatenate(chunks) * .25
stream.write(chunk.astype(numpy.float32).tostring())
def bassline():
frequency = 300
for i in range(1000000):
play_tone(stream, frequency, .15)
change = random.choice([-75, -75, -10, 10, 2, 3, 100, -125])
print (frequency)
if frequency < 0:
frequency = random.choice([100, 200, 250, 300])
else:
frequency = frequency + change
if __name__ == '__main__':
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paFloat32,
channels=1, rate=44100, output=4)
bassline()
/EDIT
I've plotted the tones and it looks like the discontinuity is in the relationship between the starting and ending phase of each tone.
Any ideas how to remedy this?