wavefile.cpp
Go to the documentation of this file.
00001 #include "wavefile.h"
00002 
00003 typedef struct {
00004   char           chunkID[4];
00005   long           chunkSize;
00006   short          formatTag;
00007   unsigned short channels;
00008   unsigned int   samplesPerSec;
00009   unsigned int   avgBytesPerSec;
00010   unsigned short blockAlign;
00011   unsigned short bitsPerSample;
00012 } FormatChunk;
00013 
00014 typedef struct {
00015   char           chunkID[4];
00016   unsigned int   chunkSize;
00017 } DataChunk;
00018 
00019 typedef struct {
00020   char         riff[4];
00021   unsigned int size;
00022   char         wave[4];
00023   FormatChunk  format;
00024   DataChunk    data;
00025 } WavHeader;
00026 
00027 #define WAV_STRING_RIFF "RIFF"
00028 #define WAV_STRING_WAVE "WAVE"
00029 #define WAV_STRING_FMT  "fmt "
00030 #define WAV_STRING_DATA "data"
00031 
00032 int WaveFile::SAMPLING_RATE = 16000;
00033 int WaveFile::SAMPLE_BYTE = 2;
00034 
00035 WaveFile::WaveFile()
00036 {
00037 }
00038 
00039 QFile* WaveFile::createHeader(const QString &filename)
00040 {
00041     QFile *file = new QFile(filename);
00042     if (file->open(QIODevice::WriteOnly)){
00043         file->seek(sizeof(WavHeader));
00044     }
00045     else{
00046         delete file;
00047         file = NULL;
00048     }
00049 
00050     return file;
00051 }
00052 
00053 void WaveFile::fillHeader(QFile *file, int sampleSize)
00054 {
00055     file->reset();
00056 
00057     WavHeader header;
00058 
00059     memcpy(header.riff, WAV_STRING_RIFF, strlen(WAV_STRING_RIFF));
00060     header.size = sampleSize + sizeof(WavHeader) - 8; // filesize - 8
00061     memcpy(header.wave, WAV_STRING_WAVE, strlen(WAV_STRING_WAVE));
00062 
00063     // FormatChunk
00064     memcpy(header.format.chunkID, WAV_STRING_FMT, strlen(WAV_STRING_FMT));
00065     header.format.chunkSize = 16;
00066     header.format.formatTag = 1;
00067     header.format.channels = 1;
00068     header.format.samplesPerSec = SAMPLING_RATE;
00069     header.format.avgBytesPerSec = header.format.samplesPerSec * SAMPLE_BYTE * header.format.channels;
00070     header.format.blockAlign = SAMPLE_BYTE * header.format.channels;
00071     header.format.bitsPerSample = SAMPLE_BYTE * 8;
00072 
00073     // DataChunk
00074     memcpy(header.data.chunkID, WAV_STRING_DATA, strlen(WAV_STRING_DATA));
00075     header.data.chunkSize = sampleSize;
00076 
00077     file->write((const char*)&header, sizeof(header));
00078 }


rospeex_audiomonitor
Author(s): Komei Sugiura
autogenerated on Wed Aug 26 2015 16:10:37