Search code examples
pythonjupyter-notebookmidimagenta

How to Change Tempo of a Notes Sequence in Magenta?


I tried changing qpm. But it doesn't change anything. I want to change the tempo of the notes sequence. I am working on Colab, my notebook can be found here.

    from note_seq.protobuf import music_pb2
    from note_seq import sequences_lib
    
    twinkle_twinkle = music_pb2.NoteSequence()
    
    # Add the notes to the sequence.
    twinkle_twinkle.notes.add(pitch=60, start_time=0.0, end_time=0.5, velocity=80)
    twinkle_twinkle.notes.add(pitch=60, start_time=0.5, end_time=1.0, velocity=80)
    twinkle_twinkle.notes.add(pitch=67, start_time=1.0, end_time=1.5, velocity=80)
    twinkle_twinkle.notes.add(pitch=67, start_time=1.5, end_time=2.0, velocity=80)
    twinkle_twinkle.notes.add(pitch=69, start_time=2.0, end_time=2.5, velocity=80)
    twinkle_twinkle.notes.add(pitch=69, start_time=2.5, end_time=3.0, velocity=80)
    twinkle_twinkle.notes.add(pitch=67, start_time=3.0, end_time=4.0, velocity=80)
    twinkle_twinkle.notes.add(pitch=65, start_time=4.0, end_time=4.5, velocity=80)
    twinkle_twinkle.notes.add(pitch=65, start_time=4.5, end_time=5.0, velocity=80)
    twinkle_twinkle.notes.add(pitch=64, start_time=5.0, end_time=5.5, velocity=80)
    twinkle_twinkle.notes.add(pitch=64, start_time=5.5, end_time=6.0, velocity=80)
    twinkle_twinkle.notes.add(pitch=62, start_time=6.0, end_time=6.5, velocity=80)
    twinkle_twinkle.notes.add(pitch=62, start_time=6.5, end_time=7.0, velocity=80)
    twinkle_twinkle.notes.add(pitch=60, start_time=7.0, end_time=8.0, velocity=80) 
    twinkle_twinkle.total_time = 8
    
    twinkle_twinkle.tempos.add(qpm=120);
    
    
    # This is a colab utility method that visualizes a NoteSequence.
    note_seq.plot_sequence(twinkle_twinkle)
    
    # This is a colab utility method that plays a NoteSequence.
    note_seq.play_sequence(twinkle_twinkle,synth=note_seq.fluidsynth)


Solution

  • You can find several examples at magenta/demos in github. You can find there this solution. Hope it helps:

    from note_seq.protobuf import music_pb2
    import copy
    
    def change_tempo(note_sequence, new_tempo):
        new_sequence = copy.deepcopy(note_sequence)
        ratio = note_sequence.tempos[0].qpm / new_tempo
        for note in new_sequence.notes:
            note.start_time = note.start_time * ratio
            note.end_time = note.end_time * ratio
        new_sequence.tempos[0].qpm = new_tempo
        return new_sequence
    
    twinkle_twinkle = music_pb2.NoteSequence()
    
    # Add the notes to the sequence.
    twinkle_twinkle.notes.add(pitch=60, start_time=0.0, end_time=0.5, velocity=80)
    twinkle_twinkle.notes.add(pitch=60, start_time=0.5, end_time=1.0, velocity=80)
    twinkle_twinkle.notes.add(pitch=67, start_time=1.0, end_time=1.5, velocity=80)
    twinkle_twinkle.notes.add(pitch=67, start_time=1.5, end_time=2.0, velocity=80)
    twinkle_twinkle.notes.add(pitch=69, start_time=2.0, end_time=2.5, velocity=80)
    twinkle_twinkle.notes.add(pitch=69, start_time=2.5, end_time=3.0, velocity=80)
    twinkle_twinkle.notes.add(pitch=67, start_time=3.0, end_time=4.0, velocity=80)
    twinkle_twinkle.notes.add(pitch=65, start_time=4.0, end_time=4.5, velocity=80)
    twinkle_twinkle.notes.add(pitch=65, start_time=4.5, end_time=5.0, velocity=80)
    twinkle_twinkle.notes.add(pitch=64, start_time=5.0, end_time=5.5, velocity=80)
    twinkle_twinkle.notes.add(pitch=64, start_time=5.5, end_time=6.0, velocity=80)
    twinkle_twinkle.notes.add(pitch=62, start_time=6.0, end_time=6.5, velocity=80)
    twinkle_twinkle.notes.add(pitch=62, start_time=6.5, end_time=7.0, velocity=80)
    twinkle_twinkle.notes.add(pitch=60, start_time=7.0, end_time=8.0, velocity=80) 
    twinkle_twinkle.total_time = 8
    
    twinkle_twinkle.tempos.add(qpm=60);
    
    # New note sequence with changed tempo
    twinkle_twinkle_tempo_changed = change_tempo(twinkle_twinkle, new_tempo = 130)
    
    # This is a colab utility method that visualizes a NoteSequence.
    note_seq.plot_sequence(twinkle_twinkle)
    
    # This is a colab utility method that plays a NoteSequence.
    note_seq.play_sequence(twinkle_twinkle,synth=note_seq.fluidsynth)
    note_seq.play_sequence(twinkle_twinkle_tempo_changed,synth=note_seq.fluidsynth)