File: audio.pl

package info (click to toggle)
libdevice-cdio-perl 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,852 kB
  • sloc: ansic: 14,413; perl: 4,795; makefile: 19; sh: 3
file content (211 lines) | stat: -rwxr-xr-x 6,628 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/perl -w
#  Copyright (C) 2006, 2008, 2011 Rocky Bernstein <rocky@cpan.org>
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Program to read CD blocks. See read-cd from the libcdio distribution
# for a more complete program.

use strict;

BEGIN {
    chdir 'example' if -d 'example';
    use lib '../lib';
    eval "use blib";  # if we fail keep going - maybe we have installed Cdio
}

use Device::Cdio;
use Device::Cdio::Device;
use Device::Cdio::Track;

use vars qw($0 $program $pause %opts);

sub init() {
  use File::Basename;
  $program = basename($0); # Who am I today, anyway?
  $opts{close}=undef;
  $opts{eject}=0;
  $opts{pause}=0;
  $opts{play}=0;
  $opts{resume}=0;
  $opts{stop}=0;
  $opts{get_audio_volume}=0;
  $opts{set_audio_volume}='';
  $opts{set_audio}=0;
}

# Show the CVS version id string and quit.
sub show_version() {
    print "$program version $Device::Cdio::VERSION\n";
    exit 1;
}

sub usage {
  my($full_help) = @_;

  print "
usage:

   $program [options] [device]
    Issue analog audio CD controls - like playing

options:
    --help          -- print this help and exit
    --version       -- show a CVS version string and exit
    --eject DEVICE  -- eject disc from DEVICE
    --close DEVICE  -- close CD tray of DEVICE
    -P | --pause    -- pause playing
    --play          -- play entire CD
    --resume        -- resume playing
    --stop          -- stop playing
    --track=N       -- play track N
    --volume        -- get audio volume levels
    --set-volume=s  -- set audio volume levels
";
  exit 100;
}

# The bane of programming.
sub process_options() {
  use Getopt::Long;
  my(@opt_cf);
  $Getopt::Long::autoabbrev = 1;
  my($help, $long_help, $show_version);

  my $result = &GetOptions
    (
     'help'           => \$help,
     'close'          => \$opts{close},
     'eject'          => \$opts{eject},
     'version'        => \$opts{show_version},
     'P|pause'        => \$opts{pause},
     'play'           => \$opts{play},
     'resume'         => \$opts{resume},
     'stop'           => \$opts{stop},
     'volume'         => \$opts{get_audio_volume},
     'track=n'        => \$opts{track},
     'set-volume=s'   => \$opts{set_audio_volume},
    );
  show_version() if $show_version;

  usage(0) unless $result || @ARGV > 1;
  usage(1) if $help;
}

init();
process_options();

my ($drc, $device_name);

# Handle closing the CD-ROM tray if that was specified.
if ($opts{close}) {
    $device_name = $opts{close};
    $drc = Device::Cdio::close_tray(-drive=>$opts{close});
    if ($drc) {
	printf "Error closing: %s\n", perlcdio::driver_errmsg($drc) ;
	exit $drc;
    }
}

my $d;
if ($ARGV[0]) {
    $d = Device::Cdio::Device->new($ARGV[0]);
    exit(1) if !defined($d);
} else {
    # For audio any *has* to be set false.
    my @drives = Device::Cdio::get_devices_with_cap(-capabilities=>
						    $perlcdio::FS_AUDIO,
						    -any=>0);
    if (@drives >= 1) {
	$d = Device::Cdio::Device->new($drives[0]);
	if (!defined($d)) {
	    print "Sorry having trouble opening $drives[0]\n";
	    exit(1);
	}
    } else {
	print "Could not automatically find a CD-ROM with an audio CD in it\n";
	print "Please specify a device name\n";
	exit(1);
    }
}

$device_name = $d->get_device();

if ($opts{play}) {
    if ($d->get_disc_mode() ne 'CD-DA') {
        printf "The disc on %s I found was not CD-DA, but CD is %s\n",
	$device_name, $d->get_disc_mode();
        print "I have bad feeling about trying to play this...\n";
    }
    my $start_lsn = $d->get_first_track()->get_lsn();
    my $end_lsn=$d->get_disc_last_lsn();
    printf "Playing entire CD on %s\n", $device_name;
    $drc = $d->audio_play_lsn($start_lsn, $end_lsn);
    printf "Error playing CD: %s\n", perlcdio::driver_errmsg($drc) if ($drc);

} elsif ($opts{track}) {
    if ($d->get_disc_mode() ne 'CD-DA') {
        printf "The disc on %s I found was not CD-DA, but CD is %s\n",
	$device_name, $d->get_disc_mode();
        print "I have bad feeling about trying to play this...\n";
    }
    if ($opts{track} > $d->get_last_track()->{track}) {
	printf "Requested track %d but CD only has %d tracks\n", 
		$opts{track}, $d->get_last_track()->{track};
	exit(2);
    }
        
    if ($opts{track} < $d->get_first_track()->{track}) {
	printf "Requested track %d but first track on CD is %d\n",
	       $opts{track}, $d->get_first_track()->{track};
	exit(2);
    }
    printf("Playing track %d on %s\n", $opts{track}, $device_name);
    my $start_lsn = $d->get_track($opts{track})->get_lsn();
    my $end_lsn = $d->get_track($opts{track}+1)->get_lsn();
    $drc = $d->audio_play_lsn($start_lsn, $end_lsn);
    printf "Error closing: %s\n", perlcdio::driver_errmsg($drc) if ($drc);
} elsif ($opts{get_audio_volume}) {
    my($vol, $rc) = $d->audio_get_volume();
    printf "Audio volume levels: %s\n", join(', ', @$vol);
} elsif ($opts{set_audio_volume}) {
    my @levels=split(/,\s*/, $opts{set_audio_volume});
    # $d->audio_set_volume(@levels);
    $d->audio_set_volume($levels[0], $levels[1], $levels[2], $levels[3]);
     my($vol, $rc) = $d->audio_get_volume();
     printf "Audio volume levels are now: %s\n", join(', ', @$vol);
} elsif ($opts{pause}) {
    printf "Pausing playing in drive %s\n", $device_name;
    $drc = $d->audio_pause();
    printf "Error pausing: %s\n", perlcdio::driver_errmsg($drc) if ($drc);
} elsif ($opts{resume}) {
    printf "Resuming playing in drive %s\n", $device_name;
    $drc = $d->audio_resume();
    printf "Error resuming: %s\n", perlcdio::driver_errmsg($drc) if ($drc);
} elsif ($opts{stop}) {
    printf "Stopping playing in drive %s\n", $device_name;
    $drc = $d->audio_stop();
    printf("Stopping failed on drive %s: %s\n", 
	   $device_name, perlcdio::driver_errmsg($drc)) 
	if $drc;
} elsif ($opts{eject}) {
    print "Ejecting CD in CD-ROM drive $device_name\n";
    $drc = $d->eject_media();
    printf("Eject failed on drive %s: %s\n", 
	   $device_name, perlcdio::driver_errmsg($drc))
	if $drc;
    exit $drc;
}
        
$d->close();