Create 'transpose' function
This commit is contained in:
@@ -12,6 +12,7 @@ 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
|
||||
|
||||
@@ -30,7 +31,8 @@ def createEnvironment():
|
||||
random,
|
||||
tuplet,
|
||||
synth,
|
||||
pause
|
||||
pause,
|
||||
transpose
|
||||
]
|
||||
|
||||
methods = [
|
||||
|
||||
26
smnp/library/function/transpose.py
Normal file
26
smnp/library/function/transpose.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from smnp.library.model import CombinedFunction, Function
|
||||
from smnp.library.signature import varargSignature, ofTypes, listOf
|
||||
from smnp.type.model import Type
|
||||
from smnp.type.value import Value
|
||||
|
||||
|
||||
def _transpose1(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))
|
||||
|
||||
|
||||
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(
|
||||
'transpose',
|
||||
Function(_sign1, _transpose1),
|
||||
Function(_sign2, _transpose2)
|
||||
)
|
||||
@@ -1,42 +0,0 @@
|
||||
from smnp.library.tools import returnElementOrList
|
||||
|
||||
from smnp.library.function.semitones import semitonesList
|
||||
from smnp.note.model import Note
|
||||
|
||||
|
||||
def transposeTo(args, env):
|
||||
if len(args) > 1 and isinstance(args[0], Note) and all(isinstance(x, list) for i, x in enumerate(args) if i != 0):
|
||||
target = args[0]
|
||||
result = []
|
||||
for i, notes in enumerate(args):
|
||||
if i == 0:
|
||||
continue
|
||||
if len(notes) > 0:
|
||||
first = notes[0]
|
||||
semitones = semitonesList([first, target])[0]
|
||||
result.append([note.transpose(semitones) for note in notes if isinstance(note, Note)])
|
||||
else:
|
||||
result.append([])
|
||||
return returnElementOrList(result)
|
||||
else:
|
||||
pass # not valid signature
|
||||
|
||||
|
||||
def transpose(args, env):
|
||||
if len(args) > 1 and isinstance(args[0], int) and all(
|
||||
isinstance(arg, list) for i, arg in enumerate(args) if i != 0):
|
||||
value = args[0]
|
||||
transposed = []
|
||||
for i, arg in enumerate(args):
|
||||
if i == 0:
|
||||
continue
|
||||
if not isinstance(arg, list):
|
||||
return # is not list
|
||||
transposed.append([note.transpose(value) for note in arg if isinstance(note, Note)])
|
||||
return returnElementOrList(transposed)
|
||||
if len(args) > 1 and all(isinstance(arg, Note) for i, arg in enumerate(args) if i != 0):
|
||||
value = args[0]
|
||||
transposed = [note.transpose(value) for i, note in enumerate(args) if i != 0]
|
||||
return returnElementOrList(transposed)
|
||||
else:
|
||||
return # not valid signature
|
||||
@@ -5,6 +5,8 @@ from smnp.library.signature import signature, allTypes
|
||||
def _objectType(env, obj):
|
||||
return obj.type.name
|
||||
|
||||
|
||||
_sign = signature(allTypes())
|
||||
|
||||
|
||||
objectType = Function(_sign, _objectType, 'type')
|
||||
Reference in New Issue
Block a user