-
Notifications
You must be signed in to change notification settings - Fork 1.8k
new plugin: audio-stream #4216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
dnecra
wants to merge
43
commits into
pear-devs:master
Choose a base branch
from
dnecra:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
new plugin: audio-stream #4216
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
f8e6045
new plugin: audio-stream
dnecra f44a1f0
fix audio-stream
dnecra bb38318
Merge branch 'pear-devs:master' into master
dnecra 1e8b093
fix audio-stream
dnecra 70673db
fix audio-stream
dnecra f27372a
Update src/plugins/audio-stream/backend.ts
dnecra fab4b6b
Update src/plugins/audio-stream/backend.ts
dnecra 8b36e8b
Update src/plugins/audio-stream/backend.ts
dnecra d19c899
Update src/plugins/audio-stream/backend.ts
dnecra ec30930
Update src/plugins/audio-stream/backend.ts
dnecra e679311
Update src/plugins/audio-stream/backend.ts
dnecra 16da921
Update src/plugins/audio-stream/backend.ts
dnecra 8b4448a
Update src/plugins/audio-stream/backend.ts
dnecra 87146da
Update src/plugins/audio-stream/backend.ts
dnecra bc9d099
Update src/plugins/audio-stream/backend.ts
dnecra e4c748e
Update src/plugins/audio-stream/backend.ts
dnecra 792b440
Update src/plugins/audio-stream/backend.ts
dnecra d1d891b
Update src/plugins/audio-stream/backend.ts
dnecra 7a1fbea
Update src/plugins/audio-stream/backend.ts
dnecra 024508b
Update src/plugins/audio-stream/backend.ts
dnecra 0af3e8e
Update src/plugins/audio-stream/backend.ts
dnecra 6c112be
Update src/plugins/audio-stream/backend.ts
dnecra 5ca2a7f
Update src/plugins/audio-stream/backend.ts
dnecra 738872d
Update src/plugins/audio-stream/backend.ts
dnecra 13f818a
Update src/plugins/audio-stream/backend.ts
dnecra d995d9f
Update src/plugins/audio-stream/backend.ts
dnecra 8fa70f8
Update src/plugins/audio-stream/backend.ts
dnecra 59490ed
Update src/plugins/audio-stream/backend.ts
dnecra d87c843
Update src/plugins/audio-stream/backend.ts
dnecra 668ac39
Update src/plugins/audio-stream/backend.ts
dnecra 80e17d9
Update src/plugins/audio-stream/backend.ts
dnecra a2fea13
Merge branch 'pear-devs:master' into master
dnecra d443777
Migrate audio processing to AudioWorkletNode
dnecra 01842c8
Implement binary PCM data handling in audio stream
dnecra 02a74de
Update src/plugins/audio-stream/backend.ts
dnecra 5e85f8b
Update src/plugins/audio-stream/backend.ts
dnecra d6aa17d
Update src/plugins/audio-stream/backend.ts
dnecra ddefbb6
Update src/plugins/audio-stream/backend.ts
dnecra 278bcaf
Update src/plugins/audio-stream/backend.ts
dnecra b562ea1
Merge branch 'master' into master
ArjixWasTaken 9078890
init rewrite of plugin
ArjixWasTaken 39275d6
Merge branch 'master' into master
ArjixWasTaken 966142d
Merge branch 'master' into master
ArjixWasTaken File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| export class BroadcastStream { | ||
| private subscribers: Set<ReadableStreamDefaultController<Uint8Array>> = | ||
| new Set(); | ||
|
|
||
| // A way for readers to get a new stream | ||
| subscribe() { | ||
| let controller!: ReadableStreamDefaultController<Uint8Array>; | ||
| const stream = new ReadableStream<Uint8Array>({ | ||
| start(c) { | ||
| controller = c; | ||
| }, | ||
| cancel: () => { | ||
| this.subscribers.delete(controller); | ||
| }, | ||
| }); | ||
|
|
||
| this.subscribers.add(controller); | ||
| return stream; | ||
| } | ||
|
|
||
| // A way for you to write data to all readers | ||
| write(chunk: Uint8Array) { | ||
| for (const controller of this.subscribers) { | ||
| controller.enqueue(chunk); | ||
| } | ||
| } | ||
|
|
||
| close() { | ||
| for (const controller of this.subscribers) { | ||
| controller.close(); | ||
| } | ||
| this.subscribers.clear(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // audio-processor.js (loaded by audioContext.audioWorklet.addModule) | ||
| class RecorderProcessor extends AudioWorkletProcessor { | ||
| constructor(options) { | ||
| super(); | ||
| const bufferSize = options.bufferSize || 4096; | ||
| // Prepare an interleaved stereo buffer [L,R,L,R,...] | ||
| this.buffer = new Float32Array(bufferSize * 2); | ||
| this.bufferIndex = 0; | ||
| } | ||
|
|
||
| process(inputs, outputs) { | ||
| const input = inputs[0]; | ||
| if (input && input[0]) { | ||
| const left = input[0]; | ||
| const right = input[1] || left; // if mono input, duplicate for right | ||
| for (let i = 0; i < left.length; i++) { | ||
| this.buffer[this.bufferIndex++] = left[i]; | ||
| this.buffer[this.bufferIndex++] = right[i]; | ||
| if (this.bufferIndex >= this.buffer.length) { | ||
| // Buffer full: send a copy to the main thread | ||
| this.port.postMessage(new Float32Array(this.buffer)); | ||
| this.bufferIndex = 0; | ||
| } | ||
| } | ||
| } | ||
| // Optionally pass the audio through unchanged | ||
| if (outputs[0] && inputs[0]) { | ||
| outputs[0][0].set(inputs[0][0]); | ||
| if (inputs[0][1]) outputs[0][1].set(inputs[0][1]); | ||
| } | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| registerProcessor('recorder-processor', RecorderProcessor); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import { Hono } from 'hono'; | ||
| import { streamText } from 'hono/streaming'; | ||
| import { serve, type ServerType } from '@hono/node-server'; | ||
|
|
||
| import { createBackend } from '@/utils'; | ||
| import { type AudioStreamConfig } from './config'; | ||
| import { BroadcastStream } from './BroadcastStream'; | ||
|
|
||
| const META_INT = 16_000; | ||
|
|
||
| let config: AudioStreamConfig; | ||
| const broadcast = new BroadcastStream(); | ||
|
|
||
| export const backend = createBackend< | ||
| { | ||
| app: Hono; | ||
| server?: ServerType; | ||
| }, | ||
| AudioStreamConfig | ||
| >({ | ||
| app: new Hono().get('/stream', (ctx) => { | ||
| const icyMetadata = ctx.req.header('Icy-Metadata'); | ||
| if (icyMetadata === '1') { | ||
| ctx.header('icy-metaint', META_INT.toString(10)); | ||
| ctx.header('icy-name', 'Pear Desktop'); | ||
| ctx.header('icy-url', 'https://github.com/pear-devs/pear-desktop'); | ||
| ctx.header( | ||
| 'icy-audio-info', | ||
| `ice-channels=2;ice-samplerate=${config.sampleRate.toString( | ||
| 10, | ||
| )};ice-bitrate=128`, | ||
| ); | ||
| ctx.header('icy-pub', '1'); | ||
| ctx.header('icy-sr', config.sampleRate.toString(10)); | ||
| ctx.header('Content-Type', 'audio/L16'); | ||
| ctx.header('Server', 'Pear Desktop'); | ||
| } | ||
|
|
||
| return streamText(ctx, async (stream) => { | ||
| let readable = broadcast.subscribe(); | ||
| if (icyMetadata === '1') { | ||
| let bytesUntilMetadata = META_INT; | ||
|
|
||
| readable = readable.pipeThrough( | ||
| new TransformStream({ | ||
| transform( | ||
| chunk: Uint8Array, | ||
| controller: TransformStreamDefaultController<Uint8Array>, | ||
| ) { | ||
| console.log({ bytesUntilMetadata }); | ||
| let offset = 0; | ||
|
|
||
| while (offset < chunk.byteLength) { | ||
| if (bytesUntilMetadata === 0) { | ||
| const encoder = new TextEncoder(); | ||
|
|
||
| // TODO: add real metadata | ||
| const metaBuffer = encoder.encode( | ||
| ".StreamTitle='My Cool Stream Title';", | ||
| ); | ||
|
|
||
| const padding = (16 - (metaBuffer.byteLength % 16)) % 16; | ||
| const metaLength = metaBuffer.byteLength + padding; | ||
| const lengthByte = metaLength / 16; | ||
|
|
||
| controller.enqueue(Uint8Array.from([lengthByte])); | ||
|
|
||
| if (metaLength > 0) { | ||
| controller.enqueue(Uint8Array.from(metaBuffer)); | ||
| } | ||
|
|
||
| bytesUntilMetadata = META_INT; | ||
| } | ||
|
|
||
| const chunkRemaining = chunk.byteLength - offset; | ||
| const canSend = Math.min(chunkRemaining, bytesUntilMetadata); | ||
| controller.enqueue(chunk.subarray(offset, offset + canSend)); | ||
|
|
||
| bytesUntilMetadata -= canSend; | ||
| offset += canSend; | ||
| } | ||
| }, | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
| return await stream.pipe(readable); | ||
| }); | ||
| }), | ||
|
|
||
| async start({ getConfig, ipc }) { | ||
| config = await getConfig(); | ||
|
|
||
| this.server = serve( | ||
| { | ||
| fetch: this.app.fetch.bind(this.app), | ||
| hostname: config.hostname, | ||
| port: config.port, | ||
| }, | ||
| ({ address, port }) => console.log('Listening on', { address, port }), | ||
| ); | ||
|
|
||
| ipc.on('audio-stream:pcm-binary', (chunk: Uint8Array) => { | ||
| broadcast.write(chunk); | ||
| }); | ||
| }, | ||
| async stop() { | ||
| let resolve; | ||
|
|
||
| const promise = new Promise((r) => (resolve = r)); | ||
| this.server?.close(resolve); | ||
|
|
||
| await promise; | ||
| }, | ||
| onConfigChange(newConfig) { | ||
| config = newConfig; | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| export interface AudioStreamConfig { | ||
| enabled: boolean; | ||
| port: number; | ||
| hostname: string; | ||
| // Audio quality settings for PCM streaming | ||
| sampleRate: number; // Audio sample rate (e.g., 44100, 48000, 96000) | ||
| bitDepth: number; // Bit depth (16 or 32) | ||
| channels: number; // Number of channels (1 = mono, 2 = stereo) | ||
| bufferSize: number; // Audio buffer size (1024, 2048, 4096, 8192) - affects latency | ||
| } | ||
|
|
||
| export const defaultAudioStreamConfig: AudioStreamConfig = { | ||
| enabled: false, | ||
| port: 8765, | ||
| hostname: '0.0.0.0', | ||
| // High quality audio settings for local network | ||
| // Using 48kHz/16-bit for stability - can increase to 96kHz/32-bit once working | ||
| sampleRate: 48000, // 48kHz - high quality and widely supported | ||
| bitDepth: 16, // 16-bit - reliable and well-tested | ||
| channels: 2, // Stereo | ||
| bufferSize: 2048, // Low latency buffer size | ||
| }; |
ArjixWasTaken marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { t } from '@/i18n'; | ||
| import { createPlugin } from '@/utils'; | ||
|
|
||
| import { defaultAudioStreamConfig } from './config'; | ||
| import { backend } from './backend'; | ||
| import { onMenu } from './menu'; | ||
| import { renderer } from './renderer'; | ||
|
|
||
| export default createPlugin({ | ||
| name: () => t('plugins.audio-stream.name'), | ||
| description: () => t('plugins.audio-stream.description'), | ||
| restartNeeded: false, | ||
| config: defaultAudioStreamConfig, | ||
| backend, | ||
| renderer, | ||
| menu: onMenu, | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.