qm-dsp
1.8
|
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ 00002 00003 /* 00004 QM DSP Library 00005 00006 Centre for Digital Music, Queen Mary, University of London. 00007 This file 2005-2006 Christian Landone. 00008 00009 This program is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU General Public License as 00011 published by the Free Software Foundation; either version 2 of the 00012 License, or (at your option) any later version. See the file 00013 COPYING included with this distribution for more information. 00014 */ 00015 00016 #include "Framer.h" 00017 #include <math.h> 00018 00020 // Construction/Destruction 00022 00023 Framer::Framer() 00024 { 00025 m_dataFrame = NULL; 00026 m_strideFrame = NULL; 00027 } 00028 00029 Framer::~Framer() 00030 { 00031 if( m_dataFrame != NULL ) 00032 delete [] m_dataFrame; 00033 00034 if( m_strideFrame != NULL ) 00035 delete [] m_strideFrame; 00036 } 00037 00038 void Framer::configure( unsigned int frameLength, unsigned int hop ) 00039 { 00040 m_frameLength = frameLength; 00041 m_stepSize = hop; 00042 00043 resetCounters(); 00044 00045 if( m_dataFrame != NULL ) 00046 { 00047 delete [] m_dataFrame; 00048 m_dataFrame = NULL; 00049 } 00050 m_dataFrame = new double[ m_frameLength ]; 00051 00052 if( m_strideFrame != NULL ) 00053 { 00054 delete [] m_strideFrame; 00055 m_strideFrame = NULL; 00056 } 00057 m_strideFrame = new double[ m_stepSize ]; 00058 } 00059 00060 void Framer::getFrame(double *dst) 00061 { 00062 00063 if( (m_ulSrcIndex + ( m_frameLength) ) < m_ulSampleLen ) 00064 { 00065 for( unsigned int u = 0; u < m_frameLength; u++) 00066 { 00067 dst[ u ] = m_srcBuffer[ m_ulSrcIndex++ ]; 00068 } 00069 m_ulSrcIndex -= ( m_frameLength - m_stepSize ); 00070 } 00071 else 00072 { 00073 unsigned int rem = (m_ulSampleLen - m_ulSrcIndex ); 00074 unsigned int zero = m_frameLength - rem; 00075 00076 for( unsigned int u = 0; u < rem; u++ ) 00077 { 00078 dst[ u ] = m_srcBuffer[ m_ulSrcIndex++ ]; 00079 } 00080 00081 for( unsigned int u = 0; u < zero; u++ ) 00082 { 00083 dst[ rem + u ] = 0; 00084 } 00085 00086 m_ulSrcIndex -= (( rem - m_stepSize ) ); 00087 } 00088 00089 m_framesRead++; 00090 } 00091 00092 void Framer::resetCounters() 00093 { 00094 m_framesRead = 0; 00095 m_ulSrcIndex = 0; 00096 } 00097 00098 unsigned int Framer::getMaxNoFrames() 00099 { 00100 return m_maxFrames; 00101 } 00102 00103 void Framer::setSource(double *src, unsigned int length) 00104 { 00105 m_srcBuffer = src; 00106 m_ulSampleLen = length; 00107 00108 m_maxFrames = (unsigned int)ceil( (double)m_ulSampleLen/(double)m_stepSize ) ; 00109 }