comparison data/fileio/test/AudioTestData.h @ 756:02390a4c2abe

Toward audio read tests
author Chris Cannam
date Fri, 08 Mar 2013 16:14:21 +0000
parents
children a1cd5abcb38b
comparison
equal deleted inserted replaced
755:dc6c0e50724c 756:02390a4c2abe
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2013 Chris Cannam.
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 #ifndef AUDIO_TEST_DATA_H
17 #define AUDIO_TEST_DATA_H
18
19 #include <cmath>
20
21 /**
22 * Class that generates a single fixed test pattern to a given sample
23 * rate and number of channels.
24 *
25 * The test pattern is two seconds long and consists of:
26 *
27 * -- in channel 0, a 600Hz sinusoid with peak amplitude 1.0
28 *
29 * -- in channel 1, four triangular forms with peaks at +1.0, -1.0,
30 * +1.0, -1.0 respectively, of 10ms width, starting at 0.0, 0.5,
31 * 1.0 and 1.5 seconds; silence elsewhere
32 *
33 * -- in subsequent channels, a flat DC offset at +(channelNo / 20.0)
34 */
35 class AudioTestData
36 {
37 public:
38 AudioTestData(float rate, int channels) :
39 m_channelCount(channels),
40 m_duration(2.0),
41 m_sampleRate(rate),
42 m_sinFreq(600.0),
43 m_pulseFreq(2)
44 {
45 m_frameCount = lrint(m_duration * m_sampleRate);
46 m_data = new float[m_frameCount * m_channelCount];
47 m_pulseWidth = 0.01 * m_sampleRate;
48 generate();
49 }
50
51 ~AudioTestData() {
52 delete[] m_data;
53 }
54
55 void generate() {
56
57 float hpw = m_pulseWidth / 2.0;
58
59 for (int i = 0; i < m_frameCount; ++i) {
60 for (int c = 0; c < m_channelCount; ++c) {
61
62 float s = 0.f;
63
64 if (c == 0) {
65
66 float phase = (i * m_sinFreq * 2.f * M_PI) / m_sampleRate;
67 s = sinf(phase);
68
69 } else if (c == 1) {
70
71 int pulseNo = int((i * m_pulseFreq) / m_sampleRate);
72 int index = (i * m_pulseFreq) - (m_sampleRate * pulseNo);
73 if (index < m_pulseWidth) {
74 s = 1.0 - fabsf(hpw - index) / hpw;
75 if (pulseNo % 2) s = -s;
76 }
77
78 } else {
79
80 s = c / 20.0;
81 }
82
83 m_data[i * m_channelCount + c] = s;
84 }
85 }
86 }
87
88 float *getInterleavedData() const {
89 return m_data;
90 }
91
92 int getFrameCount() const {
93 return m_frameCount;
94 }
95
96 int getChannelCount() const {
97 return m_channelCount;
98 }
99
100 float getSampleRate () const {
101 return m_sampleRate;
102 }
103
104 float getDuration() const { // seconds
105 return m_duration;
106 }
107
108 private:
109 float *m_data;
110 int m_frameCount;
111 int m_channelCount;
112 float m_duration;
113 float m_sampleRate;
114 float m_sinFreq;
115 float m_pulseFreq;
116 float m_pulseWidth;
117 };
118
119 #endif
120