Browse Source

Fixed squarewave. Works with and without table.

akshaycadambi 5 years ago
parent
commit
0e781208b6
4 changed files with 15 additions and 16 deletions
  1. 7 11
      table.cpp
  2. 3 0
      table.h
  3. 3 3
      ugens.cpp
  4. 2 2
      ugens.h

+ 7 - 11
table.cpp

@@ -2,15 +2,12 @@
 #include <math.h>
 #include <stdlib.h>
 
-#define DC 0
-#define PEAK 127
-
 sample * gen::sine(int len)
 {
     sample *SineTable = (sample* ) malloc(sizeof(sample) * len);
     for (int i=0; i<len; i++)
     {
-        *(SineTable+i) = DC + PEAK* sin(2*M_PI*i/len);
+        *(SineTable+i) = TABLE_DC + TABLE_PEAK* sin(2*M_PI*i/len);
     }
     return SineTable;
 }
@@ -20,15 +17,15 @@ sample * gen::tri(int len)
     sample *TriTable = (sample* ) malloc(sizeof(sample) * len);
     for (int i=0 ; i<=len/4; i++)
     {
-        TriTable[i] = DC + PEAK*4.0/len * i;
+        TriTable[i] = TABLE_DC + TABLE_PEAK*4.0/len * i;
     }
     for (int i=1; i<=len/2; i++)
     {
-        TriTable[i + len/4] = (DC+PEAK) - (PEAK)*4.0/len * i;
+        TriTable[i + len/4] = (TABLE_DC+TABLE_PEAK) - (TABLE_PEAK)*4.0/len * i;
     }
     for (int i=1; i<=len/4; i++)
     {
-        TriTable[i + 3*len/4] = PEAK*4.0/len * i + (DC-PEAK);
+        TriTable[i + 3*len/4] = TABLE_PEAK*4.0/len * i + (TABLE_DC-TABLE_PEAK);
     }
 
     return TriTable;
@@ -39,7 +36,7 @@ sample * gen::saw(int len)
     sample *SawTable = (sample* ) malloc(sizeof(sample) * len);
     for (int i=0; i<len; i++)
     {
-        *(SawTable+i) = (20.*PEAK)/len * i + (DC-PEAK) ;
+        *(SawTable+i) = (2.0*TABLE_PEAK)/len * i + (TABLE_DC-TABLE_PEAK) ;
     }
     return SawTable;
 }
@@ -49,9 +46,8 @@ sample * gen::square(int len)
     sample *SquareTable = (sample* ) malloc(sizeof(sample) * len);
     for(int i=0; i<=len/2; i++)
     {
-        *(SquareTable+i) = DC+PEAK;
-        *(SquareTable+i+i/2) = DC-PEAK;
-
+        *(SquareTable+i) = TABLE_DC+TABLE_PEAK;
+        *(SquareTable+i+len/2) = TABLE_DC-TABLE_PEAK;
     }
     return SquareTable;
 }

+ 3 - 0
table.h

@@ -6,6 +6,9 @@
 #define DEFAULT_TABLE_LENGTH 1024
 #define DEFAULT_PHASE_MASK DEFAULT_TABLE_LENGTH-1
 
+#define TABLE_DC 0
+#define TABLE_PEAK 127
+
 // TODO: Frequency division of sample rate (for control rate) - Comments in synth and ugens.
 #define LF_FREQ_DIV 2
 

+ 3 - 3
ugens.cpp

@@ -68,7 +68,7 @@ ugen::Square::Square(float freq)
 {
     setFreq(freq);
     // Note. This is optimized. See table.cpp for details.
-#if SQUAREWAVE_MEMORY_OPTIMIZATION == 1
+#ifdef SQUAREWAVE_OPTIMIZE_MEMORY
     tbl = NULL;
     half_length = Tables::length()/2;
 #else
@@ -78,8 +78,8 @@ ugen::Square::Square(float freq)
 
 sample ugen::Square::operator()(void)
 {
-#if SQUAREWAVE_MEMORY_OPTIMIZATION == 1
-    sample outValue = ( current_phase <= half_length );
+#ifdef SQUAREWAVE_OPTIMIZE_MEMORY
+    sample outValue = ( current_phase <= half_length ) ? (TABLE_DC+TABLE_PEAK) : (TABLE_DC-TABLE_PEAK);
     current_phase += phase_inc;
     current_phase = current_phase & Tables::phase_mask();
     return outValue;

+ 2 - 2
ugens.h

@@ -4,7 +4,7 @@
 #include "synth.h"
 
 // Optimizes square-wave ugen to use lesser memory
-#define SQUAREWAVE_MEMORY_OPTIMIZATION 1
+#define SQUAREWAVE_OPTIMIZE_MEMORY
 
 namespace ugen{
 
@@ -40,7 +40,7 @@ class Square: public synth::WavetableSynth
     {
     public:
 
-#if SQUAREWAVE_MEMORY_OPTIMIZATION == 1
+#ifdef SQUAREWAVE_OPTIMIZE_MEMORY
         int half_length;
 #endif
         Square(float freq);