Add 'read' function
This commit is contained in:
@@ -6,6 +6,8 @@ import Synth
|
||||
import time
|
||||
from Error import RuntimeException
|
||||
from NoiseDetector import waitForSound
|
||||
from Parser import parseNote
|
||||
from Tokenizer import Token, TokenType, tokenizeNote
|
||||
|
||||
types = {
|
||||
int: 'integer',
|
||||
@@ -63,18 +65,18 @@ def semitonesList(list):
|
||||
pass # invalid arguments
|
||||
withoutPauses = tuple(filter(lambda x: isinstance(x, Note), list))
|
||||
r = [Note.checkInterval(withoutPauses[i-1], withoutPauses[i]) for i, _ in enumerate(withoutPauses) if i != 0]
|
||||
return returnElementOrList(r)
|
||||
return r
|
||||
|
||||
def returnElementOrList(list):
|
||||
return list[0] if len(list) == 1 else list
|
||||
|
||||
def semitones(args, env):
|
||||
if len(args) > 0 and isinstance(args[0], list):
|
||||
return semitonesList(args[0])
|
||||
return semitonesList(args)
|
||||
return returnElementOrList(semitonesList(args[0]))
|
||||
return returnElementOrList(semitonesList(args))
|
||||
|
||||
def intervalList(list):
|
||||
r = [intervalToString(x) for x in list]
|
||||
r = [intervalToString(x) for x in semitonesList(list)]
|
||||
return returnElementOrList(r)
|
||||
|
||||
def interval(args, env):
|
||||
@@ -91,7 +93,7 @@ def transposeTo(args, env):
|
||||
continue
|
||||
if len(notes) > 0:
|
||||
first = notes[0]
|
||||
semitones = semitonesList([target, first])
|
||||
semitones = semitonesList([first, target])
|
||||
result.append([note.transpose(semitones) for note in notes if isinstance(note, Note)])
|
||||
else:
|
||||
result.append([])
|
||||
@@ -148,6 +150,42 @@ def rand(args, env):
|
||||
if choice <= acc:
|
||||
return e[1]
|
||||
|
||||
def read(args, env):
|
||||
if len(args) == 2 and isinstance(args[0], str) and isinstance(args[1], str):
|
||||
print(args[0], end="")
|
||||
value = input()
|
||||
if args[1] == "integer":
|
||||
try:
|
||||
return int(value)
|
||||
except ValueError as v:
|
||||
pass # not int
|
||||
elif args[1] == "string":
|
||||
return value
|
||||
elif args[1] == "note":
|
||||
chars, token = tokenizeNote(value, 0, 0)
|
||||
if chars == 0:
|
||||
return # not note
|
||||
return parseNote([token], None).value
|
||||
else:
|
||||
pass # invalid type
|
||||
elif len(args) == 1 and isinstance(args[0], str):
|
||||
print(args[0], end="")
|
||||
return input()
|
||||
elif len(args) == 0:
|
||||
return input()
|
||||
else:
|
||||
pass # not valid signature
|
||||
|
||||
def changeDuration(args, env):
|
||||
if len(args) == 2 and isinstance(args[0], Note) and isinstance(args[1], int):
|
||||
return args[0].withDuration(args[1])
|
||||
return # invalid signature
|
||||
|
||||
def changeOctave(args, env):
|
||||
if len(args) == 2 and isinstance(args[0], Note) and isinstance(args[1], int):
|
||||
return args[0].withOctave(args[1])
|
||||
return # invalid signature
|
||||
|
||||
def createEnvironment():
|
||||
functions = {
|
||||
'print': doPrint,
|
||||
@@ -161,7 +199,10 @@ def createEnvironment():
|
||||
'transposeTo': transposeTo,
|
||||
'sleep': sleep,
|
||||
'random': rand,
|
||||
'changeDuration': changeDuration,
|
||||
'changeOctave': changeOctave,
|
||||
'wait': waitForSound,
|
||||
'read': read,
|
||||
'exit': exit
|
||||
|
||||
}
|
||||
|
||||
26
Note.py
26
Note.py
@@ -74,6 +74,12 @@ class Note:
|
||||
transposedIntRepr = origIntRepr + interval
|
||||
return Note._fromIntRepr(transposedIntRepr, self.duration)
|
||||
|
||||
def withDuration(self, duration):
|
||||
return Note(self.note, self.octave, duration)
|
||||
|
||||
def withOctave(self, octave):
|
||||
return Note(self.note, octave, self.duration)
|
||||
|
||||
def _intRepr(self):
|
||||
return self.octave * len(NotePitch) + self.note.value
|
||||
|
||||
@@ -92,7 +98,7 @@ class Note:
|
||||
|
||||
@staticmethod
|
||||
def checkInterval(a, b):
|
||||
return a._intRepr() - b._intRepr()
|
||||
return b._intRepr() - a._intRepr()
|
||||
|
||||
@staticmethod
|
||||
def range(a, b):
|
||||
@@ -103,16 +109,16 @@ def intervalToString(interval):
|
||||
pitchInterval = abs(interval) % len(NotePitch)
|
||||
pitchIntervalName = {
|
||||
0: "1",
|
||||
1: "1>/2<",
|
||||
2: "2",
|
||||
3: "3<",
|
||||
4: "3>",
|
||||
1: "2m",
|
||||
2: "2M",
|
||||
3: "3m",
|
||||
4: "3M",
|
||||
5: "4",
|
||||
6: "4>/5<",
|
||||
6: "5d/4A",
|
||||
7: "5",
|
||||
8: "6<",
|
||||
9: "6>",
|
||||
10: "7<",
|
||||
11: "7>"
|
||||
8: "6m",
|
||||
9: "6M",
|
||||
10: "7m",
|
||||
11: "7M"
|
||||
}
|
||||
return (str(pitchIntervalName[pitchInterval]) + (f"(+{octaveInterval}')" if octaveInterval > 0 else ""))
|
||||
|
||||
Reference in New Issue
Block a user