Framer.cpp
Go to the documentation of this file.
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 
18 #include <limits.h>
19 
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)
30 {
31 }
32 
34 {
35  delete[] m_dataFrame;
36  delete[] m_strideFrame;
37 }
38 
39 void Framer::configure(int frameLength, int hop)
40 {
41  m_frameLength = frameLength;
42  m_stepSize = hop;
43 
44  resetCounters();
45 
46  delete[] m_dataFrame;
47  m_dataFrame = new double[ m_frameLength ];
48 
49  delete [] m_strideFrame;
50  m_strideFrame = new double[ m_stepSize ];
51 }
52 
53 void Framer::getFrame(double *dst)
54 {
55  if ((m_srcIndex + int64_t(m_frameLength)) < m_sampleLen) {
56 
57  for (int i = 0; i < m_frameLength; i++) {
58  dst[i] = m_srcBuffer[m_srcIndex++];
59  }
60  m_srcIndex -= (m_frameLength - m_stepSize);
61 
62  } else { // m_srcIndex is within m_frameLength of m_sampleLen
63 
64  int rem = int(m_sampleLen - m_srcIndex);
65  int zero = m_frameLength - rem;
66 
67  for (int i = 0; i < rem; i++) {
68  dst[i] = m_srcBuffer[m_srcIndex++];
69  }
70 
71  for (int i = 0; i < zero; i++ ) {
72  dst[rem + i] = 0.0;
73  }
74 
75  m_srcIndex -= (rem - m_stepSize);
76  }
77 
78  m_framesRead++;
79 }
80 
82 {
83  m_framesRead = 0;
84  m_srcIndex = 0;
85 }
86 
88 {
89  return m_maxFrames;
90 }
91 
92 void Framer::setSource(double *src, int64_t length)
93 {
94  m_srcBuffer = src;
95  m_sampleLen = length;
96 
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;
103 }
void getFrame(double *dst)
Definition: Framer.cpp:53
int m_frameLength
Definition: Framer.h:42
void configure(int frameLength, int hop)
Definition: Framer.cpp:39
double * m_srcBuffer
Definition: Framer.h:39
int m_framesRead
Definition: Framer.h:37
double * m_dataFrame
Definition: Framer.h:40
int getMaxNoFrames()
Definition: Framer.cpp:87
double * m_strideFrame
Definition: Framer.h:41
int m_stepSize
Definition: Framer.h:43
void setSource(double *src, int64_t length)
Definition: Framer.cpp:92
Framer()
Definition: Framer.cpp:20
int64_t m_sampleLen
Definition: Framer.h:36
int64_t m_srcIndex
Definition: Framer.h:47
virtual ~Framer()
Definition: Framer.cpp:33
int m_maxFrames
Definition: Framer.h:45
void resetCounters()
Definition: Framer.cpp:81