Guest User

Untitled

a guest
Jun 18th, 2014
505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1.  
  2. /*!
  3. *
  4. * stagas - got some 303
  5. *
  6. */
  7.  
  8. var bpm = 125;
  9. var tuning = 440;
  10. var transpose = -1;
  11.  
  12. // constants
  13. var tau = 2 * Math.PI;
  14.  
  15. // time coefficients
  16. var t, tt;
  17.  
  18. function Oscillator(type, size){
  19. if (!(this instanceof Oscillator)) return new Oscillator(type, size);
  20. this.pos = 0;
  21. this.size = size || sampleRate;
  22. this.coeff = this.size / sampleRate;
  23. this.table = new Float32Array(this.size);
  24. this.build(type);
  25. }
  26.  
  27. Oscillator.prototype.build = function(type){
  28. switch (type) {
  29. case 'sin':
  30. var scale = 2 * Math.PI / this.size;
  31. for (var i = 0; i < this.size; i++) {
  32. this.table[i] = Math.sin(i * scale);
  33. }
  34. break;
  35.  
  36. case 'saw':
  37. for (var i = 0; i < this.size; i++) {
  38. var x = (i / this.size);
  39. this.table[i] = +2.0 * (x - Math.round(x));
  40. }
  41. break;
  42.  
  43. case 'ramp':
  44. for (var i = 0; i < this.size; i++) {
  45. var x = (i / this.size);
  46. this.table[i] = -2.0 * (x - Math.round(x));
  47. }
  48. break;
  49.  
  50. case 'tri':
  51. for (var i = 0; i < this.size; i++) {
  52. var x = (i / this.size) - 0.25;
  53. this.table[i] = 1.0 - 4.0 * Math.abs(Math.round(x) - x);
  54. }
  55. break;
  56.  
  57. case 'sqr':
  58. var half = this.size / 2;
  59. for (var i = 0; i < this.size; i++) {
  60. this.table[i] = i < half ? +1 : -1;
  61. }
  62. break;
  63. }
  64. };
  65.  
  66. Oscillator.prototype.play = function(freq){
  67. this.pos += freq * this.coeff;
  68. if (this.pos >= this.size) this.pos -= this.size;
  69. this.index = this.pos | 0;
  70. this.alpha = this.pos - this.index;
  71. this.next = this.table[this.index == this.size - 1 ? 0 : this.index + 1];
  72. this.curr = this.table[this.index];
  73. return this.curr + (this.next - this.curr) * this.alpha;
  74. };
  75.  
  76. function clock(_t){
  77. t = _t;
  78. t *= bpm / 120;
  79. tt = tau * t;
  80. }
  81.  
  82. function clip(x){
  83. return x / (1 + Math.abs(x));
  84. }
  85.  
  86. function sequence(measure, seq){
  87. return seq[(t / measure / 2 | 0) % seq.length];
  88. }
  89.  
  90. function slide(measure, seq, speed){
  91. var pos = (t / measure / 2) % seq.length;
  92. var now = pos | 0;
  93. var next = now + 1;
  94. var alpha = pos - now;
  95. if (next == seq.length) next = 0;
  96. return seq[now] + ((seq[next] - seq[now]) * Math.pow(alpha, speed));
  97. }
  98.  
  99. function arp(measure, x, y, z){
  100. var ts = t / 2 % measure;
  101. return Math.sin(x * (Math.exp(-ts * y))) * Math.exp(-ts * z);
  102. }
  103.  
  104. function env(measure, z){
  105. var ts = t / 2 % measure;
  106. return Math.exp(-ts * z);
  107. }
  108.  
  109. function Noise(){
  110. return Math.random() * 2 - 1;
  111. }
  112.  
  113. function note(n, octave){
  114. return Math.pow(2, (
  115. n + transpose - 33 + (12 * (octave || 0))
  116. ) / 12) * tuning; // A4 tuning
  117. }
  118.  
  119. function octave(o){
  120. return function(n){
  121. return n * o;
  122. };
  123. }
  124.  
  125. function DiodeFilter(){
  126. this.k = 0;
  127. this.A = 0;
  128. this.z = [0,0,0,0,0];
  129. this.ah;
  130. this.bh;
  131. this.fc;
  132. this.set_q(0);
  133. this.set_hpf(0.5);
  134. this.set_fc(.5);
  135. }
  136.  
  137. DiodeFilter.prototype.set_hpf = function(fc){
  138. var K = fc * Math.PI;
  139. this.ah = (K - 2) / (K + 2);
  140. this.bh = 2 / (K + 2);
  141. };
  142.  
  143. DiodeFilter.prototype.reset = function(){
  144. if (this.k < 17) this.z = [0,0,0,0,0];
  145. };
  146.  
  147. DiodeFilter.prototype.set_q = function(q){
  148. this.k = 20 * q;
  149. this.A = 1 + 0.5 * this.k;
  150. };
  151.  
  152. DiodeFilter.prototype.set_fc = function(cutoff){
  153. cutoff = (cutoff * cutoff);
  154. this.fc = cutoff <= 0
  155. ? .02
  156. : (cutoff >= 1.0 ? .999 : cutoff);
  157. };
  158.  
  159. DiodeFilter.prototype.run = function(x){
  160. var a = Math.PI * this.fc;
  161. a = 2 * Math.tan(0.5*a); // dewarping, not required with 2x oversampling
  162. var ainv = 1 / a;
  163. var a2 = a*a;
  164. var b = 2*a + 1;
  165. var b2 = b*b;
  166. var c = 1 / (2*a2*a2 - 4*a2*b2 + b2*b2);
  167. var g0 = 2*a2*a2*c;
  168. var g = g0 * this.bh;
  169.  
  170. // current state
  171. var s0 = (a2*a*this.z[0] + a2*b*this.z[1] + this.z[2]*(b2 - 2*a2)*a + this.z[3]*(b2 - 3*a2)*b) * c;
  172. var s = this.bh*s0 - this.z[4];
  173.  
  174. // solve feedback loop (linear)
  175. var y5 = (g*x + s) / (1 + g*this.k);
  176.  
  177. // input clipping
  178. var y0 = clip(x - this.k*y5);
  179. y5 = g*y0 + s;
  180.  
  181. // compute integrator outputs
  182. var y4 = g0*y0 + s0;
  183. var y3 = (b*y4 - this.z[3]) * ainv;
  184. var y2 = (b*y3 - a*y4 - this.z[2]) * ainv;
  185. var y1 = (b*y2 - a*y3 - this.z[1]) * ainv;
  186.  
  187. // update filter state
  188. this.z[0] += 4*a*(y0 - y1 + y2);
  189. this.z[1] += 2*a*(y1 - 2*y2 + y3);
  190. this.z[2] += 2*a*(y2 - 2*y3 + y4);
  191. this.z[3] += 2*a*(y3 - 2*y4);
  192. this.z[4] = this.bh*y4 + this.ah*y5;
  193.  
  194. return this.A*y4;
  195. };
  196.  
  197. var osc = Oscillator('saw', 1024);
  198. var lfo = Oscillator('sin', 512);
  199. var lfo2 = Oscillator('sin', 512);
  200.  
  201. var melody = [5, 25, 5, 25, 4, 16, 13, 4 ].map(function(n){
  202. return note(n, 0);
  203. });
  204.  
  205. var filter = new DiodeFilter();
  206.  
  207. filter.set_q(0.66);
  208. filter.set_hpf(.0007);
  209.  
  210. function dsp(t){
  211. clock(t);
  212.  
  213. // filter.set_fc(0.001 + ((lfo.play(2.66) * 0.16 + 1) / 2) * (0.258 + lfo2.play(.02) * 0.12)) ;
  214.  
  215. var lfo_out = lfo.play(0.06);
  216. lfo_out *= lfo_out;
  217. var f_decay = 30+30*lfo_out;
  218. var f_c = 200 + 5000*env(1/16, f_decay);
  219. var cutoff = 2*Math.PI*f_c/sampleRate;
  220.  
  221. filter.set_fc(cutoff) ;
  222.  
  223. var n = slide(1/16, melody, 12);
  224.  
  225. var synth_osc = osc.play(n);
  226. var synth = arp(1/16, synth_osc, 12, 7);
  227.  
  228. synth = filter.run(synth * 0.5);
  229. synth = clip(synth * 12);
  230.  
  231. var kick = arp(1/4, 40, 90, 15);
  232.  
  233. // mixer
  234. return 0.7 * (
  235. 0.6 * synth +
  236. Math.atan(10*kick)
  237. );
  238. }
Advertisement
Add Comment
Please, Sign In to add comment