-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbackground.js
More file actions
137 lines (122 loc) · 3.46 KB
/
Copy pathbackground.js
File metadata and controls
137 lines (122 loc) · 3.46 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const OFFSCREEN_URL = 'offscreen.html';
let state = {
running: false,
connected: false,
manual: false,
tabTitle: '',
host: 'patternflow.local',
error: '',
levels: [],
outputs: [],
spectrum: []
};
async function ensureOffscreen() {
const offscreenUrl = chrome.runtime.getURL(OFFSCREEN_URL);
const contexts = await chrome.runtime.getContexts({
contextTypes: ['OFFSCREEN_DOCUMENT'],
documentUrls: [offscreenUrl]
});
if (contexts.length > 0) return;
await chrome.offscreen.createDocument({
url: OFFSCREEN_URL,
reasons: ['USER_MEDIA'],
justification: 'Analyze captured tab audio and send Patternflow knob values.'
});
}
function broadcast() {
chrome.runtime.sendMessage({ type: 'state', state }).catch(() => {});
}
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
(async () => {
if (message.type === 'start') {
await ensureOffscreen();
state = {
...state,
running: true,
connected: false,
manual: false,
tabTitle: message.tabTitle || '',
host: message.config.host,
error: ''
};
await chrome.runtime.sendMessage({
target: 'offscreen',
type: 'start',
streamId: message.streamId,
config: message.config
});
broadcast();
sendResponse({ ok: true, state });
return;
}
if (message.type === 'manual-connect') {
await ensureOffscreen();
state = {
...state,
running: false,
connected: false,
manual: true,
tabTitle: '',
host: message.config.host,
error: ''
};
await chrome.runtime.sendMessage({
target: 'offscreen',
type: 'manual-connect',
config: message.config
});
broadcast();
sendResponse({ ok: true, state });
return;
}
if (message.type === 'manual-value') {
await chrome.runtime.sendMessage({
target: 'offscreen',
type: 'manual-value',
knob: message.knob,
value: message.value
}).catch(() => {});
sendResponse({ ok: true, state });
return;
}
if (message.type === 'stop') {
await chrome.runtime.sendMessage({ target: 'offscreen', type: 'stop' }).catch(() => {});
state = { ...state, running: false, connected: false, manual: false, error: '' };
broadcast();
sendResponse({ ok: true, state });
return;
}
if (message.type === 'config') {
state = { ...state, host: message.config.host };
await chrome.runtime.sendMessage({
target: 'offscreen',
type: 'config',
config: message.config
}).catch(() => {});
broadcast();
sendResponse({ ok: true, state });
return;
}
if (message.type === 'release') {
await chrome.runtime.sendMessage({ target: 'offscreen', type: 'release' }).catch(() => {});
sendResponse({ ok: true, state });
return;
}
if (message.type === 'status') {
sendResponse({ ok: true, state });
return;
}
if (message.type === 'offscreen-state') {
state = { ...state, ...message.patch };
broadcast();
sendResponse({ ok: true });
return;
}
sendResponse({ ok: false, error: 'Unknown message' });
})().catch((error) => {
state = { ...state, running: false, connected: false, error: String(error.message || error) };
broadcast();
sendResponse({ ok: false, error: state.error, state });
});
return true;
});