Guest User

Untitled

a guest
Jan 24th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. <html>
  2. <header>
  3.  
  4. <script lang="javascript" >
  5. var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
  6.  
  7. var sineTerms = new Float32Array([0, 0, 1, 0, 1]);
  8. var cosineTerms = new Float32Array(sineTerms.length);
  9. var customWaveform = audioCtx.createPeriodicWave(cosineTerms, sineTerms);
  10.  
  11. var pianoFreqKeyMap = {
  12. "]":"739.989",
  13. "'":"698.456",
  14. ";":"659.255",
  15. "p":"622.254",
  16. "l":"587.330",
  17. "o":"554.365",
  18. "k":"523.251",
  19. "j":"493.883",
  20. "u":"466.164",
  21. "h":"440.000",
  22. "y":"415.305",
  23. "g":"391.995",
  24. "t":"369.994",
  25. "f":"349.228",
  26. "d":"329.628",
  27. "e":"311.127",
  28. "s":"293.665",
  29. "w":"277.183",
  30. "a":"261.626"
  31. }
  32.  
  33. var keysToIgnore = {
  34. 'Alt': true
  35. }
  36.  
  37. var oscillators = {};
  38. function playNote(frequency, id) {
  39. // create Oscillator node
  40. var oscillator = audioCtx.createOscillator();
  41. oscillator.setPeriodicWave(customWaveform);
  42. if(oscillators[id]) {
  43. oscillators[id].stop();
  44.  
  45. }
  46.  
  47. oscillators[id] = oscillator;
  48. // oscillator.type = 'square';
  49. oscillator.frequency.value = frequency; // value in hertz
  50. oscillator.connect(audioCtx.destination);
  51. oscillator.start();
  52. }
  53.  
  54. function stopNote(id) {
  55. if(oscillators[id]) {
  56. oscillators[id].stop();
  57. oscillators[id] = null;
  58. }
  59. }
  60.  
  61. function startNote(e) {
  62. console.log(e);
  63.  
  64. console.log(`REPEAT: ${e.repeat}`)
  65. if(!e.repeat && !keysToIgnore[e.key]) {
  66. var freq = e.keyCode *20;
  67. if(pianoFreqKeyMap[e.key]) {
  68. freq = pianoFreqKeyMap[e.key]
  69. }
  70. playNote(freq, e.key);
  71.  
  72. displayKeys();
  73. }
  74.  
  75. }
  76.  
  77. function toColor(num) {
  78. num >>>= 0;
  79. var b = num & 0xFF,
  80. g = (num & 0xFF00) >>> 8,
  81. r = (num & 0xFF0000) >>> 16,
  82. a = ( (num & 0xFF000000) >>> 24 ) / 255 ;
  83. return "rgba(" + [r, g, b, a].join(",") + ")";
  84. }
  85.  
  86. function displayKeys() {
  87. // document.body.innerHTML = '';
  88. for (const key in oscillators) {
  89. if (oscillators.hasOwnProperty(key)) {
  90. const element = oscillators[key];
  91. if(element) {
  92. document.body.innerHTML = `<div style="background-color: ${toColor(element.frequency.value * 10000000)}"><h1>${key}</h1></div>` + document.body.innerHTML;
  93. }
  94.  
  95. }
  96. }
  97. }
  98.  
  99. function endNote(e) {
  100. console.log(e)
  101. if(!keysToIgnore[e.key]) {
  102. stopNote(e.key);
  103. }
  104. }
  105.  
  106. window.addEventListener("keydown", startNote);
  107. window.addEventListener("keyup", endNote);
  108. </script>
  109. </header>
  110.  
  111. <body>
  112.  
  113. </body>
  114. </html>
Add Comment
Please, Sign In to add comment