Skip to content
Browse files

Clean up and bumped the script

  • Loading branch information...
1 parent 4ec77f8 commit d55ec084e1e3ad61992ec2144bafda7cef5f3265 @kthakore kthakore committed Jan 1, 2011
Showing with 90 additions and 62 deletions.
  1. +37 −24 code_listings/stereograph.pl
  2. +34 −24 dist/SDL_Manual.html
  3. BIN dist/SDL_Manual.pdf
  4. +19 −14 src/08-music_and_sound.pod
View
61 code_listings/stereograph.pl
@@ -18,23 +18,17 @@
use SDL::Mixer::Effects;
use SDLx::App;
-
-my $stream_data :shared = '';
-my $stream_lock :shared = 0;
-
-
-my $lines = 100;
-
my $app = SDLx::App->new(
init => SDL_INIT_AUDIO | SDL_INIT_VIDEO,
width => 800,
height => 600,
depth => 32,
- title => "Sound Event Demo",
+ title => "Music Visualizer",
eoq => 1,
dt => 0.2,
);
+
# Initialize the Audio
unless ( SDL::Mixer::open_audio( 44100, AUDIO_S16, 2, 1024 ) == 0 ) {
Carp::croak "Cannot open audio: " . SDL::get_error();
@@ -44,6 +38,9 @@
my $data_dir = '.';
my @songs = glob 'data/music/*.ogg';
+my $stream_data :shared = '';
+my $stream_lock :shared = 0;
+
# Music Effect to pull Stream Data
sub music_data {
my $channel = shift;
@@ -60,39 +57,58 @@ sub music_data {
}
sub done_music_data { }
+
+my $music_data_effect_id =
+ SDL::Mixer::Effects::register( MIX_CHANNEL_POST, "main::music_data",
+ "main::done_music_data", 0 );
+
+
# Music Playing Callbacks
my $current_song = 0;
+
+my $lines = $ARGV[0];
+ $lines = 50 unless $lines;
+
+
my $current_music_callback = sub {
my( $delta, $app ) = @_;
if( $stream_lock == 0 ){
$app->draw_rect([ 0, 0, $app->w(), $app->h()], 0x000000FF );
+ $app->draw_gfx_text([5,$app->h()-10], [255,0,0,255], "Playing Song: ".$songs[$current_song-1]);
my @stream = split( ',', $stream_data );
$stream_data = '';
my @left;
my @right;
+
+ #To show the right amount of lines we choose a cut of the stream
+ #this is purely for asthetic reasons.
my $cut = $#stream/$lines;
my @x;
-
+
+ #The width of each line is calculated to use.
my $l_wdt= ( $app->w() / $lines) /2;
for ( my $i = 0 ; $i < $#stream ; $i += $cut ) {
+ # In stereo mode the stream is split between two alternating streams
my $left = $stream[$i];
my $right = $stream[ $i + 1 ];
+ # For each bar we calculate a Y point and a X point
my $point_y = ( ( ($left) ) * $app->h()/4 / 32000 ) + ($app->h/2);
my $point_y_r = ( ( ($right) ) * $app->h()/4 / 32000 )+ ($app->h/2);
my $point_x = ( $i / $#stream ) * $app->w;
-
- SDL::GFX::Primitives::box_RGBA( $app, $point_x-$l_wdt, 300, $point_x+$l_wdt, $point_y, 40, 0, 255, 128 );
- SDL::GFX::Primitives::box_RGBA( $app, $point_x-$l_wdt, 300, $point_x+$l_wdt, $point_y_r , 255, 0, 40, 128 );
+ #Using the parameters
+ # Surface, box coordinates and color as RGBA
+ SDL::GFX::Primitives::box_RGBA( $app, $point_x-$l_wdt, $app->h()/2, $point_x+$l_wdt, $point_y, 40, 0, 255, 128 );
+ SDL::GFX::Primitives::box_RGBA( $app, $point_x-$l_wdt, $app->h()/2, $point_x+$l_wdt, $point_y_r , 255, 0, 40, 128 );
}
@@ -102,8 +118,8 @@ sub music_data {
$app->flip();
};
-my $cms_move_callback;
-my $pns_move_callback;
+my $cms_move_callback_id;
+my $pns_move_callback_id;
my $play_next_song_callback = sub {
@@ -113,20 +129,20 @@ sub music_data {
SDL::Mixer::Music::hook_music_finished('main::music_finished_playing');
SDL::Mixer::Music::play_music($song, 0 );
- $app->remove_move_handler( $pns_move_callback ) if defined $pns_move_callback;
- $cms_move_callback = $app->add_show_handler( $current_music_callback );
+ $app->remove_move_handler( $pns_move_callback_id ) if defined $pns_move_callback_id;
+ $cms_move_callback_id = $app->add_show_handler( $current_music_callback );
};
sub music_finished_playing {
SDL::Mixer::Music::halt_music();
- $pns_move_callback = $app->add_move_handler( $play_next_song_callback );
- $app->remove_show_handler($cms_move_callback);
+ $pns_move_callback_id = $app->add_move_handler( $play_next_song_callback );
+ $app->remove_show_handler($cms_move_callback_id);
}
-my $music_data_effect_id =
- SDL::Mixer::Effects::register( MIX_CHANNEL_POST, "main::music_data",
- "main::done_music_data", 0 );
+
+$pns_move_callback_id= $app->add_move_handler( $play_next_song_callback);
+
$app->add_event_handler(
sub {
@@ -141,9 +157,6 @@ sub music_finished_playing {
}
);
-$pns_move_callback= $app->add_move_handler( $play_next_song_callback);
-
-
$app->run();
SDL::Mixer::Effects::unregister( MIX_CHANNEL_POST, $music_data_effect_id );
View
58 dist/SDL_Manual.html
@@ -3272,6 +3272,8 @@
+<h3>The Code and Comments</h3>
+
<pre><code> use strict;
use warnings;
@@ -3291,11 +3293,11 @@
use SDL::Mixer::Music;
use SDL::Mixer::Effects;
- use SDLx::App;
+ use SDLx::App;</code></pre>
- my $lines = 100;
+<p>Start the application with audio and video support.</p>
- my $app = SDLx::App-&gt;new(
+<pre><code> my $app = SDLx::App-&gt;new(
init =&gt; SDL_INIT_AUDIO | SDL_INIT_VIDEO,
width =&gt; 800,
height =&gt; 600,
@@ -3352,7 +3354,12 @@
playing. We read the stream data and show it on the screen as a wave
form.</p>
-<pre><code> # Music Playing Callbacks
+<p>The number of lines to show in our visualizer.</p>
+
+<pre><code> my $lines = $ARGV[0];
+ $lines = 50 unless $lines;
+
+ # Music Playing Callbacks
my $current_song = 0;
my $current_music_callback = sub {
@@ -3362,6 +3369,7 @@
if( $stream_lock == 0 ){
$app-&gt;draw_rect([ 0, 0, $app-&gt;w(), $app-&gt;h()], 0x000000FF );
+ $app-&gt;draw_gfx_text([5,$app-&gt;h()-10], [255,0,0,255], &quot;Playing Song: &quot;.$songs[$current_song-1]);
my @stream = split( &#39;,&#39;, $stream_data );
$stream_data = &#39;&#39;;
@@ -3383,8 +3391,13 @@
my $point_x = ( $i / $#stream ) * $app-&gt;w;
- SDL::GFX::Primitives::box_RGBA( $app, $point_x-$l_wdt, 300, $point_x+$l_wdt, $point_y, 40, 0, 255, 128 );
- SDL::GFX::Primitives::box_RGBA( $app, $point_x-$l_wdt, 300, $point_x+$l_wdt, $point_y_r , 255, 0, 40, 128 );
+ SDL::GFX::Primitives::box_RGBA( $app, $point_x-$l_wdt, 300,
+ $point_x+$l_wdt, $point_y,
+ 40, 0, 255, 128 );
+
+ SDL::GFX::Primitives::box_RGBA( $app, $point_x-$l_wdt, 300,
+ $point_x+$l_wdt, $point_y_r ,
+ 255, 0, 40, 128 );
}
@@ -3393,18 +3406,17 @@
}
$app-&gt;flip();
};
+When a song is finished playing we remove the show handler and hook on another callback to switch to the
+next song gracefully.
- my $cms_move_callback;
- my $pns_move_callback; </code></pre>
-
-<p>When a song is finished playing we remove the show handler and hook on
-another callback to switch to the next song gracefully.</p>
+ my $cms_move_callback_id;
+ my $pns_move_callback_id;
-<pre><code> sub music_finished_playing {
+ sub music_finished_playing {
SDL::Mixer::Music::halt_music();
- $pns_move_callback = $app-&gt;add_move_handler( $play_next_song_callback );
- $app-&gt;remove_show_handler($cms_move_callback);
+ $pns_move_callback_id = $app-&gt;add_move_handler( $play_next_song_callback );
+ $app-&gt;remove_show_handler($cms_move_callback_id);
}
@@ -3416,11 +3428,14 @@
SDL::Mixer::Music::hook_music_finished(&#39;main::music_finished_playing&#39;);
SDL::Mixer::Music::play_music($song, 0 );
- $app-&gt;remove_move_handler( $pns_move_callback ) if defined $pns_move_callback;
- $cms_move_callback = $app-&gt;add_show_handler( $current_music_callback );
- };</code></pre>
+ $app-&gt;remove_move_handler( $pns_move_callback_id ) if defined $pns_move_callback_id;
+ $cms_move_callback_id = $app-&gt;add_show_handler( $current_music_callback );
+ };
+Add the first callback to get our first song.
+
+ $pns_move_callback_id= $app-&gt;add_move_handler( $play_next_song_callback);</code></pre>
-<p>The event handler allows you to move through the songs.</p>
+<p>Add a event handler allows you to move through the songs.</p>
<pre><code> $app-&gt;add_event_handler(
sub {
@@ -3433,12 +3448,7 @@
}
}
- );</code></pre>
-
-<p>Add the first callback to get our first song, and start runing the
-<code>$app</code>.</p>
-
-<pre><code> $pns_move_callback= $app-&gt;add_move_handler( $play_next_song_callback);
+ );
$app-&gt;run();</code></pre>
View
BIN dist/SDL_Manual.pdf
Binary file not shown.
View
33 src/08-music_and_sound.pod
@@ -445,6 +445,7 @@ like:
\caption{Simple Music Visualization}
\label{fig:Visualization}
+=head2 The Code and Comments
use strict;
use warnings;
@@ -467,7 +468,7 @@ like:
use SDLx::App;
- my $lines = 100;
+Start the application with audio and video support.
my $app = SDLx::App->new(
init => SDL_INIT_AUDIO | SDL_INIT_VIDEO,
@@ -523,6 +524,11 @@ We hook the callback into C<SDL::Mixer::Effects>.
The drawing callback for the C<SDLx::App> when a song is playing. We read the stream data and show it on the screen as a wave form.
+The number of lines to show in our visualizer.
+
+ my $lines = $ARGV[0];
+ $lines = 50 unless $lines;
+
# Music Playing Callbacks
my $current_song = 0;
@@ -533,6 +539,7 @@ The drawing callback for the C<SDLx::App> when a song is playing. We read the st
if( $stream_lock == 0 ){
$app->draw_rect([ 0, 0, $app->w(), $app->h()], 0x000000FF );
+ $app->draw_gfx_text([5,$app->h()-10], [255,0,0,255], "Playing Song: ".$songs[$current_song-1]);
my @stream = split( ',', $stream_data );
$stream_data = '';
@@ -569,18 +576,17 @@ The drawing callback for the C<SDLx::App> when a song is playing. We read the st
}
$app->flip();
};
-
- my $cms_move_callback;
- my $pns_move_callback;
-
When a song is finished playing we remove the show handler and hook on another callback to switch to the
next song gracefully.
+ my $cms_move_callback_id;
+ my $pns_move_callback_id;
+
sub music_finished_playing {
SDL::Mixer::Music::halt_music();
- $pns_move_callback = $app->add_move_handler( $play_next_song_callback );
- $app->remove_show_handler($cms_move_callback);
+ $pns_move_callback_id = $app->add_move_handler( $play_next_song_callback );
+ $app->remove_show_handler($cms_move_callback_id);
}
@@ -592,11 +598,14 @@ next song gracefully.
SDL::Mixer::Music::hook_music_finished('main::music_finished_playing');
SDL::Mixer::Music::play_music($song, 0 );
- $app->remove_move_handler( $pns_move_callback ) if defined $pns_move_callback;
- $cms_move_callback = $app->add_show_handler( $current_music_callback );
+ $app->remove_move_handler( $pns_move_callback_id ) if defined $pns_move_callback_id;
+ $cms_move_callback_id = $app->add_show_handler( $current_music_callback );
};
+Add the first callback to get our first song.
-The event handler allows you to move through the songs.
+ $pns_move_callback_id= $app->add_move_handler( $play_next_song_callback);
+
+Add a event handler allows you to move through the songs.
$app->add_event_handler(
sub {
@@ -611,10 +620,6 @@ The event handler allows you to move through the songs.
}
);
-Add the first callback to get our first song, and start runing the C<$app>.
-
- $pns_move_callback= $app->add_move_handler( $play_next_song_callback);
-
$app->run();
Gracefully stop C<SDL::Mixer>.

0 comments on commit d55ec08

Please sign in to comment.
Something went wrong with that request. Please try again.