Create audio module which allows to play sound files

This commit is contained in:
Bartłomiej Pluta
2019-07-09 19:04:55 +02:00
parent b786241f12
commit fc023f8a5d
7 changed files with 49 additions and 1 deletions

0
smnp/audio/__init__.py Normal file
View File

17
smnp/audio/sound.py Normal file
View File

@@ -0,0 +1,17 @@
import sounddevice as sd
import soundfile as sf
class Sound:
def __init__(self, file):
self.file = file
self.data, self.fs = sf.read(file, dtype='float32')
def play(self):
sd.play(self.data, self.fs, blocking=True)
def __str__(self):
return f"sound[{self.file}]"
def __repr__(self):
return self.__str__()

View File

@@ -1,6 +1,6 @@
from smnp.environment.environment import Environment from smnp.environment.environment import Environment
from smnp.library.function import display, sleep, semitones, interval, combine, flat, wait, rand, tuplet, synth, pause, \ from smnp.library.function import display, sleep, semitones, interval, combine, flat, wait, rand, tuplet, synth, pause, \
transpose, type, exit, duration, octave, debug, get transpose, type, exit, duration, octave, debug, get, sound, play
from smnp.type.model import Type from smnp.type.model import Type
@@ -20,12 +20,14 @@ def createEnvironment():
synth.function, synth.function,
pause.function, pause.function,
transpose.function, transpose.function,
sound.function,
debug.function debug.function
] ]
methods = [ methods = [
duration.function, duration.function,
octave.function, octave.function,
play.function,
get.function get.function
] ]

0
smnp/error/audio.py Normal file
View File

View File

@@ -0,0 +1,10 @@
from smnp.library.model import Function
from smnp.library.signature import signature, ofType
from smnp.type.model import Type
_signature = signature(ofType(Type.SOUND))
def _function(env, sound):
sound.value.play()
function = Function(_signature, _function, 'play')

View File

@@ -0,0 +1,10 @@
from smnp.audio.sound import Sound
from smnp.library.model import Function
from smnp.library.signature import signature, ofType
from smnp.type.model import Type
_signature = signature(ofType(Type.STRING))
def _function(env, file):
return Type.sound(Sound(file.value))
function = Function(_signature, _function, 'Sound')

View File

@@ -1,5 +1,6 @@
from enum import Enum from enum import Enum
from smnp.audio.sound import Sound
from smnp.error.runtime import RuntimeException from smnp.error.runtime import RuntimeException
from smnp.note.model import Note from smnp.note.model import Note
from smnp.type.value import Value from smnp.type.value import Value
@@ -12,6 +13,7 @@ class Type(Enum):
MAP = (dict, lambda x: '{' + ', '.join(f"'{k.stringify()}' -> '{v.stringify()}'" for k, v in x.items()) + '}') MAP = (dict, lambda x: '{' + ', '.join(f"'{k.stringify()}' -> '{v.stringify()}'" for k, v in x.items()) + '}')
PERCENT = (float, lambda x: f"{int(x * 100)}%") PERCENT = (float, lambda x: f"{int(x * 100)}%")
NOTE = (Note, lambda x: x.note.name) NOTE = (Note, lambda x: x.note.name)
SOUND = (Sound, lambda x: x.file)
TYPE = (None, lambda x: str(x.type.name.lower())) TYPE = (None, lambda x: str(x.type.name.lower()))
VOID = (type(None), lambda x: _failStringify(Type.VOID)) VOID = (type(None), lambda x: _failStringify(Type.VOID))
@@ -48,6 +50,13 @@ class Type(Enum):
"dot": Type.string('.' if value.dot else '') "dot": Type.string('.' if value.dot else '')
}) })
@staticmethod
def sound(value):
return Value(Type.SOUND, value, {
"file": Type.string(value.file),
"fs": Type.integer(value.fs)
})
@staticmethod @staticmethod
def type(value): def type(value):
return Value(Type.TYPE, value, {}) return Value(Type.TYPE, value, {})