Advertisement
misingnoglic

random.js

Feb 24th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. inlets = 1;
  2. outlets = 3;
  3.  
  4. Gm_triad = triad(7,false) //notes of Gm chord
  5. Gm_scale = scale(7,true,true)
  6. F_triad = triad(5,true) //notes of F chord
  7. F_scale = scale(5,true)
  8. D_triad = triad(2,true) // notes of D chord
  9. special = pentatonic(0,true)
  10.  
  11. bass = [55,53,50,0] //bass notes (not implemented)
  12.  
  13. last_mode = 1
  14. last_note = 76
  15. displacement = 0
  16. last_movement = 0
  17.  
  18. //below is dictionary of MIDI number to note name
  19. notes = {0:"C", 1:"C#", 2:"D", 3:"D#", 4:"E", 5:"F", 6:"F#", 7:"G", 9:"A", 10:"Bb", 11:"B"}
  20.  
  21. min_octave = 4
  22. max_octave = 7
  23.  
  24. function get_note(mode) {
  25.     offset = 0
  26.     post(mode)
  27.     post(offset)
  28.     //offset = 0
  29.     modes = [Gm_triad,F_triad,D_triad,special]
  30.     mode = modes[mode]//selects the mode we are in
  31.     sorted_mode = []
  32.     for (i=0; i<mode.length; i++){
  33.         sorted_mode.push(mode[i]%12)
  34.     }
  35.     a.sort //puts the mode in sorted value (for octave reasons)
  36.    
  37.    
  38.     last_pitch = last_note%12 //gets the previous pitch we used
  39.     last_octave = Math.floor(last_note/12) //previous octave we were in
  40.    
  41.     index = sorted_mode.indexOf(last_pitch); //where the last pitch is in the mode
  42.    
  43.     note_shift = 0 //how much we will move the pitch
  44.     octave_shift = 0 //how much we will move the octave
  45.     movements = [0,1,2,3,4] //possibly movements (positive or negative)
  46.     movement_probabilities = [1.5,5,2,3.5,2] //probability of hitting each note (weighted, so 5 is 5x more likely than 1)
  47.    
  48.    
  49.     position=0 //current movement we could be at
  50.     random_num = random_float(0,list_sum(movement_probabilities)) //random number placed between one of these movements
  51.    
  52.     post("random num: ")
  53.     post(random_num)
  54.     for (i=0; i<movements.length; i++){
  55.         position += movement_probabilities[i]
  56.  
  57.         if (random_num < position){ //if the number is that movement
  58.             note_shift = movements[i] //make that the note shift
  59.             break
  60.         }
  61.     }
  62.     d = .3*sign(last_movement) // -.3 or +.3
  63.     s = Math.random() //random number between 0 and 1
  64.  
  65.     if (s>.5+d){ //if that random number is greater than .5+d, it will go in the negative direction
  66.         //this is so when there is a downward trend, it is more likely that it will go down than up, and vice versa
  67.         note_shift = note_shift*-1
  68.     }
  69.     post("amount moving: ")
  70.     post(note_shift)
  71.    
  72.     //the new note is the old index plus however many the note shifted, wrapped around (and positive)
  73.     new_note = sorted_mode[Math.abs((index+note_shift)%mode.length)]
  74.      
  75.     post ("new pitch: ")
  76.     post(new_note)
  77.    
  78.     //the octave shift is the old octave shift, plus however much the shift made it go up or down
  79.     octave_shift = last_octave + Math.floor((index+note_shift)/mode.length)
  80.    
  81.     if (octave_shift<min_octave){
  82.         octave_shift = last_octave+1
  83.         note_shift = -1*(last_movement) + 3
  84.         post("too low!")
  85.     }
  86.     else if (octave_shift>max_octave){
  87.         post("Too high!")
  88.         octave_shift = last_octave-1
  89.         note_shift = -1*(last_movement) - 3
  90.     }
  91.    
  92.     new_note = new_note + 12*octave_shift + offset
  93.    
  94.     last_note = new_note
  95.     last_movement = note_shift
  96.    
  97.    
  98.    
  99. /*  note = mode[random(0,3)]; //chooses a random note from the mode
  100.     midi_value = note+(12*random(min_octave,max_octave+1))
  101.    
  102.     outlet(1,midi_value); // puts in outlet 1 that note from 4-8 octaves above
  103.     outlet(0,notes[note%12]); //puts the name of that note in outlet 0
  104.     outlet(2,toHz(midi_value)) */
  105.    
  106.     outlet(1,new_note);
  107.     outlet(0,notes[new_note%12])
  108.     outlet(2,toHz(new_note))
  109. }
  110.  
  111. function random(min,maximum){
  112.     //simple function that returns a number within the min/max bounds
  113.     return Math.floor(Math.random()*(maximum-min)+min);
  114. }
  115.  
  116. function random_float(min,maximum){
  117.     return Math.random()*(maximum-min)+min
  118. }
  119.  
  120. function toHz(midi){
  121.     return Math.pow(2,(midi-69)/12)*440
  122. }
  123.  
  124. function triad(midi, major){
  125.     add = 0
  126.     if (major){
  127.         add=1
  128.     }
  129.     return [midi,midi+3+add,midi+7]
  130. }
  131.  
  132.  
  133. function scale(midi, major, harmonic){
  134.     add=0
  135.     seventh=0
  136.     if (major){
  137.         add = 1
  138.     }
  139.     else{
  140.         if (harmonic){
  141.             seventh=1
  142.         }
  143.     }
  144.     L = [midi,midi+2, midi+3+add, midi+5, midi+7, midi+8+add, midi+10+add+seventh, midi+12]
  145.     return L
  146. }
  147.  
  148. function pentatonic(midi,major){
  149.     add = 0
  150.     if (major){
  151.         add=1
  152.     }
  153.     return [midi, midi+2, midi+3+add, midi+7, midi+8+add, midi+12]
  154. }
  155.  
  156. function sign(n){
  157.     if (n<0){
  158.         return -1
  159.     }
  160.     else if (n>0){
  161.         return 1
  162.     }
  163.     else{
  164.         return 0
  165.     }
  166. }
  167.  
  168. function list_sum(L){
  169.     sum=0
  170.     for (i=0; i<L.length; i++){
  171.         sum+=L[i]
  172.     }
  173.     return sum
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement