Create audio module which allows to play sound files
This commit is contained in:
0
smnp/audio/__init__.py
Normal file
0
smnp/audio/__init__.py
Normal file
17
smnp/audio/sound.py
Normal file
17
smnp/audio/sound.py
Normal 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__()
|
||||||
@@ -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
0
smnp/error/audio.py
Normal file
10
smnp/library/function/play.py
Normal file
10
smnp/library/function/play.py
Normal 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')
|
||||||
10
smnp/library/function/sound.py
Normal file
10
smnp/library/function/sound.py
Normal 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')
|
||||||
@@ -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, {})
|
||||||
|
|||||||
Reference in New Issue
Block a user