Generate and play sine waves in real time, that can make smooth, continuous transitions in pitch and volume.
PySineWave 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 an sound channel) behind the scenes, so that all you have to do is call their easy-to-use functions!
Open the terminal, and type:
pip install pysinewave
This code will play a sinewave that smoothly decreases its pitch.
import time from pysinewave import SineWave # 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)
SineWave.set_pitch(pitch) to change the pitch of a SineWave object. The SineWave object will smoothly transition to this new pitch at a rate of
SineWave.set_volume(decibels) to change the volume of a SineWave object. The SineWave object will smoothly transition to this new volume at a rate of
SineWave.stop() to start and stop the SineWave, respectively.
SineWave.set_decibels_per_second(decibels_per_second) to change the values of
A Note on Pitch and Volume
You may want to directly modify the frequency and amplitude of a SineWave. We do provide two alternative functions,
SineWave.set_amplitude(percent), however we suggest that you use
Why? The brain naturally perceives ratios between 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)/2). 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:
If you don't know anything about music theory, no worries! Just be sure to stick to
SineWave.set_volume(decibels). Your Python projects will thank you.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size pysinewave-0.0.6-py3-none-any.whl (7.1 kB)||File type Wheel||Python version py3||Upload date||Hashes View|
|Filename, size pysinewave-0.0.6.tar.gz (5.8 kB)||File type Source||Python version None||Upload date||Hashes View|
Hashes for pysinewave-0.0.6-py3-none-any.whl