#import "SmsObject.h"

@implementation SmsObject

- initFromFile:(char*)fileName
{  
    [super init];
    if([self readFile:fileName] != SMS_OK)
    {
      [self free];
      return nil;
    }
    
    iCurrentFrame = -1;
    initSms(&smsRecord);
    return self;
}

- (int)readFile:(char*)fileName
{
    int iError;
    
    iError = readSmsFile(fileName, &pSmsHeader);
    return iError;
}

- (int)getNRecords
{
    return(pSmsHeader->nRecords);
}

- (int)getNTrajectories
{
    return(pSmsHeader->nTrajectories);
}

- (int)getNStochasticCoeff        
{
    return(pSmsHeader->nStochasticCoeff);
}

- (int)getFormat
{
    return(pSmsHeader->iFormat);
}
    
- (int)getFrameRate
{
    return(pSmsHeader->iFrameRate);
}  

- (int)getStochasticType
{
    return(pSmsHeader->iStochasticType);
}  

- (float)getOriginalSRate
{
    return(pSmsHeader->fOriginalSRate);
}  
      
- (SMS_DATA *)getRecord:(int)iFrame
{    
    if(iFrame >= 0 && iFrame < pSmsHeader->nRecords)
    {
      char *pData;
      pData = (char *) pSmsHeader->pChDataRecords + 
    		     iFrame * pSmsHeader->iRecordBSize;
      setSmsRecord(pSmsHeader, pData, &smsRecord);
      iCurrentFrame = iFrame;
    }
    else
      ClearSms (&smsRecord, pSmsHeader->iFormat);
      
    return &smsRecord;
 }    

- setSmsRecord:(int)iFrame record:(SMS_DATA *)pRecord
{    
	if(iFrame >= 0 && iFrame < pSmsHeader->nRecords)
	{
		char *pData;
		pData = (char *) pSmsHeader->pChDataRecords + 
		        iFrame * pSmsHeader->iRecordBSize;
		setSmsRecord(pSmsHeader, pData, pRecord);
		iCurrentFrame = iFrame;
	}
	else
		ClearSms (pRecord, pSmsHeader->iFormat);
      
    return self;
 }    

- (float)getDeterministicFreq:(int)iFrame iTraj:(int)iTraj
{
    if(iFrame >= 0 && iFrame < pSmsHeader->nRecords &&
       iTraj >=0 && iTraj < pSmsHeader->nTrajectories)
    {
      if(iFrame == iCurrentFrame)
    	  return(smsRecord.pFFreqTraj[iTraj]);
      else
      {
   	  float *pFData;
   	  pFData = (float *) ((char*)pSmsHeader->pChDataRecords + 
    	  	iFrame * pSmsHeader->iRecordBSize + iTraj * sizeof(float));
          return(*pFData);
      }
    }
    else
      return(-1);
}

- (float)getDeterministicMag:(int)iFrame iTraj:(int)iTraj
{
	if(iFrame >= 0 && iFrame < pSmsHeader->nRecords &&
	   iTraj >=0 && iTraj < pSmsHeader->nTrajectories)
	{
		if(iFrame == iCurrentFrame)
			return(smsRecord.pFMagTraj[iTraj]);
		else
		{
			float *pFData;
    
			pFData = (float *) ((char*)pSmsHeader->pChDataRecords + 
				iFrame * pSmsHeader->iRecordBSize +
			sizeof(float) * pSmsHeader->nTrajectories + 
			iTraj * sizeof(float));
    	
			return(*pFData);
		}
	}
	else
		return(-1);
}

- (float)getStochasticGain:(int)iFrame
{
	if(iFrame >= 0 && iFrame < pSmsHeader->nRecords)
	{
		if(iFrame == iCurrentFrame)
			return(*smsRecord.pFStocGain);
		else
		{
			float *pFData;
			int nComponents = 2;
	  
			if (pSmsHeader->iFormat == FORMAT_HARMONIC_WITH_PHASE ||
			    pSmsHeader->iFormat == FORMAT_INHARMONIC_WITH_PHASE)
				nComponents = 3;
	    
			pFData = (float *) ((char*)pSmsHeader->pChDataRecords + 
				iFrame * pSmsHeader->iRecordBSize +
			sizeof(float) * nComponents * pSmsHeader->nTrajectories +
			sizeof(float) * pSmsHeader->nStochasticCoeff);
    	
			return(*pFData);
		}
	}
	else
		return(-1);
}

- (float)getStochasticCoefficient:(int)iFrame iCoeff:(int)iCoeff
{
	if(iFrame >= 0 && iFrame < pSmsHeader->nRecords &&
	   iCoeff >=0 && iCoeff < pSmsHeader->nStochasticCoeff)
	{
		if(iFrame == iCurrentFrame)
			return(smsRecord.pFStocCoeff[iCoeff]);
		else
		{
			float *pFData;
			int nComponents = 2;
	  
			if (pSmsHeader->iFormat == FORMAT_HARMONIC_WITH_PHASE ||
			    pSmsHeader->iFormat == FORMAT_INHARMONIC_WITH_PHASE)
				nComponents = 3;

			pFData = (float *) ((char*)pSmsHeader->pChDataRecords + 
				iFrame * pSmsHeader->iRecordBSize +
			sizeof(float) * nComponents * pSmsHeader->nTrajectories +
			sizeof(float) * iCoeff);
    	
			return(*pFData);
		}
	}
	else
		return(-1);
}

-(char *)getTextCharacters
{
	return (pSmsHeader->pChTextCharacters);
}

- free
{
  free (pSmsHeader);
  return [super free];
}

@end
