Skip to content

Commit

Permalink
feat(audio): new audio resume command and fix for playing audio that …
Browse files Browse the repository at this point in the history
…was paused (#66)
  • Loading branch information
liana-p committed Apr 16, 2023
1 parent d1a1c95 commit 64713de
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 12 deletions.
11 changes: 6 additions & 5 deletions docs/commands/all-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ description: This page contains a list of all the existing narrat commands

#### Audio

| Command | Example | Description |
| ---------------------------------------------------------------------- | ------------------------------ | --------------------------------------------------------------------------------------------------------------------------- |
| [play](audio-commands-music-and-sounds/play-function.md#play-function) | `play music myMusic [channel]` | Plays the music `myMusic` in the mode `music` (possible modes: `music`, `ambiance, sound), with an optional channel number` |
| [pause](audio-commands-music-and-sounds/pause.md) | `pause music [channel]` | Pauses a music mode with an optional channel number |
| [stop](audio-commands-music-and-sounds/stop.md) | `stop music [channel]` | Same as pause but stops |
| Command | Example | Description |
| -------------------------------------------- | ------------------------------ | --------------------------------------------------------------------------------------------------------------------------- |
| [play](audio/play-function.md#play-function) | `play music myMusic [channel]` | Plays the music `myMusic` in the mode `music` (possible modes: `music`, `ambiance, sound), with an optional channel number` |
| [pause](audio/pause.md) | `pause music [channel]` | Pauses a music mode with an optional channel number |
| [resume]((audio/pause.md) /resume.md) | `resume music [channel]` | Resumes a music mode with an optional channel number |
| [stop](audio/stop.md) | `stop music [channel]` | Same as pause but stops |

#### Items

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ See [playing audio](../../features/audio.md) for more info on how to setup the a

Syntax: `$play [mode] [audioName] [channel (optional)]`

* mode: `music` , `ambiant` or `sound`
* channel: A number indicating which channel to play audio on. Defaults to 0. Can be used to play multiple musics in parallel on the same mode
- mode: `music` , `ambiant` or `sound`
- channel: A number indicating which channel to play audio on. Defaults to 0. Can be used to play multiple musics in parallel on the same mode

## Example

Expand Down
22 changes: 22 additions & 0 deletions docs/commands/audio/resume.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Resume

## Resume Function

The `resume` function resumes an audio channel

See [playing audio](../../features/audio.md) for more info on how to setup the audio system

Syntax: `$play [mode] [channel (optional)]`

- mode: `music` , `ambiant` or `sound`
- channel: A number indicating which channel to resume. Defaults to 0. Can be used to play multiple musics in parallel on the same mode

## Example

```
play music musicName
wait 3000
pause music
wait 200
resume music
```
File renamed without changes.
6 changes: 5 additions & 1 deletion docs/features/audio.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ It is also possible to pause or stop sounds or music:

`stop sound soundName` (name must be specified)

`pause music` will pause the music.

It can later be resumed with `resume music` or with `play music musicName` (the play command requires the name of the music to play)

For example, one could do a dramatic pause of the music

```
Expand All @@ -74,5 +78,5 @@ pause music
play sound scary
wait 100
talk character idle "Suddenly, something happened!"
play music # resume the music
resume music # resume the music
```
10 changes: 8 additions & 2 deletions packages/narrat/examples/games/default/scripts/default.nar
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@ main:
// if (== $global.counter 2):
// unlock_achievement win_game
// talk player idle "Global counter is %{$global.counter}"
// jump quest_demo
jump achievements_demo
jump quest_demo

achievements_demo:
talk helper idle "Let's play rock paper scissors!"
talk helper idle "I will choose one of the three options before you, and once you choose yours I'll reveal mine."
jump janken

audio_test:
play music battle
wait 3000
pause music
wait 500
resume music

janken:
talk helper idle "It's time to duel!"
set data.janken (new Array "rock" "paper" "scissors")
Expand Down
14 changes: 13 additions & 1 deletion packages/narrat/src/stores/audio-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,23 @@ export const useAudio = defineStore('audio', {
if (audioChannel && audioChannel.audio !== audio) {
await this.changeChannel(mode, audio, channelIndex);
} else if (audioChannel && audioChannel.audio === audio) {
// Ignore, it's the same music
await this.resumeChannel(mode, channelIndex);
} else {
await this.actuallyPlayChannel(mode, channelIndex, audio);
}
},
async resumeChannel(mode: AudioModeKey, channelIndex: number) {
const audioChannel = this.getAudioChannel(mode, channelIndex);
if (!audioChannel) {
return;
}
const audio = getAudio(audioChannel.audio);
if (!audio) {
error(`Could not find audio ${audioChannel.audio}`);
return;
}
audio.play(audioChannel.howlerId);
},
async changeChannel(
mode: AudioModeKey,
audio: string,
Expand Down
19 changes: 19 additions & 0 deletions packages/narrat/src/vm/commands/audio-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ export const playCommand = new CommandPlugin<PlayCommandArgs>(
},
);

export const resumeCommand = new CommandPlugin<{
mode: string;
channel?: number;
}>(
'resume',
[
{ name: 'mode', type: 'string' },
{ name: 'channel', type: 'number', optional: true },
],
async (cmd) => {
const audioStore = useAudio();
const { mode: modeInput, channel } = cmd.options;
const mode = validateAudioMode(cmd, modeInput);
if (mode) {
audioStore.resumeChannel(mode, channel ?? 0);
}
},
);

export const pauseCommand = new CommandPlugin<{
mode: string;
channel?: number;
Expand Down
8 changes: 7 additions & 1 deletion packages/narrat/src/vm/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ import {
savePlugin,
savePromptPlugin,
} from './flow-commands';
import { pauseCommand, playCommand, stopCommand } from './audio-commands';
import {
pauseCommand,
playCommand,
resumeCommand,
stopCommand,
} from './audio-commands';
import { addPlugin, setCommand } from './set';
import {
emptyLayerCommand,
Expand Down Expand Up @@ -151,6 +156,7 @@ export function registerBaseCommands(vm: VM) {
// Audio
vm.addCommand(pauseCommand);
vm.addCommand(playCommand);
vm.addCommand(resumeCommand);
vm.addCommand(stopCommand);

// Screens
Expand Down

0 comments on commit 64713de

Please sign in to comment.