Add 'plot' function and remove deprecated 'percent' type

This commit is contained in:
Bartłomiej Pluta
2019-07-30 13:59:18 +02:00
parent 8abae7c2ff
commit 7e55fe4c1a
5 changed files with 145 additions and 8 deletions

View File

@@ -1,5 +1,6 @@
import time
import matplotlib.pyplot as plt
import numpy as np
import sounddevice as sd
@@ -12,6 +13,13 @@ def pause(value, bpm):
time.sleep(60 * 4 / value / bpm)
def plot(note, config):
Y = sineForNote(note, config)
X = np.arange(len(Y))
plt.plot(X, Y)
plt.show()
def play(notes, config):
compiled = compilePolyphony(notes, config)
sd.play(compiled)
@@ -46,7 +54,7 @@ def sineForNote(note, config):
def sound(frequency, duration, config):
return decay(sum(a * sine((i+1) * frequency, duration) for i, a in enumerate(config.overtones)), config)
return attack(decay(sum(a * sine((i+1) * frequency, duration) for i, a in enumerate(config.overtones)), config), config)
def decay(wave, config):
@@ -55,6 +63,14 @@ def decay(wave, config):
return magnitude * wave
def attack(wave, config):
magnitude = -np.exp(-config.attack / len(wave) * np.arange(len(wave)))+1 \
if config.attack > 0 \
else np.ones(len(wave))
return magnitude * wave
def sine(frequency, duration):
return (np.sin(2 * np.pi * np.arange(FS * duration) * frequency / FS)).astype(np.float32)