Mercurial > hg > svapp
comparison audio/ContinuousSynth.cpp @ 515:51befd6165a3 alignment-simple
Merge in from SV 3.0-integration branches
author | Chris Cannam |
---|---|
date | Wed, 02 Mar 2016 17:25:27 +0000 |
parents | 56acd9368532 |
children |
comparison
equal
deleted
inserted
replaced
459:74d575708e06 | 515:51befd6165a3 |
---|---|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 /* | |
4 Sonic Visualiser | |
5 An audio file viewer and annotation editor. | |
6 Centre for Digital Music, Queen Mary, University of London. | |
7 | |
8 This program is free software; you can redistribute it and/or | |
9 modify it under the terms of the GNU General Public License as | |
10 published by the Free Software Foundation; either version 2 of the | |
11 License, or (at your option) any later version. See the file | |
12 COPYING included with this distribution for more information. | |
13 */ | |
14 | |
15 #include "ContinuousSynth.h" | |
16 | |
17 #include "base/Debug.h" | |
18 #include "system/System.h" | |
19 | |
20 #include <cmath> | |
21 | |
22 ContinuousSynth::ContinuousSynth(int channels, sv_samplerate_t sampleRate, sv_frame_t blockSize, int waveType) : | |
23 m_channels(channels), | |
24 m_sampleRate(sampleRate), | |
25 m_blockSize(blockSize), | |
26 m_prevF0(-1.0), | |
27 m_phase(0.0), | |
28 m_wavetype(waveType) // 0: 3 sinusoids, 1: 1 sinusoid, 2: sawtooth, 3: square | |
29 { | |
30 } | |
31 | |
32 ContinuousSynth::~ContinuousSynth() | |
33 { | |
34 } | |
35 | |
36 void | |
37 ContinuousSynth::reset() | |
38 { | |
39 m_phase = 0; | |
40 } | |
41 | |
42 void | |
43 ContinuousSynth::mix(float **toBuffers, float gain, float pan, float f0f) | |
44 { | |
45 double f0(f0f); | |
46 if (f0 == 0.0) f0 = m_prevF0; | |
47 | |
48 bool wasOn = (m_prevF0 > 0.0); | |
49 bool nowOn = (f0 > 0.0); | |
50 | |
51 if (!nowOn && !wasOn) { | |
52 m_phase = 0; | |
53 return; | |
54 } | |
55 | |
56 sv_frame_t fadeLength = 100; | |
57 | |
58 float *levels = new float[m_channels]; | |
59 | |
60 for (int c = 0; c < m_channels; ++c) { | |
61 levels[c] = gain * 0.5f; // scale gain otherwise too loud compared to source | |
62 } | |
63 if (pan != 0.0 && m_channels == 2) { | |
64 levels[0] *= 1.0f - pan; | |
65 levels[1] *= pan + 1.0f; | |
66 } | |
67 | |
68 // cerr << "ContinuousSynth::mix: f0 = " << f0 << " (from " << m_prevF0 << "), phase = " << m_phase << endl; | |
69 | |
70 for (sv_frame_t i = 0; i < m_blockSize; ++i) { | |
71 | |
72 double fHere = (nowOn ? f0 : m_prevF0); | |
73 | |
74 if (wasOn && nowOn && (f0 != m_prevF0) && (i < fadeLength)) { | |
75 // interpolate the frequency shift | |
76 fHere = m_prevF0 + ((f0 - m_prevF0) * double(i)) / double(fadeLength); | |
77 } | |
78 | |
79 double phasor = (fHere * 2 * M_PI) / m_sampleRate; | |
80 | |
81 m_phase = m_phase + phasor; | |
82 | |
83 int harmonics = int((m_sampleRate / 4) / fHere - 1); | |
84 if (harmonics < 1) harmonics = 1; | |
85 | |
86 switch (m_wavetype) { | |
87 case 1: | |
88 harmonics = 1; | |
89 break; | |
90 case 2: | |
91 break; | |
92 case 3: | |
93 break; | |
94 default: | |
95 harmonics = 3; | |
96 break; | |
97 } | |
98 | |
99 for (int h = 0; h < harmonics; ++h) { | |
100 | |
101 double v = 0; | |
102 double hn = 0; | |
103 double hp = 0; | |
104 | |
105 switch (m_wavetype) { | |
106 case 1: // single sinusoid | |
107 v = sin(m_phase); | |
108 break; | |
109 case 2: // sawtooth | |
110 if (h != 0) { | |
111 hn = h + 1; | |
112 hp = m_phase * hn; | |
113 v = -(1.0 / M_PI) * sin(hp) / hn; | |
114 } else { | |
115 v = 0.5; | |
116 } | |
117 break; | |
118 case 3: // square | |
119 hn = h*2 + 1; | |
120 hp = m_phase * hn; | |
121 v = sin(hp) / hn; | |
122 break; | |
123 default: // 3 sinusoids | |
124 hn = h + 1; | |
125 hp = m_phase * hn; | |
126 v = sin(hp) / hn; | |
127 break; | |
128 } | |
129 | |
130 if (!wasOn && i < fadeLength) { | |
131 // fade in | |
132 v = v * (double(i) / double(fadeLength)); | |
133 } else if (!nowOn) { | |
134 // fade out | |
135 if (i > fadeLength) v = 0; | |
136 else v = v * (1.0 - (double(i) / double(fadeLength))); | |
137 } | |
138 | |
139 for (int c = 0; c < m_channels; ++c) { | |
140 toBuffers[c][i] += float(levels[c] * v); | |
141 } | |
142 } | |
143 } | |
144 | |
145 m_prevF0 = f0; | |
146 | |
147 delete[] levels; | |
148 } | |
149 |