File indexing completed on 2024-04-28 05:43:15

0001 /***************************************************************************
0002  *   Copyright (C) 2003-2004 by David Saxton                               *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #include "elementsignal.h"
0012 #include <cmath>
0013 
0014 ElementSignal::ElementSignal()
0015 {
0016     m_type = ElementSignal::st_sinusoidal;
0017     m_time = 0.;
0018     m_frequency = 0.;
0019 }
0020 
0021 ElementSignal::~ElementSignal()
0022 {
0023 }
0024 
0025 void ElementSignal::setStep(Type type, double frequency)
0026 {
0027     m_type = type;
0028     m_frequency = frequency;
0029     m_omega = 2 * M_PI * m_frequency;
0030     m_time = 1. / (4. * m_frequency);
0031 }
0032 
0033 double ElementSignal::advance(double delta)
0034 {
0035     m_time += delta;
0036     if (m_time >= 1. / m_frequency)
0037         m_time -= 1. / m_frequency;
0038 
0039     switch (m_type) {
0040     case ElementSignal::st_sawtooth: {
0041         double val = (m_time * m_omega / M_PI);
0042         return 1 - remainder(val, 2);
0043     }
0044     case ElementSignal::st_square:
0045         return ((int(trunc(m_time * m_omega / M_PI)) & 1) == 0) ? 1 : -1;
0046     case ElementSignal::st_triangular:
0047         // TODO Triangular signal
0048         return 0.;
0049     case ElementSignal::st_sinusoidal:
0050     default:
0051         return sin(m_time * m_omega);
0052     }
0053 }