00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00024 #ifndef _SMS_H
00025 #define _SMS_H
00026
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <math.h>
00030 #include <memory.h>
00031 #include <strings.h>
00032 #include <sndfile.h>
00033
00034 #define SMS_VERSION 1.1
00036 #define SMS_MAX_NPEAKS 400
00038 #ifdef DOUBLE_PRECISION
00039 #define sfloat double
00040 #else
00041 #define sfloat float
00042 #endif
00043
00064 typedef struct
00065 {
00066 int iSmsMagic;
00067 int iHeadBSize;
00068 int nFrames;
00069 int iFrameBSize;
00070 int iSamplingRate;
00071 int iFormat;
00072 int nTracks;
00073 int iFrameRate;
00074 int iStochasticType;
00075 int nStochasticCoeff;
00076 int iEnvType;
00077 int nEnvCoeff;
00078 int iMaxFreq;
00079
00080
00081
00082
00083 sfloat fResidualPerc;
00084 int nTextCharacters;
00085 char *pChTextCharacters;
00086 } SMS_Header;
00087
00091 typedef struct {
00092 int nSamples;
00093 int iSamplingRate;
00094 int channelCount;
00095 int iReadChannel;
00096 int sizeHeader;
00097 } SMS_SndHeader;
00098
00111 typedef struct
00112 {
00113 sfloat *pSmsData;
00114 int sizeData;
00115 sfloat *pFSinFreq;
00116 sfloat *pFSinAmp;
00117 sfloat *pFSinPha;
00118 int nTracks;
00119 sfloat *pFStocGain;
00120 int nCoeff;
00121 sfloat *pFStocCoeff;
00122 sfloat *pResPhase;
00123 int nEnvCoeff;
00124 sfloat *pSpecEnv;
00125 } SMS_Data;
00126
00127
00136 typedef struct
00137 {
00138 sfloat *pFBuffer;
00139 int sizeBuffer;
00140 int iMarker;
00141 int iFirstGood;
00142 } SMS_SndBuffer;
00143
00148
00149 typedef struct
00150 {
00151 sfloat fFreq;
00152 sfloat fMag;
00153 sfloat fPhase;
00154 } SMS_Peak;
00155
00162 typedef struct
00163 {
00164 int iFrameSample;
00165 int iFrameSize;
00166 int iFrameNum;
00167 SMS_Peak *pSpectralPeaks;
00168 int nPeaks;
00169 sfloat fFundamental;
00170 SMS_Data deterministic;
00171 int iStatus;
00173 } SMS_AnalFrame;
00174
00179 typedef struct
00180 {
00181 sfloat fLowestFreq;
00182 sfloat fHighestFreq;
00183 sfloat fMinPeakMag;
00184 int iSamplingRate;
00185 int iMaxPeaks;
00186 int nPeaksFound;
00187 sfloat fHighestFundamental;
00188 int iRefHarmonic;
00189 sfloat fMinRefHarmMag;
00190 sfloat fRefHarmMagDiffFromMax;
00191 int iSoundType;
00192 } SMS_PeakParams;
00193
00198 typedef struct
00199 {
00200 int iType;
00201 int iOrder;
00202 int iMaxFreq;
00203 sfloat fLambda;
00204 int nCoeff;
00205 int iAnchor;
00206 } SMS_SEnvParams;
00207
00208
00209
00224 typedef struct
00225 {
00226 int iDebugMode;
00227 int iFormat;
00228 int iSoundType;
00229 int iStochasticType;
00230 int iFrameRate;
00231 int nStochasticCoeff;
00232 sfloat fLowestFundamental;
00233 sfloat fHighestFundamental;
00234 sfloat fDefaultFundamental;
00235 sfloat fPeakContToGuide;
00236 sfloat fFundContToGuide;
00237 sfloat fFreqDeviation;
00238 int iSamplingRate;
00239 int iDefaultSizeWindow;
00240 int sizeHop;
00241 sfloat fSizeWindow;
00242 int nTracks;
00243 int nGuides;
00244 int iCleanTracks;
00245
00246 sfloat fMinRefHarmMag;
00247 sfloat fRefHarmMagDiffFromMax;
00248 int iRefHarmonic;
00249 int iMinTrackLength;
00250 int iMaxSleepingTime;
00251 sfloat fHighestFreq;
00252 sfloat fMinPeakMag;
00253 int iAnalysisDirection;
00254 int iSizeSound;
00255 int nFrames;
00256 int iWindowType;
00257 int iMaxDelayFrames;
00258 sfloat fResidualAccumPerc;
00259 int sizeNextRead;
00260 SMS_PeakParams peakParams;
00261 SMS_Data prevFrame;
00262 SMS_SEnvParams specEnvParams;
00263 SMS_SndBuffer soundBuffer;
00264 SMS_SndBuffer synthBuffer;
00265 SMS_AnalFrame *pFrames;
00266 SMS_AnalFrame **ppFrames;
00267 } SMS_AnalParams;
00268
00273 typedef struct
00274 {
00275 int ready;
00276 int maxFreq;
00277 int doResGain;
00278 sfloat resGain;
00279 int doTranspose;
00280 sfloat transpose;
00281 int doSinEnv;
00282 sfloat sinEnvInterp;
00283 int sizeSinEnv;
00284 sfloat *sinEnv;
00285 int doResEnv;
00286 sfloat resEnvInterp;
00287 int sizeResEnv;
00288 sfloat *resEnv;
00289 } SMS_ModifyParams;
00290
00299 typedef struct
00300 {
00301 int iStochasticType;
00303 int iSynthesisType;
00304 int iDetSynthType;
00306 int iOriginalSRate;
00307 int iSamplingRate;
00308 int sizeHop;
00309 int origSizeHop;
00310 sfloat *pFDetWindow;
00311 sfloat *pFStocWindow;
00312 sfloat *pSynthBuff;
00313 sfloat *pMagBuff;
00314 sfloat *pPhaseBuff;
00315 sfloat *pSpectra;
00316 SMS_Data prevFrame;
00317 SMS_ModifyParams modParams;
00318 } SMS_SynthParams;
00319
00327 typedef struct
00328 {
00329 sfloat fFreq;
00330 sfloat fMag;
00331 sfloat fMagPerc;
00332 sfloat fFreqDev;
00333 sfloat fHarmRatio;
00334 } SMS_HarmCandidate;
00335
00342 typedef struct
00343 {
00344 sfloat fFreqDev;
00345 sfloat fMagDev;
00346 int iPeak;
00347 } SMS_ContCandidate;
00348
00357 typedef struct
00358 {
00359 sfloat fFreq;
00360 sfloat fMag;
00361 int iStatus;
00362 int iPeakChosen;
00363 } SMS_Guide;
00364
00373 enum SMS_Format
00374 {
00375 SMS_FORMAT_H,
00376 SMS_FORMAT_IH,
00377 SMS_FORMAT_HP,
00378 SMS_FORMAT_IHP
00379 };
00380
00388 enum SMS_SynthType
00389 {
00390 SMS_STYPE_ALL,
00391 SMS_STYPE_DET,
00392 SMS_STYPE_STOC
00393 };
00394
00404 enum SMS_DetSynthType
00405 {
00406 SMS_DET_IFFT,
00407 SMS_DET_SIN
00408 };
00409
00431 enum SMS_StocSynthType
00432 {
00433 SMS_STOC_NONE,
00434 SMS_STOC_APPROX,
00435 SMS_STOC_IFFT
00436 };
00437
00447 enum SMS_SpecEnvType
00448 {
00449 SMS_ENV_NONE,
00450 SMS_ENV_CEP,
00451 SMS_ENV_FBINS
00452 };
00453
00454
00456
00457 enum SMS_ERRORS
00458 {
00459 SMS_OK,
00460 SMS_NOPEN,
00461 SMS_NSMS ,
00462 SMS_MALLOC,
00463 SMS_RDERR,
00464 SMS_WRERR,
00465 SMS_SNDERR
00466 };
00467
00472 enum SMS_DBG
00473 {
00474 SMS_DBG_NONE,
00475 SMS_DBG_DET,
00476 SMS_DBG_PEAK_DET,
00477 SMS_DBG_HARM_DET,
00478 SMS_DBG_PEAK_CONT,
00479 SMS_DBG_CLEAN_TRAJ,
00480 SMS_DBG_SINE_SYNTH,
00481 SMS_DBG_STOC_ANAL,
00482 SMS_DBG_STOC_SYNTH,
00483 SMS_DBG_SMS_ANAL,
00484 SMS_DBG_ALL,
00485 SMS_DBG_RESIDUAL,
00486 SMS_DBG_SYNC,
00488 };
00489
00490 #define SMS_MAX_WINDOW 8190
00492
00493
00494
00495
00496 enum SMS_SOUND_TYPE
00497 {
00498 SMS_SOUND_TYPE_MELODY,
00499 SMS_SOUND_TYPE_NOTE
00500 };
00501
00502
00503
00504
00505
00506
00507
00508 enum SMS_DIRECTION
00509 {
00510 SMS_DIR_FWD,
00511 SMS_DIR_REV
00512 };
00513
00514
00515
00516 enum SMS_WINDOWS
00517 {
00518 SMS_WIN_HAMMING,
00519 SMS_WIN_BH_62,
00520 SMS_WIN_BH_70,
00521 SMS_WIN_BH_74,
00522 SMS_WIN_BH_92,
00523 SMS_WIN_HANNING,
00524 SMS_WIN_IFFT
00526 };
00527
00528
00529
00530 #define SMS_MIN_GOOD_FRAMES 3
00532 #define SMS_MAX_DEVIATION .01
00534 #define SMS_ANAL_DELAY 100
00536 #define SMS_DELAY_FRAMES (SMS_MIN_GOOD_FRAMES + SMS_ANAL_DELAY)
00537
00541 enum SMS_FRAME_STATUS
00542 {
00543 SMS_FRAME_EMPTY,
00544 SMS_FRAME_READY,
00545 SMS_FRAME_PEAKS_FOUND,
00546 SMS_FRAME_FUND_FOUND,
00547 SMS_FRAME_TRAJ_FOUND,
00548 SMS_FRAME_CLEANED,
00549 SMS_FRAME_RECOMPUTED,
00550 SMS_FRAME_DETER_SYNTH,
00551 SMS_FRAME_STOC_COMPUTED,
00552 SMS_FRAME_DONE,
00553 SMS_FRAME_END
00554 };
00555
00556
00557 #define SMS_MIN_SIZE_FRAME 128
00558
00564 #define PI 3.141592653589793238462643
00565 #define TWO_PI 6.28318530717958647692
00566 #define INV_TWO_PI (1 / TWO_PI)
00567 #define PI_2 1.57079632679489661923
00568 #define LOG2 0.69314718055994529
00569 #define LOG10 2.3025850929940459
00570 #define EXP 2.7182818284590451
00572 sfloat sms_magToDB(sfloat x);
00573 sfloat sms_dBToMag(sfloat x);
00574 void sms_arrayMagToDB(int sizeArray, sfloat *pArray);
00575 void sms_arrayDBToMag(int sizeArray, sfloat *pArray);
00576 void sms_setMagThresh(sfloat x);
00577 sfloat sms_rms ( int sizeArray, sfloat *pArray );
00578 sfloat sms_sine (sfloat fTheta);
00579 sfloat sms_sinc (sfloat fTheta);
00580 sfloat sms_random ( void );
00581 int sms_power2(int n);
00582
00583
00584
00586
00587 sfloat sms_scalarTempered( float x);
00588 void sms_arrayScalarTempered( int sizeArray, sfloat *pArray);
00589
00590 #ifndef MAX
00591
00592 #define MAX(a,b) ((a) > (b) ? (a) : (b))
00593 #endif
00594 #ifndef MIN
00595
00596 #define MIN(a,b) ((a) < (b) ? (a) : (b))
00597 #endif
00598
00600
00601 int sms_analyze (int sizeWaveform, sfloat *pWaveform, SMS_Data *pSmsFrame,
00602 SMS_AnalParams *pAnalParams);
00603
00604 void sms_analyzeFrame (int iCurrentFrame, SMS_AnalParams *pAnalParams, sfloat fRefFundamental );
00605
00606 int sms_init( void );
00607
00608 void sms_free( void );
00609
00610 int sms_initAnalysis ( SMS_AnalParams *pAnalParams, SMS_SndHeader *pSoundHeader);
00611
00612 void sms_initAnalParams (SMS_AnalParams *pAnalParams);
00613
00614 void sms_initSynthParams(SMS_SynthParams *synthParams);
00615
00616 int sms_initSynth( SMS_Header *pSmsHeader, SMS_SynthParams *pSynthParams );
00617
00618 int sms_changeSynthHop( SMS_SynthParams *pSynthParams, int sizeHop);
00619
00620 void sms_freeAnalysis (SMS_AnalParams *pAnalParams);
00621
00622 void sms_freeSynth( SMS_SynthParams *pSynthParams );
00623
00624 void sms_fillSoundBuffer (int sizeWaveform, sfloat *pWaveform, SMS_AnalParams *pAnalParams);
00625
00626 void sms_windowCentered (int sizeWindow, sfloat *pWaveform, float *pWindow, int sizeFft, float *pFftBuffer);
00627
00628 void sms_getWindow (int sizeWindow, sfloat *pWindow, int iWindowType);
00629
00630 void sms_scaleWindow (int sizeWindow, sfloat *pWindow);
00631
00632 int sms_spectrum (int sizeWindow, sfloat *pWaveform, float *pWindow, int sizeMag,
00633 sfloat *pMag, float *pPhase);
00634
00635 int sms_invSpectrum (int sizeWaveform, sfloat *pWaveform, float *pWindow ,
00636 int sizeMag, sfloat *pMag, float *pPhase);
00637
00638
00639 int sms_invQuickSpectrumW (sfloat *pFMagSpectrum, float *pFPhaseSpectrum,
00640 int sizeFft, sfloat *pFWaveform, int sizeWave,
00641 sfloat *pFWindow);
00642
00643 int sms_spectralApprox (sfloat *pSpec1, int sizeSpec1, int sizeSpec1Used,
00644 sfloat *pSpec2, int sizeSpec2, int nCoefficients);
00645
00646 int sms_spectrumMag (int sizeWindow, sfloat *pWaveform, float *pWindow,
00647 int sizeMag, sfloat *pMag);
00648
00649 void sms_dCepstrum( int sizeCepstrum, sfloat *pCepstrum, int sizeFreq, float *pFreq, float *pMag,
00650 sfloat fLambda, int iSamplingRate);
00651
00652 void sms_dCepstrumEnvelope (int sizeCepstrum, sfloat *pCepstrum, int sizeEnv, float *pEnv);
00653
00654 void sms_spectralEnvelope ( SMS_Data *pSmsData, SMS_SEnvParams *pSpecEnvParams);
00655
00656 int sms_sizeNextWindow (int iCurrentFrame, SMS_AnalParams *pAnalParams);
00657
00658 sfloat sms_fundDeviation (SMS_AnalParams *pAnalParams, int iCurrentFrame);
00659
00660 int sms_detectPeaks (int sizeSpec, sfloat *pFMag, float *pPhase,
00661 SMS_Peak *pSpectralPeaks, SMS_PeakParams *pPeakParams);
00662
00663 void sms_harmDetection (SMS_AnalFrame *pFrame, sfloat fRefFundamental,
00664 SMS_PeakParams *pPeakParams);
00665
00666 int sms_peakContinuation (int iFrame, SMS_AnalParams *pAnalParams);
00667
00668 sfloat sms_preEmphasis (float fInput);
00669
00670 sfloat sms_deEmphasis (float fInput);
00671
00672 void sms_cleanTracks (int iCurrentFrame, SMS_AnalParams *pAnalParams);
00673
00674 void sms_scaleDet (sfloat *pSynthBuffer, float *pOriginalBuffer,
00675 sfloat *pSinAmp, SMS_AnalParams *pAnalParams, int nTracks);
00676
00677 int sms_prepSine (int nTableSize);
00678
00679 int sms_prepSinc (int nTableSize);
00680
00681 void sms_clearSine( void );
00682
00683 void sms_clearSinc( void );
00684
00685 void sms_synthesize (SMS_Data *pSmsFrame, sfloat*pSynthesis,
00686 SMS_SynthParams *pSynthParams);
00687
00688 void sms_sineSynthFrame (SMS_Data *pSmsFrame, sfloat *pBuffer,
00689 int sizeBuffer, SMS_Data *pLastFrame,
00690 int iSamplingRate);
00691
00692 void sms_initHeader (SMS_Header *pSmsHeader);
00693
00694 int sms_getHeader (char *pChFileName, SMS_Header **ppSmsHeader,
00695 FILE **ppInputFile);
00696
00697 void sms_fillHeader (SMS_Header *pSmsHeader, SMS_AnalParams *pAnalParams,
00698 char *pProgramString);
00699
00700 int sms_writeHeader (char *pFileName, SMS_Header *pSmsHeader,
00701 FILE **ppOutSmsFile);
00702
00703 int sms_writeFile (FILE *pSmsFile, SMS_Header *pSmsHeader);
00704
00705 int sms_initFrame (int iCurrentFrame, SMS_AnalParams *pAnalParams,
00706 int sizeWindow);
00707
00708 int sms_allocFrame (SMS_Data *pSmsFrame, int nTracks, int nCoeff,
00709 int iPhase, int stochType, int nEnvCoeff);
00710
00711 int sms_allocFrameH (SMS_Header *pSmsHeader, SMS_Data *pSmsFrame);
00712
00713 int sms_getFrame (FILE *pInputFile, SMS_Header *pSmsHeader, int iFrame,
00714 SMS_Data *pSmsFrame);
00715
00716 int sms_writeFrame (FILE *pSmsFile, SMS_Header *pSmsHeader,
00717 SMS_Data *pSmsFrame);
00718
00719 void sms_freeFrame (SMS_Data *pSmsFrame);
00720
00721 void sms_clearFrame (SMS_Data *pSmsFrame);
00722
00723 void sms_copyFrame (SMS_Data *pCopySmsFrame, SMS_Data *pOriginalSmsFrame);
00724
00725 int sms_frameSizeB (SMS_Header *pSmsHeader);
00726
00727 int sms_residual ( int sizeWindow, sfloat *pSynthesis, float *pOriginal, float *pResidual, float *pWindow);
00728
00729 void sms_filterHighPass ( int sizeResidual, sfloat *pResidual, int iSamplingRate);
00730
00731 int sms_stocAnalysis ( int sizeWindow, sfloat *pResidual, float *pWindow,
00732 SMS_Data *pSmsFrame);
00733
00734 void sms_interpolateFrames (SMS_Data *pSmsFrame1, SMS_Data *pSmsFrame2,
00735 SMS_Data *pSmsFrameOut, sfloat fInterpFactor);
00736
00737 int sms_openSF (char *pChInputSoundFile, SMS_SndHeader *pSoundHeader);
00738
00739 void sms_closeSF();
00740
00741 int sms_getSound (SMS_SndHeader *pSoundHeader, long sizeSound, sfloat *pSound,
00742 long offset);
00743
00744 int sms_createSF (char *pChOutputSoundFile, int iSamplingRate, int iType);
00745
00746 void sms_writeSound (sfloat *pBuffer, int sizeBuffer);
00747
00748 void sms_writeSF ( void );
00749
00750 void sms_fft(int sizeFft, sfloat *pArray);
00751
00752 void sms_ifft(int sizeFft, sfloat *pArray);
00753
00754 void sms_RectToPolar( int sizeSpec, sfloat *pReal, float *pMag, float *pPhase);
00755
00756 void sms_PolarToRect( int sizeSpec, sfloat *pReal, float *pMag, float *pPhase);
00757
00758 void sms_spectrumRMS( int sizeMag, sfloat *pReal, float *pMag);
00759
00760 void sms_initModify(SMS_Header *header, SMS_ModifyParams *params);
00761
00762 void sms_initModifyParams(SMS_ModifyParams *params);
00763
00764 void sms_freeModify(SMS_ModifyParams *params);
00765
00766 void sms_modify(SMS_Data *frame, SMS_ModifyParams *params);
00767
00768
00769
00770 int sms_createResSF (int iSamplingRate);
00771
00772 int sms_writeResSound (sfloat *pBuffer, int sizeBuffer);
00773
00774 void sms_writeResSF ( void );
00775
00776 int sms_createDebugFile (SMS_AnalParams *pAnalParams);
00777
00778 void sms_writeDebugData (sfloat *pBuffer1, float *pBuffer2,
00779 sfloat *pBuffer3, int sizeBuffer);
00780
00781 void sms_writeDebugFile ( void );
00782
00783 void sms_error( char *pErrorMessage );
00784
00785 int sms_errorCheck( void );
00786
00787 char* sms_errorString( void );
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811 #endif
00812