plotwave 0.1.0__py3-none-any.whl
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.
- plotwave/__init__.py +25 -0
- plotwave/_core.py +881 -0
- plotwave/_render.py +464 -0
- plotwave/py.typed +1 -0
- plotwave-0.1.0.dist-info/METADATA +135 -0
- plotwave-0.1.0.dist-info/RECORD +8 -0
- plotwave-0.1.0.dist-info/WHEEL +4 -0
- plotwave-0.1.0.dist-info/licenses/LICENSE +21 -0
plotwave/_render.py
ADDED
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import html
|
|
4
|
+
import json
|
|
5
|
+
import textwrap
|
|
6
|
+
import uuid
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass(slots=True)
|
|
12
|
+
class PreparedTrace:
|
|
13
|
+
plotly_trace: dict[str, Any]
|
|
14
|
+
audio_info: dict[str, Any] | None = None
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def deep_merge(base: dict[str, Any], override: dict[str, Any] | None) -> dict[str, Any]:
|
|
18
|
+
merged = dict(base)
|
|
19
|
+
if not override:
|
|
20
|
+
return merged
|
|
21
|
+
for key, value in override.items():
|
|
22
|
+
current = merged.get(key)
|
|
23
|
+
if isinstance(current, dict) and isinstance(value, dict):
|
|
24
|
+
merged[key] = deep_merge(current, value)
|
|
25
|
+
else:
|
|
26
|
+
merged[key] = value
|
|
27
|
+
return merged
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def build_html(
|
|
31
|
+
traces: list[PreparedTrace],
|
|
32
|
+
layout: dict[str, Any],
|
|
33
|
+
config: dict[str, Any],
|
|
34
|
+
frame_height: int,
|
|
35
|
+
) -> str:
|
|
36
|
+
root_id = f"plotwave-{uuid.uuid4().hex}"
|
|
37
|
+
plot_id = f"{root_id}-plot"
|
|
38
|
+
controls_id = f"{root_id}-controls"
|
|
39
|
+
controls_group_id = f"{root_id}-controls-group"
|
|
40
|
+
timing_group_id = f"{root_id}-timing-group"
|
|
41
|
+
toggle_id = f"{root_id}-toggle"
|
|
42
|
+
toggle_icon_id = f"{root_id}-toggle-icon"
|
|
43
|
+
toggle_label_id = f"{root_id}-toggle-label"
|
|
44
|
+
channel_id = f"{root_id}-channel"
|
|
45
|
+
speed_id = f"{root_id}-speed"
|
|
46
|
+
current_time_id = f"{root_id}-current-time"
|
|
47
|
+
current_time_value_id = f"{root_id}-current-time-value"
|
|
48
|
+
total_time_id = f"{root_id}-total-time"
|
|
49
|
+
|
|
50
|
+
plotly_traces = [trace.plotly_trace for trace in traces]
|
|
51
|
+
audio_infos = [trace.audio_info for trace in traces if trace.audio_info is not None]
|
|
52
|
+
|
|
53
|
+
channel_options_html = "".join(
|
|
54
|
+
f"<option value='{index}'>{html.escape(info['name'])}</option>"
|
|
55
|
+
for index, info in enumerate(audio_infos)
|
|
56
|
+
)
|
|
57
|
+
audio_elements_html = "".join(
|
|
58
|
+
(
|
|
59
|
+
f"<audio id='{root_id}-audio-{index}' "
|
|
60
|
+
f"src='data:audio/wav;base64,{info['b64_data']}'></audio>"
|
|
61
|
+
)
|
|
62
|
+
for index, info in enumerate(audio_infos)
|
|
63
|
+
)
|
|
64
|
+
speed_options_html = "".join(
|
|
65
|
+
[
|
|
66
|
+
f"<option value='{speed}' {'selected' if speed == 1.0 else ''}>{speed}x</option>"
|
|
67
|
+
for speed in [0.5, 0.75, 1.0, 1.5, 2.0]
|
|
68
|
+
]
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
controls_disabled = "disabled" if not audio_infos else ""
|
|
72
|
+
controls_style = "" if audio_infos else "display: none;"
|
|
73
|
+
x_range = layout.get("xaxis", {}).get("range", [0.0, 1.0])
|
|
74
|
+
y_range = layout.get("yaxis", {}).get("range", [-1.0, 1.0])
|
|
75
|
+
audio_element_ids = [f"{root_id}-audio-{index}" for index in range(len(audio_infos))]
|
|
76
|
+
title = layout.get("title", "plotwave")
|
|
77
|
+
if isinstance(title, dict):
|
|
78
|
+
title_text = str(title.get("text", "plotwave"))
|
|
79
|
+
else:
|
|
80
|
+
title_text = str(title)
|
|
81
|
+
duration = float(x_range[1]) - float(x_range[0])
|
|
82
|
+
|
|
83
|
+
script = f"""
|
|
84
|
+
<!DOCTYPE html>
|
|
85
|
+
<html>
|
|
86
|
+
<head>
|
|
87
|
+
<meta charset="utf-8">
|
|
88
|
+
<title>{html.escape(title_text)}</title>
|
|
89
|
+
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
|
|
90
|
+
<style>
|
|
91
|
+
html, body {{
|
|
92
|
+
margin: 0;
|
|
93
|
+
padding: 0;
|
|
94
|
+
overflow: hidden;
|
|
95
|
+
}}
|
|
96
|
+
body {{
|
|
97
|
+
font-family: "SF Pro Text", "Segoe UI", "Helvetica Neue", Arial, sans-serif;
|
|
98
|
+
background: #fcfcfb;
|
|
99
|
+
}}
|
|
100
|
+
#{root_id} {{
|
|
101
|
+
width: 100%;
|
|
102
|
+
height: {frame_height}px;
|
|
103
|
+
display: flex;
|
|
104
|
+
flex-direction: column;
|
|
105
|
+
color: #161616;
|
|
106
|
+
}}
|
|
107
|
+
#{controls_id} {{
|
|
108
|
+
display: flex;
|
|
109
|
+
justify-content: space-between;
|
|
110
|
+
align-items: center;
|
|
111
|
+
flex: 0 0 auto;
|
|
112
|
+
flex-wrap: wrap;
|
|
113
|
+
gap: 12px;
|
|
114
|
+
padding: 10px 14px;
|
|
115
|
+
background: rgba(252, 252, 251, 0.92);
|
|
116
|
+
border-bottom: 1px solid #e4e1dc;
|
|
117
|
+
}}
|
|
118
|
+
#{controls_group_id},
|
|
119
|
+
#{timing_group_id} {{
|
|
120
|
+
display: flex;
|
|
121
|
+
align-items: center;
|
|
122
|
+
gap: 10px;
|
|
123
|
+
min-width: 0;
|
|
124
|
+
}}
|
|
125
|
+
#{timing_group_id} {{
|
|
126
|
+
justify-content: flex-end;
|
|
127
|
+
flex-wrap: wrap;
|
|
128
|
+
color: #5c5852;
|
|
129
|
+
font-size: 12px;
|
|
130
|
+
letter-spacing: 0.01em;
|
|
131
|
+
}}
|
|
132
|
+
#{plot_id} {{
|
|
133
|
+
width: 100%;
|
|
134
|
+
flex: 1 1 auto;
|
|
135
|
+
min-height: 240px;
|
|
136
|
+
}}
|
|
137
|
+
select, button {{
|
|
138
|
+
font: inherit;
|
|
139
|
+
font-size: 13px;
|
|
140
|
+
border-radius: 10px;
|
|
141
|
+
transition: border-color 120ms ease, background 120ms ease, box-shadow 120ms ease;
|
|
142
|
+
}}
|
|
143
|
+
#{toggle_id} {{
|
|
144
|
+
width: 94px;
|
|
145
|
+
height: 36px;
|
|
146
|
+
box-sizing: border-box;
|
|
147
|
+
display: inline-flex;
|
|
148
|
+
align-items: center;
|
|
149
|
+
justify-content: center;
|
|
150
|
+
gap: 7px;
|
|
151
|
+
padding: 0 14px;
|
|
152
|
+
border: 1px solid #d8d3cc;
|
|
153
|
+
background: #fff;
|
|
154
|
+
color: #151515;
|
|
155
|
+
text-align: center;
|
|
156
|
+
line-height: 1;
|
|
157
|
+
white-space: nowrap;
|
|
158
|
+
box-shadow: 0 1px 0 rgba(20, 20, 20, 0.03);
|
|
159
|
+
cursor: pointer;
|
|
160
|
+
}}
|
|
161
|
+
#{toggle_id}:hover {{
|
|
162
|
+
background: #f7f4ef;
|
|
163
|
+
border-color: #cfc8bf;
|
|
164
|
+
}}
|
|
165
|
+
#{toggle_icon_id} {{
|
|
166
|
+
display: inline-flex;
|
|
167
|
+
width: 12px;
|
|
168
|
+
flex: 0 0 12px;
|
|
169
|
+
justify-content: center;
|
|
170
|
+
font-size: 11px;
|
|
171
|
+
}}
|
|
172
|
+
#{toggle_label_id} {{
|
|
173
|
+
min-width: 34px;
|
|
174
|
+
}}
|
|
175
|
+
#{toggle_id}:focus-visible,
|
|
176
|
+
select:focus-visible {{
|
|
177
|
+
outline: none;
|
|
178
|
+
box-shadow: 0 0 0 3px rgba(34, 34, 34, 0.08);
|
|
179
|
+
border-color: #bfb7ad;
|
|
180
|
+
}}
|
|
181
|
+
select {{
|
|
182
|
+
height: 36px;
|
|
183
|
+
box-sizing: border-box;
|
|
184
|
+
padding: 0 32px 0 12px;
|
|
185
|
+
border: 1px solid #d8d3cc;
|
|
186
|
+
background: #fff;
|
|
187
|
+
color: #1f1d1a;
|
|
188
|
+
appearance: none;
|
|
189
|
+
line-height: 1.2;
|
|
190
|
+
background-image:
|
|
191
|
+
linear-gradient(45deg, transparent 50%, #6a645c 50%),
|
|
192
|
+
linear-gradient(135deg, #6a645c 50%, transparent 50%);
|
|
193
|
+
background-position:
|
|
194
|
+
calc(100% - 18px) 15px,
|
|
195
|
+
calc(100% - 12px) 15px;
|
|
196
|
+
background-size: 6px 6px, 6px 6px;
|
|
197
|
+
background-repeat: no-repeat;
|
|
198
|
+
}}
|
|
199
|
+
select:hover {{
|
|
200
|
+
background-color: #f7f4ef;
|
|
201
|
+
border-color: #cfc8bf;
|
|
202
|
+
}}
|
|
203
|
+
select:disabled,
|
|
204
|
+
#{toggle_id}:disabled {{
|
|
205
|
+
opacity: 0.55;
|
|
206
|
+
cursor: default;
|
|
207
|
+
}}
|
|
208
|
+
.plotwave-time-label {{
|
|
209
|
+
display: inline-flex;
|
|
210
|
+
align-items: center;
|
|
211
|
+
height: 36px;
|
|
212
|
+
padding: 0 12px;
|
|
213
|
+
border-radius: 10px;
|
|
214
|
+
background: #f5f2ec;
|
|
215
|
+
color: #514b44;
|
|
216
|
+
white-space: nowrap;
|
|
217
|
+
}}
|
|
218
|
+
.plotwave-time-label strong {{
|
|
219
|
+
font-weight: 600;
|
|
220
|
+
color: #191714;
|
|
221
|
+
}}
|
|
222
|
+
@media (max-width: 720px) {{
|
|
223
|
+
#{controls_id} {{
|
|
224
|
+
flex-wrap: wrap;
|
|
225
|
+
justify-content: flex-start;
|
|
226
|
+
}}
|
|
227
|
+
#{timing_group_id} {{
|
|
228
|
+
width: 100%;
|
|
229
|
+
justify-content: flex-start;
|
|
230
|
+
}}
|
|
231
|
+
}}
|
|
232
|
+
</style>
|
|
233
|
+
</head>
|
|
234
|
+
<body>
|
|
235
|
+
<div id="{root_id}" tabindex="0">
|
|
236
|
+
<div id="{controls_id}">
|
|
237
|
+
<div id="{controls_group_id}">
|
|
238
|
+
<button id="{toggle_id}" {controls_disabled}>
|
|
239
|
+
<span id="{toggle_icon_id}">▶</span><span id="{toggle_label_id}">Play</span>
|
|
240
|
+
</button>
|
|
241
|
+
<select id="{channel_id}" {controls_disabled} style="{controls_style}">
|
|
242
|
+
{channel_options_html}
|
|
243
|
+
</select>
|
|
244
|
+
<select id="{speed_id}" {controls_disabled} style="{controls_style}">
|
|
245
|
+
{speed_options_html}
|
|
246
|
+
</select>
|
|
247
|
+
</div>
|
|
248
|
+
<div id="{timing_group_id}">
|
|
249
|
+
<span id="{current_time_id}" class="plotwave-time-label">
|
|
250
|
+
Current Time: <strong id="{current_time_value_id}">{float(x_range[0]):.2f}s</strong>
|
|
251
|
+
</span>
|
|
252
|
+
<span id="{total_time_id}" class="plotwave-time-label">
|
|
253
|
+
Total Timespan: <strong>{duration:.2f}s</strong>
|
|
254
|
+
</span>
|
|
255
|
+
</div>
|
|
256
|
+
</div>
|
|
257
|
+
<div id="{plot_id}"></div>
|
|
258
|
+
{audio_elements_html}
|
|
259
|
+
</div>
|
|
260
|
+
<script>
|
|
261
|
+
const rootElement = document.getElementById({json.dumps(root_id)});
|
|
262
|
+
const controlsDiv = document.getElementById({json.dumps(controls_id)});
|
|
263
|
+
const plotDiv = document.getElementById({json.dumps(plot_id)});
|
|
264
|
+
const plotData = {json.dumps(plotly_traces)};
|
|
265
|
+
const layout = {json.dumps(layout)};
|
|
266
|
+
const config = {json.dumps(config)};
|
|
267
|
+
const audioInfos = {json.dumps(audio_infos)};
|
|
268
|
+
const audioIds = {json.dumps(audio_element_ids)};
|
|
269
|
+
const resizePlot = () => Plotly.Plots.resize(plotDiv);
|
|
270
|
+
|
|
271
|
+
if (audioInfos.length > 0) {{
|
|
272
|
+
const initialTime = audioInfos[0].start_time;
|
|
273
|
+
plotData.push({{
|
|
274
|
+
x: [initialTime, initialTime],
|
|
275
|
+
y: [{json.dumps(y_range[0])}, {json.dumps(y_range[1])}],
|
|
276
|
+
mode: "lines",
|
|
277
|
+
type: "scatter",
|
|
278
|
+
name: "cursor",
|
|
279
|
+
showlegend: false,
|
|
280
|
+
line: {{ color: "red", width: 2.5 }}
|
|
281
|
+
}});
|
|
282
|
+
}}
|
|
283
|
+
|
|
284
|
+
Plotly.newPlot(plotDiv, plotData, layout, config).then(() => {{
|
|
285
|
+
requestAnimationFrame(() => requestAnimationFrame(resizePlot));
|
|
286
|
+
}});
|
|
287
|
+
|
|
288
|
+
if (typeof ResizeObserver !== "undefined") {{
|
|
289
|
+
const resizeObserver = new ResizeObserver(() => resizePlot());
|
|
290
|
+
resizeObserver.observe(rootElement);
|
|
291
|
+
resizeObserver.observe(controlsDiv);
|
|
292
|
+
}}
|
|
293
|
+
|
|
294
|
+
if (audioInfos.length > 0) {{
|
|
295
|
+
const toggleBtn = document.getElementById({json.dumps(toggle_id)});
|
|
296
|
+
const toggleIcon = document.getElementById({json.dumps(toggle_icon_id)});
|
|
297
|
+
const toggleLabel = document.getElementById({json.dumps(toggle_label_id)});
|
|
298
|
+
const channelSelect = document.getElementById({json.dumps(channel_id)});
|
|
299
|
+
const speedSelect = document.getElementById({json.dumps(speed_id)});
|
|
300
|
+
const currentTimeValue = document.getElementById({json.dumps(current_time_value_id)});
|
|
301
|
+
const audioElements = audioIds.map((audioId) => document.getElementById(audioId));
|
|
302
|
+
let currentAudioIndex = 0;
|
|
303
|
+
let currentAudio = audioElements[currentAudioIndex];
|
|
304
|
+
let animationFrameId = null;
|
|
305
|
+
|
|
306
|
+
const cursorTraceIndex = () => plotData.length - 1;
|
|
307
|
+
const currentGlobalTime = () => {{
|
|
308
|
+
const info = audioInfos[currentAudioIndex];
|
|
309
|
+
return info.start_time + currentAudio.currentTime;
|
|
310
|
+
}};
|
|
311
|
+
const setToggleState = (isPlaying) => {{
|
|
312
|
+
toggleIcon.textContent = isPlaying ? "⏸" : "▶";
|
|
313
|
+
toggleLabel.textContent = isPlaying ? "Pause" : "Play";
|
|
314
|
+
}};
|
|
315
|
+
const updateCurrentTimeLabel = (time) => {{
|
|
316
|
+
currentTimeValue.textContent = `${{time.toFixed(2)}}s`;
|
|
317
|
+
}};
|
|
318
|
+
const drawCursor = (time) => (
|
|
319
|
+
Plotly.restyle(plotDiv, {{ x: [[time, time]] }}, [cursorTraceIndex()])
|
|
320
|
+
);
|
|
321
|
+
const syncTimeline = (time) => {{
|
|
322
|
+
updateCurrentTimeLabel(time);
|
|
323
|
+
drawCursor(time);
|
|
324
|
+
}};
|
|
325
|
+
const seekBy = (deltaSeconds) => {{
|
|
326
|
+
const info = audioInfos[currentAudioIndex];
|
|
327
|
+
const nextTime = Math.max(
|
|
328
|
+
0,
|
|
329
|
+
Math.min(info.duration, currentAudio.currentTime + deltaSeconds)
|
|
330
|
+
);
|
|
331
|
+
currentAudio.currentTime = nextTime;
|
|
332
|
+
syncTimeline(info.start_time + nextTime);
|
|
333
|
+
if (!currentAudio.paused) {{
|
|
334
|
+
if (animationFrameId !== null) cancelAnimationFrame(animationFrameId);
|
|
335
|
+
animationFrameId = requestAnimationFrame(updatePlayback);
|
|
336
|
+
}}
|
|
337
|
+
}};
|
|
338
|
+
const togglePlayback = async () => {{
|
|
339
|
+
if (currentAudio.paused) {{
|
|
340
|
+
await currentAudio.play();
|
|
341
|
+
setToggleState(true);
|
|
342
|
+
animationFrameId = requestAnimationFrame(updatePlayback);
|
|
343
|
+
}} else {{
|
|
344
|
+
currentAudio.pause();
|
|
345
|
+
setToggleState(false);
|
|
346
|
+
if (animationFrameId !== null) cancelAnimationFrame(animationFrameId);
|
|
347
|
+
}}
|
|
348
|
+
}};
|
|
349
|
+
|
|
350
|
+
const updatePlayback = () => {{
|
|
351
|
+
if (!currentAudio || currentAudio.paused) return;
|
|
352
|
+
syncTimeline(currentGlobalTime());
|
|
353
|
+
animationFrameId = requestAnimationFrame(updatePlayback);
|
|
354
|
+
}};
|
|
355
|
+
|
|
356
|
+
toggleBtn.onclick = togglePlayback;
|
|
357
|
+
|
|
358
|
+
channelSelect.onchange = async () => {{
|
|
359
|
+
const previousInfo = audioInfos[currentAudioIndex];
|
|
360
|
+
const globalTime = previousInfo.start_time + currentAudio.currentTime;
|
|
361
|
+
const wasPlaying = !currentAudio.paused;
|
|
362
|
+
currentAudio.pause();
|
|
363
|
+
if (animationFrameId !== null) cancelAnimationFrame(animationFrameId);
|
|
364
|
+
currentAudioIndex = Number(channelSelect.value);
|
|
365
|
+
currentAudio = audioElements[currentAudioIndex];
|
|
366
|
+
currentAudio.playbackRate = Number(speedSelect.value);
|
|
367
|
+
const nextInfo = audioInfos[currentAudioIndex];
|
|
368
|
+
const nextTime = globalTime - nextInfo.start_time;
|
|
369
|
+
const isInsideNextAudio = nextTime >= 0 && nextTime <= nextInfo.duration;
|
|
370
|
+
|
|
371
|
+
if (isInsideNextAudio) {{
|
|
372
|
+
currentAudio.currentTime = nextTime;
|
|
373
|
+
}} else {{
|
|
374
|
+
currentAudio.currentTime = 0;
|
|
375
|
+
}}
|
|
376
|
+
|
|
377
|
+
syncTimeline(isInsideNextAudio ? globalTime : nextInfo.start_time);
|
|
378
|
+
|
|
379
|
+
if (wasPlaying && isInsideNextAudio) {{
|
|
380
|
+
await currentAudio.play();
|
|
381
|
+
animationFrameId = requestAnimationFrame(updatePlayback);
|
|
382
|
+
}}
|
|
383
|
+
if (!isInsideNextAudio) {{
|
|
384
|
+
setToggleState(false);
|
|
385
|
+
return;
|
|
386
|
+
}}
|
|
387
|
+
setToggleState(!currentAudio.paused);
|
|
388
|
+
}};
|
|
389
|
+
|
|
390
|
+
speedSelect.onchange = () => {{
|
|
391
|
+
const rate = Number(speedSelect.value);
|
|
392
|
+
audioElements.forEach((audioElement) => {{
|
|
393
|
+
audioElement.playbackRate = rate;
|
|
394
|
+
}});
|
|
395
|
+
}};
|
|
396
|
+
|
|
397
|
+
audioElements.forEach((audioElement, index) => {{
|
|
398
|
+
audioElement.onended = () => {{
|
|
399
|
+
if (index !== currentAudioIndex) return;
|
|
400
|
+
setToggleState(false);
|
|
401
|
+
if (animationFrameId !== null) cancelAnimationFrame(animationFrameId);
|
|
402
|
+
const finalTime = audioInfos[index].start_time + audioInfos[index].duration;
|
|
403
|
+
syncTimeline(finalTime);
|
|
404
|
+
}};
|
|
405
|
+
}});
|
|
406
|
+
|
|
407
|
+
plotDiv.addEventListener("click", (event) => {{
|
|
408
|
+
rootElement.focus();
|
|
409
|
+
const fullLayout = plotDiv._fullLayout;
|
|
410
|
+
if (!fullLayout || !fullLayout.xaxis) return;
|
|
411
|
+
const plotRect = plotDiv.getBoundingClientRect();
|
|
412
|
+
const clickXPixel = event.clientX - plotRect.left - fullLayout.margin.l;
|
|
413
|
+
const globalClickTime = fullLayout.xaxis.p2c(clickXPixel);
|
|
414
|
+
const info = audioInfos[currentAudioIndex];
|
|
415
|
+
const targetTime = globalClickTime - info.start_time;
|
|
416
|
+
if (targetTime >= 0 && targetTime <= info.duration) {{
|
|
417
|
+
currentAudio.currentTime = targetTime;
|
|
418
|
+
syncTimeline(globalClickTime);
|
|
419
|
+
if (!currentAudio.paused) {{
|
|
420
|
+
if (animationFrameId !== null) cancelAnimationFrame(animationFrameId);
|
|
421
|
+
animationFrameId = requestAnimationFrame(updatePlayback);
|
|
422
|
+
}}
|
|
423
|
+
}}
|
|
424
|
+
}});
|
|
425
|
+
|
|
426
|
+
rootElement.addEventListener("keydown", async (event) => {{
|
|
427
|
+
if (event.defaultPrevented || event.altKey || event.ctrlKey || event.metaKey) return;
|
|
428
|
+
const activeElement = document.activeElement;
|
|
429
|
+
const tagName = activeElement ? activeElement.tagName : "";
|
|
430
|
+
if (
|
|
431
|
+
tagName === "BUTTON" ||
|
|
432
|
+
tagName === "SELECT" ||
|
|
433
|
+
tagName === "INPUT" ||
|
|
434
|
+
tagName === "TEXTAREA" ||
|
|
435
|
+
(activeElement && activeElement.isContentEditable)
|
|
436
|
+
) {{
|
|
437
|
+
return;
|
|
438
|
+
}}
|
|
439
|
+
if (event.code === "Space") {{
|
|
440
|
+
if (event.repeat) return;
|
|
441
|
+
event.preventDefault();
|
|
442
|
+
await togglePlayback();
|
|
443
|
+
return;
|
|
444
|
+
}}
|
|
445
|
+
if (event.key === "j" || event.key === "J") {{
|
|
446
|
+
event.preventDefault();
|
|
447
|
+
seekBy(-5);
|
|
448
|
+
return;
|
|
449
|
+
}}
|
|
450
|
+
if (event.key === "l" || event.key === "L") {{
|
|
451
|
+
event.preventDefault();
|
|
452
|
+
seekBy(5);
|
|
453
|
+
}}
|
|
454
|
+
}});
|
|
455
|
+
setToggleState(false);
|
|
456
|
+
syncTimeline(audioInfos[currentAudioIndex].start_time);
|
|
457
|
+
}}
|
|
458
|
+
|
|
459
|
+
window.addEventListener("resize", () => Plotly.Plots.resize(plotDiv));
|
|
460
|
+
</script>
|
|
461
|
+
</body>
|
|
462
|
+
</html>
|
|
463
|
+
"""
|
|
464
|
+
return textwrap.dedent(script).strip()
|
plotwave/py.typed
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: plotwave
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Interactive audio and waveform plotting for notebooks and browsers.
|
|
5
|
+
Project-URL: Homepage, https://github.com/camilziane/plotwave
|
|
6
|
+
Project-URL: Repository, https://github.com/camilziane/plotwave
|
|
7
|
+
Author: Camil Ziane
|
|
8
|
+
License: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: audio,jupyter,plotly,visualization,waveform
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Intended Audience :: Science/Research
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Topic :: Multimedia :: Sound/Audio :: Analysis
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Visualization
|
|
20
|
+
Requires-Python: >=3.11
|
|
21
|
+
Requires-Dist: numpy>=1.26
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# plotwave
|
|
25
|
+
<p align="left">
|
|
26
|
+
<img src="https://raw.githubusercontent.com/camilziane/plotwave/main/assets/logo_name.png" alt="plotwave logo" width="200">
|
|
27
|
+
</p>
|
|
28
|
+
|
|
29
|
+
<p align="left">
|
|
30
|
+
Interactive Plotly waveforms with synchronized audio playback
|
|
31
|
+
</p>
|
|
32
|
+
|
|
33
|
+
<p align="left">
|
|
34
|
+
<a href="https://camilziane.github.io/plotwave/">Live interactive demo</a>
|
|
35
|
+
</p>
|
|
36
|
+
|
|
37
|
+
**Click anywhere in the waveform to hear the audio while inspecting it visually.
|
|
38
|
+
Overlay multiple audio tracks or add additional signals (labels, predictions, segmentation, scores) on top of the waveform.**
|
|
39
|
+
|
|
40
|
+
Designed for **Jupyter notebooks**, `plotwave` can also be exported to **HTML**, making it easy to share interactive audio visualizations or log them in tools like **MLflow** for experiment analysis.
|
|
41
|
+
|
|
42
|
+
## Install
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
uv add plotwave
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
or
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
pip install plotwave
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Smallest useful example
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
import soundfile as sf
|
|
58
|
+
import plotwave
|
|
59
|
+
|
|
60
|
+
wav, sr = sf.read("wave.wav", always_2d=False)
|
|
61
|
+
|
|
62
|
+
plotwave.plot(wav, sr=sr, name="voice")
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
`soundfile` is only used here as a convenient audio loader. `plotwave` itself only depends on `numpy`.
|
|
66
|
+
|
|
67
|
+
## Audio + curve
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
env = np.abs(wav)
|
|
71
|
+
|
|
72
|
+
plotwave.plot(
|
|
73
|
+
[
|
|
74
|
+
plotwave.audio(wav, sr, name="audio", color="#2563eb"),
|
|
75
|
+
plotwave.series(env, name="envelope", color="#f97316", fill="tozeroy"),
|
|
76
|
+
],
|
|
77
|
+
layout={"title": {"text": "Audio + envelope"}, "height": 520},
|
|
78
|
+
)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
If `series(..., time=None)` is plotted next to audio, `plotwave` infers its time axis automatically when the audio timing is unambiguous.
|
|
82
|
+
|
|
83
|
+
## Segments
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
plotwave.plot(
|
|
87
|
+
[
|
|
88
|
+
plotwave.audio(wav, sr, name="audio"),
|
|
89
|
+
plotwave.segments(
|
|
90
|
+
[
|
|
91
|
+
{"start": 0.0, "end": 0.7, "label": "Bm"},
|
|
92
|
+
{"start": 1.0, "end": 1.6, "label": "G"},
|
|
93
|
+
],
|
|
94
|
+
name="Pred",
|
|
95
|
+
lane="top",
|
|
96
|
+
color_map={"Bm": "#2563eb", "G": "#16a34a"},
|
|
97
|
+
),
|
|
98
|
+
]
|
|
99
|
+
)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
`segments(...)` adds:
|
|
103
|
+
|
|
104
|
+
- colored background blocks
|
|
105
|
+
- label boxes
|
|
106
|
+
- hoverable segment names
|
|
107
|
+
- top/bottom lanes for comparisons like prediction vs ground truth
|
|
108
|
+
|
|
109
|
+
## Export
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
plot = plotwave.plot(wav, sr=sr)
|
|
113
|
+
plot.save("wave.html")
|
|
114
|
+
html = plot.html()
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## API
|
|
118
|
+
|
|
119
|
+
Public API:
|
|
120
|
+
|
|
121
|
+
- `plotwave.plot(...)`
|
|
122
|
+
- `plotwave.audio(...)`
|
|
123
|
+
- `plotwave.series(...)`
|
|
124
|
+
- `plotwave.segments(...)`
|
|
125
|
+
- `plotwave.Plot`
|
|
126
|
+
|
|
127
|
+
See [examples/getting_started.ipynb](https://github.com/camilziane/plotwave/blob/main/examples/getting_started.ipynb) for a full walkthrough.
|
|
128
|
+
|
|
129
|
+
Developer workflow: [DEVELOPERS.md](https://github.com/camilziane/plotwave/blob/main/DEVELOPERS.md)
|
|
130
|
+
|
|
131
|
+
To refresh the GitHub Pages demo locally:
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
uv run python scripts/build_pages_demo.py
|
|
135
|
+
```
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
plotwave/__init__.py,sha256=QFvvRLN-ywNp-dc1qb3fNKbMYSo-NoWuinCOgziwrpM,349
|
|
2
|
+
plotwave/_core.py,sha256=t6wy1h2Q-jtbAfHWyTDTA35uKLatUMfhS_X2HK915Bs,30128
|
|
3
|
+
plotwave/_render.py,sha256=p5oJM1nrO6Jn9qvTcuR_d8OtDyObyzvjK-qwqo6Y1r4,15300
|
|
4
|
+
plotwave/py.typed,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
|
|
5
|
+
plotwave-0.1.0.dist-info/METADATA,sha256=OPKjx0DdNKevefW7gFzsUHFTxg8NJgjXogOcvtEh68k,3592
|
|
6
|
+
plotwave-0.1.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
|
|
7
|
+
plotwave-0.1.0.dist-info/licenses/LICENSE,sha256=LCXQ_Y_0fsfx0tsbD7L9uq35ORDLxKHwKzZvfi8zT2k,1068
|
|
8
|
+
plotwave-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Camil Ziane
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|