Simple synthesizer for STM32F401RE/STMstation.

Dependencies:   FastPWM mbed-dsp

This is a basic synthesizer to play music on the STM32F401RE Nucleo/STMstation development board:

/media/uploads/kkado/imgp1229.jpg

Please see the API documentation for further details.

Here's a demo of the synthesizer at work in a music composing program on the STMstation. This one is "Miku" by Anamanaguchi.

Revision:
1:db0c24aebb8a
Parent:
0:c5ca205c0a80
--- a/STMstation_synth.cpp	Mon Jul 03 08:16:42 2017 +0000
+++ b/STMstation_synth.cpp	Mon Jul 03 08:35:31 2017 +0000
@@ -7,18 +7,29 @@
 const uint8_t   freqLength = sizeof(freqs)/sizeof(freqs[0]);
 const float     pi = 3.14159265359;
 
+//Sampling rate, uncomment one line only!
+//const uint16_t FSAMP = 44100; const float period = 0.000023;
+//const uint16_t FSAMP = 22050; const float period = 0.000045;
+const uint16_t FSAMP = 11025; const float period = 0.000091;
+
 //Constructor
-STMstation_synth::STMstation_synth():
-    //FSAMP(44100), period(0.000023),       //<<Only uncomment one of these lines!
-    //FSAMP(22050), period(0.000045),       //<<
-    FSAMP(11025), period(0.000091),         //<<
-    tone(AUDIO_PIN,1)
+STMstation_synth::STMstation_synth():tone(AUDIO_PIN,1)
 {     
-    tone.pulsewidth_ticks(1);
-    tone.period_ticks(256);
-    sample.attach(this,&STMstation_synth::note2,period);
+    begin();
 }
 
+//Constructor
+STMstation_synth::STMstation_synth(PinName audio_pin):tone(audio_pin,1)
+{     
+    begin();
+}
+
+//Set up PWM and timer interrupt
+void STMstation_synth::begin(){
+    tone.pulsewidth_ticks(128);
+    tone.period_ticks(256);
+    sample.attach(this,&STMstation_synth::note,period);
+}
 
 //Calculate the coefficients
 void STMstation_synth::calc_coefs(int i){  
@@ -46,7 +57,7 @@
 }
 
 //Calculate vSum
-void STMstation_synth::calc_vSum2(){
+void STMstation_synth::calc_vSum(){
     uint8_t active = 0; //Number of active channels
     uint16_t accum = 0; //Sum of volume channel values
     bool over = 0;      //Is any value over 127?
@@ -131,7 +142,7 @@
 }
 
 //Calculate values
-void STMstation_synth::calc_val2(){
+void STMstation_synth::calc_val(){
     Master.val = 128;
     for(int i=0; i<CHANNELS; i++){
         if(Master.notes[i] != NULL){
@@ -216,17 +227,17 @@
 void STMstation_synth::check_start(){
     for(int i=0; i<CHANNELS; i++){
         if(Master.counter[i] == 0){
-            calc_vSum2();
+            calc_vSum();
             calc_coefs(i);
         }
     }
 }
 
 //Play dat funky music
-void STMstation_synth::note2(){
+void STMstation_synth::note(){
     check_start();
     calc_env();
-    calc_val2();
+    calc_val();
     tone.pulsewidth_ticks(Master.val);
     check_end();
 }
@@ -275,7 +286,7 @@
             }            
         }
     }
-    calc_vSum2();
+    calc_vSum();
     for(uint16_t i=0; i<CHANNELS; i++){
         if(Master.notes[i]!=NULL){
             calc_coefs(i);