This is sms.h in view mode; [Download] [Up]
/*
* header file for all the SMS programs
*
*/
#ifndef SMSHEADER
#define SMSHEADER
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <memory.h>
#include <strings.h>
#include "SMSSound.h"
/* structure for header of SMS file */
typedef struct
{
/* fix part */
int iSmsMagic; /* magic number for SMS data file */
int iHeadBSize; /* size in bytes of header */
int nRecords; /* number of data records */
int iRecordBSize; /* size in bytes of data record */
int iFormat; /* type of data format */
int iFrameRate; /* rate in Hz of data records */
int iStochasticType; /* representation of stochastic coefficients */
int nTrajectories; /* number of trajectoires in each record */
int nStochasticCoeff; /* number of stochastic coefficients in each
record */
float fAmplitude; /* average amplitude of represented sound */
float fFrequency; /* average fundamental frequency */
float fOriginalSRate; /* sampling rate of original sound */
int iBegSteadyState; /* record number of begining of steady state */
int iEndSteadyState; /* record number of end of steady state */
float fResidualPerc; /* percentage of the residual with respect to the
original */
int nLoopRecords; /* number of loop records specified */
int nSpecEnvelopePoints; /* number of breakpoints in spectral envelope */
int nTextCharacters; /* number of text characters */
/* variable part */
int *pILoopRecords; /* array of record numbers of loop points */
float *pFSpectralEnvelope; /* spectral envelope of partials */
char *pChTextCharacters; /* Textual information relating to the sound */
char *pChDataRecords; /* pointer to data records */
} SMSHeader;
#define SMS_MAGIC 767
/* for iFormat */
#define FORMAT_HARMONIC 1
#define FORMAT_INHARMONIC 2
#define FORMAT_HARMONIC_WITH_PHASE 3
#define FORMAT_INHARMONIC_WITH_PHASE 4
/* for iStochasticType */
#define STOC_FILTER_TYPE 1
#define STOC_MAG_ENV_TYPE 2
#define STOC_NONE 3
/* structure with SMS data */
typedef struct
{
float *pFFreqTraj; /* frequency of sinusoids */
float *pFMagTraj; /* magnitude of sinusoids */
float *pFPhaTraj; /* phase of sinusoids */
int nTraj; /* number of sinusoids */
float *pFStocGain; /* gain of stochastic component */
float *pFStocCoeff; /* filter coefficients for stochastic component */
int nCoeff; /* number of filter coefficients */
} SMS_DATA;
/* useful macros */
/* Error codes returned by SMS file functions */
#define SMS_OK 0 /* no error*/
#define SMS_NOPEN -1 /* couldn't open file */
#define SMS_NSMS -2 /* not a SMS file */
#define SMS_MALLOC -3 /* couldn't allocate memory */
#define SMS_RDERR -4 /* read error */
#define SMS_WRERR -5 /* write error */
/* debug modes */
#define DEBUG_INIT 1 /* debug initialitzation functions */
#define DEBUG_PEAK_DET 2 /* debug peak detection function */
#define DEBUG_HARM_DET 3 /* debug harmonic detection function */
#define DEBUG_PEAK_CONT 4 /* debug peak continuation function */
#define DEBUG_CLEAN_TRAJ 5 /* debug clean trajectories function */
#define DEBUG_SINE_SYNTH 6 /* debug sine synthesis function */
#define DEBUG_STOC_ANAL 7 /* debug stochastic analysis function */
#define DEBUG_STOC_SYNTH 8 /* debug stochastic synthesis function */
#define DEBUG_SMS_ANAL 9 /* debug top level analysis function */
#define DEBUG_ALL 10 /* debug everything */
#define DEBUG_RESIDUAL 11 /* write residual to file */
#define DEBUG_SYNC 12 /* write original, synthesis and residual to
a text file */
#ifndef MAX
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#endif
#ifndef MIN
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#endif
#define TWO_PI 6.28318530717958647692
#define PI 3.141592653589793238462643
#define PI_2 1.57079632679489661923
#define HALF_MAX 1073741823.5
#define LOG2 0.69314718
#define EMPHASIS_COEFF .9 /* coefficient for pre_emphasis filter */
#define TO_DB(x) ((x > MAG_THRESHOLD) ? 20 * log10(x/MAG_THRESHOLD) : 0)
#define TO_MAG(x) ((x <= 0) ? 0 : MAG_THRESHOLD * pow(10.0, x/20.0))
/* short-time Fourier analysis */
#define WINDOWS_IN_FFT 2 /* the number of analysis windows
that fit in one FFT */
#define MAX_SIZE_WINDOW 8190 /* maximum size for analysis window */
#define MAX_SIZE_MAG 8192 /* maximum size for magnitude spectrum */
/* peak detection */
/* information attached to a spectral peak */
typedef struct
{
float fFreq; /* frequency of peak */
float fMag; /* magnitude of peak */
float fPhase; /* phase of peak */
} PEAK;
#define MAG_THRESHOLD .3 /* minimum magnitude to be searched */
#define MAX_NUM_PEAKS 200 /* maximum number of peaks */
/* harmonic detection */
/* information attached to a harmonic candidate */
typedef struct
{
float fFreq; /* frequency */
float fMag; /* magnitude */
float fMagPerc; /* percentage of magnitude */
float fFreqDev; /* deviation from perfect harmonic */
float fHarmRatio; /* percentage of harmonics found */
} HARM_CANDIDATE;
#define N_FUND_HARM 6 /* number of harmonics to use for fundamental
detection */
#define N_HARM_PEAKS 4 /* number of peaks to check as possible ref
harmonics */
#define FREQ_DEV_THRES .07 /* threshold for deviation from perfect
harmonics */
#define MAG_PERC_THRES .6 /* threshold for magnitude of harmonics
with respect to the total magnitude */
#define HARM_RATIO_THRES .8 /* threshold for percentage of harmonics found */
#define TYPE_MELODY 0 /* sound composed of several notes */
#define TYPE_SINGLE_NOTE 1 /* sound composed of a single note */
#define DIRECT 0 /* analysis from left to right */
#define REVERSE 1 /* analysis from right to left */
#define HAMMING 0
#define BLACKMAN_HARRIS_62 1
#define BLACKMAN_HARRIS_70 2
#define BLACKMAN_HARRIS_74 3
#define BLACKMAN_HARRIS_92 4
/* peak continuation */
/* diferent status of guide */
#define BEGINNING -2
#define DEAD -1
#define ACTIVE 0
/* information attached to a guide */
typedef struct
{
float fFreq; /* frequency of guide */
float fMag; /* magnitude of guide */
int iStatus; /* status of guide: DEAD, SLEEPING, ACTIVE */
short iPeakChosen; /* peak number chosen by the guide */
} GUIDE;
/* information attached to a continuation candidate */
typedef struct
{
float fFreqDev; /* frequency deviation from guide */
float fMagDev; /* magnitude deviation from guide */
int iPeak; /* peak number */
} CONT_CANDIDATE;
#define MAX_CONT_CANDIDATES 5 /* maximum number of peak continuation
candidates */
/* re-analyze and clean trajectories */
#define MIN_GOOD_FRAMES 3 /* minimum number of stable frames for backward
search */
#define MAX_DEVIATION .01 /* maximum deviation allowed */
#define ANAL_DELAY 10 /* number of frames in the past to be
looked in possible re-analyze */
/* total number of delay frames */
#define DELAY_FRAMES (MIN_GOOD_FRAMES + ANAL_DELAY)
/* structure with useful information for the analysis program */
typedef struct
{
int iDebugMode; /* 0 no debug
1 debug initialitzation functions,
2 debug peak detection function,
3 debug harmonic detection function,
4 debug peak continuation function,
5 debug clean trajectories function,
6 debug sine synthesis function,
7 debug stochastic analysis function,
8 debug stochastic synthesis function,
9 debug top level analysis function,
10 debug everything */
int iFormat; /* 1 the sound is analized as harmonic,
2 is inharmonic
3 is harmonic and phases are kept
4 is inharmonic and phases are kept */
int iStochasticType; /* 1: filter, 2: lines, 3: none */
float fLowestFundamental; /* lowest fundamental frequency in Hz */
float fHighestFundamental;/* highest fundamental frequency in Hz */
float fDefaultFundamental;/* default fundamental in Hz */
float fPeakContToGuide; /* contribution of previous peak to current
guide (between 0 and 1) */
float fFundContToGuide; /* contribution of current fundamental to
current guide (between 0 and 1) */
float fFreqDeviation; /* maximum deviation from peak to peak */
float fSamplingRate; /* sampling rate of input sound */
int iDefaultSizeWindow; /* default size of analysis window in samples */
int sizeHop; /* hop size of analysis window in samples */
float fSizeWindow; /* size of analysis window in number of periods */
int nGuides; /* number of guides used */
int iCleanTraj; /* whether or not to clean trajectories */
float fMinRefHarmMag; /* minimum magnitude in dB for reference peak */
float fRefHarmMagDiffFromMax; /* maximum magnitude difference from
reference peak to highest peak */
int iRefHarmonic; /* reference harmonic to use in the fundamental
detection */
int iMinTrajLength; /* minimum length in samples of a given
trajectory */
int iMaxSleepingTime; /* maximum sleeping time for a trajectory */
float fHighestFreq; /* highest frequency to be searched */
float fMinPeakMag; /* minimum magnitude in dB for a good peak */
int iSoundType; /* type of sound to be analyzed */
int iAnalysisDirection; /* analysis direction, direct or reverse */
int iSizeSound; /* total size of input sound */
int iWindowType; /* type of analysis window */
} ANAL_PARAMS;
/* structure with useful information for synthesis */
typedef struct
{
int iStochasticType;
int iSynthesisType; /* 0: both det & stoc, 1, 1: det, 2: stoc */
float fOriginalSRate;
float fSamplingRate;
SMS_DATA previousFrame;
int sizeHop;
float *pFDetWindow;
float *pFStocWindow;
} SYNTH_PARAMS;
/* buffer for sound data */
typedef struct
{
int iSoundSample; /* sample number of the sound file that
corresponds to the first sample in buffer */
float *pFBuffer; /* buffer for original sound */
int sizeBuffer; /* size of buffer */
int iFirstSample; /* first sample in buffer that is a good one */
} SOUND_BUFFER;
/* there are two sound buffers, one that holds the original sound and another */
/* that hold the deterministic synthesis */
SOUND_BUFFER soundBuffer, synthBuffer;
enum frameStatus {EMPTY, READY, PEAKS_FOUND, FUND_FOUND, TRAJ_FOUND, CLEANED,
RECOMPUTED, DETER_SYNTH, STOC_COMPUTED, DONE, END};
/* analysis frame structure */
typedef struct
{
int iFrameSample; /* sample number of the sound file that
corresponds to the middle of the frame */
int iFrameSize; /* number of samples used in the frame */
int iFrameNum; /* frame number */
PEAK pSpectralPeaks[MAX_NUM_PEAKS]; /* spectral peaks found in frame */
int nPeaks; /* number of peaks found */
float fFundamental; /* fundamental frequency in frame */
SMS_DATA deterministic; /* deterministic data */
enum frameStatus iStatus; /* status of frame */
} ANAL_FRAME;
ANAL_FRAME **ppFrames, *pFrames;
/* global variables */
float FSamplingRate;
float FHalfSamplingRate;
int MaxCoefficients;
int MaxTrajectories;
int MaxDelayFrames;
double *pFSTab;
double *pFSincTab;
float FResidualPerc;
float *pFWindowSpec; /* only used in the function Spectrum */
/* function declarations */
int SmsAnalysis (short *pSWaveform, int sizeNewData, SMS_DATA *pSmsData,
ANAL_PARAMS analParams, int *pINextSizeRead);
int Initialize (ANAL_PARAMS analParams);
void ClearSms (SMS_DATA *pSmsData, int iFormat);
void FillBuffer (short *pSWaveform, int sizeNewData,
SOUND_BUFFER *pSoundBuffer, ANAL_PARAMS analParams);
void moveFrames();
void InitializeFrame (int iCurrentFrame, SOUND_BUFFER soundBuffer,
ANAL_PARAMS analParams, int sizeWindow);
void ComputeFrame (int iCurrentFrame, ANAL_PARAMS analParams,
float fRefFundamental);
void GetWindow (int sizeWindow, float *pFWindow, int iWindowType);
void BlackmanHarris (int sizeWindow, float *pFWindow);
void Hamming (int sizeWindow, float *pFWindow);
void Hanning (int sizeWindow, float *pFWindow);
void realft (float *data, int n, int isign);
int Spectrum (float *pFWaveform, int sizeWindow, float *pFMagSpectrum,
float *pFPhaseSpectrum, ANAL_PARAMS analParams);
int QuickSpectrum (short *pIWaveform, float *pFWindow, int sizeWindow,
float *pFMagSpectrum, float *pFPhaseSpectrum, int sizeFft);
int QuickSpectrumF (float *pFWaveform, float *pFWindow, int sizeWindow,
float *pFMagSpectrum, float *pFPhaseSpectrum, int sizeFft);
int InverseQuickSpectrum (float *pFMagSpectrum, float *pFPhaseSpectrum,
int sizeFft, float *pFWaveform, int sizeWave);
int InverseQuickSpectrumW (float *pFMagSpectrum, float *pFPhaseSpectrum,
int sizeFft, float *pFWaveform, int sizeWave,
float *pFWindow);
int SpectralApprox (float *pFSpec1, int sizeSpec1, int sizeSpec1Used,
float *pFSpec2, int sizeSpec2, int nCoefficients);
int SetSizeWindow (int iCurrentFrame, ANAL_PARAMS analParams);
float GetDeviation (int iCurrentFrame);
int ReAnalyze (int iCurrentFrame, ANAL_PARAMS analParams);
int PeakDetection (float *pFMagSpectrum, float *pAPhaSpectrum, int sizeMag,
int sizeWindow, PEAK *pSpectralPeaks,
ANAL_PARAMS analParams);
void HarmDetection (ANAL_FRAME *pFrame, float fRefFundamental,
ANAL_PARAMS analParams);
void GenPeakContinuation (int iFrame, ANAL_PARAMS analParams);
int DeleteCandidate (CONT_CANDIDATE *pCandidate, int nCand, int iBestPeak);
int PeakContinuation (int iFrame, ANAL_PARAMS analParams);
float PreEmphasis (float fInput);
float DeEmphasis (float fInput);
void Covariance (float *pFSig, int nSig, int nStage, float *pFPhi, int nMax);
void CovLatticeHarm (float *pFPhi, int nMax, int m, float *pFPredCoeff,
float *pFReflexCoeff, float *pFError, float *pFScr);
int CleanTrajectories (int iCurrentFrame, ANAL_PARAMS analParams);
void ScaleDeterministic (float *pFSynthBuffer, float *pFOriginalBuffer,
float *pFMagTraj, ANAL_PARAMS analParams, int nTraj);
int PrepSine (int nTableSize);
double SinTab (double fTheta);
int PrepSinc ();
double SincTab (double fTheta);
int SmsSynthesis (SMS_DATA *pSmsData, short *pSSynthesis,
SYNTH_PARAMS *pSynthParams);
int FrameSineSynth (SMS_DATA *pSmsData, float *pFBuffer,
int sizeBuffer, SMS_DATA *pLastFrame);
long random ();
int writeSmsFile (char *pChFileName, SMSHeader *pSmsHeader);
int readSmsFile (char *pChFileName, SMSHeader **ppSmsHeader);
int initSmsHeader (SMSHeader *pSmsHeader);
int setSmsRecord (SMSHeader *pSmsHeader, char *pData, SMS_DATA *pSmsData);
int getRecordBSize (SMSHeader *pSmsHeader);
int quit (char *pChText);
void initSms (SMS_DATA *pSmsData);
void allocateSmsRecord (SMS_DATA *pSmsData, int nTraj, int nCoeff, int iPhase);
void freeSmsRecord (SMS_DATA *pSmsData);
int copySmsRecord (SMS_DATA *pCopySmsData, SMS_DATA *pOriginalSmsData);
void MoveFrames ();
int GetResidual (float *pFSynthesis, float *pFOriginal,
float *pFResidual, int sizeWindow, ANAL_PARAMS analParams);
int StocAnalysis (float *pFResidual, int sizeWindow,
SMS_DATA *pSmsData, ANAL_PARAMS analParams);
int createResidualFile (ANAL_PARAMS analParams);
int writeResidualFile ();
int createDebugFile (ANAL_PARAMS analParams);
int writeDebugFile ();
int InterpolateArrays (float *pFArray1, int sizeArray1, float *pFArray2,
int sizeArray2, float *pFArrayOut, int sizeArrayOut,
float fInterpFactor);
int InterpolateSmsRecords (SMS_DATA *pSmsRecord1, SMS_DATA *pSmsRecord2,
SMS_DATA *pSmsRecordOut, float fInterpFactor);
int FilterArray (float *pFArray, int size1, int size2, float *pFOutArray);
void ClearSine();
void IFFTwindow (int sizeWindow, float *pFWindow);
#endif
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.