From ef59547993300299ff4d23d381855286df22c37c Mon Sep 17 00:00:00 2001 From: Raphael Roberts Date: Fri, 2 Nov 2018 00:34:14 -0500 Subject: [PATCH] added version 1 files --- Assets/device.txt | 42 +++++++++++++++++++ Assets/notes.txt | 30 +++++++++++++ Assets/scales.txt | 104 ++++++++++++++++++++++++++++++++++++++++++++++ Fingering.py | 26 ++++++++++++ add_device.py | 17 ++++++++ convert_to.py | 39 +++++++++++++++++ load_asset.py | 13 ++++++ main.py | 75 +++++++++++++++++++++++++++++++++ temp.py | 8 ++++ 9 files changed, 354 insertions(+) create mode 100644 Assets/device.txt create mode 100644 Assets/notes.txt create mode 100644 Assets/scales.txt create mode 100644 Fingering.py create mode 100644 add_device.py create mode 100644 convert_to.py create mode 100644 load_asset.py create mode 100644 main.py create mode 100644 temp.py diff --git a/Assets/device.txt b/Assets/device.txt new file mode 100644 index 0000000..d4deb84 --- /dev/null +++ b/Assets/device.txt @@ -0,0 +1,42 @@ +{ + "devices": { + "baritone": { + "fund": "A#|1", + "sys": "valves" + }, + "french_horn": { + "fund": "F|1", + "sys": "valves" + }, + "trombone": { + "fund": "A#|1", + "sys": "slide" + }, + "trumpet": { + "fund": "A#|2", + "sys": "valves" + }, + "tuba": { + "fund": "A#|0", + "sys": "valves" + } + }, + "slide": [ + "1", + "2", + "3", + "4", + "5", + "6", + "7" + ], + "valves": [ + "0", + "2", + "1", + "1,2", + "2,3", + "1,3", + "1,2,3" + ] +} \ No newline at end of file diff --git a/Assets/notes.txt b/Assets/notes.txt new file mode 100644 index 0000000..0eca525 --- /dev/null +++ b/Assets/notes.txt @@ -0,0 +1,30 @@ +{ + "flats": [ + "C", + "D\u266d", + "D", + "E\u266d", + "E", + "F", + "G\u266d", + "G", + "A\u266d", + "A", + "B\u266d", + "B" + ], + "sharps": [ + "C", + "C#", + "D", + "D#", + "E", + "F", + "F#", + "G", + "G#", + "A", + "A#", + "B" + ] +} \ No newline at end of file diff --git a/Assets/scales.txt b/Assets/scales.txt new file mode 100644 index 0000000..b1614a7 --- /dev/null +++ b/Assets/scales.txt @@ -0,0 +1,104 @@ +[ + { + "aeolian": 5, + "dominant": 4, + "dorian": 1, + "ionian": 0, + "locrian": 6, + "lydian": 3, + "major": 0, + "minor": 5, + "mixolydian": 4, + "pentatonic major": 8, + "pentatonic minor": 7, + "phrygian": 2 + }, + [ + [ + "C", + "D", + "E", + "F", + "G", + "A", + "B", + "C|1" + ], + [ + "C", + "D", + "E\u266d", + "F", + "G", + "A", + "B\u266d", + "C|1" + ], + [ + "C", + "D\u266d", + "E\u266d", + "F", + "G", + "A\u266d", + "B\u266d", + "C|1" + ], + [ + "C", + "D", + "E", + "G\u266d", + "G", + "A", + "B", + "C|1" + ], + [ + "C", + "D", + "E", + "F", + "G", + "A", + "B\u266d", + "C|1" + ], + [ + "C", + "D", + "E\u266d", + "F", + "G", + "A\u266d", + "B\u266d", + "C|1" + ], + [ + "C", + "D\u266d", + "E\u266d", + "F", + "G\u266d", + "A\u266d", + "B\u266d", + "C|1" + ], + [ + "C", + "E\u266d", + "F", + "G", + "B\u266d", + "C|1" + ], + [ + "C", + "E", + "F", + "G", + "B", + "C|1" + ] + ] +] \ No newline at end of file diff --git a/Fingering.py b/Fingering.py new file mode 100644 index 0000000..eef6dc0 --- /dev/null +++ b/Fingering.py @@ -0,0 +1,26 @@ +from load_asset import load +from math import log +import convert_to +device = load('device') +#cents between notes +def cents(f1,f2): + + return 1200*log(f1/f2,2) + +#fingering based off of valves or slide +def fingering(note,dev,_a = 440): + dev = device['devices'][dev] + fund = convert_to.frequency(convert_to.above_c(dev['fund'])) + sys = device[dev['sys']] + + f_note = convert_to.frequency(convert_to.above_c(note),a =_a) + n = int(f_note/fund) + t = abs(cents(f_note,n*fund)) + if t >= 31: + n += 1 + if n == 7: + n += 1 + try: + return sys[round(convert_to.frequency(n*fund,invert = True,a = _a)) - convert_to.above_c(note)] + except IndexError: + return 'Note too damn low' diff --git a/add_device.py b/add_device.py new file mode 100644 index 0000000..dd23381 --- /dev/null +++ b/add_device.py @@ -0,0 +1,17 @@ + +from load_asset import save,load +device = load('device') + +def add_device(name,fund,sys): + device['devices'][name] = {} + device['devices'][name]['fund'] = fund.upper() + device['devices'][name]['sys'] = sys + return None +t = '' +while t != 'exit': + t = t.split(' ') + if len(t) == 3: + add_device(*t) + t = input('(name fund sys) (or exit):') + +save(device,'device') diff --git a/convert_to.py b/convert_to.py new file mode 100644 index 0000000..87dfdfd --- /dev/null +++ b/convert_to.py @@ -0,0 +1,39 @@ +from load_asset import load +from math import log +notes = load('notes') + +#converts from above c0 to frequency in hertz or the inverse +def frequency(n,a = 440,invert = False): + c = a/(2**(57/12)) + if not invert: + return (2**(n/12))*c + else: + return 12 * log(n/c,2) + +#swaps from nn to above c or inverse +##def swap(n,notation): + +def notename(n,notation): + note = n%12 + octave = n//12 + if octave > 0: + return '{}|{}'.format(notes[notation][note],octave) + else: + return notes[notation][note] + + +def above_c(n): + n = n.upper().split('|') + if len(n) == 2: + octave = 12*int(n[1]) + else: + octave = 0 + note = n[0] + notation = 'sharps' if note[-1] == '#' else 'flats' + for i in range(12): + if notes[notation][i] == note: + return octave + i +## if n is int: +## return notename(n,notation): +## else: + diff --git a/load_asset.py b/load_asset.py new file mode 100644 index 0000000..09219fe --- /dev/null +++ b/load_asset.py @@ -0,0 +1,13 @@ +import os +import json +def load(asset): + direct = os.getcwd() + path = '{}{sep}Assets{sep}{}'.format(direct,asset + '.txt',**{'sep':os.sep}) + return json.loads(open(path,'r').read()) +def save(asset,asset_name): + direct = os.getcwd() + path = '{}{sep}Assets{sep}{}'.format(direct,asset_name + '.txt',**{'sep':os.sep}) + file = open(path,'w') + file.write(json.dumps(asset, sort_keys=True,indent=4, separators=(',', ': '))) + print('Saved as: {}'.format(asset_name)) + return diff --git a/main.py b/main.py new file mode 100644 index 0000000..d68c86d --- /dev/null +++ b/main.py @@ -0,0 +1,75 @@ +import convert_to +import load_asset +from Fingering import fingering +scales = load_asset.load('scales') +mod = False +#scales[0] = ailias,scales[1] = scales + +#adds scale with name +def add(ns,name): + new = scale(ns).transpose('c').notes + scales[1].append(new) + scales[0][name] = len(scales[1])-1 + load_asset.save(scales,'scales') + +#adds alternative name to scale +def alias(exist,con): + scale[0][con] = scale[0][exist] + load_asset.save(scales,'scales') + +#creates scale with specified starting note, type, and notation +def create(n,scale_type,notation = 'flats'): + return scale( + scales[1][ + scales[0][scale_type.lower()] + ] + ).transpose(n,notation) + +class scale: + #converts scale notes to above c0, in effect giving raw intervals from the start note + def normalize(self): + #ensures that the start note is the same as the end note. This affects the data returned by the .notes method + if convert_to.above_c(self.notes[0])%12 != convert_to.above_c(self.notes[-1])%12: + self.notes.append(self.notes[0]) + + #subtracts starting note from each note + start = convert_to.above_c(self.notes[0]) + note_list = list( + map( + lambda n: convert_to.above_c(n)-start, + self.notes + ) + ) + + #ensures scales are ascending + for i in range(len(note_list)-1): + while note_list[i] > note_list[i+1]: + note_list[i+1] += 12 + + return note_list + + #returns fingering with specified device + def fingers(self,d,tuning_a = 440): + return list( + map( + + lambda n: '{} ({})'.format(n,fingering(n,d,tuning_a)), + self.notes + + ) + ) + + #transposes scale to specified note + def transpose(self,n,notation = 'flats'): + n = convert_to.above_c(n) + return scale( + list( + map( + lambda m: convert_to.notename(m+n,notation), + self.intv + ) + ) + ) + def __init__(self,notes): + self.notes = notes + self.intv = self.normalize() diff --git a/temp.py b/temp.py new file mode 100644 index 0000000..b6d6e85 --- /dev/null +++ b/temp.py @@ -0,0 +1,8 @@ +from load_asset import save,load +valves = load('valves') +slide = load('slide') +device = {} +device['valves'] = valves +device['slide'] = slide +device['devices'] = {} +save(device,'device')