@octane-xplat/audio 0.19.0 → 0.20.0

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/README.md CHANGED
@@ -1,117 +1,5 @@
1
1
  # `@octane-xplat/audio`
2
2
 
3
- Long-form audio playback for Octane xplat apps — a queue player, not a
4
- sound-effect bank. iOS uses `AVPlayer`/`AVAudioSession` (interruption
5
- handling included) and Android uses a package-owned Media3 playback service.
6
- macOS uses a package-owned AVPlayer bridge. Web uses an
7
- `HTMLAudioElement` plus the Media Session API for system controls.
3
+ [Documentation](https://octane-xplat.goddardai.org/package-audio).
8
4
 
9
- ```sh
10
- bun add @octane-xplat/audio
11
- ```
12
-
13
- ```ts
14
- import { createAudioPlayer } from '@octane-xplat/audio'
15
-
16
- const player = createAudioPlayer()
17
- await player.setQueue([{ id: 'ep1', source: 'https://example.com/ep1.mp3' }])
18
- await player.setPlaybackRate(1.5)
19
- await player.setVolume(0.4)
20
- await player.setMuted(false)
21
- await player.play()
22
-
23
- const off = player.subscribe((snapshot) => {
24
- // { state, currentTime, duration } — poll or subscribe, your choice
25
- })
26
- ```
27
-
28
- Playback features vary by target and runtime state, so call
29
- `capabilities()` before promising behavior like lock-screen controls. It
30
- reports `supported`, `backgroundPlayback`, `systemControls`,
31
- `userGestureRequired`, and `interruptions`. The `macos` package condition
32
- selects the desktop bridge without importing mobile implementations. Rebuild
33
- the macOS app after installing this package. macOS reports system controls and
34
- interruption recovery as unavailable; a missing bridge reports `supported: false`.
35
-
36
- ```ts
37
- // Continue with the player created above.
38
- const capabilities = player.capabilities()
39
- console.log(capabilities.backgroundPlayback, capabilities.systemControls)
40
- off()
41
- player.dispose()
42
- ```
43
-
44
- On macOS, use an absolute local path, `file:` URL, or HTTP(S) URL. Queue
45
- replacement rejects obsolete pending loads and stops the previous item.
46
- Malformed media, network errors, reported output errors, and loading or
47
- buffering stalled for 30 seconds become terminal errors. A new queue can retry;
48
- disposal stops playback and notifications. Position, duration, and seek use
49
- seconds. Real audible playback remains a separate qualification gate.
50
-
51
- ```ts
52
- import { createAudioPlayer } from '@octane-xplat/audio'
53
-
54
- export async function playTrack(source: string) {
55
- const player = createAudioPlayer()
56
- try {
57
- await player.setQueue([{ id: 'track', source }])
58
- await player.play()
59
- await player.seek(2.5)
60
- await player.pause()
61
- } finally {
62
- player.dispose()
63
- }
64
- }
65
- ```
66
-
67
- For short UI sounds use [`@octane-xplat/sounds`](https://github.com/octane-xplat/octane-xplat/blob/main/packages/sounds/README.md).
68
-
69
- Native Windows selects an unavailable leaf without loading mobile backends.
70
- Creating a player throws; this import boundary does not provide Windows playback.
71
-
72
- ```ts
73
- import { createAudioPlayer } from '@octane-xplat/audio'
74
-
75
- try {
76
- const candidate = createAudioPlayer()
77
- candidate.dispose()
78
- } catch (error) {
79
- console.log('Audio unavailable:', String(error))
80
- }
81
- ```
82
-
83
- Guide: [Media services](https://octane-xplat.goddardai.org/media-services); per-target
84
- availability: [platform notes](https://github.com/octane-xplat/octane-xplat/blob/main/.agents/docs/notes/platform-notes.md). Exercised by
85
- the harness `MediaServices` screen
86
- ([`packages/app/src/MediaServices.tsrx`](https://github.com/octane-xplat/octane-xplat/blob/main/packages/app/src/MediaServices.tsrx)).
87
-
88
- ## Foreground verification
89
-
90
- The maintained silent-WAV fixture measures observable player state against
91
- fixed budgets. Run it on an owned build host and iOS simulator (or Android
92
- emulator with `--target android`). Its local HTTP addresses target virtual
93
- devices; physical media acceptance needs a reachable server and a separate run:
94
-
95
- ```sh
96
- bun run turbo run test:browser --filter=@octane-xplat/audio -- --foreground-budgets
97
- bun run turbo run test:runtime --filter=@octane-xplat/audio -- --foreground-budgets --target ios --device OWNED_UDID
98
- bun run turbo run test:macos-budgets --filter=@octane-xplat/audio
99
- ```
100
-
101
- | Measurement | Fixture ceiling |
102
- | --- | --- |
103
- | First observed progress after loading | 10 seconds |
104
- | Preserving queue update | 250 milliseconds |
105
- | Position lost during that update | 0.15 seconds |
106
- | Observable seek position | 1.5 seconds |
107
- | Estimated extra boundary delay | 1.5 seconds |
108
- | Appended waiting queue resumes | 2 seconds |
109
- | Sampled AppKit host peak RSS | 384 MiB |
110
-
111
- These are initial fixture ceilings, not application performance guarantees.
112
- Boundary timing comes from media positions and polling; it does not measure
113
- an audible gap. RSS samples cover the owned AppKit process, excluding GPU and
114
- other processes. Startup, leaks, durable progress, background execution and
115
- physical output need separate acceptance. The
116
- [packed listening/settings check](https://github.com/octane-xplat/octane-xplat/blob/main/tools/native-readiness/README.md)
117
- exercises these services together in an external mobile consumer.
5
+ [API reference](https://octane-xplat.goddardai.org/api-audio).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@octane-xplat/audio",
3
- "version": "0.19.0",
3
+ "version": "0.20.0",
4
4
  "description": "Long-form audio playback for Octane xplat",
5
5
  "keywords": [
6
6
  "android",
@@ -55,18 +55,19 @@
55
55
  "test:macos-budgets": "node tests/profile-macos.mjs"
56
56
  },
57
57
  "dependencies": {
58
- "@octane-xplat/platform": "0.19.0"
58
+ "@octane-xplat/platform": "0.20.0"
59
59
  },
60
60
  "devDependencies": {
61
61
  "@nativescript-community/octane": "0.2.5",
62
62
  "@nativescript/core": "9.1.3",
63
63
  "@octanejs/vite-plugin": "0.5.0",
64
64
  "octane": "0.16.3",
65
- "tsrx-typegen": "0.5.0",
66
- "typescript": "5.9.3",
67
- "vite": "8.3.0",
65
+ "tsrx-typegen": "0.6.1",
66
+ "typescript": "7.1.0-beta",
67
+ "vite": "8.3.6",
68
68
  "playwright": "1.63.0",
69
- "@tsrx/typescript-plugin": "0.4.11"
69
+ "@tsrx/content-mapper": "0.1.2",
70
+ "typescript-classic": "npm:typescript@5.9.3"
70
71
  },
71
72
  "peerDependencies": {
72
73
  "@nativescript-community/octane": ">=0.2.1 <1",
@@ -6,188 +6,198 @@ import { android } from '@octane-xplat/platform/android'
6
6
 
7
7
  // The plugin's Java bridge is exposed by NativeScript's runtime metadata.
8
8
  declare const com: {
9
- xplat: {
10
- audio: {
11
- AudioBridge: {
12
- ACTION_QUEUE: string
13
- ACTION_UPDATE: string
14
- ACTION_PLAY: string
15
- ACTION_PAUSE: string
16
- ACTION_SEEK: string
17
- ACTION_RELEASE: string
18
- ACTION_RATE: string
19
- ACTION_VOLUME: string
20
- ACTION_MUTED: string
21
- dispatch(
22
- context: android.content.Context,
23
- action: string,
24
- payload: string,
25
- index: number,
26
- seconds: number,
27
- ): void
28
- snapshot(): string
29
- }
30
- }
31
- }
9
+ xplat: {
10
+ audio: {
11
+ AudioBridge: {
12
+ ACTION_QUEUE: string
13
+ ACTION_UPDATE: string
14
+ ACTION_PLAY: string
15
+ ACTION_PAUSE: string
16
+ ACTION_SEEK: string
17
+ ACTION_RELEASE: string
18
+ ACTION_RATE: string
19
+ ACTION_VOLUME: string
20
+ ACTION_MUTED: string
21
+ dispatch(
22
+ context: android.content.Context,
23
+ action: string,
24
+ payload: string,
25
+ index: number,
26
+ seconds: number,
27
+ ): void
28
+ snapshot(): string
29
+ }
30
+ }
31
+ }
32
32
  }
33
33
 
34
34
  /** Create an independent player on web/iOS/macOS; Android owns one app playback service. */
35
35
  export const createAudioPlayer = (): AudioPlayer => {
36
- const actions = com.xplat.audio.AudioBridge
37
- // `Utils.android` is a {} stub in the pinned Windows core build.
38
- const context = (Utils.android as any).getApplicationContext()
39
- const listeners = new Set<(snapshot: AudioSnapshot) => void>()
40
- let queue: Track[] = []
41
- let index = -1
42
- let disposed = false
43
- let playbackRate = 1
44
- let volume = 1
45
- let muted = false
46
- let queueComplete = true
47
- let poll: ReturnType<typeof setInterval> | undefined
48
-
49
- const snapshot = (): AudioSnapshot => {
50
- const value = JSON.parse(actions.snapshot()) as {
51
- state: AudioSnapshot['state']
52
- index?: number
53
- id?: string
54
- currentTime?: number
55
- duration?: number
56
- error?: string
57
- }
58
-
59
- if (Number.isInteger(value.index) && value.index! >= 0) {
60
- index = value.index!
61
- }
62
-
63
- return {
64
- state: value.state,
65
- playbackRate,
66
- volume,
67
- muted,
68
- queueComplete,
69
- track: value.id ? queue.find((track) => track.id === value.id) : queue[index],
70
- currentTime: value.currentTime ?? 0,
71
- duration: value.duration ?? 0,
72
- error: value.error ? new Error(value.error) : undefined,
73
- }
74
- }
75
-
76
- const emit = () => {
77
- const value = snapshot()
78
- for (const listener of listeners) {
79
- listener(value)
80
- }
81
- }
82
-
83
- const dispatch = (action: string, payload = '', at = 0, seconds = 0) => {
84
- if (disposed) {
85
- throw new Error('AudioPlayer is disposed')
86
- }
87
-
88
- actions.dispatch(context, action, payload, at, seconds)
89
- }
90
-
91
- const ensurePolling = () => {
92
- if (listeners.size && !poll) {
93
- poll = setInterval(emit, 250)
94
- }
95
-
96
- if (!listeners.size && poll) {
97
- clearInterval(poll)
98
- poll = undefined
99
- }
100
- }
101
-
102
- return {
103
- capabilities: (): AudioCapabilities => ({
104
- supported: true,
105
- backgroundPlayback: true,
106
- systemControls: true,
107
- userGestureRequired: false,
108
- interruptions: true,
109
- }),
110
- snapshot,
111
- subscribe: (listener) => {
112
- listeners.add(listener)
113
- listener(snapshot())
114
- ensurePolling()
115
- return () => {
116
- listeners.delete(listener)
117
- ensurePolling()
118
- }
119
- },
120
- setQueue: async (tracks, startAt = 0, options = {}) => {
121
- if (disposed) {
122
- throw new Error('AudioPlayer is disposed')
123
- }
124
-
125
- queue = copyQueue(tracks)
126
- queueComplete = options.complete ?? true
127
- index = queue.length
128
- ? Math.max(
129
- 0,
130
- Math.min(Number.isFinite(startAt) ? Math.floor(startAt) : 0, queue.length - 1),
131
- )
132
- : -1
133
-
134
- dispatch(
135
- actions.ACTION_QUEUE,
136
- JSON.stringify({ tracks: queue, complete: queueComplete }),
137
- index,
138
- )
139
- emit()
140
- },
141
- play: async () => dispatch(actions.ACTION_PLAY),
142
- pause: async () => dispatch(actions.ACTION_PAUSE),
143
- seek: async (seconds) => {
144
- if (Number.isFinite(seconds)) {
145
- dispatch(actions.ACTION_SEEK, '', 0, Math.max(0, seconds))
146
- }
147
-
148
- emit()
149
- },
150
-
151
- updateQueue: async (tracks, options = {}) => {
152
- const next = copyQueue(tracks)
153
- const complete = options.complete ?? queueComplete
154
- dispatch(actions.ACTION_UPDATE, JSON.stringify({ tracks: next, complete }))
155
- queue = next
156
- queueComplete = complete
157
- emit()
158
- },
159
- setPlaybackRate: async (rate) => {
160
- const next = bounded(rate, 0.5, 3)
161
- dispatch(actions.ACTION_RATE, '', 0, next)
162
- playbackRate = next
163
- emit()
164
- },
165
- setVolume: async (gain) => {
166
- const next = bounded(gain, 0, 1)
167
- dispatch(actions.ACTION_VOLUME, '', 0, next)
168
- volume = next
169
- emit()
170
- },
171
- setMuted: async (value) => {
172
- dispatch(actions.ACTION_MUTED, '', 0, value ? 1 : 0)
173
- muted = value
174
- emit()
175
- },
176
- dispose: () => {
177
- if (disposed) {
178
- return
179
- }
180
-
181
- disposed = true
182
- actions.dispatch(context, actions.ACTION_RELEASE, '', 0, 0)
183
- if (poll) {
184
- clearInterval(poll)
185
- }
186
-
187
- poll = undefined
188
- listeners.clear()
189
- queue = []
190
- index = -1
191
- },
192
- }
36
+ const actions = com.xplat.audio.AudioBridge
37
+ // `Utils.android` is a {} stub in the pinned Windows core build.
38
+ const context = (Utils.android as any).getApplicationContext()
39
+ const listeners = new Set<(snapshot: AudioSnapshot) => void>()
40
+ let queue: Track[] = []
41
+ let index = -1
42
+ let disposed = false
43
+ let playbackRate = 1
44
+ let volume = 1
45
+ let muted = false
46
+ let queueComplete = true
47
+ let poll: ReturnType<typeof setInterval> | undefined
48
+
49
+ /** The Android bridge's snapshot payload. */
50
+ interface SnapshotReply {
51
+ state: AudioSnapshot['state']
52
+ index?: number
53
+ id?: string
54
+ currentTime?: number
55
+ duration?: number
56
+ error?: string
57
+ }
58
+
59
+ const snapshot = (): AudioSnapshot => {
60
+ const parsed: unknown = JSON.parse(actions.snapshot())
61
+ // Bridge-supplied envelope; validated fields below, bounded once here.
62
+ const value = (typeof parsed === 'object' && parsed !== null ? parsed : {}) as SnapshotReply
63
+
64
+ if (Number.isInteger(value.index) && value.index! >= 0) {
65
+ index = value.index!
66
+ }
67
+
68
+ return {
69
+ state: value.state,
70
+ playbackRate,
71
+ volume,
72
+ muted,
73
+ queueComplete,
74
+ track: value.id ? queue.find((track) => track.id === value.id) : queue[index],
75
+ currentTime: value.currentTime ?? 0,
76
+ duration: value.duration ?? 0,
77
+ error: value.error ? new Error(value.error) : undefined,
78
+ }
79
+ }
80
+
81
+ const emit = () => {
82
+ const value = snapshot()
83
+
84
+ for (const listener of listeners) {
85
+ listener(value)
86
+ }
87
+ }
88
+
89
+ const dispatch = (action: string, payload = '', at = 0, seconds = 0) => {
90
+ if (disposed) {
91
+ throw new Error('AudioPlayer is disposed')
92
+ }
93
+
94
+ actions.dispatch(context, action, payload, at, seconds)
95
+ }
96
+
97
+ const ensurePolling = () => {
98
+ if (listeners.size && !poll) {
99
+ poll = setInterval(emit, 250)
100
+ }
101
+
102
+ if (!listeners.size && poll) {
103
+ clearInterval(poll)
104
+ poll = undefined
105
+ }
106
+ }
107
+
108
+ return {
109
+ capabilities: (): AudioCapabilities => ({
110
+ supported: true,
111
+ backgroundPlayback: true,
112
+ systemControls: true,
113
+ userGestureRequired: false,
114
+ interruptions: true,
115
+ }),
116
+ snapshot,
117
+ subscribe: (listener) => {
118
+ listeners.add(listener)
119
+ listener(snapshot())
120
+ ensurePolling()
121
+ return () => {
122
+ listeners.delete(listener)
123
+ ensurePolling()
124
+ }
125
+ },
126
+ setQueue: async (tracks, startAt = 0, options = {}) => {
127
+ if (disposed) {
128
+ throw new Error('AudioPlayer is disposed')
129
+ }
130
+
131
+ queue = copyQueue(tracks)
132
+ queueComplete = options.complete ?? true
133
+ index = queue.length
134
+ ? Math.max(
135
+ 0,
136
+ Math.min(Number.isFinite(startAt) ? Math.floor(startAt) : 0, queue.length - 1),
137
+ )
138
+ : -1
139
+
140
+ dispatch(
141
+ actions.ACTION_QUEUE,
142
+ JSON.stringify({ tracks: queue, complete: queueComplete }),
143
+ index,
144
+ )
145
+
146
+ emit()
147
+ },
148
+ play: async () => dispatch(actions.ACTION_PLAY),
149
+ pause: async () => dispatch(actions.ACTION_PAUSE),
150
+ seek: async (seconds) => {
151
+ if (Number.isFinite(seconds)) {
152
+ dispatch(actions.ACTION_SEEK, '', 0, Math.max(0, seconds))
153
+ }
154
+
155
+ emit()
156
+ },
157
+
158
+ updateQueue: async (tracks, options = {}) => {
159
+ const next = copyQueue(tracks)
160
+ const complete = options.complete ?? queueComplete
161
+
162
+ dispatch(actions.ACTION_UPDATE, JSON.stringify({ tracks: next, complete }))
163
+ queue = next
164
+ queueComplete = complete
165
+ emit()
166
+ },
167
+ setPlaybackRate: async (rate) => {
168
+ const next = bounded(rate, 0.5, 3)
169
+
170
+ dispatch(actions.ACTION_RATE, '', 0, next)
171
+ playbackRate = next
172
+ emit()
173
+ },
174
+ setVolume: async (gain) => {
175
+ const next = bounded(gain, 0, 1)
176
+
177
+ dispatch(actions.ACTION_VOLUME, '', 0, next)
178
+ volume = next
179
+ emit()
180
+ },
181
+ setMuted: async (value) => {
182
+ dispatch(actions.ACTION_MUTED, '', 0, value ? 1 : 0)
183
+ muted = value
184
+ emit()
185
+ },
186
+ dispose: () => {
187
+ if (disposed) {
188
+ return
189
+ }
190
+
191
+ disposed = true
192
+ actions.dispatch(context, actions.ACTION_RELEASE, '', 0, 0)
193
+ if (poll) {
194
+ clearInterval(poll)
195
+ }
196
+
197
+ poll = undefined
198
+ listeners.clear()
199
+ queue = []
200
+ index = -1
201
+ },
202
+ }
193
203
  }