Generate and play sine waves in real time, that can make smooth, continuous transitions in pitch and volume.
Project description
PySineWave
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 a sound channel) behind the scenes. All you have to do is create SineWaves and call their easy-to-use functions!
Installation
Open the terminal, and type:
pip install pysinewave
Code Example
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)
Useful Functions
Use 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.pitch_per_second
.
Use 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.decibels_per_second
.
Use SineWave.play()
and SineWave.stop()
to start and stop the SineWave, respectively.
Use SineWave.set_pitch_per_second(pitch_per_second)
and SineWave.set_decibels_per_second(decibels_per_second)
to change the values of SineWave.pitch_per_second
and SineWave.decibels_per_second
, respectively.
Channel management
You are provided the ablility to output stereo audio. To do so, specify channels=2
when 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 Volume
You may want to directly modify the frequency and amplitude of a SineWave. We do provide two alternative functions, SineWave.set_frequency(hertz)
and SineWave.set_amplitude(percent)
, however we suggest that you use SineWave.set_pitch(pitch)
and SineWave.set_volume(decibels)
instead.
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)/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:
Pitch | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Frequency | 261.63 | 277.18 | 293.66 | 311.13 | 329.63 | 349.23 | 369.99 | 392.00 | 415.30 | 440.00 | 466.16 | 493.88 | 523.25 |
Note | C | C#/Db | D | D#/Eb | E | F | F#/Gb | G | G#/Ab | A | A#/Bb | B | C |
If you don't know anything about music theory, no worries! Just be sure to stick to SineWave.set_pitch(pitch)
and SineWave.set_volume(decibels)
. Your Python projects will thank you.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pysinewave-0.0.7.tar.gz
.
File metadata
- Download URL: pysinewave-0.0.7.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e76bfb1b6cfe928f9339fe942b25efc6868977c613444570b19c36f1aeafb301 |
|
MD5 | df0a1baef3cccf09b68d51cbcc4cba36 |
|
BLAKE2b-256 | 9213b0eb648c249d541d957d2eaeb88dc412f1798918214b8da54fc5422ff97e |
File details
Details for the file pysinewave-0.0.7-py3-none-any.whl
.
File metadata
- Download URL: pysinewave-0.0.7-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 496859471063499897007483b95797f83c13e27a47eb37b06902b8f1453b3596 |
|
MD5 | 52b9f1fb1901c1d74db7feeef171c421 |
|
BLAKE2b-256 | 15612435ebf1dd9765bc31a56041379fb936a626dc3cc386a01c7218c2f570f4 |