Search code examples
pythondeep-learningmusic21

Unable to write 'rest' objects in midi file


I have a list containing several 'notes', 'chords' and 'rest' object and trying to write them into a midi file. I am trying with following code

midi_stream = stream.Stream(output_notes)
midi_stream.write('midi', fp='test_output.mid')

On reading the created midi file, I have found out no 'rest' objects were appended in midi file.

Content of output_notes variable is:

 <music21.note.Rest rest> <music21.chord.Chord A1 E2>
 <music21.chord.Chord A1 E2> <music21.chord.Chord A1 E2>
 <music21.note.Rest rest> <music21.chord.Chord A1 E2>
 <music21.note.Rest rest> <music21.note.Rest rest>
 <music21.chord.Chord A1 E2> <music21.chord.Chord A1 E2>
 <music21.note.Rest rest> <music21.note.Rest rest>
 <music21.note.Rest rest> <music21.note.Note A> <music21.note.Rest rest>
 <music21.note.Rest rest> <music21.note.Note A> <music21.note.Rest rest>
 <music21.note.Rest rest> <music21.note.Rest rest> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Rest rest> <music21.note.Rest rest>
 <music21.note.Rest rest> <music21.chord.Chord A4 C#5 F5>
 <music21.note.Rest rest> <music21.note.Rest rest>
 <music21.chord.Chord A4 C#5 F5> <music21.note.Note B>
 <music21.note.Rest rest> <music21.note.Rest rest>
 <music21.note.Rest rest> <music21.note.Rest rest>
 <music21.chord.Chord A4 C#5 F5> <music21.note.Rest rest>
 <music21.chord.Chord A4 C#5 F5> <music21.note.Rest rest>
 <music21.chord.Chord A4 C#5 F5> <music21.note.Rest rest>
 <music21.note.Rest rest> <music21.chord.Chord G4 B4 D5>
 <music21.chord.Chord F#4 B4 D5> <music21.chord.Chord F#4 B4 D5>
 <music21.chord.Chord F#4 B4 D5> <music21.chord.Chord F#4 B4 D5>
 <music21.note.Note C#> <music21.note.Rest rest> <music21.note.Rest rest>
 <music21.chord.Chord F#4 B4 D5> <music21.note.Rest rest>
 <music21.note.Rest rest> <music21.chord.Chord E3 A3>
 <music21.note.Rest rest> <music21.note.Note C> <music21.note.Rest rest>
 <music21.note.Rest rest> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.chord.Chord C3 G3>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.chord.Chord C3 G3> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Rest rest> <music21.note.Note G>
 <music21.chord.Chord C3 G3> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.chord.Chord C3 G3> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.chord.Chord C3 G3>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.chord.Chord C3 G3> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.chord.Chord C3 G3>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.chord.Chord C3 G3> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.chord.Chord C3 G3>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.chord.Chord C3 G3>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G>]

Content in created midi file is:

[<music21.chord.Chord A1 E2> <music21.chord.Chord A1 E2>
 <music21.chord.Chord A1 E2> <music21.chord.Chord A1 E2>
 <music21.chord.Chord A1 E2> <music21.note.Note A> <music21.note.Note G>
 <music21.note.Note G> <music21.chord.Chord A4 C#5 F5>
 <music21.note.Note B> <music21.chord.Chord A4 C#5 F5>
 <music21.chord.Chord A4 C#5 F5> <music21.note.Note G>
 <music21.chord.Chord B4 D5> <music21.note.Note F#>
 <music21.chord.Chord B4 D5> <music21.note.Note F#>
 <music21.chord.Chord B4 D5> <music21.note.Note C#>
 <music21.chord.Chord F#4 B4 D5> <music21.chord.Chord E3 A3>
 <music21.note.Note C> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.chord.Chord C3 G3> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.chord.Chord C3 G3>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.chord.Chord C3 G3>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.chord.Chord C3 G3> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.chord.Chord C3 G3>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.chord.Chord C3 G3>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.chord.Chord C3 G3> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.chord.Chord C3 G3> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.chord.Chord C3 G3> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.chord.Chord C3 G3> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G> <music21.note.Note G> <music21.note.Note G>
 <music21.note.Note G>]

Solution

  • What you have done is to take a music21 stream, convert it to MIDI, and then convert it back to a music21 stream, and you are surprised that the round-trip doesn't give you the same output as your original input.

    But if you were to convert, say, an Excel spreadsheet to a .csv and back, I think you would not be so surprised.

    The assumption you are making is that music21 and MIDI are two equivalent notations. They are not. music21 is oriented towards analysis of scores. It knows about notes, rests, bars, key-signatures and clefs (and a lot more besides).

    MIDI, on the other hand, is a synthesizer-oriented playback instruction set. Think of the piano-roll notation that you will see in some synth programs, for example this one: piano-roll notation.

    The black horizontal stripes indicate the notes that are played. But where are the rests? There aren't any, because there is no distinction between a note that is silent for the entire piece and a note in a bar that is interrupted by silence for the length of a crotchet/quarter note. That is why I said in my comment that MIDI doesn't actually have any rests. Rests are only there to show a player where to not play notes in a bar. But MIDI doesn't know anything about bars (if you see bars in a MIDI file they are essentially comments) and so it doesn't need rests either. When music21 imports from MIDI it does its best with the input, but guessing where a rest should go from the absence of notes is exceedingly tricky and you mustn't be surprised if music21 doesn't do as good a job as you would like.

    MIDI is a very less rich medium of music expression because it substitutes direct raw control of the target instrument for notations that make sense to humans. You don't need a double-sharp sign if all you have to do is add 2 (or 1, depending on context) to the note number in the message. That is why converting from music21 to MIDI and back is kind of like writing out a spreadsheet as a .csv and reading it back. Much information gets lost.

    There does not in fact exist an application-independent standard for music notation, and it certainly isn't MIDI. MusicXML tries to be that, but it doesn't cover everything, as you will discover if you use it to convert between, say, musci21, Sibelius and Dorico. Dorico in fact will ignore some spacing notations in MusicXML because it reckons it can do a better job itself.

    The real test of the absence of rests in your MIDI file is to play the MIDI file, and not to convert it to some other notation and treat that as a proxy for what is in the MIDI file. Can you hear the absence of rests in the file? Does the played output sound different from the original (ignoring non-note attributes like instrumentation, timbre, tempo, and similar)?