annotate data/fileio/test/AudioTestData.h @ 1616:de446dd905e6 single-point

Rework EventSeries to explicitly store counts of events (+ add comments explaining, among other things, why)
author Chris Cannam
date Fri, 08 Mar 2019 10:16:12 +0000
parents 48e9f538e6e9
children
rev   line source
Chris@756 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@756 2
Chris@756 3 /*
Chris@756 4 Sonic Visualiser
Chris@756 5 An audio file viewer and annotation editor.
Chris@756 6 Centre for Digital Music, Queen Mary, University of London.
Chris@756 7 This file copyright 2013 Chris Cannam.
Chris@756 8
Chris@756 9 This program is free software; you can redistribute it and/or
Chris@756 10 modify it under the terms of the GNU General Public License as
Chris@756 11 published by the Free Software Foundation; either version 2 of the
Chris@756 12 License, or (at your option) any later version. See the file
Chris@756 13 COPYING included with this distribution for more information.
Chris@756 14 */
Chris@756 15
Chris@756 16 #ifndef AUDIO_TEST_DATA_H
Chris@756 17 #define AUDIO_TEST_DATA_H
Chris@756 18
Chris@756 19 #include <cmath>
Chris@756 20
Chris@1040 21 #include "base/BaseTypes.h"
Chris@1040 22
Chris@756 23 /**
Chris@756 24 * Class that generates a single fixed test pattern to a given sample
Chris@756 25 * rate and number of channels.
Chris@756 26 *
Chris@756 27 * The test pattern is two seconds long and consists of:
Chris@756 28 *
Chris@756 29 * -- in channel 0, a 600Hz sinusoid with peak amplitude 1.0
Chris@756 30 *
Chris@756 31 * -- in channel 1, four triangular forms with peaks at +1.0, -1.0,
Chris@756 32 * +1.0, -1.0 respectively, of 10ms width, starting at 0.0, 0.5,
Chris@756 33 * 1.0 and 1.5 seconds; silence elsewhere
Chris@756 34 *
Chris@756 35 * -- in subsequent channels, a flat DC offset at +(channelNo / 20.0)
Chris@756 36 */
Chris@756 37 class AudioTestData
Chris@756 38 {
Chris@756 39 public:
Chris@1040 40 AudioTestData(double rate, int channels) :
Chris@1429 41 m_channelCount(channels),
Chris@1429 42 m_duration(2.0),
Chris@1429 43 m_sampleRate(rate),
Chris@1429 44 m_sinFreq(600.0),
Chris@1429 45 m_pulseFreq(2)
Chris@756 46 {
Chris@1429 47 m_frameCount = lrint(m_duration * m_sampleRate);
Chris@1429 48 m_data = new float[m_frameCount * m_channelCount];
Chris@1429 49 m_pulseWidth = 0.01 * m_sampleRate;
Chris@1429 50 generate();
Chris@756 51 }
Chris@756 52
Chris@756 53 ~AudioTestData() {
Chris@1429 54 delete[] m_data;
Chris@756 55 }
Chris@756 56
Chris@756 57 void generate() {
Chris@756 58
Chris@1429 59 double hpw = m_pulseWidth / 2.0;
Chris@756 60
Chris@1429 61 for (int i = 0; i < m_frameCount; ++i) {
Chris@1429 62 for (int c = 0; c < m_channelCount; ++c) {
Chris@756 63
Chris@1429 64 double s = 0.0;
Chris@756 65
Chris@1429 66 if (c == 0) {
Chris@756 67
Chris@1429 68 double phase = (i * m_sinFreq * 2.0 * M_PI) / m_sampleRate;
Chris@1429 69 s = sin(phase);
Chris@756 70
Chris@1429 71 } else if (c == 1) {
Chris@756 72
Chris@1429 73 int pulseNo = int((i * m_pulseFreq) / m_sampleRate);
Chris@1429 74 int index = int(round((i * m_pulseFreq) -
Chris@1040 75 (m_sampleRate * pulseNo)));
Chris@1429 76 if (index < m_pulseWidth) {
Chris@1429 77 s = 1.0 - fabs(hpw - index) / hpw;
Chris@1429 78 if (pulseNo % 2) s = -s;
Chris@1429 79 }
Chris@756 80
Chris@1429 81 } else {
Chris@756 82
Chris@1429 83 s = c / 20.0;
Chris@1429 84 }
Chris@756 85
Chris@1429 86 m_data[i * m_channelCount + c] = float(s);
Chris@1429 87 }
Chris@1429 88 }
Chris@756 89 }
Chris@756 90
Chris@756 91 float *getInterleavedData() const {
Chris@1429 92 return m_data;
Chris@756 93 }
Chris@756 94
Chris@1040 95 sv_frame_t getFrameCount() const {
Chris@1429 96 return m_frameCount;
Chris@756 97 }
Chris@756 98
Chris@756 99 int getChannelCount() const {
Chris@1429 100 return m_channelCount;
Chris@756 101 }
Chris@756 102
Chris@1040 103 sv_samplerate_t getSampleRate () const {
Chris@1429 104 return m_sampleRate;
Chris@756 105 }
Chris@756 106
Chris@1040 107 double getDuration() const { // seconds
Chris@1429 108 return m_duration;
Chris@756 109 }
Chris@756 110
Chris@756 111 private:
Chris@756 112 float *m_data;
Chris@1040 113 sv_frame_t m_frameCount;
Chris@756 114 int m_channelCount;
Chris@1040 115 double m_duration;
Chris@1040 116 sv_samplerate_t m_sampleRate;
Chris@1040 117 double m_sinFreq;
Chris@1040 118 double m_pulseFreq;
Chris@1040 119 double m_pulseWidth;
Chris@756 120 };
Chris@756 121
Chris@756 122 #endif
Chris@756 123