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.
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#if (_MSC_VER < 1900)
#define snprintf _snprintf
#endif
#endif
#include <stdio.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
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;
if (argc > 1) {
device = argv[1];
} else {
device = NULL;
}
return 1;
}
printf("First track : %d\n", first_track);
printf("Last track : %d\n", last_track);
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_to_time(sectors, ROUND_SECONDS,
time_str, sizeof time_str);
printf("Track %-2d : %8d %8d (%s)\n",
sectors, time_str);
}
return 0;
}
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.