feat: Add SoundManager for managing music/fx/audio in a friendly way (#3472)
Closes #590
Related #1161
## Changes:
- Added a new `ex.Sound({...})` option back constructor to set all the props you could set on sound at constructor time
- Added new `ex.SoundManager({...}` api to manage volume and muting on large groups of sounds at once
```typescript
var soundManager = new ex.SoundManger({
channels: ['fx', 'music', 'background'],
sounds: {
jumpSnd: { sound: jumpSnd, volume: 0.4, channels: ['fx'] },
forestSnd: { sound: forestSnd, volume: 0.2, channels: ['music', 'background'] },
challengeMusic: { sound: challengeMusic, volume: 0.2, channels: ['music'] },
guitarLoop: { sound: guitarLoop, volume: 0.2, channels: ['music'] }
}
});
toggleMusic.on('pointerdown', () => {
soundManager.channel.toggle('music');
});
game.add(toggleMusic);
game.input.keyboard.on('press', (evt) => {
if (evt.key === ex.Keys.J) {
soundManager.play('jumpSnd');
}
if (evt.key === ex.Keys.M) {
soundManager.channel.mute('music');
}
if (evt.key === ex.Keys.A) {
soundManager.mute();
}
if (evt.key === ex.Keys.S) {
soundManager.unmute();
}
if (evt.key === ex.Keys.U) {
soundManager.channel.unmute('music');
}
if (evt.key === ex.Keys.P) {
soundManager.channel.play('music');
}
if (evt.key === ex.Keys.V) {
soundManager.channel.setVolume('music', 0.9);
}
});
```
chore: migrate tests to vitest (#3381)
managed to migrate the engine leak & memory reporters, although it took a bit of rigamarole. Mainly because, as opposed to karma, the reporter runs in node and not the browser. So I have to track the data needed separately in some global hooks _within_ the browser environment, which the reporter then reads and creates the logs if needed.
However, it seems the memory tracking is regularly reporting >1 mb, so I'm not sure if it's tracking properly or if things have changed with the new browsers. My only theory is that because the timing where memory is read is during an `afterEach` _before_ the test's `afterEach`, it's running before any potential cleanup. The timing of this is not something that's easy to control, unfortunately. I did try to prove this theory by doing the memory analysis on the next test's `beforeEach`, but it didnt seem to change the results, so it may not be an issue.
Implementation is done in `src/spec/vitest/__reporters__/memory.ts` and `src/spec/vitest/__reporters/memory.setup.ts`
---
I noticed CouroutineSpec was an offender for >10mb memory, which spins up additional engines. I added a short 100ms wait after the engine.dispose and the memory usage did drop, so it does seem like this is prone to scheduled garbage collecting.
---
I was able to run the garbage collector (if exposed, currently only on chrome) and this makes the reports more accurate
feat: Add SoundManager for managing music/fx/audio in a friendly way (#3472)
Closes #590
Related #1161
## Changes:
- Added a new `ex.Sound({...})` option back constructor to set all the props you could set on sound at constructor time
- Added new `ex.SoundManager({...}` api to manage volume and muting on large groups of sounds at once
```typescript
var soundManager = new ex.SoundManger({
channels: ['fx', 'music', 'background'],
sounds: {
jumpSnd: { sound: jumpSnd, volume: 0.4, channels: ['fx'] },
forestSnd: { sound: forestSnd, volume: 0.2, channels: ['music', 'background'] },
challengeMusic: { sound: challengeMusic, volume: 0.2, channels: ['music'] },
guitarLoop: { sound: guitarLoop, volume: 0.2, channels: ['music'] }
}
});
toggleMusic.on('pointerdown', () => {
soundManager.channel.toggle('music');
});
game.add(toggleMusic);
game.input.keyboard.on('press', (evt) => {
if (evt.key === ex.Keys.J) {
soundManager.play('jumpSnd');
}
if (evt.key === ex.Keys.M) {
soundManager.channel.mute('music');
}
if (evt.key === ex.Keys.A) {
soundManager.mute();
}
if (evt.key === ex.Keys.S) {
soundManager.unmute();
}
if (evt.key === ex.Keys.U) {
soundManager.channel.unmute('music');
}
if (evt.key === ex.Keys.P) {
soundManager.channel.play('music');
}
if (evt.key === ex.Keys.V) {
soundManager.channel.setVolume('music', 0.9);
}
});
```