#include #include #include #include #include #include #include #include #include #include #include #include static char *filename, *filenametail, *sampbuf; static int fd, bytes_per_samp, samps_per_frame, frames_per_sec; static int bytes_per_buf, samps_per_buf, frames_per_buf; static long filefmt, bits_per_samp, compression; static double file_rate, secs_per_frame, secs_per_buf; int InitAudio(); int PlaySamples(); void GetPathTail(); void main(int argc, char **argv) { AFfilehandle audio_file; ALport audio_port; extern int optind; filename = argv[optind]; GetPathTail(filename, &filenametail); fd = open(filename, O_RDONLY); AFidentifyfd(fd); audio_file = AFopenfd(fd,"r",AF_NULL_FILESETUP); InitAudio(audio_file, &audio_port); PlaySamples(audio_file, audio_port); AFclosefile(audio_file); ALcloseport(audio_port); free(sampbuf); } static int PlaySamples(AFfilehandle audio_file, ALport audio_port) { int num_bufs, leftover_bytes, leftover_samps, leftover_frames, i; int bytes_read, samples_read, frames_read, total_frames, total_samps, total_samp_bytes; float file_playingtime, fileplayingtime; char compressionname[10]; int samp_count = 0, frame_count = 0; double sec_count = 0.0; total_frames = AFgetframecnt(audio_file, AF_DEFAULT_TRACK); total_samps = total_frames * samps_per_frame; total_samp_bytes = total_samps * bytes_per_samp; num_bufs = total_samp_bytes / bytes_per_buf; leftover_bytes = total_samp_bytes % bytes_per_buf; leftover_samps = leftover_bytes / bytes_per_samp; leftover_frames = leftover_samps / samps_per_frame; GetPathTail(filename, &filenametail); schedctl(NDPRI, 0, NDPNORMMAX); AFseekframe(audio_file, AF_DEFAULT_TRACK, 0); for (i=0; num_bufs<0 || i0) printf("\n%d sample frames %6.3f secs\n", frame_count, sec_count); frames_read = AFreadframes(audio_file, AF_DEFAULT_TRACK, sampbuf, leftover_frames); samples_read = frames_read * samps_per_frame; ALwritesamps(audio_port, sampbuf, samples_read); while(ALgetfilled(audio_port) > 0) sginap(1); sginap(10); } static int InitAudio(AFfilehandle audio_file, ALport *audio_port) { ALconfig audio_port_config; long pvbuf[4], audio_rate, output_rate, samp_type, samp_wordsize, vers; samps_per_frame = AFgetchannels(audio_file, AF_DEFAULT_TRACK); file_rate = AFgetrate(audio_file, AF_DEFAULT_TRACK); compression = AFgetcompression(audio_file, AF_DEFAULT_TRACK); filefmt = AFgetfilefmt(audio_file, &vers); AFgetsampfmt(audio_file, AF_DEFAULT_TRACK, &samp_type, &bits_per_samp); pvbuf[0] = AL_OUTPUT_COUNT; pvbuf[2] = AL_MONITOR_CTL; ALgetparams(AL_DEFAULT_DEVICE, pvbuf, 4); audio_rate = frames_per_sec = (long) file_rate; output_rate = GetOutputRate(); if (bits_per_samp <= 8) { bytes_per_samp = 1; samp_wordsize = AL_SAMPLE_8; } else if (bits_per_samp <= 16) { bytes_per_samp = 2; samp_wordsize = AL_SAMPLE_16; } else if (bits_per_samp <= 24) { bytes_per_samp = 4; samp_wordsize = AL_SAMPLE_24; } secs_per_frame = 1.0 / ((double)frames_per_sec); frames_per_buf = (frames_per_sec+1)/2; samps_per_buf = frames_per_buf * samps_per_frame; bytes_per_buf = samps_per_buf * bytes_per_samp; secs_per_buf = secs_per_frame * frames_per_buf; sampbuf = malloc(bytes_per_buf); audio_port_config = ALnewconfig(); ALsetwidth(audio_port_config, samp_wordsize); ALsetchannels(audio_port_config, samps_per_frame); ALsetqueuesize(audio_port_config, samps_per_buf*2); *audio_port = ALopenport("player", "w", audio_port_config); } int GetInputRate() { long buf[6]; buf[0] = AL_INPUT_RATE; buf[2] = AL_INPUT_SOURCE; buf[4] = AL_DIGITAL_INPUT_RATE; ALgetparams(AL_DEFAULT_DEVICE, buf, 6); if ((buf[1] == AL_RATE_AES_1)||(buf[3] == AL_INPUT_DIGITAL)){ if (ALgetdefault(AL_DEFAULT_DEVICE, AL_DIGITAL_INPUT_RATE) >= 0) return (buf[5]); } else if (buf[1] > 0) return (buf[1]); return (AL_RATE_UNDEFINED); } int GetOutputRate() { long buf[4]; buf[0] = AL_OUTPUT_RATE; buf[2] = AL_DIGITAL_INPUT_RATE; ALgetparams(AL_DEFAULT_DEVICE, buf, 4); if (buf[1] > 0) return (buf[1]); else if (buf[1] == AL_RATE_AES_1){ if (ALgetdefault(AL_DEFAULT_DEVICE,AL_DIGITAL_INPUT_RATE) >= 0) return (buf[3]); } else if (buf[1] == AL_RATE_INPUTRATE) return (GetInputRate()); return (AL_RATE_UNDEFINED); } static void GetPathTail(char *thepath, char **thetail) { char *p; p = strrchr(thepath, '/'); if (p) p++; else p = thepath; *thetail = (char *)malloc(strlen(p) + 1); strcpy(*thetail, p); }