File Coverage

blib/lib/MIDI.pm
Criterion Covered Total %
statement 25 27 92.5
branch 5 6 83.3
condition 3 3 100.0
subroutine 7 7 100.0
pod n/a
total 40 43 93.0


line stmt bran cond sub pod time code
1              
2             # Time-stamp: "2010-02-14 21:39:10 conklin"
3             require 5;
4             package MIDI;
5 11     11   83947 use strict;
  11         30  
  11         521  
6 11         1347 use vars qw($Debug $VERSION %number2note %note2number %number2patch
7 11     11   58 %patch2number %notenum2percussion %percussion2notenum);
  11         22  
8 11     11   11041 use MIDI::Opus;
  11         33  
  11         364  
9 11     11   8508 use MIDI::Track;
  11         32  
  11         335  
10 11     11   11290 use MIDI::Event;
  11         37  
  11         869  
11 11     11   8633 use MIDI::Score;
  11         28  
  11         10940  
12              
13             # Doesn't use MIDI::Simple -- but MIDI::Simple uses this
14              
15             $Debug = 0; # currently doesn't do anything
16             $VERSION = '0.83';
17              
18             # MIDI.pm doesn't do much other than 1) 'use' all the necessary submodules
19             # 2) provide some publicly useful hashes, 3) house a few private routines
20             # common to the MIDI::* modules, and 4) contain POD, glorious POD.
21              
22             =head1 NAME
23              
24             MIDI - read, compose, modify, and write MIDI files
25              
26             =head1 SYNOPSIS
27              
28             use MIDI;
29             use strict;
30             use warnings;
31             my @events = (
32             ['text_event',0, 'MORE COWBELL'],
33             ['set_tempo', 0, 450_000], # 1qn = .45 seconds
34             );
35              
36             for (1 .. 20) {
37             push @events,
38             ['note_on' , 90, 9, 56, 127],
39             ['note_off', 6, 9, 56, 127],
40             ;
41             }
42             foreach my $delay (reverse(1..96)) {
43             push @events,
44             ['note_on' , 0, 9, 56, 127],
45             ['note_off', $delay, 9, 56, 127],
46             ;
47             }
48              
49             my $cowbell_track = MIDI::Track->new({ 'events' => \@events });
50             my $opus = MIDI::Opus->new(
51             { 'format' => 0, 'ticks' => 96, 'tracks' => [ $cowbell_track ] } );
52             $opus->write_to_file( 'cowbell.mid' );
53              
54              
55             =head1 DESCRIPTION
56              
57             This suite of modules provides routines for reading, composing, modifying,
58             and writing MIDI files.
59              
60             From FOLDOC (C):
61              
62             =over
63              
64             B
65            
66             Emultimedia, file formatE (MIDI /mi'-dee/, /mee'-dee/) A
67             hardware specification and protocol used to communicate note and
68             effect information between synthesisers, computers, music keyboards,
69             controllers and other electronic music devices. [...]
70              
71             The basic unit of information is a "note on/off" event which includes
72             a note number (pitch) and key velocity (loudness). There are many
73             other message types for events such as pitch bend, patch changes and
74             synthesizer-specific events for loading new patches etc.
75              
76             There is a file format for expressing MIDI data which is like a dump
77             of data sent over a MIDI port. [...]
78              
79             =back
80              
81             =head1 COMPONENTS
82              
83             The MIDI-Perl suite consists of these modules:
84              
85             L (which you're looking at), L, L,
86             L, L, and
87             L. All of these contain documentation in pod format.
88             You should read all of these pods.
89              
90             The order you want to read them in will depend on what you want to do
91             with this suite of modules: if you are focused on manipulating the
92             guts of existing MIDI files, read the pods in the order given above.
93              
94             But if you aim to compose music with this suite, read this pod, then
95             L and L, and then skim the rest.
96              
97              
98             =head1 INTRODUCTION
99              
100             This suite of modules is basically object-oriented, with the exception
101             of MIDI::Simple. MIDI opuses ("songs") are represented as objects
102             belonging to the class MIDI::Opus. An opus contains tracks, which are
103             objects belonging to the class MIDI::Track. A track will generally
104             contain a list of events, where each event is a list consisting of a
105             command, a delta-time, and some number of parameters. In other words,
106             opuses and tracks are objects, and the events in a track comprise a
107             LoL (and if you don't know what an LoL is, you must read L).
108              
109             Furthermore, for some purposes it's useful to analyze the totality of
110             a track's events as a "score" -- where a score consists of notes where
111             each event is a list consisting of a command, a time offset from the
112             start of the track, and some number of parameters. This is the level
113             of abstraction that MIDI::Score and MIDI::Simple deal with.
114              
115             While this suite does provide some functionality accessible only if
116             you're comfortable with various kinds of references, and while there
117             are some options that deal with the guts of MIDI encoding, you can (I
118             hope) get along just fine with just a basic grasp of the MIDI
119             "standard", and a command of LoLs. I have tried, at various points in
120             this documentation, to point out what things are not likely to be of
121             use to the casual user.
122              
123             =head1 GOODIES
124              
125             The bare module MIDI.pm doesn't I much more than C the
126             necessary component submodules (i.e., all except MIDI::Simple). But
127             it does provide some hashes you might find useful:
128              
129             =over
130              
131             =cut
132              
133             ###########################################################################
134             # Note numbers => a representation of them
135              
136             =item C<%MIDI::note2number> and C<%MIDI::number2note>
137              
138             C<%MIDI::number2note> correponds MIDI note numbers to a more
139             comprehensible representation (e.g., 68 to 'Gs4', for G-sharp, octave
140             4); C<%MIDI::note2number> is the reverse. Have a look at the source
141             to see the contents of the hash.
142              
143             =cut
144             @number2note{0 .. 127} = (
145             # (Do) (Re) (Mi) (Fa) (So) (La) (Ti)
146             'C0', 'Cs0', 'D0', 'Ds0', 'E0', 'F0', 'Fs0', 'G0', 'Gs0', 'A0', 'As0', 'B0',
147             'C1', 'Cs1', 'D1', 'Ds1', 'E1', 'F1', 'Fs1', 'G1', 'Gs1', 'A1', 'As1', 'B1',
148             'C2', 'Cs2', 'D2', 'Ds2', 'E2', 'F2', 'Fs2', 'G2', 'Gs2', 'A2', 'As2', 'B2',
149             'C3', 'Cs3', 'D3', 'Ds3', 'E3', 'F3', 'Fs3', 'G3', 'Gs3', 'A3', 'As3', 'B3',
150             'C4', 'Cs4', 'D4', 'Ds4', 'E4', 'F4', 'Fs4', 'G4', 'Gs4', 'A4', 'As4', 'B4',
151             'C5', 'Cs5', 'D5', 'Ds5', 'E5', 'F5', 'Fs5', 'G5', 'Gs5', 'A5', 'As5', 'B5',
152             'C6', 'Cs6', 'D6', 'Ds6', 'E6', 'F6', 'Fs6', 'G6', 'Gs6', 'A6', 'As6', 'B6',
153             'C7', 'Cs7', 'D7', 'Ds7', 'E7', 'F7', 'Fs7', 'G7', 'Gs7', 'A7', 'As7', 'B7',
154             'C8', 'Cs8', 'D8', 'Ds8', 'E8', 'F8', 'Fs8', 'G8', 'Gs8', 'A8', 'As8', 'B8',
155             'C9', 'Cs9', 'D9', 'Ds9', 'E9', 'F9', 'Fs9', 'G9', 'Gs9', 'A9', 'As9', 'B9',
156             'C10','Cs10','D10','Ds10','E10','F10','Fs10','G10',
157             # Note number 69 reportedly == A440, under a default tuning.
158             # and note 60 = Middle C
159             );
160             %note2number = reverse %number2note;
161             # Note how I deftly avoid having to figure out how to represent a flat mark
162             # in ASCII.
163              
164             ###########################################################################
165             # **** TABLE 1 - General MIDI Instrument Patch Map ****
166             # (groups sounds into sixteen families, w/8 instruments in each family)
167             # Note that I call the map 0-127, not 1-128.
168              
169             =item C<%MIDI::patch2number> and C<%MIDI::number2patch>
170              
171             C<%MIDI::number2patch> correponds General MIDI patch numbers
172             (0 to 127) to English names (e.g., 79 to 'Ocarina');
173             C<%MIDI::patch2number> is the reverse. Have a look at the source
174             to see the contents of the hash.
175              
176             =cut
177             @number2patch{0 .. 127} = ( # The General MIDI map: patches 0 to 127
178             #0: Piano
179             "Acoustic Grand", "Bright Acoustic", "Electric Grand", "Honky-Tonk",
180             "Electric Piano 1", "Electric Piano 2", "Harpsichord", "Clav",
181             # Chrom Percussion
182             "Celesta", "Glockenspiel", "Music Box", "Vibraphone",
183             "Marimba", "Xylophone", "Tubular Bells", "Dulcimer",
184              
185             #16: Organ
186             "Drawbar Organ", "Percussive Organ", "Rock Organ", "Church Organ",
187             "Reed Organ", "Accordion", "Harmonica", "Tango Accordion",
188             # Guitar
189             "Acoustic Guitar(nylon)", "Acoustic Guitar(steel)",
190             "Electric Guitar(jazz)", "Electric Guitar(clean)",
191             "Electric Guitar(muted)", "Overdriven Guitar",
192             "Distortion Guitar", "Guitar Harmonics",
193              
194             #32: Bass
195             "Acoustic Bass", "Electric Bass(finger)",
196             "Electric Bass(pick)", "Fretless Bass",
197             "Slap Bass 1", "Slap Bass 2", "Synth Bass 1", "Synth Bass 2",
198             # Strings
199             "Violin", "Viola", "Cello", "Contrabass",
200             "Tremolo Strings", "Pizzicato Strings", "Orchestral Strings", "Timpani",
201              
202             #48: Ensemble
203             "String Ensemble 1", "String Ensemble 2", "SynthStrings 1", "SynthStrings 2",
204             "Choir Aahs", "Voice Oohs", "Synth Voice", "Orchestra Hit",
205             # Brass
206             "Trumpet", "Trombone", "Tuba", "Muted Trumpet",
207             "French Horn", "Brass Section", "SynthBrass 1", "SynthBrass 2",
208              
209             #64: Reed
210             "Soprano Sax", "Alto Sax", "Tenor Sax", "Baritone Sax",
211             "Oboe", "English Horn", "Bassoon", "Clarinet",
212             # Pipe
213             "Piccolo", "Flute", "Recorder", "Pan Flute",
214             "Blown Bottle", "Skakuhachi", "Whistle", "Ocarina",
215              
216             #80: Synth Lead
217             "Lead 1 (square)", "Lead 2 (sawtooth)", "Lead 3 (calliope)", "Lead 4 (chiff)",
218             "Lead 5 (charang)", "Lead 6 (voice)", "Lead 7 (fifths)", "Lead 8 (bass+lead)",
219             # Synth Pad
220             "Pad 1 (new age)", "Pad 2 (warm)", "Pad 3 (polysynth)", "Pad 4 (choir)",
221             "Pad 5 (bowed)", "Pad 6 (metallic)", "Pad 7 (halo)", "Pad 8 (sweep)",
222              
223             #96: Synth Effects
224             "FX 1 (rain)", "FX 2 (soundtrack)", "FX 3 (crystal)", "FX 4 (atmosphere)",
225             "FX 5 (brightness)", "FX 6 (goblins)", "FX 7 (echoes)", "FX 8 (sci-fi)",
226             # Ethnic
227             "Sitar", "Banjo", "Shamisen", "Koto",
228             "Kalimba", "Bagpipe", "Fiddle", "Shanai",
229              
230             #112: Percussive
231             "Tinkle Bell", "Agogo", "Steel Drums", "Woodblock",
232             "Taiko Drum", "Melodic Tom", "Synth Drum", "Reverse Cymbal",
233             # Sound Effects
234             "Guitar Fret Noise", "Breath Noise", "Seashore", "Bird Tweet",
235             "Telephone Ring", "Helicopter", "Applause", "Gunshot",
236             );
237             %patch2number = reverse %number2patch;
238              
239             ###########################################################################
240             # **** TABLE 2 - General MIDI Percussion Key Map ****
241             # (assigns drum sounds to note numbers. MIDI Channel 9 is for percussion)
242             # (it's channel 10 if you start counting at 1. But WE start at 0.)
243              
244             =item C<%MIDI::notenum2percussion> and C<%MIDI::percussion2notenum>
245              
246             C<%MIDI::notenum2percussion> correponds General MIDI Percussion Keys
247             to English names (e.g., 56 to 'Cowbell') -- but note that only numbers
248             35 to 81 (inclusive) are defined; C<%MIDI::percussion2notenum> is the
249             reverse. Have a look at the source to see the contents of the hash.
250              
251             =cut
252              
253             @notenum2percussion{35 .. 81} = (
254             'Acoustic Bass Drum', 'Bass Drum 1', 'Side Stick', 'Acoustic Snare',
255             'Hand Clap',
256              
257             # the forties
258             'Electric Snare', 'Low Floor Tom', 'Closed Hi-Hat', 'High Floor Tom',
259             'Pedal Hi-Hat', 'Low Tom', 'Open Hi-Hat', 'Low-Mid Tom', 'Hi-Mid Tom',
260             'Crash Cymbal 1',
261              
262             # the fifties
263             'High Tom', 'Ride Cymbal 1', 'Chinese Cymbal', 'Ride Bell', 'Tambourine',
264             'Splash Cymbal', 'Cowbell', 'Crash Cymbal 2', 'Vibraslap', 'Ride Cymbal 2',
265              
266             # the sixties
267             'Hi Bongo', 'Low Bongo', 'Mute Hi Conga', 'Open Hi Conga', 'Low Conga',
268             'High Timbale', 'Low Timbale', 'High Agogo', 'Low Agogo', 'Cabasa',
269              
270             # the seventies
271             'Maracas', 'Short Whistle', 'Long Whistle', 'Short Guiro', 'Long Guiro',
272             'Claves', 'Hi Wood Block', 'Low Wood Block', 'Mute Cuica', 'Open Cuica',
273              
274             # the eighties
275             'Mute Triangle', 'Open Triangle',
276             );
277             %percussion2notenum = reverse %notenum2percussion;
278              
279             ###########################################################################
280              
281             =back
282              
283             =head1 BRIEF GLOSSARY
284              
285             This glossary defines just a few terms, just enough so you can
286             (hopefully) make some sense of the documentation for this suite of
287             modules. If you're going to do anything serious with these modules,
288             however, you I invest in a good book about the MIDI
289             standard -- see the References.
290              
291             B: a logical channel to which control changes and patch
292             changes apply, and in which MIDI (note-related) events occur.
293              
294             B: one of the various numeric parameters associated with a
295             given channel. Like S registers in Hayes-set modems, MIDI controls
296             consist of a few well-known registers, and beyond that, it's
297             patch-specific and/or sequencer-specific.
298              
299             B: the time (in ticks) that a sequencer should wait
300             between playing the previous event and playing the current event.
301              
302             B: any of a mixed bag of events whose common trait is
303             merely that they are similarly encoded. Most meta-events apply to all
304             channels, unlike events, which mostly apply to just one channel.
305              
306             B: my oversimplistic term for items in a score structure.
307              
308             B: the term I prefer for a piece of music, as represented in
309             MIDI. Most specs use the term "song", but I think that this
310             falsely implies that MIDI files represent vocal pieces.
311              
312             B: an electronic model of the sound of a given notional
313             instrument.
314              
315             B: a form of modest compression where an event lacking
316             an event command byte (a "status" byte) is to be interpreted as having
317             the same event command as the preceding event -- which may, in turn,
318             lack a status byte and may have to be interpreted as having the same
319             event command as I previous event, and so on back.
320              
321             B: a structure of notes like an event structure, but where
322             notes are represented as single items, and where timing of items is
323             absolute from the beginning of the track, instead of being represented
324             in delta-times.
325              
326             B: what some MIDI specs call a song, I call an opus.
327              
328             B: a device or program that interprets and acts on MIDI
329             data. This prototypically refers to synthesizers or drum machines,
330             but can also refer to more limited devices, such as mixers or even
331             lighting control systems.
332              
333             B: a synonym for "event".
334              
335             B: a chunk of binary data encapsulated in the MIDI data stream,
336             for whatever purpose.
337              
338             B: any of the several meta-events (one of which is
339             actually called 'text_event') that conveys text. Most often used to
340             just label tracks, note the instruments used for a track, or to
341             provide metainformation about copyright, performer, and piece title
342             and author.
343              
344             B: the timing unit in a MIDI opus.
345              
346             B: an encoding method identical to what Perl
347             calls the 'w' (BER, Basic Encoding Rules) pack/unpack format for
348             integers.
349              
350             =head1 SEE ALSO
351              
352             L -- the MIDI-Perl homepage
353             on the Interwebs!
354              
355             L -- All the MIDI
356             things in CPAN!
357              
358             =head1 REFERENCES
359              
360             Christian Braut. I ISBN 0782112854.
361             [This one is indispensible, but sadly out of print. Look at abebooks.com
362             for it maybe --SMB]
363              
364             Langston, Peter S. 1998. "Little Music Languages", p.587-656 in:
365             Salus, Peter H,. editor in chief, /Handbook of Programming Languages/,
366             vol. 3. MacMillan Technical, 1998. [The volume it's in is probably
367             not worth the money, but see if you can at least glance at this
368             article anyway. It's not often you see 70 pages written on music
369             languages. --SMB]
370              
371             =head1 COPYRIGHT
372              
373             Copyright (c) 1998-2005 Sean M. Burke. All rights reserved.
374              
375             This library is free software; you can redistribute it and/or
376             modify it under the same terms as Perl itself.
377              
378             =head1 AUTHORS
379              
380             Sean M. Burke C (until 2010)
381              
382             Darrell Conklin C (from 2010)
383             =cut
384              
385             ###########################################################################
386             sub _dump_quote {
387             # Used variously by some MIDI::* modules. Might as well keep it here.
388 27     27   67 my @stuff = @_;
389             return
390             join(", ",
391             map
392             { # the cleaner-upper function
393 27 100 100     42 if(!length($_)) { # empty string
  150 100       845  
    50          
394 1         105 "''";
395             } elsif(
396             $_ eq '0' or m/^-?(?:[1-9]\d*)$/s # integers
397              
398             # Was just: m/^-?\d+(?:\.\d+)?$/s
399             # but that's over-broad, as let "0123" thru, which is
400             # wrong, since that's octal 0123, == decimal 83.
401              
402             # m/^-?(?:(?:[1-9]\d*)|0)(?:\.\d+)?$/s and $_ ne '-0'
403             # would let thru all well-formed numbers, but also
404             # non-canonical forms of them like 0.3000000.
405             # Better to just stick to integers I think.
406             ) {
407 121         2988 $_;
408             } elsif( # text with junk in it
409             s<([^\x20\x21\x23\x27-\x3F\x41-\x5B\x5D-\x7E])>
410 0         0 <'\\x'.(unpack("H2",$1))>eg
411             ) {
412 0         0 "\"$_\"";
413             } else { # text with no junk in it
414 28         32 s<'><\\'>g;
415 28         165 "\'$_\'";
416             }
417             }
418             @stuff
419             );
420             }
421             ###########################################################################
422              
423             1;
424              
425             __END__