Browse Source

Major fixes in MSynth. Interrupt works. 2 Examples.

akshaycadambi 5 years ago
parent
commit
62f926f0e2
4 changed files with 93 additions and 19 deletions
  1. 27 19
      MSynth.h
  2. 2 0
      TimerOne.h
  3. 32 0
      examples/01-BareMinimum.ino
  4. 32 0
      examples/02-SineOscillator.ino

+ 27 - 19
MSynth.h

@@ -16,51 +16,59 @@
 
 #include "TimerOne.h"
 #include "ugens.h"
+#include "wiring_private.h"
 
 #define AUDIO_OUTPUT_REGISTER OCR1A
 
 using namespace ugen;
 
 // This function will defined by the user in the Ardunino Sketch
-inline sample audioLoop(void);
+sample audioLoop(void);
 
-inline void audioISR(void){
-    /*
-     * Writes audio to port.
-     * This function is called by the Timer1 Routine within MSynthMaster
-     * This is simply an non-exposed alias for audioLoop()
-     * */
+inline void audioISR(void)
+{
+   /*
+    * Writes audio to port.
+    * This is the default audio-rate ISR and is a 'hidden' alias for audioLoop()
+    * Set within MSynthMaster.
+    *
+    * TODO: Add a global DC_OFFSET for easier amplitude modulation
+    * */
 
-    // TODO: Range conversion
     AUDIO_OUTPUT_REGISTER = audioLoop();
 }
 
+
 class MSynthMaster
 {
     /*
      * Used to automate the ISR and provide an interface between the sketch
      * and the backend.
-     *
-     * Note:
-     *  The interrupt service routine MUST be the audioISR defined in this file.
      * */
 public:
     MSynthMaster(void)
     {
-        _ISRfrequency = SystemSR; // SystemSR is defined in table.h
-        initTimer();
+        _ISRfrequency = SystemSR;
     }
 
-    void initTimer()
+    MSynthMaster(long int SR)
     {
-        int ISRPeriod_inMicroseconds = 1000000 / (double) _ISRfrequency ;
+        // TODO: Find a way to integrate this into everything
+        // This doesn't really affect the oscillators yet.
+        _ISRfrequency = SR;
+    }
+
+    void init(void)
+    {
+        long int ISRPeriod_inMicroseconds = 1000000 / (double) _ISRfrequency ;
+
+        pinMode(9, OUTPUT);
         Timer1.initialize( ISRPeriod_inMicroseconds );
-        Timer1.attachInterrupt( audioISR );
+        Timer1.attachInterrupt(audioISR);
+        sbi(TCCR1A, COM1A1);                // Associates Pin 9 with the Timer1 Output
     }
 
-private:
-    long int _ISRfrequency;
-    TimerOne Timer1;
+    long int _ISRfrequency;                 // Audio Rate
 
 } MSynth;
 

+ 2 - 0
TimerOne.h

@@ -66,4 +66,6 @@ class TimerOne
     void (*isrCallback)();
 };
 
+extern TimerOne Timer1;
+
 #endif

+ 32 - 0
examples/01-BareMinimum.ino

@@ -0,0 +1,32 @@
+/* 
+Example 1: Bare Minimum
+-----------------------
+This example simply initializes MSynth. This is the bare minimum code for including MSynth in your project. 
+
+MSynth: A modular-synthesis library for Arduino
+Author: Akshay Cadambi (akshay19.92@gmail.com)
+*/
+
+#include "MSynth.h"
+
+// Different from traditional 'loop' function in arduino sketches. 
+sample audioLoop(void)
+{	
+	sample out; 			// To hold the output. Pass all audio to this variable.
+
+	// This is where the audio code goes. 
+
+	return out;
+}
+
+
+void setup()
+{
+	// Setup code
+    MSynth.init();
+}
+
+void loop()
+{
+	// Regular loop code. 
+}

+ 32 - 0
examples/02-SineOscillator.ino

@@ -0,0 +1,32 @@
+/* 
+Example 2: Sine Oscillator
+--------------------------
+This example shows you how to output a Sine-wave. 
+
+
+MSynth: A modular-synthesis library for Arduino
+Author: Akshay Cadambi (akshay19.92@gmail.com)
+*/
+
+sample audioLoop(void)
+{	
+	sample out;			
+
+	Sine osc(440.0);	// Sine-oscillator of frequency 440Hz
+
+	out = osc();		// Send the sine-wave to the output
+    return out;
+}
+
+
+void setup()
+{
+    MSynth.init();
+}
+
+void loop()
+{
+
+}
+
+