annotate data/fileio/test/AudioTestData.h @ 1162:1dd98a5432cf

Work around libsndfile-1.0.26 bug that reports all files as non-seekable
author Chris Cannam
date Wed, 24 Feb 2016 11:35:51 +0000
parents a1cd5abcb38b
children 48e9f538e6e9
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@756 41 m_channelCount(channels),
Chris@756 42 m_duration(2.0),
Chris@756 43 m_sampleRate(rate),
Chris@756 44 m_sinFreq(600.0),
Chris@756 45 m_pulseFreq(2)
Chris@756 46 {
Chris@756 47 m_frameCount = lrint(m_duration * m_sampleRate);
Chris@756 48 m_data = new float[m_frameCount * m_channelCount];
Chris@756 49 m_pulseWidth = 0.01 * m_sampleRate;
Chris@756 50 generate();
Chris@756 51 }
Chris@756 52
Chris@756 53 ~AudioTestData() {
Chris@756 54 delete[] m_data;
Chris@756 55 }
Chris@756 56
Chris@756 57 void generate() {
Chris@756 58
Chris@1040 59 double hpw = m_pulseWidth / 2.0;
Chris@756 60
Chris@756 61 for (int i = 0; i < m_frameCount; ++i) {
Chris@756 62 for (int c = 0; c < m_channelCount; ++c) {
Chris@756 63
Chris@1040 64 double s = 0.0;
Chris@756 65
Chris@756 66 if (c == 0) {
Chris@756 67
Chris@1040 68 double phase = (i * m_sinFreq * 2.0 * M_PI) / m_sampleRate;
Chris@1040 69 s = sin(phase);
Chris@756 70
Chris@756 71 } else if (c == 1) {
Chris@756 72
Chris@756 73 int pulseNo = int((i * m_pulseFreq) / m_sampleRate);
Chris@1040 74 int index = int(round((i * m_pulseFreq) -
Chris@1040 75 (m_sampleRate * pulseNo)));
Chris@756 76 if (index < m_pulseWidth) {
Chris@1040 77 s = 1.0 - fabs(hpw - index) / hpw;
Chris@756 78 if (pulseNo % 2) s = -s;
Chris@756 79 }
Chris@756 80
Chris@756 81 } else {
Chris@756 82
Chris@756 83 s = c / 20.0;
Chris@756 84 }
Chris@756 85
Chris@1040 86 m_data[i * m_channelCount + c] = float(s);
Chris@756 87 }
Chris@756 88 }
Chris@756 89 }
Chris@756 90
Chris@756 91 float *getInterleavedData() const {
Chris@756 92 return m_data;
Chris@756 93 }
Chris@756 94
Chris@1040 95 sv_frame_t getFrameCount() const {
Chris@756 96 return m_frameCount;
Chris@756 97 }
Chris@756 98
Chris@756 99 int getChannelCount() const {
Chris@756 100 return m_channelCount;
Chris@756 101 }
Chris@756 102
Chris@1040 103 sv_samplerate_t getSampleRate () const {
Chris@756 104 return m_sampleRate;
Chris@756 105 }
Chris@756 106
Chris@1040 107 double getDuration() const { // seconds
Chris@756 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