-
Notifications
You must be signed in to change notification settings - Fork 0
/
old_main.py
111 lines (86 loc) · 3.56 KB
/
old_main.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from collections import OrderedDict
from synthplayer.synth import *
from synthplayer.oscillators import *
from synthplayer.playback import Output
from synthplayer.sample import Sample
from typing import Generator, List, Sequence, Optional, Tuple, Iterator
from scipy import fft
import scipy.signal as signal
import matplotlib.pyplot as plt
import numpy as np
import array
notes = [
None,
OrderedDict((note, key_freq(4+i)) for i, note in enumerate(octave_notes)),
OrderedDict((note, key_freq(16+i)) for i, note in enumerate(octave_notes)),
OrderedDict((note, key_freq(28+i)) for i, note in enumerate(octave_notes)),
OrderedDict((note, key_freq(40+i)) for i, note in enumerate(octave_notes)),
OrderedDict((note, key_freq(52+i)) for i, note in enumerate(octave_notes)),
OrderedDict((note, key_freq(64+i)) for i, note in enumerate(octave_notes)),
OrderedDict((note, key_freq(76+i)) for i, note in enumerate(octave_notes))
]
fs = 44100 # SAMPLE RATE
class LPF:
def __init__(self, wave):
self.fs = 44100 # sample rate
self.wave = wave # waveform
self.wins = 100 # number of windows in rolling window filter
self.max_cutoff = 1000 / (fs/2) # max normalised cutoff
self.env_params = [0.3, 0.1, 0.5, 1, 0.5]
self.A = self.env_params[0]
self.D = self.env_params[1]
self.S = self.env_params[2]
self.SL = self.env_params[3]
self.R = self.env_params[4]
def lpf(self):
lfo = Linear(1000, samplerate=self.fs)
lfo = EnvelopeFilter(lfo, self.A, self.D, self.S, self.SL, self.R, stop_at_end=True).blocks()
env = sum(list(lfo), [])
L = len(self.wave) # length of wave
Lwin = int(L / self.wins) # length of wave window
R = len(env) # length of envelope
Rwin = int(R / self.wins) # length of envelope window
wave_filt = np.array([])
for n in range(self.wins):
win_wave = self.wave[n*Lwin:(n+1)*Lwin]
win_env = env[n*Rwin:(n+1)*Rwin]
cutoff = np.mean(win_env) # cutoff freq
cutoff_norm = ((cutoff - min(env))/(max(env) - min(env))) * self.max_cutoff
# print('max, min', max(env), min(env))
# print('cutoff', cutoff, cutoff_norm)
filt = signal.butter(4, cutoff_norm, 'lowpass', output = 'sos') # filter
win_filt = signal.sosfilt(filt, win_wave)
wave_filt = np.append(wave_filt, win_filt)
print('LENGTH',len(wave_filt))
# print(wave_filt)
return wave_filt
def get_data(osc: Oscillator) -> List[float]:
return next(osc.blocks())
def plot(data):
plt.figure()
plt.plot(data)
def plot_f(data):
f, t, Sxx = signal.spectrogram(np.array(data), fs)
plt.figure()
plt.pcolormesh(t, f, Sxx)
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [sec]')
# plt.ylim(0, 2000)
W = WaveSynth(fs, 2)
w = W.sawtooth(200, duration = 1).get_frame_array()
# w = get_data(Sawtooth(100, samplerate=fs))
LPF = LPF(w)
w_filt = LPF.lpf()
# plot(w)
plot(w_filt)
w_filt = w_filt.astype(int) # set as integer array
w_filt = array.array('i',w_filt)
out1 = Sample().from_array(w, fs, 1)
out2 = Sample(samplewidth=4).from_array(w_filt, fs, 1).make_16bit()
# with Output(nchannels=1, mixing="sequential", queue_size=2, samplewidth = 2) as out:
# out.play_sample(out1)
# out.wait_all_played()
with Output(nchannels=1, mixing="sequential", queue_size=2, samplewidth = 2) as out:
out.play_sample(out2)
out.wait_all_played()
plt.show()