为什么我的程序没有打开WAV文件?

时间:2018-04-25 07:09:33

标签: c++ file wav libsndfile

我有一个C ++代码。我正在尝试使用此代码打开大约20个WAV文件。但当它到达第10个文件时,它会停止打开并读取文件?问题是什么?我有20个WAV文件存储在我的计算机上的某个路径上。它适用于文件编号1到9,但它不会从文件10到20打开。我确定我的文件路径是正确的,我正在给libsnd打开文件函数。

#include <stdio.h>
#include <stdlib.h>
#include <sndfile.h>
#include <iostream>
#include <math.h>
#include <fftw3.h>
#include <stdint.h>
#include <string.h>
using namespace std;

void func_fileopen(int son, char note[], int samplenum) { 
    SNDFILE *sf;
    SF_INFO info;
    int num_channels;
    int num, num_items;
    float *buf = NULL; //pointer to float
    int f, sr, c;
    int i, j;
    FILE *out;
    int samples = 913;
    float outbuf[samples];
    char n[son];
    char temp1[son] = {0};
    char temp2[son + 13] = {0};
    strcpy(n, note);
    char filebasepath[] = "C:\\Users\\Gumm\\Documents\\Audacity\\10ms\\";
    char samplepath[] = "\\Sample";
    char wav[] = ".wav";
    temp1[16] = sprintf(temp1, "%s", n);
    temp2[13] = sprintf(temp2, "%s%d%s", samplepath, samplenum, wav);
    strcat(filebasepath, temp1);
    strcat(filebasepath, temp2);
    puts(filebasepath);

    /* Open the WAV file. */
    info.format = 0;

    sf = sf_open(filebasepath, SFM_READ, &info);

    if (sf == NULL) {
        printf("Failed to open the file.\n");
        exit(-1);
    }

    /* Print some of the info, and figure out how much data to read. */
    f = info.frames;
    sr = info.samplerate;
    c = info.channels;
    printf("frames=%d\n", f);
    printf("samplerate=%d\n", sr);
    printf("channels=%d\n", c);
    num_items = f * c;
    printf("num_items=%d\n", num_items);

    /* Allocate space for the data to be read, then read it. */
    buf = (float *) malloc(num_items * sizeof(float));
    num = sf_read_float(sf, buf, num_items);
    sf_close(sf);
    printf("Read %d items\n", num);
    /* Write the data to filedata.out. */
    //out = fopen("filedata.out","w");
    }

int main() {   
    int notenum;
    int samplenum;
    int notevar;
    int sizeofnote;
    char note[sizeofnote];
    for (notenum = 1; notenum < 8; notenum++)
    {
        for (samplenum = 1; samplenum < 21; samplenum++)
        {

        notevar = notenum;
        if (notevar == 1) {
            char note[] = "ardha chapu";
            sizeofnote = sizeof(note) / sizeof(char) - 1;
            func_fileopen(sizeofnote,note,samplenum); 
        } else if (notevar == 2) {
            char note[] = "chapu";
            sizeofnote = sizeof(note) / sizeof(char) - 1;
            func_fileopen(sizeofnote, note, samplenum);
        } else if (notevar == 3) {
            char note[] = "dheem";
            sizeofnote = sizeof(note) / sizeof(char) - 1;
            func_fileopen(sizeofnote, note, samplenum);
        } else if (notevar == 4) {
            char note[] = "dhi";
            sizeofnote=sizeof(note) / sizeof(char) - 1;
            func_fileopen(sizeofnote, note, samplenum);
        } else if (notevar == 5) {
            char note[] = "num";
            sizeofnote = sizeof(note) / sizeof(char) - 1;
            func_fileopen(sizeofnote, note, samplenum);
        } else if (notevar==6) {
            char note[] = "sampoorna chapu";
            sizeofnote = sizeof(note) / sizeof(char) - 1;
            func_fileopen(sizeofnote, note, samplenum);
        } else {
            char note[] = "ta";
            sizeofnote = sizeof(note) / sizeof(char) - 1;
            func_fileopen(sizeofnote, note, samplenum);
        }
    }
    return 0;    
}

这是我的输出

C:\Users\Gumm\Documents\Audacity\10ms\ardha chapu\Sample1.wav
frames=944
samplerate=96000
channels=1
num_items=944
Read 944 items

C:\Users\Gumm\Documents\Audacity\10ms\ardha chapu\Sample2.wav
frames=941
samplerate=96000
channels=1
num_items=941
Read 941 items

C:\Users\Gumm\Documents\Audacity\10ms\ardha chapu\Sample3.wav
frames=946
samplerate=96000
channels=1
num_items=946
Read 946 items

C:\Users\Gumm\Documents\Audacity\10ms\ardha chapu\Sample4.wav
frames=961
samplerate=96000
channels=1
num_items=961
Read 961 items

C:\Users\Gumm\Documents\Audacity\10ms\ardha chapu\Sample5.wav
frames=967
samplerate=96000
channels=1
num_items=967
Read 967 items

C:\Users\Gumm\Documents\Audacity\10ms\ardha chapu\Sample6.wav
frames=915
samplerate=96000
channels=1
num_items=915
Read 915 items

C:\Users\Gumm\Documents\Audacity\10ms\ardha chapu\Sample7.wav
frames=973
samplerate=96000
channels=1
num_items=973
Read 973 items

C:\Users\Gumm\Documents\Audacity\10ms\ardha chapu\Sample8.wav
frames=998
samplerate=96000
channels=1
num_items=998
Read 998 items

C:\Users\Gumm\Documents\Audacity\10ms\ardha chapu\Sample9.wav
frames=1005
samplerate=96000
channels=1
num_items=1005
Read 1005 items

C:\Users\Gumm\Documents\Audacity\10ms\ardha chapu\Sample10.wav
Failed to open the file.

我怎么能解决这个问题?

编辑 - 我通过将我的所有文件重命名为sampleA,sampleB,sampleC等来使其工作,并且我将samplenum作为字符类型从“a”迭代到'u'[a = 1并且u = 21]。但我仍然想知道为什么当我给出数字时它不起作用。

0 个答案:

没有答案