#include < sys/types.h > #include < sys/stat.h > #include < sys/soundcard.h > #include < sys/ioctl.h > #include < unistd.h > #include < fcntl.h > #include < errno.h > #include < stdlib.h > #define SECONDS 5 //segundos de playback int main() { int fd; int handle = -1; int channels = 1; // 0=mono 1=stereo int format = AFMT_U8; int rate = 22000; unsigned char* data; /* Abre el archivo correspondiente a la tarjeta de sonido para escritura, DSP es Procesador de Señal Digital */ if ( (handle = open("/dev/dsp",O_WRONLY)) == -1 ) { perror("open /dev/dsp"); return -1; } /* Le dice a la tarjeta de sonido que el sonido a punto de reproducirse es estéreo. 0=mono 1=stereo */ if ( ioctl(handle, SNDCTL_DSP_STEREO,&channels) == -1 ) { perror("ioctl stereo"); return errno; } /* Informa a la tarjeta de sonido del formato del audio */ if ( ioctl(handle, SNDCTL_DSP_SETFMT,&format) == -1 ) { perror("ioctl format"); return errno; } /* Establece la tasa de playback de DSP, tasa de muestreo del audio PCM en bruto */ if (ioctl(handle, SNDCTL_DSP_SPEED,&rate) == -1 ) { perror("ioctl sample rate"); return errno; } // rate * 5 seconds * two channels data = malloc(rate*SECONDS*(channels+1)); if((fd=open("demo.pcm",O_RDONLY))==-1) { perror("open file"); exit(-1); } /* lee los datos contenidos en el archivo demo a la memoria reservada */ read(fd,data,rate*SECONDS*(channels+1)); close(fd); /* ¡Sólo escribe los datos en la tarjeta de sonido!just write the data to the sound card! Esto lo reproducirá. */ write(handle, data, rate*SECONDS*(channels+1)); if (ioctl(handle, SNDCTL_DSP_SYNC) == -1) { perror("ioctl sync"); return errno; } free(data); //Sé bueno y limpia. close(handle); printf("\nDone\n"); return 0; }