#!/usr/bin/perl -w  

# claudio fsr ;; 04 july 2005
# claudiofsr@yahoo.com :: see http://gnormalize.sourceforge.net

# see <man Audio::CD> - Perl interface to libcdaudio (cd + cddb)

# see /usr/include/cdaudio.h :

#/* Invisible volume structure */
#struct __volume { 
#  int left;
#  int right;
#};

#/* Volume structure */
#struct disc_volume {
#  struct __volume vol_front;                    /* Normal volume
#                                                   settings */
#  struct __volume vol_back;                     /* Surround sound
#                                                   volume settings */
#};

#int cd_set_volume(int cd_desc, struct disc_volume vol);
#int cd_get_volume(int cd_desc, struct disc_volume *vol);

# "A structure is a collection of one or more variables, possibly of different 
# types, grouped together under a single name for convenient handling.
# Structures help to organize complicated data, particularly in large programs, 
# because they permit a group of related variables to be treated as a unit instead
# of as separate entities." pg. 110 (The C Programing Language - Ritchie & Kernighan)

use Audio::CD;
my $cd = Audio::CD->init("/dev/cdrom");
my $info = $cd->stat;
my $ttrack = $info->total_tracks; # Returns the total number of tracks on the cd.

for (my $i=1; $i<=10; $i++){ # play random tracks

my $strarttrack = int( rand( $ttrack ) ) + 1;
my $endtrack = $strarttrack ;
$cd->play_track($strarttrack, $endtrack);

print "***------- track = $strarttrack --------***\n";

# Valid volumes: 0 - 255
# volume: front or back
my $vol = $cd->get_volume; # Returns an Audio::CD::Volume object.

print "Inicial volume:\n";

print_volume($vol);

print "Change the volume to:\n";

my $new_value_front_left  = rand ( 255 ); # Valid volumes: 0 - 255
my $new_value_front_right = rand ( 255 );
my $new_value_back_left   = rand ( 255 );
my $new_value_back_right  = rand ( 255 );

# Change the volume channels
$vol->front->left ( $new_value_front_left  );
$vol->front->right( $new_value_front_right );
$vol->back->left  ( $new_value_back_left   );
$vol->back->right ( $new_value_back_right  );
# Save this changes
$cd->set_volume ( $vol );

print_volume($vol);

#sleep 15 && $cd->stop;
sleep 15 && next;

} # for loop

sub print_volume{
   my $vol = shift;
   
   print "   front_left = ",$vol->front->left," ;; ";
   print "   front_right = ",$vol->front->right,"\n";
   print "   back_left  = ",$vol->back->left," ;; ";
   print "   back_right = ",$vol->back->right,"\n\n";
}


