aboutsummaryrefslogtreecommitdiffstats
path: root/classes/ZZZ.sc
blob: 662da36a382795bdf753a71fd04e171ede8f7591 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
ZZZ{

  classvar <cvMinLFO = -2.5,
  < cvMaxLFO = 2.5,
  < cvMinADSR = 0.0,
  < cvMaxADSR = 8.0,
  < cvMinTriggerGateClock = 0.0,
  < cvMaxTriggerGateClock = 5.0,
  < noteVoltageRangeMin = -5.0,
  < noteVoltageRangeMax = 7.0,
  < noteCpsRangeMin = 4.088,
  < noteCpsRangeMax = 16744.036;
  var <channels;

  *new{
    arg channels;
    ^super.newCopyArgs(channels);
  }

  *initClass{
    StartUp.add{
      // adding SynthDef for clock
      SynthDef(\ZZZClock, { |out = 0, freq = 2, iphase = 0, width = 0.1, mul= 1.0|
        Out.ar(out, LFPulse.ar(freq, iphase, width, mul));
      }).add;
      // adding SynthDef for constant notes
      SynthDef(\ZZZConstant, { |out = 0, mul= 0.0|
        Out.ar(
          out,
          Lag.ar(
            LFPulse.ar(
              500,
              0,
              1),
            0.1,
            mul
          )
        );
      }).add;
      //adding synth for percussive environments (trigger, gate)
      SynthDef(\ZZZPerc, { |out = 0, freq = 20000, amp = 0.0, attack = 0.001, release = 1, time = 1, curve = -4|
        var signal, envelope;
        envelope = EnvGen.kr(Env.perc(attack, release, amp, curve),  doneAction: 2);
        signal = SinOsc.ar(freq, 0, envelope);
        Out.ar(out, signal);
      }).add;
    }
  }

  /**
   * Calculates voltage for supplied amplitude.
   * Maximum: 10
   * Minimum: -10
   * @return voltage that corresponds to the amplitude supplied.
   */
  *amplitudevoltage{
    arg amplitude;
    var voltage = 0.0;
    if((amplitude == 0.0), {
      ^voltage;
    },{
      voltage = amplitude*10.0;
      if(voltage < -10.0, {
        ^(-10.0);
      });
      if(voltage > 10.0, {
        ^(10.0);
      });
      ^voltage;
    });
  }

  /**
   * Calculates amplitude for supplied voltage.
   * Maximum: 1.0
   * Minimum: -1.0
   * @return amplitude needed to achieve the voltage supplied.
   */
  *voltageamplitude{
    arg voltage;
    var amplitude = 0.0;
    if((voltage == 0.0), {
      ^amplitude;
    },{
      amplitude = voltage/10.0;
      if(amplitude < -1.0, {
        ^(-1.0);
      });
      if(amplitude > 1.0, {
        ^(1.0);
      });
      ^amplitude;
    });
  }

  /**
   * Calculates voltage for supplied cycles per second.
   * @return voltage for corresponding cycles per second
   */
  *cpsvoltage{
    arg cps;
    if(cps < noteCpsRangeMin, {
      cps = noteCpsRangeMin;
    });
    if(cps > noteCpsRangeMax, {
      cps = noteCpsRangeMax;
    });
    ^cps.explin(noteCpsRangeMin, noteCpsRangeMax, noteVoltageRangeMin, noteVoltageRangeMax);
  }

  /**
   * Calculates cycles per second for supplied voltage.
   * @return cycles per second for corresponding voltage
   */
  *voltagecps{
    arg voltage;
    if(voltage < noteVoltageRangeMin, {
      voltage = noteVoltageRangeMin;
    });
    if(voltage > noteVoltageRangeMax, {
      voltage = noteVoltageRangeMax;
    });
    ^voltage.linexp(noteVoltageRangeMin, noteVoltageRangeMax, noteCpsRangeMin, noteCpsRangeMax);
  }

  /**
   * Calculates amplitude for supplied frequency.
   * @return amplitude for corresponding frequency
   */
  *cpsamp{
    arg cps;
    ^this.voltageamplitude(this.cpsvoltage(cps));
  }

  /**
   * Calculates frequency for supplied amplitude.
   * Wraps call to ampToVAC -> vacToHz
   * @return frequency for corresponding amplitude
   */
  *ampcps{
    arg amplitude;
    ^this.voltagecps(this.amplitudevoltage(amplitude));
  }
}