Skip to content

Instantly share code, notes, and snippets.

@zeffii
Forked from anonymous/PolyWaveForm.ck
Last active December 31, 2015 11:19
Show Gist options
  • Save zeffii/7978771 to your computer and use it in GitHub Desktop.
Save zeffii/7978771 to your computer and use it in GitHub Desktop.
Machine.add(me.dir() + "/WaveformMixerMono.ck");
Machine.add(me.dir() + "/PolyWaveForm.ck");
Machine.add(me.dir() + "/score.ck");
4::second => now;
public class PolyWaveForm{
// this will act as the master out gain control
0.8 => float max_gain;
Mix2 sum_waveforms;
sum_waveforms.gain(max_gain);
// defaults to a mono synth.
1 => int polyphony;
max_gain / polyphony => float voice_volume;
WaveformMixerMono bloop[polyphony];
Pan2 pan_array[polyphony];
ADSR wfEnv[polyphony];
fun void play_chord(int notes[], int attack, int decay){
// check if number of notes has change, an rewire if it has
if (!(notes.cap() == polyphony)){
set_polyphony(notes);
}
set_voice_volume(notes.cap());
for(0 => int n; n<notes.cap(); n++){
notes[n] => Std.mtof => this.bloop[n].freq;
this.wfEnv[n].keyOn();
this.wfEnv[n].set( attack::ms, decay::ms, 0.00, 0::ms );
}
}
fun void set_voice_volume(int polyphony){
<<< "this" >>>;
this.max_gain / this.polyphony => this.voice_volume;
}
fun void set_polyphony(int notes[]){
if (notes.cap() <= 1) 1 => this.polyphony;
else notes.cap() => this.polyphony;
// this could be optimized by only adding elements if polyphony increases
// and .clear() on the array and build a new one if polyphony decreases
WaveformMixerMono bloop[this.polyphony] @=> this.bloop;
Pan2 pan_array[this.polyphony] @=> this.pan_array;
ADSR wfEnv[this.polyphony] @=> this.wfEnv;
<<< "here" >>>;
for(0 => int n; n<notes.cap(); n++){
this.bloop[n].mixer2(2.3);
this.bloop[n].final => this.wfEnv[n] => this.pan_array[n] => this.sum_waveforms;
this.bloop[n].final.gain(this.voice_volume);
((((n%2)*2)-1)*0.9) => this.pan_array[n].pan;
}
}
}
// gist -m
[50,54,57,59] @=> int notes[];
PolyWaveForm synth;
synth.sum_waveforms => dac;
synth.play_chord(notes, 0, 300);
3::second => now;
public class WaveformMixerMono{
SinOsc vsin => Gain final;
SawOsc vsaw => final;
TriOsc vtri => final;
PulseOsc vpls => final;
Noise vnse => final;
440 => float frequency;
fun void pwidth(float pw){
pw => this.vpls.width;
}
fun void freq(float frequency){
frequency => this.frequency;
vsin.freq(frequency);
vsaw.freq(frequency);
vtri.freq(frequency);
vpls.freq(frequency);
}
fun void mixer(float components[], float mgain){
mixer(components);
mgain => this.final.gain;
}
fun void mixer2(float continual){
// continual range is 0.0 to 8.0
// goes from (sin, saw, tri, pulse to noise)
float mix_comp[];
if (continual >= 7.9996) [0.0, 0.0, 0.0, 0.0, 1.0] @=> mix_comp;
else if (continual <= 0.0001) [1.0, 0.0, 0.0, 0.0, 0.0] @=> mix_comp;
else {
continual => float j;
Math.floor(j/2.0) $ int => int k;
k*2 => int k2;
((j - k2) / 2.0) => float n;
mix_arrays(k, n) @=> mix_comp;
}
mixer(mix_comp);
}
fun void mixer(float components[]){
// set defautls, just in case -- make it a SinOsc
if (!(components.cap() == 5)) {
[1.0, 0.0, 0.0, 0.0, 0.0] @=> components;
}
float summer;
for(0 => int i; i<components.cap(); i++){
components[i] +=> summer;
}
if (summer <= 0.0) {
<<< "no sound!" >>>;
0.0 => this.vsin.gain;
0.0 => this.vsaw.gain;
0.0 => this.vtri.gain;
0.0 => this.vpls.gain;
0.0 => this.vnse.gain;
}
else{
components[0] / summer => this.vsin.gain;
components[1] / summer => this.vsaw.gain;
components[2] / summer => this.vtri.gain;
components[3] / summer => this.vpls.gain;
components[4] / summer => this.vnse.gain;
}
}
fun float fixmin(float fin){
if (fin < 0.0001) return 0.0;
else return fin;
}
fun float[] mix_arrays(int idx, float amount){
[0.0, 0.0, 0.0, 0.0, 0.0] @=> float final_mix[];
(1-amount) => fixmin => final_mix[idx];
(amount) => fixmin => final_mix[idx+1];
return final_mix;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment