-
Notifications
You must be signed in to change notification settings - Fork 0
/
lpf.py
32 lines (27 loc) · 984 Bytes
/
lpf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from audiolazy import sHz, sinusoid, Stream, AudioIO, z, pi, chunks
import time, sys
# Basic initialization
rate = 44100
s, Hz = sHz(rate)
# Some time-variant coefficients
cycle_a1 = [.1, .2, .1, 0, -.1, -.2, -.1, 0]
cycle_a2 = [.1, 0, -.1, 0, 0]
a1 = Stream(*cycle_a1)
a2 = Stream(*cycle_a2) * 2
b1 = sinusoid(18 * Hz) # Sine phase
b2 = sinusoid(freq=7 * Hz, phase=pi/2) # Cosine phase
# The filter
filt = (1 + b1 * z ** -1 + b2 * z ** -2 + .7 * z ** -5)
filt /= (1 - a1 * z ** -1 - a2 * z ** -2 - .1 * z ** -3)
# A really simple input
input_data = sinusoid(220 * Hz)
# Let's play it!
api = sys.argv[1] if sys.argv[1:] else None # Choose API via command-line
chunks.size = 1 if api == "jack" else 16
with AudioIO(api=api) as player:
th = player.play(input_data, rate=rate)
time.sleep(1) # Wait a sec
th.stop()
time.sleep(1) # One sec "paused"
player.play(filt(input_data), rate=rate) # It's nice with rate/2 here =)
time.sleep(3) # Play the "filtered" input (3 secs)