Browse files

Starts an experimental audio support, by producing noise when the shi…

…p is moving...
  • Loading branch information...
1 parent e39503f commit fd7395fccd0098c17ed392b3373b47c8d4daa1c0 @ruoso committed Jan 8, 2010
Showing with 53 additions and 0 deletions.
  1. +2 −0 lib/Game/PerlInvaders.pm
  2. +51 −0 lib/Game/PerlInvaders/Audio.pm
View
2 lib/Game/PerlInvaders.pm
@@ -7,13 +7,15 @@ our $VERSION = 0.002;
use SDL 2.3;
use Game::PerlInvaders::Shared;
+use Game::PerlInvaders::Audio;
use Game::PerlInvaders::App;
use Game::PerlInvaders::Events;
use Game::PerlInvaders::Timer;
sub setup {
Game::PerlInvaders::App::setup_app;
Game::PerlInvaders::Timer::setup_timer;
+ Game::PerlInvaders::Audio::setup_audio;
}
sub run {
View
51 lib/Game/PerlInvaders/Audio.pm
@@ -0,0 +1,51 @@
+package Game::PerlInvaders::Audio;
+use strict;
+use warnings;
+use utf8;
+
+use threads;
+use threads::shared;
+
+use SDL::Constants;
+use SDL::Audio;
+use SDL::AudioSpec;
+
+my @sequencer;
+sub setup_audio {
+ my $desired = SDL::AudioSpec->new;
+ my $obtained = SDL::AudioSpec->new;
+ $desired->callback( 'main::Game::PerlInvaders::Audio::audio_callback'); #canno
+ $desired->freq( 11025 );
+ $desired->format( AUDIO_U8 );
+ $desired->samples( 2048 );
+ $desired->channels( 1 );
+ die('AudioMixer, Unable to open audio: '.SDL::get_error."\n" ) if ( SDL::Audio::open($desired, $obtained) < 0 );
+ SDL::Audio::pause(0);
+}
+
+sub audio_callback {
+ my ($int_size, $len, $streamref) = @_;
+
+ # produce noise if the ship is moving...
+ my $noise_amp = 0.2;
+
+ for my $i (0..($len-1)) {
+ # this should be the result value (-1 to +1) of this sample
+ my $val = 0;
+
+ if ($Game::PerlInvaders::Shared::keymask) {
+ $val += rand($noise_amp);
+ }
+
+ $val = int($val * 100) + 0x80; # this is the center...
+ $val = 0 if $val < 0;
+ $val = 255 if $val > 255;
+
+ substr($$streamref, $i, 1, chr($val));
+ }
+
+}
+
+
+
+42;

0 comments on commit fd7395f

Please sign in to comment.