Chris@756: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
Chris@756: 
Chris@756: /*
Chris@756:     Sonic Visualiser
Chris@756:     An audio file viewer and annotation editor.
Chris@756:     Centre for Digital Music, Queen Mary, University of London.
Chris@756:     This file copyright 2013 Chris Cannam.
Chris@756:     
Chris@756:     This program is free software; you can redistribute it and/or
Chris@756:     modify it under the terms of the GNU General Public License as
Chris@756:     published by the Free Software Foundation; either version 2 of the
Chris@756:     License, or (at your option) any later version.  See the file
Chris@756:     COPYING included with this distribution for more information.
Chris@756: */
Chris@756: 
Chris@756: #ifndef AUDIO_TEST_DATA_H
Chris@756: #define AUDIO_TEST_DATA_H
Chris@756: 
Chris@756: #include <cmath>
Chris@756: 
Chris@1040: #include "base/BaseTypes.h"
Chris@1040: 
Chris@756: /**
Chris@756:  * Class that generates a single fixed test pattern to a given sample
Chris@756:  * rate and number of channels.
Chris@756:  *
Chris@756:  * The test pattern is two seconds long and consists of:
Chris@756:  *
Chris@756:  * -- in channel 0, a 600Hz sinusoid with peak amplitude 1.0
Chris@756:  *
Chris@756:  * -- in channel 1, four triangular forms with peaks at +1.0, -1.0,
Chris@756:  *    +1.0, -1.0 respectively, of 10ms width, starting at 0.0, 0.5,
Chris@756:  *    1.0 and 1.5 seconds; silence elsewhere
Chris@756:  *
Chris@756:  * -- in subsequent channels, a flat DC offset at +(channelNo / 20.0)
Chris@756:  */
Chris@756: class AudioTestData
Chris@756: {
Chris@756: public:
Chris@1040:     AudioTestData(double rate, int channels) :
Chris@1429:         m_channelCount(channels),
Chris@1429:         m_duration(2.0),
Chris@1429:         m_sampleRate(rate),
Chris@1429:         m_sinFreq(600.0),
Chris@1429:         m_pulseFreq(2)
Chris@756:     {
Chris@1429:         m_frameCount = lrint(m_duration * m_sampleRate);
Chris@1429:         m_data = new float[m_frameCount * m_channelCount];
Chris@1429:         m_pulseWidth = 0.01 * m_sampleRate;
Chris@1429:         generate();
Chris@756:     }
Chris@756: 
Chris@756:     ~AudioTestData() {
Chris@1429:         delete[] m_data;
Chris@756:     }
Chris@756: 
Chris@756:     void generate() {
Chris@756: 
Chris@1429:         double hpw = m_pulseWidth / 2.0;
Chris@756: 
Chris@1429:         for (int i = 0; i < m_frameCount; ++i) {
Chris@1429:             for (int c = 0; c < m_channelCount; ++c) {
Chris@756: 
Chris@1429:                 double s = 0.0;
Chris@756: 
Chris@1429:                 if (c == 0) {
Chris@756: 
Chris@1429:                     double phase = (i * m_sinFreq * 2.0 * M_PI) / m_sampleRate;
Chris@1429:                     s = sin(phase);
Chris@756: 
Chris@1429:                 } else if (c == 1) {
Chris@756: 
Chris@1429:                     int pulseNo = int((i * m_pulseFreq) / m_sampleRate);
Chris@1429:                     int index = int(round((i * m_pulseFreq) -
Chris@1040:                                           (m_sampleRate * pulseNo)));
Chris@1429:                     if (index < m_pulseWidth) {
Chris@1429:                         s = 1.0 - fabs(hpw - index) / hpw;
Chris@1429:                         if (pulseNo % 2) s = -s;
Chris@1429:                     }
Chris@756: 
Chris@1429:                 } else {
Chris@756: 
Chris@1429:                     s = c / 20.0;
Chris@1429:                 }
Chris@756: 
Chris@1429:                 m_data[i * m_channelCount + c] = float(s);
Chris@1429:             }
Chris@1429:         }
Chris@756:     }
Chris@756: 
Chris@756:     float *getInterleavedData() const {
Chris@1429:         return m_data;
Chris@756:     }
Chris@756: 
Chris@1040:     sv_frame_t getFrameCount() const { 
Chris@1429:         return m_frameCount;
Chris@756:     }
Chris@756: 
Chris@756:     int getChannelCount() const {
Chris@1429:         return m_channelCount;
Chris@756:     }
Chris@756: 
Chris@1040:     sv_samplerate_t getSampleRate () const {
Chris@1429:         return m_sampleRate;
Chris@756:     }
Chris@756: 
Chris@1040:     double getDuration() const { // seconds
Chris@1429:         return m_duration;
Chris@756:     }
Chris@756: 
Chris@756: private:
Chris@756:     float *m_data;
Chris@1040:     sv_frame_t m_frameCount;
Chris@756:     int m_channelCount;
Chris@1040:     double m_duration;
Chris@1040:     sv_samplerate_t m_sampleRate;
Chris@1040:     double m_sinFreq;
Chris@1040:     double m_pulseFreq;
Chris@1040:     double m_pulseWidth;
Chris@756: };
Chris@756: 
Chris@756: #endif
Chris@756: