Move 'synth' module
This commit is contained in:
0
smnp/module/synth/lib/__init__.py
Normal file
0
smnp/module/synth/lib/__init__.py
Normal file
24
smnp/module/synth/lib/player.py
Normal file
24
smnp/module/synth/lib/player.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import time
|
||||
|
||||
from smnp.module.synth.lib.wave import sine
|
||||
from smnp.note.model import Note
|
||||
|
||||
|
||||
def playNotes(notes, bpm):
|
||||
for note in notes:
|
||||
{
|
||||
Note: play,
|
||||
int: pause
|
||||
}[type(note)](note, bpm)
|
||||
|
||||
|
||||
def play(note, bpm):
|
||||
frequency = note.toFrequency()
|
||||
duration = 60 * 4 / note.duration / bpm
|
||||
duration *= 1.5 if note.dot else 1
|
||||
sine(frequency, duration)
|
||||
|
||||
|
||||
def pause(value, bpm):
|
||||
time.sleep(60 * 4 / value / bpm)
|
||||
|
||||
12
smnp/module/synth/lib/wave.py
Normal file
12
smnp/module/synth/lib/wave.py
Normal file
@@ -0,0 +1,12 @@
|
||||
import time
|
||||
|
||||
import numpy as np
|
||||
import sounddevice as sd
|
||||
|
||||
FS = 44100
|
||||
|
||||
|
||||
def sine(frequency, duration):
|
||||
samples = (np.sin(2*np.pi*np.arange(FS*duration)*frequency/FS)).astype(np.float32)
|
||||
sd.play(samples, FS)
|
||||
time.sleep(duration)
|
||||
Reference in New Issue
Block a user