def add_ornament(annotated_notes, tonality, num_chromatics=-1, duration=1.0 / 8.0, loudness=[0, -48]): if type(loudness) is int or type(loudness) is float: loudness = [0, loudness] notes = [] for element in annotated_notes: notation = element['notation'] note = element['note'] if note.is_rest() == True: notes.append(note.copy()) elif note.duration < 3 * duration: notes.append( Note(pitch=note.pitch, start=note.start, duration=note.duration, dynamic=note.dynamic + loudness[0], channel=note.channel)) else: notes.append( Note(pitch=note.pitch + num_chromatics, start=note.start, duration=duration, dynamic=note.dynamic + loudness[1], channel=note.channel)) notes.append( Note(pitch=note.pitch, start=note.start + duration, duration=note.duration - duration, dynamic=note.dynamic + loudness[0], channel=note.channel)) tonality = {'key': tonality['key'], 'mode': tonality['mode']} return (Analyzer.annotate_notes(notes, tonality), tonality)
def __init__(self, track_melody, unit, offset = 0, resolution = None): if resolution == None: melody_notes = MIDI.list_notes(track_melody) else: melody_notes = MIDI.list_notes(track_melody, resolution) self.tempo_multiplier = 1.0 self.adjust_melody_notes(melody_notes, offset) self.harmony_length = unit important_notes = Analyzer.find_important_notes(melody_notes, unit) if len(important_notes) > 0 and offset != 0: important_notes.pop(0) if len(important_notes) > 0: self.num_chord = int(important_notes[-1].start / unit) + 1 else: self.num_chord = 1 (self.tonalities, change_points) = Analyzer.find_tonality(melody_notes, important_notes, unit) self.annotated_melody = Analyzer.annotate_all_notes(melody_notes, self.tonalities, unit) self.annotated_important_notes = Analyzer.annotate_all_notes(important_notes, self.tonalities, unit) self.progression_notations = Composer.gen_chord_progression(self.annotated_important_notes, change_points, unit) Instrument.init()
def to_shepard_tone(annotated_notes, tonality, octave=None): key = tonality['key'] if octave == None: pitches = [] for element in annotated_notes: if element['note'].is_rest() == False: pitches.append(element['note'].pitch - key) average = mean(pitches) octave = int(average) / 12 - 1 notes = [] for element in annotated_notes: notation = element['notation'] note = element['note'] if note.is_rest() == True: notes.append(note.copy()) else: # as pitch increases, lower octave one appears and higher octave one disappears ratio = { 1: sin(0 * pi / 14) / 2, 1.5: sin(1 * pi / 14) / 2, 2: sin(2 * pi / 14) / 2, 2.5: sin(3 * pi / 14) / 2, 3: sin(4 * pi / 14) / 2, 3.5: sin(5 * pi / 14) / 2, 4: sin(6 * pi / 14) / 2, 4.5: sin(7 * pi / 14) / 2, 5: 1 - sin(6 * pi / 14) / 2, 5.5: 1 - sin(5 * pi / 14) / 2, 6: 1 - sin(4 * pi / 14) / 2, 6.5: 1 - sin(3 * pi / 14) / 2, 7: 1 - sin(2 * pi / 14) / 2, 7.5: 1 - sin(1 * pi / 14) / 2 } pitch = 12 * (octave + 1) + (note.pitch - key) % 12 + key notes.append( Note(pitch=pitch - 12, start=note.start, duration=note.duration, dynamic=int(note.dynamic * ratio[notation]), channel=note.channel)) notes.append( Note(pitch=pitch, start=note.start, duration=note.duration, dynamic=note.dynamic, channel=note.channel)) notes.append( Note(pitch=pitch + 12, start=note.start, duration=note.duration, dynamic=int(note.dynamic * (1.0 - ratio[notation])), channel=note.channel)) tonality = {'key': tonality['key'], 'mode': tonality['mode']} return (Analyzer.annotate_notes(notes, tonality), tonality)
def add_tremolo(annotated_notes, tonality, num_chromatics=-12, unit=1.0 / 4.0, loudness=[0, 0]): if type(loudness) is int or type(loudness) is float: loudness = [0, loudness] multiplier = 2.0 if (loudness[0] - loudness[1]) >= 25 else 1.0 notes = [] for element in annotated_notes: notation = element['notation'] note = element['note'] if note.is_rest() == True: notes.append(note.copy()) elif note.duration < 2 * unit: notes.append( Note(pitch=note.pitch, start=note.start, duration=note.duration, dynamic=note.dynamic + loudness[0], channel=note.channel)) else: num_notes = min(int(note.duration / unit), 8) num_notes = max(2, int(num_notes * 0.75)) current_beat = note.start for i in range(num_notes): if i == num_notes - 1 and multiplier > 1: multiplier = 1 if i % 2 == 0: notes.append( Note(pitch=note.pitch, start=current_beat, duration=unit * multiplier, dynamic=note.dynamic + loudness[0], channel=note.channel)) else: notes.append( Note(pitch=note.pitch + num_chromatics, start=current_beat, duration=unit, dynamic=note.dynamic + loudness[1], channel=note.channel)) current_beat += unit tonality = {'key': tonality['key'], 'mode': tonality['mode']} return (Analyzer.annotate_notes(notes, tonality), tonality)
def gen_staccato_bass(annotated_important_notes, tonality, progression_notations, \ harmony_length, dynamic = 48, octave = 3, ratio = 0.25): unit = 0.5 num_notes = harmony_length * 2 while num_notes > 4: num_notes /= 2 unit *= 2 while num_notes < 2: num_notes *= 2 unit /= 2 num_notes = int(num_notes) configures = { 'harmony_length': harmony_length, 'dynamic': dynamic, 'octave': octave, 'chord_map': { 1: [1, 3, 5, 3, 5, 3, 5], 2: [-1, 2, 4, 2, 4, 2, 4], 3: [-2, 0, 3, 0, 3, 0, 3], 4: [1, 4, 6, 4, 6, 4, 6], 5: [0, 2, 5, 2, 5, 2, 5], 6: [-1, 1, 3, 1, 3, 1, 3], 7: [0, 2, 4, 2, 4, 2, 4] }, 'num_notes': num_notes * 2 - 1, 'note_start': [0] + [ float((i + 2) / 2) / harmony_length * unit for i in range(num_notes * 2 - 2) ], 'note_duration': [1.0 / harmony_length * unit] + [ratio / harmony_length * unit for i in range(num_notes * 2 - 2)], 'note_dynamic_shift': [12, 0, 0, 0, 0, 0, 0], 'note_channel': [1, 2, 2, 2, 2, 2, 2] } notes = Bass.gen_bass(annotated_important_notes, tonality, progression_notations, configures) tonality = {'key': tonality['key'], 'mode': tonality['mode']} return (Analyzer.annotate_notes(notes, tonality), tonality)
def gen_arpeggio_bass(annotated_important_notes, tonality, progression_notations, \ harmony_length, dynamic = 48, octave = 3): unit = 0.5 num_notes = harmony_length * 2 while num_notes > 6: num_notes /= 2 unit *= 2 while num_notes < 3: num_notes *= 2 unit /= 2 num_notes = int(num_notes) # non-overlapping version: 'note_duration': [1.0 / harmony_length * unit] * num_notes, configures = { 'harmony_length': harmony_length, 'dynamic': dynamic, 'octave': octave, 'chord_map': { 1: [1, 3, 5, 8, 5, 3], 2: [-1, 2, 4, 6, 4, 2], 3: [-2, 0, 3, 5, 3, 0], 4: [1, 4, 6, 8, 6, 4], 5: [0, 2, 5, 7, 5, 2], 6: [-1, 1, 3, 6, 3, 1], 7: [0, 2, 4, 7, 4, 2] }, 'num_notes': num_notes, 'note_start': [float(i) / harmony_length * unit for i in range(num_notes)], 'note_duration': [1 - float(i) / harmony_length * unit for i in range(num_notes)], 'note_dynamic_shift': [12, 0, 0, 0, 0, 0], 'note_channel': [1, 2, 2, 2, 2, 2] } notes = Bass.gen_bass(annotated_important_notes, tonality, progression_notations, configures) tonality = {'key': tonality['key'], 'mode': tonality['mode']} return (Analyzer.annotate_notes(notes, tonality), tonality)
def gen_alberti_bass(annotated_important_notes, tonality, progression_notations, \ harmony_length, dynamic = 48, octave = 3, ratio = 0.25): unit = 0.5 num_notes = harmony_length * 2 while num_notes > 8: num_notes /= 2 unit *= 2 while num_notes < 4: num_notes *= 2 unit /= 2 num_notes = int(num_notes) configures = { 'harmony_length': harmony_length, 'dynamic': dynamic, 'octave': octave, 'chord_map': { 1: [1, 5, 3, 5] * 2, 2: [-1, 4, 2, 4] * 2, 3: [-2, 3, 0, 3] * 2, 4: [1, 6, 4, 6] * 2, 5: [0, 5, 2, 5] * 2, 6: [-1, 3, 1, 3] * 2, 7: [0, 4, 2, 4] * 2 }, 'num_notes': num_notes, 'note_start': [float(i) / harmony_length * unit for i in range(num_notes)], 'note_duration': [ratio / harmony_length * unit for i in range(num_notes)], 'note_dynamic_shift': [12, 0, 6, 0] * 2, 'note_channel': [1, 2, 1, 2] * 2 } notes = Bass.gen_bass(annotated_important_notes, tonality, progression_notations, configures) tonality = {'key': tonality['key'], 'mode': tonality['mode']} return (Analyzer.annotate_notes(notes, tonality), tonality)
def adjust_mode(annotated_notes, tonality, mode_map): notes = [] for element in annotated_notes: notation = element['notation'] note = element['note'] key = tonality['key'] if note.is_rest() == True: adjusted_pitch = 0 else: adjusted_pitch = (int(note.pitch - key) / 12) * 12 + key + mode_map[notation] notes.append( Note(pitch=adjusted_pitch, start=note.start, duration=note.duration, dynamic=note.dynamic, channel=note.channel)) adjusted_tonality = {'key': tonality['key'], 'mode': mode_map['name']} return (Analyzer.annotate_notes(notes, adjusted_tonality), adjusted_tonality)
def add_multi_parallel_8_degrees(annotated_notes, tonality, loudness): notes = [] for element in annotated_notes: notation = element['notation'] note = element['note'] if note.is_rest() == True: notes.append(note.copy()) else: for i in range(5): if loudness[i] >= median(loudness) and loudness[i] > 0: channel = note.channel channel = max(0, min(15, channel)) notes.append( Note(pitch=note.pitch - 24 + 12 * i, start=note.start, duration=note.duration, dynamic=note.dynamic * loudness[i], channel=channel)) tonality = {'key': tonality['key'], 'mode': tonality['mode']} return (Analyzer.annotate_notes(notes, tonality), tonality)
def gen_single_note_bass(annotated_important_notes, tonality, progression_notations, \ harmony_length, dynamic = 48, octave = 3): unit = 1.0 num_notes = harmony_length while num_notes > 4: num_notes /= 2 unit *= 2 while num_notes < 2: num_notes *= 2 unit /= 2 num_notes = int(num_notes) configures = { 'harmony_length': harmony_length, 'dynamic': dynamic, 'octave': octave, 'chord_map': { 1: [1, 1, 1, 1], 2: [2, 2, 2, 2], 3: [3, 3, 3, 3], 4: [4, 4, 4, 4], 5: [-2, -2, -2, -2], 6: [-1, -1, -1, -1], 7: [0, 0, 0, 0] }, 'num_notes': num_notes, 'note_start': [float(i) / harmony_length * unit for i in range(num_notes)], 'note_duration': [1.0 / harmony_length * unit] * num_notes, 'note_dynamic_shift': [12, 0, 0, 0], 'note_channel': [1, 2, 2, 2] } notes = Bass.gen_bass(annotated_important_notes, tonality, progression_notations, configures) tonality = {'key': tonality['key'], 'mode': tonality['mode']} return (Analyzer.annotate_notes(notes, tonality), tonality)
def add_parallel_n_degrees(annotated_notes, tonality, degrees=8, loudness=[0, -24]): if type(loudness) is int or type(loudness) is float: loudness = [0, loudness] notes = [] degrees = degrees - 1 for element in annotated_notes: notation = element['notation'] note = element['note'] if note.is_rest() == True: notes.append(note.copy()) else: key = tonality['key'] mode_map = Mode.get_mode_map(tonality['mode']) pitch_shift = 0 adjusted_notation = notation - degrees if adjusted_notation < 1: adjusted_notation = adjusted_notation + 7 pitch_shift = -12 adjusted_pitch = ( int(note.pitch - key) / 12) * 12 + key + mode_map[adjusted_notation] + pitch_shift notes.append( Note(pitch=note.pitch, start=note.start, duration=note.duration, dynamic=note.dynamic + loudness[0], channel=note.channel)) notes.append( Note(pitch=adjusted_pitch, start=note.start, duration=note.duration, dynamic=note.dynamic + loudness[1], channel=note.channel)) tonality = {'key': tonality['key'], 'mode': tonality['mode']} return (Analyzer.annotate_notes(notes, tonality), tonality)
def to_staccato(annotated_notes, tonality, ratio=0.25, loudness=0): notes = [] for element in annotated_notes: notation = element['notation'] note = element['note'] if note.is_rest() == True: notes.append(note.copy()) else: notes.append( Note(pitch=note.pitch, start=note.start, duration=note.duration * ratio, dynamic=note.dynamic + loudness, channel=note.channel)) if ratio < 1: notes.append( Note(pitch=0, start=note.start + note.duration * ratio, duration=note.duration * (1 - ratio), dynamic=0, channel=note.channel)) tonality = {'key': tonality['key'], 'mode': tonality['mode']} return (Analyzer.annotate_notes(notes, tonality), tonality)
def transpose(annotated_notes, tonality, num_chromatics=0, loudness=0): notes = [] for element in annotated_notes: notation = element['notation'] note = element['note'] if note.is_rest() == True: pitch_adjustment = 0 loudness_adjustment = 0 else: pitch_adjustment = num_chromatics loudness_adjustment = loudness notes.append( Note(pitch=note.pitch + pitch_adjustment, start=note.start, duration=note.duration, dynamic=note.dynamic + loudness_adjustment, channel=note.channel)) adjusted_tonality = { 'key': (tonality['key'] + num_chromatics) % 12, 'mode': tonality['mode'] } return (Analyzer.annotate_notes(notes, adjusted_tonality), adjusted_tonality)
def gen_wide_range_bass(annotated_important_notes, tonality, progression_notations, \ harmony_length, dynamic = 48, octave = 3): unit = 0.5 num_notes = harmony_length * 2 while num_notes > 8: num_notes /= 2 unit *= 2 while num_notes < 4: num_notes *= 2 unit /= 2 num_notes = int(num_notes) if num_notes >= 6: # non-overlapping version: 'note_duration': [1.0 / harmony_length * unit] * 4 + [1.0 - 4.0 / harmony_length * unit], configures = { 'harmony_length': harmony_length, 'dynamic': dynamic, 'octave': octave, 'chord_map': { 1: [1 - 7, 5 - 7, 1, 3, 5], 2: [2 - 7, 6 - 7, 2, 4, 6], 3: [3 - 7, 7 - 7, 3, 5, 7], 4: [4 - 7, 8 - 7, 4, 6, 8], 5: [-2 - 7, 2 - 7, -2, 0, 2], 6: [-1 - 7, 3 - 7, -1, 1, 3], 7: [0 - 7, 4 - 7, 0, 2, 4] }, 'num_notes': 5, 'note_start': [float(i) / harmony_length * unit for i in range(5)], 'note_duration': [1 - float(i) / harmony_length * unit for i in range(5)], 'note_dynamic_shift': [12, 0, 0, 0, 0], 'note_channel': [1, 2, 2, 2, 2] } else: # non-overlapping version: 'note_duration': [1.0 / harmony_length * unit] * 2 + [1 - 2.0 / harmony_length * unit] * 2, configures = { 'harmony_length': harmony_length, 'dynamic': dynamic, 'octave': octave, 'chord_map': { 1: [1 - 7, 5 - 7, 1, 3], 2: [2 - 7, 6 - 7, 2, 4], 3: [3 - 7, 7 - 7, 3, 5], 4: [4 - 7, 8 - 7, 4, 6], 5: [-2 - 7, 2 - 7, -2, 0], 6: [-1 - 7, 3 - 7, -1, 1], 7: [0 - 7, 4 - 7, 0, 2] }, 'num_notes': 4, 'note_start': [ 0, 1.0 / harmony_length * unit, 2.0 / harmony_length * unit, 2.0 / harmony_length * unit ], 'note_duration': [ 1, 1 - 1.0 / harmony_length * unit, 1 - 2.0 / harmony_length * unit, 1 - 2.0 / harmony_length * unit ], 'note_dynamic_shift': [12, 0, 0, 0], 'note_channel': [1, 2, 2, 2] } notes = Bass.gen_bass(annotated_important_notes, tonality, progression_notations, configures) tonality = {'key': tonality['key'], 'mode': tonality['mode']} return (Analyzer.annotate_notes(notes, tonality), tonality)