From 5bba29570edb05a58845236134ab5f917bb0a70d Mon Sep 17 00:00:00 2001 From: Anthony Wang Date: Tue, 21 Mar 2023 22:56:34 -0400 Subject: Implement saw wave and honk waveform --- keyboard.py | 2 +- yue.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/keyboard.py b/keyboard.py index 73892ea..6dd05f0 100644 --- a/keyboard.py +++ b/keyboard.py @@ -10,7 +10,7 @@ bitrate = 44100 note = [] for i in range(0, 24): - note.append([yue.tone(yue.freq(3, i), j / bitrate) / 4 for j in range(0, 3 * bitrate)]) + note.append([yue.seething(yue.freq(4, i), j / bitrate) / 4 for j in range(0, 3 * bitrate)]) sd.default.samplerate = bitrate print("READY") diff --git a/yue.py b/yue.py index 5ded276..7435d48 100644 --- a/yue.py +++ b/yue.py @@ -8,6 +8,13 @@ bitrate = 44100 music = [] +def saw(x): + """ + Sawtooth wave + """ + return x / 2 / math.pi % 1 + + def freq(octave, step): """ Returns the frequency of a note @@ -45,6 +52,16 @@ def seething(f, t): return Y +def honk(f, t): + """ + Returns the intensity of the "honk" waveform of frequency f sampled at time t + """ + w = 2 * math.pi * f + Y = sum([(saw(i * w * t) / 3 + math.sin(i * w * t)) * math.exp(-0.002 * w * t) for i in range(1, 6)]) / 2 + Y = math.copysign(abs(Y) ** (1 / 2), Y) + return Y + + def at(t): """ Returns the total intensity of music sampled at time t -- cgit v1.2.3-70-g09d2