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.library.function.combine import combine
from smnp.library.function.display import display
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
from smnp.library.function import display, sleep, semitones, interval, combine, flat, wait, rand, tuplet, synth, pause, \
transpose, type, exit, duration, octave
def createEnvironment():
functions = [
display,
objectType,
exit,
sleep,
semitones,
interval,
combine,
flat,
wait,
random,
tuplet,
synth,
pause,
transpose
display.function,
type.function,
exit.function,
sleep.function,
semitones.function,
interval.function,
combine.function,
flat.function,
wait.function,
rand.function,
tuplet.function,
synth.function,
pause.function,
transpose.function
]
methods = [
withDuration,
withOctave
duration.function,
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 = {
"bpm": 120

View File

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

View File

@@ -2,9 +2,9 @@ from smnp.library.model import Function
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]))
_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
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))
_sign = signature(ofTypes(Type.NOTE), ofTypes(Type.INTEGER))
withDuration = Function(_sign, _withDuration, 'withDuration')
function = Function(_signature, _function, 'withDuration')

View File

@@ -4,10 +4,9 @@ from smnp.library.model import Function
from smnp.library.signature import signature, ofTypes
from smnp.type.model import Type
def _exit(env, code):
_signature = signature(ofTypes(Type.INTEGER))
def _function(env, code):
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
def _flat(env, vararg):
_signature = varargSignature(allTypes())
def _function(env, vararg):
return Value(Type.LIST, doFlat(vararg, [])).decompose()
def doFlat(input, output=[]):
for item in input:
if item.type == Type.LIST:
@@ -16,6 +18,4 @@ def doFlat(input, output=[]):
return output
_sign = varargSignature(allTypes())
flat = Function(_sign, _flat, 'flat')
function = Function(_signature, _function, 'flat')

View File

@@ -6,7 +6,8 @@ from smnp.type.model import Type
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]
if len(withoutPauses) < 2:
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()
_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):
return Value(Type.LIST, [_interval1(env, arg.value) for arg in vararg]).decompose()
_sign2 = varargSignature(listOf(Type.NOTE, Type.INTEGER))
interval = CombinedFunction(
function = CombinedFunction(
'interval',
Function(_sign1, _interval1),
Function(_sign2, _interval2)
Function(_signature1, _function1),
Function(_signature2, _function2)
)

View File

@@ -4,11 +4,9 @@ from smnp.type.model import Type
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))
_sign = signature(ofTypes(Type.NOTE), ofTypes(Type.INTEGER))
withOctave = Function(_sign, _withOctave, 'withOctave')
function = Function(_signature, _function, 'withOctave')

View File

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

View File

@@ -7,7 +7,8 @@ from smnp.type.model import Type
def forType(t):
def _random(env, vararg):
_signature = varargSignature(listMatches(ofTypes(Type.PERCENT), ofTypes(t)))
def _function(env, vararg):
choice = r.random()
acc = 0
if sum(arg.value[0].value for arg in vararg) != 1.0:
@@ -18,24 +19,9 @@ def forType(t):
if choice <= acc:
return item
_sign = varargSignature(listMatches(ofTypes(Type.PERCENT), ofTypes(t)))
return Function(_sign, _random)
return Function(_signature, _function)
random = 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]
#
function = CombinedFunction('random', *[ forType(t) for t in Type if t != Type.VOID ])
#TODO: sample

View File

@@ -5,25 +5,21 @@ from smnp.type.model import Type
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]
if len(withoutPauses) < 2:
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()
_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):
return Value(Type.LIST, [_semitones1(env, arg.value) for arg in vararg]).decompose()
_sign2 = varargSignature(listOf(Type.NOTE, Type.INTEGER))
semitones = CombinedFunction(
function = CombinedFunction(
"semitones",
Function(_sign1, _semitones1),
Function(_sign2, _semitones2),
Function(_signature1, _function1),
Function(_signature2, _function2),
)

View File

@@ -4,10 +4,9 @@ from smnp.library.model import Function
from smnp.library.signature import ofTypes, signature
from smnp.type.model import Type
def _sleep(env, value):
_signature = signature(ofTypes(Type.INTEGER))
def _function(env, 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
def _synth1(env, vararg):
_signature1 = varargSignature(ofTypes(Type.NOTE, Type.INTEGER))
def _function1(env, vararg):
notes = [arg.value for arg in vararg]
bpm = env.findVariable('bpm')
playNotes(notes, bpm)
_sign1 = varargSignature(ofTypes(Type.NOTE, Type.INTEGER))
def _synth2(env, vararg):
_signature2 = varargSignature(listOf(Type.NOTE, Type.INTEGER))
def _function2(env, vararg):
for arg in vararg:
_synth1(env, arg.value)
_function1(env, arg.value)
_sign2 = varargSignature(listOf(Type.NOTE, Type.INTEGER))
synth = CombinedFunction(
function = CombinedFunction(
'synth',
Function(_sign1, _synth1),
Function(_sign2, _synth2)
Function(_signature1, _function1),
Function(_signature2, _function2)
)

View File

@@ -4,23 +4,19 @@ from smnp.type.model import Type
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]
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):
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(
function = CombinedFunction(
'transpose',
Function(_sign1, _transpose1),
Function(_sign2, _transpose2)
Function(_signature1, _function1),
Function(_signature2, _function2)
)

View File

@@ -4,41 +4,20 @@ from smnp.type.model import Type
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]
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)
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(
function = CombinedFunction(
'tuplet',
Function(_sign1, _tuplet1),
Function(_sign2, _tuplet2)
Function(_function1, _function1),
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
def _objectType(env, obj):
_signature = signature(allTypes())
def _function(env, obj):
return obj.type.name
_sign = signature(allTypes())
objectType = Function(_sign, _objectType, 'type')
function = Function(_signature, _function, 'type')

View File

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