Skip to content

Commit

Permalink
✨ [Earwurm] Can now pass an optional request config (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
beefchimi authored Jan 25, 2023
1 parent 5ef336e commit d9dc55b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-pants-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'earwurm': patch
---

Can now pass a request object to the config for both Earwurm and Stack.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ import type {ManagerConfig} from '@beefchimi/earwurm';

// Optionally configure some global settings.
const customConfig: ManagerConfig = {
volume: 0.5,
requestOptions: {},
fadeMs: 200,
request: {},
};

// Initialize the global audio controller.
Expand Down
18 changes: 8 additions & 10 deletions docs/roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ While we are intentionally keeping the scope of `Earwurm` to an absolute minimum

## Tasks

1. Pass `requestOptions?: RequestInit;` to all `config` objects.
- Will allow consumers to customize the `fetch` request.
2. Ability to `seek()`.
3. Ability to set `speed`.
4. Ability to set `pitch`.
5. Calculate `elapsed` time per-sound.
1. Ability to `seek()`.
2. Ability to set `speed`.
3. Ability to set `pitch`.
4. Calculate `elapsed` time per-sound.
- If this can be achieved _(taking into consideration how `speed` will influence the calculation of `elapsed`)_, then we can add a `progress` event.
6. Support `sprite`.
7. Tests.
8. Finish docs.
5. Support `sprite`.
6. Tests.
7. Finish docs.
- We need to elaborate on returning a `scratchBuffer` if the `fetch` fails.
- If we failed to load the sound, return a “scratch buffer” so that we can still register an `ended` listener and call `.stop()` on the `source`. Since `Stack` will `emit` an `error` event with the `Sound > id`, consumer’s can listen for that event and perform their own handling.
9. …and more…
8. …and more…

## Considerations

Expand Down
3 changes: 3 additions & 0 deletions src/Earwurm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class Earwurm extends EmittenProtected<ManagerEventMap> {
#outputNode: AudioNode;

#fadeSec = 0;
#request: ManagerConfig['request'];
#library: Stack[] = [];
#suspendId: TimeoutId = 0;
#queuedResume = false;
Expand All @@ -42,6 +43,7 @@ export class Earwurm extends EmittenProtected<ManagerEventMap> {

this._volume = config?.volume ?? this._volume;
this.#fadeSec = config?.fadeMs ? msToSec(config.fadeMs) : this.#fadeSec;
this.#request = config?.request ?? undefined;
this.#outputNode = this.#gainNode.connect(this.#context.destination);

this.#gainNode.gain.setValueAtTime(this._volume, this.#context.currentTime);
Expand Down Expand Up @@ -127,6 +129,7 @@ export class Earwurm extends EmittenProtected<ManagerEventMap> {

const newStack = new Stack(id, path, this.#context, this.#outputNode, {
fadeMs: secToMs(this.#fadeSec),
request: this.#request,
});

newStack.on('statechange', this.#handleStackStateChange);
Expand Down
22 changes: 13 additions & 9 deletions src/Stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class Stack extends EmittenProtected<StackEventMap> {
#outputNode: AudioNode;
#fadeSec = 0;
#totalSoundsCreated = 0;
#request: StackConfig['request'];
#queue: Sound[] = [];

constructor(
Expand All @@ -49,6 +50,7 @@ export class Stack extends EmittenProtected<StackEventMap> {

this._volume = config?.volume ?? this._volume;
this.#fadeSec = config?.fadeMs ? msToSec(config.fadeMs) : this.#fadeSec;
this.#request = config?.request ?? undefined;

this.#gainNode = this.context.createGain();
this.#outputNode = this.#gainNode.connect(this.destination);
Expand Down Expand Up @@ -151,16 +153,18 @@ export class Stack extends EmittenProtected<StackEventMap> {
async #load() {
this.#setState('loading');

const result = await fetchAudioBuffer(this.path, this.context).catch(
(error) => {
this.emit(
'error',
Stack.#loadError(this.id, this.path, getErrorMessage(error)),
);
const result = await fetchAudioBuffer(
this.path,
this.context,
this.#request,
).catch((error) => {
this.emit(
'error',
Stack.#loadError(this.id, this.path, getErrorMessage(error)),
);

return scratchBuffer(this.context);
},
);
return scratchBuffer(this.context);
});

this.#setStateFromQueue();

Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface ManagerEventMap {
export interface ManagerConfig {
volume?: number;
fadeMs?: number;
request?: RequestInit;
}

export interface LibraryEntry {
Expand Down Expand Up @@ -43,6 +44,7 @@ export interface StackEventMap {
export interface StackConfig {
volume?: number;
fadeMs?: number;
request?: RequestInit;
}

///
Expand Down

0 comments on commit d9dc55b

Please sign in to comment.