@tobyt/react-native-audio-browser 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/src/main/java/com/audiobrowser/AudioBrowser.kt +3 -2
- package/android/src/main/java/com/audiobrowser/ChosenChannelMode.kt +4 -1
- package/android/src/main/java/com/audiobrowser/LoopRangeCommand.kt +13 -0
- package/android/src/main/java/com/audiobrowser/player/Player.kt +100 -14
- package/android/src/main/java/com/audiobrowser/player/PlayerListener.kt +11 -6
- package/android/src/test/java/com/audiobrowser/ChosenChannelModeTest.kt +22 -0
- package/android/src/test/java/com/audiobrowser/LoopRangeCommandTest.kt +22 -0
- package/android/src/test/java/com/audiobrowser/player/LoopRangeCaseTest.kt +6 -0
- package/android/src/test/java/com/audiobrowser/player/LoopRangeEngineLifecycleTest.kt +170 -0
- package/android/src/test/java/com/audiobrowser/player/LoopRangePinSeekTest.kt +158 -0
- package/android/src/test/java/com/audiobrowser/player/PlayerTrackChangeTest.kt +20 -0
- package/ios/AVPlayerEngine.swift +2 -2
- package/ios/Player/PlaybackBoundaryTime.swift +11 -0
- package/ios/Player/Player.swift +3 -3
- package/ios/Tests/ChannelModeTapAudioMixTests.swift +144 -0
- package/ios/Tests/LoopRangeCaseTests.swift +14 -3
- package/ios/Tests/PlaybackBoundaryTimeTests.swift +20 -0
- package/ios/Tests/PlayerChannelModeTests.swift +18 -0
- package/ios/Tests/SupersedingTaskTests.swift +4 -1
- package/lib/commonjs/test-utils/fakeAudioContext.js +221 -0
- package/lib/commonjs/test-utils/fakeAudioContext.js.map +1 -0
- package/lib/commonjs/web/NativeAudioBrowser.js +14 -10
- package/lib/commonjs/web/NativeAudioBrowser.js.map +1 -1
- package/lib/commonjs/web/TrackPlayer/ChannelRouter.js +34 -11
- package/lib/commonjs/web/TrackPlayer/ChannelRouter.js.map +1 -1
- package/lib/commonjs/web/TrackPlayer/Player.js +6 -0
- package/lib/commonjs/web/TrackPlayer/Player.js.map +1 -1
- package/lib/commonjs/web/TrackPlayer/QueuePlayer.js +6 -2
- package/lib/commonjs/web/TrackPlayer/QueuePlayer.js.map +1 -1
- package/lib/module/test-utils/fakeAudioContext.js +212 -0
- package/lib/module/test-utils/fakeAudioContext.js.map +1 -0
- package/lib/module/web/NativeAudioBrowser.js +14 -10
- package/lib/module/web/NativeAudioBrowser.js.map +1 -1
- package/lib/module/web/TrackPlayer/ChannelRouter.js +34 -11
- package/lib/module/web/TrackPlayer/ChannelRouter.js.map +1 -1
- package/lib/module/web/TrackPlayer/Player.js +6 -0
- package/lib/module/web/TrackPlayer/Player.js.map +1 -1
- package/lib/module/web/TrackPlayer/QueuePlayer.js +6 -2
- package/lib/module/web/TrackPlayer/QueuePlayer.js.map +1 -1
- package/lib/typescript/src/test-utils/fakeAudioContext.d.ts +86 -0
- package/lib/typescript/src/test-utils/fakeAudioContext.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/test-utils/fakeAudioContext.ts +229 -0
- package/src/web/NativeAudioBrowser.audioProcessing.test.ts +7 -234
- package/src/web/NativeAudioBrowser.ts +19 -10
- package/src/web/TrackPlayer/ChannelRouter.ts +36 -12
- package/src/web/TrackPlayer/Player.channelMode.test.ts +159 -0
- package/src/web/TrackPlayer/Player.loopRange.test.ts +17 -4
- package/src/web/TrackPlayer/Player.ts +11 -0
- package/src/web/TrackPlayer/QueuePlayer.ts +4 -2
- package/src/web/__tests__/lifecycle.test.ts +53 -0
- package/src/web/__tests__/lifecycleHarness.ts +4 -2
|
@@ -3,13 +3,16 @@
|
|
|
3
3
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
4
4
|
import type { ChannelMode } from '../features'
|
|
5
5
|
import type { Track } from '../types'
|
|
6
|
+
import {
|
|
7
|
+
FakeAudioContext,
|
|
8
|
+
installFakeAudioContext
|
|
9
|
+
} from '../test-utils/fakeAudioContext'
|
|
6
10
|
import { NativeAudioBrowser } from './NativeAudioBrowser'
|
|
7
11
|
|
|
8
12
|
/**
|
|
9
13
|
* The web `web.audioProcessing` opt-in, through the real `setupPlayer`, a fake
|
|
10
|
-
* Shaka and a fake
|
|
11
|
-
*
|
|
12
|
-
* defines it, and the tests read which source channel reaches each ear.
|
|
14
|
+
* Shaka and a fake `AudioContext`. What each Channel Mode plays is in
|
|
15
|
+
* `TrackPlayer/Player.channelMode.test.ts`.
|
|
13
16
|
*/
|
|
14
17
|
|
|
15
18
|
const loads: { uri: string; crossOrigin: string | null }[] = []
|
|
@@ -44,177 +47,6 @@ vi.mock('shaka-player/dist/shaka-player.compiled', () => ({
|
|
|
44
47
|
}
|
|
45
48
|
}))
|
|
46
49
|
|
|
47
|
-
interface Edge {
|
|
48
|
-
to: FakeNode
|
|
49
|
-
output: number
|
|
50
|
-
input: number
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* One channel of a signal: the source channels summed into it. Gains are
|
|
55
|
-
* left out; only which source channel reaches which ear matters here.
|
|
56
|
-
*/
|
|
57
|
-
type Channel = string[]
|
|
58
|
-
|
|
59
|
-
const sourceChannelNames: Record<number, string[]> = {
|
|
60
|
-
1: ['M'],
|
|
61
|
-
2: ['L', 'R'],
|
|
62
|
-
4: ['L', 'R', 'SL', 'SR'],
|
|
63
|
-
6: ['L', 'R', 'C', 'LFE', 'SL', 'SR']
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const sum = (...channels: Channel[]): Channel =>
|
|
67
|
-
[...new Set(channels.flat())].sort()
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* The Web Audio spec's channel up-mixing and down-mixing of `input` to
|
|
71
|
-
* `count` channels. Only the speaker layouts this graph can meet are
|
|
72
|
-
* modelled; any other throws, so an unexpected mix fails the test.
|
|
73
|
-
*/
|
|
74
|
-
function mix(
|
|
75
|
-
input: Channel[],
|
|
76
|
-
count: number,
|
|
77
|
-
interpretation: string
|
|
78
|
-
): Channel[] {
|
|
79
|
-
if (input.length === count) return input
|
|
80
|
-
if (interpretation === 'discrete') {
|
|
81
|
-
return Array.from({ length: count }, (_, i) => input[i] ?? [])
|
|
82
|
-
}
|
|
83
|
-
const [l, r, c, , sl, sr] = input
|
|
84
|
-
if (count === 2 && input.length === 1) return [input[0]!, input[0]!]
|
|
85
|
-
if (count === 1 && input.length === 2) return [sum(l!, r!)]
|
|
86
|
-
if (count === 2 && input.length === 4) {
|
|
87
|
-
return [sum(l!, input[2]!), sum(r!, input[3]!)]
|
|
88
|
-
}
|
|
89
|
-
if (count === 2 && input.length === 6) {
|
|
90
|
-
return [sum(l!, c!, sl!), sum(r!, c!, sr!)]
|
|
91
|
-
}
|
|
92
|
-
throw new Error(`no speaker mix from ${input.length} to ${count}`)
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
class FakeNode {
|
|
96
|
-
edges: Edge[] = []
|
|
97
|
-
channelCount = 2
|
|
98
|
-
channelCountMode = 'max'
|
|
99
|
-
channelInterpretation = 'speakers'
|
|
100
|
-
constructor(
|
|
101
|
-
readonly kind: string,
|
|
102
|
-
readonly context: FakeAudioContext,
|
|
103
|
-
readonly ports = 1
|
|
104
|
-
) {
|
|
105
|
-
context.nodes.push(this)
|
|
106
|
-
if (kind === 'splitter') {
|
|
107
|
-
this.channelCount = ports
|
|
108
|
-
this.channelCountMode = 'explicit'
|
|
109
|
-
this.channelInterpretation = 'discrete'
|
|
110
|
-
} else if (kind === 'merger') {
|
|
111
|
-
this.channelCount = 1
|
|
112
|
-
this.channelCountMode = 'explicit'
|
|
113
|
-
} else if (kind === 'destination') {
|
|
114
|
-
this.channelCountMode = 'explicit'
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
connect(to: FakeNode, output = 0, input = 0): FakeNode {
|
|
118
|
-
this.edges.push({ to, output, input })
|
|
119
|
-
return to
|
|
120
|
-
}
|
|
121
|
-
disconnect(): void {
|
|
122
|
-
this.edges = []
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/** The node's input `input`, mixed as its channel properties say. */
|
|
126
|
-
private input(input: number): Channel[] {
|
|
127
|
-
const sources = this.context.nodes.flatMap((node) =>
|
|
128
|
-
node.edges
|
|
129
|
-
.filter((e) => e.to === this && e.input === input)
|
|
130
|
-
.map((e) => node.output(e.output))
|
|
131
|
-
)
|
|
132
|
-
if (sources.length === 0) return []
|
|
133
|
-
const widest = Math.max(...sources.map((s) => s.length))
|
|
134
|
-
const count =
|
|
135
|
-
this.channelCountMode === 'explicit'
|
|
136
|
-
? this.channelCount
|
|
137
|
-
: this.channelCountMode === 'clamped-max'
|
|
138
|
-
? Math.min(widest, this.channelCount)
|
|
139
|
-
: widest
|
|
140
|
-
const mixed = sources.map((s) => mix(s, count, this.channelInterpretation))
|
|
141
|
-
return Array.from({ length: count }, (_, i) =>
|
|
142
|
-
sum(...mixed.map((s) => s[i]!))
|
|
143
|
-
)
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/** What leaves output `output`. */
|
|
147
|
-
output(output: number): Channel[] {
|
|
148
|
-
switch (this.kind) {
|
|
149
|
-
case 'source':
|
|
150
|
-
return sourceChannelNames[this.context.sourceChannels]!.map((n) => [n])
|
|
151
|
-
case 'splitter':
|
|
152
|
-
return [this.input(0)[output]!]
|
|
153
|
-
case 'merger':
|
|
154
|
-
return Array.from(
|
|
155
|
-
{ length: this.ports },
|
|
156
|
-
(_, i) => this.input(i)[0] ?? []
|
|
157
|
-
)
|
|
158
|
-
default:
|
|
159
|
-
return this.input(0)
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/** What the node plays, for the destination. */
|
|
164
|
-
heard(): Channel[] {
|
|
165
|
-
return this.input(0)
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
class FakeAudioContext {
|
|
170
|
-
static instances: FakeAudioContext[] = []
|
|
171
|
-
state = 'suspended'
|
|
172
|
-
readonly nodes: FakeNode[] = []
|
|
173
|
-
readonly destination = new FakeNode('destination', this)
|
|
174
|
-
source?: FakeNode
|
|
175
|
-
sourceElement?: HTMLMediaElement
|
|
176
|
-
/** How many channels the element's current source has. */
|
|
177
|
-
sourceChannels = 2
|
|
178
|
-
resumes = 0
|
|
179
|
-
constructor() {
|
|
180
|
-
FakeAudioContext.instances.push(this)
|
|
181
|
-
}
|
|
182
|
-
createMediaElementSource(element: HTMLMediaElement): FakeNode {
|
|
183
|
-
if (this.source) throw new Error('InvalidStateError')
|
|
184
|
-
this.sourceElement = element
|
|
185
|
-
this.source = new FakeNode('source', this)
|
|
186
|
-
return this.source
|
|
187
|
-
}
|
|
188
|
-
createGain(): FakeNode {
|
|
189
|
-
return new FakeNode('gain', this)
|
|
190
|
-
}
|
|
191
|
-
createChannelSplitter(outputs: number): FakeNode {
|
|
192
|
-
return new FakeNode('splitter', this, outputs)
|
|
193
|
-
}
|
|
194
|
-
createChannelMerger(inputs: number): FakeNode {
|
|
195
|
-
return new FakeNode('merger', this, inputs)
|
|
196
|
-
}
|
|
197
|
-
resume(): Promise<void> {
|
|
198
|
-
this.resumes++
|
|
199
|
-
this.state = 'running'
|
|
200
|
-
return Promise.resolve()
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Which source channels each ear hears, for a source of `channels` channels:
|
|
206
|
-
* the graph's mixing worked out as the Web Audio spec defines it.
|
|
207
|
-
*/
|
|
208
|
-
function ears(
|
|
209
|
-
ctx: FakeAudioContext,
|
|
210
|
-
channels = 2
|
|
211
|
-
): { left: string[]; right: string[] } {
|
|
212
|
-
ctx.sourceChannels = channels
|
|
213
|
-
const [left, right, ...rest] = ctx.destination.heard()
|
|
214
|
-
expect(rest).toEqual([])
|
|
215
|
-
return { left: left!, right: right! }
|
|
216
|
-
}
|
|
217
|
-
|
|
218
50
|
const track = (n: number): Track => ({
|
|
219
51
|
id: `t${n}`,
|
|
220
52
|
src: `https://cdn.example.com/${n}.mp3`,
|
|
@@ -237,8 +69,7 @@ async function setup(web?: {
|
|
|
237
69
|
beforeEach(() => {
|
|
238
70
|
;(globalThis as unknown as { __DEV__: boolean }).__DEV__ = false
|
|
239
71
|
loads.length = 0
|
|
240
|
-
|
|
241
|
-
vi.stubGlobal('AudioContext', FakeAudioContext)
|
|
72
|
+
installFakeAudioContext()
|
|
242
73
|
document.body.innerHTML = ''
|
|
243
74
|
})
|
|
244
75
|
|
|
@@ -296,64 +127,6 @@ describe('web audioProcessing on', () => {
|
|
|
296
127
|
expect(FakeAudioContext.instances[0]!.sourceElement).toBe(element)
|
|
297
128
|
})
|
|
298
129
|
|
|
299
|
-
it("plays 'stereo' unchanged", async () => {
|
|
300
|
-
await setup({ audioProcessing: true })
|
|
301
|
-
|
|
302
|
-
expect(ears(FakeAudioContext.instances[0]!)).toEqual({
|
|
303
|
-
left: ['L'],
|
|
304
|
-
right: ['R']
|
|
305
|
-
})
|
|
306
|
-
})
|
|
307
|
-
|
|
308
|
-
it("plays 'left' and 'right' as centred mono", async () => {
|
|
309
|
-
const { browser } = await setup({ audioProcessing: true })
|
|
310
|
-
const ctx = FakeAudioContext.instances[0]!
|
|
311
|
-
|
|
312
|
-
browser.setChannelMode('left')
|
|
313
|
-
expect(ears(ctx)).toEqual({ left: ['L'], right: ['L'] })
|
|
314
|
-
|
|
315
|
-
browser.setChannelMode('right')
|
|
316
|
-
expect(ears(ctx)).toEqual({ left: ['R'], right: ['R'] })
|
|
317
|
-
|
|
318
|
-
browser.setChannelMode('stereo')
|
|
319
|
-
expect(ears(ctx)).toEqual({ left: ['L'], right: ['R'] })
|
|
320
|
-
})
|
|
321
|
-
|
|
322
|
-
it('applies a mode set before setup', async () => {
|
|
323
|
-
const browser = new NativeAudioBrowser()
|
|
324
|
-
browser.setChannelMode('right')
|
|
325
|
-
await browser.setupPlayer({ web: { audioProcessing: true } })
|
|
326
|
-
|
|
327
|
-
expect(ears(FakeAudioContext.instances[0]!)).toEqual({
|
|
328
|
-
left: ['R'],
|
|
329
|
-
right: ['R']
|
|
330
|
-
})
|
|
331
|
-
})
|
|
332
|
-
|
|
333
|
-
it('plays a mono source in both ears in every mode', async () => {
|
|
334
|
-
const { browser } = await setup({ audioProcessing: true })
|
|
335
|
-
const ctx = FakeAudioContext.instances[0]!
|
|
336
|
-
|
|
337
|
-
for (const mode of ['stereo', 'left', 'right'] as const) {
|
|
338
|
-
browser.setChannelMode(mode)
|
|
339
|
-
expect(ears(ctx, 1)).toEqual({ left: ['M'], right: ['M'] })
|
|
340
|
-
}
|
|
341
|
-
})
|
|
342
|
-
|
|
343
|
-
it.each([4, 6])(
|
|
344
|
-
'plays only the first two channels of a %i-channel source',
|
|
345
|
-
async (channels) => {
|
|
346
|
-
const { browser } = await setup({ audioProcessing: true })
|
|
347
|
-
const ctx = FakeAudioContext.instances[0]!
|
|
348
|
-
|
|
349
|
-
expect(ears(ctx, channels)).toEqual({ left: ['L'], right: ['R'] })
|
|
350
|
-
browser.setChannelMode('left')
|
|
351
|
-
expect(ears(ctx, channels)).toEqual({ left: ['L'], right: ['L'] })
|
|
352
|
-
browser.setChannelMode('right')
|
|
353
|
-
expect(ears(ctx, channels)).toEqual({ left: ['R'], right: ['R'] })
|
|
354
|
-
}
|
|
355
|
-
)
|
|
356
|
-
|
|
357
130
|
it('resumes the audio context on play', async () => {
|
|
358
131
|
const { browser } = await setup({ audioProcessing: true })
|
|
359
132
|
const ctx = FakeAudioContext.instances[0]!
|
|
@@ -52,6 +52,7 @@ import type {
|
|
|
52
52
|
TransformableRequestConfig
|
|
53
53
|
} from '../types'
|
|
54
54
|
import type { NativeBrowserConfiguration } from '../types/browser-native'
|
|
55
|
+
import type { LoadOptions } from './TrackPlayer/Player'
|
|
55
56
|
import { getTrackIdentity } from '../utils/getTrackIdentity'
|
|
56
57
|
import { BrowserManager } from './browser/BrowserManager'
|
|
57
58
|
import { classifyTrackNavigation } from './browser/classifyTrackNavigation'
|
|
@@ -88,6 +89,7 @@ export class NativeAudioBrowser
|
|
|
88
89
|
this.clearUpdateEventInterval()
|
|
89
90
|
this.setPlaybackIntervalEnabled(false)
|
|
90
91
|
this.remoteCommands.dispose()
|
|
92
|
+
this.closeChannelRouter()
|
|
91
93
|
// Remove window event listeners to prevent memory leaks
|
|
92
94
|
if (typeof window !== 'undefined' && this.onlineHandler) {
|
|
93
95
|
window.removeEventListener('online', this.onlineHandler)
|
|
@@ -697,20 +699,23 @@ export class NativeAudioBrowser
|
|
|
697
699
|
* keeps `activeTrack` ahead of `loading` — the order a consumer that reads
|
|
698
700
|
* `getActiveTrack()` on the loading edge depends on.
|
|
699
701
|
*/
|
|
700
|
-
private transitionTo(track: Track): void {
|
|
702
|
+
private transitionTo(track: Track, reloadsActiveTrack: boolean): void {
|
|
701
703
|
const element = this.requireElement()
|
|
702
704
|
const lastTrack = this.current
|
|
703
705
|
const lastPosition = element.currentTime
|
|
704
706
|
const lastIndex = this.queue.lastIndex
|
|
705
707
|
const currentIndex = this.queue.currentIndex
|
|
706
708
|
|
|
707
|
-
//
|
|
708
|
-
//
|
|
709
|
+
// One rule: every load but a reload of the current queue entry is a new
|
|
710
|
+
// Active Track. That clears the Loop Range, which belongs to the Active
|
|
711
|
+
// Track, and is announced. The same `Track` at another queue entry, or a
|
|
712
|
+
// `load()` of the Active Track itself, is a new Active Track, as on iOS;
|
|
713
|
+
// comparing `Track` objects would miss both. A reload (a retry, a play
|
|
714
|
+
// after a stop) is not announced: ExoPlayer's `prepare()` does not re-fire
|
|
709
715
|
// `onMediaItemTransition` for an unchanged media item, so a consumer that
|
|
710
|
-
// resets on a track change would have its UI wiped by every retry.
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
if (isTransition) this.clearLoopRange()
|
|
716
|
+
// resets on a track change would have its UI wiped by every retry.
|
|
717
|
+
const isNewActiveTrack = !reloadsActiveTrack
|
|
718
|
+
if (isNewActiveTrack) this.clearLoopRange()
|
|
714
719
|
|
|
715
720
|
// `current` first, so `getActiveTrack()` already reads the new track inside
|
|
716
721
|
// a handler and so `getNowPlaying()` publishes the new track's metadata. It
|
|
@@ -721,7 +726,7 @@ export class NativeAudioBrowser
|
|
|
721
726
|
|
|
722
727
|
// Separately guarded: a throwing consumer handler must not skip the
|
|
723
728
|
// publish, and neither may fail the load.
|
|
724
|
-
if (
|
|
729
|
+
if (isNewActiveTrack) {
|
|
725
730
|
// As Android's onMediaItemTransition does: a new item is a clean slate,
|
|
726
731
|
// a retry of the same one keeps what the consumer set.
|
|
727
732
|
this.nowPlayingManager.clearNowPlayingOverride()
|
|
@@ -769,7 +774,11 @@ export class NativeAudioBrowser
|
|
|
769
774
|
this.dispatch({ type: 'trackLoading' })
|
|
770
775
|
}
|
|
771
776
|
|
|
772
|
-
load(
|
|
777
|
+
load(
|
|
778
|
+
track: Track,
|
|
779
|
+
callback?: (track: Track) => void,
|
|
780
|
+
options?: Pick<LoadOptions, 'reloadsActiveTrack'>
|
|
781
|
+
): void {
|
|
773
782
|
// Before the queue is touched, so a load before `setupPlayer()` throws
|
|
774
783
|
// rather than half-applying.
|
|
775
784
|
this.requireElement()
|
|
@@ -796,7 +805,7 @@ export class NativeAudioBrowser
|
|
|
796
805
|
this._loadInProgress = true
|
|
797
806
|
this._pendingSeek = undefined
|
|
798
807
|
|
|
799
|
-
this.transitionTo(track)
|
|
808
|
+
this.transitionTo(track, reprepare || options?.reloadsActiveTrack === true)
|
|
800
809
|
|
|
801
810
|
// Resolve the media URL before loading (async but we don't await)
|
|
802
811
|
const doLoad = async () => {
|
|
@@ -2,26 +2,34 @@ import type { ChannelMode } from '../../features'
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Applies the Channel Mode on web by routing the audio element through a Web
|
|
5
|
-
* Audio graph:
|
|
5
|
+
* Audio graph. `'stereo'` plays the source unchanged, every channel of it:
|
|
6
|
+
*
|
|
7
|
+
* element → source → destination
|
|
8
|
+
*
|
|
9
|
+
* `'left'` and `'right'` play one channel centred:
|
|
6
10
|
*
|
|
7
11
|
* element → source → first two → up-mix → splitter(2) → merger(2) → destination
|
|
8
12
|
*
|
|
9
|
-
*
|
|
10
|
-
* element
|
|
11
|
-
*
|
|
13
|
+
* Created once per element, because a browser allows one
|
|
14
|
+
* `MediaElementAudioSourceNode` per element and the element has no direct
|
|
15
|
+
* output once it exists. Each mode change rewires the source's, the
|
|
16
|
+
* splitter's and the merger's outputs, so no stage of the other route stays
|
|
17
|
+
* connected.
|
|
12
18
|
*
|
|
13
19
|
* The two gain nodes before the splitter shape the source into the
|
|
14
20
|
* splitter's two channels, using the Web Audio channel mixing rules:
|
|
15
21
|
*
|
|
16
22
|
* - "first two" (`clamped-max`, 2, `discrete`) keeps a mono or stereo source
|
|
17
23
|
* as it is and drops every channel past the first two of a wider one, so a
|
|
18
|
-
* 5.1 source gives its front left
|
|
24
|
+
* 5.1 source gives its front left or right, not a speaker down-mix.
|
|
19
25
|
* - "up-mix" (`explicit`, 2, `speakers`) copies a mono source to both
|
|
20
26
|
* channels, so a mono source plays the same in every mode, and leaves two
|
|
21
27
|
* channels as they are.
|
|
22
28
|
*/
|
|
23
29
|
export class ChannelRouter {
|
|
24
30
|
private readonly context: AudioContext
|
|
31
|
+
private readonly source: MediaElementAudioSourceNode
|
|
32
|
+
private readonly firstTwo: GainNode
|
|
25
33
|
private readonly splitter: ChannelSplitterNode
|
|
26
34
|
private readonly merger: ChannelMergerNode
|
|
27
35
|
|
|
@@ -39,21 +47,28 @@ export class ChannelRouter {
|
|
|
39
47
|
this.splitter = context.createChannelSplitter(2)
|
|
40
48
|
this.merger = context.createChannelMerger(2)
|
|
41
49
|
|
|
42
|
-
source.connect(firstTwo)
|
|
43
50
|
firstTwo.connect(upmix)
|
|
44
51
|
upmix.connect(this.splitter)
|
|
45
|
-
this.merger.connect(context.destination)
|
|
46
52
|
this.context = context
|
|
53
|
+
this.source = source
|
|
54
|
+
this.firstTwo = firstTwo
|
|
47
55
|
this.setMode(mode)
|
|
48
56
|
}
|
|
49
57
|
|
|
50
|
-
/** Rewires the
|
|
58
|
+
/** Rewires the graph at once; the next render quantum hears it. */
|
|
51
59
|
setMode(mode: ChannelMode): void {
|
|
52
|
-
|
|
53
|
-
mode === 'left' ? [0, 0] : mode === 'right' ? [1, 1] : [0, 1]
|
|
60
|
+
this.source.disconnect()
|
|
54
61
|
this.splitter.disconnect()
|
|
55
|
-
this.
|
|
56
|
-
|
|
62
|
+
this.merger.disconnect()
|
|
63
|
+
if (mode === 'stereo') {
|
|
64
|
+
this.source.connect(this.context.destination)
|
|
65
|
+
return
|
|
66
|
+
}
|
|
67
|
+
const channel = mode === 'left' ? 0 : 1
|
|
68
|
+
this.source.connect(this.firstTwo)
|
|
69
|
+
this.splitter.connect(this.merger, channel, 0)
|
|
70
|
+
this.splitter.connect(this.merger, channel, 1)
|
|
71
|
+
this.merger.connect(this.context.destination)
|
|
57
72
|
}
|
|
58
73
|
|
|
59
74
|
/**
|
|
@@ -66,4 +81,13 @@ export class ChannelRouter {
|
|
|
66
81
|
if (this.context.state === 'running') return
|
|
67
82
|
this.context.resume().catch(() => {})
|
|
68
83
|
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Releases the context's audio hardware. Final: a browser caps how many
|
|
87
|
+
* contexts a page may hold, so a disposed player must not keep its own.
|
|
88
|
+
*/
|
|
89
|
+
close(): void {
|
|
90
|
+
if (this.context.state === 'closed') return
|
|
91
|
+
this.context.close().catch(() => {})
|
|
92
|
+
}
|
|
69
93
|
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/** @vitest-environment happy-dom */
|
|
2
|
+
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
4
|
+
import {
|
|
5
|
+
ears,
|
|
6
|
+
FakeAudioContext,
|
|
7
|
+
installFakeAudioContext,
|
|
8
|
+
onlyAudioContext,
|
|
9
|
+
reachingDestination
|
|
10
|
+
} from '../../test-utils/fakeAudioContext'
|
|
11
|
+
import { LifecycleHarness, trackA } from '../__tests__/lifecycleHarness'
|
|
12
|
+
import { NativeAudioBrowser } from '../NativeAudioBrowser'
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The web Channel Mode, through the real `setupPlayer` with
|
|
16
|
+
* `web.audioProcessing`, a fake Shaka and a fake `AudioContext` that records
|
|
17
|
+
* the audio graph. The tests read which source channels reach the
|
|
18
|
+
* destination, one entry per connected stage.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
async function setup(): Promise<NativeAudioBrowser> {
|
|
22
|
+
const browser = new NativeAudioBrowser()
|
|
23
|
+
await browser.setupPlayer({ web: { audioProcessing: true } })
|
|
24
|
+
return browser
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const sixChannels = [['L'], ['R'], ['C'], ['LFE'], ['SL'], ['SR']]
|
|
28
|
+
|
|
29
|
+
beforeEach(() => {
|
|
30
|
+
;(globalThis as unknown as { __DEV__: boolean }).__DEV__ = false
|
|
31
|
+
installFakeAudioContext()
|
|
32
|
+
document.body.innerHTML = ''
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
afterEach(() => {
|
|
36
|
+
vi.unstubAllGlobals()
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
describe('web Channel Mode', () => {
|
|
40
|
+
it("plays a stereo source unchanged in 'stereo'", async () => {
|
|
41
|
+
await setup()
|
|
42
|
+
|
|
43
|
+
expect(reachingDestination(onlyAudioContext())).toEqual([[['L'], ['R']]])
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it("plays every channel of a 6-channel source in 'stereo'", async () => {
|
|
47
|
+
await setup()
|
|
48
|
+
|
|
49
|
+
expect(reachingDestination(onlyAudioContext(), 6)).toEqual([sixChannels])
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it("plays one channel of a 6-channel source centred in 'left' and 'right'", async () => {
|
|
53
|
+
const browser = await setup()
|
|
54
|
+
const ctx = onlyAudioContext()
|
|
55
|
+
|
|
56
|
+
browser.setChannelMode('left')
|
|
57
|
+
expect(reachingDestination(ctx, 6)).toEqual([[['L'], ['L']]])
|
|
58
|
+
|
|
59
|
+
browser.setChannelMode('right')
|
|
60
|
+
expect(reachingDestination(ctx, 6)).toEqual([[['R'], ['R']]])
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it("leaves no stale stage connected from 'left' to 'stereo' to 'right'", async () => {
|
|
64
|
+
const browser = await setup()
|
|
65
|
+
const ctx = onlyAudioContext()
|
|
66
|
+
|
|
67
|
+
browser.setChannelMode('left')
|
|
68
|
+
expect(reachingDestination(ctx, 6)).toEqual([[['L'], ['L']]])
|
|
69
|
+
|
|
70
|
+
browser.setChannelMode('stereo')
|
|
71
|
+
expect(reachingDestination(ctx, 6)).toEqual([sixChannels])
|
|
72
|
+
|
|
73
|
+
browser.setChannelMode('right')
|
|
74
|
+
expect(reachingDestination(ctx, 6)).toEqual([[['R'], ['R']]])
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it('applies a mode set before setup', async () => {
|
|
78
|
+
const browser = new NativeAudioBrowser()
|
|
79
|
+
browser.setChannelMode('right')
|
|
80
|
+
await browser.setupPlayer({ web: { audioProcessing: true } })
|
|
81
|
+
|
|
82
|
+
expect(reachingDestination(onlyAudioContext())).toEqual([[['R'], ['R']]])
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
it('plays a mono source in both ears in every mode', async () => {
|
|
86
|
+
const browser = await setup()
|
|
87
|
+
const ctx = onlyAudioContext()
|
|
88
|
+
|
|
89
|
+
for (const mode of ['stereo', 'left', 'right'] as const) {
|
|
90
|
+
browser.setChannelMode(mode)
|
|
91
|
+
expect(ears(ctx, 1)).toEqual({ left: ['M'], right: ['M'] })
|
|
92
|
+
}
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
it("plays a 4-channel source's front channel centred in 'left' and 'right'", async () => {
|
|
96
|
+
const browser = await setup()
|
|
97
|
+
const ctx = onlyAudioContext()
|
|
98
|
+
|
|
99
|
+
browser.setChannelMode('left')
|
|
100
|
+
expect(ears(ctx, 4)).toEqual({ left: ['L'], right: ['L'] })
|
|
101
|
+
browser.setChannelMode('right')
|
|
102
|
+
expect(ears(ctx, 4)).toEqual({ left: ['R'], right: ['R'] })
|
|
103
|
+
})
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
describe('web Channel Mode AudioContext', () => {
|
|
107
|
+
async function harness(): Promise<LifecycleHarness> {
|
|
108
|
+
const h = new LifecycleHarness()
|
|
109
|
+
await h.setup({ web: { audioProcessing: true } })
|
|
110
|
+
return h
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
it('runs after an async setQueue with playWhenReady, without play()', async () => {
|
|
114
|
+
const h = await harness()
|
|
115
|
+
const ctx = onlyAudioContext()
|
|
116
|
+
|
|
117
|
+
h.browser.setQueue([trackA], 0)
|
|
118
|
+
h.browser.setPlayWhenReady(true)
|
|
119
|
+
await h.flush()
|
|
120
|
+
await h.completeLoad()
|
|
121
|
+
|
|
122
|
+
expect(h.browser.getPlayback().state).toBe('playing')
|
|
123
|
+
expect(ctx.state).toBe('running')
|
|
124
|
+
expect(reachingDestination(ctx)).toEqual([[['L'], ['R']]])
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
it('runs after an async load with playWhenReady, without play()', async () => {
|
|
128
|
+
const h = await harness()
|
|
129
|
+
const ctx = onlyAudioContext()
|
|
130
|
+
|
|
131
|
+
h.browser.setPlayWhenReady(true)
|
|
132
|
+
h.browser.load(trackA)
|
|
133
|
+
await h.flush()
|
|
134
|
+
await h.completeLoad()
|
|
135
|
+
|
|
136
|
+
expect(h.browser.getPlayback().state).toBe('playing')
|
|
137
|
+
expect(ctx.state).toBe('running')
|
|
138
|
+
expect(reachingDestination(ctx)).toEqual([[['L'], ['R']]])
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
it('closes on dispose(), and a new Player after it plays', async () => {
|
|
142
|
+
const first = await harness()
|
|
143
|
+
const firstCtx = onlyAudioContext()
|
|
144
|
+
|
|
145
|
+
first.browser.dispose()
|
|
146
|
+
expect(firstCtx.state).toBe('closed')
|
|
147
|
+
|
|
148
|
+
const second = await harness()
|
|
149
|
+
const [, secondCtx] = FakeAudioContext.instances
|
|
150
|
+
second.browser.load(trackA)
|
|
151
|
+
second.browser.play()
|
|
152
|
+
await second.flush()
|
|
153
|
+
await second.completeLoad()
|
|
154
|
+
|
|
155
|
+
expect(second.browser.getPlayback().state).toBe('playing')
|
|
156
|
+
expect(secondCtx!.state).toBe('running')
|
|
157
|
+
expect(reachingDestination(secondCtx!)).toEqual([[['L'], ['R']]])
|
|
158
|
+
})
|
|
159
|
+
})
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/** @vitest-environment happy-dom */
|
|
2
2
|
|
|
3
3
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
4
|
-
import type { LoopRange } from '../../features/loopRange'
|
|
5
4
|
import type { RepeatMode } from '../../features/queue/repeatMode'
|
|
6
5
|
import type { Track } from '../../types'
|
|
7
6
|
import cases from '../../../spec/loop-range-cases.json'
|
|
@@ -26,8 +25,16 @@ class TestPlayer extends NativeAudioBrowser {
|
|
|
26
25
|
await this.completeLoad()
|
|
27
26
|
}
|
|
28
27
|
|
|
28
|
+
private readonly tracks = new Map<string, Track>()
|
|
29
|
+
|
|
30
|
+
/** The fixture Track for `id`: the same object every time, as the cases require. */
|
|
29
31
|
track(id: string): Track {
|
|
30
|
-
|
|
32
|
+
let track = this.tracks.get(id)
|
|
33
|
+
if (!track) {
|
|
34
|
+
track = { id, title: id, src: `https://example.com/${id}.mp3` }
|
|
35
|
+
this.tracks.set(id, track)
|
|
36
|
+
}
|
|
37
|
+
return track
|
|
31
38
|
}
|
|
32
39
|
|
|
33
40
|
async completeLoad(): Promise<void> {
|
|
@@ -190,7 +197,7 @@ describe('Loop Range shared conformance', () => {
|
|
|
190
197
|
for (const step of scenario.steps) {
|
|
191
198
|
const action = step as {
|
|
192
199
|
action: string
|
|
193
|
-
range?:
|
|
200
|
+
range?: { start: number | string; end: number | string }
|
|
194
201
|
position?: number
|
|
195
202
|
offset?: number
|
|
196
203
|
seconds?: number
|
|
@@ -247,7 +254,10 @@ describe('Loop Range shared conformance', () => {
|
|
|
247
254
|
player.setActiveTrackFavorited(true)
|
|
248
255
|
break
|
|
249
256
|
case 'set':
|
|
250
|
-
player.setLoopRange(
|
|
257
|
+
player.setLoopRange({
|
|
258
|
+
start: Number(action.range!.start),
|
|
259
|
+
end: Number(action.range!.end)
|
|
260
|
+
})
|
|
251
261
|
break
|
|
252
262
|
case 'clear':
|
|
253
263
|
cleared = player.clearLoopRange()
|
|
@@ -326,6 +336,9 @@ describe('Loop Range shared conformance', () => {
|
|
|
326
336
|
case 'playWhenReady':
|
|
327
337
|
expect(player.playWhenReady).toBe(value)
|
|
328
338
|
break
|
|
339
|
+
case 'state':
|
|
340
|
+
expect(player.getPlayback().state).toBe(value)
|
|
341
|
+
break
|
|
329
342
|
case 'activeTrack':
|
|
330
343
|
expect(player.getActiveTrack()?.id).toBe(value ?? undefined)
|
|
331
344
|
break
|
|
@@ -51,6 +51,11 @@ export interface LoadOptions {
|
|
|
51
51
|
* `super.load()` after it, once the flag has been reset.
|
|
52
52
|
*/
|
|
53
53
|
reprepare?: boolean
|
|
54
|
+
/**
|
|
55
|
+
* Whether this load reloads the current queue entry rather than changing
|
|
56
|
+
* the Active Track — a skip to the entry already current while stopped.
|
|
57
|
+
*/
|
|
58
|
+
reloadsActiveTrack?: boolean
|
|
54
59
|
/** Headers resolved from the media configuration, applied to Shaka's requests. */
|
|
55
60
|
headers?: Record<string, string>
|
|
56
61
|
/**
|
|
@@ -805,6 +810,12 @@ export class Player {
|
|
|
805
810
|
return this._channelMode
|
|
806
811
|
}
|
|
807
812
|
|
|
813
|
+
/** Closes the Channel Mode's `AudioContext`, for `dispose()`. */
|
|
814
|
+
protected closeChannelRouter(): void {
|
|
815
|
+
this.channelRouter?.close()
|
|
816
|
+
this.channelRouter = undefined
|
|
817
|
+
}
|
|
818
|
+
|
|
808
819
|
public getProgress(): Progress {
|
|
809
820
|
const element = this.requireElement()
|
|
810
821
|
let buffered = 0
|