Difference between revisions of "MIDI file parsing homework"

From CCARH Wiki
Jump to navigation Jump to search
 
(26 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
== Assignment ==
 
== Assignment ==
  
# Find a MIDI file on the web.  Tip: include <em>[https://www.google.com/search?q=filetype%3Amidi&lr=lang_en filetype:midi]</em>or <em>[https://www.google.com/search?q=filetype%3Amid&lr=lang_en filetype:mid]</em> when searching for MIDI files in Google searches.
+
# Find a MIDI file on the web or export MIDI from MuseScore.   
# Save the MIDI file to your desktop and open it (usually double-clicking to open with the default MIDI playing program on your computer). 
+
# Drag-and-drop the MIDI file into the box below to convert it to hex bytes: <br> <html><iframe style="overflow: hidden; width:750px; height:700px;" src="https://ccrma.stanford.edu/~craig/binasc"></iframe></html>
# If it plays correctly, drag and drop the MIDI file from your desktop onto the [http://www.ccarh.org/software/binasc binasc online] webpage.
+
# Parse the resulting display of the MIDI file's hexadecimal bytes by hand.  Make note of the number of tracks in the file; whether it is type-0 or type-1 (or type-2), and how many ticks per quarter note are used for basic timing in the file.
# Parse the resulting display of the MIDI file's hexadecimal bytes by hand.  Make note of the number of tracks in the file; whether it is type-0 or type-1 (or type-2), and how many ticks per quarter note (or SMPTE frames/subframes) are used for basic timing in the file.
+
# Parse at least 25 delta-time/message pairs in one of the tracks of the MIDI File.  Note that type-1 files only store meta messages such as tempo changes in the first track.  If your file  is a type-1 file, try parsing a few of these meta messages, and then switch to another track with notes.
# Parse at least 25 time/message pairs in one of the tracks of the MIDI File.  Note that type-1 files only stores meta messages such as tempo changes in the first track.  If your file  is a type-1 file, try parsing a few of these meta messages, and then switch to another track with notes.
 
 
# Give the basic meaning of MIDI messages (such as this is a note-on messages for pitch A4, or this is a tempo meta message).
 
# Give the basic meaning of MIDI messages (such as this is a note-on messages for pitch A4, or this is a tempo meta message).
 +
# Does the MIDI file use "running status"?  Running status would result in the first byte of a MIDI message in any (delta-time/message) pair to be a data byte rather than a command-byte (and the last command-byte in the track would be the inferred command byte).
 
# Decoding VLVs for delta timings is optional.
 
# Decoding VLVs for delta timings is optional.
 +
# Does the MIDI file look like it was performed live? or does it represent quantized/proportional time?  In other words, is there a variety of VHV or decoded delta ticks, or are there only a small set of unique delta times?
 
# <em>Extra Credit</em>: Write your own Standard MIDI File parser which will do this homework for you.
 
# <em>Extra Credit</em>: Write your own Standard MIDI File parser which will do this homework for you.
# Email MIDI file and your parsing to you know whos.
+
# Email MIDI file and your parsing to you-know-whos.
  
 
== Example ==
 
== Example ==
  
Here is the simple example done in class using [[File:twinkle.mid]].  Converting bytes into a list of hexadecimal numbers with [http://www.ccarh.org/software/binasc binasc online]:
+
Here is the simple example done in class using [[File:twinkle.mid]]:
  
 
  4d 54 68 64 00 00 00 06 00 00 00 01 00 80 4d 54 72 6b 00 00 00 8c 00 ff 58   
 
  4d 54 68 64 00 00 00 06 00 00 00 01 00 80 4d 54 72 6b 00 00 00 8c 00 ff 58   
Line 23: Line 24:
 
  90 3c 1e 82 00 90 3c 00 00 ff 2f 00
 
  90 3c 1e 82 00 90 3c 00 00 ff 2f 00
  
Example parsing to be done by hand (don't worry about unpacking VLV timestamps), or write your own extra-credit MIDI file parser:
+
Basic parsing:
 +
 
 +
4d 54 68 64                    ; ASCII characters "MThd"
 +
00 00 00 06                    ; Number of bytes in header to follow: 6
 +
00 00                          ; MIDI file type: 0
 +
00 01                          ; Number of tracks: 1
 +
00 80                          ; Ticks per quarter note: 128
 
   
 
   
4d 54 68 64      MIDI header chunk marker ("MThd")
+
4d 54 72 6b                    ; ASCII characters "MTrk"
 +
00 00 00 8c                    ; Number of bytes in track to follow: 140
 +
00                            ; 0 ticks
 +
        ff 58 04 04 02 30 08    ; time signature meta event
 +
00                            ; 0 ticks
 +
        ff 59 02 00 00          ; key signature meta event
 +
00                            ; 0 ticks
 +
        90 3c 28                ; note-on C4
 +
81 00                          ; 128 ticks
 +
        90 3c 00                ; note off C4
 +
00                            ; 0 ticks
 +
        90 3c 1e                ; note-on C4
 +
81 00                          ; 128 ticks
 +
        90 3c 00                ; note off C4
 +
00                            ; 0 ticks
 +
        90 43 2d                ; note-on
 +
81 00                          ; 128 ticks
 +
        90 43 00                ; note-off
 +
00                            ; 0 ticks
 +
        90 43 32                ; note-on
 +
81 00                          ; 128 ticks
 +
        90 43 00                ; note-off
 +
00                            ; 0 ticks
 +
        90 45 2d                ; note-on
 +
81 00                          ; 128 ticks
 +
        90 45 00                ; note-off
 +
00                            ; 0 ticks
 +
        90 45 32                ; note-on
 +
81 00                          ; 128 ticks
 +
        90 45 00                ; note-off
 +
00                            ; 0 ticks
 +
        90 43 23                ; note-on
 +
82 00                          ; 256 ticks
 +
        90 43 00                ; note-off
 +
00                            ; 0 ticks
 +
        90 41 32                ; note-on
 +
81 00                          ; 128 ticks
 +
        90 41 00                ; note-off
 +
00                            ; 0 ticks
 +
        90 41 2d                ; note-on
 +
81 00                          ; 128 ticks
 +
        90 41 00                ; note-off
 +
00                            ; 0 ticks
 +
        90 40 32                ; note-on
 +
40                            ; 64 ticks
 +
        90 40 00                ; note-off
 +
40                            ; 64 ticks
 +
        90 40 28                ; note-on
 +
40                            ; 64 ticks
 +
        90 40 00                ; note-off
 +
40                            ; 64 ticks
 +
        90 3e 2d                ; note-on
 +
40                            ; 64 ticks
 +
        90 3e 00                ; note-off
 +
40                            ; 64 ticks
 +
        90 3e 32                ; note-on
 +
40                            ; 64 ticks
 +
        90 3e 00                ; note-off
 +
40                            ; 64 ticks
 +
        90 3c 1e                ; note-on
 +
82 00                          ; 256 ticks
 +
        90 3c 00                ; note-off
 +
00                            ; 0 ticks
 +
        ff 2f 00
 +
 
 +
Example of more decoding VLV numbers and converting MIDI data bytes to decimal:
 +
 
 +
4d 54 68 64      MIDI header chunk marker ("MThd")
 
  00 00 00 06      Bytes to follow in header chunk (6)
 
  00 00 00 06      Bytes to follow in header chunk (6)
 
  00 00            file format: Type-0 (0 == single track format)
 
  00 00            file format: Type-0 (0 == single track format)
Line 37: Line 111:
 
   
 
   
 
   
 
   
  VLV    MIDI message
+
  Delta time  /  MIDI message
 
  ============================================
 
  ============================================
  0      ff 58 '4 4 2 30 8      Meta-message for tempo
+
  0      ff 58 '4 4 2 30 8      Meta-message for time signature 4/; 48 ticks per metronome click;  8 32nds per beat
  0      ff 59 '2 0 0            Meta-message for time signature
+
  0      ff 59 '2 0 0            Meta-message for key signature: C major
 
  0      90 '60 '40              Note on, C4
 
  0      90 '60 '40              Note on, C4
  128    90 '60  0               Note off, C4
+
  128    90 '60  0             Note off, C4
 
  0      90 '60 '30              etc.
 
  0      90 '60 '30              etc.
 
  128    90 '60  0
 
  128    90 '60  0
Line 70: Line 144:
 
  256    90 '60  0
 
  256    90 '60  0
 
  0      ff 2f 0                Meta-message for end-of-track
 
  0      ff 2f 0                Meta-message for end-of-track
 +
 +
Notice that this MIDI file was clearly not performed live.  The delta-time durations are: 0, 64, 128, and 265 which are all multiples of two from each other (representing eighth notes, quarter notes and half notes).

Latest revision as of 07:44, 25 January 2022

Assignment

  1. Find a MIDI file on the web or export MIDI from MuseScore.
  2. Drag-and-drop the MIDI file into the box below to convert it to hex bytes:
  3. Parse the resulting display of the MIDI file's hexadecimal bytes by hand. Make note of the number of tracks in the file; whether it is type-0 or type-1 (or type-2), and how many ticks per quarter note are used for basic timing in the file.
  4. Parse at least 25 delta-time/message pairs in one of the tracks of the MIDI File. Note that type-1 files only store meta messages such as tempo changes in the first track. If your file is a type-1 file, try parsing a few of these meta messages, and then switch to another track with notes.
  5. Give the basic meaning of MIDI messages (such as this is a note-on messages for pitch A4, or this is a tempo meta message).
  6. Does the MIDI file use "running status"? Running status would result in the first byte of a MIDI message in any (delta-time/message) pair to be a data byte rather than a command-byte (and the last command-byte in the track would be the inferred command byte).
  7. Decoding VLVs for delta timings is optional.
  8. Does the MIDI file look like it was performed live? or does it represent quantized/proportional time? In other words, is there a variety of VHV or decoded delta ticks, or are there only a small set of unique delta times?
  9. Extra Credit: Write your own Standard MIDI File parser which will do this homework for you.
  10. Email MIDI file and your parsing to you-know-whos.

Example

Here is the simple example done in class using File:Twinkle.mid:

4d 54 68 64 00 00 00 06 00 00 00 01 00 80 4d 54 72 6b 00 00 00 8c 00 ff 58  
04 04 02 30 08 00 ff 59 02 00 00 00 90 3c 28 81 00 90 3c 00 00 90 3c 1e 81 
00 90 3c 00 00 90 43 2d 81 00 90 43 00 00 90 43 32 81 00 90 43 00 00 90 45 
2d 81 00 90 45 00 00 90 45 32 81 00 90 45 00 00 90 43 23 82 00 90 43 00 00 
90 41 32 81 00 90 41 00 00 90 41 2d 81 00 90 41 00 00 90 40 32 40 90 40 00 
40 90 40 28 40 90 40 00 40 90 3e 2d 40 90 3e 00 40 90 3e 32 40 90 3e 00 40 
90 3c 1e 82 00 90 3c 00 00 ff 2f 00

Basic parsing:

4d 54 68 64                    ; ASCII characters "MThd"
00 00 00 06                    ; Number of bytes in header to follow: 6
00 00                          ; MIDI file type: 0
00 01                          ; Number of tracks: 1
00 80                          ; Ticks per quarter note: 128

4d 54 72 6b                    ; ASCII characters "MTrk"
00 00 00 8c                    ; Number of bytes in track to follow: 140
00                             ; 0 ticks
       ff 58 04 04 02 30 08    ; time signature meta event
00                             ; 0 ticks
       ff 59 02 00 00          ; key signature meta event
00                             ; 0 ticks
       90 3c 28                ; note-on C4
81 00                          ; 128 ticks
       90 3c 00                ; note off C4
00                             ; 0 ticks
       90 3c 1e                ; note-on C4
81 00                          ; 128 ticks
       90 3c 00                ; note off C4
00                             ; 0 ticks
       90 43 2d                ; note-on
81 00                          ; 128 ticks
       90 43 00                ; note-off
00                             ; 0 ticks
       90 43 32                ; note-on
81 00                          ; 128 ticks
       90 43 00                ; note-off
00                             ; 0 ticks
       90 45 2d                ; note-on
81 00                          ; 128 ticks 
       90 45 00                ; note-off
00                             ; 0 ticks
       90 45 32                ; note-on
81 00                          ; 128 ticks
       90 45 00                ; note-off
00                             ; 0 ticks
       90 43 23                ; note-on
82 00                          ; 256 ticks
       90 43 00                ; note-off
00                             ; 0 ticks
       90 41 32                ; note-on
81 00                          ; 128 ticks
       90 41 00                ; note-off
00                             ; 0 ticks
       90 41 2d                ; note-on
81 00                          ; 128 ticks
       90 41 00                ; note-off
00                             ; 0 ticks
       90 40 32                ; note-on
40                             ; 64 ticks
       90 40 00                ; note-off
40                             ; 64 ticks
       90 40 28                ; note-on
40                             ; 64 ticks
       90 40 00                ; note-off
40                             ; 64 ticks
       90 3e 2d                ; note-on
40                             ; 64 ticks
       90 3e 00                ; note-off
40                             ; 64 ticks
       90 3e 32                ; note-on
40                             ; 64 ticks
       90 3e 00                ; note-off
40                             ; 64 ticks
       90 3c 1e                ; note-on
82 00                          ; 256 ticks
       90 3c 00                ; note-off
00                             ; 0 ticks
       ff 2f 00

Example of more decoding VLV numbers and converting MIDI data bytes to decimal:

4d 54 68 64      MIDI header chunk marker ("MThd")
00 00 00 06      Bytes to follow in header chunk (6)
00 00            file format: Type-0 (0 == single track format)
00 01            number of tracks (1)
00 80            ticks per quarter note (128)

First (and only track):

4d 54 72 6b       MIDI track chunk marker ("MTrk")
00 00 00 8c       Bytes to follow in track chunk (140 bytes)


Delta time  /   MIDI message
============================================
0       ff 58 '4 4 2 30 8       Meta-message for time signature 4/; 48 ticks per metronome click;  8 32nds per beat
0       ff 59 '2 0 0            Meta-message for key signature: C major
0       90 '60 '40              Note on, C4
128     90 '60   0              Note off, C4
0       90 '60 '30              etc.
128     90 '60   0
0       90 '67 '45
128     90 '67   0
0       90 '67 '50
128     90 '67   0 
0       90 '69 '45
128     90 '69   0
0       90 '69 '50
128     90 '69   0
0       90 '67 '35
256     90 '67   0
0       90 '65 '50
128     90 '65   0
0       90 '65 '45
128     90 '65   0
0       90 '64 '50
64      90 '64   0
64      90 '64 '40
64      90 '64   0
64      90 '62 '45
64      90 '62   0
64      90 '62 '50
64      90 '62   0
64      90 '60 '30
256     90 '60   0
0       ff 2f 0                Meta-message for end-of-track

Notice that this MIDI file was clearly not performed live. The delta-time durations are: 0, 64, 128, and 265 which are all multiples of two from each other (representing eighth notes, quarter notes and half notes).