分段错误和wav文件阅读器

时间:2016-10-13 08:10:02

标签: c++ audio wav

我决定在音频算法上使用C ++,更习惯于Matlab我有点挣扎。

第一步是使用sndfile库将wav文件记录到数组中。 我使用了库中的例子:

https://github.com/michaelwu/libsndfile/blob/master/examples/sfprocess.c

我想创建一个wavF类,其中包含wav的一般信息(采样频率,通道...)和wav文件的数据,以便进一步处理。

我几乎已经实现了这一点但是在wavfile.cpp中进入while ((readcount = sf_read_double (infile, data, BUFFER_LEN)))循环(但并非总是)时发生了分段错误。我无法弄清楚原因。

我想使用带有缓冲区循环的类构造函数中的process_data函数将wav数据保存在数组double * m_wavData中。

这是他的代码:

主要:

#include    <sndfile.h>
#include    <stdio.h>
#include    "wavfile.h"


int main (void)
{
    SNDFILE      *infile ;
    const char  *infilename = "Piano_mf_Gb2.wav" ;
    SndfileHandle file;
    file=SndfileHandle(infilename);

    int test(initwav(infile, file, infilename));
    if(test==1) return 0 ;

    wavF wav1(infile, file, infilename);

    sf_close (infile) ;
    /* Close input and output files. */


    return 0 ;
} /* main */

标题:

    #ifndef WAVFILE_H_INCLUDED
#define WAVFILE_H_INCLUDED

#define BUFFER_LEN  1024
#include    <sndfile.hh>

class wavF
{
    public:
    wavF(SNDFILE  *infile, SndfileHandle file,const char * fName);
    wavF(const unsigned int wavsize,const unsigned int channels,const unsigned int samplerate,const char * fileName);
    ~wavF();

    protected:
         const unsigned int m_wavsize;
         const unsigned int m_channels;
         const unsigned int m_samplerate;

        const char * m_fileName;
        double * m_wavData;

};
static void process_data (double *wavData, double * data, int count, int channels);
int initwav(SNDFILE  *infile,SndfileHandle file,const char * fName);
#endif // WAVFILE_H_INCLUDED

和代码wavfile.cpp

   #include "wavfile.h"

wavF::wavF(SNDFILE  *infile, SndfileHandle file,const char * fName): m_channels(file.channels()),m_samplerate(file.samplerate()),m_wavsize(file.frames()),m_fileName(fName)
{
     /* This is a buffer of double precision floating point values
    ** which will hold our data while we process it.
    */
    static double data [BUFFER_LEN] ;
    //initialise class array
    double* m_wavData = new double[m_wavsize*m_channels] ;


    printf ("Opened file '%s'\n", m_fileName) ;
    printf ("Sample rate : %d\n", m_samplerate) ;
    printf ("Channels    : %d\n", m_channels) ;
    printf ("Size    : %d\n", m_wavsize) ;

    puts ("") ;

    //fill array with the wav channel
    int readcount(0);
    int counter(0);
    while ((readcount = sf_read_double (infile, data, BUFFER_LEN)))
    {
     //  printf("%i \n",counter);
      // counter++;
       process_data (m_wavData,data, readcount, m_channels) ;
    } ;

  /* RAII takes care of destroying SndfileHandle object. */
} /* read_file */

wavF::wavF(const unsigned int wavsize,const unsigned int channels,const unsigned int samplerate,const char * fileName):
m_channels(channels),m_samplerate(samplerate),m_wavsize(wavsize),m_fileName(fileName)
{
}

static void process_data (double *m_wavData,double *data, int count, int channels)
{
    int k;

    /* Process the data here.
    ** If the soundfile contains more then 1 channel you need to take care of
    ** the data interleaving youself.
    */

    for (k = 0 ; k < channels*count ; k ++)
            m_wavData [k] = data[k] ;
} /* process_data */

wavF::~wavF()
{
    delete  m_wavData;
    printf("array is detroyed");
}

int initwav(SNDFILE  *infile,SndfileHandle file, const char * fName)
{

    SF_INFO     sfinfo ;
        if (! (infile = sf_open (fName, SFM_READ, &sfinfo)))
    {   /* Open failed so print an error message. */
        printf ("Not able to open input file %s.\n", fName) ;
        puts (sf_strerror (NULL)) ;
        return 1;
    } ;
    return 0;
}

我写了什么编码异端?有什么建议吗?

(在uiowa.edu数据库中找到音频wav) 谢谢。

1 个答案:

答案 0 :(得分:1)

谢谢大家! 他们有几个错误: 1)正如Jean-FrançoisFabre所指出的,缓冲区大小不正确,导致尝试在数组<blockquote class="blockquote-reverse"> <p>Angular2 have a simplified functionality</p> <footer>This is what we can<cite title="Source Title">Anirudha Das</cite></footer> </blockquote> 中写入数据,从包含wav值m_wavData的数组中取值。 ---&GT; data

2)正如WhozCraig所指出的,我在类的构造函数中重新声明变量m_wavData = new double[m_wavsize*m_channels] ;,导致隐藏同名的成员变量。 - &GT;删除了重新申报。

3)正如molbdnilo指出的那样,参数m_wavData对我使用它的方式没有任何影响,导致不读取wav数据。 - &gt;通过在init和构造函数中打开来解决问题。

4)我一直在编写数组infile的相同大小写(缓冲区的大小)。 - &GT;在好的案例中加入反写作。

这是我在更小版本(πάνταῥεῖ)中的解决方案:

m_wavData