Mercurial > hg > svapp
comparison audioio/ContinuousSynth.cpp @ 323:5c69d40a0e30 tonioni
Added alternate waveforms for sonification. Created parameter m_wavetype in ContinuousSynth.
author | Rachel Bittner <rmb456@nyu.edu> |
---|---|
date | Sun, 12 Jan 2014 05:12:08 -0500 |
parents | 8611eb7be689 |
children | 7bdfbaa8d93f |
comparison
equal
deleted
inserted
replaced
319:ccd3c927638b | 323:5c69d40a0e30 |
---|---|
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) : | 22 ContinuousSynth::ContinuousSynth(int channels, int sampleRate, int 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.f), |
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 { | 29 { |
29 } | 30 } |
30 | 31 |
31 ContinuousSynth::~ContinuousSynth() | 32 ContinuousSynth::~ContinuousSynth() |
32 { | 33 { |
74 fHere = m_prevF0 + ((f0 - m_prevF0) * i) / fadeLength; | 75 fHere = m_prevF0 + ((f0 - m_prevF0) * i) / fadeLength; |
75 } | 76 } |
76 | 77 |
77 double phasor = (fHere * 2 * M_PI) / m_sampleRate; | 78 double phasor = (fHere * 2 * M_PI) / m_sampleRate; |
78 | 79 |
79 m_phase = m_phase + phasor; | 80 m_phase = m_phase + phasor; |
80 | 81 |
81 int harmonics = (m_sampleRate / 4) / fHere - 1; | 82 int harmonics = (m_sampleRate / 4) / fHere - 1; |
82 if (harmonics < 1) harmonics = 1; | 83 if (harmonics < 1) harmonics = 1; |
83 | 84 |
85 switch (m_wavetype) { | |
86 case 1: | |
87 harmonics = 1; | |
88 break; | |
89 case 2: | |
90 break; | |
91 case 3: | |
92 break; | |
93 default: | |
94 harmonics = 3; | |
95 break; | |
96 } | |
97 | |
98 | |
84 for (int h = 0; h < harmonics; ++h) { | 99 for (int h = 0; h < harmonics; ++h) { |
85 | 100 |
86 double hn = h*2 + 1; | 101 double v = 0; |
87 double hp = m_phase * hn; | 102 double hn = 0; |
88 double v = sin(hp) / hn; | 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*2 + 1; | |
125 hp = m_phase * hn; | |
126 v = sin(hp) / hn; | |
127 break; | |
128 } | |
89 | 129 |
90 if (!wasOn && i < fadeLength) { | 130 if (!wasOn && i < fadeLength) { |
91 // fade in | 131 // fade in |
92 v = v * (i / double(fadeLength)); | 132 v = v * (i / double(fadeLength)); |
93 } else if (!nowOn) { | 133 } else if (!nowOn) { |