python - How to change continuously the frequency of a sinusoidal sound? -


i using pygame render sprite in window , want play sinusoidal sound frequency depends on y position of sprite. don't want discontinuities in phase of signal. what's best way achieve this?

i came solution. change freq newfreq , change phase this: newphase = 2*np.pi*t*(freq-newfreq)+phase

import pyaudio import numpy np time import time  channels = 2 rate = 44100  tt = time() freq = 100 newfreq = 100 phase = 0 def callback(in_data, frame_count, time_info, status):     global tt,phase,freq,newfreq     if newfreq != freq:         phase = 2*np.pi*tt*(freq-newfreq)+phase         freq=newfreq     left = (np.sin(phase+2*np.pi*freq*(tt+np.arange(frame_count)/float(rate))))     data = np.zeros((left.shape[0]*2,),np.float32)     data[::2] = left     data[1::2] = left     tt+=frame_count/float(rate)     return (data, pyaudio.pacontinue)  p = pyaudio.pyaudio()  stream = p.open(format=pyaudio.pafloat32,                 channels=channels,                 rate=rate,                 output=true,                 stream_callback=callback)  stream.start_stream() start = time() try:     while 1:         = time()              if now-start>1/24.:             newfreq=200+np.sin(2*np.pi*1/20.*now)*100 #update frequency depend on y on future             print newfreq         start=now finally:     stream.stop_stream()     stream.close()     p.terminate() 

Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -