Skip to content

Commit

Permalink
add in a different audio system for dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
amcolash committed Dec 1, 2024
1 parent 180d815 commit dd59eb4
Show file tree
Hide file tree
Showing 32 changed files with 60 additions and 2 deletions.
Binary file added concept-art/audio/letters-andrew.aup3
Binary file not shown.
Binary file added concept-art/audio/letters-andrew.aup3-shm
Binary file not shown.
Binary file added concept-art/audio/letters-andrew.aup3-wal
Binary file not shown.
Binary file added public/assets/sounds/letters/a.mp3
Binary file not shown.
Binary file added public/assets/sounds/letters/b.mp3
Binary file not shown.
Binary file added public/assets/sounds/letters/c.mp3
Binary file not shown.
Binary file added public/assets/sounds/letters/d.mp3
Binary file not shown.
Binary file added public/assets/sounds/letters/e.mp3
Binary file not shown.
Binary file added public/assets/sounds/letters/f.mp3
Binary file not shown.
Binary file added public/assets/sounds/letters/g.mp3
Binary file not shown.
Binary file added public/assets/sounds/letters/h.mp3
Binary file not shown.
Binary file added public/assets/sounds/letters/i.mp3
Binary file not shown.
Binary file added public/assets/sounds/letters/j.mp3
Binary file not shown.
Binary file added public/assets/sounds/letters/k.mp3
Binary file not shown.
Binary file added public/assets/sounds/letters/l.mp3
Binary file not shown.
Binary file added public/assets/sounds/letters/m.mp3
Binary file not shown.
Binary file added public/assets/sounds/letters/n.mp3
Binary file not shown.
Binary file added public/assets/sounds/letters/o.mp3
Binary file not shown.
Binary file added public/assets/sounds/letters/p.mp3
Binary file not shown.
Binary file added public/assets/sounds/letters/q.mp3
Binary file not shown.
Binary file added public/assets/sounds/letters/r.mp3
Binary file not shown.
Binary file added public/assets/sounds/letters/s.mp3
Binary file not shown.
Binary file added public/assets/sounds/letters/t.mp3
Binary file not shown.
Binary file added public/assets/sounds/letters/u.mp3
Binary file not shown.
Binary file added public/assets/sounds/letters/v.mp3
Binary file not shown.
Binary file added public/assets/sounds/letters/w.mp3
Binary file not shown.
Binary file added public/assets/sounds/letters/x.mp3
Binary file not shown.
Binary file added public/assets/sounds/letters/y.mp3
Binary file not shown.
Binary file added public/assets/sounds/letters/z.mp3
Binary file not shown.
3 changes: 2 additions & 1 deletion src/classes/UI/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ export class Message extends GameObjects.Container {
const { promise: audioPromise, stop: stopAudio } = playMessageAudio(
message,
voice,
this.scene.sound.mute ? 0 : this.scene.sound.volume
this.scene.sound.mute ? 0 : this.scene.sound.volume,
this.scene
);
const { promise: textPromise, stop: stopAnimation } = animateText(this.text);

Expand Down
5 changes: 5 additions & 0 deletions src/scenes/Preloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ export class Preloader extends Scene {
// puzzles
this.load.image('arrow', 'puzzles/arrow.png');

// letters
Array.from({ length: 26 }, (_, i) => String.fromCharCode(97 + i)).forEach((l) =>
this.load.audio(l, `sounds/letters/${l}.mp3`)
);

// optionally preload intro
if (!localStorage.getItem(saveKey)) {
preloadIntro(this);
Expand Down
54 changes: 53 additions & 1 deletion src/utils/message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GameObjects } from 'phaser';
import { GameObjects, Scene } from 'phaser';

import { Voice } from '../data/voices';

Expand Down Expand Up @@ -82,6 +82,58 @@ function getPitch(note: Note, octave: number) {
}

export function playMessageAudio(
text: string,
voice: Voice,
gameVolume: number,
scene: Scene
): { promise: Promise<void>; stop?: () => void } {
// const { speed, octave, volume, type } = voice;
if (gameVolume === 0) return { promise: Promise.resolve() };

scene.sound.unlock();

const playAudioSequentially = (text: string, scene: Phaser.Scene) => {
let stopRequested = false;

const stop = () => {
stopRequested = true;
};

const playAudio = (key: string): Promise<void> => {
return new Promise((resolve) => {
const sound = scene.sound.add(key);
sound.on('complete', () => {
sound.destroy(); // Clean up after playback
resolve();
});
sound.play({ rate: 8, detune: (voice.octave - 4) * 300 });
});
};

const promise = new Promise<void>(async (resolve) => {
const letters = text.toLowerCase().split('');

// Only play a letter every so often to match with message timing
const letterSkip = 3.5;

for (let i = 0; i < letters.length; i += letterSkip) {
if (stopRequested) break;

const letter = letters[Math.floor(i)];
if (letter.charCodeAt(0) >= 97 && letter.charCodeAt(0) <= 122) {
await playAudio(letter);
}
}
resolve(); // Resolve the promise once all playback is complete
});

return { promise, stop };
};

return playAudioSequentially(text, scene);
}

export function playMessageAudio_Old(
text: string,
voice: Voice,
gameVolume: number
Expand Down

0 comments on commit dd59eb4

Please sign in to comment.