从二进制文件中读取一个Int

时间:2022-10-01 19:45:22

I have been having a heck of a time reading in an int from a .Wav file. I've been following the .wav specifications listed Here. I'm trying to read an int (Subchunk2Size) from the header and then the data into an array of shorts. I am running into obstacles with both fread() and ifstream.read(). I will list my problems with both reading technique problems, but really only need an answer to one.

我一直在从.Wav文件中读取一个int。我一直在遵循这里列出的.wav规范。我正在尝试从头部读取一个int(Subchunk2Size),然后将数据读入一个short数组。我遇到了fread()和ifstream.read()的障碍。我会用阅读技巧问题列出我的问题,但实际上只需要一个答案。

fread()

FREAD()

    sprintf(filename, "C:\\Users\\MacbookWin7\\wavs\\%s.wav", word);
FILE * infile = fopen(filename, "rb");
if (infile != NULL)
{
    int f = ferror(infile);
    int d = fseek(infile, 40, SEEK_SET);
    int * subChunk2Size = new int[1];
    int i = fread(subChunk2Size, sizeof(int), 1, infile);
    int g = ferror(infile);

The Problem: fread does not read in anything, i is always initialized to 0.

问题:fread没有读入任何内容,我总是被初始化为0。

I have tried many different combinations of declaring subChunk2Size, trying to declare a pointer, an array - both with single and multiple elements, declaring a nullptr, and declaring an int and passing in its reference.

我已经尝试了许多不同的组合来声明subChunk2Size,尝试声明一个指针,一个数组 - 包含单个和多个元素,声明一个nullptr,并声明一个int并传入它的引用。

ifstream.read()

ifstream.read()

    sprintf(filename, "C:\\Users\\MacbookWin7\\wavs\\%s.wav", word);
ifstream sampleStream(filename,ios::binary);
char * samplesStr;

if (sampleStream.is_open())
{
    sampleStream.seekg(40);
    char * subChunk2SizeStr = new char[4];
    sampleStream.read(subChunk2SizeStr, 4);
    int subChunk2Size = 0;
    for (int i = 3; i >= 0; i--)
    {
        subChunk2Size = (subChunk2Size << 8) + subChunk2SizeStr[i];
    }
    int numBytes = subChunk2Size + *nsamples * 2;
    char * samplesStr = new char[numBytes];
    sampleStream.read(samplesStr, numBytes);
    sampleStream.close();
    short * samples = new short[numBytes / 2];
    for (int i = 0; i < numBytes / 2; i++)
    {
        int b = i * 2;
        samples[i] = samplesStr[b+1];
        samples[i] = (samples[i] << 8) + samplesStr[b];
    }

The Problem: My first reads and bit shifts turn out the correct answer of around 22,000, but I am unable to read in the shorts with the second loop.

问题:我的第一次读取和位移转出大约22,000的正确答案,但是我无法用第二个循环读取短路。

I would like to thank anybody for giving my problem some consideration.

我要感谢任何人对我的问题进行一些考虑。

1 个解决方案

#1


3  

I took your code, mangled it a little bit, and it seems to show reasonable results when I'm using a 16-bit per sample, stereo input, such as this

我拿了你的代码,把它弄错了一点,当我使用每个样本16位的立体声输入时,它似乎显示出合理的结果,比如这个

#include <fstream>
#include <cstdio>
#include <iostream>
#include <climits>

using namespace std;

int main()
{
    const char *filename = "audio/M1F1-int16-AFsp.wav";
    ifstream sampleStream(filename,ios::binary);
    int nsamples = 10000;
    short * samples;
    if (sampleStream.is_open())
    {
        sampleStream.seekg(44);  // Skip all of the header. 
        samples = new short[nsamples];
        sampleStream.read(reinterpret_cast<char *>(samples), sizeof(short)*nsamples);
        sampleStream.close();
    }
    short max = 0; 
    short min = 32767;
    int min_at = 0; 
    int max_at = 0;
    for(int i = 0; i < nsamples; i++)
    {
        if (samples[i] > max) 
        {
            max = samples[i];
            max_at = i;
        }
        if (samples[i] < min)
        {
            min = samples[i];
            min_at = i;
        }
    }
    cout << "Max=" << max << " at " << max_at << endl;
    cout << "Min=" << min << " at " << min_at << endl;
}

It shows a top peak at 14325 at sample 990 [sample 495 in stereo], and a lowest level of -7125 at 8766, both of which match up when comparing to the values in the audacity software. Note that I'm relying on the x86 processor on my machine being little endian. If you are running on a Mac, then that would be the case too, unless it's a really old PowerPC Mac.

它在样本990 [样本495在立体声中]显示14325的顶峰,在8766显示最低水平-7125,与audacity软件中的值相比,两者都匹配。请注意,我依赖于我的机器上的x86处理器是小端。如果你在Mac上运行,那也是如此,除非它是一台非常古老的PowerPC Mac。

#1


3  

I took your code, mangled it a little bit, and it seems to show reasonable results when I'm using a 16-bit per sample, stereo input, such as this

我拿了你的代码,把它弄错了一点,当我使用每个样本16位的立体声输入时,它似乎显示出合理的结果,比如这个

#include <fstream>
#include <cstdio>
#include <iostream>
#include <climits>

using namespace std;

int main()
{
    const char *filename = "audio/M1F1-int16-AFsp.wav";
    ifstream sampleStream(filename,ios::binary);
    int nsamples = 10000;
    short * samples;
    if (sampleStream.is_open())
    {
        sampleStream.seekg(44);  // Skip all of the header. 
        samples = new short[nsamples];
        sampleStream.read(reinterpret_cast<char *>(samples), sizeof(short)*nsamples);
        sampleStream.close();
    }
    short max = 0; 
    short min = 32767;
    int min_at = 0; 
    int max_at = 0;
    for(int i = 0; i < nsamples; i++)
    {
        if (samples[i] > max) 
        {
            max = samples[i];
            max_at = i;
        }
        if (samples[i] < min)
        {
            min = samples[i];
            min_at = i;
        }
    }
    cout << "Max=" << max << " at " << max_at << endl;
    cout << "Min=" << min << " at " << min_at << endl;
}

It shows a top peak at 14325 at sample 990 [sample 495 in stereo], and a lowest level of -7125 at 8766, both of which match up when comparing to the values in the audacity software. Note that I'm relying on the x86 processor on my machine being little endian. If you are running on a Mac, then that would be the case too, unless it's a really old PowerPC Mac.

它在样本990 [样本495在立体声中]显示14325的顶峰,在8766显示最低水平-7125,与audacity软件中的值相比,两者都匹配。请注意,我依赖于我的机器上的x86处理器是小端。如果你在Mac上运行,那也是如此,除非它是一台非常古老的PowerPC Mac。