From: http://blog.chinaunix.net/uid-9672747-id-3309602.html
# Include <stdio. h> # include <sys/types. h> # include <unistd. h> # include <stdlib. h> # include <sys/STAT. h> # include <fcntl. h> # include <sys/IOCTL. h> # include <sys/Soundcard. h> typedef struct _ wavefile_header // waveform file header Structure {char chunkid [4]; // "riff" Long chunksize; // the entire file size-8 bytes char format [4]; // "wave" char subchunk1id [3]; // "FMT" indicates the following format chunck long subchunk1size; // The number of bytes of the format data is generally 16, there are also special 18 short audioformat; // encoding methods Short numchannels; // number of channels long samplerate; // sampling rate long byterate; // number of required bytes per second short blockalign; // number of bytes required for each sampling short bitspersample; // The number of bits required for each sampling char subchunk2id [3]; // The 'dat 'string, which indicates long subchunk2size; // data size} wavefile_header; /// install the DSP to set the number of sampling channels int setup_dsp (INT dsp_fd, int rate, int channels) {int format; If (IOCTL (dsp_fd, sndctl_dsp_stereo, & channels) =-1) {perror ("IOCTL sterr failed"); return- 1 ;}switch (channels) {Case 0: format = afmt_u8; break; Case 1: format = afmt_s16_le; break; default: perror ("unsuport channels "); return-1;} If (IOCTL (dsp_fd, sndctl_dsp_setfmt, & format) =-1) {perror ("IOCTL set formalt"); Return-1;} If (format! = Afmt_s16_le) {perror ("dddddddd \ n"); Return-1;} If (IOCTL (dsp_fd, sndctl_dsp_speed, & rate) =-1) {perror ("IOCTL set_speed failed"); Return-1 ;}return 0 ;}// you can specify the synchronous playback int sync_dsp (INT dsp_fd) {If (IOCTL (dsp_fd, sndctl_dsp_sync, 0 )! = 0) {perror ("IOCTL sync failed"); Return-1;} return 0;} int play_music (char * music_name, int dsp_fd) {wavefile_header head; file * music_fp; int music_len; long rate; char * Buf; long n; // open the music file music_fp = fopen (music_name, "R +"); If (music_fp = NULL) {perror ("fopen music file failed"); Return-1 ;}// read the music file header if (fread (& head, sizeof (head), 1, music_fp )! = 1) {perror ("fread head Err"); Return-1;} // verify the file header if (memcmp (& head. chunkid, "riff", 4 )! = 0) {perror ("Music head chunkid Err"); Return-1 ;}// verify the bit rate if (head. bitspersample = 32) {perror ("unsuport samply size"); Return-1;} music_len = head. chunksize + 8; printf ("music_len: % d", music_len); // install the DSP to set the number of sampling rate channels if (setup_dsp (dsp_fd, head. samplerate, head. numchannels )! = 0) {perror ("setup_dsp failed"); Return-1 ;}/// the setup_dsp setting if (sync_dsp (dsp_fd) takes effect )! = 0) {perror ("Sync DSP failed"); Return-1 ;} // ----- set sampling rate -------- // number of sampling digits * sampling rate/8 = number of bytes // + 7 case where the sampling rate is not an integer multiple of 8 // 8*2 =/4 processing by dual-channel // size = (head. bitspersample * head. samplerate + 7)> 2; rate = head. byterate; // bytes read each time = byte rate Buf = (char *) malloc (rate); // The Music data cache printf ("rate: % LD \ n ", rate); // print the total time printf ("Music time: % d", head. subchunk2size/head. byterate); If (BUF! = NULL) {do {n = fread (BUF, 1, rate, music_fp); printf ("N: % LD \ n", n); If (n> 0) {write (dsp_fd, Buf, n);} If (feof (music_fp) {perror ("play over. "); break ;}}while (1) ;}int main (INT argc, char ** argv) {int dsp_fd; If (access (argv [1], r_ OK) {perror ("Access failed"); exit (0);} dsp_fd = open ("/dev/DSP", o_wronly); If (dsp_fd <0) {perror ("Open DSP Err"); exit (0);} play_music (argv [1], dsp_fd );}