def make_music21_score( part_names=('violin', 'flute', 'oboe', 'clarinet', 'alto_saxophone', 'trumpet', 'bass', 'percussion'), title='Title', composer='Jonathan Marmor', time_signature=None, starting_tempo_bpm=60, starting_tempo_quarter_duration=1.0, timestamp=None, ): if not timestamp: timestamp = datetime.datetime.utcnow() metadata = Metadata() metadata.title = title metadata.composer = composer metadata.date = timestamp.strftime('%Y/%m/%d') score = Score() score.insert(0, metadata) for part_name in part_names: instrument_name, instrument_number = parse_part_name(part_name) instrument = instrument_data[instrument_name] part = Part() metronome_mark = MetronomeMark( number=starting_tempo_bpm, referent=Duration(starting_tempo_quarter_duration)) part.append(metronome_mark) if time_signature: # Should be a string like '12/8' music21_time_signature = TimeSignature(time_signature) part.append(music21_time_signature) m21_instrument = instrument['class']() m21_instrument.partName = instrument['name'] m21_instrument.partAbbreviation = instrument['abbreviation'] if instrument_number > 1: m21_instrument.partName = '{} {}'.format(instrument['name'], instrument_number) m21_instrument.partAbbreviation = '{} {}'.format( instrument['abbreviation'], instrument_number) part.insert(0, m21_instrument) clef = instrument.get('clef') if clef: part.append(clef()) score.insert(0, part) return score
def estimate_tempo(self): tempo, beats = librosa.beat.beat_track(y=None, sr=self.sr, onset_envelope=self.onsets[2], hop_length=self.hop_length, start_bpm=120.0, tightness=100.0, trim=True, units='frames') tempo = int(2 * round(tempo / 2)) mm = MetronomeMark(referent='quarter', number=tempo) return tempo, beats, mm
def generate_notes_in_batch(note_params_df, output_dir, audio_format='flac', sample_rate=44100): """ Generates a batch of single note samples from the given table of parameters. `note_params_df` - a Pandas Dataframe with columns: `midi_number, midi_instrument, volume, duration, tempo`. Their meaning is the same as in generate_single_note. `output_dir` - output directory for the MIDI files Each sample goes to a single MIDI file named by the numeric index. Also each synthesized audio sample goes to a """ os.makedirs(output_dir, exist_ok=True) fs = FluidSynth(sample_rate=sample_rate) stream = Stream() for i, row in note_params_df.iterrows(): stream.append(MetronomeMark(number=row['tempo'])) stream.append(make_instrument(int(row['midi_instrument']))) duration = row['duration'] stream.append( chord_with_volume( Chord([ Note(midi=int(row['midi_number']), duration=Duration(duration)) ]), row['volume'])) stream.append(Rest(duration=Duration(2 * duration))) midi_file = '{0}/all_samples.midi'.format(output_dir) audio_file_stereo = '{0}/all_samples_stereo.{1}'.format( output_dir, audio_format) audio_file = '{0}/all_samples.{1}'.format(output_dir, audio_format) audio_index_file = '{0}/all_samples_index.csv'.format(output_dir) # TODO: We currently assume some fixed duration and tempo (1.0, 120)!!! # The parts should be split according to an index. audio_index = make_audio_index(note_params_df, 3.0, 0.5, sample_rate) audio_index.to_csv(audio_index_file) write_midi(stream, midi_file) fs.midi_to_audio(midi_file, audio_file_stereo) convert_to_mono(audio_file_stereo, audio_file) os.remove(audio_file_stereo) x, sample_rate = sf.read(audio_file) parts = split_audio_to_parts(x, sample_rate, audio_index) store_parts_to_files(parts, sample_rate, output_dir, audio_format)
def generate_single_note(midi_number, midi_instrument=0, volume=1.0, duration=1.0, tempo=120): """ Generates a stream containing a single note with given parameters. midi_number - MIDI note number, 0 to 127 midi_instrument - MIDI intrument number, 0 to 127 duration - floating point number (in quarter note lengths) volume - 0.0 to 1.0 tempo - number of quarter notes per minute (eg. 120) Note that there's a quarter note rest at the beginning and at the end. """ return Stream([ MetronomeMark(number=tempo), make_instrument(int(midi_instrument)), chord_with_volume( Chord([Note(midi=int(midi_number), duration=Duration(duration))]), volume) ])
inter_cqt_tuning(mag_exp, cqt_threshold, pre_post_max, backtrack) # Estimate Tempo tempo, beats = librosa.beat.beat_track(y=None, sr=fs, onset_envelope=onsets[2], hop_length=hop_length, start_bpm=120.0, tightness=100, trim=True, bpm=None, units='frames') tempo = int(2 * round(tempo / 2)) print("BPM: {}".format(tempo)) mm = MetronomeMark(referent='quarter', number=tempo) # Convert Seconds to Quarter-Notes def time_to_beat(duration, tempo): return (tempo * duration / 60) # Remap input to 0-1 for Sine Amplitude or to 0-127 for MIDI def remap(x, in_min, in_max, out_min, out_max): return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min # Generate Sinewave and music21 notes def generate_sine_midi_note(f0_info, sr, n_duration): f0 = f0_info[0]
def to_music21_metronome(tempo: Tempo) -> MetronomeMark: """Return a Tempo object as a music21 MetronomeMark object.""" metronome = MetronomeMark(number=tempo.qpm) metronome.offset = tempo.time return metronome
args.melody, args.series)) # Pick melody if args.melody == 'little_happiness': melody = converter.parse(A_LITTLE_HAPPINESS) elif args.melody == 'jj_lin': melody = converter.parse(JJ_LIN_MELODY) else: print('Unrecognized melody: should be jj_lin or little_happiness') sys.exit(1) if args.series not in ('major', 'minor'): print('Unrecognized series: should be major or minor') sys.exit(1) melody.insert(0, MetronomeMark(number=95)) # Pick algorithm if args.algorithm == 'basic': chord_search.run(chords, melody, args.series) elif args.algorithm == 'hmm': viterbi.run(chords, melody, args.series) else: print('Unrecognized algorithm: should be basic or hmm') sys.exit(1) # Combine two parts song = Stream() song.insert(0, melody) song.insert(0, chords)
def tensors_to_stream(outputs, config, metadata=None): cur_measure_number = 0 parts = {} for part_name in outputs.keys(): if part_name == 'extra': continue part = Part(id=part_name) parts[part_name] = part last_time_signature = None cur_time_signature = '4/4' for step in range(outputs['soprano'].shape[0]): extra = outputs['extra'][step] if extra[indices_extra['has_time_signature_3/4']].item() == 1: cur_time_signature = '3/4' elif extra[indices_extra['has_time_signature_4/4']].item() == 1: cur_time_signature = '4/4' elif extra[indices_extra['has_time_signature_3/2']].item() == 1: cur_time_signature = '3/2' cur_time_pos = extra[indices_extra['time_pos']].item() has_fermata = extra[indices_extra['has_fermata']].item() == 1 if cur_time_pos == 1.0 or cur_measure_number == 0: for part_name, part in parts.items(): part.append(Measure(number=cur_measure_number)) if cur_measure_number == 0: if part_name in ['soprano', 'alto']: part[-1].append(clef.TrebleClef()) else: part[-1].append(clef.BassClef()) key = int( torch.argmax( outputs['extra'][0, indices_extra['has_sharps_0']: indices_extra['has_sharps_11'] + 1], dim=0).item()) if key >= 6: key -= 12 part[-1].append(KeySignature(key)) part[-1].append(MetronomeMark(number=90)) cur_measure_number += 1 if last_time_signature is None or cur_time_signature != last_time_signature: for part in parts.values(): part[-1].append(TimeSignature(cur_time_signature)) last_time_signature = cur_time_signature for part_name, part in parts.items(): idx = torch.argmax(outputs[part_name][step]).item() if idx == indices_parts['is_continued']: try: last_element = part[-1].flat.notesAndRests[-1] cur_element = deepcopy(last_element) if last_element.tie is not None and last_element.tie.type == 'stop': last_element.tie = Tie('continue') else: last_element.tie = Tie('start') cur_element.tie = Tie('stop') except IndexError: logging.debug( 'Warning: "is_continued" on first beat. Replaced by rest.' ) cur_element = Rest(quarterLength=config.time_grid) part[-1].append(cur_element) elif idx == indices_parts['is_rest']: part[-1].append(Rest(quarterLength=config.time_grid)) else: pitch = Pitch() part[-1].append(Note(pitch, quarterLength=config.time_grid)) # Set pitch value AFTER appending to measure in order to avoid unnecessary accidentals pitch.midi = idx + min_pitches[part_name] - len(indices_parts) if has_fermata: for part in parts.values(): fermata = Fermata() fermata.type = 'upright' part[-1][-1].expressions.append(fermata) score = Score() if metadata is not None: score.append(Metadata()) score.metadata.title = f"{metadata.title} ({metadata.number})" score.metadata.composer = f"Melody: {metadata.composer}\nArrangement: BachNet ({datetime.now().year})" for part in parts.values(): part[-1].rightBarline = 'light-heavy' score.append(parts['soprano']) if 'alto' in parts: score.append(parts['alto']) score.append(parts['tenor']) score.append(parts['bass']) score.stripTies(inPlace=True, retainContainers=True) return score
def __init__(self, ranges=False): score = self.score = Score() self.instruments = self.i = Instruments() self.parts = Parts(self.i) # Make Metadata timestamp = datetime.datetime.utcnow() metadata = Metadata() metadata.title = 'Early Montreal' metadata.composer = 'Jonathan Marmor' metadata.date = timestamp.strftime('%Y/%m/%d') score.insert(0, metadata) [score.insert(0, part) for part in self.parts.l] score.insert(0, StaffGroup(self.parts.l)) if ranges: # Don't make a piece, just show the instrument ranges for inst, part in zip(self.instruments.l, self.parts.l): measure = Measure() measure.timeSignature = TimeSignature('4/4') low = Note(inst.lowest_note) measure.append(low) high = Note(inst.highest_note) measure.append(high) part.append(measure) return # 18 to 21 minutes piece_duration_minutes = scale(random.random(), 0, 1, 18, 21) # Make the "songs" songs = [] total_minutes = 0 n = 1 while total_minutes < piece_duration_minutes: print 'Song {}'.format(n) n += 1 song = Song(self) songs.append(song) total_minutes += song.duration_minutes # Make notation previous_duration = None for song in songs: for bar in song.bars: for part in bar.parts: measure = Measure() if bar.tempo: measure.insert( 0, MetronomeMark(number=bar.tempo, referent=Duration(1))) measure.leftBarline = 'double' if bar.duration != previous_duration: ts = TimeSignature('{}/4'.format(bar.duration)) measure.timeSignature = ts # Fix Durations durations = [note['duration'] for note in part['notes']] components_list = split_at_beats(durations) components_list = [ join_quarters(note_components) for note_components in components_list ] for note, components in zip(part['notes'], components_list): note['durations'] = components for note in part['notes']: if note['pitch'] == 'rest': n = Rest() if isinstance(note['pitch'], list): pitches = [] for pitch_number in note['pitch']: p = Pitch(pitch_number) # Force all flats if p.accidental.name == 'sharp': p = p.getEnharmonic() pitches.append(p) n = Chord(notes=pitches) # TODO add slurs # TODO add glissandos # TODO add -50 cent marks else: p = Pitch(note['pitch']) # Force all flats if p.accidental.name == 'sharp': p = p.getEnharmonic() n = Note(p) # TODO add slurs # TODO add glissandos # TODO add -50 cent marks d = Duration() if note['duration'] == 0: d.quarterLength = .5 d = d.getGraceDuration() else: d.fill(note['durations']) n.duration = d measure.append(n) self.parts.d[part['instrument_name']].append(measure) previous_duration = bar.duration