📄 readaudio.c
字号:
/* * readaudio.c * Example program to read digital audio data from CD * * usage: readaudio <min> <sec> <frame> <# frames> <device> * e.g. readaudio 1 2 3 4 /dev/cdrom * */#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <sys/ioctl.h>#include <fcntl.h>#include <linux/cdrom.h>#include <linux/sbpcd.h>int main(int argc, char *argv[]){ int fd; /* CD-ROM file descriptor */ int status; /* hold status of system calls */ struct cdrom_read_audio read_audio; /* parameter for ioctl call */ unsigned char *buffer; /* buffer for audio data */ int i; /* loop index */ int minute, second, frame; /* where to start reading */ int nframes; /* number of frames to read */ char *device; /* CD-ROM device name */ /* check and parse command line arguments */ if (argc != 6) { fprintf(stderr, "usage: readaudio <min> <sec> <frame> <# frames> <device>\n" " e.g. readaudio 1 2 3 4 /dev/cdrom\n"); exit(1); } minute = strtol(argv[1], 0, 10); second = strtol(argv[2], 0, 10); frame = strtol(argv[3], 0, 10); nframes = strtol(argv[4], 0, 10); device = argv[5]; /* open device */ status = fd = open(device, O_RDONLY); if (status < 0) { perror("open failed"); exit(1); } /* set audio buffer size - only needed for SBPCD drives */ status = ioctl(fd, CDROMAUDIOBUFSIZ, nframes); if (status <= 0) { perror("CDROMAUDIOBUFSIZ ioctl failed (ignore for non-SBPCD drives)"); /* don't exit, in case it is a non-SBPCD drive */ } /* dynamically allocate buffer for audio data */ buffer = (unsigned char*) malloc(CD_FRAMESIZE_RAW * nframes); if (buffer == 0) { perror("malloc failed"); exit(1); } read_audio.addr_format = CDROM_MSF; /* use MSF format */ read_audio.addr.msf.minute = minute; read_audio.addr.msf.second = second; read_audio.addr.msf.frame = frame; read_audio.nframes = nframes; read_audio.buf = buffer; printf("Reading %d frames from %s starting from %02d:%02d.%03d (%d bytes)\n", nframes, device, minute, second, frame, CD_FRAMESIZE_RAW * nframes); status = ioctl(fd, CDROMREADAUDIO, &read_audio); if (status != 0) { perror("CDROMREADAUDIO ioctl failed"); exit(1); } /* display data in hex, 24 bytes per line */ for (i = 0 ; i < CD_FRAMESIZE_RAW * nframes ; i++) { printf("%02X ", buffer[i]); if ((i+1) % 24 == 0) printf("\n"); } exit(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -