home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Track.pm < prev    next >
Encoding:
Perl POD Document  |  2002-11-16  |  13.3 KB  |  430 lines

  1.  
  2. # Time-stamp: "2002-11-16 01:57:00 MST"
  3. require 5;
  4. package MIDI::Track;
  5. use strict;
  6. use vars qw($Debug $VERSION);
  7. use Carp;
  8.  
  9. $Debug = 0;
  10. $VERSION = 0.76;
  11.  
  12. =head1 NAME
  13.  
  14. MIDI::Track -- functions and methods for MIDI tracks
  15.  
  16. =head1 SYNOPSIS
  17.  
  18.  use MIDI; # ...which "use"s MIDI::Track et al
  19.  $taco_track = MIDI::Track->new;
  20.  $taco_track->events(
  21.   ['text_event', 0, "I like tacos!"],
  22.   ['note_on',    0, 4, 50, 96 ],
  23.   ['note_off', 300, 4, 50, 96 ],
  24.  );
  25.  $opus = MIDI::Opus->new(
  26.   {  'format' => 0,  'ticks' => 240,  'tracks' => [ $taco_track ] }
  27.  );
  28.    ...etc...
  29.  
  30. =head1 DESCRIPTION
  31.  
  32. MIDI::Track provides a constructor and methods for objects
  33. representing a MIDI track.  It is part of the MIDI suite.
  34.  
  35. MIDI tracks have, currently, three attributes: a type, events, and
  36. data.  Almost all tracks you'll ever deal with are of type "MTrk", and
  37. so this is the type by default.  Events are what make up an MTrk
  38. track.  If a track is not of type MTrk, or is an unparsed MTrk, then
  39. it has (or better have!) data.
  40.  
  41. When an MTrk track is encoded, if there is data defined for it, that's
  42. what's encoded (and "encoding data" means just passing it thru
  43. untouched).  Note that this happens even if the data defined is ""
  44. (but it won't happen if the data is undef).  However, if there's no
  45. data defined for the MTrk track (as is the general case), then the
  46. track's events are encoded, via a call to C<MIDI::Event::encode>.
  47.  
  48. (If neither events not data are defined, it acts as a zero-length
  49. track.)
  50.  
  51. If a non-MTrk track is encoded, its data is encoded.  If there's no
  52. data for it, it acts as a zero-length track.
  53.  
  54. In other words, 1) events are meaningful only in an MTrk track, 2) you
  55. probably don't want both data and events defined, and 3) 99.999% of
  56. the time, just worry about events in MTrk tracks, because that's all
  57. you ever want to deal with anyway.
  58.  
  59. =head1 CONSTRUCTOR AND METHODS
  60.  
  61. MIDI::Track provides...
  62.  
  63. =over
  64.  
  65. =cut
  66.  
  67. ###########################################################################
  68.  
  69. =item the constructor MIDI::Track->new({ ...options... })
  70.  
  71. This returns a new track object.  By default, the track is of type
  72. MTrk, which is probably what you want.  The options, which are
  73. optional, is an anonymous hash.  There are four recognized options:
  74. C<data>, which sets the data of the new track to the string provided;
  75. C<type>, which sets the type of the new track to the string provided;
  76. C<events>, which sets the events of the new track to the contents of
  77. the list-reference provided (i.e., a reference to a LoL -- see
  78. L<perllol> for the skinny on LoLs); and C<events_r>, which is an exact
  79. synonym of C<events>.
  80.  
  81. =cut
  82.  
  83. sub new {
  84.   # make a new track.
  85.   my $class = shift;
  86.   my $this = bless( {}, $class );
  87.   print "New object in class $class\n" if $Debug;
  88.   $this->_init( @_ );
  89.   return $this;
  90. }
  91.  
  92. sub _init {
  93.   # You can specify options:
  94.   #  'event' => [a list of events],  AKA 'event_r'
  95.   #  'type'  => 'Whut', # default is 'MTrk'
  96.   #  'data'  => 'scads of binary data as you like it'
  97.   my $this = shift;
  98.   my $options_r = ref($_[0]) eq 'HASH' ? $_[0] : {};
  99.   print "_init called against $this\n" if $Debug;
  100.   if($Debug) {
  101.     if(%$options_r) {
  102.       print "Parameters: ", map("<$_>", %$options_r), "\n";
  103.     } else {
  104.       print "Null parameters for opus init\n";
  105.     }
  106.   }
  107.  
  108.   $this->{'type'} =
  109.     defined($options_r->{'type'}) ? $options_r->{'type'} : 'MTrk';
  110.   $this->{'data'} = $options_r->{'data'}
  111.     if defined($options_r->{'data'});
  112.  
  113.   $options_r->{'events'} = $options_r->{'events_r'}
  114.     if( exists( $options_r->{'events_r'} ) and not
  115.     exists( $options_r->{'events'} )
  116.       );
  117.   # so events_r => [ @events ] is a synonym for 
  118.   #    events   => [ @events ]
  119.   # as on option for new()
  120.  
  121.   $this->{'events'} =
  122.     ( defined($options_r->{'events'})
  123.       and ref($options_r->{'events'}) eq 'ARRAY' )
  124.     ? $options_r->{'events'} : []
  125.   ;
  126.   return;
  127. }
  128.  
  129. =item the method $new_track = $track->copy
  130.  
  131. This duplicates the contents of the given track, and returns
  132. the duplicate.  If you are unclear on why you may need this function,
  133. consider:
  134.  
  135.           $funk  = MIDI::Opus->new({'from_file' => 'funk1.mid'});
  136.           $samba = MIDI::Opus->new({'from_file' => 'samba1.mid'});
  137.           
  138.           $bass_track = ( $funk->tracks )[-1]; # last track
  139.           push(@{ $samba->tracks_r }, $bass_track );
  140.                # make it the last track
  141.           
  142.           &funk_it_up(  ( $funk->tracks )[-1]  );
  143.                # modifies the last track of $funk
  144.           &turn_it_out(  ( $samba->tracks )[-1]  );
  145.                # modifies the last track of $samba
  146.           
  147.           $funk->write_to_file('funk2.mid');
  148.           $samba->write_to_file('samba2.mid');
  149.           exit;
  150.  
  151. So you have your routines funk_it_up and turn_it_out, and they each
  152. modify the track they're applied to in some way.  But the problem is that
  153. the above code probably does not do what you want -- because the last
  154. track-object of $funk and the last track-object of $samba are the
  155. I<same object>.  An object, you may be surprised to learn, can be in
  156. different opuses at the same time -- which is fine, except in cases like
  157. the above code.  That's where you need to do copy the object.  Change
  158. the above code to read:
  159.  
  160.           push(@{ $samba->tracks_r }, $bass_track->copy );
  161.  
  162. and what you want to happen, will.
  163.  
  164. Incidentally, this potential need to copy also occurs with opuses (and
  165. in fact any reference-based data structure, altho opuses and tracks
  166. should cover almost all cases with MIDI stuff), which is why there's
  167. $opus->copy, for copying entire opuses.
  168.  
  169. (If you happen to need to copy a single event, it's just $new = [@$old] ;
  170. and if you happen to need to copy an event structure (LoL) outside of a
  171. track for some reason, use MIDI::Event::copy_structure.)
  172.  
  173. =cut
  174.  
  175. sub copy {
  176.   # Duplicate a given track.  Even dupes the events.
  177.   # Call as $new_one = $track->copy
  178.   my $track = shift;
  179.  
  180.   my $new = bless( { %{$track} }, ref $track );
  181.   # a first crude dupe
  182.   $new->{'events'} = &MIDI::Event::copy_structure( $new->{'events'} )
  183.     if $new->{'events'};
  184.   return $new;
  185. }
  186.  
  187. ###########################################################################
  188. # These three modify all the possible attributes of a track
  189.  
  190. =item the method $track->events( @events )
  191.  
  192. Returns the list of events in the track, possibly after having set it
  193. to @events, if specified and not empty.  (If you happen to want to set
  194. the list of events to an empty list, for whatever reason, you have to use
  195. "$track->events_r([])".)
  196.  
  197. In other words: $track->events(@events) is how to set the list of events
  198. (assuming @events is not empty), and @events = $track->events is how to
  199. read the list of events.
  200.  
  201. =cut
  202.  
  203. sub events { # list or set events in this object
  204.   my $this = shift;
  205.   $this->{'events'} = [ @_ ] if @_;
  206.   return @{ $this->{'events'} };
  207. }
  208.  
  209. =item the method $track->events_r( $event_r )
  210.  
  211. Returns a reference to the list of events in the track, possibly after
  212. having set it to $events_r, if specified.  Actually, "$events_r" can be
  213. any listref to a LoL, whether it comes from a scalar as in
  214. C<$some_events_r>, or from something like C<[@events]>, or just plain
  215. old C<\@events>
  216.  
  217. Originally $track->events was the only way to deal with events, but I
  218. added $track->events_r to make possible 1) setting the list of events
  219. to (), for whatever that's worth, and 2) so you can directly
  220. manipulate the track's events, without having to I<copy> the list of
  221. events (which might be tens of thousands of elements long) back
  222. and forth.  This way, you can say:
  223.  
  224.           $events_r = $track->events_r();
  225.           @some_stuff = splice(@$events_r, 4, 6);
  226.  
  227. But if you don't know how to deal with listrefs outside of LoLs,
  228. that's OK, just use $track->events.
  229.  
  230. =cut
  231.  
  232. sub events_r {
  233.   # return (maybe set) a list-reference to the event-structure for this track
  234.   my $this = shift;
  235.   if(@_) {
  236.     croak "parameter for MIDI::Track::events_r must be an array-ref"
  237.       unless ref($_[0]);
  238.     $this->{'events'} = $_[0];
  239.   }
  240.   return $this->{'events'};
  241. }
  242.  
  243. =item the method $track->type( 'MFoo' )
  244.  
  245. Returns the type of $track, after having set it to 'MFoo', if provided.
  246. You probably won't ever need to use this method, other than in
  247. a context like:
  248.  
  249.           if( $track->type eq 'MTrk' ) { # The usual case
  250.             give_up_the_funk($track);
  251.           } # Else just keep on walkin'!
  252.  
  253. Track types must be 4 bytes long; see L<MIDI::Filespec> for details.
  254.  
  255. =cut
  256.  
  257. sub type {
  258.   my $this = shift;
  259.   $this->{'type'} = $_[0] if @_; # if you're setting it
  260.   return $this->{'type'};
  261. }
  262.  
  263. =item the method $track->data( $kooky_binary_data )
  264.  
  265. Returns the data from $track, after having set it to
  266. $kooky_binary_data, if provided -- even if it's zero-length!  You
  267. probably won't ever need to use this method.  For your information,
  268. $track->data(undef) is how to undefine the data for a track.
  269.  
  270. =cut
  271.  
  272. sub data {
  273.   # meant for reading/setting generally non-MTrk track data
  274.   my $this = shift;
  275.   $this->{'data'} = $_[0] if @_;
  276.   return $this->{'data'};
  277. }
  278.  
  279. ###########################################################################
  280.  
  281. =item the method $track->new_event('event', ...parameters... )
  282.  
  283. This adds the event ('event', ...parameters...) to the end of the
  284. event list for $track.  It's just sugar for:
  285.  
  286.           push( @{$this_track->events_r}, [ 'event', ...params... ] )
  287.  
  288. If you want anything other than the equivalent of that, like some
  289. kinda splice(), then do it yourself with $track->events_r or
  290. $track->events.
  291.  
  292. =cut
  293.  
  294. sub new_event {
  295.   # Usage:
  296.   #  $this_track->new_event('text_event', 0, 'Lesbia cum Prono');
  297.  
  298.   my $track = shift;
  299.   push( @{$track->{'events'}}, [ @_ ] );
  300.   # this returns the new number of events in that event list, if that
  301.   # interests you.
  302. }
  303.  
  304. ###########################################################################
  305.  
  306. =item the method $track->dump({ ...options... })
  307.  
  308. This dumps the track's contents for your inspection.  The dump format
  309. is code that looks like Perlcode you'd use to recreate that track.
  310. This routine outputs with just C<print>, so you can use C<select> to
  311. change where that'll go.  I intended this to be just an internal
  312. routine for use only by the method MIDI::Opus::dump, but I figure it
  313. might be useful to you, if you need te dump the code for just a given
  314. track.
  315. Read the source if you really need to know how this works.
  316.  
  317. =cut
  318.  
  319. sub dump { # dump a track's contents
  320.   my $this = $_[0];
  321.   my $options_r = ref($_[1]) eq 'HASH' ? $_[1] : {};
  322.   my $type = $this->type;
  323.  
  324.   my $indent = '    ';
  325.   my @events = $this->events;
  326.   print(
  327.     $indent, "MIDI::Track->new({\n",
  328.     $indent, "  'type' => ", &MIDI::_dump_quote($type), ",\n",
  329.     defined($this->{'data'}) ?
  330.       ( $indent, "  'data' => ",
  331.         &MIDI::_dump_quote($this->{'data'}), ",\n" )
  332.       : (),
  333.     $indent, "  'events' => [  # ", scalar(@events), " events.\n",
  334.        );
  335.   foreach my $event (@events) {
  336.     &MIDI::Event::dump(@$event);
  337.     # was: print( $indent, "    [", &MIDI::_dump_quote(@$event), "],\n" );
  338.   }
  339.   print( "$indent  ]\n$indent}),\n$indent\n" );
  340.   return;
  341. }
  342.  
  343. ###########################################################################
  344.  
  345. # CURRENTLY UNDOCUMENTED -- no end-user ever needs to call this as such
  346. #
  347. sub encode { # encode a track object into track data (not a chunk)
  348.   # Calling format:
  349.   #  $data_r = $track->encode( { .. options .. } )
  350.   # The (optional) argument is an anonymous hash of options.
  351.   # Returns a REFERENCE to track data.
  352.   #
  353.   my $track  = $_[0];
  354.   croak "$track is not a track object!" unless ref($track);
  355.   my $options_r = ref($_[1]) eq 'HASH' ? $_[1] : {};
  356.  
  357.   my $data = '';
  358.  
  359.   if( exists( $track->{'data'} )  and  defined( $track->{'data'} ) ) {
  360.     # It might be 0-length, by the way.  Might this be problematic?
  361.     $data = $track->{'data'};
  362.     # warn "Encoding 0-length track data!" unless length $data;
  363.   } else { # Data is not defined for this track.  Parse the events
  364.     if( ($track->{'type'} eq 'MTrk'  or  length($track->{'type'}) == 0)
  365.         and defined($track->{'events'})
  366.              # not just exists -- but DEFINED!
  367.         and ref($track->{'events'})
  368.     ) {
  369.       print "Encoding ", $track->{'events'}, "\n" if $Debug;
  370.       return
  371.         &MIDI::Event::encode($track->{'events'}, $options_r );
  372.     } else {
  373.       $data = ''; # what else to do?
  374.       warn "Spork 8851\n" if $Debug;
  375.     }
  376.   }
  377.   return \$data;
  378. }
  379. ###########################################################################
  380.  
  381. # CURRENTLY UNDOCUMENTED -- no end-user ever needs to call this as such
  382. #
  383. sub decode { # returns a new object, but doesn't accept constructor syntax
  384.   # decode track data (not a chunk) into a new track object
  385.   # Calling format:
  386.   #  $new_track = 
  387.   #   MIDI::Track::decode($type, \$track_data, { .. options .. })
  388.   # Returns a new track_object.
  389.   # The anonymous hash of options is, well, optional
  390.  
  391.   my $track = MIDI::Track->new();
  392.  
  393.   my ($type, $data_r, $options_r)  = @_[0,1,2];
  394.   $options_r = {} unless ref($options_r) eq 'HASH';
  395.  
  396.   die "\$_[0] \"$_[0]\" is not a data reference!"
  397.     unless ref($_[1]) eq 'SCALAR';
  398.  
  399.   $track->{'type'} = $type;
  400.   if($type eq 'MTrk' and not $options_r->{'no_parse'}) {
  401.     $track->{'events'} =
  402.       &MIDI::Event::decode($data_r, $options_r);
  403.         # And that's where all the work happens
  404.   } else {
  405.     $track->{'data'} = $$data_r;
  406.   }
  407.   return $track;
  408. }
  409.  
  410. ###########################################################################
  411.  
  412. =back
  413.  
  414. =head1 COPYRIGHT 
  415.  
  416. Copyright (c) 1998-2002 Sean M. Burke. All rights reserved.
  417.  
  418. This library is free software; you can redistribute it and/or
  419. modify it under the same terms as Perl itself.
  420.  
  421. =head1 AUTHOR
  422.  
  423. Sean M. Burke C<sburke@cpan.org>
  424.  
  425. =cut
  426.  
  427. 1;
  428.  
  429. __END__
  430.