Advertisement
drocta

4chordlayers

Nov 29th, 2015
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1.  
  2. /*!
  3. *
  4. * This is the four chord song.
  5. * ------------------
  6. *
  7. *I don't know any music, but I tried to make this play stuff while
  8.  
  9. messing around,
  10. * and I got sine and triangle waves to work.
  11. * and then I got chords to work
  12. * and then with looking at an example
  13.  
  14. I got it to play chords in sequence
  15. * and then I looked up the chords of the four chord song
  16. * and then the notes of those chords
  17. *
  18.  
  19. and then the frequencies of those notes
  20. * and then I had it play it with triangle and sine chords together.
  21. * I think it sounds
  22. (for use on wavepot.com)
  23. pretty neat.
  24. * -humbleElitist
  25. *
  26. */
  27.  
  28. export function dsp(t) {
  29. return generalWackyMusic(fourChordSong,t,t/10-1,note);
  30. //return rTriNote(261.6,t);
  31.  
  32. //simpleGeneralSong(fourChordSong,t,note);
  33. //return subNoise(t,10*abs(wackyMusic(fourChordSong,t*.9,3)))-0.5;
  34. //return
  35.  
  36. //simpleSquareSong(fourChordSong,t)*subNoise(t,1.5);
  37. //return simpleSineSong(fourChordSong,t) + 0.1*simpleSineSong(fourChordSong,t+0.0001*Math.random());
  38. //return (squareNote(413,t)+triangleNote(413,t)+note(413,t))/3 + 0.09*noise(t);
  39. //return wackyMusic
  40.  
  41. //(fourChordSong,t,5)+0.6*simpleTriangleSong(fourChordSong,t*1);
  42. //return 0.9*simpleTriangleSong(fourChordSong,t) + 0.9*simpleSineSong(fourChordSong,t);
  43. }
  44.  
  45. function note(f,t) {
  46. return 0.1 * Math.sin(2 * Math.PI * t * f);
  47. }
  48.  
  49. function chord(notes,t) {
  50. return notes.map(function(f){return note(f,t);}).reduce(function(a,b){return a+b;});
  51. }
  52.  
  53. function triangleNote(f,t){
  54. return 0.1 * ((t*f % 2) -1);
  55. }
  56.  
  57. function rTriNote(f,t){
  58.  
  59. return f*(t-Math.floor(t*f+0.5)/f)*(Math.pow(-1,Math.floor(t*f+0.5)));
  60. }
  61.  
  62. function triangleChord(notes,t) {
  63. return notes.map(function(f){return triangleNote(f,t);}).reduce(function(a,b){return a+b;});
  64. }
  65.  
  66. function simpleSineSong(chords,t){
  67. var p = chords[(t / 2 | 0) % chords.length];
  68.  
  69. return chord(p,t);
  70.  
  71. }
  72.  
  73. function simpleTriangleSong(chords,t){
  74. var p = chords[(t / 2 | 0) % chords.length];
  75.  
  76. return triangleChord(p,t);
  77.  
  78. }
  79.  
  80. function wackyMusic(chords,t,n){
  81. if(n<=0){
  82. return simpleSineSong(chords,t);
  83. }else{
  84. return simpleSineSong(chords,t)+0.5*wackyMusic(chords,t*2,n-1);
  85. }
  86. }
  87.  
  88. function squareNote(f,t){
  89.  
  90. if((f*t%2)>=1){
  91. return 0.1;
  92. }else{
  93. return -0.1;
  94. }
  95. }
  96.  
  97. function squareChord(notes,t) {
  98. return notes.map(function(f){return squareNote(f,t);}).reduce(function(a,b){return a+b;});
  99. }
  100.  
  101. //function simpleSquareSong(chords,t)
  102.  
  103. function generalChord(notes,t,noteType){
  104.  
  105.  
  106. return notes.map(function(f){return noteType(f,t);}).reduce(function(a,b){return a+b;});
  107. }
  108.  
  109. function simpleGeneralSong(chords,t,noteType){
  110. var p = chords[(t / 2 | 0) % chords.length];
  111.  
  112. return generalChord(p,t,noteType);
  113. }
  114.  
  115. function generalWackyMusic(chords,t,n,noteType){
  116. if(n<=0){
  117. return simpleGeneralSong(chords,t,noteType);
  118. }else{
  119. return simpleGeneralSong(chords,t,noteType)+0.5*generalWackyMusic(chords,t*2,n-1,noteType);
  120. }
  121. }
  122.  
  123. function noise(t){
  124. return Math.random();
  125. }
  126.  
  127. function subNoise(t,level){
  128. return (level+noise(t))/(1+level);
  129. }
  130.  
  131. function abs(value){
  132. return Math.abs(value);
  133. }
  134.  
  135.  
  136. function noteShift(f,n){
  137. return f*Math.pow(1.059463094,n);
  138. }
  139. var cNote=261.6;
  140.  
  141. var cMajChord=[261.6,329.6,392.0];
  142. var eMajChord=[329.6,415.3,493.9];// E - G# - B
  143. var bMajChord=[493.9,311.127,369.994];// B - D# - F#
  144. var cMinChord=[261.6,311.1,392.0];//C Eb G
  145. var cSharpMinChord=[277.2,329.6,415.3];// Root Note: C#
  146.  
  147. //Minor Third: E Perfect Fifth: G#
  148. var aMajChord=[220.0,277.2,329.6];//A C# E
  149.  
  150. var simpleTune=[bMajChord,cMajChord,bMajChord,cMajChord,eMajChord,cMinChord];
  151. var fourChordSong=[eMajChord,bMajChord,cSharpMinChord,aMajChord];// E
  152.  
  153. //major, B major, C# minor, and A major
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement