Synthesizer based on the Unzen / Nucleo F746ZG

Dependencies:   amakusa mbed-dsp mbed shimabara ukifune unzen_nucleo_f746

Fork of skeleton_unzen_nucleo_f746 by seiichi horie

雲仙フレームワークのテストとして作っているプロジェクトです。中身はどんどん変っていきます。 説明はDSP空挺団の「シンセサイザー」カテゴリーを参照してください。初回は「ドッグフードを食べる」です。

Revision:
16:d4ea3e6a0bce
Parent:
15:de22b9d147e0
Child:
17:728ffc633179
--- a/vfo.cpp	Mon Jan 30 15:01:29 2017 +0000
+++ b/vfo.cpp	Tue Jan 31 12:52:59 2017 +0000
@@ -8,6 +8,8 @@
     this->Fs = 48000;
     this->frequency = 440;
     this->duty_cycle = 0.5;
+    
+    this->update_parameters();
 }   // End of constructor()
     
     
@@ -24,12 +26,17 @@
         if ( this->style == square ) 
         {
             if ( this->current_phase < this->half_way )
-                out_buffer[i] = 0.5;
+                out_buffer[i] = 1.0;
             else
                 out_buffer[i] = 0.0;
         }
-        
-            // to do, triangle.
+        else    // style == triangle
+        {
+            if ( this->current_phase < this->half_way )
+                out_buffer[i] = this->rising_rate * this->current_phase;
+            else
+                out_buffer[i] = 1 + this->falling_rate * ( this->current_phase - this->half_way );
+        }
         
             // update phase
         this->current_phase += this->frequency;
@@ -46,13 +53,17 @@
     if ( Fs != 32000 && Fs != 44100 && Fs != 96000 && Fs != 48000 )
         Fs = 48000;
     this->Fs = Fs;
+    
+    this->update_parameters();
 }
 
 void VFO::set_frequency( int freq )
 {
     if ( freq > this->Fs / 4 )
-        freq = Fs/4;
+        freq = Fs / 4;
     this->frequency = freq;
+    
+    this->update_parameters();
 }
 
 void VFO::set_duty_cycle( float duty )
@@ -62,6 +73,8 @@
     if ( duty < 0.01f )  // low limit
         duty = 0.01f;
     this->duty_cycle = duty;
+    
+    this->update_parameters();
 }
 
 void VFO::set_wave_style( wave_style style )
@@ -70,4 +83,19 @@
 }
 
 
-           
+    // update the internal parameter by given parameters
+void VFO::update_parameters(void)
+{
+        // calc the half_way;
+    this-> half_way = this->frequency * this-> duty_cycle;
+
+        // forbid to be zero.
+    if ( this-> half_way < this->frequency )
+        half_way = this->frequency;              
+        
+        // for triangle wave;
+    this->rising_rate = 1.0 / this->half_way;
+    
+    this->falling_rate = - 1.0 / ( this->Fs - this->half_way ); 
+}
+