程序集之·一(初步结果)
Main
#include <stdio.h> #include <stdlib.h> #include "wavIo.h" //#include "PreAccent.h" //#include "HammingWindow.h" //#include "Enframe.h" #define factor 0.97 void main() { char* rawfile = "MF.wav"; //len=Getlength(rawfile); double sample[65000]; int len; len=Read(rawfile,sample); /*frame*/ int i=0,j=0; double frames[600][512]; int winsize=512; int hopsize=128; int framenum=(int)((len-winsize)/hopsize)+1; //frames = new double [framenum][512]; for(int i=0; i<framenum; i++) { for(int j=0; j<winsize; j++) { frames[i][j] = sample[i*hopsize+j]; } } for(int i=0;i<100;i++) printf("the frames[][] is %f\n",frames[i][j]); /*Pre-cent*/ //PreAccent(sample,len,factor );//欲加重,factor 为预加重因子 /*Enframe*/ }
wavIo.h
#ifndef ML_WAVFILE #define ML_WAVFILE //#include <afx.h> //#include "RealVec.h" #include <stdio.h> int Read(char *filename,double *sample); // int Getlength(char *filename); #endif
wavIo.cpp
#include "WavIo.h" #include "stdio.h" #ifndef MAXWORD #define MAXWORD 0xFFFF #endif typedef unsigned int uint; typedef unsigned long dword; typedef unsigned short word; int Read(char *filename,double *s) { FILE *f = fopen(filename, "rb"); if((f=fopen(filename,"rb"))==NULL) printf("open file failed!\n"); int size, format, samp_freq, num_samp_bits, NumSamples; dword dw; word w; fread(&dw, sizeof(dword), 1, f); if (dw != 0x46464952) /* RIFF*/ return false; //else //printf("the RIFF is %c;\n",dw); fread(&dw, sizeof(dword), 1, f); fread( &dw, sizeof(dword), 1, f); if (dw != 0x45564157) /* WAVE */ return false; //else //printf("the WAVE is %c;\n",dw); fread(&dw, sizeof(dword), 1, f); if (dw != 0x20746d66) /* fmt */ return false; fread( &dw, sizeof(dword), 1, f); /* format */ fread(&w, sizeof(word), 1, f); if (w != 7 && w != 1) return false; format=w; /* read the number of channels */ fread( &w, sizeof(word), 1, f); if (w != 1) return false; else printf("the channel is:%d\n",w); fread( &dw, sizeof(dword), 1, f); samp_freq=dw; printf("the samp_freq is:%d\n",dw); /* Skip a few bytes */ fread( &dw, sizeof(dword), 1, f); fread( &w, sizeof(word), 1, f); fread( &w, sizeof(word), 1, f); if (w!=16) return false; num_samp_bits=w; printf("the samp_bits is:%d\n",w); if ((num_samp_bits == 8 && (format != 7 && format != 6)) || (num_samp_bits == 16 && format != 1)) return false; fread( &dw, sizeof(dword), 1, f); if (dw != 0x61746164) /* data */ return false; fread( &dw, sizeof(dword), 1, f); size=dw; if (size == -1) NumSamples = -1; else { if (format == 6 || format == 7) NumSamples = size; else if (format == 1) NumSamples = size / 2; printf("the NumSamples is:%d\n",NumSamples); } signed short *buf; int numread; int i=0; //s.Resize(NumSamples); buf = new signed short [NumSamples]; numread = fread( buf, sizeof(signed short), NumSamples, f ); if (numread) { for (int i=0 ; i<NumSamples ; ++i) { s[i] = ( (double) buf[i] ) / MAXWORD; } } delete buf; fclose(f); return NumSamples; }