comparison data/fileio/test/AudioTestData.h @ 1040:a1cd5abcb38b cxx11

Introduce and use a samplerate type
author Chris Cannam
date Wed, 04 Mar 2015 12:01:04 +0000
parents 02390a4c2abe
children 48e9f538e6e9
comparison
equal deleted inserted replaced
1039:b14064bd1f97 1040:a1cd5abcb38b
16 #ifndef AUDIO_TEST_DATA_H 16 #ifndef AUDIO_TEST_DATA_H
17 #define AUDIO_TEST_DATA_H 17 #define AUDIO_TEST_DATA_H
18 18
19 #include <cmath> 19 #include <cmath>
20 20
21 #include "base/BaseTypes.h"
22
21 /** 23 /**
22 * Class that generates a single fixed test pattern to a given sample 24 * Class that generates a single fixed test pattern to a given sample
23 * rate and number of channels. 25 * rate and number of channels.
24 * 26 *
25 * The test pattern is two seconds long and consists of: 27 * The test pattern is two seconds long and consists of:
33 * -- in subsequent channels, a flat DC offset at +(channelNo / 20.0) 35 * -- in subsequent channels, a flat DC offset at +(channelNo / 20.0)
34 */ 36 */
35 class AudioTestData 37 class AudioTestData
36 { 38 {
37 public: 39 public:
38 AudioTestData(float rate, int channels) : 40 AudioTestData(double rate, int channels) :
39 m_channelCount(channels), 41 m_channelCount(channels),
40 m_duration(2.0), 42 m_duration(2.0),
41 m_sampleRate(rate), 43 m_sampleRate(rate),
42 m_sinFreq(600.0), 44 m_sinFreq(600.0),
43 m_pulseFreq(2) 45 m_pulseFreq(2)
52 delete[] m_data; 54 delete[] m_data;
53 } 55 }
54 56
55 void generate() { 57 void generate() {
56 58
57 float hpw = m_pulseWidth / 2.0; 59 double hpw = m_pulseWidth / 2.0;
58 60
59 for (int i = 0; i < m_frameCount; ++i) { 61 for (int i = 0; i < m_frameCount; ++i) {
60 for (int c = 0; c < m_channelCount; ++c) { 62 for (int c = 0; c < m_channelCount; ++c) {
61 63
62 float s = 0.f; 64 double s = 0.0;
63 65
64 if (c == 0) { 66 if (c == 0) {
65 67
66 float phase = (i * m_sinFreq * 2.f * M_PI) / m_sampleRate; 68 double phase = (i * m_sinFreq * 2.0 * M_PI) / m_sampleRate;
67 s = sinf(phase); 69 s = sin(phase);
68 70
69 } else if (c == 1) { 71 } else if (c == 1) {
70 72
71 int pulseNo = int((i * m_pulseFreq) / m_sampleRate); 73 int pulseNo = int((i * m_pulseFreq) / m_sampleRate);
72 int index = (i * m_pulseFreq) - (m_sampleRate * pulseNo); 74 int index = int(round((i * m_pulseFreq) -
75 (m_sampleRate * pulseNo)));
73 if (index < m_pulseWidth) { 76 if (index < m_pulseWidth) {
74 s = 1.0 - fabsf(hpw - index) / hpw; 77 s = 1.0 - fabs(hpw - index) / hpw;
75 if (pulseNo % 2) s = -s; 78 if (pulseNo % 2) s = -s;
76 } 79 }
77 80
78 } else { 81 } else {
79 82
80 s = c / 20.0; 83 s = c / 20.0;
81 } 84 }
82 85
83 m_data[i * m_channelCount + c] = s; 86 m_data[i * m_channelCount + c] = float(s);
84 } 87 }
85 } 88 }
86 } 89 }
87 90
88 float *getInterleavedData() const { 91 float *getInterleavedData() const {
89 return m_data; 92 return m_data;
90 } 93 }
91 94
92 int getFrameCount() const { 95 sv_frame_t getFrameCount() const {
93 return m_frameCount; 96 return m_frameCount;
94 } 97 }
95 98
96 int getChannelCount() const { 99 int getChannelCount() const {
97 return m_channelCount; 100 return m_channelCount;
98 } 101 }
99 102
100 float getSampleRate () const { 103 sv_samplerate_t getSampleRate () const {
101 return m_sampleRate; 104 return m_sampleRate;
102 } 105 }
103 106
104 float getDuration() const { // seconds 107 double getDuration() const { // seconds
105 return m_duration; 108 return m_duration;
106 } 109 }
107 110
108 private: 111 private:
109 float *m_data; 112 float *m_data;
110 int m_frameCount; 113 sv_frame_t m_frameCount;
111 int m_channelCount; 114 int m_channelCount;
112 float m_duration; 115 double m_duration;
113 float m_sampleRate; 116 sv_samplerate_t m_sampleRate;
114 float m_sinFreq; 117 double m_sinFreq;
115 float m_pulseFreq; 118 double m_pulseFreq;
116 float m_pulseWidth; 119 double m_pulseWidth;
117 }; 120 };
118 121
119 #endif 122 #endif
120 123