PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

Unknown | 323 lines | 200 code | 123 blank | 0 comment | 0 complexity | 904df4b2372b35a3239b40bdfc1fccf8 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. =head1 NAME
  2. SDLx::Music - A powerful, convenient interface to C<SDL::Mixer::Music>
  3. =head1 CATEGORY
  4. Extension
  5. =head1 SYNOPSIS
  6. use SDL;
  7. use SDLx::Music;
  8. my $music = SDLx::Music->new;
  9. #define music data with just a name and file path
  10. $music->data(
  11. fast => 'music/fast.ogg',
  12. slow => 'music/slow.ogg',
  13. magical => 'music/magic/cake.ogg',
  14. );
  15. #define more the long way with a parameter hash
  16. $music->data(
  17. squelch => {
  18. file => 'music/squelch.ogg',
  19. loops => 3,
  20. fade_in => 0.5,
  21. volume => 72,
  22. }
  23. splurge => {
  24. file => 'music/splurge.ogg',
  25. finished => sub { print 'Splurged!' },
  26. },
  27. );
  28. #instead, do it the short way with the help of defaults
  29. #clobber everything
  30. $music->clear;
  31. #specify the class-wide default for file extension
  32. SDLx::Music->default->ext('.ogg');
  33. #specify the object-wide default for file directory
  34. $music->default->dir('music/');
  35. #redefine squelch and splurge
  36. $music->data(
  37. squelch => {
  38. #file defaults to the data name, so the path becomes
  39. #'music/squelch.ogg', which is what we wanted
  40. loops => 3,
  41. fade_in => 0.5,
  42. volume => 72,
  43. }
  44. splurge => {
  45. finished => sub { print 'Splurged!' },
  46. },
  47. );
  48. #and we can redefine the others like this
  49. $music->data_for(
  50. 'fast',
  51. 'slow',
  52. )->data(
  53. magical => 'magic/cake',
  54. );
  55. #get to that named data
  56. my $splurge = $music->data('splurge');
  57. #and add to/modify it without clobbering existing data
  58. $splurge
  59. ->volume(55)
  60. ->file('data/' . $splurge->file)
  61. ;
  62. #play it once, fading in for 2 seconds
  63. $music->play($splurge, loops => 1, fade_in => 2);
  64. #(it will be loaded automatically and played with its specified params)
  65. sleep 5;
  66. #pause it
  67. $music->pause if $music->playing;
  68. #load everything else
  69. $music->load;
  70. #resume playing it at a lower volume
  71. $music->volume(44);
  72. $music->play;
  73. #get the names for all music
  74. my @names = keys %{ $music->data };
  75. for my $name (@names) {
  76. #play it in an infinite loop
  77. $music->play($name, loops => 0);
  78. warn "Cake!" if $music->playing eq "magical";
  79. sleep 10;
  80. }
  81. #fade out the last song
  82. $music->fade_out(5);
  83. sleep 4;
  84. die "CAKE!" if $music->fading->name eq "magical";
  85. sleep 1;
  86. =head1 DESCRIPTION
  87. This class provides a powerful and convenient interface to L<SDL::Mixer::Music>. The main goal was to make music code neater and clearer. Among the things that help this, this class provides class-wide and object-wide defaults and it automatically shares duplicate use of the same files.
  88. The following document is intended for reference. For a more beginner-friendly description of this class, see chapter X of the SDL Perl Manual (when it is written).
  89. B<Please note:> do not mix use of this class with L<SDL::Mixer::Music> if you want everything to work right.
  90. =head1 METHODS
  91. =head2 new
  92. SDLx::Music->new;
  93. #Option arguments showing the default parameters
  94. SDLx::Music->new( freq => 44100, format => SDL::Audio::AUDIO_S16SYS, channels => 2, chunksize => 4096);
  95. Creates the new music object. Inits audio with a call to L<SDLx::Mixer::init|SDLx::Mixer/init>, if it isn't already (if you want more precise control over what is initialized, make sure you call L<SDLx::Mixer::init|SDLx::Mixer/init> before you call this method). Creates an empty default data object for object-wide defaults. If arguments are supplied, calls L</data> with them to set up any initial data objects. Returns the new music object.
  96. =head2 data
  97. $music->data;
  98. $music->data( $name );
  99. $music->data( $data );
  100. $music->data( %args );
  101. With no arguments: returns a reference to the data hash. This hash has data names as keys and the associated data objects as values.
  102. With a name: creates the data name if it doesn't already exist. Does this with a call to L<SDLx::Music::Data->new|SDLx::Music::Data/new> with C<name => $name> and puts that new object in the data hash under the key of C<$name>. Returns the data object for that name.
  103. With a hash of arguments: for each pair, and returns a L<SDLx::Music::Data>. Returns C<$music>.
  104. =head2 data_for
  105. $music->data_for( @names_or_data_objects );
  106. Calls L</data> repeatedly, passing it one element of the list at a time, to initialise multiple empty names and/or add data objects. Returns C<$music>.
  107. =head2 has_data
  108. $music->has_data;
  109. $music->has_data( $name );
  110. $music->has_data( $data );
  111. Without arguments: returns how many data objects the class has.
  112. With a name: returns the data object for C<$name>. If there is no data object for C<$name> it is not created and undef is returned instead.
  113. With a data object: does a (slowish) reverse of the data hash to see if the data object belongs to C<$music>. Returns it or undef.
  114. =head2 default
  115. $music->default;
  116. SDLx::Music->default;
  117. Returns the default data object belonging to C<$music> (created in L</new>), or to the class. See L<SDLx::Music::Data> for information on how defaults work.
  118. =head2 load
  119. $music->load;
  120. $music->load( @names_or_data_objects );
  121. SDLx::Music->load( @data_objects );
  122. Without arguments: for every data object belonging to C<$music>, if the data object doesn't already have a L<loaded|SDLx::Music::Data/loaded> file, loads the file named by L<dir|SDLx::Music::Data/dir>, L<file|SDLx::Music::Data/file> and L<ext|SDLx::Music::Data/ext> if it hasn't been loaded already. Sets the data object's L<loaded|SDLx::Music::Data/loaded> parameter to this. Two or more objects that use the same file will use the same loaded file. Reference counting is respected, so if two data objects use the same loaded file it will be removed from memory only after both are destroyed. Returns <$music>.
  123. With arguments: does the same, but only for the names or data objects in the list. If there isn't a data object for any name, it will be created.
  124. =head2 unload
  125. $music->unload;
  126. $music->unload( @names_or_data_objects );
  127. SDLx::Music->unload( @data_objects );
  128. Without arguments: clears the L<loaded|SDLx::Music::Data/loaded> parameter for all of the data objects in C<$music>. The loaded file is removed from memory if it loses its last reference to it. Returns <$music>.
  129. With arguments: does the same, but only for the names or data objects in the list. Doesn't create a data object for a name that doesn't exist.
  130. =head2 clear
  131. $music->clear;
  132. $music->clear( @names );
  133. Without arguments: empties C<$music>'s data hash of all of its objects. The objects will be destroyed only if the last reference to them is removed, and no parameters will be cleared if this is not the case. Returns C<$music>.
  134. With arguments: does the same, but only deletes the values of the data hash for the names in the list.
  135. =head2 real_clear
  136. $music->real_clear;
  137. $music->real_clear( @names_or_data_objects );
  138. SDLx::Music->real_clear( @data_objects );
  139. The full, brute force version of L</clear>.
  140. Without arguments: empties out the parameters of every data object in C<$music> (including L</unload>ing them) and then removes them from the data hash. This may not remove the objects from memory if there are still remaining references to them, but it is the closest thing to it. Returns C<$music>.
  141. With arguments: does the same, but only clears out the names or data objects in the list.
  142. =head2 play
  143. $music_or_class->play;
  144. $music->play( $name, $params );
  145. $music_or_class->play( $data, %params );
  146. Without arguments: resumes any paused music. Returns the object or class.
  147. With arguments: plays the sound found in C<$music>'s C<$name>, or in C<$data> (depending on which is specified). L</load>s it if it needs to be loaded (which in turn creates the name if it needs to be created). The C<%params> are all optional and, if defined, are used instead of the values returned by the data object's parameter methods. The accepted parameters here are:
  148. =over
  149. =item L<loops|SDLx::Music::Data/loops>
  150. Plays the music file C<loops> times. If C<loops> is 0, loops it infinitely.
  151. =item L<fade_in|SDLx::Music::Data/fade_in>
  152. Fades the music in for its first C<fade_in> milliseconds, if C<fade_in> is defined.
  153. =item L<vol|SDLx::Music::Data/vol>
  154. Sets the music volume to C<vol>.
  155. =item L<vol_portion|SDLx::Music::Data/vol_portion>
  156. Multiplies the C<vol> by C<vol_portion> (values from 0 to 1 are encouraged) before setting the volume.
  157. =item L<pos|SDLx::Music::Data/pos>
  158. Sets the music position to C<pos> if C<pos> is defined.
  159. =back
  160. Returns the object or class.
  161. =head2 pause
  162. $music_or_class->pause;
  163. Pauses any playing music. Returns the object or class.
  164. =head2 stop
  165. $music_or_class->stop;
  166. Stops any playing music. Returns the object or class.
  167. =head2 last_played
  168. my $last_played = $music_or_class->last_played;
  169. Returns the data object that was L</play>ed last.
  170. =head2 playing
  171. my $playing = $music->playing;
  172. If there is something playing, returns the data object that was L</play>ed last. Otherwise, returns undef.
  173. =head2 paused
  174. If there is something paused, returns the data object that was L</play>ed last. Otherwise, returns undef.
  175. =head2 fading
  176. If there is something fading, returns the data object that was L</play>ed last. Otherwise, returns undef.
  177. =head2 volume
  178. my $volume = $music_or_class->volume;
  179. $music_or_class->volume( $volume );
  180. Without arguments: returns the current music volume
  181. With arguments: Sets the music volume to C<$volume>. Returns the object or class.
  182. =head2 fade_out
  183. $music_or_class->fade_out( $fade_out );
  184. Fades the music out for its next C<fade_in> milliseconds. Returns the object or class.
  185. =head2 rewind
  186. $music_or_class->rewind;
  187. Rewinds the music to its start. Returns the object or class.
  188. =head2 pos
  189. $music_or_class->pos( $pos );
  190. Sets the music position to C<$pos> milliseconds. It does different things for different file types, so see L<SDL::Mixer::Music::set_music_position|SDL::Mixer::Music/set_music_position> for details. Note that this method divides C<$pos> by 1000 to pass it to that function, which uses seconds. Returns the object or class.
  191. =head1 SEE ALSO
  192. L<SDLx::Music::Data>
  193. L<SDLx::Mixer>
  194. L<SDL::Mixer::Music>
  195. L<SDL::Mixer>
  196. =head1 AUTHORS
  197. See L<SDL/AUTHORS>.
  198. =head1 COPYRIGHT & LICENSE
  199. This program is free software; you can redistribute it and/or modify it
  200. under the same terms as Perl itself.