将.wav文件的数据存储在C++

发布于 2025-02-10 07:43:41 字数 2120 浏览 1 评论 0原文

我正在尝试将.wav文件的数据存储在数组中。我无法找到一种将来自SubChunk2数据存储在数组中的方法。有人可以帮我吗?

这是我到现在使用的代码,

typedef struct header_file
{
    char chunk_id[4];
    int chunk_size;
    char format[4];
    char subchunk1_id[4];
    int subchunk1_size;
    short int audio_format;
    short int num_channels;
    int sample_rate;            // sample_rate denotes the sampling rate.
    int byte_rate;
    short int block_align;
    short int bits_per_sample;
    char subchunk2_id[4];
    int subchunk2_size;         // subchunk2_size denotes the number of samples.
} header;

typedef struct header_file* header_p;

基本上主要是

 //Load wave file
    FILE* infile = fopen("E:/fCWT-main/MATLAB/1s_speech.wav", "rb");        // Open wave file in read mode
    int BUFSIZE = 512;                  // BUFSIZE can be changed according to the frame size required (eg:512)
    int count = 0;                      // For counting number of frames in wave file.
    short int buff16[BUFSIZE];              // short int used for 16 bit as input data format is 16 bit PCM audio
    header_p meta = (header_p)malloc(sizeof(header));   // header_p points to a header struct that contains the wave file metadata fields
    int nb;                         // variable storing number of byes returned
    

   
    if (infile)
    {
        fread(meta, 1, sizeof(header), infile);
        //fwrite(meta, 1, sizeof(*meta), outfile);
        int samples = meta->subchunk2_size;
        size_t result;
        tmp = (float*)malloc(sizeof(float) * samples);
        while (!feof(infile))
        {
            nb = fread(buff16, 1, BUFSIZE, infile);     // Reading data in chunks of BUFSIZE
            cout << nb << endl;
                        
            count++;                    // Incrementing Number of frames

           
        }
                         
        cout << " Number of frames in the input wave file are " << count << endl;
    }

在这里,我想存储并将WAV文件的数据显示到一个数组中,以便我可以使用该数组进行进一步的处理,并且我不知道该怎么做。我尝试在线搜索它,但是解决方案仅限于阅读标题文件。

我是C ++的新手,因此对此有任何帮助。 谢谢!

I am trying to store data of .wav file in an array. I am not able to find a way to store data from subchunk2 in an array. Can anybody help me with this?

This is the code which I have used till now

typedef struct header_file
{
    char chunk_id[4];
    int chunk_size;
    char format[4];
    char subchunk1_id[4];
    int subchunk1_size;
    short int audio_format;
    short int num_channels;
    int sample_rate;            // sample_rate denotes the sampling rate.
    int byte_rate;
    short int block_align;
    short int bits_per_sample;
    char subchunk2_id[4];
    int subchunk2_size;         // subchunk2_size denotes the number of samples.
} header;

typedef struct header_file* header_p;

Main Part

 //Load wave file
    FILE* infile = fopen("E:/fCWT-main/MATLAB/1s_speech.wav", "rb");        // Open wave file in read mode
    int BUFSIZE = 512;                  // BUFSIZE can be changed according to the frame size required (eg:512)
    int count = 0;                      // For counting number of frames in wave file.
    short int buff16[BUFSIZE];              // short int used for 16 bit as input data format is 16 bit PCM audio
    header_p meta = (header_p)malloc(sizeof(header));   // header_p points to a header struct that contains the wave file metadata fields
    int nb;                         // variable storing number of byes returned
    

   
    if (infile)
    {
        fread(meta, 1, sizeof(header), infile);
        //fwrite(meta, 1, sizeof(*meta), outfile);
        int samples = meta->subchunk2_size;
        size_t result;
        tmp = (float*)malloc(sizeof(float) * samples);
        while (!feof(infile))
        {
            nb = fread(buff16, 1, BUFSIZE, infile);     // Reading data in chunks of BUFSIZE
            cout << nb << endl;
                        
            count++;                    // Incrementing Number of frames

           
        }
                         
        cout << " Number of frames in the input wave file are " << count << endl;
    }

Basically here I want to store and display the data of wav file into an array so that I can use that array for further processing and I don't have any idea how to do it. I have tried searching it online but that solutions were limited to reading Header File.

I am new in C++ so any help regarding this will work.
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文