globally remove all trailing whitespace from .cpp and .hpp files missed by previous...
[ardour.git] / libs / qm-dsp / dsp / signalconditioning / Framer.cpp
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
2
3 /*
4     QM DSP Library
5
6     Centre for Digital Music, Queen Mary, University of London.
7     This file 2005-2006 Christian Landone.
8
9     This program is free software; you can redistribute it and/or
10     modify it under the terms of the GNU General Public License as
11     published by the Free Software Foundation; either version 2 of the
12     License, or (at your option) any later version.  See the file
13     COPYING included with this distribution for more information.
14 */
15
16 #include "Framer.h"
17 #include <math.h>
18
19 //////////////////////////////////////////////////////////////////////
20 // Construction/Destruction
21 //////////////////////////////////////////////////////////////////////
22
23 Framer::Framer()
24 {
25     m_dataFrame = NULL;
26     m_strideFrame = NULL;
27 }
28
29 Framer::~Framer()
30 {
31     if( m_dataFrame != NULL )
32         delete [] m_dataFrame;
33
34     if( m_strideFrame != NULL )
35         delete [] m_strideFrame;
36 }
37
38 void Framer::configure( unsigned int frameLength, unsigned int hop )
39 {
40     m_frameLength = frameLength;
41     m_stepSize = hop;
42
43     resetCounters();
44
45     if( m_dataFrame != NULL )
46     {
47         delete [] m_dataFrame;  
48         m_dataFrame = NULL;
49     }
50     m_dataFrame = new double[ m_frameLength ];
51
52     if( m_strideFrame != NULL )
53     {
54         delete [] m_strideFrame;        
55         m_strideFrame = NULL;
56     }
57     m_strideFrame = new double[ m_stepSize ];
58 }
59
60 void Framer::getFrame(double *dst)
61 {
62
63     if( (m_ulSrcIndex + ( m_frameLength) ) < m_ulSampleLen )
64     {
65         for( unsigned int u = 0; u < m_frameLength; u++)
66         {
67             dst[ u ] = m_srcBuffer[ m_ulSrcIndex++ ];
68         }       
69         m_ulSrcIndex -= ( m_frameLength - m_stepSize );
70     }
71     else
72     {
73         unsigned int rem = (m_ulSampleLen - m_ulSrcIndex );
74         unsigned int zero = m_frameLength - rem;
75
76         for( unsigned int u = 0; u < rem; u++ )
77         {
78             dst[ u ] = m_srcBuffer[ m_ulSrcIndex++ ];
79         }
80                 
81         for( unsigned int u = 0; u < zero; u++ )
82         {
83             dst[ rem + u ] = 0;
84         }
85
86         m_ulSrcIndex -= (( rem - m_stepSize ) );
87     }
88
89     m_framesRead++;
90 }
91
92 void Framer::resetCounters()
93 {
94     m_framesRead = 0;
95     m_ulSrcIndex = 0;
96 }
97
98 unsigned int Framer::getMaxNoFrames()
99 {
100     return m_maxFrames;
101 }
102
103 void Framer::setSource(double *src, unsigned int length)
104 {
105     m_srcBuffer = src;
106     m_ulSampleLen = length;
107
108     m_maxFrames = (unsigned int)ceil( (double)m_ulSampleLen/(double)m_stepSize ) ;
109 }