Skip to content

Commit

Permalink
Clean up and bumped the script
Browse files Browse the repository at this point in the history
  • Loading branch information
kthakore committed Jan 1, 2011
1 parent 4ec77f8 commit d55ec08
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 62 deletions.
61 changes: 37 additions & 24 deletions code_listings/stereograph.pl
Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -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 );

}

Expand All @@ -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 {

Expand All @@ -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 {
Expand All @@ -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 );
Expand Down
58 changes: 34 additions & 24 deletions dist/SDL_Manual.html
Expand Up @@ -3272,6 +3272,8 @@ <h2>Music Visualizer</h2>



<h3>The Code and Comments</h3>

<pre><code> use strict;
use warnings;

Expand All @@ -3291,11 +3293,11 @@ <h2>Music Visualizer</h2>
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,
Expand Down Expand Up @@ -3352,7 +3354,12 @@ <h2>Music Visualizer</h2>
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 {
Expand All @@ -3362,6 +3369,7 @@ <h2>Music Visualizer</h2>
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;;
Expand All @@ -3383,8 +3391,13 @@ <h2>Music Visualizer</h2>
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 );

}

Expand All @@ -3393,18 +3406,17 @@ <h2>Music Visualizer</h2>
}
$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);

}

Expand All @@ -3416,11 +3428,14 @@ <h2>Music Visualizer</h2>
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 {
Expand All @@ -3433,12 +3448,7 @@ <h2>Music Visualizer</h2>
}

}
);</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>

Expand Down
Binary file modified dist/SDL_Manual.pdf
Binary file not shown.
33 changes: 19 additions & 14 deletions src/08-music_and_sound.pod
Expand Up @@ -445,6 +445,7 @@ like:
\caption{Simple Music Visualization}
\label{fig:Visualization}

=head2 The Code and Comments

use strict;
use warnings;
Expand All @@ -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,
Expand Down Expand Up @@ -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;

Expand All @@ -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 = '';
Expand Down Expand Up @@ -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);

}

Expand All @@ -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 {
Expand All @@ -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>.
Expand Down

0 comments on commit d55ec08

Please sign in to comment.