Wave form generation for audio synthesis. We'll focus on additive FM synthesis first.
Term | denotes |
---|---|
\( A \) | amplitude |
\( \sin \) | |
\( 2 \pi \) | |
\( f \) | frequency(hz) |
\( t \) | time |
\( \varphi \) | phase |
Sinewave playback example on Github
Only works with PulseAudio PulseAudio pacat
import subprocess
import numpy as np
import math
f = 3 # frequency in hz (incorrect, need to modify code for accurate timing.)
a = 0.1 # amplitude
p = 0 # phase
t = 0 # time step
buffersize = 1024 * 4 # size of sound buffer
current_time = math.pi*2 / buffersize
latency = buffersize * 8 # latency in number of bytes
pacatbuffer = bytearray()
# output sound using Pulseaudio using Pacat and stdin
output_sound = subprocess.Popen(('pacat', '--latency', str(latency), '--format', 'float32ne',
'--channels', '1'), stdin=subprocess.PIPE)
while True:
for i in range(buffersize):
t = t+current_time
sinewave = math.sin((math.pi*2)*(f*t)+p)*a
sample = np.float32(sinewave) # Python float to Numpy float32
sample.tobytes() # convert sound sample to bytes
pacatbuffer += bytearray(sample) # add bytes to buffer
output_sound.stdin.write(pacatbuffer) #write buffer to stdin
pacatbuffer = bytearray() # empty buffer
Episode 4: Sines And Cosines Part I - Project MATHEMATICS!
https://en.wikipedia.org/wiki/Decibel
High-pass
Band-pass
Low-pass
Moving average
Brown noise