|
NAMEMIDI::Simple - procedural/OOP interface for MIDI compositionSYNOPSISuse MIDI::Simple; new_score; text_event 'http://www.ely.anglican.org/parishes/camgsm/bells/chimes.html'; text_event 'Lord through this hour/ be Thou our guide'; text_event 'so, by Thy power/ no foot shall slide'; set_tempo 500000; # 1 qn => .5 seconds (500,000 microseconds) patch_change 1, 8; # Patch 8 = Celesta noop c1, f, o5; # Setup # Now play n qn, Cs; n F; n Ds; n hn, Gs_d1; n qn, Cs; n Ds; n F; n hn, Cs; n qn, F; n Cs; n Ds; n hn, Gs_d1; n qn, Gs_d1; n Ds; n F; n hn, Cs; write_score 'westmister_chimes.mid'; DESCRIPTIONThis module sits on top of all the MIDI modules -- notably MIDI::Score (so you should skim MIDI::Score) -- and is meant to serve as a basic interface to them, for composition. By composition, I mean composing anew; you can use this module to add to or modify existing MIDI files, but that functionality is to be considered a bit experimental.This module provides two related but distinct bits of functionality: 1) a mini-language (implemented as procedures that can double as methods) for composing by adding notes to a score structure; and 2) simple functions for reading and writing scores, specifically the scores you make with the composition language. The fact that this module's interface is both procedural and object-oriented makes it a definite two-headed beast. The parts of the guts of the source code are not for the faint of heart. NOTE ON VERSION CHANGESThis module is somewhat incompatible with the MIDI::Simple versions before .700 (but that was a looong time ago).OBJECT STRUCTUREA MIDI::Simple object is a data structure with the following attributes:
Each package that you call the procedure "new_score" from, has a default MIDI::Simple object associated with it, and all the above attributes are accessible as: @Score $Time $Channel $Duration $Octave @Notes $Volume $Tempo %Cookies (Although I doubt you'll use these from any package other than "main".) If you don't know what a package is, don't worry about it. Just consider these attributes synonymous with the above-listed variables. Just start your programs with use MIDI::Simple; new_score; and you'll be fine. Routine/Method/ProcedureMIDI::Simple provides some pure functions (i.e., things that take input, and give a return value, and that's all they do), but what you're mostly interested in its routines. By "routine" I mean a subroutine that you call, whether as a procedure or as a method, and that affects data structures other than the return value.Here I'm using "procedure" to mean a routine you call like this: name(parameters...); # or, just maybe: name; (In technical terms, I mean a non-method subroutine that can have side effects, and which may not even provide a useful return value.) And I'm using "method" to mean a routine you call like this: $object->name(parameters); So bear these terms in mind when you see routines below that act like one, or the other, or both. MAIN ROUTINESThese are the most important routines:
Parameters for n/r/noopA parameter in an "n", "r", or "noop" call is meant to change an attribute (AKA state variable), namely Channel, Duration, Octave, Volume, or Notes.Here are the kinds of parameters you can use in calls to n/r/noop: * A numeric volume parameter. This has the form "V" followed by a positive integer in the range 0 (completely inaudible?) to 127 (AS LOUD AS POSSIBLE). Example: "V90" sets Volume to 90. * An alphanumeric volume parameter. This is a key from the hash %MIDI::Simple::Volume. Current legal values are "ppp", "pp", "p", "mp", "mezzo" (or "m"), "mf", "f", "ff", and "fff". Example: "ff" sets Volume to 112. (Note that "m" isn't a good bareword, so use "mezzo" instead, or just always remember to use quotes around "m".) * A numeric channel parameter. This has the form "c" followed by a positive integer 0 to 15. Example: "c2", to set Channel to 2. * A numeric duration parameter. This has the form "d" followed by a positive (presumably nonzero) integer. Example: "d48", to set Duration to 48. * An alphabetic (or in theory, possibly alphanumeric) duration parameter. This is a key from the hash %MIDI::Simple::Length. Current legal values start with "wn", "hn", "qn", "en", "sn" for whole, half, quarter, eighth, or sixteenth notes. Add "d" to the beginning of any of these to get "dotted..." (e.g., "dqn" for a dotted quarter note). Add "dd" to the beginning of any of that first list to get "double-dotted..." (e.g., "ddqn" for a double-dotted quarter note). Add "t" to the beginning of any of that first list to get "triplet..." (e.g., "tsn" for a triplet sixteenth note -- i.e. a note such that 3 of them add up to something as long as one eighth note). You may add to the contents of %MIDI::Simple::Length to support whatever abbreviations you want, as long as the parser can't mistake them for any other kind of n/r/noop parameter. * A numeric, absolute octave specification. This has the form: an "o" (lowercase oh), and then an integer in the range 0 to 10, representing an octave 0 to 10. The Octave attribute is used only in resolving relative note specifications, as explained further below in this section. (All absolute note specifications also set Octave to whatever octave they occur in.) * A numeric, relative octave specification. This has the form: "o_d" ("d" for down) or "o_u" ("u" for down), and then an integer. This increments, or decrements, Octave. E.g., if Octave is 6, "o_d2" will decrement Octave by 2, making it 4. If this moves Octave below 0, it is forced to 0. Or if it moves Octave above 10, it is forced to 10. (For more information, see the section "Invalid or Out-of-Range Parameters to n/r/noop", below.) * A numeric, absolute note specification. This has the form: an optional "n", and then an integer in the range 0 to 127, representing a note ranging from C0 to G10. The source to MIDI has a useful reference table showing the meanings of given note numbers. Examples: "n60", or "60", which each add a 60 to the list Notes. Since this is a kind of absolute note specification, it sets Octave to whatever octave the given numeric note occurs in. E.g., "n60" is "C5", and therefore sets Octave to 5. The setting of the Notes list is a bit special, compared to how setting the other attributes works. If there are any note specifications in a given parameter list for n, r, or noop, then all those specifications together are assigned to Notes. If there are no note specifications in the parameter list for n, r, or noop, then Notes isn't changed. (But see the description of "rest", at the end of this section.) So this: n mf, n40, n47, n50; sets Volume to 80, and Notes to (40, 47, 50). And it sets Octave, first to 3 (since n40 is in octave 3), then to 3 again (since n47 = B3), and then finally to 4 (since n50 = D4). Note that this is the same as: n n40, n47, n50, mf; The relative orders of parameters is usually irrelevant; but see the section "Order of Parameters in a Call to n/r/noop", below. * An alphanumeric, absolute note specification. These have the form: a string denoting a note within the octave (as determined by %MIDI::Simple::Note -- see below, in the description of alphanumeric, relative note specifications), and then a number denoting the octave number (in the range 0-10). Examples: "C3", "As4" or "Asharp4", "Bf9" or "Bflat9". Since this is a kind of absolute note specification, it sets Octave to whatever octave the given numeric note occurs in. E.g., "C3" sets Octave to 3, "As4" sets Octave to 4, and "Bflat9" sets Octave to 9. This: n E3, B3, D4, mf; does the same as this example of ours from before: n n40, n47, n50, mf; * An alphanumeric, relative note specification. These have the form: a string denoting a note within the octave (as determined by %MIDI::Simple::Note), and then an optional parameter "_u[number]" meaning "so many octaves up from the current octave" or "_d[parameter]" meaning "so many octaves down from the current octave". Examples: "C", "As" or "Asharp", "Bflat" or "Bf", "C_d3", "As_d1" or "Asharp_d1", "Bflat_u3" or "Bf_u3". In resolving what actual notes these kinds of specifications denote, the current value of Octave is used. What's a legal for the first bit (before any optional octave up/down specification) comes from the keys to the hash %MIDI::Simple::Note. The current acceptable values are: C (maps to the value 0) Cs or Df or Csharp or Dflat (maps to the value 1) D (maps to the value 2) Ds or Ef or Dsharp or Eflat (maps to the value 3) E (maps to the value 4) F (maps to the value 5) Fs or Gf or Fsharp or Gflat (maps to the value 6) G (maps to the value 7) Gs or Af or Gsharp or Aflat (maps to the value 8) A (maps to the value 9) As or Bf or Asharp or Bflat (maps to the value 10) B (maps to the value 11) (Note that these are based on the English names for these notes. If you prefer to add values to accomodate other strings denoting notes in the octave, you may do so by adding to the hash %MIDI::Simple::Note like so: use MIDI::Simple; %MIDI::Simple::Note = (%MIDI::Simple::Note, # keep all the old values 'H' => 10, 'Do' => 0, # ...etc... ); But the values you add must not contain any characters outside the range [A-Za-z\x80-\xFF]; and your new values must not look like anything that could be any other kind of specification. E.g., don't add "mf" or "o3" to %MIDI::Simple::Note.) Consider that these bits of code all do the same thing: n E3, B3, D4, mf; # way 1 n E3, B, D_u1, mf; # way 2 n o3, E, B, D_u1, mf; # way 3 noop o3, mf; # way 4 n E, B, D_u1; or even n o3, E, B, o4, D, mf; # way 5! n o6, E_d3, B_d3, D_d2, mf; # way 6! If a "_d[number]" would refer to a note in an octave below 0, it is forced into octave 0. If a "_u[number]" would refer to a note in an octave above 10, it is forced into octave 10. E.g., if Octave is 8, "G_u4" would resolve to the same as "G10" (not "G12" -- as that's out of range); if Octave is 2, "G_d4" would resolve to the same as "G0". (For more information, see the section "Invalid or Out-of-Range Parameters to n/r/noop", below.) * The string ""rest"" acts as a sort of note specification -- it sets Notes to empty-list. That way you can make a call to "n" actually make a rest: n qn, G; # makes a G quarter-note n hn, rest; # half-rest -- alters Notes, making it () n C,G; # half-note chord: simultaneous C and G r; # half-rest -- DOESN'T alter Notes. n qn; # quarter-note chord: simultaneous C and G n rest; # quarter-rest n; # another quarter-rest (If you can follow the above code, then you understand.) A ""rest"" that occurs in a parameter list with other note specs (e.g., "n qn, A, rest, G") has no effect, so don't do that. Order of Parameters in a Call to n/r/noopThe order of parameters in calls to n/r/noop is not important except insofar as the parameters change the Octave parameter, which may change how some relative note specifications are resolved. For example:noop o4, mf; n G, B, A3, C; is the same as "n mf, G4, B4, A3, C3". But just move that "C" to the start of the list: noop o4, mf; n C, G, B, A3; and you something different, equivalent to "n mf, C4, G4, B4, A3". But note that you can put the "mf" anywhere without changing anything. But stylistically, I strongly advise putting note parameters at the end of the parameter list: n mf, c10, C, B; # 1. good n C, B, mf, c10; # 2. bad n C, mf, c10, B; # 3. so bad! 3 is particularly bad because an uninformed/inattentive reader may get the impression that the C may be at a different volume and on a different channel than the B. (Incidentally, "n C5,G5" and "n G5,C5" are the same for most purposes, since the C and the G are played at the same time, and with the same parameters (channel and volume); but actually they differ in which note gets put in the Score first, and therefore which gets encoded first in the MIDI file -- but this makes no difference at all, unless you're manipulating the note-items in Score or the MIDI events in a track.) Invalid or Out-of-Range Parameters to n/r/noopIf a parameter in a call to n/r/noop is uninterpretable, Perl dies with an error message to that effect.If a parameter in a call to n/r/noop has an out-of-range value (like "o12" or "c19"), Perl dies with an error message to that effect. As somewhat of a merciful exception to this rule, if a parameter in a call to n/r/noop is a relative specification (whether like "o_d3" or "o_u3", or like "G_d3" or "G_u3") which happens to resolve to an out-of-range value (like "G_d3" given an Octave value of 2), then Perl will not die, but instead will silently try to bring that note back into range, by forcing it up to octave 0 (if it would have been lower), or down into 9 or 10 (if it would have been an octave higher than 10, or a note higher than G10), as appropriate. (This becomes strange in that, given an Octave of 8, "G_u4" is forced down to G10, but "A_u4" is forced down to an A9. But that boundary has to pop up someplace -- it's just unfortunate that it's in the middle of octave 10.) ATTRIBUTE METHODSThe object attributes discussed above are readable and writeable with object methods. For each attribute there is a read/write method, and a read-only method that returns a reference to the attribute's value:Attribute || R/W-Method || RO-R-Method ----------++-------------++-------------------------------------- Score || Score || Score_r (returns a listref) Notes || Notes || Notes_r (returns a listref) Time || Time || Time_r (returns a scalar ref) Duration || Duration || Duration_r (returns a scalar ref) Channel || Channel || Channel_r (returns a scalar ref) Octave || Octave || Octave_r (returns a scalar ref) Volume || Volume || Volume_r (returns a scalar ref) Tempo || Tempo || Tempo_r (returns a scalar ref) Cookies || Cookies || Cookies_r (returns a hashref) To read any of the above via a R/W-method, call with no parameters, e.g.: $notes = $obj->Notes; # same as $obj->Notes() The above is the read-attribute ("get") form. To set the value, call with parameters: $obj->Notes(13,17,22); The above is the write-attribute ("put") form. Incidentally, when used in write-attribute form, the return value is the same as the parameters, except for Score or Cookies. (In those two cases, I've suppressed it for efficiency's sake.) Alternately (and much more efficiently), you can use the read-only reference methods to read or alter the above values; $notes_r = $obj->Notes_r; # to read: @old_notes = @$notes_r; # to write: @$notes_r = (13,17,22); And this is the only way to set Cookies, Notes, or Score to a (), like so: $notes_r = $obj->Notes_r; @$notes_r = (); Since this: $obj->Notes; is just the read-format call, remember? Like all methods in this class, all the above-named attribute methods double as procedures that act on the default object -- in other words, you can say: Volume 10; # same as: $Volume = 10; @score_copy = Score; # same as: @score_copy = @Score Score @new_score; # same as: @Score = @new_score; $score_ref = Score_r; # same as: $score_ref = \@Score Volume(Volume + 10) # same as: $Volume += 10 But, stylistically, I suggest not using these procedures -- just directly access the variables instead. MIDI EVENT ROUTINESThese routines, below, add a MIDI event to the Score, with a start-time of Time. Example:text_event "And now the bongos!"; # procedure use $obj->text_event "And now the bongos!"; # method use These are named after the MIDI events they add to the score, so see MIDI::Event for an explanation of what the data types (like "velocity" or "pitch_wheel") mean. I've reordered this list so that what I guess are the most important ones are toward the top:
And here's the ones I'll be surprised if anyone ever uses:
About TempoThe chart above shows that tempo is set with a method/procedure that takes the form set_tempo(tempo), and MIDI::Event says that tempo is "microseconds, a value 0 to 16,777,215 (0x00FFFFFF)". But at the same time, you see that there's an attribute of the MIDI::Simple object called "Tempo", which I've warned you to leave at the default value of 96. So you may wonder what the deal is.The "Tempo" attribute (AKA "Divisions") is an integer that specifies the number of "ticks" per MIDI quarter note. Ticks is just the notional timing unit all MIDI events are expressed in terms of. Calling it "Tempo" is misleading, really; what you want to change to make your music go faster or slower isn't that parameter, but instead the mapping of ticks to actual time -- and that is what "set_tempo" does. Its one parameter is the number of microseconds each quarter note should get. Suppose you wanted a tempo of 120 quarter notes per minute. In terms of microseconds per quarter note: set_tempo 500_000; # you can use _ like a thousands-separator comma In other words, this says to make each quarter note take up 500,000 microseconds, namely .5 seconds. And there's 120 of those half-seconds to the minute; so, 120 quarter notes to the minute. If you see a "[quarter note symbol] = 160" in a piece of sheet music, and you want to figure out what number you need for the "set_tempo", do: 60_000_000 / 160 ... and you get: 375_000 Therefore, you should call: set_tempo 375_000; So in other words, this general formula: set_tempo int(60_000_000 / $quarter_notes_per_minute); should do you fine. As to the Tempo/Duration parameter, leave it alone and just assume that 96 ticks-per-quarter-note is a universal constant, and you'll be happy. (You may wonder: Why 96? As far as I've worked out, all purmutations of the normal note lengths (whole, half, quarter, eighth, sixteenth, and even thirty-second notes) and tripletting, dotting, or double-dotting, times 96, all produce integers. For example, if a quarter note is 96 ticks, then a double-dotted thirty-second note is 21 ticks (i.e., 1.75 * 1/8 * 96). But that'd be a messy 10.5 if there were only 48 ticks to a quarter note. Now, if you wanted a quintuplet anywhere, you'd be out of luck, since 96 isn't a factor of five. It's actually 3 * (2 ** 5), i.e., three times two to the fifth. If you really need quintuplets, then you have my very special permission to mess with the Tempo attribute -- I suggest multiples of 96, e.g., 5 * 96.) (You may also have read in MIDI::Filespec that "time_signature" allows you to define an arbitrary mapping of your concept of quarter note, to MIDI's concept of quarter note. For your sanity and mine, leave them the same, at a 1:1 mapping -- i.e., with an '8' for "time_signature"'s last parameter, for "eight notated 32nd-notes per MIDI quarter note". And this is relevant only if you're calling "time_signature" anyway, which is not necessarily a given.) MORE ROUTINES
FUNCTIONSThese are subroutines that aren't methods and don't affect anything (i.e., don't have "side effects") -- they just take input and/or give output.
COPYRIGHTCopyright (c) 1998-2005 Sean M. Burke. All rights reserved.This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. AUTHORSean M. Burke "sburke@cpan.org"
Visit the GSP FreeBSD Man Page Interface. |