Exemplo n.º 1
0
bool wave_open()
{
    static char    *channel_mappings[] = {NULL,"mono","stereo"};
    unsigned short  wFormatTag;
    unsigned long   dAvgBytesPerSec;
    unsigned short  wBlockAlign;
    long            filesize;
    long            header_size;

    //if((config.wave.file = fopen(config.infile,"rb")) == NULL) ERROR("Unable to open file");

    if(!checkString(config.wave.file,"RIFF")) ERROR("Input not a MS-RIFF file");
    filesize = Read32BitsLowHigh(config.wave.file); /* complete wave chunk size */
    fprintf(stderr, "Microsoft RIFF, ");

    if(!checkString(config.wave.file,"WAVE")) ERROR("Input not WAVE audio");
    fprintf(stderr, "WAVE audio, ");

/* WAVE FMT format chunk */
    if(!checkString(config.wave.file,"fmt "))  ERROR("Can't find format chunk");
/* my total header size calculations don't work, so this is bogus... */
    header_size = Read32BitsLowHigh(config.wave.file); /* chunk size */

    wFormatTag = Read16BitsLowHigh(config.wave.file);
    if(wFormatTag!=0x0001) ERROR("Unknown WAVE format");
    config.wave.type = WAVE_RIFF_PCM;

    config.wave.channels   = Read16BitsLowHigh(config.wave.file);
    config.wave.samplerate = Read32BitsLowHigh(config.wave.file);
    dAvgBytesPerSec        = Read32BitsLowHigh(config.wave.file);
    wBlockAlign            = Read16BitsLowHigh(config.wave.file);

/* PCM specific */
    if(config.wave.channels>2) ERROR("More than 2 channels\n");
   fprintf(stderr, "PCM, %s %ldHz ", 
           channel_mappings[config.wave.channels], config.wave.samplerate);
    config.wave.bits       = Read16BitsLowHigh(config.wave.file);
    if(config.wave.bits!=16) ERROR("NOT 16 Bit !!!\n");
    fprintf(stderr, "%dbit, ", config.wave.bits);

    if(!checkString(config.wave.file,"data")) ERROR("Can't find data chunk");

    header_size = ftell(config.wave.file);
    fseek(config.wave.file, 0, SEEK_END);
    filesize = ftell(config.wave.file);
    fseek(config.wave.file, header_size, SEEK_SET);

    config.wave.total_samples = (filesize-header_size)/(2*config.wave.channels);
    config.wave.length = config.wave.total_samples/config.wave.samplerate;
    fprintf(stderr, "Length: %2ld:%2ld:%2ld\n", (config.wave.length/3600), 
                                       (config.wave.length/60)%60,
                                       (config.wave.length%60));
    return true;
}
Exemplo n.º 2
0
static int
parse_wave_header(FILE *sf)
{
	fmt_chunk_data wave_info;
	int is_wav = 0;
	long data_length = 0, file_length, subSize = 0;
	int loop_sanity = 0;

	memset(&wave_info, 0, sizeof(wave_info));

	file_length = Read32BitsHighLow(sf);

	if (Read32BitsHighLow(sf) != WAV_ID_WAVE)
		return 0;

	for (loop_sanity = 0; loop_sanity < 20; ++loop_sanity) {
		u_int type = Read32BitsHighLow(sf);

		if (type == WAV_ID_FMT) {
			subSize = Read32BitsLowHigh(sf);
			if (subSize < 16) {
			  /*fprintf(stderr,
			    "'fmt' chunk too short (only %ld bytes)!", subSize);  */
				return 0;
			}

			wave_info.format_tag		= Read16BitsLowHigh(sf);
			subSize -= 2;
			wave_info.channels			= Read16BitsLowHigh(sf);
			subSize -= 2;
			wave_info.samples_per_sec	= Read32BitsLowHigh(sf);
			subSize -= 4;
			wave_info.avg_bytes_per_sec = Read32BitsLowHigh(sf);
			subSize -= 4;
			wave_info.block_align		= Read16BitsLowHigh(sf);
			subSize -= 2;
			wave_info.bits_per_sample	= Read16BitsLowHigh(sf);
			subSize -= 2;

			/* fprintf(stderr, "   skipping %d bytes\n", subSize); */

			if (subSize > 0) {
				if (fskip(sf, (long)subSize, SEEK_CUR) != 0 )
					return 0;
			};

		} else if (type == WAV_ID_DATA) {
			subSize = Read32BitsLowHigh(sf);
			data_length = subSize;
			is_wav = 1;
			/* We've found the audio data.	Read no further! */
			break;

		} else {
			subSize = Read32BitsLowHigh(sf);
			if (fskip(sf, (long) subSize, SEEK_CUR) != 0 ) return 0;
		}
	}

	if (is_wav) {
		/* make sure the header is sane */
		wave_check("name", &wave_info);

		num_channels  = wave_info.channels;
		samp_freq     = wave_info.samples_per_sec;
		num_samples   = data_length / (wave_info.channels * wave_info.bits_per_sample / 8);
	}
	return is_wav;
}