Reputation: 23
I decided to play with C++ on audio algorithm, being more used to Matlab I struggle a bit.
First step is to record a wav file into an array, using sndfile library. I used the examples from the library :
https://github.com/michaelwu/libsndfile/blob/master/examples/sfprocess.c
I want to create a wavF class which contains the general information of the wav (sampling frequency,channels... ) and the data of the wav file for further treatments.
I have almost achieve this but there is a segmentation fault occurring when entering in the while ((readcount = sf_read_double (infile, data, BUFFER_LEN)))
loop (but not always) in the wavfile.cpp. I can't figure out why.
I want to save the wav data in the array double* m_wavData, using the function process_data in the class constructor with a buffer loop.
Here his the codes:
The main:
#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 */
The header:
#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
and the code 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;
}
What coding heresy did I write ? Any advices?
(audio wav found on uiowa.edu database) Thank You.
Upvotes: 0
Views: 586
Reputation: 23
Thanks all !
They were several mistakes:
1) As pointed out by Jean-François Fabre, the buffer size wasn't correct, leading in trying to write data in the array m_wavData
taking value out of the array containing the wav valuesdata
.
---> m_wavData = new double[m_wavsize*m_channels] ;
2) As pointed out by WhozCraig I was re-declaring the variable m_wavData
in the constructor of the class leading in hiding the member variale of the same name. --> erased re-declaration.
3) As pointed out by molbdnilo the parameter infile
add no effect in the way i used it leading in not reading the wav data.
-->worked around the problem by opening in the init and in the constructor.
4) I was continuously writing the same case of the array m_wavData
(size of the buffer).
--> added counter to write in the good cases.
Here is my solution in a more minimal version(πάντα ῥεῖ):
#include <sndfile.h>
#include <stdio.h>
#define BUFFER_LEN 1024
#include <sndfile.hh>
class wavF
{
public:
wavF(SNDFILE *infile, SndfileHandle file,const char * fName);
void prinvalue(int k);// help to see if the values are good
~wavF();
protected:
unsigned int m_wavsize;
unsigned int m_channels;
unsigned int m_samplerate;
const char * m_fileName;
double * m_wavData;
};
int main(void)
{
// initiate variables to open the file
SNDFILE *infile ;
const char *infilename = "test.wav" ;
SndfileHandle file;
file=SndfileHandle(infilename);
SF_INFO sfinfo ;
if (! (infile = sf_open (infilename, SFM_READ, &sfinfo)))
{ /* Open failed so print an error message. */
printf ("Not able to open input file %s.\n", infilename) ;
puts (sf_strerror (NULL)) ;
return 1;
} ;
wavF wav1(infile, file, infilename);
wav1.prinvalue(1000);//test if the value inside of the wav is correct
sf_close (infile);
/* Close input and output files. */
return 0 ;
}
void wavF::prinvalue(int k)
{
printf("the value is %lf",m_wavData[k]);
}
wavF::wavF(SNDFILE *infile, SndfileHandle file,const char * fName):m_channels(file.channels()),m_samplerate(file.samplerate()),m_wavsize(file.frames()),m_fileName(fName)
{ //declaration of the allocated arrays
static double* data= new double [BUFFER_LEN*m_channels] ;
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 ("") ;
int readcount(0);//number of data in the buffer
int counter(0);// number of time buffer is used
while ((readcount = sf_read_double (infile, data, BUFFER_LEN*m_channels)))
{
int k;
for (k = 0; k < readcount ; k ++)
{
m_wavData[counter*readcount+k] = data[k] ;//put data in the array
}
counter++;
printf("the wav value is %lf \n",m_wavData[counter*readcount-1]);//look if the stored values are good
} ;
delete[] data; //clear allocated buffer array
} /* read_file */
wavF::~wavF()
{
delete[] m_wavData;
}
Upvotes: 1