Advertisement
Guest User

Piano code 1st Draft

a guest
Sep 24th, 2020
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. // Kanav Arora
  2. // Piano Instrument with gif
  3. // Refrence P5.js library for osc,Notekeys,Frequency of sound
  4.  
  5. let notes = [60, 62, 64, 65, 67, 69, 71, 72];
  6. let blackkeys = [61, 63, 66, 68, 70];
  7. let index = 0;
  8. let trigger = 0;
  9. let osc;
  10.  
  11.  
  12.  
  13.  
  14.  
  15. function setup() {
  16. createCanvas(800, 600);
  17.  
  18.  
  19. osc = new p5.TriOsc();
  20.  
  21. osc.start();
  22. osc.amp(0);
  23.  
  24. }
  25. function playNote(note, duration) {
  26. osc.freq(midiToFreq(note));
  27.  
  28. osc.fade(0.5,0.2);
  29.  
  30.  
  31. if (1) {
  32. setTimeout(function() {
  33. osc.fade(0,0.2);
  34. }, duration-50);
  35. }
  36. }
  37.  
  38.  
  39. function draw() {
  40. background(255);
  41.  
  42.  
  43. stroke(5);
  44. line(width / 8, 0, width / 8, height);
  45. line(2 * width / 8, 0, 2 * width / 8, height);
  46. line(3 * width / 8, 0, 3 * width / 8, height);
  47. line(4 * width / 8, 0, 4 * width / 8, height);
  48. line(5 * width / 8, 0, 5 * width / 8, height);
  49. line(6 * width / 8, 0, 6 * width / 8, height);
  50. line(7 * width / 8, 0, 7 * width / 8, height);
  51. line(8 * width / 8, 0, 8 * width / 8, height);
  52.  
  53. // white rect 1
  54. if (mouseX < width / 8 && mouseX > 0) {
  55. fill('pink');
  56. rect(0, 0, width / 8, height);
  57.  
  58. // white rect 2
  59. } else if (mouseX < 2 * width / 8) {
  60. fill('lightblue');
  61. rect(width / 8, 0, width / 8, height);
  62.  
  63. // white rect 3
  64. } else if (mouseX < 3 * width / 8) {
  65. fill('pink');
  66. rect(2 * width / 8, 0, width / 8, height);
  67.  
  68. // white rect 4
  69. } else if (mouseX < 4 * width / 8) {
  70. fill('lightblue');
  71. rect(3 * width / 8, 0, width / 8, height);
  72.  
  73. // white rect 5
  74. } else if (mouseX < 5 * width / 8) {
  75. fill('pink');
  76. rect(4 * width / 8, 0, width / 8, height);
  77.  
  78. // white rect 6
  79. } else if (mouseX < 6 * width / 8) {
  80. fill('pink');
  81. rect(5 * width / 8, 0, width / 8, height);
  82.  
  83. // white rect7
  84. } else if (mouseX < 7 * width / 8) {
  85. fill('lightblue');
  86. rect(6 * width / 8, 0, width / 8, height);
  87.  
  88. //white rect 8
  89. } else if (mouseX < 8 * width / 8) {
  90. fill('pink');
  91. rect(7 * width / 8, 0, width / 8, height);
  92. }
  93.  
  94.  
  95.  
  96. // black rect 1
  97. fill(200);
  98. rect(3 / 4 * width / 8, 0, width / 16, height / 2)
  99.  
  100. if (mouseX >= 3 / 32 * width && mouseX <= 5 / 32 * width&&mouseY<height/2) {
  101. if (mouseIsPressed) {
  102. //play music
  103. fill(255);
  104. rect(3 / 4 * width / 8, 0, width / 16, height / 2);
  105. } else {
  106. fill(200);
  107. }
  108.  
  109. }
  110.  
  111.  
  112. // black rect 2
  113. fill(0);
  114. rect(7 / 4 * width / 8, 0, width / 16, height / 2);
  115. if (mouseX >= 7 / 32 * width && mouseX <= 9 / 32 * width &&mouseY<height/2) {
  116. if (mouseIsPressed) {
  117. fill(255);
  118. rect(7 / 4 * width / 8, 0, width / 16, height / 2);
  119. } else {
  120. fill(0);
  121. }
  122. }
  123.  
  124.  
  125. // black rect 4
  126.  
  127. fill(0);
  128. rect(15 / 4 * width / 8, 0, width / 16, height / 2)
  129.  
  130. if (mouseX >= 15 / 32 * width && mouseX <= 17 / 32 * width && mouseY<height/2) {
  131. if (mouseIsPressed) {
  132. //play music
  133. fill(255);
  134. rect(15 / 4 * width / 8, 0, width / 16, height / 2);
  135. } else {
  136. fill(0);
  137. }
  138.  
  139. }
  140.  
  141.  
  142. // black rect 5
  143. fill(200);
  144. rect(19 / 4 * width / 8, 0, width / 16, height / 2);
  145. if (mouseX >= 19 / 32 * width && mouseX <= 21 / 32 * width&&mouseY<height/2) {
  146. if (mouseIsPressed) {
  147. //play music
  148. fill(255);
  149. rect(19 / 4 * width / 8, 0, width / 16, height / 2);
  150. } else {
  151. fill(200);
  152. }
  153.  
  154. }
  155. // black rect 6
  156.  
  157. fill(0);
  158. rect(23 / 4 * width / 8, 0, width / 16, height / 2);
  159. if (mouseX >= 23 / 32 * width && mouseX <= 25 / 32 * width&&mouseY<height/2) {
  160. if (mouseIsPressed) {
  161. //play music
  162. fill(255);
  163. rect(23 / 4 * width / 8, 0, width / 16, height / 2);
  164. } else {
  165. fill(0);
  166. }
  167.  
  168. }
  169.  
  170. // black rect 7
  171.  
  172. fill(200);
  173. rect(31 / 4 * width / 8, 0, width / 32, height / 2);
  174. if (mouseX >= 31 / 32 * width && mouseX <= 33 / 32 * width&&mouseY<height/2) {
  175. if (mouseIsPressed) {
  176. fill(255);
  177. rect(31 / 4 * width / 8, 0, width / 32, height / 2);
  178. } else {
  179. fill(200);
  180. }
  181.  
  182. }
  183.  
  184.  
  185. }
  186. function mousePressed(event) {
  187. if(event.button == 0 && event.clientX < width && event.clientY < height) {
  188.  
  189. let key = floor(map(mouseX, 0, width, 0, notes.length));
  190. playNote(notes[key]);
  191. }
  192. }
  193.  
  194.  
  195. function mouseReleased() {
  196. osc.fade(0,0.5);
  197. }
  198.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement