PageRenderTime 12129ms CodeModel.GetById 39ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/SDLx/Sound.pm

http://github.com/PerlGameDev/SDL
Perl | 108 lines | 88 code | 13 blank | 7 comment | 9 complexity | 80d9fc855f0d4c4a974a0538fc6bfa61 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. package SDLx::Sound;
  2. use strict;
  3. use warnings;
  4. use Carp;
  5. use SDL;
  6. #use SDL::Audio;
  7. #use SDL::AudioSpec;
  8. use SDL::Mixer;
  9. use SDL::Mixer::Music;
  10. #use SDL::Mixer::Channels;
  11. #use SDL::Mixer::Samples;
  12. #use SDL::Mixer::MixChunk;
  13. our $VERSION = 2.548;
  14. # SDL::Mixer must be inited only one time
  15. my $audioInited = undef;
  16. sub new {
  17. my $class = shift;
  18. my $self = {@_};
  19. bless ($self, $class);
  20. _initAudio() unless $audioInited;
  21. $self->{supported} = _initMixer();
  22. return $self;
  23. }
  24. sub _initAudio {
  25. SDL::Mixer::open_audio( 44100, AUDIO_S16SYS, 2, 4096 );
  26. my ($status, $freq, $format, $channels) = @{ SDL::Mixer::query_spec() };
  27. $audioInited = 1 if $status == 1;
  28. return ($status, $freq, $format, $channels); #TODO: Save this information in $self;
  29. }
  30. sub _initMixer {
  31. my $init_flags = SDL::Mixer::init( MIX_INIT_MP3 | MIX_INIT_MOD | MIX_INIT_FLAC | MIX_INIT_OGG );
  32. my %init = ();
  33. # Short circuit if we have and older version of SDL_Mixer
  34. return \%init unless $init_flags;
  35. $init{ mp3 } = 1 if $init_flags & MIX_INIT_MP3;
  36. $init{ mod } = 1 if $init_flags & MIX_INIT_MOD;
  37. $init{ flac } = 1 if $init_flags & MIX_INIT_FLAC;
  38. $init{ ogg } = 1 if $init_flags & MIX_INIT_OGG;
  39. return \%init
  40. }
  41. sub load {
  42. my $self = shift;
  43. $self->{files} = {@_};
  44. }
  45. sub unload {
  46. my $self = shift;
  47. $self->{files} = {};
  48. }
  49. sub play {
  50. my $self = shift;
  51. $self->{files} = {@_} if $#_ > 0 && @_;
  52. my $play = 1;
  53. if (-e $_[0]) {
  54. my $music = SDL::Mixer::Music::load_MUS($_[0])
  55. or Carp::croak 'Sound file not found: ' . SDL::get_error();
  56. SDL::Mixer::Music::volume_music(85);
  57. if (SDL::Mixer::Music::play_music($music, -1)<0) {
  58. print("Can't play!\n". SDL::get_error()."\n");
  59. $play = 0;
  60. }
  61. } else {
  62. carp("No newline ".$self->{files}."\n".$_[0]."\n");
  63. $play = 0;
  64. }
  65. return $play;
  66. }
  67. sub loud {
  68. }
  69. sub pause {
  70. my $self = shift;
  71. SDL::Mixer::Music::pause_music();
  72. }
  73. sub resume {
  74. my $self = shift;
  75. SDL::Mixer::Music::resume_music();
  76. }
  77. sub stop {
  78. my $self = shift;
  79. SDL::Mixer::Music::halt_music();
  80. #SDL::Mixer::quit();
  81. }
  82. sub fade {
  83. }
  84. 1; # End of SDLx::Sound