-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path06-stream-basics.ts
More file actions
80 lines (65 loc) · 2.32 KB
/
06-stream-basics.ts
File metadata and controls
80 lines (65 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { AudioCapture, AudioStream, type AudioSample } from '../../src/index';
import { pipeline } from 'stream';
import fs from 'fs';
import path from 'path';
// Global error handlers for test suite
process.on('uncaughtException', (err) => {
console.error('❌ Uncaught Exception:', err.message);
process.exit(1);
});
process.on('unhandledRejection', (reason) => {
console.error('❌ Unhandled Rejection:', reason);
process.exit(1);
});
const capture = new AudioCapture();
// Error handler
capture.on('error', (err) => {
console.error('❌ Capture Error:', err.message);
});
// Find an app - use TARGET_APP env var if set
const appList = process.env.TARGET_APP ? [process.env.TARGET_APP] : ['Spotify', 'Music', 'Chrome'];
const app = capture.selectApp(appList, { fallbackToFirst: true });
if (!app) {
console.log('No app found for stream basics.');
process.exit(0);
}
console.log(`Streaming from: ${app.applicationName}`);
// Create a readable stream
const audioStream: AudioStream = capture.createAudioStream(app.processId, {
minVolume: 0.01,
format: 'float32'
});
// Pipe to a file (just for demonstration)
const outputPath = path.join(__dirname, 'stream_output.raw');
const writeStream = fs.createWriteStream(outputPath);
// Pipe to any writable stream
audioStream.pipe(writeStream);
// Stop the stream
setTimeout(() => {
console.log('Stopping stream...');
audioStream.stop();
// Run Object Mode example
runObjectModeExample();
}, 3000);
function runObjectModeExample() {
console.log('\n--- Object Mode Example ---');
const capture2 = new AudioCapture();
const appList2 = process.env.TARGET_APP ? [process.env.TARGET_APP] : undefined;
const app2 = capture2.selectApp(appList2, { fallbackToFirst: true });
if (!app2) return;
// Returns full sample objects with metadata
const audioStream2: AudioStream = capture2.createAudioStream(app2.processId, {
objectMode: true
});
audioStream2.on('data', (sample: AudioSample) => {
// sample contains both data and metadata
if (sample.rms > 0.01) {
console.log(`Loud audio detected: ${sample.rms.toFixed(4)}`);
// processAudio(sample.data);
}
});
setTimeout(() => {
console.log('Stopping object mode stream...');
audioStream2.stop();
}, 3000);
}