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 #include "base/BaseTypes.h"
22 
23 /**
24  * Class that generates a single fixed test pattern to a given sample
25  * rate and number of channels.
26  *
27  * The test pattern is two seconds long and consists of:
28  *
29  * -- in channel 0, a 600Hz sinusoid with peak amplitude 1.0
30  *
31  * -- in channel 1, four triangular forms with peaks at +1.0, -1.0,
32  *    +1.0, -1.0 respectively, of 10ms width, starting at 0.0, 0.5,
33  *    1.0 and 1.5 seconds; silence elsewhere
34  *
35  * -- in subsequent channels, a flat DC offset at +(channelNo / 20.0)
36  */
37 class AudioTestData
38 {
39 public:
AudioTestData(double rate,int channels)40     AudioTestData(double rate, int channels) :
41         m_channelCount(channels),
42         m_duration(2.0),
43         m_sampleRate(rate),
44         m_sinFreq(600.0),
45         m_pulseFreq(2)
46     {
47         m_frameCount = lrint(m_duration * m_sampleRate);
48         m_data = new float[m_frameCount * m_channelCount];
49         m_pulseWidth = 0.01 * m_sampleRate;
50         generate();
51     }
52 
~AudioTestData()53     ~AudioTestData() {
54         delete[] m_data;
55     }
56 
generate()57     void generate() {
58 
59         double hpw = m_pulseWidth / 2.0;
60 
61         for (int i = 0; i < m_frameCount; ++i) {
62             for (int c = 0; c < m_channelCount; ++c) {
63 
64                 double s = 0.0;
65 
66                 if (c == 0) {
67 
68                     double phase = (i * m_sinFreq * 2.0 * M_PI) / m_sampleRate;
69                     s = sin(phase);
70 
71                 } else if (c == 1) {
72 
73                     int pulseNo = int((i * m_pulseFreq) / m_sampleRate);
74                     int index = int(round((i * m_pulseFreq) -
75                                           (m_sampleRate * pulseNo)));
76                     if (index < m_pulseWidth) {
77                         s = 1.0 - fabs(hpw - index) / hpw;
78                         if (pulseNo % 2) s = -s;
79                     }
80 
81                 } else {
82 
83                     s = c / 20.0;
84                 }
85 
86                 m_data[i * m_channelCount + c] = float(s);
87             }
88         }
89     }
90 
getInterleavedData()91     float *getInterleavedData() const {
92         return m_data;
93     }
94 
getFrameCount()95     sv_frame_t getFrameCount() const {
96         return m_frameCount;
97     }
98 
getChannelCount()99     int getChannelCount() const {
100         return m_channelCount;
101     }
102 
getSampleRate()103     sv_samplerate_t getSampleRate () const {
104         return m_sampleRate;
105     }
106 
getDuration()107     double getDuration() const { // seconds
108         return m_duration;
109     }
110 
111 private:
112     float *m_data;
113     sv_frame_t m_frameCount;
114     int m_channelCount;
115     double m_duration;
116     sv_samplerate_t m_sampleRate;
117     double m_sinFreq;
118     double m_pulseFreq;
119     double m_pulseWidth;
120 };
121 
122 #endif
123 
124