comparison audioio/ContinuousSynth.cpp @ 318:8611eb7be689 tonioni

Square wave
author Chris Cannam
date Thu, 09 Jan 2014 16:00:15 +0000
parents 65b75e23bbd5
children 5c69d40a0e30
comparison
equal deleted inserted replaced
317:abfde177731f 318:8611eb7be689
13 */ 13 */
14 14
15 #include "ContinuousSynth.h" 15 #include "ContinuousSynth.h"
16 16
17 #include "base/Debug.h" 17 #include "base/Debug.h"
18 #include "system/System.h"
18 19
19 #include <cmath> 20 #include <cmath>
20 21
21 ContinuousSynth::ContinuousSynth(int channels, int sampleRate, int blockSize) : 22 ContinuousSynth::ContinuousSynth(int channels, int sampleRate, int blockSize) :
22 m_channels(channels), 23 m_channels(channels),
73 fHere = m_prevF0 + ((f0 - m_prevF0) * i) / fadeLength; 74 fHere = m_prevF0 + ((f0 - m_prevF0) * i) / fadeLength;
74 } 75 }
75 76
76 double phasor = (fHere * 2 * M_PI) / m_sampleRate; 77 double phasor = (fHere * 2 * M_PI) / m_sampleRate;
77 78
78 // cerr << "phasor = " << phasor << endl;
79
80 m_phase = m_phase + phasor; 79 m_phase = m_phase + phasor;
81
82 double v = sin(m_phase);
83 80
84 if (!wasOn && i < fadeLength) { 81 int harmonics = (m_sampleRate / 4) / fHere - 1;
85 // fade in 82 if (harmonics < 1) harmonics = 1;
86 v = v * (i / double(fadeLength));
87 } else if (!nowOn) {
88 // fade out
89 if (i > fadeLength) v = 0;
90 else v = v * (1.0 - (i / double(fadeLength)));
91 }
92 83
93 for (int c = 0; c < m_channels; ++c) { 84 for (int h = 0; h < harmonics; ++h) {
94 toBuffers[c][i] += levels[c] * v; 85
95 } 86 double hn = h*2 + 1;
87 double hp = m_phase * hn;
88 double v = sin(hp) / hn;
89
90 if (!wasOn && i < fadeLength) {
91 // fade in
92 v = v * (i / double(fadeLength));
93 } else if (!nowOn) {
94 // fade out
95 if (i > fadeLength) v = 0;
96 else v = v * (1.0 - (i / double(fadeLength)));
97 }
98
99 for (int c = 0; c < m_channels; ++c) {
100 toBuffers[c][i] += levels[c] * v;
101 }
102 }
96 } 103 }
97 104
98 m_prevF0 = f0; 105 m_prevF0 = f0;
99 106
100 delete[] levels; 107 delete[] levels;