comparison audioio/ContinuousSynth.cpp @ 436:72c662fe7ea3 cxx11

Further dedicated-types fixes
author Chris Cannam
date Tue, 10 Mar 2015 17:02:52 +0000
parents d2c13ec0f148
children aa6fb3516e28
comparison
equal deleted inserted replaced
435:618d5816b04d 436:72c662fe7ea3
17 #include "base/Debug.h" 17 #include "base/Debug.h"
18 #include "system/System.h" 18 #include "system/System.h"
19 19
20 #include <cmath> 20 #include <cmath>
21 21
22 ContinuousSynth::ContinuousSynth(int channels, int sampleRate, int blockSize, int waveType) : 22 ContinuousSynth::ContinuousSynth(int channels, sv_samplerate_t sampleRate, sv_frame_t blockSize, int waveType) :
23 m_channels(channels), 23 m_channels(channels),
24 m_sampleRate(sampleRate), 24 m_sampleRate(sampleRate),
25 m_blockSize(blockSize), 25 m_blockSize(blockSize),
26 m_prevF0(-1.f), 26 m_prevF0(-1.0),
27 m_phase(0.0), 27 m_phase(0.0),
28 m_wavetype(waveType) // 0: 3 sinusoids, 1: 1 sinusoid, 2: sawtooth, 3: square 28 m_wavetype(waveType) // 0: 3 sinusoids, 1: 1 sinusoid, 2: sawtooth, 3: square
29 { 29 {
30 } 30 }
31 31
38 { 38 {
39 m_phase = 0; 39 m_phase = 0;
40 } 40 }
41 41
42 void 42 void
43 ContinuousSynth::mix(float **toBuffers, float gain, float pan, float f0) 43 ContinuousSynth::mix(float **toBuffers, float gain, float pan, float f0f)
44 { 44 {
45 if (f0 == 0.f) f0 = m_prevF0; 45 double f0(f0f);
46 if (f0 == 0.0) f0 = m_prevF0;
46 47
47 bool wasOn = (m_prevF0 > 0.f); 48 bool wasOn = (m_prevF0 > 0.0);
48 bool nowOn = (f0 > 0.f); 49 bool nowOn = (f0 > 0.0);
49 50
50 if (!nowOn && !wasOn) { 51 if (!nowOn && !wasOn) {
51 m_phase = 0; 52 m_phase = 0;
52 return; 53 return;
53 } 54 }
54 55
55 int fadeLength = 100; // samples 56 sv_frame_t fadeLength = 100;
56 57
57 float *levels = new float[m_channels]; 58 float *levels = new float[m_channels];
58 59
59 for (int c = 0; c < m_channels; ++c) { 60 for (int c = 0; c < m_channels; ++c) {
60 levels[c] = gain * 0.5; // scale gain otherwise too loud compared to source 61 levels[c] = gain * 0.5f; // scale gain otherwise too loud compared to source
61 } 62 }
62 if (pan != 0.0 && m_channels == 2) { 63 if (pan != 0.0 && m_channels == 2) {
63 levels[0] *= 1.0 - pan; 64 levels[0] *= 1.0f - pan;
64 levels[1] *= pan + 1.0; 65 levels[1] *= pan + 1.0f;
65 } 66 }
66 67
67 // cerr << "ContinuousSynth::mix: f0 = " << f0 << " (from " << m_prevF0 << "), phase = " << m_phase << endl; 68 // cerr << "ContinuousSynth::mix: f0 = " << f0 << " (from " << m_prevF0 << "), phase = " << m_phase << endl;
68 69
69 for (int i = 0; i < m_blockSize; ++i) { 70 for (sv_frame_t i = 0; i < m_blockSize; ++i) {
70 71
71 double fHere = (nowOn ? f0 : m_prevF0); 72 double fHere = (nowOn ? f0 : m_prevF0);
72 73
73 if (wasOn && nowOn && (f0 != m_prevF0) && (i < fadeLength)) { 74 if (wasOn && nowOn && (f0 != m_prevF0) && (i < fadeLength)) {
74 // interpolate the frequency shift 75 // interpolate the frequency shift
75 fHere = m_prevF0 + ((f0 - m_prevF0) * i) / fadeLength; 76 fHere = m_prevF0 + ((f0 - m_prevF0) * double(i)) / double(fadeLength);
76 } 77 }
77 78
78 double phasor = (fHere * 2 * M_PI) / m_sampleRate; 79 double phasor = (fHere * 2 * M_PI) / m_sampleRate;
79 80
80 m_phase = m_phase + phasor; 81 m_phase = m_phase + phasor;
81 82
82 int harmonics = (m_sampleRate / 4) / fHere - 1; 83 int harmonics = int((m_sampleRate / 4) / fHere - 1);
83 if (harmonics < 1) harmonics = 1; 84 if (harmonics < 1) harmonics = 1;
84 85
85 switch (m_wavetype) { 86 switch (m_wavetype) {
86 case 1: 87 case 1:
87 harmonics = 1; 88 harmonics = 1;
92 break; 93 break;
93 default: 94 default:
94 harmonics = 3; 95 harmonics = 3;
95 break; 96 break;
96 } 97 }
97
98 98
99 for (int h = 0; h < harmonics; ++h) { 99 for (int h = 0; h < harmonics; ++h) {
100 100
101 double v = 0; 101 double v = 0;
102 double hn = 0; 102 double hn = 0;
127 break; 127 break;
128 } 128 }
129 129
130 if (!wasOn && i < fadeLength) { 130 if (!wasOn && i < fadeLength) {
131 // fade in 131 // fade in
132 v = v * (i / double(fadeLength)); 132 v = v * (double(i) / double(fadeLength));
133 } else if (!nowOn) { 133 } else if (!nowOn) {
134 // fade out 134 // fade out
135 if (i > fadeLength) v = 0; 135 if (i > fadeLength) v = 0;
136 else v = v * (1.0 - (i / double(fadeLength))); 136 else v = v * (1.0 - (double(i) / double(fadeLength)));
137 } 137 }
138 138
139 for (int c = 0; c < m_channels; ++c) { 139 for (int c = 0; c < m_channels; ++c) {
140 toBuffers[c][i] += levels[c] * v; 140 toBuffers[c][i] += float(levels[c] * v);
141 } 141 }
142 } 142 }
143 } 143 }
144 144
145 m_prevF0 = f0; 145 m_prevF0 = f0;