Browse Source

First commit

akshaycadambi 5 years ago
commit
9150c4595b
4 changed files with 219 additions and 0 deletions
  1. 40 0
      oscillator.cpp
  2. 42 0
      oscillator.h
  3. 102 0
      table.cpp
  4. 35 0
      table.h

+ 40 - 0
oscillator.cpp

@@ -0,0 +1,40 @@
+#include <iostream>
+#include "oscillator.h"
+#include "math.h"
+
+double oscillator::WavetableSynth::freqMod(float freq)
+{
+    phase_inc = floor( frequency * (double) Tables::table_length() / SystemSR );
+    return this->tick();
+}
+
+double oscillator::WavetableSynth::tick()
+{
+    current_phase += phase_inc;
+    current_phase = current_phase & Tables::phase_mask();
+    return *(tbl + current_phase - phase_inc);
+}
+
+void oscillator::WavetableSynth::setFreq(float freq)
+{
+    frequency = freq;
+    phase_inc = lrint( frequency * (double) Tables::table_length() / SystemSR );
+}
+
+oscillator::WavetableSynth::WavetableSynth()
+{
+    current_phase = 0;
+    tbl = NULL;
+}
+
+oscillator::Sine::Sine(float freq)
+{
+    setFreq(freq);
+    tbl = Tables::sine();
+}
+
+oscillator::Saw::Saw(float freq)
+{
+    setFreq(freq);
+    tbl = Tables::saw();
+}

+ 42 - 0
oscillator.h

@@ -0,0 +1,42 @@
+#ifndef OSCILLATOR
+#define OSCILLATOR
+
+#include <inttypes.h>
+#include <cstdlib>
+#include "table.h"
+#include "math.h"
+
+namespace oscillator{
+
+class WavetableSynth
+    {
+    public:
+        float frequency;
+        float phase;
+        int current_phase;
+        int phase_inc;
+        double *tbl;
+
+        WavetableSynth();
+        void setFreq(float freq);
+        double tick();
+        double freqMod(float freq);
+
+    protected:
+    };
+
+class Sine : public WavetableSynth
+    {
+    public:
+        Sine(float freq);
+    };
+
+class Saw: public WavetableSynth
+    {
+    public:
+        Saw(float freq);
+    };
+
+}
+
+#endif // OSCILLATOR

+ 102 - 0
table.cpp

@@ -0,0 +1,102 @@
+#include "table.h"
+#include "inttypes.h"
+#include <math.h>
+#include <stdlib.h>
+
+double * gen::sine(int len)
+{
+    double *SineTable = (double* ) malloc(sizeof(double) * len);
+    for (int i=0; i<len; i++)
+    {
+        *(SineTable+i) = sin(2*M_PI*i/len);
+    }
+    return SineTable;
+}
+
+double * gen::tri(int len)
+{
+    double *TriTable = (double* ) malloc(sizeof(double) * len);
+    for (int i=0; i<len; i++)
+    {
+        *TriTable = sin(2*M_PI*i/len);
+    }
+    return TriTable;
+}
+
+double * gen::saw(int len)
+{
+    double *SawTable = (double* ) malloc(sizeof(double) * len);
+    for (int i=0; i<len; i++)
+    {
+            *(SawTable+i) += sin(2*M_PI * i / len);
+    }
+    return SawTable;
+}
+
+double * gen::square(int len)
+{
+    double *SquareTable = (double* ) malloc(sizeof(double) * len);
+    for (int i=0; i<len; i++)
+    {
+        *SquareTable = sin(2*M_PI*i/len);
+    }
+    return SquareTable;
+}
+
+//*****************************************************
+
+int Tables::table_length(void)
+{
+    static int table_length = DEFAULT_TABLE_LENGTH;
+    return table_length;
+}
+
+int Tables::phase_mask(void)
+{
+    static int table_length = DEFAULT_PHASE_MASK;
+    return table_length;
+}
+
+double * Tables::sine()
+{
+    static double* tbl_sine;
+
+    if (tbl_sine == NULL)
+       {
+           tbl_sine = gen::sine( Tables::table_length() );
+       }
+       return tbl_sine;
+}
+
+double * Tables::tri()
+{
+    static double* tbl_tri;
+
+    if (tbl_tri == NULL)
+       {
+           tbl_tri = gen::tri(Tables::table_length());
+       }
+       return tbl_tri;
+}
+
+double * Tables::saw()
+{
+    static double* tbl_saw;
+
+    if (tbl_saw == NULL)
+       {
+           tbl_saw = gen::saw(Tables::table_length());
+       }
+       return tbl_saw;
+}
+
+double * Tables::square()
+{
+    static double* tbl_square;
+
+    if (tbl_square == NULL)
+       {
+           tbl_square = gen::square(Tables::table_length());
+       }
+       return tbl_square;
+}

+ 35 - 0
table.h

@@ -0,0 +1,35 @@
+#ifndef TABLE
+#define TABLE
+
+#include "inttypes.h"
+#include <cstdlib>
+
+#define DEFAULT_TABLE_LENGTH 1023
+#define DEFAULT_PHASE_MASK DEFAULT_TABLE_LENGTH
+
+// Global Sample Rate
+#define SystemSR 44100
+
+namespace gen {
+    // Table filling functions
+    double *sine(int len);
+    double *tri(int len);
+    double *saw(int len);
+    double *square(int len);
+}
+
+namespace Tables{
+
+    // Table containers
+    //  These funcitons return singletons of a table
+    int table_length(void);
+    int phase_mask(void);
+    double * sine();
+    double * tri();
+    double * saw();
+    double * square();
+
+}
+
+
+#endif //TABLE