/* pcm_dat.c * This extra small demo sends a sound data file to your speakers. * The file has floating point samples in range -1.0 to +1.0 * You need to set sample_reate * sudo apt-get install libasound2 * sudo apt-get install libsound2-dev * get this file from web or * cp /afs/umbc.edu/users/s/q/squire/pub/download/pcm_dat.c . * gcc pcm_dat.c -l asound * a.out long_count.dat 10000 # you should hear a voice counting */ #include "alsa/asoundlib.h" #include #include #include #include #include static char *device = "default"; /* playback device */ snd_output_t *output = NULL; unsigned char *buffer; /* sound playback buffer */ int nbuf; int main(int argc, char *argv[]) { int err; int nrepeat = 1; unsigned int i; float x, y; float volume=1.0; /* range 0.0 to 1.0 max loud */ int nsamp, sample_rate; FILE * inp; char file_name[100]; int stat; snd_pcm_t *handle; snd_pcm_sframes_t frames; if(argc<3) { printf("sound data file and sample rate needed \n"); exit(1); } strcpy(file_name, argv[1]); printf("reading sound data from %s \n", file_name); sample_rate=atoi(argv[2]); printf("at sample rate %d Hz \n", sample_rate); inp=fopen(file_name, "r"); if(inp==NULL) { printf("can not open file %s for reading \n", file_name); exit(1); } nbuf=32*sample_rate; /* max 32 seconds played */ buffer = (unsigned char *)malloc(nbuf); for(i=0; i 0 && frames < (long)nbuf) printf("Short write (expected %li, wrote %li)\n", (long)sizeof(buffer), frames); } snd_pcm_close(handle); return 0; } /* end pcm_dat.c */