changeset 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 ccd3c927638b
children ce2b123fc2de
files audioio/AudioGenerator.cpp audioio/AudioGenerator.h audioio/ContinuousSynth.cpp audioio/ContinuousSynth.h
diffstat 4 files changed, 54 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/audioio/AudioGenerator.cpp	Thu Jan 09 21:31:54 2014 +0000
+++ b/audioio/AudioGenerator.cpp	Sun Jan 12 05:12:08 2014 -0500
@@ -48,6 +48,7 @@
 AudioGenerator::AudioGenerator() :
     m_sourceSampleRate(0),
     m_targetChannelCount(1),
+	m_waveType(0),
     m_soloing(false)
 {
     initialiseSampleDir();
@@ -226,7 +227,8 @@
 
     ContinuousSynth *synth = new ContinuousSynth(m_targetChannelCount,
                                                  m_sourceSampleRate,
-                                                 m_processingBlockSize);
+                                                 m_processingBlockSize,
+                                                 m_waveType);
 
     std::cerr << "AudioGenerator::makeSynthFor(" << model << "): created synth" << std::endl;
 
--- a/audioio/AudioGenerator.h	Thu Jan 09 21:31:54 2014 +0000
+++ b/audioio/AudioGenerator.h	Sun Jan 12 05:12:08 2014 -0500
@@ -99,6 +99,7 @@
 protected:
     size_t m_sourceSampleRate;
     size_t m_targetChannelCount;
+    size_t m_waveType;
 
     bool m_soloing;
     std::set<Model *> m_soloModelSet;
--- a/audioio/ContinuousSynth.cpp	Thu Jan 09 21:31:54 2014 +0000
+++ b/audioio/ContinuousSynth.cpp	Sun Jan 12 05:12:08 2014 -0500
@@ -19,12 +19,13 @@
 
 #include <cmath>
 
-ContinuousSynth::ContinuousSynth(int channels, int sampleRate, int blockSize) :
+ContinuousSynth::ContinuousSynth(int channels, int sampleRate, int blockSize, int waveType) :
     m_channels(channels),
     m_sampleRate(sampleRate),
     m_blockSize(blockSize),
     m_prevF0(-1.f),
-    m_phase(0.0)
+    m_phase(0.0),
+	m_wavetype(waveType) // 0: 3 sinusoids, 1: 1 sinusoid, 2: sawtooth, 3: square
 {
 }
 
@@ -76,16 +77,55 @@
 
         double phasor = (fHere * 2 * M_PI) / m_sampleRate;
     
-	m_phase = m_phase + phasor;
+        m_phase = m_phase + phasor;
 
         int harmonics = (m_sampleRate / 4) / fHere - 1;
         if (harmonics < 1) harmonics = 1;
 
+    	switch (m_wavetype) {
+			case 1:
+				harmonics = 1;
+				break;
+			case 2:
+				break;
+			case 3:
+				break;
+			default:
+				harmonics = 3;
+				break;
+    	}
+
+
         for (int h = 0; h < harmonics; ++h) {
-            
-            double hn = h*2 + 1;
-            double hp = m_phase * hn;
-            double v = sin(hp) / hn;
+
+        	double v = 0;
+        	double hn = 0;
+        	double hp = 0;
+
+        	switch (m_wavetype) {
+        		case 1: // single sinusoid
+        			v = sin(m_phase);
+        			break;
+        		case 2: // sawtooth
+        			if (h != 0) {
+						hn = h + 1;
+						hp = m_phase * hn;
+						v = -(1.0 / M_PI) * sin(hp) / hn;
+        			} else {
+        				v = 0.5;
+        			}
+        			break;
+        		case 3: // square
+                    hn = h*2 + 1;
+                    hp = m_phase * hn;
+                    v = sin(hp) / hn;
+                    break;
+        		default: // 3 sinusoids
+                    hn = h*2 + 1;
+                    hp = m_phase * hn;
+                    v = sin(hp) / hn;
+                    break;
+        	}
 
             if (!wasOn && i < fadeLength) {
                 // fade in
--- a/audioio/ContinuousSynth.h	Thu Jan 09 21:31:54 2014 +0000
+++ b/audioio/ContinuousSynth.h	Sun Jan 12 05:12:08 2014 -0500
@@ -24,7 +24,7 @@
 class ContinuousSynth
 {
 public:
-    ContinuousSynth(int channels, int sampleRate, int blockSize);
+    ContinuousSynth(int channels, int sampleRate, int blockSize, int waveType);
     ~ContinuousSynth();
     
     void setChannelCount(int channels);
@@ -56,6 +56,8 @@
 
     double m_prevF0;
     double m_phase;
+
+    int m_wavetype;
 };
 
 #endif