Example #1
0
static int check_header(FILE *f)
{
    AU_HEADER(header);
    u_int32_t magic;
    u_int32_t hdr_size;
    u_int32_t data_size;
    u_int32_t encoding;
    u_int32_t sample_rate;
    u_int32_t channels;

    if (fread(header, 1, AU_HEADER_SIZE, f) != AU_HEADER_SIZE)
    {
        cw_log(LOG_WARNING, "Read failed (header)\n");
        return -1;
    }
    magic = ltohl(header[AU_HDR_MAGIC_OFF]);
    if (magic != (u_int32_t) AU_MAGIC)
    {
        cw_log(LOG_WARNING, "Bad magic: 0x%x\n", magic);
    }
/*    hdr_size = ltohl(header[AU_HDR_HDR_SIZE_OFF]);
    if (hdr_size < AU_HEADER_SIZE)*/
    hdr_size = AU_HEADER_SIZE;
/*    data_size = ltohl(header[AU_HDR_DATA_SIZE_OFF]); */
    encoding = ltohl(header[AU_HDR_ENCODING_OFF]);
    if (encoding != AU_ENC_8BIT_ULAW)
    {
        cw_log(LOG_WARNING, "Unexpected format: %d. Only 8bit ULAW allowed (%d)\n", encoding, AU_ENC_8BIT_ULAW);
        return -1;
    }
    sample_rate = ltohl(header[AU_HDR_SAMPLE_RATE_OFF]);
    if (sample_rate != 8000)
    {
        cw_log(LOG_WARNING, "Sample rate can only be 8000 not %d\n", sample_rate);
        return -1;
    }
    channels = ltohl(header[AU_HDR_CHANNELS_OFF]);
    if (channels != 1)
    {
        cw_log(LOG_WARNING, "Not in mono: channels=%d\n", channels);
        return -1;
    }
    /* Skip to data */
    fseek(f, 0, SEEK_END);
    data_size = ftell(f) - hdr_size;
    if (fseek(f, hdr_size, SEEK_SET) == -1)
    {
        cw_log(LOG_WARNING, "Failed to skip to data: %d\n", hdr_size);
        return -1;
    }
    return data_size;
}
Example #2
0
static int write_header(FILE *f)
{
	AU_HEADER(header);

	header[AU_HDR_MAGIC_OFF] = htoll((uint32_t) AU_MAGIC);
	header[AU_HDR_HDR_SIZE_OFF] = htoll(AU_HEADER_SIZE);
	header[AU_HDR_DATA_SIZE_OFF] = 0;
	header[AU_HDR_ENCODING_OFF] = htoll(AU_ENC_8BIT_ULAW);
	header[AU_HDR_SAMPLE_RATE_OFF] = htoll(DEFAULT_SAMPLE_RATE);
	header[AU_HDR_CHANNELS_OFF] = htoll(1);

	/* Write an au header, ignoring sizes which will be filled in later */
	fseek(f, 0, SEEK_SET);
	if (fwrite(header, 1, AU_HEADER_SIZE, f) != AU_HEADER_SIZE) {
		ast_log(LOG_WARNING, "Unable to write header\n");
		return -1;
	}
	return 0;
}