annotate data/fileio/test/AudioTestData.h @ 997:2104ea2204d2

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