how to use the ioctl() function with audio CD drives

576 views Asked by At

I am trying to write a C program using the I/O call system in Ubuntu.

I found this documentation, CDROM API from Linux-sxs.org, but I don't understand where to find those arguments.

Can you please give me an example about how to use the ioctl() function?

struct cdrom_read_audio ra
{
    union cdrom_addr addr; /* REQUIRED frame address */
    u_char addr_format; /* REQUIRED .....CDROM_LBA or CDROM_MSF */
    int nframes;         /* REQUIRED number of 2352-byte-frames to read*/
    u_char *buf;         /* REQUIRED frame buffer (size: nframes*2352 bytes) */
};

if (ioctl(cdrom, CDROMREADAUDIO, &ra)<0)
{
    perror("ioctl");
    exit(1);
}
1

There are 1 answers

0
abewieland On

According to the kernel documentation for the cdrom driver, cdrom.txt, the format of the command is as follows:

CDROMREADAUDIO          (struct cdrom_read_audio)

usage:

  struct cdrom_read_audio ra;
  ioctl(fd, CDROMREADAUDIO, &ra);

inputs:
  cdrom_read_audio structure containing read start
  point and length

outputs:
  audio data, returned to buffer indicated by ra

error return:
  EINVAL    format not CDROM_MSF or CDROM_LBA
  EINVAL    nframes not in range [1 75]
  ENXIO     drive has no queue (probably means invalid fd)
  ENOMEM    out of memory

The format of the cdrom_read_audio struct can be found in cdrom.h:

/* This struct is used by the CDROMREADAUDIO ioctl */
struct cdrom_read_audio
{
    union cdrom_addr addr; /* frame address */
    __u8 addr_format;      /* CDROM_LBA or CDROM_MSF */
    int nframes;           /* number of 2352-byte-frames to read at once */
    __u8 __user *buf;      /* frame buffer (size: nframes*2352 bytes) */
};

It uses a union cdrom_addr type, defined in the same file:

/* Address in either MSF or logical format */
union cdrom_addr        
{
    struct cdrom_msf0   msf;
    int                 lba;
};

Here we have a choice - use MSF (Mintues-Seconds-Frames) or LBA (Logical Block Addressing). Since you're reading audio, you'll probably want MSF. struct cdrom_msf0 can also be found in the header file:

/* Address in MSF format */
struct cdrom_msf0       
{
    __u8    minute;
    __u8    second;
    __u8    frame;
};

With this research, we can write a simple test:

#include <sys/ioctl.h>    //Provides ioctl()
#include <linux/cdrom.h>  //Provides struct and #defines
#include <unistd.h>       //Provides open() and close()
#include <sys/types.h>    //Provides file-related #defines and functions
#include <sys/stat.h>     //Ditto
#include <fcntl.h>        //Ditto
#include <stdlib.h>       //Provides malloc()
#include <string.h>       //Provides memset()
#include <stdint.h>       //Provides uint8_t, etc
#include <errno.h>        //Provides errno
#include <stdio.h>        //Provides printf(), fprintf()

int main() 
{
  int fd = open("/dev/cdrom", O_RDONLY | O_NONBLOCK);
  if (errno != 0)
  {
    fprintf(stderr, "Error opening file: %u\n", errno);
    return -1;
  }
  struct cdrom_msf0 time; //The start read time ...
  time.minute = 2;
  time.second = 45;
  time.frame = 0;
  union cdrom_addr address;  //... in a union
  address.msf = time;
  struct cdrom_read_audio ra;  //Our data object
  ra.addr = address;           //With the start time
  ra.addr_format = CDROM_MSF;  //We used MSF
  ra.nframes = CD_FRAMES;      //A second - 75 frames (the most we can read at a time anyway)
  uint8_t* buff = malloc(CD_FRAMES * CD_FRAMESIZE_RAW); //Frames per second (75) * bytes per frame (2352)
  memset(buff, 0, CD_FRAMES * CD_FRAMESIZE_RAW); //Make sure it's empty
  ra.buf = buff;               //Set our buffer in our object
  if (ioctl(fd, CDROMREADAUDIO, &ra) != 0)  //The ioctl call
  {
    fprintf(stderr, "Error giving ioctl command: %u\n", errno);
    return -1;
  }
  for (int frame = 0; frame < CD_FRAMES; frame++)  //A hexdump (could be a real use for the data)
  {
    printf("Frame %u:", frame);
    for (int byte = 0; byte < CD_FRAMESIZE_RAW; byte++)
    {
      printf(" %.2X", buff[frame * CD_FRAMESIZE_RAW + byte]);
    }
    printf("\n");
  }
  close(fd);  //Close our file
  return 0;   //And exit
}

Make sure you use an audio CD, or the ioctl call will throw EIO (with a CD-ROM, for example). In reality, you might write this data to file, or process it. Either way, you'd likely end up reading more than one second using a loop.