Improve environment and library

This commit is contained in:
Bartłomiej Pluta
2019-07-04 15:23:57 +02:00
parent 5f89fca2ac
commit f0cbf37fe9
17 changed files with 98 additions and 211 deletions

View File

@@ -1,75 +1,30 @@
from smnp.environment.environment import Environment from smnp.environment.environment import Environment
from smnp.library.function.combine import combine from smnp.library.function import display, sleep, semitones, interval, combine, flat, wait, rand, tuplet, synth, pause, \
from smnp.library.function.display import display transpose, type, exit, duration, octave
from smnp.library.function.duration import withDuration
from smnp.library.function.exit import exit
from smnp.library.function.flat import flat
from smnp.library.function.interval import interval
from smnp.library.function.mic import wait
from smnp.library.function.octave import withOctave
from smnp.library.function.pause import pause
from smnp.library.function.rand import random
from smnp.library.function.semitones import semitones
from smnp.library.function.sleep import sleep
from smnp.library.function.synth import synth
from smnp.library.function.transpose import transpose
from smnp.library.function.tuplet import tuplet
from smnp.library.function.type import objectType
def createEnvironment(): def createEnvironment():
functions = [ functions = [
display, display.function,
objectType, type.function,
exit, exit.function,
sleep, sleep.function,
semitones, semitones.function,
interval, interval.function,
combine, combine.function,
flat, flat.function,
wait, wait.function,
random, rand.function,
tuplet, tuplet.function,
synth, synth.function,
pause, pause.function,
transpose transpose.function
] ]
methods = [ methods = [
withDuration, duration.function,
withOctave octave.function,
] ]
# 'exit': Function(base.exit, ONLY_FUNCTION),
# 'print': Function(base.display, ONLY_FUNCTION),
# 'read': Function(base.read, ONLY_FUNCTION),
# 'type': Function(base.objectType, ONLY_FUNCTION),
# 'sleep': Function(base.sleep, ONLY_FUNCTION),
# 'synth': Function(synth.synth, ONLY_FUNCTION),
# 'pause': Function(synth.pause, ONLY_FUNCTION),
# 'changeDuration': Function(note.changeDuration, ONLY_FUNCTION),
# 'changeOctave': Function(note.changeOctave, ONLY_FUNCTION),
# 'semitones': Function(interval.semitones, ONLY_FUNCTION),
# 'interval': Function(interval.interval, ONLY_FUNCTION),
# 'transpose': Function(transposer.transpose, ONLY_FUNCTION),
# 'transposeTo': Function(transposer.transposeTo, ONLY_FUNCTION),
# 'random': Function(rand.random, ONLY_FUNCTION),
# # 'sample': sample,
# 'wait': Function(mic.wait, ONLY_FUNCTION),
# 'tuplet': Function(note.tuplet, ONLY_FUNCTION),
# 'combine': Function(l.combine, ONLY_FUNCTION),
# 'flat': Function(l.flat, ONLY_FUNCTION),
# 'debug': Function(lambda args, env: print(args), ONLY_FUNCTION),
# methods = {
# str: {},
# list: {},
# float: {},
# Note: {
# 'synth': synth.synth
# },
# type(None): {},
# }
variables = { variables = {
"bpm": 120 "bpm": 120

View File

@@ -5,8 +5,8 @@ from smnp.library.signature import varargSignature, ofTypes
from smnp.type.model import Type from smnp.type.model import Type
from smnp.type.value import Value from smnp.type.value import Value
_signature = varargSignature(ofTypes(Type.LIST))
def _combine(env, vararg): def _function(env, vararg):
if len(vararg) == 1: if len(vararg) == 1:
return vararg[0] return vararg[0]
@@ -14,9 +14,5 @@ def _combine(env, vararg):
return Value(Type.LIST, combined) return Value(Type.LIST, combined)
function = Function(_signature, _function, 'combine')
_sign = varargSignature(ofTypes(Type.LIST))
combine = Function(_sign, _combine, 'combine')

View File

@@ -2,9 +2,9 @@ from smnp.library.model import Function
from smnp.library.signature import varargSignature, allTypes from smnp.library.signature import varargSignature, allTypes
def _display(env, vararg): _signature = varargSignature(allTypes())
def _function(env, vararg):
print("".join([arg.stringify() for arg in vararg])) print("".join([arg.stringify() for arg in vararg]))
_sign = varargSignature(allTypes())
display = Function(_sign, _display, 'print') function = Function(_signature, _function, 'print')

View File

@@ -4,11 +4,9 @@ from smnp.type.model import Type
from smnp.type.value import Value from smnp.type.value import Value
def _withDuration(env, note, duration): _signature = signature(ofTypes(Type.NOTE), ofTypes(Type.INTEGER))
def _function(env, note, duration):
return Value(Type.NOTE, note.value.withDuration(duration.value)) return Value(Type.NOTE, note.value.withDuration(duration.value))
_sign = signature(ofTypes(Type.NOTE), ofTypes(Type.INTEGER)) function = Function(_signature, _function, 'withDuration')
withDuration = Function(_sign, _withDuration, 'withDuration')

View File

@@ -4,10 +4,9 @@ from smnp.library.model import Function
from smnp.library.signature import signature, ofTypes from smnp.library.signature import signature, ofTypes
from smnp.type.model import Type from smnp.type.model import Type
_signature = signature(ofTypes(Type.INTEGER))
def _exit(env, code): def _function(env, code):
sys.exit(code.value) sys.exit(code.value)
_sign = signature(ofTypes(Type.INTEGER))
exit = Function(_sign, _exit, 'exit') function = Function(_signature, _function, 'exit')

View File

@@ -4,9 +4,11 @@ from smnp.type.model import Type
from smnp.type.value import Value from smnp.type.value import Value
def _flat(env, vararg): _signature = varargSignature(allTypes())
def _function(env, vararg):
return Value(Type.LIST, doFlat(vararg, [])).decompose() return Value(Type.LIST, doFlat(vararg, [])).decompose()
def doFlat(input, output=[]): def doFlat(input, output=[]):
for item in input: for item in input:
if item.type == Type.LIST: if item.type == Type.LIST:
@@ -16,6 +18,4 @@ def doFlat(input, output=[]):
return output return output
_sign = varargSignature(allTypes()) function = Function(_signature, _function, 'flat')
flat = Function(_sign, _flat, 'flat')

View File

@@ -6,7 +6,8 @@ from smnp.type.model import Type
from smnp.type.value import Value from smnp.type.value import Value
def _interval1(env, vararg): _signature1 = varargSignature(ofTypes(Type.NOTE, Type.INTEGER))
def _function1(env, vararg):
withoutPauses = [note.value for note in vararg if note.type == Type.NOTE] withoutPauses = [note.value for note in vararg if note.type == Type.NOTE]
if len(withoutPauses) < 2: if len(withoutPauses) < 2:
return Value(Type.LIST, []) return Value(Type.LIST, [])
@@ -14,17 +15,13 @@ def _interval1(env, vararg):
return Value(Type.LIST, [Value(Type.STRING, intervalToString(s)) for s in semitones]).decompose() return Value(Type.LIST, [Value(Type.STRING, intervalToString(s)) for s in semitones]).decompose()
_sign1 = varargSignature(ofTypes(Type.NOTE, Type.INTEGER)) _signature2 = varargSignature(listOf(Type.NOTE, Type.INTEGER))
def _function2(env, vararg):
return Value(Type.LIST, [_function1(env, arg.value) for arg in vararg]).decompose()
def _interval2(env, vararg): function = CombinedFunction(
return Value(Type.LIST, [_interval1(env, arg.value) for arg in vararg]).decompose()
_sign2 = varargSignature(listOf(Type.NOTE, Type.INTEGER))
interval = CombinedFunction(
'interval', 'interval',
Function(_sign1, _interval1), Function(_signature1, _function1),
Function(_sign2, _interval2) Function(_signature2, _function2)
) )

View File

@@ -4,11 +4,9 @@ from smnp.type.model import Type
from smnp.type.value import Value from smnp.type.value import Value
def _withOctave(env, note, octave): _signature = signature(ofTypes(Type.NOTE), ofTypes(Type.INTEGER))
def _function(env, note, octave):
return Value(Type.NOTE, note.value.withOctave(octave.value)) return Value(Type.NOTE, note.value.withOctave(octave.value))
_sign = signature(ofTypes(Type.NOTE), ofTypes(Type.INTEGER)) function = Function(_signature, _function, 'withOctave')
withOctave = Function(_sign, _withOctave, 'withOctave')

View File

@@ -4,12 +4,10 @@ from smnp.synth import player
from smnp.type.model import Type from smnp.type.model import Type
def _pause(env, value): _signature = signature(ofTypes(Type.INTEGER))
def _function(env, value):
bpm = env.findVariable('bpm') bpm = env.findVariable('bpm')
player.pause(value.value, bpm) player.pause(value.value, bpm)
_sign = signature(ofTypes(Type.INTEGER)) function = Function(_signature, _function, 'pause')
pause = Function(_sign, _pause, 'pause')

View File

@@ -7,7 +7,8 @@ from smnp.type.model import Type
def forType(t): def forType(t):
def _random(env, vararg): _signature = varargSignature(listMatches(ofTypes(Type.PERCENT), ofTypes(t)))
def _function(env, vararg):
choice = r.random() choice = r.random()
acc = 0 acc = 0
if sum(arg.value[0].value for arg in vararg) != 1.0: if sum(arg.value[0].value for arg in vararg) != 1.0:
@@ -18,24 +19,9 @@ def forType(t):
if choice <= acc: if choice <= acc:
return item return item
_sign = varargSignature(listMatches(ofTypes(Type.PERCENT), ofTypes(t))) return Function(_signature, _function)
return Function(_sign, _random)
random = CombinedFunction('random', *[ forType(t) for t in Type if t != Type.VOID ]) function = CombinedFunction('random', *[ forType(t) for t in Type if t != Type.VOID ])
#
# def random(args, env):
# if not all(isinstance(x, list) and len(x) == 2 and isinstance(x[0], float) for x in args):
# return # not valid signature
# if sum([x[0] for x in args]) != 1.0:
# return # not sums to 100%
# choice = r.random()
# acc = 0
# for e in args:
# acc += e[0]
# if choice <= acc:
# return e[1]
#
#TODO: sample #TODO: sample

View File

@@ -5,25 +5,21 @@ from smnp.type.model import Type
from smnp.type.value import Value from smnp.type.value import Value
def _semitones1(env, vararg): _signature1 = varargSignature(ofTypes(Type.NOTE, Type.INTEGER))
def _function1(env, vararg):
withoutPauses = [note.value for note in vararg if note.type == Type.NOTE] withoutPauses = [note.value for note in vararg if note.type == Type.NOTE]
if len(withoutPauses) < 2: if len(withoutPauses) < 2:
return Value(Type.LIST, []) return Value(Type.LIST, [])
return Value(Type.LIST, [Value(Type.INTEGER, Note.checkInterval(withoutPauses[i-1], withoutPauses[i])) for i in range(1, len(withoutPauses))]).decompose() return Value(Type.LIST, [Value(Type.INTEGER, Note.checkInterval(withoutPauses[i-1], withoutPauses[i])) for i in range(1, len(withoutPauses))]).decompose()
_sign1 = varargSignature(ofTypes(Type.NOTE, Type.INTEGER)) _signature2 = varargSignature(listOf(Type.NOTE, Type.INTEGER))
def _function2(env, vararg):
return Value(Type.LIST, [_function1(env, arg.value) for arg in vararg]).decompose()
def _semitones2(env, vararg): function = CombinedFunction(
return Value(Type.LIST, [_semitones1(env, arg.value) for arg in vararg]).decompose()
_sign2 = varargSignature(listOf(Type.NOTE, Type.INTEGER))
semitones = CombinedFunction(
"semitones", "semitones",
Function(_sign1, _semitones1), Function(_signature1, _function1),
Function(_sign2, _semitones2), Function(_signature2, _function2),
) )

View File

@@ -4,10 +4,9 @@ from smnp.library.model import Function
from smnp.library.signature import ofTypes, signature from smnp.library.signature import ofTypes, signature
from smnp.type.model import Type from smnp.type.model import Type
_signature = signature(ofTypes(Type.INTEGER))
def _sleep(env, value): def _function(env, value):
time.sleep(value.value) time.sleep(value.value)
_sign = signature(ofTypes(Type.INTEGER))
sleep = Function(_sign, _sleep, 'sleep') function = Function(_signature, _function, 'sleep')

View File

@@ -4,25 +4,21 @@ from smnp.synth.player import playNotes
from smnp.type.model import Type from smnp.type.model import Type
def _synth1(env, vararg): _signature1 = varargSignature(ofTypes(Type.NOTE, Type.INTEGER))
def _function1(env, vararg):
notes = [arg.value for arg in vararg] notes = [arg.value for arg in vararg]
bpm = env.findVariable('bpm') bpm = env.findVariable('bpm')
playNotes(notes, bpm) playNotes(notes, bpm)
_sign1 = varargSignature(ofTypes(Type.NOTE, Type.INTEGER)) _signature2 = varargSignature(listOf(Type.NOTE, Type.INTEGER))
def _function2(env, vararg):
def _synth2(env, vararg):
for arg in vararg: for arg in vararg:
_synth1(env, arg.value) _function1(env, arg.value)
_sign2 = varargSignature(listOf(Type.NOTE, Type.INTEGER)) function = CombinedFunction(
synth = CombinedFunction(
'synth', 'synth',
Function(_sign1, _synth1), Function(_signature1, _function1),
Function(_sign2, _synth2) Function(_signature2, _function2)
) )

View File

@@ -4,23 +4,19 @@ from smnp.type.model import Type
from smnp.type.value import Value from smnp.type.value import Value
def _transpose1(env, value, vararg): _signature1 = varargSignature(ofTypes(Type.INTEGER, Type.NOTE), ofTypes(Type.INTEGER))
def _function1(env, value, vararg):
transposed = [Value(Type.NOTE, arg.value.transpose(value.value)) if arg.type == Type.NOTE else arg for arg in vararg] transposed = [Value(Type.NOTE, arg.value.transpose(value.value)) if arg.type == Type.NOTE else arg for arg in vararg]
return Value(Type.LIST, transposed).decompose() return Value(Type.LIST, transposed).decompose()
_sign1 = varargSignature(ofTypes(Type.INTEGER, Type.NOTE), ofTypes(Type.INTEGER)) _signature2 = varargSignature(listOf(Type.INTEGER, Type.NOTE), ofTypes(Type.INTEGER))
def _function2(env, value, vararg):
return Value(Type.LIST, [_function1(env, value, arg.value) for arg in vararg]).decompose()
def _transpose2(env, value, vararg): function = CombinedFunction(
return Value(Type.LIST, [_transpose1(env, value, arg.value) for arg in vararg]).decompose()
_sign2 = varargSignature(listOf(Type.INTEGER, Type.NOTE), ofTypes(Type.INTEGER))
transpose = CombinedFunction(
'transpose', 'transpose',
Function(_sign1, _transpose1), Function(_signature1, _function1),
Function(_sign2, _transpose2) Function(_signature2, _function2)
) )

View File

@@ -4,41 +4,20 @@ from smnp.type.model import Type
from smnp.type.value import Value from smnp.type.value import Value
def _tuplet1(env, n, m, vararg): _signature1 = varargSignature(ofTypes(Type.NOTE), ofTypes(Type.INTEGER), ofTypes(Type.INTEGER))
def _function1(env, n, m, vararg):
t = [Value(Type.NOTE, arg.value.withDuration(arg.value.duration * n.value / m.value)) for arg in vararg] t = [Value(Type.NOTE, arg.value.withDuration(arg.value.duration * n.value / m.value)) for arg in vararg]
return Value(Type.LIST, t).decompose() return Value(Type.LIST, t).decompose()
_sign1 = varargSignature(ofTypes(Type.NOTE), ofTypes(Type.INTEGER), ofTypes(Type.INTEGER))
_signature2 = signature(ofTypes(Type.INTEGER), ofTypes(Type.INTEGER), listOf(Type.NOTE))
def _function2(env, n, m, notes):
return _function1(env, n, m, notes.value)
function = CombinedFunction(
def _tuplet2(env, n, m, notes):
return _tuplet1(env, n, m, notes.value)
_sign2 = signature(ofTypes(Type.INTEGER), ofTypes(Type.INTEGER), listOf(Type.NOTE))
tuplet = CombinedFunction(
'tuplet', 'tuplet',
Function(_sign1, _tuplet1), Function(_function1, _function1),
Function(_sign2, _tuplet2) Function(_function2, _function2)
) )
# def tupletList(n, m, list):
# return [note.withDuration(note.duration * n / m) for note in list]
#
#
# def tuplet(args, env):
# if len(args) > 2 and type(args[0]) == int and type(args[1]) == int and all(type(x) == Note for x in args[2:]):
# n = args[0] # how many notes
# m = args[1] # instead of number of notes (3-tuplet: 3 instead 2; 5-tuplet: 5 instead 4 etc.)
# return returnElementOrList(tupletList(n, m, args[2:]))
# elif len(args) == 3 and type(args[0]) == int and type(args[1]) == int and type(args[2]) == list and all(type(x) == Note for x in args[2]):
# n = args[0]
# m = args[1]
# l = args[2]
# return returnElementOrList(tupletList(n, m, l))
# else:
# pass # not valid signature

View File

@@ -2,11 +2,9 @@ from smnp.library.model import Function
from smnp.library.signature import signature, allTypes from smnp.library.signature import signature, allTypes
def _objectType(env, obj): _signature = signature(allTypes())
def _function(env, obj):
return obj.type.name return obj.type.name
_sign = signature(allTypes()) function = Function(_signature, _function, 'type')
objectType = Function(_sign, _objectType, 'type')

View File

@@ -4,25 +4,21 @@ from smnp.mic.detector.noise import NoiseDetector
from smnp.type.model import Type from smnp.type.model import Type
def _wait1(env): _signature1 = signature()
def _function1(env):
nd = NoiseDetector() nd = NoiseDetector()
nd.waitForComplete() nd.waitForComplete()
_sign1 = signature() _signature2 = signature(ofTypes(Type.INTEGER), ofTypes(Type.INTEGER))
def _function2(env, noiseTreshold, silenceTreshold):
def _wait2(env, noiseTreshold, silenceTreshold):
nd = NoiseDetector(noiseTreshold.value, silenceTreshold.value) nd = NoiseDetector(noiseTreshold.value, silenceTreshold.value)
nd.waitForComplete() nd.waitForComplete()
_sign2 = signature(ofTypes(Type.INTEGER), ofTypes(Type.INTEGER)) function = CombinedFunction(
wait = CombinedFunction(
'wait', 'wait',
Function(_sign1, _wait1), Function(_signature1, _function1),
Function(_sign2, _wait2) Function(_signature2, _function2)
) )