Skip to main content
Formatted the code to make it more readable
Source Link
frarugi87
  • 2.7k
  • 12
  • 19

#include "Arduino.h" #include "common.h" #include "synth.h" #include "serial-in.h" #include "audio-out.h"

void setup() { Synth<0>::initialize(); SerialIn<0>::open(); AudioOut<0>::open(); }

void loop() { int val = analogRead(A0);
int val1 = analogRead(A1);
int val2 = analogRead(A2);
int val3 = analogRead(A3);
int val4 = analogRead(A4);
while (true) { if (SerialIn<0>::available()) { uint8_t b = SerialIn<0>::read(); Synth<0>::receive_midi_byte(b); }

#include "Arduino.h"
#include "common.h"
#include "synth.h"
#include "serial-in.h"
#include "audio-out.h"

void setup() {
  Synth<0>::initialize();
  SerialIn<0>::open();
  AudioOut<0>::open();
}

void loop() {
  int val = analogRead(A0);   
  int val1 = analogRead(A1);   
  int val2 = analogRead(A2);   
  int val3 = analogRead(A3);   
  int val4 = analogRead(A4);   
  while (true) {
    if (SerialIn<0>::available()) {
      uint8_t b = SerialIn<0>::read();
      Synth<0>::receive_midi_byte(b);
    }

    int8_t level = Synth<0>::clock();
    AudioOut<0>::write(level);
  }
}

} }

.h file code

#pragma once

// #define private public // for tests

#include "common.h"

// associations of units #define IOsc Osc #define IFilter Filter #define IAmp Amp #define IEG EG #define IVoice Voice #define ISynthCore SynthCore

#include "osc.h" #include "filter.h" #include "amp.h" #include "eg.h" #include "voice.h" #include "synth-core.h"

template <uint8_t T> class Synth { public: INLINE static void initialize() { ISynthCore<0>::initialize(); int val = analogRead(A0);
int val1 = analogRead(A1);
int val2 = analogRead(A2);
int val3 = analogRead(A3);
int val4 = analogRead(A4);
// Preset ISynthCore<0>::control_change(OSC_MODE , 0 ); ISynthCore<0>::control_change(OSC_COLOR , val); ISynthCore<0>::control_change(MOD_RATE , 8 ); ISynthCore<0>::control_change(MOD_DEPTH , val1); ISynthCore<0>::control_change(LPF_CUTOFF_ENV, val2); ISynthCore<0>::control_change(LPF_RESONANCE , val3); ISynthCore<0>::control_change(ENV_A , val4 ); ISynthCore<0>::control_change(ENV_D_R , 1 ); }

INLINE static void receive_midi_byte(uint8_t b) { ISynthCore<0>::receive_midi_byte(b); }

INLINE static int8_t clock() { return ISynthCore<0>::clock(); } };

#pragma once

// #define private public  // for tests

#include "common.h"

// associations of units
#define IOsc        Osc
#define IFilter     Filter
#define IAmp        Amp
#define IEG         EG
#define IVoice      Voice
#define ISynthCore  SynthCore

#include "osc.h"
#include "filter.h"
#include "amp.h"
#include "eg.h"
#include "voice.h"
#include "synth-core.h"

template <uint8_t T>
class Synth {
public:
  INLINE static void initialize() {
    ISynthCore<0>::initialize();
    int val = analogRead(A0);   
    int val1 = analogRead(A1);   
    int val2 = analogRead(A2);   
    int val3 = analogRead(A3);   
    int val4 = analogRead(A4);   
    // Preset
    ISynthCore<0>::control_change(OSC_MODE      , 0  );
    ISynthCore<0>::control_change(OSC_COLOR     , val);
    ISynthCore<0>::control_change(MOD_RATE      , 8  );
    ISynthCore<0>::control_change(MOD_DEPTH     , val1);
    ISynthCore<0>::control_change(LPF_CUTOFF_ENV, val2);
    ISynthCore<0>::control_change(LPF_RESONANCE , val3);
    ISynthCore<0>::control_change(ENV_A         , val4 );
    ISynthCore<0>::control_change(ENV_D_R       , 1 );
  }

  INLINE static void receive_midi_byte(uint8_t b) {
    ISynthCore<0>::receive_midi_byte(b);
  }

  INLINE static int8_t clock() {
    return ISynthCore<0>::clock();
  }
};

#include "Arduino.h" #include "common.h" #include "synth.h" #include "serial-in.h" #include "audio-out.h"

void setup() { Synth<0>::initialize(); SerialIn<0>::open(); AudioOut<0>::open(); }

void loop() { int val = analogRead(A0);
int val1 = analogRead(A1);
int val2 = analogRead(A2);
int val3 = analogRead(A3);
int val4 = analogRead(A4);
while (true) { if (SerialIn<0>::available()) { uint8_t b = SerialIn<0>::read(); Synth<0>::receive_midi_byte(b); }

int8_t level = Synth<0>::clock();
AudioOut<0>::write(level);

} }

.h file code

#pragma once

// #define private public // for tests

#include "common.h"

// associations of units #define IOsc Osc #define IFilter Filter #define IAmp Amp #define IEG EG #define IVoice Voice #define ISynthCore SynthCore

#include "osc.h" #include "filter.h" #include "amp.h" #include "eg.h" #include "voice.h" #include "synth-core.h"

template <uint8_t T> class Synth { public: INLINE static void initialize() { ISynthCore<0>::initialize(); int val = analogRead(A0);
int val1 = analogRead(A1);
int val2 = analogRead(A2);
int val3 = analogRead(A3);
int val4 = analogRead(A4);
// Preset ISynthCore<0>::control_change(OSC_MODE , 0 ); ISynthCore<0>::control_change(OSC_COLOR , val); ISynthCore<0>::control_change(MOD_RATE , 8 ); ISynthCore<0>::control_change(MOD_DEPTH , val1); ISynthCore<0>::control_change(LPF_CUTOFF_ENV, val2); ISynthCore<0>::control_change(LPF_RESONANCE , val3); ISynthCore<0>::control_change(ENV_A , val4 ); ISynthCore<0>::control_change(ENV_D_R , 1 ); }

INLINE static void receive_midi_byte(uint8_t b) { ISynthCore<0>::receive_midi_byte(b); }

INLINE static int8_t clock() { return ISynthCore<0>::clock(); } };

#include "Arduino.h"
#include "common.h"
#include "synth.h"
#include "serial-in.h"
#include "audio-out.h"

void setup() {
  Synth<0>::initialize();
  SerialIn<0>::open();
  AudioOut<0>::open();
}

void loop() {
  int val = analogRead(A0);   
  int val1 = analogRead(A1);   
  int val2 = analogRead(A2);   
  int val3 = analogRead(A3);   
  int val4 = analogRead(A4);   
  while (true) {
    if (SerialIn<0>::available()) {
      uint8_t b = SerialIn<0>::read();
      Synth<0>::receive_midi_byte(b);
    }

    int8_t level = Synth<0>::clock();
    AudioOut<0>::write(level);
  }
}

.h file code

#pragma once

// #define private public  // for tests

#include "common.h"

// associations of units
#define IOsc        Osc
#define IFilter     Filter
#define IAmp        Amp
#define IEG         EG
#define IVoice      Voice
#define ISynthCore  SynthCore

#include "osc.h"
#include "filter.h"
#include "amp.h"
#include "eg.h"
#include "voice.h"
#include "synth-core.h"

template <uint8_t T>
class Synth {
public:
  INLINE static void initialize() {
    ISynthCore<0>::initialize();
    int val = analogRead(A0);   
    int val1 = analogRead(A1);   
    int val2 = analogRead(A2);   
    int val3 = analogRead(A3);   
    int val4 = analogRead(A4);   
    // Preset
    ISynthCore<0>::control_change(OSC_MODE      , 0  );
    ISynthCore<0>::control_change(OSC_COLOR     , val);
    ISynthCore<0>::control_change(MOD_RATE      , 8  );
    ISynthCore<0>::control_change(MOD_DEPTH     , val1);
    ISynthCore<0>::control_change(LPF_CUTOFF_ENV, val2);
    ISynthCore<0>::control_change(LPF_RESONANCE , val3);
    ISynthCore<0>::control_change(ENV_A         , val4 );
    ISynthCore<0>::control_change(ENV_D_R       , 1 );
  }

  INLINE static void receive_midi_byte(uint8_t b) {
    ISynthCore<0>::receive_midi_byte(b);
  }

  INLINE static int8_t clock() {
    return ISynthCore<0>::clock();
  }
};
Source Link

Pass changing analog value from void loop to library .h file

How would I go about passing a void loop analog read value to a library .h file? I've tried it but it only reads the pot position as soon as I plug the arduino in and does not change once booted up.

arduino main sketch file code;

#include "Arduino.h" #include "common.h" #include "synth.h" #include "serial-in.h" #include "audio-out.h"

void setup() { Synth<0>::initialize(); SerialIn<0>::open(); AudioOut<0>::open(); }

void loop() { int val = analogRead(A0);
int val1 = analogRead(A1);
int val2 = analogRead(A2);
int val3 = analogRead(A3);
int val4 = analogRead(A4);
while (true) { if (SerialIn<0>::available()) { uint8_t b = SerialIn<0>::read(); Synth<0>::receive_midi_byte(b); }

int8_t level = Synth<0>::clock();
AudioOut<0>::write(level);

} }

.h file code

#pragma once

// #define private public // for tests

#include "common.h"

// associations of units #define IOsc Osc #define IFilter Filter #define IAmp Amp #define IEG EG #define IVoice Voice #define ISynthCore SynthCore

#include "osc.h" #include "filter.h" #include "amp.h" #include "eg.h" #include "voice.h" #include "synth-core.h"

template <uint8_t T> class Synth { public: INLINE static void initialize() { ISynthCore<0>::initialize(); int val = analogRead(A0);
int val1 = analogRead(A1);
int val2 = analogRead(A2);
int val3 = analogRead(A3);
int val4 = analogRead(A4);
// Preset ISynthCore<0>::control_change(OSC_MODE , 0 ); ISynthCore<0>::control_change(OSC_COLOR , val); ISynthCore<0>::control_change(MOD_RATE , 8 ); ISynthCore<0>::control_change(MOD_DEPTH , val1); ISynthCore<0>::control_change(LPF_CUTOFF_ENV, val2); ISynthCore<0>::control_change(LPF_RESONANCE , val3); ISynthCore<0>::control_change(ENV_A , val4 ); ISynthCore<0>::control_change(ENV_D_R , 1 ); }

INLINE static void receive_midi_byte(uint8_t b) { ISynthCore<0>::receive_midi_byte(b); }

INLINE static int8_t clock() { return ISynthCore<0>::clock(); } };