sound via ToneJS

webchuck
Fabien Benetou 2 years ago
parent c6588752a0
commit 35aa65b4af
  1. 7
      index.html

@ -26,6 +26,7 @@
<script src="https://cdn.jsdelivr.net/gh/c-frame/aframe-physics-system@v4.2.2/dist/aframe-physics-system.min.js"></script> <script src="https://cdn.jsdelivr.net/gh/c-frame/aframe-physics-system@v4.2.2/dist/aframe-physics-system.min.js"></script>
<script src='dependencies/mannequin.js'></script> <script src='dependencies/mannequin.js'></script>
<script src="https://cdn.jsdelivr.net/npm/webmidi@next/dist/iife/webmidi.iife.js"></script> <script src="https://cdn.jsdelivr.net/npm/webmidi@next/dist/iife/webmidi.iife.js"></script>
<script src="https://unpkg.com/tone"></script>
<!-- replacing with local copies as CDNs are like unpkg tend to be slow <!-- replacing with local copies as CDNs are like unpkg tend to be slow
<script type="module" src="https://unpkg.com/immers-client/dist/destination.bundle.js"></script> <script type="module" src="https://unpkg.com/immers-client/dist/destination.bundle.js"></script>
@ -2420,21 +2421,23 @@ function startWebMIDI(){
} }
let notesPlaying = {} let notesPlaying = {}
let synth
function webMidiStarted(){ function webMidiStarted(){
let midiStarted = Date.now() let midiStarted = Date.now()
console.log("WebMidi enabled!") console.log("WebMidi enabled!")
synth = new Tone.Synth().toDestination()
//let myInput = WebMidi.getInputByName("MPK mini Play mk3"); // on Quest 2 //let myInput = WebMidi.getInputByName("MPK mini Play mk3"); // on Quest 2
let myInput = WebMidi.getInputByName("MPK mini Play mk3:MPK mini Play mk3"); // on desktop let myInput = WebMidi.getInputByName("MPK mini Play mk3:MPK mini Play mk3"); // on desktop
WebMidi.inputs.forEach(input => console.log(input.manufacturer, input.name)); WebMidi.inputs.forEach(input => console.log(input.manufacturer, input.name));
myInput.addListener("noteon", e => { myInput.addListener("noteon", e => {
let identifierNoOctave = e.note.identifier.slice(0,-1) let identifierNoOctave = e.note.identifier.slice(0,-1)
let pos = ""+ (Object.getOwnPropertyNames(notes).indexOf(identifierNoOctave)/40+e.note.octave/3.3-1.1)+ " 1.2 -.2" let pos = ""+ (Object.getOwnPropertyNames(notes).indexOf(identifierNoOctave)/40+e.note.octave/3.3-1.1)+ " 1.5 -.1"
let b = addBlockCodeExample(identifierNoOctave, pos) let b = addBlockCodeExample(identifierNoOctave, pos)
b.setAttribute("rotation", "-90 90 0") b.setAttribute("rotation", "-90 90 0")
b.classList.add("musicalnote") // until now often assumed as single class, probably will lead to bugs b.classList.add("musicalnote") // until now often assumed as single class, probably will lead to bugs
let inlineColored = [...b.querySelectorAll(".outlined_object")].map( el => el.setAttribute("color", notes[identifierNoOctave]) ) let inlineColored = [...b.querySelectorAll(".outlined_object")].map( el => el.setAttribute("color", notes[identifierNoOctave]) )
notesPlaying[e.note.identifier] = true; notesPlaying[e.note.identifier] = true;
console.log(e.note); synth.triggerAttackRelease(e.note.identifier, "8n");
}); // for now assuming an arbitrary duration but should rely on notesPlaying }); // for now assuming an arbitrary duration but should rely on notesPlaying
myInput.addListener("noteoff", e => { notesPlaying[e.note.identifier] = false; console.log(e.note); }); // could be used for note duration myInput.addListener("noteoff", e => { notesPlaying[e.note.identifier] = false; console.log(e.note); }); // could be used for note duration
setInterval( _=> pushBackClass("musicalnote", .001), 10) setInterval( _=> pushBackClass("musicalnote", .001), 10)

Loading…
Cancel
Save