def set_note(self, name='C', octave=4, dynamics={}): """Set the note to name in octave with dynamics. Return the objects if it succeeded, raise an NoteFormatError otherwise. """ dash_index = name.split('-') if len(dash_index) == 1: if notes.is_valid_note(name): self.name = name self.octave = octave self.dynamics = dynamics return self else: raise NoteFormatError("The string '%s' is not a valid " "representation of a note in mingus" % name) elif len(dash_index) == 2: if notes.is_valid_note(dash_index[0]): self.name = dash_index[0] self.octave = int(dash_index[1]) self.dynamics = dynamics return self else: raise NoteFormatError("The string '%s' is not a valid " "representation of a note in mingus" % name) return False
def set_note(self, name='C', octave: int = 4, dynamics: dict = None) \ -> 'Note': """Set the note to name in octave with dynamics. Raises: NoteFormatError if objects fail to be returned """ if dynamics is None: dynamics = {} dash_index = name.split('-') # We are in the simple case if len(dash_index) == 1: if notes.is_valid_note(name): self.name = name self.octave = octave self.dynamics = dynamics return self raise NoteFormatError("The string '{}' is not a valid " "representation of a note in " "mingus".format(name)) # We have a note with an octave if len(dash_index) == 2: if notes.is_valid_note(dash_index[0]): self.name = dash_index[0] self.octave = int(dash_index[1]) self.dynamics = dynamics return self raise NoteFormatError("The string '{}' is not a valid " "representation of a note in " "mingus".format(name))
def set_note( self, name='C', octave=4, dynamics={}, ): """Sets the note to `name` in `octave` with `dynamics` if the name of the \ note is valid. Returns the objects if it succeeded, raises an \ NoteFormatError otherwise.""" dash_index = name.split('-') if len(dash_index) == 1: if notes.is_valid_note(name): self.name = name self.octave = octave self.dynamics = dynamics return self else: raise NoteFormatError, \ "The string '%s' is not a valid representationof a note in mingus"\ % name elif len(dash_index) == 2: if notes.is_valid_note(dash_index[0]): self.name = dash_index[0] self.octave = int(dash_index[1]) self.dynamics = dynamics return self else: raise NoteFormatError, \ "The string '%s' is not a valid representationof a note in mingus"\ % name return False
def set_note(self, name = 'C', octave = 4, dynamics = {}): """Sets the note to `name` in `octave` with `dynamics` if \ the name of the note is valid. Returns the objects if it \ succeeded, raises an NoteFormatError otherwise.""" dash_index = name.split('-') if len(dash_index) == 1: if notes.is_valid_note(name): self.name = name self.octave = octave self.dynamics = dynamics return self else: raise NoteFormatError,\ "The string '%s' is not a valid representation"\ "of a note in mingus" % name elif len(dash_index) == 2: if notes.is_valid_note(dash_index[0]): self.name = dash_index[0] self.octave = int(dash_index[1]) self.dynamics = dynamics return self else: raise NoteFormatError,\ "The string '%s' is not a valid representation"\ "of a note in mingus" % name return False
def set_note(self, name="C", octave=4, dynamics=None, velocity=None, channel=None): """Set the note to name in octave with dynamics. Return the objects if it succeeded, raise an NoteFormatError otherwise. :param name: :param octave: :param dynamics: Deprecated. Use `velocity` and `channel` directly. :param int velocity: Integer (0-127) :param int channel: Integer (0-15) :return: """ if dynamics is None: dynamics = {} if velocity is not None: self.set_velocity(velocity) elif "velocity" in dynamics: self.set_velocity(dynamics["velocity"]) if channel is not None: self.set_channel(channel) if "channel" in dynamics: self.set_channel(dynamics["channel"]) dash_index = name.split("-") if len(dash_index) == 1: if notes.is_valid_note(name): self.name = name self.octave = octave return self else: raise NoteFormatError("Invalid note representation: %r" % name) elif len(dash_index) == 2: note, octave = dash_index if notes.is_valid_note(note): self.name = note self.octave = int(octave) return self else: raise NoteFormatError("Invalid note representation: %r" % name) else: raise NoteFormatError("Invalid note representation: %r" % name)
def isvalidnote(answer): try: # return True if response is numerical 1-7 return int(answer) in range(1, 8) except: pass try: # return True if response is a valid note name return notes.is_valid_note(answer[0].upper() + answer[1:]) except: pass return False
def from_shorthand(note, interval, up=True): """Return the note on interval up or down. Examples: >>> from_shorthand('A', 'b3') 'C' >>> from_shorthand('D', '2') 'E' >>> from_shorthand('E', '2', False) 'D' """ # warning should be a valid note. if not notes.is_valid_note(note): return False # [shorthand, interval function up, interval function down] shorthand_lookup = [ ['1', major_unison, major_unison], ['2', major_second, minor_seventh], ['3', major_third, minor_sixth], ['4', major_fourth, major_fifth], ['5', major_fifth, major_fourth], ['6', major_sixth, minor_third], ['7', major_seventh, minor_second], ] # Looking up last character in interval in shorthand_lookup and calling that # function. val = False for shorthand in shorthand_lookup: if shorthand[0] == interval[-1]: if up: val = shorthand[1](note) else: val = shorthand[2](note) # warning Last character in interval should be 1-7 if val == False: return False # Collect accidentals for x in interval: if x == '#': if up: val = notes.augment(val) else: val = notes.diminish(val) elif x == 'b': if up: val = notes.diminish(val) else: val = notes.augment(val) else: return val
def note_validity_chk_redundancy_remover(self, note): ''' Objective: Check if note is valid or not; if so remove any redundancy from the note ''' if M_notes.is_valid_note(note): note1 = M_notes.reduce_accidentals( M_notes.remove_redundant_accidentals(note)) if self.__original_notation_intact(note) not in note1: return (note, True) else: return (note1, True) return (note, False)
def main(): if len(sys.argv) < 3: print("Usage: python3 getscale.py BASETONE MODE") print("e,g. python3 getscale.py C# minor") sys.exit(1) basetone = sys.argv[1] mode = sys.argv[2] if not notes.is_valid_note(basetone): print("Invalid note: ", basetone) sys.exit(1) print("Your scale is:") scale = get_scale(basetone, mode) print(scale)
def interval(key, start_note, interval): """Return the note found at the interval starting from start_note in the given key. Raise a KeyError exception if start_note is not a valid note. Example: >>> interval('C', 'D', 1) 'E' """ if not notes.is_valid_note(start_note): raise KeyError("The start note '%s' is not a valid note" % start_note) notes_in_key = keys.get_notes(key) for n in notes_in_key: if n[0] == start_note[0]: index = notes_in_key.index(n) return notes_in_key[(index + interval) % 7]
def set_key(reset_score=True): mes = ("Enter the desired key, use upper-case for major " "and lower-case for minor (e.g. C or c).\n" "Enter R/r for a random major/minor key.") newkey = input(mes) keys = ['A', 'Bb', 'B', 'C', 'C#', 'D', 'Eb', 'E', 'F', 'F#', 'G', 'Ab'] if newkey == 'R': st.KEY = random.choice(keys) elif newkey == 'r': st.KEY = random.choice(keys).lower() elif notes.is_valid_note(newkey): st.KEY = newkey else: print("Input key not understood, key unchanged.") st.CURRENT_MODE.intro() if reset_score: st.COUNT = 0 st.SCORE = 0
def get_answer(self): a = raw_input('answer : ').upper() while not notes.is_valid_note(a) and not a.startswith('R'): print 'Invalid input' a = raw_input('answer : ').upper() return a if not a.startswith('R') else None
def test_sharp_note_validity(self): map( lambda x: self.assert_(notes.is_valid_note(x), 'Sharp notes A#-G#' ), self.sharps)
def test_faulty_note_invalidity(self): for x in ["asdasd", "C###f", "c", "d", "E*"]: self.assertEqual(False, notes.is_valid_note(x), "Faulty notes")
def is_valid_note(key): return notes.is_valid_note(key[0].upper() + key[1:])
import mingus.core.notes as notes import mingus.core.keys as keys import mingus.core.intervals as intervals print(notes.is_valid_note('C#')) print(notes.int_to_note(0)) # [0,..,11] print(keys.get_notes("C")) print(intervals.second("E", "C")) print(intervals.determine("Gbb", "Ab")) print(intervals.determine("Gbb", "Ab", True)) print(intervals.measure("C", "D")) print(intervals.measure("D", "C"))
def map_key(key): octave, note = convert_to_note(key.upper()) if note is None or not notes.is_valid_note(note): print(f"invalid note: {key}") return (octave, note)
def test_flat_note_validity(self): map(lambda x: self.assert_(notes.is_valid_note(x), 'Flat notes Ab-Gb'), self.flats)
#https://wiki.python.org/moin/PythonInMusic #http://bspaans.github.io/python-mingus/doc/wiki/tutorialCore import mingus.core.notes as notes aa = notes.is_valid_note("C") print(aa)
def test_exotic_note_validity(self): map( lambda x: self.assert_(notes.is_valid_note(x), 'Exotic notes Ab##b#-Gb###b#'), self.exotic)
KEY = user_args.key.lower() else: KEY = user_args.key.upper() if user_args.sevenths: [I, II, III, IV, V, VI, VII] = ["I7", "II7", "III7", "IV7", "V7", "VI7", "VII7"] TONES = [1, 3, 5, 7] else: [I, II, III, IV, V, VI, VII] = ["I", "II", "III", "IV", "V", "VI", "VII"] TONES = [1, 3, 5] CADENCE = [I, IV, V, I] NUMERALS = [I, II, III, IV, V, VI, VII] if not notes.is_valid_note(KEY): print("ATTENTION: User-input key, {}, not valid, using C Major " "instead.".format(KEY)) KEY = "C" # Other user args MANY_OCTAVES = user_args.many_octaves # DELAY = user_args.delay PROGRESSION_MODE = False BPM = 60 * user_args.delay # Other args that should be user-adjustable, but aren't yet PROG_LENGTHS = range(2, 5) # Number of strums in a progression CHORD_LENGTHS = range(1, 3) # Number of strums per chord RESOLVE_WHEN_INCORRECT = True RESOLVE_WHEN_CORRECT = True
def from_shorthand(shorthand_string, slash=None): """Take a chord written in shorthand and return the notes in the chord. The function can recognize triads, sevenths, sixths, ninths, elevenths, thirteenths, slashed chords and a number of altered chords. The second argument should not be given and is only used for a recursive call when a slashed chord or polychord is found. See http://tinyurl.com/3hn6v8u for a nice overview of chord patterns. Examples: >>> from_shorthand('Amin') ['A', 'C', 'E'] >>> from_shorthand('Am/M7') ['A', 'C', 'E', 'G#'] >>> from_shorthand('A') ['A', 'C#', 'E'] >>> from_shorthand('A/G') ['G', 'A', 'C#', 'E'] >>> from_shorthand('Dm|G') ['G', 'B', 'D', 'F', 'A'] Recognised abbreviations: the letters "m" and "M" in the following abbreviations can always be substituted by respectively "min", "mi" or "-" and "maj" or "ma". Example: >>> from_shorthand('Amin7') == from_shorthand('Am7') True Triads: 'm', 'M' or '', 'dim' Sevenths: 'm7', 'M7', '7', 'm7b5', 'dim7', 'm/M7' or 'mM7' Augmented chords: 'aug' or '+', '7#5' or 'M7+5', 'M7+', 'm7+', '7+' Suspended chords: 'sus4', 'sus2', 'sus47' or '7sus4', 'sus', '11', 'sus4b9' or 'susb9' Sixths: '6', 'm6', 'M6', '6/7' or '67', '6/9' or '69' Ninths: '9' or 'add9', 'M9', 'm9', '7b9', '7#9' Elevenths: '11' or 'add11', '7#11', 'm11' Thirteenths: '13' or 'add13', 'M13', 'm13' Altered chords: '7b5', '7b9', '7#9', '67' or '6/7' Special: '5', 'NC', 'hendrix' """ # warning reduce?? if type(shorthand_string) == list: res = [] for x in shorthand_string: res.append(from_shorthand(x)) return res if shorthand_string in ['NC', 'N.C.']: return [] # Shrink shorthand_string to a format recognised by chord_shorthand shorthand_string = shorthand_string.replace('min', 'm') shorthand_string = shorthand_string.replace('mi', 'm') shorthand_string = shorthand_string.replace('-', 'm') shorthand_string = shorthand_string.replace('maj', 'M') shorthand_string = shorthand_string.replace('ma', 'M') # Get the note name if not notes.is_valid_note(shorthand_string[0]): raise(NoteFormatError, "Unrecognised note '%s' in chord '%s'"\ % (shorthand_string[0], shorthand_string)) name = shorthand_string[0] # Look for accidentals for n in shorthand_string[1:]: if n == '#': name += n elif n == 'b': name += n else: break # Look for slashes and polychords '|' slash_index = -1 s = 0 rest_of_string = shorthand_string[len(name):] for n in rest_of_string: if n == '/': slash_index = s elif n == '|': # Generate polychord return from_shorthand( shorthand_string[:len(name) + s], from_shorthand(shorthand_string[len(name) + s + 1:])) s += 1 # Generate slash chord if slash_index != -1 and rest_of_string not in ['m/M7', '6/9', '6/7']: res = shorthand_string[:len(name) + slash_index] return from_shorthand(shorthand_string[:len(name) + slash_index], shorthand_string[len(name) + slash_index + 1:]) shorthand_start = len(name) short_chord = shorthand_string[shorthand_start:] if chord_shorthand.has_key(short_chord): res = chord_shorthand[short_chord](name) if slash != None: # Add slashed chords if type(slash) == str: if notes.is_valid_note(slash): res = [slash] + res else: raise (NoteFormatError, \ "Unrecognised note '%s' in slash chord'%s'" % (slash, slash + shorthand_string)) elif type(slash) == list: # Add polychords r = slash for n in res: if n != r[-1]: r.append(n) return r return res else: raise (FormatError, 'Unknown shorthand: %s' % shorthand_string)
def test_flat_note_validity(self): for x in self.flats: self.assertTrue(notes.is_valid_note(x), "Flat notes Ab-Gb")
def test_base_note_validity(self): map(lambda x: self.assert_(notes.is_valid_note(x), 'Base notes A-G'), self.base_notes)
def test_exotic_note_validity(self): for x in self.exotic: self.assertTrue(notes.is_valid_note(x), "Exotic notes Ab##b#-Gb###b#")
''' Created on Jan 6, 2017 @author: stephenkoh ''' import mingus.core.notes as notes note = str(input('Please enter a note: ')) if (notes.is_valid_note(note)): note = notes.to_minor(note) note = notes.diminish(note) print(note)
def test_faulty_note_invalidity(self): map( lambda x: self.assertEqual(False, notes.is_valid_note( x), 'Faulty notes'), ['asdasd', 'C###f', 'c', 'd', 'E*'])
# -*- coding: utf-8 -*- #2018/8/26 #python2 import mingus.core.notes as notes #检查音符合法性 notes.is_valid_note('C') # True #音符、值互转 notes.note_to_int('C') # 0 notes.int_to_note(1) # C# #半音升降 notes.augment('C') # C# notes.diminish('C#') # C #大小调转化(无方法) #notes.to_minor('C') # A #notes.to_major('A') # C #无模块 #import mingus.core.diatonic as diatonic #十二音 #diatonic.basic_keys #E调七音 #diatonic.get_notes('E') import mingus.core.intervals as interval #间隔半音数 interval.measure('C', 'D') #2 import mingus.core.scales as scales #爱奥尼音阶对象
def test_base_note_validity(self): for x in self.base_notes: self.assertTrue(notes.is_valid_note(x), "Base notes A-G")
def test_sharp_note_validity(self): for x in self.sharps: self.assertTrue(notes.is_valid_note(x), "Sharp notes A#-G#")
note_type = 14 if event.unicode == '6': note = "D#" note_type = 16 if event.unicode == '7': note = "F#" note_type = 19 if event.unicode == '8': note = "G#" note_type = 21 if event.unicode == '9': note = "A#" note_type = 23 if args.note: note_type = int(args.note[0], 10) if note != "" and notes.is_valid_note(note) == False: print ERROR + "invalid note" else: if note == "": break n = Note() if note == "k": n.set_Note('B') else: if note_type == 0: n.set_note(note) else: n.set_note(note, note_type) print ACTION + "playing Note : " + note fluidsynth.play_Note(n) #saving the play