Package: pysinewave
PySineWavePySineWave offers an easy way to generate and play sine waves that can make smooth, continuous transitions in pitch and volume in real time. These sine waves are created, threaded and played (fed into a sound channel) behind the scenes. All you have to do is create SineWaves and call their easy-to-use functions!InstallationOpen the terminal, and type:pipinstallpysinewaveCode ExampleThis code will play a sinewave that smoothly decreases its pitch.importtimefrompysinewaveimportSineWave# Create a sine wave, with a starting pitch of 12, and a pitch change speed of 10/second.sinewave=SineWave(pitch=12,pitch_per_second=10)# Turn the sine wave on.sinewave.play()# Sleep for 2 seconds, as the sinewave keeps playing.time.sleep(2)# Set the goal pitch to -5.sinewave.set_pitch(-5)# Sleep for 3 seconds, as the sinewave smoothly slides its pitch down from 12 to -5, and stays there.time.sleep(3)Useful FunctionsUseSineWave.set_pitch(pitch)to change the pitch of a SineWave object. The SineWave object will smoothly transition to this new pitch at a rate ofSineWave.pitch_per_second.UseSineWave.set_volume(decibels)to change the volume of a SineWave object. The SineWave object will smoothly transition to this new volume at a rate ofSineWave.decibels_per_second.UseSineWave.play()andSineWave.stop()to start and stop the SineWave, respectively.UseSineWave.set_pitch_per_second(pitch_per_second)andSineWave.set_decibels_per_second(decibels_per_second)to change the values ofSineWave.pitch_per_secondandSineWave.decibels_per_second, respectively.Channel managementYou are provided the ablility to output stereo audio. To do so, specifychannels=2when instanciating your SineWave object. In stereo mode, you have the possibility to choose which specific channel will output SineWave.There are 3 available parameters :'lr'which stands for "left-right" and outputs the audio in both left and right channels,'l'for "left", which only outputs audio in the left channel and'r'which stands for — you guessed it — "right" which outputs audio only in the right channel.A Note on Pitch and VolumeYou may want to directly modify the frequency and amplitude of a SineWave. We do provide two alternative functions,SineWave.set_frequency(hertz)andSineWave.set_amplitude(percent), however we suggest that you useSineWave.set_pitch(pitch)andSineWave.set_volume(decibels)instead.Why? The brain naturally perceivesratiosbetween sound's frequency and amplitude much better than differences. This means that working directly with frequency will cause high frequencies to be much harder to distinguish than low frequencies. Similarly for amplitude.The conversion between pitch and frequency (in Hz) is:frequency = 440 * 2^((pitch-9)/12). For instance, note that a pitch of 0 is middle C, i.e. a frequency of 261.63 Hz.The conversion between volume (in decibels) and amplitude is:amplitude = 2^(volume/10). For instance, increasing the volume by 10 decibels doubles the amplitude of the sine wave.Here's a helpful table showing the relationship between frequency, pitch, and musical notes for one octave:Pitch0123456789101112Frequency261.63277.18293.66311.13329.63349.23369.99392.00415.30440.00466.16493.88523.25NoteCC#/DbDD#/EbEFF#/GbGG#/AbAA#/BbBCIf you don't know anything about music theory, no worries! Just be sure to stick toSineWave.set_pitch(pitch)andSineWave.set_volume(decibels). Your Python projects will thank you.