The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


AudioCD::Mac - MacPerl extension for controlling Audio CDs


    #!perl -w
    use AudioCD;
    use strict;
    my $cd = new AudioCD;

    print "Now playing\n" if $cd->status == CD_PLAY;
    printf "Volume is %d\n", $cd->volume;

    print "Now paused\n" if $cd->status == CD_PAUSE;

    print "Now playing\n" if $cd->status == CD_PLAY;
    printf "Volume is %d\n", $cd->volume;

    my @info = $cd->info;
    printf "Currently at track %d, %.2d:%.2d\n", @info[0..2];

    my $status = $cd->status;
    print "Now stopped\n" if
        ($status == CD_FINISH || $status == CD_STOP);

    if (do '') {  # sold separately
        my $cddb = new CDDB;
        my @cddb_info = $cddb->calculate_id( $cd->cddb_toc );
        my @discs = $cddb->get_discs(@cddb_info[0, 3, 4]);
        print "You were probably listening to $discs[0]->[2]\n";



This is the MacPerl module to be used by the AudioCD module. Other modules can be written for other platforms, but this one is Mac specific, calling Mac OS APIs to control the CD player.


All functions except for new are methods that need to be called via the AudioCD object. All functions attempt to return undef and set $^E on failure, and unless otherwise specified, return 1 on success.

Note that the data returned from the Mac OS APIs is often in BCD format, but the functions that return track and time data convert it to decimal.


Should be called from AudioCD, not AudioCD::Mac, but should work either way. Returns the object.


Returns the current status of the CD, one of: CD_PLAY, CD_PAUSE, CD_MUTE, CD_FINISH, CD_ERR, CD_STOP, CD_OFFLINE.


Returns an array of information about the CD, with two sets of times, one for the current track, one for the disc. The first item in the list is the current track number.

    #  0          1       2        3         4       5        6
    ($t_number, $t_min, $t_sec, $t_frames, $a_min, $a_sec, $a_frames)
        = $cd->info;

If called without a track number, will start from the first track if stopped, or continue if paused. Otherwise will start at specified track number, and continue until the CD is finished.


Stops the CD. Time settings will likely be left at last play point, not at beginning of CD.


Will pause the CD if it is playing, or continue playing if paused.


Will continue playing if paused.


Will unmount the volumes on the drive if any, and then eject the tray.


Sets the left and right channels to a valume from 0 to 255. Returns the left and right channel volumes, unless the two have the same value and the method is called in a scalar context, in which case it returns just one value.

If RIGHT_VOLUME is not supplied, it will be set to the same value as supplied in LEFT_VOLUME. If not values are supplied, the current volume value(s) will be returned, and will remain unchanged.


Returns the table of contents in an anonymous array, where each element is another anonymous array, containing the track number [0] and the track's starting time offset from the beginning of the CD in minutes [1], seconds [2], and frames [3].


Returns the same as above, but in a format suitable for passing to the CDDB module.


Ideally, returns the last audio track number on the CD. If this turns out to be wrong, let me know.


Change interface to work well with others (Tuomas' Linux code)
Add support for multiple drives, multiple volumes on drive
Add support for moving forward/backward in tracks, and scanning
Add support for modes (stereo/mono/etc., random/program/repeat/etc.)
Add support to add/extract data in the CD Remote Programs file
Need good test suite ... don't know how this should work, since I won't have good data to test against. Maybe I should just play some tracks, change the volume, print out the number of tracks and have the user say whether or not it passes.


Chris Nandor <>

Copyright (c) 1998 Chris Nandor. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Please see the Perl Artistic License.

Special thanks to Matthias Neeracher for help with MacPerl XS and C, and Glenn Howes <> for his code examples that helped me understand this stuff.

For more info, check out Technote DV22:


v0.25, Thursday, December 11, 1998

Significant clean up of Perl and XS code, lots of misc. changes.

Hopefully fixed cd_toc to give all the right values.

Fixed eject problems.

   "no pointer is a pointer, too" -- Matthias Neeracher
v0.20, Wednesday, December 9, 1998

Renamed to AudioCD, added controls for Audio CD.

v0.10, Thursday, October 8, 1998

First version, made for Mac OS to get CDDB TOC data.


v0.25, Thursday, December 11, 1998