// new SAOL translation attempt global { srate 44100; krate 8820; outchannels 2; ksig gkLFO, gkamp; } instr globalfade (risetime) { // p4 is rise AND decay time exports ksig gkamp; gkamp = kexpon(0.005, risetime, 1, dur - (risetime * 2), 1, risetime , 0.005); } instr globalLFO (startrate, endrate) { ksig lfo_freq; exports ksig gkLFO; imports table f1; lfo_freq = kline(startrate, dur, endrate); gkLFO = 0.5 + 0.5 * koscil(f1, lfo_freq); // normalized from 0 to 1 } instr pwm(pitch, nothing) { // another try by PW // We simulate a square wave by overdriving a table index, and simulate // pwm by varying an offset to the index. // Aliasing is reduced by making the amount of overdrive // inversely proportional to pitch. imports table f1, f5; imports ksig gkLFO; ivar ipitch, iantialias, isquareness; ksig f5len; // only needed at i-rate, but oh well. ksig knarrowness, kamp; asig index, aout, afiltered; ipitch = cpspch(pitch); // cpspch(p4) iantialias = 15; //recommended range is 4 to about 30 isquareness = s_rate/(ipitch * iantialias); // isquareness range: infinity =square, < 1 = sine knarrowness = gkLFO * isquareness ; // Modulate! index = knarrowness + isquareness * oscil(f1, ipitch); // sine wave index. f5len = ftlen(f5); aout = tableread(f5, f5len/index); // f5 should be straight line +-1 // also, notice that we have to normalize the table index // manually. // Attempt to remove DC offset... ugh, need a better one. afiltered = hipass(aout, 15); // Ugh, translating csound linseg is clunky. // There must be a more idiomatic way? kamp = kline(0, .01, 1, .02, .5, dur -.23, .5, .2, 0); //kamp = kamp * 32000; // * gkamp for global fades output(afiltered * kamp * 32000); }