From 6c5f72e1f286f2b73f8b940d117b9cec4b197ae1 Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Sat, 13 Jul 2024 20:31:27 +0300 Subject: [PATCH] feat: implement pitch which was required for note blocks to work properly --- prismarine-viewer/viewer/lib/viewer.ts | 5 +++-- src/soundSystem.ts | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/prismarine-viewer/viewer/lib/viewer.ts b/prismarine-viewer/viewer/lib/viewer.ts index c95127f2..504c4637 100644 --- a/prismarine-viewer/viewer/lib/viewer.ts +++ b/prismarine-viewer/viewer/lib/viewer.ts @@ -32,7 +32,7 @@ export class Viewer { this.world.camera = camera } - constructor(public renderer: THREE.WebGLRenderer, worldConfig = defaultWorldRendererConfig) { + constructor (public renderer: THREE.WebGLRenderer, worldConfig = defaultWorldRendererConfig) { // https://discourse.threejs.org/t/updates-to-color-management-in-three-js-r152/50791 THREE.ColorManagement.enabled = false renderer.outputColorSpace = THREE.LinearSRGBColorSpace @@ -118,7 +118,7 @@ export class Viewer { this.world.updateCamera(pos?.offset(0, yOffset, 0) ?? null, yaw, pitch) } - playSound (position: Vec3, path: string, volume = 1) { + playSound (position: Vec3, path: string, volume = 1, pitch = 1) { if (!this.audioListener) { this.audioListener = new THREE.AudioListener() this.camera.add(this.audioListener) @@ -134,6 +134,7 @@ export class Viewer { sound.setBuffer(buffer) sound.setRefDistance(20) sound.setVolume(volume) + sound.setPlaybackRate(pitch) // set the pitch this.scene.add(sound) // set sound position sound.position.set(position.x, position.y, position.z) diff --git a/src/soundSystem.ts b/src/soundSystem.ts index e7fdaee7..d49b6a8c 100644 --- a/src/soundSystem.ts +++ b/src/soundSystem.ts @@ -50,7 +50,7 @@ subscribeKey(miscUiState, 'gameLoaded', async () => { const isMuted = options.mutedSounds.includes(soundKey) || options.volume === 0 if (position) { if (!isMuted) { - viewer.playSound(position, url, soundVolume * Math.max(Math.min(volume, 1), 0) * (options.volume / 100)) + viewer.playSound(position, url, soundVolume * Math.max(Math.min(volume, 1), 0) * (options.volume / 100), Math.max(Math.min(pitch ?? 1, 2), 0.5)) } if (getDistance(bot.entity.position, position) < 4 * 16) { lastPlayedSounds.lastServerPlayed[soundKey] ??= { count: 0, last: 0 }