|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <script src="https://cesium.com/downloads/cesiumjs/releases/1.107.1/Build/Cesium/Cesium.js"></script> |
| 6 | + <link |
| 7 | + href="https://cesium.com/downloads/cesiumjs/releases/1.107.1/Build/Cesium/Widgets/widgets.css" |
| 8 | + rel="stylesheet" |
| 9 | + /> |
| 10 | + <style> |
| 11 | + html, |
| 12 | + body, |
| 13 | + #cesiumContainer { |
| 14 | + height: 100%; |
| 15 | + margin: 0; |
| 16 | + } |
| 17 | + #play { |
| 18 | + position: absolute; |
| 19 | + right: 30px; |
| 20 | + top: 30px; |
| 21 | + font-family: monospace; |
| 22 | + font-weight: bold; |
| 23 | + font-size: 1.6em; |
| 24 | + } |
| 25 | + </style> |
| 26 | + </head> |
| 27 | + |
| 28 | + <body> |
| 29 | + <audio src="goat.mp3"></audio> |
| 30 | + |
| 31 | + <div id="cesiumContainer"></div> |
| 32 | + <button id="play" type="button">play</button> |
| 33 | + <script type="module"> |
| 34 | + import { createViewer } from "./setup.js"; |
| 35 | + import CesiumSphereCamera from "../packages/cesium-sphere-camera/cesium-sphere-camera.js"; |
| 36 | + import CesiumBinoculars from "../packages/cesium-binoculars/cesium-binoculars.js"; |
| 37 | + import CesiumAudio from "../packages/cesium-audio/cesium-audio.js"; |
| 38 | + |
| 39 | + createViewer("cesiumContainer").then((viewer) => { |
| 40 | + const sphereMode = new CesiumSphereCamera(viewer); |
| 41 | + const binocularsMode = new CesiumBinoculars(viewer); |
| 42 | + sphereMode.active = true; |
| 43 | + binocularsMode.active = true; |
| 44 | + |
| 45 | + let init = false; |
| 46 | + let audioElement; |
| 47 | + let listener; |
| 48 | + document.querySelector("#play").addEventListener("click", () => { |
| 49 | + if (!init) { |
| 50 | + const audioCtx = new AudioContext(); |
| 51 | + listener = audioCtx.listener; |
| 52 | + |
| 53 | + // position of the listener |
| 54 | + listener.positionX.value = 0; |
| 55 | + listener.positionY.value = 0; |
| 56 | + listener.positionZ.value = 0; |
| 57 | + |
| 58 | + // forward direction of the listener |
| 59 | + listener.forwardX.value = 0; |
| 60 | + listener.forwardY.value = 0; |
| 61 | + listener.forwardZ.value = -1; |
| 62 | + |
| 63 | + // head of the listener |
| 64 | + listener.upX.value = 0; |
| 65 | + listener.upY.value = 1; |
| 66 | + listener.upZ.value = 0; |
| 67 | + |
| 68 | + const panner = new PannerNode(audioCtx); |
| 69 | + // Head-related transfer function |
| 70 | + panner.panningModel = "HRTF"; |
| 71 | + |
| 72 | + panner.coneInnerAngle = 90; |
| 73 | + panner.coneOuterAngle = 160; |
| 74 | + panner.coneOuterGain = 0.3; |
| 75 | + |
| 76 | + // panner.distanceModel = "linear"; |
| 77 | + |
| 78 | + // how quickly does the volume reduce as the panner moves away from the listener |
| 79 | + // panner.rolloffFactor = 10; |
| 80 | + |
| 81 | + panner.orientationX.value = 0; |
| 82 | + panner.orientationY.value = 0; |
| 83 | + panner.orientationZ.value = -1; |
| 84 | + |
| 85 | + // const audioElement = new Audio("goat.mp3"); |
| 86 | + audioElement = document.querySelector("audio"); |
| 87 | + const track = audioCtx.createMediaElementSource(audioElement); |
| 88 | + |
| 89 | + track.connect(panner).connect(audioCtx.destination); |
| 90 | + |
| 91 | + const cesiumAudio = new CesiumAudio(viewer, listener); |
| 92 | + init = true; |
| 93 | + } |
| 94 | + audioElement.play(); |
| 95 | + // listener.positionY.value -= 1; |
| 96 | + listener.positionX.value -= 1; |
| 97 | + }); |
| 98 | + }); |
| 99 | + </script> |
| 100 | + </body> |
| 101 | +</html> |
0 commit comments