PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/pods/SDL/Mixer/Effects.pod

http://github.com/PerlGameDev/SDL
Unknown | 194 lines | 122 code | 72 blank | 0 comment | 0 complexity | 15b980ad4d9188d726696f55ba468025 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. =pod
  2. =head1 NAME
  3. SDL::Mixer::Effects - sound effect functions
  4. =head1 CATEGORY
  5. Mixer
  6. =head1 METHODS
  7. =head2 register
  8. SDL::Mixer::Effects::register( $channel, $effect_callback, $done_callback, $arg );
  9. Hook a processor function into a channel for post processing effects. You may just be reading the data and displaying it, or you may be altering
  10. the stream to add an echo. Most processors also have state data that they allocate as they are in use, this would be stored in the C<$arg> data
  11. space. When a processor is finished being used, any function passed into C<$done_callback> will be called.
  12. The effects are put into a linked list, and always appended to the end, meaning they always work on previously registered effects output.
  13. Returns: Zero on errors, such as a nonexisting channel.
  14. B<Note>: Passing MIX_CHANNEL_POST will register the C<$effect_callback> as an postmix effect.
  15. B<Note>: Do not use this on a threaded perl. This will crash.
  16. Example:
  17. use SDL;
  18. use SDL::Mixer;
  19. use SDL::Mixer::Channels;
  20. use SDL::Mixer::Effects;
  21. use SDL::Mixer::Samples;
  22. my @last_stream = ();
  23. my $echo_effect_func = sub
  24. {
  25. my $channel = shift;
  26. my $samples = shift;
  27. my $position = shift;
  28. my @stream = @_;
  29. my @stream2 = @stream;
  30. my $offset = $samples / 2;
  31. for(my $i = 0; $i < $samples; $i+=2)
  32. {
  33. if($i < $offset)
  34. {
  35. if(scalar(@last_stream) == $samples)
  36. {
  37. $stream2[$i] = $stream[$i] * 0.6 + $last_stream[$samples + $i - $offset] * 0.4; # left
  38. $stream2[$i + 1] = $stream[$i + 1] * 0.6 + $last_stream[$samples + $i - $offset + 1] * 0.4; # right
  39. }
  40. }
  41. else
  42. {
  43. $stream2[$i] = $stream[$i] * 0.6 + $stream[$i - $offset] * 0.4; # left
  44. $stream2[$i + 1] = $stream[$i + 1] * 0.6 + $stream[$i - $offset + 1] * 0.4; # right
  45. }
  46. }
  47. @last_stream = @stream;
  48. return @stream2;
  49. };
  50. my $effect_done = sub
  51. {
  52. # you may do something here
  53. };
  54. SDL::Mixer::open_audio( 44100, SDL::Constants::AUDIO_S16, 2, 1024 );
  55. my $playing_channel = SDL::Mixer::Channels::play_channel( -1, SDL::Mixer::Samples::load_WAV('test/data/sample.wav'), -1 );
  56. SDL::Mixer::Effects::register($playing_channel, $echo_effect_func, $effect_done, 0);
  57. SDL::delay(2000);
  58. SDL::Mixer::Effects::unregister($playing_channel, $echo_effect_func);
  59. SDL::Mixer::close_audio();
  60. SDL::quit();
  61. =head2 unregister
  62. SDL::Mixer::Effects::unregister( $channel, $effect_callback );
  63. Remove the registered effect function from the effect list for channel.
  64. If the channel is active the registered effect will have its C<$done_callback> function called, if it was specified in
  65. L<SDL::Mixer::Effects::register|SDL::Mixer::Effects/"register">.
  66. Returns: Zero on errors, such as invalid channel, or effect function not registered on channel.
  67. B<Note>: Do not use this on a threaded perl. This will crash.
  68. =head2 unregister_all
  69. SDL::Mixer::Effects::unregister_all( $channel );
  70. This removes all effects registered to C<$channel>. If the channel is active all the registered effects will have their C<$done_callback>
  71. functions called, if they were specified in L<SDL::Mixer::Effects::register|SDL::Mixer::Effects/"register">.
  72. Returns: Zero on errors, such as channel not existing.
  73. B<Note>: Do not use this on a threaded perl. This will crash.
  74. =head2 set_post_mix
  75. SDL::Mixer::Effects::set_post_mix( $effect_callback, $arg );
  76. Hook a processor function to the postmix stream for post processing effects. You may just be reading the data and displaying it, or you may be
  77. altering the stream to add an echo. This processor is never really finished, until you call it without arguments.
  78. There can only be one postmix function used at a time through this method. Use L<SDL::Mixer::Effects::register|SDL::Mixer::Effects/"register">
  79. with MIX_CHANNEL_POST to use multiple postmix processors.
  80. This postmix processor is run AFTER all the registered postmixers set up by L<SDL::Mixer::Effects::register|SDL::Mixer::Effects/"register">.
  81. B<Note>: Do not use this on a threaded perl. This will crash.
  82. =head2 set_distance
  83. SDL::Mixer::Effects::set_distance( $channel, $distance );
  84. This effect simulates a simple attenuation of volume due to distance. The volume never quite reaches silence, even at max distance (C<255>).
  85. NOTE: Using a distance of C<0> will cause the effect to unregister itself from channel. You cannot unregister it any other way, unless you use
  86. L<SDL::Mixer::Effects::unregister_all|SDL::Mixer::Effects/"unregister_all"> on the channel.
  87. Returns: Zero on errors, such as an invalid channel, or if Mix_RegisterEffect failed.
  88. =head2 set_panning
  89. SDL::Mixer::Effects::set_panning( $channel, $left, $right );
  90. This effect will only work on stereo audio. Meaning you called L<SDL::Mixer::open_audio|SDL::Mixer/"open_audio"> with 2 channels.
  91. B<Note>: Setting both left and right to 255 will unregister the effect from channel. You cannot unregister it any other way, unless you use
  92. L<SDL::Mixer::Effects::unregister_all|SDL::Mixer::Effects/"unregister_all"> on the channel.
  93. B<Note>: Using this function on a mono audio device will not register the effect, nor will it return an error status.
  94. Returns: Zero on errors, such as bad channel, or if L<SDL::Mixer::Effects::register|SDL::Mixer::Effects/"register"> failed.
  95. =head2 set_position
  96. SDL::Mixer::Effects::set_position( $channel, $angle, $distance );
  97. This effect emulates a simple 3D audio effect. It's not all that realistic, but it can help improve some level of realism. By giving it the
  98. angle and distance from the camera's point of view, the effect pans and attenuates volumes.
  99. C<$angle> is the direction in relation to forward from 0 to 360 degrees. Larger angles will be reduced to this range using angles % 360.
  100. =over 4
  101. =item *
  102. 0 = directly in front.
  103. =item *
  104. 90 = directly to the right.
  105. =item *
  106. 180 = directly behind.
  107. =item *
  108. 270 = directly to the left.
  109. =back
  110. So you can see it goes clockwise starting at directly in front.
  111. C<$distance> is C<0>(close/loud) to C<255>(far/quiet).
  112. B<Note>: Using angle and distance of C<0>, will cause the effect to unregister itself from channel. You cannot unregister it any other way,
  113. unless you use L<SDL::Mixer::Effects::unregister_all|SDL::Mixer::Effects/"unregister_all"> on the channel.
  114. Returns: Zero on errors, such as an invalid channel, or if L<SDL::Mixer::Effects::register|SDL::Mixer::Effects/"register"> failed.
  115. =head2 set_reverse_stereo
  116. SDL::Mixer::Effects::set_reverse_stereo( $channel, $flip );
  117. If you pass C<1> to C<$flip> it simple reverse stereo, swaps left and right channel sound.
  118. B<Note>: Using a flip of C<0>, will cause the effect to unregister itself from channel. You cannot unregister it any other way, unless you use
  119. L<SDL::Mixer::Effects::register|SDL::Mixer::Effects/"register"> on the channel.
  120. =head1 AUTHORS
  121. See L<SDL/AUTHORS>.
  122. =cut