comparison dsp/signalconditioning/Framer.cpp @ 502:162673c8f9de

Style fixes: avoid unsigned, fix formatting
author Chris Cannam <cannam@all-day-breakfast.com>
date Wed, 05 Jun 2019 11:05:58 +0100
parents 5a53b8281eb4
children
comparison
equal deleted inserted replaced
501:12b5a9244bb0 502:162673c8f9de
12 License, or (at your option) any later version. See the file 12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information. 13 COPYING included with this distribution for more information.
14 */ 14 */
15 15
16 #include "Framer.h" 16 #include "Framer.h"
17 #include <cmath>
18 #include <cstddef>
19 17
20 ////////////////////////////////////////////////////////////////////// 18 #include <limits.h>
21 // Construction/Destruction
22 //////////////////////////////////////////////////////////////////////
23 19
24 Framer::Framer() 20 Framer::Framer() :
21 m_sampleLen(0),
22 m_framesRead(0),
23 m_srcBuffer(0),
24 m_dataFrame(0),
25 m_strideFrame(0),
26 m_frameLength(0),
27 m_stepSize(0),
28 m_maxFrames(0),
29 m_srcIndex(0)
25 { 30 {
26 m_dataFrame = NULL;
27 m_strideFrame = NULL;
28 } 31 }
29 32
30 Framer::~Framer() 33 Framer::~Framer()
31 { 34 {
32 if( m_dataFrame != NULL ) { 35 delete[] m_dataFrame;
33 delete [] m_dataFrame; 36 delete[] m_strideFrame;
34 }
35
36 if( m_strideFrame != NULL ) {
37 delete [] m_strideFrame;
38 }
39 } 37 }
40 38
41 void Framer::configure( unsigned int frameLength, unsigned int hop ) 39 void Framer::configure(int frameLength, int hop)
42 { 40 {
43 m_frameLength = frameLength; 41 m_frameLength = frameLength;
44 m_stepSize = hop; 42 m_stepSize = hop;
45 43
46 resetCounters(); 44 resetCounters();
47 45
48 if( m_dataFrame != NULL ) { 46 delete[] m_dataFrame;
49 delete [] m_dataFrame;
50 m_dataFrame = NULL;
51 }
52 m_dataFrame = new double[ m_frameLength ]; 47 m_dataFrame = new double[ m_frameLength ];
53 48
54 if( m_strideFrame != NULL ) { 49 delete [] m_strideFrame;
55 delete [] m_strideFrame;
56 m_strideFrame = NULL;
57 }
58 m_strideFrame = new double[ m_stepSize ]; 50 m_strideFrame = new double[ m_stepSize ];
59 } 51 }
60 52
61 void Framer::getFrame(double *dst) 53 void Framer::getFrame(double *dst)
62 { 54 {
55 if ((m_srcIndex + int64_t(m_frameLength)) < m_sampleLen) {
63 56
64 if( (m_ulSrcIndex + ( m_frameLength) ) < m_ulSampleLen ) { 57 for (int i = 0; i < m_frameLength; i++) {
58 dst[i] = m_srcBuffer[m_srcIndex++];
59 }
60 m_srcIndex -= (m_frameLength - m_stepSize);
65 61
66 for( unsigned int u = 0; u < m_frameLength; u++) { 62 } else { // m_srcIndex is within m_frameLength of m_sampleLen
67 dst[ u ] = m_srcBuffer[ m_ulSrcIndex++ ];
68 }
69 m_ulSrcIndex -= ( m_frameLength - m_stepSize );
70 63
71 } else { 64 int rem = int(m_sampleLen - m_srcIndex);
65 int zero = m_frameLength - rem;
72 66
73 unsigned int rem = (m_ulSampleLen - m_ulSrcIndex ); 67 for (int i = 0; i < rem; i++) {
74 unsigned int zero = m_frameLength - rem; 68 dst[i] = m_srcBuffer[m_srcIndex++];
75
76 for( unsigned int u = 0; u < rem; u++ ) {
77 dst[ u ] = m_srcBuffer[ m_ulSrcIndex++ ];
78 } 69 }
79 70
80 for( unsigned int u = 0; u < zero; u++ ) { 71 for (int i = 0; i < zero; i++ ) {
81 dst[ rem + u ] = 0; 72 dst[rem + i] = 0.0;
82 } 73 }
83 74
84 m_ulSrcIndex -= (( rem - m_stepSize ) ); 75 m_srcIndex -= (rem - m_stepSize);
85 } 76 }
86 77
87 m_framesRead++; 78 m_framesRead++;
88 } 79 }
89 80
90 void Framer::resetCounters() 81 void Framer::resetCounters()
91 { 82 {
92 m_framesRead = 0; 83 m_framesRead = 0;
93 m_ulSrcIndex = 0; 84 m_srcIndex = 0;
94 } 85 }
95 86
96 unsigned int Framer::getMaxNoFrames() 87 int Framer::getMaxNoFrames()
97 { 88 {
98 return m_maxFrames; 89 return m_maxFrames;
99 } 90 }
100 91
101 void Framer::setSource(double *src, unsigned int length) 92 void Framer::setSource(double *src, int64_t length)
102 { 93 {
103 m_srcBuffer = src; 94 m_srcBuffer = src;
104 m_ulSampleLen = length; 95 m_sampleLen = length;
105 96
106 m_maxFrames = (unsigned int)ceil( (double)m_ulSampleLen/(double)m_stepSize ) ; 97 int64_t maxFrames = length / int64_t(m_stepSize);
98 if (maxFrames * int64_t(m_stepSize) < length) {
99 ++maxFrames;
100 }
101 if (maxFrames > INT_MAX) maxFrames = INT_MAX;
102 m_maxFrames = maxFrames;
107 } 103 }