/Users/richardeakin/projects/libsms/trunk/src/sms.c File Reference

initialization, free, and debug functions More...

#include "sms.h"
#include "SFMT.h"
Include dependency graph for sms.c:

Defines

#define SIZE_TABLES   4096
#define HALF_MAX   1073741823.5
#define INV_HALF_MAX   (1.0 / HALF_MAX)
#define TWENTY_OVER_LOG10   (20. / LOG10)

Functions

int sms_init (void)
 initialize global data
void sms_free (void)
 free global data
void sms_initAnalParams (SMS_AnalParams *pAnalParams)
 give default values to an SMS_AnalParams struct
int sms_initAnalysis (SMS_AnalParams *pAnalParams, SMS_SndHeader *pSoundHeader)
 initialize analysis data structure's arrays
void sms_initSynthParams (SMS_SynthParams *synthParams)
 give default values to an SMS_SynthParams struct
int sms_initSynth (SMS_Header *pSmsHeader, SMS_SynthParams *pSynthParams)
 initialize synthesis data structure's arrays
int sms_changeSynthHop (SMS_SynthParams *pSynthParams, int sizeHop)
void sms_freeAnalysis (SMS_AnalParams *pAnalParams)
 free analysis data
void sms_freeSynth (SMS_SynthParams *pSynthParams)
 free analysis data
int sms_sizeNextWindow (int iCurrentFrame, SMS_AnalParams *pAnalParams)
 set window size for next frame
int sms_initFrame (int iCurrentFrame, SMS_AnalParams *pAnalParams, int sizeWindow)
 initialize the current frame
sfloat sms_fundDeviation (SMS_AnalParams *pAnalParams, int iCurrentFrame)
 get deviation from average fundamental \
int sms_createDebugFile (SMS_AnalParams *pAnalParams)
 function to create the debug file
void sms_writeDebugData (sfloat *pFBuffer1, sfloat *pFBuffer2, sfloat *pFBuffer3, int sizeBuffer)
 function to write to the debug file
void sms_writeDebugFile ()
 function to write the residual sound file to disk
sfloat sms_magToDB (sfloat x)
 convert from magnitude to decibel
sfloat sms_dBToMag (sfloat x)
 convert from decibel to magnitude
void sms_arrayMagToDB (int sizeArray, sfloat *pArray)
 convert an array from magnitude to decibel
void sms_arrayDBToMag (int sizeArray, sfloat *pArray)
 convert and array from decibel (0-100) to magnitude (0-1)
void sms_setMagThresh (sfloat x)
 set the linear magnitude threshold
void sms_error (char *pErrorMessage)
 get a string containing information about the error code
int sms_errorCheck ()
 check if an error has been reported
char * sms_errorString ()
 get a string containing information about the last error
sfloat sms_random ()
 random number genorator
sfloat sms_rms (int sizeArray, sfloat *pArray)
 Root Mean Squared of an array.
int sms_power2 (int n)
 make sure a number is a power of 2
sfloat sms_scalarTempered (sfloat x)
 compute a value for scaling frequency based on the well-tempered scale
void sms_arrayScalarTempered (int sizeArray, sfloat *pArray)
 scale an array of linear frequencies to the well-tempered scale

Variables

char * pChDebugFile = "debug.txt"
FILE * pDebug
static char error_message [256]
static int error_status = 0
static sfloat mag_thresh = .00001
static sfloat inv_mag_thresh = 100000.
static int initIsDone = 0

Detailed Description

initialization, free, and debug functions


Define Documentation

#define HALF_MAX   1073741823.5

half the max of a 32-bit word

#define INV_HALF_MAX   (1.0 / HALF_MAX)

Referenced by sms_random().

#define SIZE_TABLES   4096

Referenced by sms_init().

#define TWENTY_OVER_LOG10   (20. / LOG10)

Referenced by sms_magToDB().


Function Documentation

int sms_changeSynthHop ( SMS_SynthParams pSynthParams,
int  sizeHop 
)
int sms_createDebugFile ( SMS_AnalParams pAnalParams  ) 

function to create the debug file

Parameters:
pAnalParams pointer to analysis params
Returns:
error value
See also:
SMS_ERRORS

References pChDebugFile, pDebug, SMS_OK, and SMS_WRERR.

void sms_error ( char *  pErrorMessage  ) 
int sms_errorCheck ( void   ) 

check if an error has been reported

Returns:
-1 if there is an error, 0 if ok

References error_status.

Referenced by sms_analyze().

char* sms_errorString ( void   ) 

get a string containing information about the last error

Returns:
pointer to a char string, or NULL if no error

References error_message, and error_status.

Referenced by sms_analyze().

void sms_free ( void   ) 

free global data

deallocates memory allocated to global arrays (windows and tables)

References initIsDone, sms_clearSinc(), and sms_clearSine().

Here is the call graph for this function:

void sms_freeAnalysis ( SMS_AnalParams pAnalParams  ) 

free analysis data

frees all the memory allocated to an SMS_AnalParams by sms_initAnalysis

Parameters:
pAnalParams pointer to analysis data structure

References SMS_AnalFrame::deterministic, SMS_AnalParams::iMaxDelayFrames, SMS_SndBuffer::pFBuffer, SMS_AnalParams::pFrames, SMS_AnalParams::ppFrames, SMS_AnalParams::prevFrame, sms_freeFrame(), SMS_AnalParams::soundBuffer, and SMS_AnalParams::synthBuffer.

Here is the call graph for this function:

void sms_freeSynth ( SMS_SynthParams pSynthParams  ) 

free analysis data

frees all the memory allocated to an SMS_SynthParams by sms_initSynthesis

Todo:
is there a way to make sure the plan has been made already? as it is, it crashes if this is called without one
Parameters:
pSynthParams pointer to synthesis data structure

References SMS_SynthParams::pFDetWindow, SMS_SynthParams::pFStocWindow, SMS_SynthParams::pMagBuff, SMS_SynthParams::pPhaseBuff, SMS_SynthParams::prevFrame, SMS_SynthParams::pSpectra, SMS_SynthParams::pSynthBuff, and sms_freeFrame().

Here is the call graph for this function:

sfloat sms_fundDeviation ( SMS_AnalParams pAnalParams,
int  iCurrentFrame 
)

get deviation from average fundamental \

Parameters:
pAnalParams pointer to analysis params
iCurrentFrame number of current frame
Returns:
deviation value or -1 if really off

References SMS_AnalFrame::fFundamental, SMS_AnalParams::ppFrames, sfloat, and SMS_MIN_GOOD_FRAMES.

Referenced by ReAnalyzeFrame(), and sms_analyze().

int sms_init ( void   ) 

initialize global data

Currently, just generating the sine and sinc tables. This is necessary before both analysis and synthesis.

If using the Mersenne Twister algorithm for random number generation, initialize (seed) it.

Returns:
error code
See also:
SMS_MALLOC or SMS_OK in SMS_ERRORS

References init_gen_rand(), initIsDone, SIZE_TABLES, sms_error(), sms_prepSinc(), and sms_prepSine().

Here is the call graph for this function:

void sms_initAnalParams ( SMS_AnalParams pAnalParams  ) 

give default values to an SMS_AnalParams struct

This will initialize an SMS_AnalParams with values that work for common analyses. It is useful to start with and then adjust the parameters manually to fit a particular sound

Certain things are hard coded in here that will have to be updated later (i.e. samplerate), so it is best to call this function first, then fill whatever parameters need to be adjusted.

Parameters:
pAnalParams pointer to analysis data structure

< depends on iFrameRate normally

< depends on iFrameRate normally

< no sound yet

< no frames yet

References SMS_AnalParams::fDefaultFundamental, SMS_AnalParams::fFreqDeviation, SMS_AnalParams::fFundContToGuide, SMS_AnalParams::fHighestFreq, SMS_AnalParams::fHighestFundamental, SMS_SEnvParams::fLambda, SMS_AnalParams::fLowestFundamental, SMS_AnalParams::fMinPeakMag, SMS_AnalParams::fMinRefHarmMag, SMS_AnalParams::fPeakContToGuide, SMS_AnalParams::fRefHarmMagDiffFromMax, SMS_AnalParams::fResidualAccumPerc, SMS_AnalParams::fSizeWindow, SMS_AnalParams::iAnalysisDirection, SMS_SEnvParams::iAnchor, SMS_AnalParams::iCleanTracks, SMS_AnalParams::iDebugMode, SMS_AnalParams::iDefaultSizeWindow, SMS_AnalParams::iFormat, SMS_AnalParams::iFrameRate, SMS_AnalParams::iMaxDelayFrames, SMS_SEnvParams::iMaxFreq, SMS_AnalParams::iMaxSleepingTime, SMS_AnalParams::iMinTrackLength, SMS_SEnvParams::iOrder, SMS_AnalParams::iRefHarmonic, SMS_AnalParams::iSamplingRate, SMS_AnalParams::iSizeSound, SMS_AnalParams::iSoundType, SMS_AnalParams::iStochasticType, SMS_SEnvParams::iType, SMS_AnalParams::iWindowType, MAX, SMS_SEnvParams::nCoeff, SMS_AnalParams::nFrames, SMS_AnalParams::nGuides, SMS_AnalParams::nStochasticCoeff, SMS_AnalParams::nTracks, SMS_AnalParams::sizeHop, SMS_DELAY_FRAMES, SMS_DIR_FWD, SMS_ENV_NONE, SMS_FORMAT_H, SMS_SOUND_TYPE_MELODY, SMS_STOC_APPROX, SMS_WIN_BH_70, and SMS_AnalParams::specEnvParams.

int sms_initAnalysis ( SMS_AnalParams pAnalParams,
SMS_SndHeader pSoundHeader 
)

initialize analysis data structure's arrays

based on the SMS_AnalParams current settings, this function will initialize the sound, synth, and fft arrays. It is necessary before analysis. there can be multple SMS_AnalParams at the same time

Parameters:
pAnalParams pointer to analysis paramaters
pSoundHeader pointer to sound header
Returns:
0 on success, -1 on error

References SMS_AnalFrame::deterministic, SMS_AnalParams::fDefaultFundamental, SMS_PeakParams::fHighestFreq, SMS_AnalParams::fHighestFreq, SMS_PeakParams::fHighestFundamental, SMS_AnalParams::fHighestFundamental, SMS_PeakParams::fLowestFreq, SMS_AnalParams::fLowestFundamental, SMS_AnalParams::fMinPeakMag, SMS_PeakParams::fMinPeakMag, SMS_AnalParams::fMinRefHarmMag, SMS_PeakParams::fMinRefHarmMag, SMS_AnalParams::fRefHarmMagDiffFromMax, SMS_PeakParams::fRefHarmMagDiffFromMax, SMS_AnalParams::fSizeWindow, SMS_AnalParams::iDefaultSizeWindow, SMS_SndBuffer::iFirstGood, SMS_AnalParams::iFrameRate, SMS_SndBuffer::iMarker, SMS_AnalParams::iMaxDelayFrames, SMS_SEnvParams::iMaxFreq, SMS_PeakParams::iMaxPeaks, SMS_SEnvParams::iOrder, SMS_AnalParams::iRefHarmonic, SMS_PeakParams::iRefHarmonic, SMS_PeakParams::iSamplingRate, SMS_AnalParams::iSamplingRate, SMS_SndHeader::iSamplingRate, SMS_AnalParams::iSizeSound, SMS_AnalParams::iSoundType, SMS_PeakParams::iSoundType, SMS_AnalFrame::iStatus, SMS_AnalParams::iStochasticType, SMS_SEnvParams::iType, SMS_SEnvParams::nCoeff, SMS_AnalParams::nFrames, SMS_AnalParams::nGuides, SMS_SndHeader::nSamples, SMS_AnalParams::nStochasticCoeff, SMS_AnalParams::peakParams, SMS_SndBuffer::pFBuffer, SMS_AnalParams::pFrames, SMS_AnalParams::ppFrames, SMS_AnalParams::prevFrame, sfloat, SMS_SndBuffer::sizeBuffer, SMS_AnalParams::sizeHop, SMS_AnalParams::sizeNextRead, sms_allocFrame(), SMS_ENV_CEP, SMS_ENV_FBINS, sms_error(), SMS_FRAME_EMPTY, SMS_MAX_NPEAKS, SMS_MAX_WINDOW, sms_power2(), SMS_STOC_IFFT, SMS_AnalParams::soundBuffer, SMS_AnalParams::specEnvParams, and SMS_AnalParams::synthBuffer.

Here is the call graph for this function:

int sms_initFrame ( int  iCurrentFrame,
SMS_AnalParams pAnalParams,
int  sizeWindow 
)

initialize the current frame

initializes arrays to zero and sets the correct sample position. Special care is taken at the end the sample source (if there is not enough samples for an entire frame.

Parameters:
iCurrentFrame frame number of current frame in buffer
pAnalParams analysis parameters
sizeWindow size of analysis window
Returns:
-1 on error
Todo:
make this return void

References SMS_AnalFrame::deterministic, SMS_AnalFrame::fFundamental, SMS_AnalFrame::iFrameNum, SMS_AnalFrame::iFrameSample, SMS_AnalFrame::iFrameSize, SMS_SndBuffer::iMarker, SMS_AnalParams::iSizeSound, SMS_AnalFrame::iStatus, SMS_AnalParams::nGuides, SMS_AnalFrame::nPeaks, SMS_Data::pFSinAmp, SMS_Data::pFSinFreq, SMS_Data::pFSinPha, SMS_AnalParams::ppFrames, SMS_AnalFrame::pSpectralPeaks, sfloat, SMS_AnalParams::sizeHop, sms_error(), SMS_FRAME_END, SMS_FRAME_READY, SMS_MAX_NPEAKS, SMS_OK, and SMS_AnalParams::soundBuffer.

Referenced by sms_analyze().

Here is the call graph for this function:

int sms_initSynth ( SMS_Header pSmsHeader,
SMS_SynthParams pSynthParams 
)

initialize synthesis data structure's arrays

Initialize the synthesis and fft arrays. It is necessary before synthesis. there can be multple SMS_SynthParams at the same time This function also sets some initial values that will create a sane synthesis environment.

This function requires an SMS_Header because it may be called to synthesize a stored .sms file, which contains a header with necessary information.

Parameters:
pSmsHeader pointer to SMS_Header
pSynthParams pointer to synthesis paramaters
Returns:
0 on success, -1 on error

References SMS_Header::iFrameRate, SMS_Header::iMaxFreq, SMS_SynthParams::iOriginalSRate, SMS_SynthParams::iSamplingRate, SMS_Header::iSamplingRate, SMS_Header::iStochasticType, SMS_SynthParams::iStochasticType, SMS_ModifyParams::maxFreq, SMS_SynthParams::modParams, SMS_Header::nEnvCoeff, SMS_Header::nStochasticCoeff, SMS_Header::nTracks, SMS_SynthParams::origSizeHop, SMS_SynthParams::pFDetWindow, SMS_SynthParams::pFStocWindow, SMS_SynthParams::pMagBuff, SMS_SynthParams::pPhaseBuff, SMS_SynthParams::prevFrame, SMS_SynthParams::pSpectra, SMS_SynthParams::pSynthBuff, sfloat, SMS_SynthParams::sizeHop, sms_allocFrame(), sms_error(), sms_getWindow(), SMS_OK, sms_power2(), SMS_WIN_HANNING, and SMS_WIN_IFFT.

Here is the call graph for this function:

void sms_initSynthParams ( SMS_SynthParams synthParams  ) 

give default values to an SMS_SynthParams struct

This will initialize an SMS_SynthParams with values that work for common analyses. It is useful to start with and then adjust the parameters manually to fit a particular sound

Parameters:
synthParams pointer to synthesis parameters data structure

References SMS_SynthParams::iDetSynthType, SMS_SynthParams::iSamplingRate, SMS_SynthParams::iSynthesisType, SMS_SynthParams::modParams, SMS_SynthParams::sizeHop, SMS_DET_IFFT, sms_initModifyParams(), SMS_MIN_SIZE_FRAME, and SMS_STYPE_ALL.

Here is the call graph for this function:

sfloat sms_scalarTempered ( sfloat  x  ) 

compute a value for scaling frequency based on the well-tempered scale

Parameters:
x linear frequency value
Returns:
(1.059...)^x, where 1.059 is the 12th root of 2 precomputed

Referenced by sms_arrayScalarTempered(), and sms_transpose().

int sms_sizeNextWindow ( int  iCurrentFrame,
SMS_AnalParams pAnalParams 
)

set window size for next frame

adjusts the next window size to fit the currently detected fundamental frequency, or resets to a default window size if unstable.

Parameters:
iCurrentFrame number of current frame
pAnalParams analysis parameters
Returns:
the size of the next window in samples

References SMS_AnalFrame::fFundamental, SMS_AnalParams::fSizeWindow, SMS_AnalParams::iDefaultSizeWindow, SMS_AnalParams::iSamplingRate, SMS_AnalParams::ppFrames, sfloat, and SMS_MAX_WINDOW.

Referenced by sms_analyze().

void sms_writeDebugData ( sfloat *  pFBuffer1,
sfloat *  pFBuffer2,
sfloat *  pFBuffer3,
int  sizeBuffer 
)

function to write to the debug file

writes three arrays of equal size to a debug text file ("./debug.txt"). There are three arrays for the frequency, magnitude, phase sets.

Parameters:
pFBuffer1 pointer to array 1
pFBuffer2 pointer to array 2
pFBuffer3 pointer to array 3
sizeBuffer the size of the buffers

References pDebug.

void sms_writeDebugFile ( void   ) 

function to write the residual sound file to disk

writes the "debug.txt" file to disk and closes the file.

References pDebug.


Variable Documentation

char error_message[256] [static]

Referenced by sms_error(), and sms_errorString().

int error_status = 0 [static]
int initIsDone = 0 [static]

Referenced by sms_free(), and sms_init().

sfloat inv_mag_thresh = 100000. [static]

inv(.00001)

Referenced by sms_magToDB(), and sms_setMagThresh().

sfloat mag_thresh = .00001 [static]

magnitude threshold for db conversion (-100db)

Referenced by sms_dBToMag(), sms_magToDB(), and sms_setMagThresh().

char* pChDebugFile = "debug.txt"

< mersenne twister random number genorator debug text file

Referenced by sms_createDebugFile().

FILE* pDebug

pointer to debug file

Referenced by sms_createDebugFile(), sms_writeDebugData(), and sms_writeDebugFile().

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on Thu Oct 29 04:08:07 2009 for libsms by  doxygen 1.6.1