File indexing completed on 2024-04-28 15:08:06

0001 /* miniSynth - A Simple Software Synthesizer
0002    SPDX-FileCopyrightText: 2015 Ville Räisänen <vsr at vsr.name>
0003 
0004    SPDX-License-Identifier: GPL-3.0-or-later
0005 */
0006 
0007 #include "linearSynthesis.h"
0008 #include <QDebug>
0009 
0010 LinearSynthesis::LinearSynthesis(unsigned int mode, unsigned int size) :
0011     Waveform(mode, size) {
0012 
0013     numHarmonics = 16;
0014 
0015     timbreAmplitudes = new int[numHarmonics];
0016     timbrePhases     = new int[numHarmonics];
0017 
0018     timbreAmplitudes[0] = 100;
0019 }
0020 
0021 void
0022 LinearSynthesis::setTimbre(QVector<int> &amplitudes, QVector<int> &phases) {
0023     Q_ASSERT(amplitudes.size() == phases.size());
0024 
0025     delete[] timbreAmplitudes;
0026     delete[] timbrePhases;
0027 
0028     numHarmonics = amplitudes.size();
0029     timbreAmplitudes = new int[numHarmonics];
0030     timbrePhases = new int[numHarmonics];
0031     for(int i = 0 ; i < numHarmonics ; ++ i) {
0032         timbreAmplitudes[i] = amplitudes[i];
0033         timbrePhases[i] = phases[i];
0034     }
0035 }
0036 
0037 LinearSynthesis::~LinearSynthesis() {
0038     delete[] timbreAmplitudes;
0039     delete[] timbrePhases;
0040 }
0041 
0042 qreal
0043 LinearSynthesis::evalTimbre(qreal t) {
0044     qreal val = 0;
0045     for (int harm = 0; harm < numHarmonics; harm++) {
0046         int qa_int = timbreAmplitudes[harm];
0047         int qp_int = timbrePhases[harm];
0048 
0049         if (qa_int > 0) {
0050             qreal qa = (qreal)qa_int/100;
0051             qreal qp = (2*M_PI*(qreal)qp_int)/360;
0052 
0053             val += qa * eval(((qreal)harm + 1) * t - qp);
0054         }
0055     }
0056     return val;
0057 }