void WvOut :: closeFile( void ) { if ( fd ) { // If there's an existing file, close it first. writeData( counter ); if ( fileType == WVOUT_RAW ) fclose( fd ); else if ( fileType == WVOUT_WAV ) closeWavFile(); else if ( fileType == WVOUT_SND ) closeSndFile(); else if ( fileType == WVOUT_AIF ) closeAifFile(); else if ( fileType == WVOUT_MAT ) closeMatFile(); fd = 0; printf("%f Seconds Computed\n\n", getTime() ); totalCount = 0; } }
int main(int argc, char *argv[]) { FILE* file; int16_t format, channels, width; long speed; int fd, samples; char *data; int stereo; int audioOutputRate=22050; int sndformat; audio_buf_info info; if (argc!=2) { fprintf(stderr, "Pass a wave file as an argument\n"); return -1; } file = openWavFile(argv[1], &format, &speed, &samples, &channels, &width); if (!file) { fprintf(stderr, "Failed to read the given file\n"); return -1; } printf("File: %s\n", argv[1]); printf("Format: %d\n", format); printf("Speed: %d\n", speed); printf("Samples: %d\n", samples); printf("Channels: %d\n", channels); printf("Width: %d\n", width); data = (char*)malloc(samples * width * channels); if (readWavData(file, data, samples * channels, width)) { fprintf(stderr, "Failed to read the wav data\n"); closeWavFile(file); return -1; } closeWavFile(file); fd = open("/dev/dsp", O_WRONLY, 0); if (fd == -1) { fd = open("/dev/sound/dsp", O_WRONLY, 0); if (fd == -1) { fprintf(stderr, "Failed to open /dev/dsp or /dev/sound/dsp\n"); return -1; } } #if BYTE_ORDER == BIG_ENDIAN sndformat=AFMT_S16_BE; #else sndformat=AFMT_S16_LE; #endif int oldFormat = sndformat; if ((ioctl(fd, SNDCTL_DSP_SETFMT, &sndformat)==-1) || sndformat!=oldFormat) { fprintf(stderr, "Format now %d\n", sndformat); close(fd); fprintf(stderr, "Couldn't put audio in 16bit mode\n"); return -1; } stereo = 1; if ((ioctl(fd, SNDCTL_DSP_STEREO, &stereo)==-1) || stereo!=1) { close(fd); fprintf(stderr, "Couldn't set stereo mode\n"); return -1; } if ((ioctl(fd, SNDCTL_DSP_SPEED, &audioOutputRate)==-1) || audioOutputRate!=22050) { close(fd); fprintf(stderr, "Couldn't set rate to %d\n", 22050); return -1; } write(fd, data, samples*channels); fprintf(stderr, "sound is %dus\n", samples * (1000000 / audioOutputRate)); usleep(samples * (1000000 / audioOutputRate)); /* while (1) { if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info)==-1) { fprintf(stderr, "Couldn't read sound buffer space\n"); return -1; } fprintf(stderr, "fragstotal=%d fragssize=%d bytes=%d\n", info.fragstotal, info.fragsize, info.bytes); if (info.bytes==info.fragstotal*info.fragsize) break; usleep(10000); } */ close(fd); return 0; }