/*************************************************************************** * Copyright (C) 2004 by Johan Maes - ON4QZ * * on4qz@telenet.be * * http://users.telenet.be/on4qz * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "wavio.h" #include #include "configdialog.h" #include "utils.h" //#include "qfilterdesignglobal.h" wavIO::wavIO(unsigned int samplingR) { dataPtr=NULL; dblDataPtr=NULL; dataIndex=0; samplingrate=samplingR; } wavIO::~wavIO() { if(dataPtr) { delete dataPtr; } } void wavIO::initHeader() { waveHeader.chunkID[0]='R'; waveHeader.chunkID[1]='I'; waveHeader.chunkID[2]='F'; waveHeader.chunkID[3]='F'; waveHeader.format[0]='W'; waveHeader.format[1]='A'; waveHeader.format[2]='V'; waveHeader.format[3]='E'; waveHeader.subChunk1ID[0]='f'; waveHeader.subChunk1ID[1]='m'; waveHeader.subChunk1ID[2]='t'; waveHeader.subChunk1ID[3]=' '; waveHeader.subChunk2ID[0]='d'; waveHeader.subChunk2ID[1]='a'; waveHeader.subChunk2ID[2]='t'; waveHeader.subChunk2ID[3]='a'; waveHeader.subChunk1Size=16; // always 16 for PCM waveHeader.audioFormat=1; // PCM waveHeader.numChannels=1; // Mono waveHeader.sampleRate=samplingrate; waveHeader.byteRate=sizeof(short int)*samplingrate; // 16 bit samples waveHeader.blockAlign=2; waveHeader.bitsPerSample=16; waveHeader.chunkSize=36+numberOfSamples*sizeof(short int); waveHeader.subChunk2Size=numberOfSamples*sizeof(short int); } bool wavIO::readFile(QString fname,bool ask) { QString tmp; if (ask) { inopf.setName(QFileDialog::getOpenFileName(audioPath,"*.wav")); } else { inopf.setName(fname); } if(!inopf.open(IO_ReadOnly)) { return FALSE; } if(inopf.readBlock(&waveHeader.chunkID[0],sizeof(sWave))!=sizeof(sWave)) { inopf.close(); return FALSE; } // check the header if( (!checkString(waveHeader.chunkID,"RIFF")) ||(!checkString(waveHeader.format,"WAVE")) ||(!checkString(waveHeader.subChunk1ID,"fmt ")) ||(!checkString(waveHeader.subChunk2ID,"data"))) { logfile.add("wavio read header error"); inopf.close(); return FALSE; } if( (waveHeader.subChunk1Size!=16) ||(waveHeader.audioFormat!=1) ||(waveHeader.numChannels!=1) ||(waveHeader.sampleRate!=samplingrate) ||(waveHeader.byteRate!=sizeof(short int)*samplingrate) ||(waveHeader.blockAlign!=2) ||(waveHeader.bitsPerSample!=16)) { logfile.add("wavio read header error, not supported"); inopf.close(); return FALSE; } numberOfSamples=waveHeader.subChunk2Size/sizeof(short int); dataPtr=new short int [numberOfSamples]; if(inopf.readBlock((char *)dataPtr,waveHeader.subChunk2Size)!=waveHeader.subChunk2Size) { inopf.close(); return FALSE; } dataIndex=0; inopf.close(); return TRUE; } double *wavIO::toDouble() { unsigned int i; if(dblDataPtr!=NULL) delete [] dblDataPtr; dblDataPtr=new double[numberOfSamples]; for(i=0;i