Move some functions to standard library
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from smnp.module import system, mic, note, iterable, sound, synth, string, util
|
||||
|
||||
functions = [ *system.functions, *mic.functions, *note.functions, *iterable.functions, *sound.functions, *synth.functions, *string.functions, *util.functions ]
|
||||
methods = [ *system.methods, *mic.methods, *note.methods, *iterable.methods, *sound.methods, *synth.methods, *string.functions, *util.methods ]
|
||||
methods = [ *system.methods, *mic.methods, *note.methods, *iterable.methods, *sound.methods, *synth.methods, *string.methods, *util.methods ]
|
||||
@@ -1,4 +1,4 @@
|
||||
from smnp.module.note.function import tuplet, transpose, semitones, octave, duration, interval
|
||||
from smnp.module.note.function import transpose, semitones, interval, note
|
||||
|
||||
functions = [ semitones.function, interval.function, transpose.function, tuplet.function ]
|
||||
methods = [ duration.function, octave.function ]
|
||||
functions = [ semitones.function, interval.function, transpose.function, note.function ]
|
||||
methods = []
|
||||
@@ -1,11 +0,0 @@
|
||||
from smnp.function.model import Function
|
||||
from smnp.function.signature import signature
|
||||
from smnp.type.model import Type
|
||||
from smnp.type.signature.matcher.type import ofType
|
||||
|
||||
_signature = signature(ofType(Type.NOTE), ofType(Type.INTEGER))
|
||||
def _function(env, note, duration):
|
||||
return Type.note(note.value.withDuration(duration.value))
|
||||
|
||||
|
||||
function = Function(_signature, _function, 'withDuration')
|
||||
11
smnp/module/note/function/note.py
Normal file
11
smnp/module/note/function/note.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from smnp.function.model import Function
|
||||
from smnp.function.signature import signature
|
||||
from smnp.note.model import Note
|
||||
from smnp.type.model import Type
|
||||
from smnp.type.signature.matcher.type import ofType
|
||||
|
||||
_signature = signature(ofType(Type.STRING), ofType(Type.INTEGER), ofType(Type.INTEGER), ofType(Type.BOOL))
|
||||
def _function(env, note, octave, duration, dot):
|
||||
return Type.note(Note(note.value, octave.value, duration.value, dot.value))
|
||||
|
||||
function = Function(_signature, _function, 'Note')
|
||||
@@ -1,11 +0,0 @@
|
||||
from smnp.function.model import Function
|
||||
from smnp.function.signature import signature
|
||||
from smnp.type.model import Type
|
||||
from smnp.type.signature.matcher.type import ofType
|
||||
|
||||
_signature = signature(ofType(Type.NOTE), ofType(Type.INTEGER))
|
||||
def _function(env, note, octave):
|
||||
return Type.note(note.value.withOctave(octave.value))
|
||||
|
||||
|
||||
function = Function(_signature, _function, 'withOctave')
|
||||
@@ -1,23 +0,0 @@
|
||||
from smnp.function.model import CombinedFunction, Function
|
||||
from smnp.function.signature import signature, varargSignature
|
||||
from smnp.type.model import Type
|
||||
from smnp.type.signature.matcher.list import listOf
|
||||
from smnp.type.signature.matcher.type import ofTypes
|
||||
|
||||
_signature1 = varargSignature(ofTypes(Type.NOTE), ofTypes(Type.INTEGER), ofTypes(Type.INTEGER))
|
||||
def _function1(env, n, m, vararg):
|
||||
t = [Type.note(arg.value.withDuration(int(arg.value.duration * n.value / m.value))) for arg in vararg]
|
||||
return Type.list(t).decompose()
|
||||
|
||||
|
||||
|
||||
_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(
|
||||
'tuplet',
|
||||
Function(_signature1, _function1),
|
||||
Function(_signature2, _function2)
|
||||
)
|
||||
@@ -1,4 +1,4 @@
|
||||
from smnp.module.string.function import concat
|
||||
from smnp.module.string.function import concat, stringify
|
||||
|
||||
functions = [ concat.function ]
|
||||
methods = []
|
||||
methods = [ stringify.function ]
|
||||
10
smnp/module/string/function/stringify.py
Normal file
10
smnp/module/string/function/stringify.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from smnp.function.model import Function
|
||||
from smnp.function.signature import signature
|
||||
from smnp.type.model import Type
|
||||
from smnp.type.signature.matcher.type import allTypes
|
||||
|
||||
_signature = signature(allTypes())
|
||||
def _function(env, object):
|
||||
return Type.string(object.stringify())
|
||||
|
||||
function = Function(_signature, _function, 'toString')
|
||||
@@ -1,4 +1,4 @@
|
||||
from smnp.module.system.function import sleep, display, debug, exit, type
|
||||
from smnp.module.system.function import sleep, display, displayln, debug, exit, type
|
||||
|
||||
functions = [ debug.function, display.function, exit.function, sleep.function, type.function ]
|
||||
functions = [ debug.function, display.function, displayln.function, exit.function, sleep.function, type.function ]
|
||||
methods = []
|
||||
@@ -4,7 +4,7 @@ from smnp.type.signature.matcher.type import allTypes
|
||||
|
||||
_signature = varargSignature(allTypes())
|
||||
def _function(env, vararg):
|
||||
print("".join([arg.stringify() for arg in vararg]))
|
||||
print("".join([arg.stringify() for arg in vararg]), end="")
|
||||
|
||||
|
||||
function = Function(_signature, _function, 'print')
|
||||
10
smnp/module/system/function/displayln.py
Normal file
10
smnp/module/system/function/displayln.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from smnp.function.model import Function
|
||||
from smnp.function.signature import varargSignature
|
||||
from smnp.type.signature.matcher.type import allTypes
|
||||
|
||||
_signature = varargSignature(allTypes())
|
||||
def _function(env, vararg):
|
||||
print("".join([arg.stringify() for arg in vararg]))
|
||||
|
||||
|
||||
function = Function(_signature, _function, 'println')
|
||||
@@ -1,29 +1,12 @@
|
||||
import random as r
|
||||
import random
|
||||
|
||||
from smnp.error.function import IllegalArgumentException
|
||||
from smnp.function.model import Function, CombinedFunction
|
||||
from smnp.function.signature import varargSignature
|
||||
from smnp.function.model import Function
|
||||
from smnp.function.signature import signature
|
||||
from smnp.type.model import Type
|
||||
from smnp.type.signature.matcher.list import listMatches
|
||||
from smnp.type.signature.matcher.type import ofTypes
|
||||
from smnp.type.signature.matcher.type import ofType
|
||||
|
||||
_signature = signature(ofType(Type.INTEGER), ofType(Type.INTEGER))
|
||||
def _function(env, min, max):
|
||||
return Type.integer(random.randint(min.value, max.value))
|
||||
|
||||
def forType(t):
|
||||
_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:
|
||||
raise IllegalArgumentException("Sum of all percentage values must be equal 100%")
|
||||
for arg in vararg:
|
||||
percent, item = arg.value
|
||||
acc += percent.value
|
||||
if choice <= acc:
|
||||
return item
|
||||
|
||||
return Function(_signature, _function)
|
||||
|
||||
|
||||
function = CombinedFunction('random', *[ forType(t) for t in Type if t != Type.VOID ])
|
||||
|
||||
#TODO: sample
|
||||
function = Function(_signature, _function, 'rand')
|
||||
@@ -16,7 +16,7 @@ class NotePitch(Enum):
|
||||
A = 9
|
||||
AIS = 10
|
||||
H = 11
|
||||
|
||||
|
||||
def toFrequency(self):
|
||||
return {
|
||||
NotePitch.C: 16.35,
|
||||
@@ -32,19 +32,23 @@ class NotePitch(Enum):
|
||||
NotePitch.AIS: 29.17,
|
||||
NotePitch.H: 30.87
|
||||
}[self]
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
def __repr__(self):
|
||||
return self.__str__()
|
||||
|
||||
|
||||
@staticmethod
|
||||
def toPitch(string):
|
||||
try:
|
||||
if string.lower() in stringToPitch:
|
||||
return stringToPitch[string.lower()]
|
||||
except KeyError as e:
|
||||
raise NoteException(f"Note '{string}' does not exist")
|
||||
|
||||
if string.upper() in NotePitch:
|
||||
return NotePitch[string.upper()]
|
||||
|
||||
raise NoteException(f"Note '{string}' does not exist")
|
||||
|
||||
|
||||
stringToPitch = {
|
||||
'cb': NotePitch.H,
|
||||
@@ -67,4 +71,4 @@ stringToPitch = {
|
||||
'a#': NotePitch.AIS,
|
||||
'b': NotePitch.AIS,
|
||||
'h': NotePitch.H
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,11 +21,11 @@ class RelationEvaluator(Evaluator):
|
||||
|
||||
@classmethod
|
||||
def equalOperatorEvaluator(cls, left, operator, right):
|
||||
return Type.bool(left.value == right.value)
|
||||
return Type.bool(left.type == right.type and left.value == right.value)
|
||||
|
||||
@classmethod
|
||||
def notEqualOperatorEvaluator(cls, left, operator, right):
|
||||
return Type.bool(left.value != right.value)
|
||||
return Type.bool(left.type != right.type or left.value != right.value)
|
||||
|
||||
@classmethod
|
||||
def otherRelationOperatorsEvaluator(cls, left, operator, right):
|
||||
|
||||
@@ -51,7 +51,7 @@ class Type(Enum):
|
||||
"pitch": Type.string(str(value.note)),
|
||||
"octave": Type.integer(value.octave),
|
||||
"duration": Type.integer(value.duration),
|
||||
"dot": Type.string('.' if value.dot else '')
|
||||
"dot": Type.bool(value.dot)
|
||||
})
|
||||
|
||||
@staticmethod
|
||||
|
||||
Reference in New Issue
Block a user