Advertisement
Guest User

Untitled

a guest
Aug 21st, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. // This work is free software.
  2. // You can redistribute it and/or modify it under the terms of the
  3. // GNU General Public License as published by the Free Software Foundation.
  4. // Either version 2 of the License, or any later version.
  5.  
  6. class Sequence {
  7. Timer timer;
  8. Shred shr;
  9. StkInstrument instrument;
  10. dur durations[];
  11. string name;
  12.  
  13. fun string[] strToArray(string input, string tr) {
  14. string result[3];
  15. 0 => int i;
  16.  
  17. while (input.find(tr) > -1 ) {
  18. input.find(tr) => int pos;
  19. input.substring(0, pos) => result[i];
  20. input.substring(pos + 1) => input;
  21. i++;
  22. }
  23.  
  24. input => result[i];
  25.  
  26. return result;
  27. }
  28.  
  29. fun void playSeq(string seq[], float scale[],
  30. StkInstrument instr, float mod) {
  31. while(true) {
  32. for(int i; i < seq.size(); i++) {
  33. seq[i] => string content;
  34. strToArray(content, " ")@=> string line[];
  35. line[2].toInt() => int len;
  36.  
  37. if (len - 1 > timer.currentBar) {
  38. timer.newBar => now;
  39. }
  40.  
  41. scale[line[1].toInt() - 1] * mod => instr.freq;
  42.  
  43. if(line[0] == "n") {
  44. Math.random2f(.7, 1) => instrument.noteOn;
  45. } else {
  46. 1 => instrument.noteOff;
  47. }
  48.  
  49. durations[line[2].toInt()] * mod => now;
  50. }
  51. }
  52. }
  53.  
  54. fun void add(string s[], float sk[], StkInstrument in, float m, string n) {
  55. n => name;
  56. in @=> instrument;
  57. spork~ playSeq(s, sk, in, m) @=> shr;
  58. }
  59.  
  60. fun void remove() {
  61. 1 => instrument.noteOff;
  62. Machine.remove(shr.id());
  63. }
  64. }
  65.  
  66. public class SequenceController {
  67. Sequence sequences[0];
  68. Timer t;
  69. dur durations[];
  70.  
  71. fun void addSeq(string s[], float scale[], StkInstrument in,
  72. float m, string name) {
  73. sequences.size() + 1 => sequences.size;
  74. new Sequence @=> sequences[sequences.size() - 1];
  75. t @=> sequences[sequences.size() - 1].timer;
  76. durations @=> sequences[sequences.size() - 1].durations;
  77. sequences[sequences.size() - 1].add(s, scale, in, m, name);
  78.  
  79. chout <= "add " <= sequences[sequences.size() - 1].name <= "\n";
  80. }
  81.  
  82. fun void removeSeq(int num) {
  83. Sequence s[sequences.size() - 1];
  84.  
  85. chout <= "remove " <= sequences[num].name <= "\n";
  86.  
  87. for(int i; i < s.size(); i++) {
  88. if(i != num) {
  89. sequences[i] @=> s[i];
  90. } else {
  91. sequences[i + 1] @=> s[i];
  92. }
  93. }
  94.  
  95. sequences[num].remove();
  96.  
  97. s @=> sequences;
  98. }
  99.  
  100. fun void lookup() {
  101. if (sequences.size() > 0) {
  102. chout <= "\n" <= "current sequences: " <= "\n";
  103. for(int i; i < sequences.size(); i++) {
  104. chout <= sequences[i].name <= " - " <= i <= "\n";
  105. }
  106. chout <= "\n";
  107. } else chout <= "no sequences" <= "\n";
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement