summaryrefslogtreecommitdiffstats
path: root/super_saw.cpp
blob: 2ce115685034a74c7783fb614a078fc7242f42af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <lv2synth.hpp>
#include "super_saw.peg"
#include <stdio.h>
#include <math.h>
extern "C" void adainit(void);
extern "C" void adafinal(void);
extern "C" float Super_Saw(float,float,float,float,float);

class SuperSawVoice : public LV2::Voice {
public:

  SuperSawVoice(double rate)
    : m_key(LV2::INVALID_KEY), m_rate(rate), m_period(10), m_counter(0) {
  }

  void on(unsigned char key, unsigned char velocity) {
    m_key = key;
    m_period = m_rate / LV2::key2hz(m_key);
  }

  void off(unsigned char velocity) {
    m_key = LV2::INVALID_KEY;
  }

  unsigned char get_key() const {
    return m_key;
  }

  void render(uint32_t from, uint32_t to) {
    if (m_key == LV2::INVALID_KEY)
      return;
    for (uint32_t i = from; i < to; ++i) {
      //Time, Pitch, Detune, Mix, Sample Rate
      float detune = *p(p_Detune);
      float mix = *p(p_Mix);
      float s = Super_Saw(m_counter,LV2::key2hz(m_key),detune,mix,m_rate)/100.0;
      p(p_left)[i] += s;
      p(p_right)[i] += s;
      m_counter = (m_counter + 1);// % m_period;
    }
  }

protected:

  unsigned char m_key;
  double m_rate;
  uint32_t m_period;
  uint32_t m_counter;

};


class SuperSaw : public LV2::Synth<SuperSawVoice, SuperSaw> {
public:
  SuperSaw(double rate)
    : LV2::Synth<SuperSawVoice, SuperSaw>(p_n_ports, p_midi) {
    adainit();
    add_voices(new SuperSawVoice(rate), new SuperSawVoice(rate), new SuperSawVoice(rate));
    add_audio_outputs(p_left, p_right);
    printf("Created object");
  }
  ~SuperSaw(){
    adafinal();
  }
};


static int _ = SuperSaw::register_class(p_uri);