在Windows上使用本机C ++读取WAV文件

时间:2018-01-16 23:07:43

标签: windows audio audio-processing

寻找一个简单的Windows本机调用来在Windows上以C ++打开和读取WAV文件(无需第三方库)。

我可以找到

  1. mmio *功能 - 已弃用。
  2. Media Foundation - 似乎更复杂,因为它“...构建一个COM服务器的DLL ...”
  3. 但是所有这些都比从本地文件读取字节更复杂。

    我可以自己编写解析WAV格式的代码,但是宁愿使用一些已经运行和测试的东西比我临时编写的东西要多得多。

    是否存在允许我执行的Microsoft WAV文件解析器/阅读器:

    1. 打开文件
    2. 检查标题
    3. 阅读PCM Dara

1 个答案:

答案 0 :(得分:-1)

只需遵守WAVE文件格式即可,基于此,您可以逐字节提取数据并对其进行解释。所以一旦你有了正确的文件路径,就可以做到这样的事情:这是我在Android中的wav文件阅读器的一段代码(不完整但有主要部分从wav文件中读取pcm数据) #39; s描述符)

typedef struct wav_file {
    unsigned char chunkId[4];
    uint32_t chunkSize;
    unsigned char format[4];
    unsigned char subchunk1Id[4];
    uint32_t subchunk1Size;
    uint16_t audioFormat;
    uint16_t numChannels;
    uint32_t sampleRate;
    uint32_t byteRate;
    uint16_t blockAlign;
    uint16_t bitsPerSample;
    unsigned char subchunk2Id[4];
    uint32_t subchunk2Size;
    unsigned char * data;
} WavFile;    

void swap_endian16(uint16_t val) {
    val = (val<<8) | (val>>8);
}

void swap_endian32(uint32_t val) {
     val = (val<<24) | ((val<<8) & 0x00ff0000) |
      ((val>>8) & 0x0000ff00) | (val>>24);
}

WavError loadWavFile(char filePath[], WavFile * wavFile) {
    WavError rc = NO_ERROR;
    FILE *fp = fopen(filePath,"rb");
    printf("wavreader file Path = %s from %s", (fp ? "file opened" : "failed to open file"), filePath);
    if(!fp) {
        rc = CANNOT_OPEN_FILE_ERROR;
        goto out1;
    }

    //chunkId
    size_t len = fread(wavFile->chunkId, sizeof(unsigned char), 4, fp);
    wavFile->chunkId[len] = '\0';
    printf("wavreader len:%d chunkId: %s", (int) len , wavFile->chunkId);
    if (strcmp((const char *) wavFile->chunkId, "RIFF")) {
        rc = NOT_WAV_FILE_ERROR;
        goto out1;
    }

    //chunkSize
    fread(&wavFile->chunkSize, sizeof(uint32_t), 1, fp);
    swap_endian32(wavFile->chunkSize);
    printf("wavreader chunkSize: %d", wavFile->chunkSize);

    //format
    len = fread(wavFile->format, sizeof(unsigned char), 4, fp);
    wavFile->format[len] = '\0';
    printf("wavreader format: %s", wavFile->format);
    if (strcmp((const char *)wavFile->format,"WAVE")) {
       rc = FMT_ERROR;
       goto out1;
    }

    //subchunk1Id
    len = fread(wavFile->subchunk1Id, sizeof(unsigned char), 4, fp);
    wavFile->subchunk1Id[len] = '\0';
    printf("wavreader subchunk1Id: %s", wavFile->subchunk1Id);
    if (strcmp((const char *)wavFile->subchunk1Id,"fmt ")) {
        rc = FMT_ID_ERROR;
        goto out1;
    }

    //subchunk1Size
    fread(&wavFile->subchunk1Size, sizeof(uint32_t), 1, fp);
    swap_endian32(wavFile->subchunk1Size);
    printf("wavreader subchunk1Size: %d", wavFile->subchunk1Size);

    //audioFormat
    fread(&wavFile->audioFormat, sizeof(uint16_t), 1, fp);
    swap_endian16(wavFile->audioFormat);
    printf("wavreader audioFormat: %d", wavFile->audioFormat);

    //numChannels
    fread(&wavFile->numChannels, sizeof(uint16_t), 1, fp);
    swap_endian16(wavFile->numChannels);
    printf("wavreader numChannels: %d", wavFile->numChannels);

    //sampleRate
    fread(&wavFile->sampleRate, sizeof(uint32_t), 1, fp);
    swap_endian32(wavFile->sampleRate);
    printf("wavreader sampleRate: %d", wavFile->sampleRate);

    //byteRate
    fread(&wavFile->byteRate, sizeof(uint32_t), 1, fp);
    swap_endian32(wavFile->byteRate);
    printf("wavreader byteRate: %d", wavFile->byteRate);

    //blockAlign
    fread(&wavFile->blockAlign, sizeof(uint16_t), 1, fp);
    swap_endian16(wavFile->blockAlign);
    printf("wavreader blockAlign: %d", wavFile->blockAlign);

    //bitsPerSample
    fread(&wavFile->bitsPerSample, sizeof(uint16_t), 1, fp);
    swap_endian16(wavFile->blockAlign);
    printf("wavreader bitRate: %d", wavFile->blockAlign);

    //subchunk2Id
    fread(wavFile->subchunk2Id, sizeof(unsigned char), 4, fp);
    wavFile->subchunk2Id[len] = '\0';
    printf("wavreader subchunk2Id: %s", wavFile->subchunk2Id);

    fread(&wavFile->subchunk2Size, sizeof(uint32_t), 1, fp);
    swap_endian32(wavFile->subchunk2Size);
    printf("wavreader subchunk2Size: %d", wavFile->subchunk2Size);

    wavFile->data = (unsigned char *) malloc (sizeof(unsigned char) * wavFile->subchunk2Size); //set aside sound buffer space
    fread(wavFile->data, sizeof(unsigned char), wavFile->subchunk2Size, fp); //read in our whole sound data chunk
    fclose(fp);
out1:
    return rc;
}
在wavFile-&gt;数据中,您可以从文件中获取实际的pcm数据。

PS:您应该使用this link作为参考