在C WAV头的打印 [英] printing of a wav header in c

查看:103
本文介绍了在C WAV头的打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有这个code采取作为输入一个wav文件,然后把华包头到一个结构,然后输出。一切都只是AudioFormat的和numChannels不错,但我不明白why.For例如,它应该输出AudioFormat的:1和numChannels:2,但它输出AudioFormat的:0和numChannels:1。我不明白为什么会这样。

  typedef结构wavHeader
{
    字节chunckID [4];
    DWORD chunckSize;
    字节格式[4];
    字节subchunk1ID [4];
    字subchunk1Size;
    字AudioFormat的;
    字numChannels;
    DWORD采样率;
    DWORD byteRate;
    字blockAlign;
    字bitsPerSample;
    字节subchunk2ID [4];
    DWORD subchunk2Size;
} wav_header;INT check_file_name(字符*文件名);空列表(字符数组**)//的argv
{
    wav_header wavHeader;
    FILE * PFILE;
    如果(check_file_name(数组[2])== 0)
    {
        的printf(错误的文件名\\ n);
        出口(1);
    }
    PFILE = FOPEN(数组[2],R);
    如果(PFILE!= NULL)
    {
        FREAD(安培; wavHeader,sizeof的(wav_header),1,PFILE);
        FCLOSE(PFILE);
        的printf(ChunkID: %c%c%c%c\
\",wavHeader.chunckID[0],wavHeader.chunckID[1],wavHeader.chunckID[2],wavHeader.chunckID[3]);
        的printf(CHUNKSIZE数:%d \\ n,wavHeader.chunckSize);
        的printf(格式:%C%C%C%C \\ N,wavHeader.format [0],wavHeader.format [1],wavHeader.format [2],wavHeader.format [3]);
        的printf(SubChunk1ID: %c%c%c%c\
\",wavHeader.subchunk1ID[0],wavHeader.subchunk1ID[1],wavHeader.subchunk1ID[2],wavHeader.subchunk1ID[3]);
        的printf(Subchunk1Size数:%d \\ n,wavHeader.subchunk1Size);
        的printf(AudioFormat的数:%d \\ n,wavHeader.audioFormat);
        的printf(NumChannels数:%d \\ n,wavHeader.numChannels);
        的printf(采样率:%d个\\ N,wavHeader.sampleRate);
        的printf(ByteRate数:%d \\ n,wavHeader.byteRate);
        的printf(BlockAlign数:%d \\ n,wavHeader.blockAlign);
        的printf(BitsPerSample数:%d \\ n,wavHeader.bitsPerSample);
        的printf(Subchunk2ID: %c%c%c%c\
\",wavHeader.subchunk2ID[0],wavHeader.subchunk2ID[1],wavHeader.subchunk2ID[2],wavHeader.subchunk2ID[3]);
        的printf(Subchunk2Size数:%d \\ n,wavHeader.subchunk2Size);
    }
    其他
    {
        的printf(此文件不退出\\ n);
        出口(1);
    }
}


解决方案

subchunk1Size 应该是 DWORD ,不一个(见的 http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html 例如)。您的其他块大小是否正确声明。

由于 DJF指出你还需要指定包装和担心字节顺序。该WAV标头是16位的包装,和little-endian。随着 GCC ,我preFER使用的#pragma包(推,2)结构声明之前,和的#pragma包(POP)之后。如果你的#include< endian.h> 您可以使用 le32toh le16toh 阅读(及其对应 htole32 htole16 写)。

Hi i have this code for taking as input a wav file and then putting the wah header into a struct and then output it. Everything is good except audioFormat and numChannels but i can't understand why.For example it should output audioFormat: 1 and numChannels: 2 but it outputs audioFormat:0 and numChannels: 1 . I can't understand why this happens.

typedef struct wavHeader
{
    byte chunckID[4];
    dword chunckSize;
    byte format[4];
    byte subchunk1ID[4];
    word subchunk1Size;
    word audioFormat;
    word numChannels;
    dword sampleRate;
    dword byteRate;
    word blockAlign;
    word bitsPerSample;
    byte subchunk2ID[4];
    dword subchunk2Size;
}wav_header;

int check_file_name(char *filename);

void list(char **array) //argv
{
    wav_header wavHeader;
    FILE *pFile;
    if(check_file_name(array[2]) == 0)
    {
        printf("wrong file name\n");
        exit(1);
    }
    pFile = fopen (array[2] ,"r");
    if( pFile != NULL)
    {
        fread(&wavHeader, sizeof(wav_header), 1, pFile);
        fclose(pFile);
        printf("ChunkID: %c%c%c%c\n",wavHeader.chunckID[0],wavHeader.chunckID[1],wavHeader.chunckID[2],wavHeader.chunckID[3]);
        printf("ChunkSize: %d\n",wavHeader.chunckSize);
        printf("Format: %c%c%c%c\n",wavHeader.format[0],wavHeader.format[1],wavHeader.format[2],wavHeader.format[3]);
        printf("SubChunk1ID: %c%c%c%c\n",wavHeader.subchunk1ID[0],wavHeader.subchunk1ID[1],wavHeader.subchunk1ID[2],wavHeader.subchunk1ID[3]);
        printf("Subchunk1Size: %d\n",wavHeader.subchunk1Size);
        printf("AudioFormat: %d\n",wavHeader.audioFormat);
        printf("NumChannels: %d\n",wavHeader.numChannels); 
        printf("SampleRate: %d\n",wavHeader.sampleRate);
        printf("ByteRate: %d\n",wavHeader.byteRate);     
        printf("BlockAlign: %d\n",wavHeader.blockAlign);   
        printf("BitsPerSample: %d\n",wavHeader.bitsPerSample);
        printf("Subchunk2ID: %c%c%c%c\n",wavHeader.subchunk2ID[0],wavHeader.subchunk2ID[1],wavHeader.subchunk2ID[2],wavHeader.subchunk2ID[3]);
        printf("Subchunk2Size: %d\n",wavHeader.subchunk2Size);
    }
    else
    {
        printf("This file doesn't exit\n");
        exit(1);
    }
}

解决方案

subchunk1Size should be a dword, not a word (see http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html for instance). Your other chunk sizes are declared correctly.

As djf points out you also need to specify the packing and worry about endianness. The WAV header is 16-bit packed, and little-endian. With gcc, I prefer to use #pragma pack(push, 2) before the struct declaration, and #pragma pack(pop) after. And if you #include <endian.h> you can use le32toh and le16toh to read (and their counterparts htole32 and htole16 to write).

这篇关于在C WAV头的打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆