libdiscid  0.6.4
discid.c

This example code uses libdiscid to read the TOC only from a CD and output the disc IDs and TOC details.The CD device to use can be specified as the first command line parameter. If none is given the platform's default device will be used.

/* --------------------------------------------------------------------------
MusicBrainz -- The Internet music metadatabase
Copyright (C) 2013 Johannes Dewender, Laurent Monin
Copyright (C) 2006 Matthias Friedrich
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
--------------------------------------------------------------------------- */
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#if (_MSC_VER < 1900)
#define snprintf _snprintf
#endif
#endif
#include <stdio.h>
#include <discid/discid.h>
#ifndef DISCID_HAVE_SPARSE_READ
#define discid_read_sparse(disc, dev, i) discid_read(disc, dev)
#endif
#define SECTORS_PER_SECOND 75
#define ROUND_SECONDS 0
/* Convert a number of sectors to a human-readable time in hours,minutes,seconds
* If round is true, seconds will be rounded to nearest,
* else 1/100 seconds time precision is used.
* If duration is over one hour, hours will be added in front of the string
* Examples:
* 33284 sectors with round=0 -> ( 7:23.79)
* 33284 sectors with round=1 -> ( 7:24)
* 356163 sectors with round=1 -> ( 1:19:09)
* 356163 sectors with round=0 -> ( 1:19:08.84)
*
* Result is written to buf
*/
void sectors_to_time(int sectors, int round, char *buf, size_t bufsize) {
float duration_in_secs = (float) sectors / SECTORS_PER_SECOND;
int hours = (int) duration_in_secs / 3600;
int minutes = (int) (duration_in_secs - hours * 3600) / 60;
float seconds = duration_in_secs - (hours * 3600 + minutes * 60);
if (round) {
int seconds_rounded = (int) (seconds + 0.5);
if (hours > 0) {
snprintf(buf, bufsize, "%d:%02d:%02d",
hours, minutes, seconds_rounded);
} else {
snprintf(buf, bufsize, " %2d:%02d",
minutes, seconds_rounded);
}
} else {
if (hours > 0) {
snprintf(buf, bufsize, "%d:%02d:%05.2f",
hours, minutes, seconds);
} else {
snprintf(buf, bufsize, " %2d:%05.2f",
minutes, seconds);
}
}
}
int main(int argc, char *argv[]) {
int i, first_track, last_track;
char *device = NULL;
char time_str[14];
int sectors;
DiscId *disc;
disc = discid_new();
/* If we have an argument, use it as the device name */
if (argc > 1) {
device = argv[1];
} else {
/* this will use discid_get_default_device() internally */
device = NULL;
}
if (discid_read_sparse(disc, device, 0) == 0) {
fprintf(stderr, "Error: %s\n", discid_get_error_msg(disc));
discid_free(disc);
return 1;
}
printf("DiscID : %s\n", discid_get_id(disc));
printf("FreeDB DiscID : %s\n", discid_get_freedb_id(disc));
first_track = discid_get_first_track_num(disc);
last_track = discid_get_last_track_num(disc);
printf("First track : %d\n", first_track);
printf("Last track : %d\n", last_track);
sectors = discid_get_sectors(disc);
sectors_to_time(sectors, ROUND_SECONDS, time_str, sizeof time_str);
printf("Length : %d sectors (%s)\n", sectors, time_str);
for ( i = first_track; i <= last_track; i++ ) {
sectors = discid_get_track_length(disc, i);
sectors_to_time(sectors, ROUND_SECONDS,
time_str, sizeof time_str);
printf("Track %-2d : %8d %8d (%s)\n",
sectors, time_str);
}
printf("Submit via : %s\n", discid_get_submission_url(disc));
discid_free(disc);
return 0;
}
/* EOF */
LIBDISCID_API DiscId * discid_new()
Return a handle for a new DiscId object.
LIBDISCID_API char * discid_get_id(DiscId *d)
Return a MusicBrainz DiscID.
LIBDISCID_API char * discid_get_submission_url(DiscId *d)
Return an URL for submitting the DiscID to MusicBrainz.
LIBDISCID_API int discid_get_track_offset(DiscId *d, int track_num)
Return the sector offset of a track.
LIBDISCID_API int discid_get_last_track_num(DiscId *d)
Return the number of the last audio track on this disc.
LIBDISCID_API int discid_get_sectors(DiscId *d)
Return the length of the disc in sectors.
void * DiscId
A transparent handle for an Audio CD.
Definition: discid.h:122
LIBDISCID_API int discid_get_track_length(DiscId *d, int track_num)
Return the length of a track in sectors.
LIBDISCID_API char * discid_get_freedb_id(DiscId *d)
Return a FreeDB DiscID.
LIBDISCID_API char * discid_get_error_msg(DiscId *d)
Return a human-readable error message.
LIBDISCID_API int discid_read_sparse(DiscId *d, const char *device, unsigned int features)
Read the disc in the given CD-ROM/DVD-ROM drive extracting only the TOC and additionally specified fe...
LIBDISCID_API int discid_get_first_track_num(DiscId *d)
Return the number of the first track on this disc.
LIBDISCID_API void discid_free(DiscId *d)
Release the memory allocated for the DiscId object.