Commit
·
a9b52e3
1
Parent(s):
41c1020
init gradio custom component
Browse files- app.py +12 -0
- sourceviewer/.gitignore +12 -0
- sourceviewer/README.md +15 -0
- sourceviewer/backend/gradio_sourceviewer/__init__.py +4 -0
- sourceviewer/backend/gradio_sourceviewer/sourceviewer.py +400 -0
- sourceviewer/demo/__init__.py +0 -0
- sourceviewer/demo/app.py +17 -0
- sourceviewer/demo/requirements.txt +1 -0
- sourceviewer/frontend/Example.svelte +19 -0
- sourceviewer/frontend/Index.svelte +275 -0
- sourceviewer/frontend/gradio.config.js +9 -0
- sourceviewer/frontend/index.ts +7 -0
- sourceviewer/frontend/interactive/InteractiveAudio.svelte +332 -0
- sourceviewer/frontend/package-lock.json +0 -0
- sourceviewer/frontend/package.json +62 -0
- sourceviewer/frontend/player/AudioPlayer.svelte +314 -0
- sourceviewer/frontend/recorder/AudioRecorder.svelte +287 -0
- sourceviewer/frontend/shared/Audio.svelte +45 -0
- sourceviewer/frontend/shared/DeviceSelect.svelte +71 -0
- sourceviewer/frontend/shared/VolumeControl.svelte +77 -0
- sourceviewer/frontend/shared/VolumeLevels.svelte +12 -0
- sourceviewer/frontend/shared/WaveformControls.svelte +406 -0
- sourceviewer/frontend/shared/WaveformRecordControls.svelte +279 -0
- sourceviewer/frontend/shared/audioBufferToWav.ts +59 -0
- sourceviewer/frontend/shared/index.ts +1 -0
- sourceviewer/frontend/shared/types.ts +9 -0
- sourceviewer/frontend/shared/utils.ts +79 -0
- sourceviewer/frontend/static/StaticAudio.svelte +91 -0
- sourceviewer/frontend/streaming/StreamAudio.svelte +205 -0
- sourceviewer/pyproject.toml +54 -0
app.py
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from gradio_sourceviewer import SourceViewer
|
3 |
+
|
4 |
+
|
5 |
+
example = SourceViewer().example_value()
|
6 |
+
|
7 |
+
with gr.Blocks() as demo:
|
8 |
+
gr.Markdown("WORK IN PROGRESS")
|
9 |
+
source_viewer = SourceViewer()
|
10 |
+
|
11 |
+
if __name__ == "__main__":
|
12 |
+
demo.launch()
|
sourceviewer/.gitignore
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.eggs/
|
2 |
+
dist/
|
3 |
+
*.pyc
|
4 |
+
__pycache__/
|
5 |
+
*.py[cod]
|
6 |
+
*$py.class
|
7 |
+
__tmp/*
|
8 |
+
*.pyi
|
9 |
+
.mypycache
|
10 |
+
.ruff_cache
|
11 |
+
node_modules
|
12 |
+
backend/**/templates/
|
sourceviewer/README.md
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
tags: [gradio-custom-component, Audio]
|
3 |
+
title: gradio_sourceviewer
|
4 |
+
short_description: A gradio custom component
|
5 |
+
colorFrom: blue
|
6 |
+
colorTo: yellow
|
7 |
+
sdk: gradio
|
8 |
+
pinned: false
|
9 |
+
app_file: space.py
|
10 |
+
---
|
11 |
+
|
12 |
+
# gradio_sourceviewer
|
13 |
+
|
14 |
+
You can auto-generate documentation for your custom component with the `gradio cc docs` command.
|
15 |
+
You can also edit this file however you like.
|
sourceviewer/backend/gradio_sourceviewer/__init__.py
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from .sourceviewer import SourceViewer
|
3 |
+
|
4 |
+
__all__ = ['SourceViewer']
|
sourceviewer/backend/gradio_sourceviewer/sourceviewer.py
ADDED
@@ -0,0 +1,400 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""gr.Audio() component."""
|
2 |
+
|
3 |
+
from __future__ import annotations
|
4 |
+
|
5 |
+
import dataclasses
|
6 |
+
import io
|
7 |
+
from collections.abc import Callable, Sequence
|
8 |
+
from pathlib import Path
|
9 |
+
from typing import TYPE_CHECKING, Any, Literal
|
10 |
+
|
11 |
+
import anyio
|
12 |
+
import httpx
|
13 |
+
import numpy as np
|
14 |
+
from gradio_client import handle_file
|
15 |
+
from gradio_client import utils as client_utils
|
16 |
+
from gradio_client.documentation import document
|
17 |
+
from pydub import AudioSegment
|
18 |
+
|
19 |
+
from gradio import processing_utils, utils, wasm_utils
|
20 |
+
from gradio.components.base import Component, StreamingInput, StreamingOutput
|
21 |
+
from gradio.data_classes import FileData, FileDataDict, MediaStreamChunk
|
22 |
+
from gradio.events import Events
|
23 |
+
from gradio.exceptions import Error
|
24 |
+
|
25 |
+
if TYPE_CHECKING:
|
26 |
+
from gradio.components import Timer
|
27 |
+
|
28 |
+
|
29 |
+
@dataclasses.dataclass
|
30 |
+
class WaveformOptions:
|
31 |
+
"""
|
32 |
+
A dataclass for specifying options for the waveform display in the SourceViewer component. An instance of this class can be passed into the `waveform_options` parameter of `gr.Audio`.
|
33 |
+
Parameters:
|
34 |
+
waveform_color: The color (as a hex string or valid CSS color) of the full waveform representing the amplitude of the audio. Defaults to a light gray color.
|
35 |
+
waveform_progress_color: The color (as a hex string or valid CSS color) that the waveform fills with to as the audio plays. Defaults to the accent color.
|
36 |
+
trim_region_color: The color (as a hex string or valid CSS color) of the trim region. Defaults to the accent color.
|
37 |
+
show_recording_waveform: Whether to show the waveform when recording audio. Defaults to True.
|
38 |
+
show_controls: Whether to show the standard HTML audio player below the waveform when recording audio or playing recorded audio. Defaults to False.
|
39 |
+
skip_length: The percentage (between 0 and 100) of the audio to skip when clicking on the skip forward / skip backward buttons. Defaults to 5.
|
40 |
+
sample_rate: The output sample rate (in Hz) of the audio after editing. Defaults to 44100.
|
41 |
+
"""
|
42 |
+
|
43 |
+
waveform_color: str | None = None
|
44 |
+
waveform_progress_color: str | None = None
|
45 |
+
trim_region_color: str | None = None
|
46 |
+
show_recording_waveform: bool = True
|
47 |
+
show_controls: bool = False
|
48 |
+
skip_length: int | float = 5
|
49 |
+
sample_rate: int = 44100
|
50 |
+
|
51 |
+
|
52 |
+
class SourceViewer(
|
53 |
+
StreamingInput,
|
54 |
+
StreamingOutput,
|
55 |
+
Component,
|
56 |
+
):
|
57 |
+
"""
|
58 |
+
Creates an audio component that can be used to upload/record audio (as an input) or display audio (as an output).
|
59 |
+
Demos: generate_tone, reverse_audio
|
60 |
+
Guides: real-time-speech-recognition
|
61 |
+
"""
|
62 |
+
|
63 |
+
EVENTS = [
|
64 |
+
Events.stream,
|
65 |
+
Events.change,
|
66 |
+
Events.clear,
|
67 |
+
Events.play,
|
68 |
+
Events.pause,
|
69 |
+
Events.stop,
|
70 |
+
Events.pause,
|
71 |
+
Events.start_recording,
|
72 |
+
Events.pause_recording,
|
73 |
+
Events.stop_recording,
|
74 |
+
Events.upload,
|
75 |
+
Events.input,
|
76 |
+
]
|
77 |
+
|
78 |
+
data_model = FileData
|
79 |
+
|
80 |
+
def __init__(
|
81 |
+
self,
|
82 |
+
value: str | Path | tuple[int, np.ndarray] | Callable | None = None,
|
83 |
+
*,
|
84 |
+
sources: list[Literal["upload", "microphone"]]
|
85 |
+
| Literal["upload", "microphone"]
|
86 |
+
| None = None,
|
87 |
+
type: Literal["numpy", "filepath"] = "numpy",
|
88 |
+
label: str | None = None,
|
89 |
+
every: Timer | float | None = None,
|
90 |
+
inputs: Component | Sequence[Component] | set[Component] | None = None,
|
91 |
+
show_label: bool | None = None,
|
92 |
+
container: bool = True,
|
93 |
+
scale: int | None = None,
|
94 |
+
min_width: int = 160,
|
95 |
+
interactive: bool | None = None,
|
96 |
+
visible: bool = True,
|
97 |
+
streaming: bool = False,
|
98 |
+
elem_id: str | None = None,
|
99 |
+
elem_classes: list[str] | str | None = None,
|
100 |
+
render: bool = True,
|
101 |
+
key: int | str | None = None,
|
102 |
+
format: Literal["wav", "mp3"] | None = None,
|
103 |
+
autoplay: bool = False,
|
104 |
+
show_download_button: bool | None = None,
|
105 |
+
show_share_button: bool | None = None,
|
106 |
+
editable: bool = True,
|
107 |
+
min_length: int | None = None,
|
108 |
+
max_length: int | None = None,
|
109 |
+
waveform_options: WaveformOptions | dict | None = None,
|
110 |
+
loop: bool = False,
|
111 |
+
recording: bool = False,
|
112 |
+
):
|
113 |
+
"""
|
114 |
+
Parameters:
|
115 |
+
value: A path, URL, or [sample_rate, numpy array] tuple (sample rate in Hz, audio data as a float or int numpy array) for the default value that SourceViewer component is going to take. If callable, the function will be called whenever the app loads to set the initial value of the component.
|
116 |
+
sources: A list of sources permitted for audio. "upload" creates a box where user can drop an audio file, "microphone" creates a microphone input. The first element in the list will be used as the default source. If None, defaults to ["upload", "microphone"], or ["microphone"] if `streaming` is True.
|
117 |
+
type: The format the audio file is converted to before being passed into the prediction function. "numpy" converts the audio to a tuple consisting of: (int sample rate, numpy.array for the data), "filepath" passes a str path to a temporary file containing the audio.
|
118 |
+
label: the label for this component. Appears above the component and is also used as the header if there are a table of examples for this component. If None and used in a `gr.Interface`, the label will be the name of the parameter this component is assigned to.
|
119 |
+
every: Continously calls `value` to recalculate it if `value` is a function (has no effect otherwise). Can provide a Timer whose tick resets `value`, or a float that provides the regular interval for the reset Timer.
|
120 |
+
inputs: Components that are used as inputs to calculate `value` if `value` is a function (has no effect otherwise). `value` is recalculated any time the inputs change.
|
121 |
+
show_label: if True, will display label.
|
122 |
+
container: If True, will place the component in a container - providing some extra padding around the border.
|
123 |
+
scale: Relative width compared to adjacent Components in a Row. For example, if Component A has scale=2, and Component B has scale=1, A will be twice as wide as B. Should be an integer.
|
124 |
+
min_width: Minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first.
|
125 |
+
interactive: If True, will allow users to upload and edit an audio file. If False, can only be used to play audio. If not provided, this is inferred based on whether the component is used as an input or output.
|
126 |
+
visible: If False, component will be hidden.
|
127 |
+
streaming: If set to True when used in a `live` interface as an input, will automatically stream webcam feed. When used set as an output, takes audio chunks yield from the backend and combines them into one streaming audio output.
|
128 |
+
elem_id: An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
|
129 |
+
elem_classes: An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.
|
130 |
+
render: if False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.
|
131 |
+
key: if assigned, will be used to assume identity across a re-render. Components that have the same key across a re-render will have their value preserved.
|
132 |
+
format: the file extension with which to save audio files. Either 'wav' or 'mp3'. wav files are lossless but will tend to be larger files. mp3 files tend to be smaller. This parameter applies both when this component is used as an input (and `type` is "filepath") to determine which file format to convert user-provided audio to, and when this component is used as an output to determine the format of audio returned to the user. If None, no file format conversion is done and the audio is kept as is. In the case where output audio is returned from the prediction function as numpy array and no `format` is provided, it will be returned as a "wav" file.
|
133 |
+
autoplay: Whether to automatically play the audio when the component is used as an output. Note: browsers will not autoplay audio files if the user has not interacted with the page yet.
|
134 |
+
show_download_button: If True, will show a download button in the corner of the component for saving audio. If False, icon does not appear. By default, it will be True for output components and False for input components.
|
135 |
+
show_share_button: If True, will show a share icon in the corner of the component that allows user to share outputs to Hugging Face Spaces Discussions. If False, icon does not appear. If set to None (default behavior), then the icon appears if this Gradio app is launched on Spaces, but not otherwise.
|
136 |
+
editable: If True, allows users to manipulate the audio file if the component is interactive. Defaults to True.
|
137 |
+
min_length: The minimum length of audio (in seconds) that the user can pass into the prediction function. If None, there is no minimum length.
|
138 |
+
max_length: The maximum length of audio (in seconds) that the user can pass into the prediction function. If None, there is no maximum length.
|
139 |
+
waveform_options: A dictionary of options for the waveform display. Options include: waveform_color (str), waveform_progress_color (str), show_controls (bool), skip_length (int), trim_region_color (str). Default is None, which uses the default values for these options. [See `gr.WaveformOptions` docs](#waveform-options).
|
140 |
+
loop: If True, the audio will loop when it reaches the end and continue playing from the beginning.
|
141 |
+
recording: If True, the audio component will be set to record audio from the microphone if the source is set to "microphone". Defaults to False.
|
142 |
+
"""
|
143 |
+
valid_sources: list[Literal["upload", "microphone"]] = ["upload", "microphone"]
|
144 |
+
if sources is None:
|
145 |
+
self.sources = ["microphone"] if streaming else valid_sources
|
146 |
+
elif isinstance(sources, str) and sources in valid_sources:
|
147 |
+
self.sources = [sources]
|
148 |
+
elif isinstance(sources, list):
|
149 |
+
self.sources = sources
|
150 |
+
else:
|
151 |
+
raise ValueError(
|
152 |
+
f"`sources` must be a list consisting of elements in {valid_sources}"
|
153 |
+
)
|
154 |
+
for source in self.sources:
|
155 |
+
if source not in valid_sources:
|
156 |
+
raise ValueError(
|
157 |
+
f"`sources` must a list consisting of elements in {valid_sources}"
|
158 |
+
)
|
159 |
+
valid_types = ["numpy", "filepath"]
|
160 |
+
if type not in valid_types:
|
161 |
+
raise ValueError(
|
162 |
+
f"Invalid value for parameter `type`: {type}. Please choose from one of: {' '.join(valid_types)}"
|
163 |
+
)
|
164 |
+
self.type = type
|
165 |
+
self.streaming = streaming
|
166 |
+
if self.streaming and "microphone" not in self.sources:
|
167 |
+
raise ValueError(
|
168 |
+
"SourceViewer streaming only available if sources includes 'microphone'."
|
169 |
+
)
|
170 |
+
valid_formats = ["wav", "mp3"]
|
171 |
+
if format is not None and format.lower() not in valid_formats:
|
172 |
+
raise ValueError(
|
173 |
+
f"Invalid value for parameter `format`: {format}. Please choose from one of: {' '.join(valid_formats)}"
|
174 |
+
)
|
175 |
+
self.format = format and format.lower()
|
176 |
+
self.autoplay = autoplay
|
177 |
+
self.loop = loop
|
178 |
+
self.show_download_button = show_download_button
|
179 |
+
self.show_share_button = (
|
180 |
+
(utils.get_space() is not None)
|
181 |
+
if show_share_button is None
|
182 |
+
else show_share_button
|
183 |
+
)
|
184 |
+
self.editable = editable
|
185 |
+
if waveform_options is None:
|
186 |
+
self.waveform_options = WaveformOptions()
|
187 |
+
elif isinstance(waveform_options, dict):
|
188 |
+
self.waveform_options = WaveformOptions(**waveform_options)
|
189 |
+
else:
|
190 |
+
self.waveform_options = waveform_options
|
191 |
+
self.min_length = min_length
|
192 |
+
self.max_length = max_length
|
193 |
+
self.recording = recording
|
194 |
+
super().__init__(
|
195 |
+
label=label,
|
196 |
+
every=every,
|
197 |
+
inputs=inputs,
|
198 |
+
show_label=show_label,
|
199 |
+
container=container,
|
200 |
+
scale=scale,
|
201 |
+
min_width=min_width,
|
202 |
+
interactive=interactive,
|
203 |
+
visible=visible,
|
204 |
+
elem_id=elem_id,
|
205 |
+
elem_classes=elem_classes,
|
206 |
+
render=render,
|
207 |
+
key=key,
|
208 |
+
value=value,
|
209 |
+
)
|
210 |
+
|
211 |
+
def example_payload(self) -> Any:
|
212 |
+
return handle_file(
|
213 |
+
"https://github.com/gradio-app/gradio/raw/main/test/test_files/audio_sample.wav"
|
214 |
+
)
|
215 |
+
|
216 |
+
def example_value(self) -> Any:
|
217 |
+
return "https://github.com/gradio-app/gradio/raw/main/test/test_files/audio_sample.wav"
|
218 |
+
|
219 |
+
def preprocess(
|
220 |
+
self, payload: FileData | None
|
221 |
+
) -> str | tuple[int, np.ndarray] | None:
|
222 |
+
"""
|
223 |
+
Parameters:
|
224 |
+
payload: audio data as a FileData object, or None.
|
225 |
+
Returns:
|
226 |
+
passes audio as one of these formats (depending on `type`): a `str` filepath, or `tuple` of (sample rate in Hz, audio data as numpy array). If the latter, the audio data is a 16-bit `int` array whose values range from -32768 to 32767 and shape of the audio data array is (samples,) for mono audio or (samples, channels) for multi-channel audio.
|
227 |
+
"""
|
228 |
+
if payload is None:
|
229 |
+
return payload
|
230 |
+
|
231 |
+
if not payload.path:
|
232 |
+
raise ValueError("payload path missing")
|
233 |
+
|
234 |
+
needs_conversion = False
|
235 |
+
original_suffix = Path(payload.path).suffix.lower()
|
236 |
+
if self.format is not None and original_suffix != f".{self.format}":
|
237 |
+
needs_conversion = True
|
238 |
+
|
239 |
+
if self.min_length is not None or self.max_length is not None:
|
240 |
+
sample_rate, data = processing_utils.audio_from_file(payload.path)
|
241 |
+
duration = len(data) / sample_rate
|
242 |
+
if self.min_length is not None and duration < self.min_length:
|
243 |
+
raise Error(
|
244 |
+
f"SourceViewer is too short, and must be at least {self.min_length} seconds"
|
245 |
+
)
|
246 |
+
if self.max_length is not None and duration > self.max_length:
|
247 |
+
raise Error(
|
248 |
+
f"SourceViewer is too long, and must be at most {self.max_length} seconds"
|
249 |
+
)
|
250 |
+
|
251 |
+
if self.type == "numpy":
|
252 |
+
return processing_utils.audio_from_file(payload.path)
|
253 |
+
elif self.type == "filepath":
|
254 |
+
if not needs_conversion:
|
255 |
+
return payload.path
|
256 |
+
sample_rate, data = processing_utils.audio_from_file(payload.path)
|
257 |
+
output_file = str(Path(payload.path).with_suffix(f".{self.format}"))
|
258 |
+
assert self.format is not None # noqa: S101
|
259 |
+
processing_utils.audio_to_file(
|
260 |
+
sample_rate, data, output_file, format=self.format
|
261 |
+
)
|
262 |
+
return output_file
|
263 |
+
else:
|
264 |
+
raise ValueError(
|
265 |
+
"Unknown type: "
|
266 |
+
+ str(self.type)
|
267 |
+
+ ". Please choose from: 'numpy', 'filepath'."
|
268 |
+
)
|
269 |
+
|
270 |
+
def postprocess(
|
271 |
+
self, value: str | Path | bytes | tuple[int, np.ndarray] | None
|
272 |
+
) -> FileData | bytes | None:
|
273 |
+
"""
|
274 |
+
Parameters:
|
275 |
+
value: expects audio data in any of these formats: a `str` or `pathlib.Path` filepath or URL to an audio file, or a `bytes` object (recommended for streaming), or a `tuple` of (sample rate in Hz, audio data as numpy array). Note: if audio is supplied as a numpy array, the audio will be normalized by its peak value to avoid distortion or clipping in the resulting audio.
|
276 |
+
Returns:
|
277 |
+
FileData object, bytes, or None.
|
278 |
+
"""
|
279 |
+
orig_name = None
|
280 |
+
if value is None:
|
281 |
+
return None
|
282 |
+
|
283 |
+
if isinstance(value, bytes):
|
284 |
+
if self.streaming:
|
285 |
+
return value
|
286 |
+
file_path = processing_utils.save_bytes_to_cache(
|
287 |
+
value, "audio", cache_dir=self.GRADIO_CACHE
|
288 |
+
)
|
289 |
+
orig_name = Path(file_path).name
|
290 |
+
elif isinstance(value, tuple):
|
291 |
+
sample_rate, data = value
|
292 |
+
file_path = processing_utils.save_audio_to_cache(
|
293 |
+
data,
|
294 |
+
sample_rate,
|
295 |
+
format=self.format or "wav",
|
296 |
+
cache_dir=self.GRADIO_CACHE,
|
297 |
+
)
|
298 |
+
orig_name = Path(file_path).name
|
299 |
+
elif isinstance(value, (str, Path)):
|
300 |
+
original_suffix = Path(value).suffix.lower()
|
301 |
+
if self.format is not None and original_suffix != f".{self.format}":
|
302 |
+
sample_rate, data = processing_utils.audio_from_file(str(value))
|
303 |
+
file_path = processing_utils.save_audio_to_cache(
|
304 |
+
data, sample_rate, format=self.format, cache_dir=self.GRADIO_CACHE
|
305 |
+
)
|
306 |
+
else:
|
307 |
+
file_path = str(value)
|
308 |
+
orig_name = Path(file_path).name if Path(file_path).exists() else None
|
309 |
+
else:
|
310 |
+
raise ValueError(f"Cannot process {value} as SourceViewer")
|
311 |
+
return FileData(path=file_path, orig_name=orig_name)
|
312 |
+
|
313 |
+
@staticmethod
|
314 |
+
def _convert_to_adts(data: bytes):
|
315 |
+
if wasm_utils.IS_WASM:
|
316 |
+
raise wasm_utils.WasmUnsupportedError(
|
317 |
+
"SourceViewer streaming is not supported in the Wasm mode."
|
318 |
+
)
|
319 |
+
segment = AudioSegment.from_file(io.BytesIO(data))
|
320 |
+
|
321 |
+
buffer = io.BytesIO()
|
322 |
+
segment.export(buffer, format="adts") # ADTS is a container format for AAC
|
323 |
+
aac_data = buffer.getvalue()
|
324 |
+
return aac_data, len(segment) / 1000.0
|
325 |
+
|
326 |
+
@staticmethod
|
327 |
+
async def covert_to_adts(data: bytes) -> tuple[bytes, float]:
|
328 |
+
return await anyio.to_thread.run_sync(SourceViewer._convert_to_adts, data)
|
329 |
+
|
330 |
+
async def stream_output(
|
331 |
+
self,
|
332 |
+
value,
|
333 |
+
output_id: str,
|
334 |
+
first_chunk: bool, # noqa: ARG002
|
335 |
+
) -> tuple[MediaStreamChunk | None, FileDataDict]:
|
336 |
+
output_file: FileDataDict = {
|
337 |
+
"path": output_id,
|
338 |
+
"is_stream": True,
|
339 |
+
"orig_name": "audio-stream.mp3",
|
340 |
+
"meta": {"_type": "gradio.FileData"},
|
341 |
+
}
|
342 |
+
if value is None:
|
343 |
+
return None, output_file
|
344 |
+
if isinstance(value, bytes):
|
345 |
+
value, duration = await self.covert_to_adts(value)
|
346 |
+
return {
|
347 |
+
"data": value,
|
348 |
+
"duration": duration,
|
349 |
+
"extension": ".aac",
|
350 |
+
}, output_file
|
351 |
+
if client_utils.is_http_url_like(value["path"]):
|
352 |
+
response = httpx.get(value["path"])
|
353 |
+
binary_data = response.content
|
354 |
+
else:
|
355 |
+
output_file["orig_name"] = value["orig_name"]
|
356 |
+
file_path = value["path"]
|
357 |
+
with open(file_path, "rb") as f:
|
358 |
+
binary_data = f.read()
|
359 |
+
value, duration = await self.covert_to_adts(binary_data)
|
360 |
+
return {"data": value, "duration": duration, "extension": ".aac"}, output_file
|
361 |
+
|
362 |
+
async def combine_stream(
|
363 |
+
self,
|
364 |
+
stream: list[bytes],
|
365 |
+
desired_output_format: str | None = None,
|
366 |
+
only_file=False, # noqa: ARG002
|
367 |
+
) -> FileData:
|
368 |
+
output_file = FileData(
|
369 |
+
path=processing_utils.save_bytes_to_cache(
|
370 |
+
b"".join(stream), "audio.mp3", cache_dir=self.GRADIO_CACHE
|
371 |
+
),
|
372 |
+
is_stream=False,
|
373 |
+
orig_name="audio-stream.mp3",
|
374 |
+
)
|
375 |
+
if desired_output_format and desired_output_format != "mp3":
|
376 |
+
new_path = Path(output_file.path).with_suffix(f".{desired_output_format}")
|
377 |
+
AudioSegment.from_file(output_file.path).export(
|
378 |
+
new_path, format=desired_output_format
|
379 |
+
)
|
380 |
+
output_file.path = str(new_path)
|
381 |
+
return output_file
|
382 |
+
|
383 |
+
def process_example(
|
384 |
+
self, value: tuple[int, np.ndarray] | str | Path | bytes | None
|
385 |
+
) -> str:
|
386 |
+
if value is None:
|
387 |
+
return ""
|
388 |
+
elif isinstance(value, (str, Path)):
|
389 |
+
return Path(value).name
|
390 |
+
return "(audio)"
|
391 |
+
|
392 |
+
def check_streamable(self):
|
393 |
+
if (
|
394 |
+
self.sources is not None
|
395 |
+
and "microphone" not in self.sources
|
396 |
+
and self.streaming
|
397 |
+
):
|
398 |
+
raise ValueError(
|
399 |
+
"SourceViewer streaming only available if source includes 'microphone'."
|
400 |
+
)
|
sourceviewer/demo/__init__.py
ADDED
File without changes
|
sourceviewer/demo/app.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
from gradio_sourceviewer import SourceViewer
|
4 |
+
|
5 |
+
|
6 |
+
example = SourceViewer().example_value()
|
7 |
+
|
8 |
+
demo = gr.Interface(
|
9 |
+
lambda x:x,
|
10 |
+
SourceViewer(), # interactive version of your component
|
11 |
+
SourceViewer(), # static version of your component
|
12 |
+
# examples=[[example]], # uncomment this line to view the "example version" of your component
|
13 |
+
)
|
14 |
+
|
15 |
+
|
16 |
+
if __name__ == "__main__":
|
17 |
+
demo.launch()
|
sourceviewer/demo/requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
gradio_sourceviewer
|
sourceviewer/frontend/Example.svelte
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<script lang="ts">
|
2 |
+
export let value: string | null;
|
3 |
+
export let type: "gallery" | "table";
|
4 |
+
export let selected = false;
|
5 |
+
</script>
|
6 |
+
|
7 |
+
<div
|
8 |
+
class:table={type === "table"}
|
9 |
+
class:gallery={type === "gallery"}
|
10 |
+
class:selected
|
11 |
+
>
|
12 |
+
{value ? value : ""}
|
13 |
+
</div>
|
14 |
+
|
15 |
+
<style>
|
16 |
+
.gallery {
|
17 |
+
padding: var(--size-1) var(--size-2);
|
18 |
+
}
|
19 |
+
</style>
|
sourceviewer/frontend/Index.svelte
ADDED
@@ -0,0 +1,275 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<svelte:options accessors={true} />
|
2 |
+
|
3 |
+
<script lang="ts">
|
4 |
+
import type { Gradio, ShareData } from "@gradio/utils";
|
5 |
+
|
6 |
+
import type { FileData } from "@gradio/client";
|
7 |
+
import type { LoadingStatus } from "@gradio/statustracker";
|
8 |
+
import { afterUpdate, onMount } from "svelte";
|
9 |
+
|
10 |
+
import StaticAudio from "./static/StaticAudio.svelte";
|
11 |
+
import InteractiveAudio from "./interactive/InteractiveAudio.svelte";
|
12 |
+
import { StatusTracker } from "@gradio/statustracker";
|
13 |
+
import { Block, UploadText } from "@gradio/atoms";
|
14 |
+
import type { WaveformOptions } from "./shared/types";
|
15 |
+
|
16 |
+
export let value_is_output = false;
|
17 |
+
export let elem_id = "";
|
18 |
+
export let elem_classes: string[] = [];
|
19 |
+
export let visible = true;
|
20 |
+
export let interactive: boolean;
|
21 |
+
export let value: null | FileData = null;
|
22 |
+
export let sources:
|
23 |
+
| ["microphone"]
|
24 |
+
| ["upload"]
|
25 |
+
| ["microphone", "upload"]
|
26 |
+
| ["upload", "microphone"];
|
27 |
+
export let label: string;
|
28 |
+
export let root: string;
|
29 |
+
export let show_label: boolean;
|
30 |
+
export let container = true;
|
31 |
+
export let scale: number | null = null;
|
32 |
+
export let min_width: number | undefined = undefined;
|
33 |
+
export let loading_status: LoadingStatus;
|
34 |
+
export let autoplay = false;
|
35 |
+
export let loop = false;
|
36 |
+
export let show_download_button: boolean;
|
37 |
+
export let show_share_button = false;
|
38 |
+
export let editable = true;
|
39 |
+
export let waveform_options: WaveformOptions = {};
|
40 |
+
export let pending: boolean;
|
41 |
+
export let streaming: boolean;
|
42 |
+
export let stream_every: number;
|
43 |
+
export let input_ready: boolean;
|
44 |
+
export let recording = false;
|
45 |
+
let uploading = false;
|
46 |
+
$: input_ready = !uploading;
|
47 |
+
|
48 |
+
let stream_state = "closed";
|
49 |
+
let _modify_stream: (state: "open" | "closed" | "waiting") => void;
|
50 |
+
export function modify_stream_state(
|
51 |
+
state: "open" | "closed" | "waiting"
|
52 |
+
): void {
|
53 |
+
stream_state = state;
|
54 |
+
_modify_stream(state);
|
55 |
+
}
|
56 |
+
export const get_stream_state: () => void = () => stream_state;
|
57 |
+
export let set_time_limit: (time: number) => void;
|
58 |
+
export let gradio: Gradio<{
|
59 |
+
input: never;
|
60 |
+
change: typeof value;
|
61 |
+
stream: typeof value;
|
62 |
+
error: string;
|
63 |
+
warning: string;
|
64 |
+
edit: never;
|
65 |
+
play: never;
|
66 |
+
pause: never;
|
67 |
+
stop: never;
|
68 |
+
end: never;
|
69 |
+
start_recording: never;
|
70 |
+
pause_recording: never;
|
71 |
+
stop_recording: never;
|
72 |
+
upload: never;
|
73 |
+
clear: never;
|
74 |
+
share: ShareData;
|
75 |
+
clear_status: LoadingStatus;
|
76 |
+
close_stream: string;
|
77 |
+
}>;
|
78 |
+
|
79 |
+
let old_value: null | FileData = null;
|
80 |
+
|
81 |
+
let active_source: "microphone" | "upload";
|
82 |
+
|
83 |
+
let initial_value: null | FileData = value;
|
84 |
+
|
85 |
+
$: if (value && initial_value === null) {
|
86 |
+
initial_value = value;
|
87 |
+
}
|
88 |
+
|
89 |
+
const handle_reset_value = (): void => {
|
90 |
+
if (initial_value === null || value === initial_value) {
|
91 |
+
return;
|
92 |
+
}
|
93 |
+
|
94 |
+
value = initial_value;
|
95 |
+
};
|
96 |
+
|
97 |
+
$: {
|
98 |
+
if (JSON.stringify(value) !== JSON.stringify(old_value)) {
|
99 |
+
old_value = value;
|
100 |
+
gradio.dispatch("change");
|
101 |
+
if (!value_is_output) {
|
102 |
+
gradio.dispatch("input");
|
103 |
+
}
|
104 |
+
}
|
105 |
+
}
|
106 |
+
|
107 |
+
let dragging: boolean;
|
108 |
+
|
109 |
+
$: if (!active_source && sources) {
|
110 |
+
active_source = sources[0];
|
111 |
+
}
|
112 |
+
|
113 |
+
let waveform_settings: Record<string, any>;
|
114 |
+
|
115 |
+
let color_accent = "darkorange";
|
116 |
+
|
117 |
+
onMount(() => {
|
118 |
+
color_accent = getComputedStyle(document?.documentElement).getPropertyValue(
|
119 |
+
"--color-accent"
|
120 |
+
);
|
121 |
+
set_trim_region_colour();
|
122 |
+
waveform_settings.waveColor = waveform_options.waveform_color || "#9ca3af";
|
123 |
+
waveform_settings.progressColor =
|
124 |
+
waveform_options.waveform_progress_color || color_accent;
|
125 |
+
waveform_settings.mediaControls = waveform_options.show_controls;
|
126 |
+
waveform_settings.sampleRate = waveform_options.sample_rate || 44100;
|
127 |
+
});
|
128 |
+
|
129 |
+
$: waveform_settings = {
|
130 |
+
height: 50,
|
131 |
+
|
132 |
+
barWidth: 2,
|
133 |
+
barGap: 3,
|
134 |
+
cursorWidth: 2,
|
135 |
+
cursorColor: "#ddd5e9",
|
136 |
+
autoplay: autoplay,
|
137 |
+
barRadius: 10,
|
138 |
+
dragToSeek: true,
|
139 |
+
normalize: true,
|
140 |
+
minPxPerSec: 20
|
141 |
+
};
|
142 |
+
|
143 |
+
const trim_region_settings = {
|
144 |
+
color: waveform_options.trim_region_color,
|
145 |
+
drag: true,
|
146 |
+
resize: true
|
147 |
+
};
|
148 |
+
|
149 |
+
function set_trim_region_colour(): void {
|
150 |
+
document.documentElement.style.setProperty(
|
151 |
+
"--trim-region-color",
|
152 |
+
trim_region_settings.color || color_accent
|
153 |
+
);
|
154 |
+
}
|
155 |
+
|
156 |
+
function handle_error({ detail }: CustomEvent<string>): void {
|
157 |
+
const [level, status] = detail.includes("Invalid file type")
|
158 |
+
? ["warning", "complete"]
|
159 |
+
: ["error", "error"];
|
160 |
+
loading_status = loading_status || {};
|
161 |
+
loading_status.status = status as LoadingStatus["status"];
|
162 |
+
loading_status.message = detail;
|
163 |
+
gradio.dispatch(level as "error" | "warning", detail);
|
164 |
+
}
|
165 |
+
|
166 |
+
afterUpdate(() => {
|
167 |
+
value_is_output = false;
|
168 |
+
});
|
169 |
+
</script>
|
170 |
+
|
171 |
+
{#if !interactive}
|
172 |
+
<Block
|
173 |
+
variant={"solid"}
|
174 |
+
border_mode={dragging ? "focus" : "base"}
|
175 |
+
padding={false}
|
176 |
+
allow_overflow={false}
|
177 |
+
{elem_id}
|
178 |
+
{elem_classes}
|
179 |
+
{visible}
|
180 |
+
{container}
|
181 |
+
{scale}
|
182 |
+
{min_width}
|
183 |
+
>
|
184 |
+
<StatusTracker
|
185 |
+
autoscroll={gradio.autoscroll}
|
186 |
+
i18n={gradio.i18n}
|
187 |
+
{...loading_status}
|
188 |
+
on:clear_status={() => gradio.dispatch("clear_status", loading_status)}
|
189 |
+
/>
|
190 |
+
|
191 |
+
<StaticAudio
|
192 |
+
i18n={gradio.i18n}
|
193 |
+
{show_label}
|
194 |
+
{show_download_button}
|
195 |
+
{show_share_button}
|
196 |
+
{value}
|
197 |
+
{label}
|
198 |
+
{loop}
|
199 |
+
{waveform_settings}
|
200 |
+
{waveform_options}
|
201 |
+
{editable}
|
202 |
+
on:share={(e) => gradio.dispatch("share", e.detail)}
|
203 |
+
on:error={(e) => gradio.dispatch("error", e.detail)}
|
204 |
+
on:play={() => gradio.dispatch("play")}
|
205 |
+
on:pause={() => gradio.dispatch("pause")}
|
206 |
+
on:stop={() => gradio.dispatch("stop")}
|
207 |
+
/>
|
208 |
+
</Block>
|
209 |
+
{:else}
|
210 |
+
<Block
|
211 |
+
variant={value === null && active_source === "upload" ? "dashed" : "solid"}
|
212 |
+
border_mode={dragging ? "focus" : "base"}
|
213 |
+
padding={false}
|
214 |
+
allow_overflow={false}
|
215 |
+
{elem_id}
|
216 |
+
{elem_classes}
|
217 |
+
{visible}
|
218 |
+
{container}
|
219 |
+
{scale}
|
220 |
+
{min_width}
|
221 |
+
>
|
222 |
+
<StatusTracker
|
223 |
+
autoscroll={gradio.autoscroll}
|
224 |
+
i18n={gradio.i18n}
|
225 |
+
{...loading_status}
|
226 |
+
on:clear_status={() => gradio.dispatch("clear_status", loading_status)}
|
227 |
+
/>
|
228 |
+
<InteractiveAudio
|
229 |
+
{label}
|
230 |
+
{show_label}
|
231 |
+
{show_download_button}
|
232 |
+
{value}
|
233 |
+
on:change={({ detail }) => (value = detail)}
|
234 |
+
on:stream={({ detail }) => {
|
235 |
+
value = detail;
|
236 |
+
gradio.dispatch("stream", value);
|
237 |
+
}}
|
238 |
+
on:drag={({ detail }) => (dragging = detail)}
|
239 |
+
{root}
|
240 |
+
{sources}
|
241 |
+
{active_source}
|
242 |
+
{pending}
|
243 |
+
{streaming}
|
244 |
+
bind:recording
|
245 |
+
{loop}
|
246 |
+
max_file_size={gradio.max_file_size}
|
247 |
+
{handle_reset_value}
|
248 |
+
{editable}
|
249 |
+
bind:dragging
|
250 |
+
bind:uploading
|
251 |
+
on:edit={() => gradio.dispatch("edit")}
|
252 |
+
on:play={() => gradio.dispatch("play")}
|
253 |
+
on:pause={() => gradio.dispatch("pause")}
|
254 |
+
on:stop={() => gradio.dispatch("stop")}
|
255 |
+
on:start_recording={() => gradio.dispatch("start_recording")}
|
256 |
+
on:pause_recording={() => gradio.dispatch("pause_recording")}
|
257 |
+
on:stop_recording={(e) => gradio.dispatch("stop_recording")}
|
258 |
+
on:upload={() => gradio.dispatch("upload")}
|
259 |
+
on:clear={() => gradio.dispatch("clear")}
|
260 |
+
on:error={handle_error}
|
261 |
+
on:close_stream={() => gradio.dispatch("close_stream", "stream")}
|
262 |
+
i18n={gradio.i18n}
|
263 |
+
{waveform_settings}
|
264 |
+
{waveform_options}
|
265 |
+
{trim_region_settings}
|
266 |
+
{stream_every}
|
267 |
+
bind:modify_stream={_modify_stream}
|
268 |
+
bind:set_time_limit
|
269 |
+
upload={(...args) => gradio.client.upload(...args)}
|
270 |
+
stream_handler={(...args) => gradio.client.stream(...args)}
|
271 |
+
>
|
272 |
+
<UploadText i18n={gradio.i18n} type="audio" />
|
273 |
+
</InteractiveAudio>
|
274 |
+
</Block>
|
275 |
+
{/if}
|
sourceviewer/frontend/gradio.config.js
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
export default {
|
2 |
+
plugins: [],
|
3 |
+
svelte: {
|
4 |
+
preprocess: [],
|
5 |
+
},
|
6 |
+
build: {
|
7 |
+
target: "modules",
|
8 |
+
},
|
9 |
+
};
|
sourceviewer/frontend/index.ts
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { default as Index } from "./Index.svelte";
|
2 |
+
export default Index;
|
3 |
+
export { default as BaseStaticAudio } from "./static/StaticAudio.svelte";
|
4 |
+
export { default as BaseInteractiveAudio } from "./interactive/InteractiveAudio.svelte";
|
5 |
+
export { default as BasePlayer } from "./player/AudioPlayer.svelte";
|
6 |
+
export type { WaveformOptions } from "./shared/types";
|
7 |
+
export { default as BaseExample } from "./Example.svelte";
|
sourceviewer/frontend/interactive/InteractiveAudio.svelte
ADDED
@@ -0,0 +1,332 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<script lang="ts">
|
2 |
+
import { onDestroy, createEventDispatcher, tick } from "svelte";
|
3 |
+
import { Upload, ModifyUpload } from "@gradio/upload";
|
4 |
+
import { prepare_files, type FileData, type Client } from "@gradio/client";
|
5 |
+
import { BlockLabel } from "@gradio/atoms";
|
6 |
+
import { Music } from "@gradio/icons";
|
7 |
+
import { StreamingBar } from "@gradio/statustracker";
|
8 |
+
import AudioPlayer from "../player/AudioPlayer.svelte";
|
9 |
+
|
10 |
+
import type { IBlobEvent, IMediaRecorder } from "extendable-media-recorder";
|
11 |
+
import type { I18nFormatter } from "js/core/src/gradio_helper";
|
12 |
+
import AudioRecorder from "../recorder/AudioRecorder.svelte";
|
13 |
+
import StreamAudio from "../streaming/StreamAudio.svelte";
|
14 |
+
import { SelectSource } from "@gradio/atoms";
|
15 |
+
import type { WaveformOptions } from "../shared/types";
|
16 |
+
|
17 |
+
export let value: null | FileData = null;
|
18 |
+
export let label: string;
|
19 |
+
export let root: string;
|
20 |
+
export let loop: boolean;
|
21 |
+
export let show_label = true;
|
22 |
+
export let show_download_button = false;
|
23 |
+
export let sources:
|
24 |
+
| ["microphone"]
|
25 |
+
| ["upload"]
|
26 |
+
| ["microphone", "upload"]
|
27 |
+
| ["upload", "microphone"] = ["microphone", "upload"];
|
28 |
+
export let pending = false;
|
29 |
+
export let streaming = false;
|
30 |
+
export let i18n: I18nFormatter;
|
31 |
+
export let waveform_settings: Record<string, any>;
|
32 |
+
export let trim_region_settings = {};
|
33 |
+
export let waveform_options: WaveformOptions = {};
|
34 |
+
export let dragging: boolean;
|
35 |
+
export let active_source: "microphone" | "upload";
|
36 |
+
export let handle_reset_value: () => void = () => {};
|
37 |
+
export let editable = true;
|
38 |
+
export let max_file_size: number | null = null;
|
39 |
+
export let upload: Client["upload"];
|
40 |
+
export let stream_handler: Client["stream"];
|
41 |
+
export let stream_every: number;
|
42 |
+
export let uploading = false;
|
43 |
+
export let recording = false;
|
44 |
+
|
45 |
+
let time_limit: number | null = null;
|
46 |
+
let stream_state: "open" | "waiting" | "closed" = "closed";
|
47 |
+
|
48 |
+
export const modify_stream: (state: "open" | "closed" | "waiting") => void = (
|
49 |
+
state: "open" | "closed" | "waiting"
|
50 |
+
) => {
|
51 |
+
if (state === "closed") {
|
52 |
+
time_limit = null;
|
53 |
+
stream_state = "closed";
|
54 |
+
} else if (state === "waiting") {
|
55 |
+
stream_state = "waiting";
|
56 |
+
} else {
|
57 |
+
stream_state = "open";
|
58 |
+
}
|
59 |
+
};
|
60 |
+
|
61 |
+
export const set_time_limit = (time: number): void => {
|
62 |
+
if (recording) time_limit = time;
|
63 |
+
};
|
64 |
+
|
65 |
+
$: dispatch("drag", dragging);
|
66 |
+
|
67 |
+
// TODO: make use of this
|
68 |
+
// export let type: "normal" | "numpy" = "normal";
|
69 |
+
let recorder: IMediaRecorder;
|
70 |
+
let mode = "";
|
71 |
+
let header: Uint8Array | undefined = undefined;
|
72 |
+
let pending_stream: Uint8Array[] = [];
|
73 |
+
let submit_pending_stream_on_pending_end = false;
|
74 |
+
let inited = false;
|
75 |
+
|
76 |
+
const NUM_HEADER_BYTES = 44;
|
77 |
+
let audio_chunks: Blob[] = [];
|
78 |
+
let module_promises: [
|
79 |
+
Promise<typeof import("extendable-media-recorder")>,
|
80 |
+
Promise<typeof import("extendable-media-recorder-wav-encoder")>
|
81 |
+
];
|
82 |
+
|
83 |
+
function get_modules(): void {
|
84 |
+
module_promises = [
|
85 |
+
import("extendable-media-recorder"),
|
86 |
+
import("extendable-media-recorder-wav-encoder")
|
87 |
+
];
|
88 |
+
}
|
89 |
+
|
90 |
+
const is_browser = typeof window !== "undefined";
|
91 |
+
if (is_browser && streaming) {
|
92 |
+
get_modules();
|
93 |
+
}
|
94 |
+
|
95 |
+
const dispatch = createEventDispatcher<{
|
96 |
+
change: FileData | null;
|
97 |
+
stream: FileData;
|
98 |
+
edit: never;
|
99 |
+
play: never;
|
100 |
+
pause: never;
|
101 |
+
stop: never;
|
102 |
+
end: never;
|
103 |
+
drag: boolean;
|
104 |
+
error: string;
|
105 |
+
upload: FileData;
|
106 |
+
clear: undefined;
|
107 |
+
start_recording: undefined;
|
108 |
+
pause_recording: undefined;
|
109 |
+
stop_recording: undefined;
|
110 |
+
close_stream: undefined;
|
111 |
+
}>();
|
112 |
+
|
113 |
+
const dispatch_blob = async (
|
114 |
+
blobs: Uint8Array[] | Blob[],
|
115 |
+
event: "stream" | "change" | "stop_recording"
|
116 |
+
): Promise<void> => {
|
117 |
+
let _audio_blob = new File(blobs, "audio.wav");
|
118 |
+
const val = await prepare_files([_audio_blob], event === "stream");
|
119 |
+
value = (
|
120 |
+
(await upload(val, root, undefined, max_file_size || undefined))?.filter(
|
121 |
+
Boolean
|
122 |
+
) as FileData[]
|
123 |
+
)[0];
|
124 |
+
dispatch(event, value);
|
125 |
+
};
|
126 |
+
|
127 |
+
onDestroy(() => {
|
128 |
+
if (streaming && recorder && recorder.state !== "inactive") {
|
129 |
+
recorder.stop();
|
130 |
+
}
|
131 |
+
});
|
132 |
+
|
133 |
+
async function prepare_audio(): Promise<void> {
|
134 |
+
let stream: MediaStream | null;
|
135 |
+
|
136 |
+
try {
|
137 |
+
stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
138 |
+
} catch (err) {
|
139 |
+
if (!navigator.mediaDevices) {
|
140 |
+
dispatch("error", i18n("audio.no_device_support"));
|
141 |
+
return;
|
142 |
+
}
|
143 |
+
if (err instanceof DOMException && err.name == "NotAllowedError") {
|
144 |
+
dispatch("error", i18n("audio.allow_recording_access"));
|
145 |
+
return;
|
146 |
+
}
|
147 |
+
throw err;
|
148 |
+
}
|
149 |
+
if (stream == null) return;
|
150 |
+
|
151 |
+
if (streaming) {
|
152 |
+
const [{ MediaRecorder, register }, { connect }] =
|
153 |
+
await Promise.all(module_promises);
|
154 |
+
await register(await connect());
|
155 |
+
recorder = new MediaRecorder(stream, { mimeType: "audio/wav" });
|
156 |
+
recorder.addEventListener("dataavailable", handle_chunk);
|
157 |
+
} else {
|
158 |
+
recorder = new MediaRecorder(stream);
|
159 |
+
recorder.addEventListener("dataavailable", (event) => {
|
160 |
+
audio_chunks.push(event.data);
|
161 |
+
});
|
162 |
+
}
|
163 |
+
recorder.addEventListener("stop", async () => {
|
164 |
+
recording = false;
|
165 |
+
// recorder.stop();
|
166 |
+
await dispatch_blob(audio_chunks, "change");
|
167 |
+
await dispatch_blob(audio_chunks, "stop_recording");
|
168 |
+
audio_chunks = [];
|
169 |
+
});
|
170 |
+
inited = true;
|
171 |
+
}
|
172 |
+
|
173 |
+
async function handle_chunk(event: IBlobEvent): Promise<void> {
|
174 |
+
let buffer = await event.data.arrayBuffer();
|
175 |
+
let payload = new Uint8Array(buffer);
|
176 |
+
if (!header) {
|
177 |
+
header = new Uint8Array(buffer.slice(0, NUM_HEADER_BYTES));
|
178 |
+
payload = new Uint8Array(buffer.slice(NUM_HEADER_BYTES));
|
179 |
+
}
|
180 |
+
if (pending) {
|
181 |
+
pending_stream.push(payload);
|
182 |
+
} else {
|
183 |
+
let blobParts = [header].concat(pending_stream, [payload]);
|
184 |
+
if (!recording || stream_state === "waiting") return;
|
185 |
+
dispatch_blob(blobParts, "stream");
|
186 |
+
pending_stream = [];
|
187 |
+
}
|
188 |
+
}
|
189 |
+
|
190 |
+
$: if (submit_pending_stream_on_pending_end && pending === false) {
|
191 |
+
submit_pending_stream_on_pending_end = false;
|
192 |
+
if (header && pending_stream) {
|
193 |
+
let blobParts: Uint8Array[] = [header].concat(pending_stream);
|
194 |
+
pending_stream = [];
|
195 |
+
dispatch_blob(blobParts, "stream");
|
196 |
+
}
|
197 |
+
}
|
198 |
+
|
199 |
+
async function record(): Promise<void> {
|
200 |
+
recording = true;
|
201 |
+
dispatch("start_recording");
|
202 |
+
if (!inited) await prepare_audio();
|
203 |
+
header = undefined;
|
204 |
+
if (streaming && recorder.state != "recording") {
|
205 |
+
recorder.start(stream_every * 1000);
|
206 |
+
}
|
207 |
+
}
|
208 |
+
|
209 |
+
function clear(): void {
|
210 |
+
dispatch("change", null);
|
211 |
+
dispatch("clear");
|
212 |
+
mode = "";
|
213 |
+
value = null;
|
214 |
+
}
|
215 |
+
|
216 |
+
function handle_load({ detail }: { detail: FileData }): void {
|
217 |
+
value = detail;
|
218 |
+
dispatch("change", detail);
|
219 |
+
dispatch("upload", detail);
|
220 |
+
}
|
221 |
+
|
222 |
+
async function stop(): Promise<void> {
|
223 |
+
recording = false;
|
224 |
+
|
225 |
+
if (streaming) {
|
226 |
+
dispatch("close_stream");
|
227 |
+
dispatch("stop_recording");
|
228 |
+
recorder.stop();
|
229 |
+
|
230 |
+
if (pending) {
|
231 |
+
submit_pending_stream_on_pending_end = true;
|
232 |
+
}
|
233 |
+
dispatch_blob(audio_chunks, "stop_recording");
|
234 |
+
dispatch("clear");
|
235 |
+
mode = "";
|
236 |
+
}
|
237 |
+
}
|
238 |
+
|
239 |
+
$: if (!recording && recorder) stop();
|
240 |
+
$: if (recording && recorder) record();
|
241 |
+
</script>
|
242 |
+
|
243 |
+
<BlockLabel
|
244 |
+
{show_label}
|
245 |
+
Icon={Music}
|
246 |
+
float={active_source === "upload" && value === null}
|
247 |
+
label={label || i18n("audio.audio")}
|
248 |
+
/>
|
249 |
+
<div class="audio-container">
|
250 |
+
<StreamingBar {time_limit} />
|
251 |
+
{#if value === null || streaming}
|
252 |
+
{#if active_source === "microphone"}
|
253 |
+
<ModifyUpload {i18n} on:clear={clear} />
|
254 |
+
{#if streaming}
|
255 |
+
<StreamAudio
|
256 |
+
{record}
|
257 |
+
{recording}
|
258 |
+
{stop}
|
259 |
+
{i18n}
|
260 |
+
{waveform_settings}
|
261 |
+
{waveform_options}
|
262 |
+
waiting={stream_state === "waiting"}
|
263 |
+
/>
|
264 |
+
{:else}
|
265 |
+
<AudioRecorder
|
266 |
+
bind:mode
|
267 |
+
{i18n}
|
268 |
+
{editable}
|
269 |
+
{recording}
|
270 |
+
{dispatch_blob}
|
271 |
+
{waveform_settings}
|
272 |
+
{waveform_options}
|
273 |
+
{handle_reset_value}
|
274 |
+
on:start_recording
|
275 |
+
on:pause_recording
|
276 |
+
on:stop_recording
|
277 |
+
/>
|
278 |
+
{/if}
|
279 |
+
{:else if active_source === "upload"}
|
280 |
+
<!-- explicitly listed out audio mimetypes due to iOS bug not recognizing audio/* -->
|
281 |
+
<Upload
|
282 |
+
filetype="audio/aac,audio/midi,audio/mpeg,audio/ogg,audio/wav,audio/x-wav,audio/opus,audio/webm,audio/flac,audio/vnd.rn-realaudio,audio/x-ms-wma,audio/x-aiff,audio/amr,audio/*"
|
283 |
+
on:load={handle_load}
|
284 |
+
bind:dragging
|
285 |
+
bind:uploading
|
286 |
+
on:error={({ detail }) => dispatch("error", detail)}
|
287 |
+
{root}
|
288 |
+
{max_file_size}
|
289 |
+
{upload}
|
290 |
+
{stream_handler}
|
291 |
+
>
|
292 |
+
<slot />
|
293 |
+
</Upload>
|
294 |
+
{/if}
|
295 |
+
{:else}
|
296 |
+
<ModifyUpload
|
297 |
+
{i18n}
|
298 |
+
on:clear={clear}
|
299 |
+
on:edit={() => (mode = "edit")}
|
300 |
+
download={show_download_button ? value.url : null}
|
301 |
+
/>
|
302 |
+
|
303 |
+
<AudioPlayer
|
304 |
+
bind:mode
|
305 |
+
{value}
|
306 |
+
{label}
|
307 |
+
{i18n}
|
308 |
+
{dispatch_blob}
|
309 |
+
{waveform_settings}
|
310 |
+
{waveform_options}
|
311 |
+
{trim_region_settings}
|
312 |
+
{handle_reset_value}
|
313 |
+
{editable}
|
314 |
+
{loop}
|
315 |
+
interactive
|
316 |
+
on:stop
|
317 |
+
on:play
|
318 |
+
on:pause
|
319 |
+
on:edit
|
320 |
+
/>
|
321 |
+
{/if}
|
322 |
+
<SelectSource {sources} bind:active_source handle_clear={clear} />
|
323 |
+
</div>
|
324 |
+
|
325 |
+
<style>
|
326 |
+
.audio-container {
|
327 |
+
height: calc(var(--size-full) - var(--size-6));
|
328 |
+
display: flex;
|
329 |
+
flex-direction: column;
|
330 |
+
justify-content: space-between;
|
331 |
+
}
|
332 |
+
</style>
|
sourceviewer/frontend/package-lock.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
sourceviewer/frontend/package.json
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"name": "gradio_sourceviewer",
|
3 |
+
"version": "0.14.8",
|
4 |
+
"description": "Gradio UI packages",
|
5 |
+
"type": "module",
|
6 |
+
"author": "",
|
7 |
+
"license": "ISC",
|
8 |
+
"private": false,
|
9 |
+
"dependencies": {
|
10 |
+
"@gradio/atoms": "0.11.1",
|
11 |
+
"@gradio/button": "0.3.7",
|
12 |
+
"@gradio/client": "1.8.0",
|
13 |
+
"@gradio/icons": "0.8.1",
|
14 |
+
"@gradio/statustracker": "0.9.5",
|
15 |
+
"@gradio/upload": "0.14.1",
|
16 |
+
"@gradio/utils": "0.8.0",
|
17 |
+
"@gradio/wasm": "0.15.0",
|
18 |
+
"@types/wavesurfer.js": "^6.0.10",
|
19 |
+
"extendable-media-recorder": "^9.0.0",
|
20 |
+
"extendable-media-recorder-wav-encoder": "^7.0.76",
|
21 |
+
"hls.js": "^1.5.13",
|
22 |
+
"resize-observer-polyfill": "^1.5.1",
|
23 |
+
"svelte-range-slider-pips": "^2.0.1",
|
24 |
+
"wavesurfer.js": "^7.4.2"
|
25 |
+
},
|
26 |
+
"devDependencies": {
|
27 |
+
"@gradio/preview": "0.13.0"
|
28 |
+
},
|
29 |
+
"main_changeset": true,
|
30 |
+
"main": "index.ts",
|
31 |
+
"exports": {
|
32 |
+
"./package.json": "./package.json",
|
33 |
+
".": {
|
34 |
+
"gradio": "./index.ts",
|
35 |
+
"svelte": "./dist/index.js",
|
36 |
+
"types": "./dist/index.d.ts"
|
37 |
+
},
|
38 |
+
"./example": {
|
39 |
+
"gradio": "./Example.svelte",
|
40 |
+
"svelte": "./dist/Example.svelte",
|
41 |
+
"types": "./dist/Example.svelte.d.ts"
|
42 |
+
},
|
43 |
+
"./shared": {
|
44 |
+
"gradio": "./shared/index.ts",
|
45 |
+
"svelte": "./dist/shared/index.js",
|
46 |
+
"types": "./dist/shared/index.d.ts"
|
47 |
+
},
|
48 |
+
"./base": {
|
49 |
+
"gradio": "./static/StaticAudio.svelte",
|
50 |
+
"svelte": "./dist/static/StaticAudio.svelte",
|
51 |
+
"types": "./dist/static/StaticAudio.svelte.d.ts"
|
52 |
+
}
|
53 |
+
},
|
54 |
+
"peerDependencies": {
|
55 |
+
"svelte": "^4.0.0"
|
56 |
+
},
|
57 |
+
"repository": {
|
58 |
+
"type": "git",
|
59 |
+
"url": "git+https://github.com/gradio-app/gradio.git",
|
60 |
+
"directory": "js/audio"
|
61 |
+
}
|
62 |
+
}
|
sourceviewer/frontend/player/AudioPlayer.svelte
ADDED
@@ -0,0 +1,314 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<script lang="ts">
|
2 |
+
import { onMount } from "svelte";
|
3 |
+
import { Music } from "@gradio/icons";
|
4 |
+
import { format_time, type I18nFormatter } from "@gradio/utils";
|
5 |
+
import WaveSurfer from "wavesurfer.js";
|
6 |
+
import { skip_audio, process_audio } from "../shared/utils";
|
7 |
+
import WaveformControls from "../shared/WaveformControls.svelte";
|
8 |
+
import { Empty } from "@gradio/atoms";
|
9 |
+
import { resolve_wasm_src } from "@gradio/wasm/svelte";
|
10 |
+
import type { FileData } from "@gradio/client";
|
11 |
+
import type { WaveformOptions } from "../shared/types";
|
12 |
+
import { createEventDispatcher } from "svelte";
|
13 |
+
|
14 |
+
import Hls from "hls.js";
|
15 |
+
|
16 |
+
export let value: null | FileData = null;
|
17 |
+
$: url = value?.url;
|
18 |
+
export let label: string;
|
19 |
+
export let i18n: I18nFormatter;
|
20 |
+
export let dispatch_blob: (
|
21 |
+
blobs: Uint8Array[] | Blob[],
|
22 |
+
event: "stream" | "change" | "stop_recording"
|
23 |
+
) => Promise<void> = () => Promise.resolve();
|
24 |
+
export let interactive = false;
|
25 |
+
export let editable = true;
|
26 |
+
export let trim_region_settings = {};
|
27 |
+
export let waveform_settings: Record<string, any>;
|
28 |
+
export let waveform_options: WaveformOptions;
|
29 |
+
export let mode = "";
|
30 |
+
export let loop: boolean;
|
31 |
+
export let handle_reset_value: () => void = () => {};
|
32 |
+
|
33 |
+
let container: HTMLDivElement;
|
34 |
+
let waveform: WaveSurfer | undefined;
|
35 |
+
let playing = false;
|
36 |
+
|
37 |
+
let timeRef: HTMLTimeElement;
|
38 |
+
let durationRef: HTMLTimeElement;
|
39 |
+
let audio_duration: number;
|
40 |
+
|
41 |
+
let trimDuration = 0;
|
42 |
+
|
43 |
+
let show_volume_slider = false;
|
44 |
+
let audio_player: HTMLAudioElement;
|
45 |
+
|
46 |
+
let stream_active = false;
|
47 |
+
|
48 |
+
const dispatch = createEventDispatcher<{
|
49 |
+
stop: undefined;
|
50 |
+
play: undefined;
|
51 |
+
pause: undefined;
|
52 |
+
edit: undefined;
|
53 |
+
end: undefined;
|
54 |
+
load: undefined;
|
55 |
+
}>();
|
56 |
+
|
57 |
+
const create_waveform = (): void => {
|
58 |
+
waveform = WaveSurfer.create({
|
59 |
+
container: container,
|
60 |
+
...waveform_settings
|
61 |
+
});
|
62 |
+
resolve_wasm_src(value?.url).then((resolved_src) => {
|
63 |
+
if (resolved_src && waveform) {
|
64 |
+
return waveform.load(resolved_src);
|
65 |
+
}
|
66 |
+
});
|
67 |
+
};
|
68 |
+
|
69 |
+
$: if (!value?.is_stream && container !== undefined && container !== null) {
|
70 |
+
if (waveform !== undefined) waveform.destroy();
|
71 |
+
container.innerHTML = "";
|
72 |
+
create_waveform();
|
73 |
+
playing = false;
|
74 |
+
}
|
75 |
+
|
76 |
+
$: waveform?.on("decode", (duration: any) => {
|
77 |
+
audio_duration = duration;
|
78 |
+
durationRef && (durationRef.textContent = format_time(duration));
|
79 |
+
});
|
80 |
+
|
81 |
+
$: waveform?.on(
|
82 |
+
"timeupdate",
|
83 |
+
(currentTime: any) =>
|
84 |
+
timeRef && (timeRef.textContent = format_time(currentTime))
|
85 |
+
);
|
86 |
+
|
87 |
+
$: waveform?.on("ready", () => {
|
88 |
+
if (!waveform_settings.autoplay) {
|
89 |
+
waveform?.stop();
|
90 |
+
} else {
|
91 |
+
waveform?.play();
|
92 |
+
}
|
93 |
+
});
|
94 |
+
|
95 |
+
$: waveform?.on("finish", () => {
|
96 |
+
if (loop) {
|
97 |
+
waveform?.play();
|
98 |
+
} else {
|
99 |
+
playing = false;
|
100 |
+
dispatch("stop");
|
101 |
+
}
|
102 |
+
});
|
103 |
+
$: waveform?.on("pause", () => {
|
104 |
+
playing = false;
|
105 |
+
dispatch("pause");
|
106 |
+
});
|
107 |
+
$: waveform?.on("play", () => {
|
108 |
+
playing = true;
|
109 |
+
dispatch("play");
|
110 |
+
});
|
111 |
+
|
112 |
+
$: waveform?.on("load", () => {
|
113 |
+
dispatch("load");
|
114 |
+
});
|
115 |
+
|
116 |
+
const handle_trim_audio = async (
|
117 |
+
start: number,
|
118 |
+
end: number
|
119 |
+
): Promise<void> => {
|
120 |
+
mode = "";
|
121 |
+
const decodedData = waveform?.getDecodedData();
|
122 |
+
if (decodedData)
|
123 |
+
await process_audio(
|
124 |
+
decodedData,
|
125 |
+
start,
|
126 |
+
end,
|
127 |
+
waveform_settings.sampleRate
|
128 |
+
).then(async (trimmedBlob: Uint8Array) => {
|
129 |
+
await dispatch_blob([trimmedBlob], "change");
|
130 |
+
waveform?.destroy();
|
131 |
+
container.innerHTML = "";
|
132 |
+
});
|
133 |
+
dispatch("edit");
|
134 |
+
};
|
135 |
+
|
136 |
+
async function load_audio(data: string): Promise<void> {
|
137 |
+
stream_active = false;
|
138 |
+
await resolve_wasm_src(data).then((resolved_src) => {
|
139 |
+
if (!resolved_src || value?.is_stream) return;
|
140 |
+
return waveform?.load(resolved_src);
|
141 |
+
});
|
142 |
+
}
|
143 |
+
|
144 |
+
$: url && load_audio(url);
|
145 |
+
|
146 |
+
function load_stream(value: FileData | null): void {
|
147 |
+
if (!value || !value.is_stream || !value.url) return;
|
148 |
+
if (!audio_player) return;
|
149 |
+
if (Hls.isSupported() && !stream_active) {
|
150 |
+
// Set config to start playback after 1 second of data received
|
151 |
+
const hls = new Hls({
|
152 |
+
maxBufferLength: 1,
|
153 |
+
maxMaxBufferLength: 1,
|
154 |
+
lowLatencyMode: true
|
155 |
+
});
|
156 |
+
hls.loadSource(value.url);
|
157 |
+
hls.attachMedia(audio_player);
|
158 |
+
hls.on(Hls.Events.MANIFEST_PARSED, function () {
|
159 |
+
if (waveform_settings.autoplay) audio_player.play();
|
160 |
+
});
|
161 |
+
hls.on(Hls.Events.ERROR, function (event, data) {
|
162 |
+
console.error("HLS error:", event, data);
|
163 |
+
if (data.fatal) {
|
164 |
+
switch (data.type) {
|
165 |
+
case Hls.ErrorTypes.NETWORK_ERROR:
|
166 |
+
console.error(
|
167 |
+
"Fatal network error encountered, trying to recover"
|
168 |
+
);
|
169 |
+
hls.startLoad();
|
170 |
+
break;
|
171 |
+
case Hls.ErrorTypes.MEDIA_ERROR:
|
172 |
+
console.error("Fatal media error encountered, trying to recover");
|
173 |
+
hls.recoverMediaError();
|
174 |
+
break;
|
175 |
+
default:
|
176 |
+
console.error("Fatal error, cannot recover");
|
177 |
+
hls.destroy();
|
178 |
+
break;
|
179 |
+
}
|
180 |
+
}
|
181 |
+
});
|
182 |
+
stream_active = true;
|
183 |
+
} else if (!stream_active) {
|
184 |
+
audio_player.src = value.url;
|
185 |
+
if (waveform_settings.autoplay) audio_player.play();
|
186 |
+
stream_active = true;
|
187 |
+
}
|
188 |
+
}
|
189 |
+
|
190 |
+
$: load_stream(value);
|
191 |
+
|
192 |
+
onMount(() => {
|
193 |
+
window.addEventListener("keydown", (e) => {
|
194 |
+
if (!waveform || show_volume_slider) return;
|
195 |
+
if (e.key === "ArrowRight" && mode !== "edit") {
|
196 |
+
skip_audio(waveform, 0.1);
|
197 |
+
} else if (e.key === "ArrowLeft" && mode !== "edit") {
|
198 |
+
skip_audio(waveform, -0.1);
|
199 |
+
}
|
200 |
+
});
|
201 |
+
});
|
202 |
+
</script>
|
203 |
+
|
204 |
+
<audio
|
205 |
+
class="standard-player"
|
206 |
+
class:hidden={!(value && value.is_stream)}
|
207 |
+
controls
|
208 |
+
autoplay={waveform_settings.autoplay}
|
209 |
+
on:load
|
210 |
+
bind:this={audio_player}
|
211 |
+
on:ended={() => dispatch("stop")}
|
212 |
+
on:play={() => dispatch("play")}
|
213 |
+
/>
|
214 |
+
{#if value === null}
|
215 |
+
<Empty size="small">
|
216 |
+
<Music />
|
217 |
+
</Empty>
|
218 |
+
{:else if !value.is_stream}
|
219 |
+
<div
|
220 |
+
class="component-wrapper"
|
221 |
+
data-testid={label ? "waveform-" + label : "unlabelled-audio"}
|
222 |
+
>
|
223 |
+
<div class="waveform-container">
|
224 |
+
<div
|
225 |
+
id="waveform"
|
226 |
+
bind:this={container}
|
227 |
+
style:height={container ? null : "58px"}
|
228 |
+
/>
|
229 |
+
</div>
|
230 |
+
|
231 |
+
<div class="timestamps">
|
232 |
+
<time bind:this={timeRef} id="time">0:00</time>
|
233 |
+
<div>
|
234 |
+
{#if mode === "edit" && trimDuration > 0}
|
235 |
+
<time id="trim-duration">{format_time(trimDuration)}</time>
|
236 |
+
{/if}
|
237 |
+
<time bind:this={durationRef} id="duration">0:00</time>
|
238 |
+
</div>
|
239 |
+
</div>
|
240 |
+
|
241 |
+
<!-- {#if waveform} -->
|
242 |
+
<WaveformControls
|
243 |
+
{container}
|
244 |
+
{waveform}
|
245 |
+
{playing}
|
246 |
+
{audio_duration}
|
247 |
+
{i18n}
|
248 |
+
{interactive}
|
249 |
+
{handle_trim_audio}
|
250 |
+
bind:mode
|
251 |
+
bind:trimDuration
|
252 |
+
bind:show_volume_slider
|
253 |
+
show_redo={interactive}
|
254 |
+
{handle_reset_value}
|
255 |
+
{waveform_options}
|
256 |
+
{trim_region_settings}
|
257 |
+
{editable}
|
258 |
+
/>
|
259 |
+
<!-- {/if} -->
|
260 |
+
</div>
|
261 |
+
{/if}
|
262 |
+
|
263 |
+
<style>
|
264 |
+
.component-wrapper {
|
265 |
+
padding: var(--size-3);
|
266 |
+
width: 100%;
|
267 |
+
}
|
268 |
+
|
269 |
+
:global(::part(wrapper)) {
|
270 |
+
margin-bottom: var(--size-2);
|
271 |
+
}
|
272 |
+
|
273 |
+
.timestamps {
|
274 |
+
display: flex;
|
275 |
+
justify-content: space-between;
|
276 |
+
align-items: center;
|
277 |
+
width: 100%;
|
278 |
+
padding: var(--size-1) 0;
|
279 |
+
}
|
280 |
+
|
281 |
+
#time {
|
282 |
+
color: var(--neutral-400);
|
283 |
+
}
|
284 |
+
|
285 |
+
#duration {
|
286 |
+
color: var(--neutral-400);
|
287 |
+
}
|
288 |
+
|
289 |
+
#trim-duration {
|
290 |
+
color: var(--color-accent);
|
291 |
+
margin-right: var(--spacing-sm);
|
292 |
+
}
|
293 |
+
.waveform-container {
|
294 |
+
display: flex;
|
295 |
+
align-items: center;
|
296 |
+
justify-content: center;
|
297 |
+
width: var(--size-full);
|
298 |
+
}
|
299 |
+
|
300 |
+
#waveform {
|
301 |
+
width: 100%;
|
302 |
+
height: 100%;
|
303 |
+
position: relative;
|
304 |
+
}
|
305 |
+
|
306 |
+
.standard-player {
|
307 |
+
width: 100%;
|
308 |
+
padding: var(--size-2);
|
309 |
+
}
|
310 |
+
|
311 |
+
.hidden {
|
312 |
+
display: none;
|
313 |
+
}
|
314 |
+
</style>
|
sourceviewer/frontend/recorder/AudioRecorder.svelte
ADDED
@@ -0,0 +1,287 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<script lang="ts">
|
2 |
+
import { onMount } from "svelte";
|
3 |
+
import type { I18nFormatter } from "@gradio/utils";
|
4 |
+
import { createEventDispatcher } from "svelte";
|
5 |
+
import WaveSurfer from "wavesurfer.js";
|
6 |
+
import { skip_audio, process_audio } from "../shared/utils";
|
7 |
+
import WSRecord from "wavesurfer.js/dist/plugins/record.js";
|
8 |
+
import WaveformControls from "../shared/WaveformControls.svelte";
|
9 |
+
import WaveformRecordControls from "../shared/WaveformRecordControls.svelte";
|
10 |
+
import RecordPlugin from "wavesurfer.js/dist/plugins/record.js";
|
11 |
+
import type { WaveformOptions } from "../shared/types";
|
12 |
+
import { format_time } from "@gradio/utils";
|
13 |
+
|
14 |
+
export let mode: string;
|
15 |
+
export let i18n: I18nFormatter;
|
16 |
+
export let dispatch_blob: (
|
17 |
+
blobs: Uint8Array[] | Blob[],
|
18 |
+
event: "stream" | "change" | "stop_recording"
|
19 |
+
) => Promise<void> | undefined;
|
20 |
+
export let waveform_settings: Record<string, any>;
|
21 |
+
export let waveform_options: WaveformOptions = {
|
22 |
+
show_recording_waveform: true
|
23 |
+
};
|
24 |
+
export let handle_reset_value: () => void;
|
25 |
+
export let editable = true;
|
26 |
+
export let recording = false;
|
27 |
+
|
28 |
+
let micWaveform: WaveSurfer;
|
29 |
+
let recordingWaveform: WaveSurfer;
|
30 |
+
let playing = false;
|
31 |
+
|
32 |
+
let recordingContainer: HTMLDivElement;
|
33 |
+
let microphoneContainer: HTMLDivElement;
|
34 |
+
|
35 |
+
let record: WSRecord;
|
36 |
+
let recordedAudio: string | null = null;
|
37 |
+
|
38 |
+
// timestamps
|
39 |
+
let timeRef: HTMLTimeElement;
|
40 |
+
let durationRef: HTMLTimeElement;
|
41 |
+
let audio_duration: number;
|
42 |
+
let seconds = 0;
|
43 |
+
let interval: NodeJS.Timeout;
|
44 |
+
let timing = false;
|
45 |
+
// trimming
|
46 |
+
let trimDuration = 0;
|
47 |
+
|
48 |
+
const start_interval = (): void => {
|
49 |
+
clearInterval(interval);
|
50 |
+
interval = setInterval(() => {
|
51 |
+
seconds++;
|
52 |
+
}, 1000);
|
53 |
+
};
|
54 |
+
|
55 |
+
const dispatch = createEventDispatcher<{
|
56 |
+
start_recording: undefined;
|
57 |
+
pause_recording: undefined;
|
58 |
+
stop_recording: undefined;
|
59 |
+
stop: undefined;
|
60 |
+
play: undefined;
|
61 |
+
pause: undefined;
|
62 |
+
end: undefined;
|
63 |
+
edit: undefined;
|
64 |
+
}>();
|
65 |
+
|
66 |
+
function record_start_callback(): void {
|
67 |
+
start_interval();
|
68 |
+
timing = true;
|
69 |
+
dispatch("start_recording");
|
70 |
+
if (waveform_options.show_recording_waveform) {
|
71 |
+
let waveformCanvas = microphoneContainer;
|
72 |
+
if (waveformCanvas) waveformCanvas.style.display = "block";
|
73 |
+
}
|
74 |
+
}
|
75 |
+
|
76 |
+
async function record_end_callback(blob: Blob): Promise<void> {
|
77 |
+
seconds = 0;
|
78 |
+
timing = false;
|
79 |
+
clearInterval(interval);
|
80 |
+
try {
|
81 |
+
const array_buffer = await blob.arrayBuffer();
|
82 |
+
const context = new AudioContext({
|
83 |
+
sampleRate: waveform_settings.sampleRate
|
84 |
+
});
|
85 |
+
const audio_buffer = await context.decodeAudioData(array_buffer);
|
86 |
+
|
87 |
+
if (audio_buffer)
|
88 |
+
await process_audio(audio_buffer).then(async (audio: Uint8Array) => {
|
89 |
+
await dispatch_blob([audio], "change");
|
90 |
+
await dispatch_blob([audio], "stop_recording");
|
91 |
+
});
|
92 |
+
} catch (e) {
|
93 |
+
console.error(e);
|
94 |
+
}
|
95 |
+
}
|
96 |
+
|
97 |
+
$: record?.on("record-resume", () => {
|
98 |
+
start_interval();
|
99 |
+
});
|
100 |
+
|
101 |
+
$: recordingWaveform?.on("decode", (duration: any) => {
|
102 |
+
audio_duration = duration;
|
103 |
+
durationRef && (durationRef.textContent = format_time(duration));
|
104 |
+
});
|
105 |
+
|
106 |
+
$: recordingWaveform?.on(
|
107 |
+
"timeupdate",
|
108 |
+
(currentTime: any) =>
|
109 |
+
timeRef && (timeRef.textContent = format_time(currentTime))
|
110 |
+
);
|
111 |
+
|
112 |
+
$: recordingWaveform?.on("pause", () => {
|
113 |
+
dispatch("pause");
|
114 |
+
playing = false;
|
115 |
+
});
|
116 |
+
|
117 |
+
$: recordingWaveform?.on("play", () => {
|
118 |
+
dispatch("play");
|
119 |
+
playing = true;
|
120 |
+
});
|
121 |
+
|
122 |
+
$: recordingWaveform?.on("finish", () => {
|
123 |
+
dispatch("stop");
|
124 |
+
playing = false;
|
125 |
+
});
|
126 |
+
|
127 |
+
const create_mic_waveform = (): void => {
|
128 |
+
if (microphoneContainer) microphoneContainer.innerHTML = "";
|
129 |
+
if (micWaveform !== undefined) micWaveform.destroy();
|
130 |
+
if (!microphoneContainer) return;
|
131 |
+
micWaveform = WaveSurfer.create({
|
132 |
+
...waveform_settings,
|
133 |
+
normalize: false,
|
134 |
+
container: microphoneContainer
|
135 |
+
});
|
136 |
+
|
137 |
+
record = micWaveform.registerPlugin(RecordPlugin.create());
|
138 |
+
record.startMic();
|
139 |
+
record?.on("record-end", record_end_callback);
|
140 |
+
record?.on("record-start", record_start_callback);
|
141 |
+
record?.on("record-pause", () => {
|
142 |
+
dispatch("pause_recording");
|
143 |
+
clearInterval(interval);
|
144 |
+
});
|
145 |
+
|
146 |
+
record?.on("record-end", (blob) => {
|
147 |
+
recordedAudio = URL.createObjectURL(blob);
|
148 |
+
|
149 |
+
const microphone = microphoneContainer;
|
150 |
+
const recording = recordingContainer;
|
151 |
+
|
152 |
+
if (microphone) microphone.style.display = "none";
|
153 |
+
if (recording && recordedAudio) {
|
154 |
+
recording.innerHTML = "";
|
155 |
+
create_recording_waveform();
|
156 |
+
}
|
157 |
+
});
|
158 |
+
};
|
159 |
+
|
160 |
+
const create_recording_waveform = (): void => {
|
161 |
+
let recording = recordingContainer;
|
162 |
+
if (!recordedAudio || !recording) return;
|
163 |
+
recordingWaveform = WaveSurfer.create({
|
164 |
+
container: recording,
|
165 |
+
url: recordedAudio,
|
166 |
+
...waveform_settings
|
167 |
+
});
|
168 |
+
};
|
169 |
+
|
170 |
+
const handle_trim_audio = async (
|
171 |
+
start: number,
|
172 |
+
end: number
|
173 |
+
): Promise<void> => {
|
174 |
+
mode = "edit";
|
175 |
+
const decodedData = recordingWaveform.getDecodedData();
|
176 |
+
if (decodedData)
|
177 |
+
await process_audio(decodedData, start, end).then(
|
178 |
+
async (trimmedAudio: Uint8Array) => {
|
179 |
+
await dispatch_blob([trimmedAudio], "change");
|
180 |
+
await dispatch_blob([trimmedAudio], "stop_recording");
|
181 |
+
recordingWaveform.destroy();
|
182 |
+
create_recording_waveform();
|
183 |
+
}
|
184 |
+
);
|
185 |
+
dispatch("edit");
|
186 |
+
};
|
187 |
+
|
188 |
+
onMount(() => {
|
189 |
+
create_mic_waveform();
|
190 |
+
|
191 |
+
window.addEventListener("keydown", (e) => {
|
192 |
+
if (e.key === "ArrowRight") {
|
193 |
+
skip_audio(recordingWaveform, 0.1);
|
194 |
+
} else if (e.key === "ArrowLeft") {
|
195 |
+
skip_audio(recordingWaveform, -0.1);
|
196 |
+
}
|
197 |
+
});
|
198 |
+
});
|
199 |
+
</script>
|
200 |
+
|
201 |
+
<div class="component-wrapper">
|
202 |
+
<div
|
203 |
+
class="microphone"
|
204 |
+
bind:this={microphoneContainer}
|
205 |
+
data-testid="microphone-waveform"
|
206 |
+
/>
|
207 |
+
<div bind:this={recordingContainer} data-testid="recording-waveform" />
|
208 |
+
|
209 |
+
{#if (timing || recordedAudio) && waveform_options.show_recording_waveform}
|
210 |
+
<div class="timestamps">
|
211 |
+
<time bind:this={timeRef} class="time">0:00</time>
|
212 |
+
<div>
|
213 |
+
{#if mode === "edit" && trimDuration > 0}
|
214 |
+
<time class="trim-duration">{format_time(trimDuration)}</time>
|
215 |
+
{/if}
|
216 |
+
{#if timing}
|
217 |
+
<time class="duration">{format_time(seconds)}</time>
|
218 |
+
{:else}
|
219 |
+
<time bind:this={durationRef} class="duration">0:00</time>
|
220 |
+
{/if}
|
221 |
+
</div>
|
222 |
+
</div>
|
223 |
+
{/if}
|
224 |
+
|
225 |
+
{#if microphoneContainer && !recordedAudio}
|
226 |
+
<WaveformRecordControls
|
227 |
+
bind:record
|
228 |
+
{i18n}
|
229 |
+
{timing}
|
230 |
+
{recording}
|
231 |
+
show_recording_waveform={waveform_options.show_recording_waveform}
|
232 |
+
record_time={format_time(seconds)}
|
233 |
+
/>
|
234 |
+
{/if}
|
235 |
+
|
236 |
+
{#if recordingWaveform && recordedAudio}
|
237 |
+
<WaveformControls
|
238 |
+
bind:waveform={recordingWaveform}
|
239 |
+
container={recordingContainer}
|
240 |
+
{playing}
|
241 |
+
{audio_duration}
|
242 |
+
{i18n}
|
243 |
+
{editable}
|
244 |
+
interactive={true}
|
245 |
+
{handle_trim_audio}
|
246 |
+
bind:trimDuration
|
247 |
+
bind:mode
|
248 |
+
show_redo
|
249 |
+
{handle_reset_value}
|
250 |
+
{waveform_options}
|
251 |
+
/>
|
252 |
+
{/if}
|
253 |
+
</div>
|
254 |
+
|
255 |
+
<style>
|
256 |
+
.microphone {
|
257 |
+
width: 100%;
|
258 |
+
display: none;
|
259 |
+
}
|
260 |
+
|
261 |
+
.component-wrapper {
|
262 |
+
padding: var(--size-3);
|
263 |
+
width: 100%;
|
264 |
+
}
|
265 |
+
|
266 |
+
.timestamps {
|
267 |
+
display: flex;
|
268 |
+
justify-content: space-between;
|
269 |
+
align-items: center;
|
270 |
+
width: 100%;
|
271 |
+
padding: var(--size-1) 0;
|
272 |
+
margin: var(--spacing-md) 0;
|
273 |
+
}
|
274 |
+
|
275 |
+
.time {
|
276 |
+
color: var(--neutral-400);
|
277 |
+
}
|
278 |
+
|
279 |
+
.duration {
|
280 |
+
color: var(--neutral-400);
|
281 |
+
}
|
282 |
+
|
283 |
+
.trim-duration {
|
284 |
+
color: var(--color-accent);
|
285 |
+
margin-right: var(--spacing-sm);
|
286 |
+
}
|
287 |
+
</style>
|
sourceviewer/frontend/shared/Audio.svelte
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<script lang="ts">
|
2 |
+
import type { HTMLAudioAttributes } from "svelte/elements";
|
3 |
+
import { createEventDispatcher } from "svelte";
|
4 |
+
interface Props extends HTMLAudioAttributes {
|
5 |
+
"data-testid"?: string;
|
6 |
+
}
|
7 |
+
type $$Props = Props;
|
8 |
+
|
9 |
+
import { resolve_wasm_src } from "@gradio/wasm/svelte";
|
10 |
+
|
11 |
+
export let src: HTMLAudioAttributes["src"] = undefined;
|
12 |
+
|
13 |
+
let resolved_src: typeof src;
|
14 |
+
|
15 |
+
// The `src` prop can be updated before the Promise from `resolve_wasm_src` is resolved.
|
16 |
+
// In such a case, the resolved value for the old `src` has to be discarded,
|
17 |
+
// This variable `latest_src` is used to pick up only the value resolved for the latest `src` prop.
|
18 |
+
let latest_src: typeof src;
|
19 |
+
$: {
|
20 |
+
// In normal (non-Wasm) Gradio, the `<audio>` element should be rendered with the passed `src` props immediately
|
21 |
+
// without waiting for `resolve_wasm_src()` to resolve.
|
22 |
+
// If it waits, a black image is displayed until the async task finishes
|
23 |
+
// and it leads to undesirable flickering.
|
24 |
+
// So set `src` to `resolved_src` here.
|
25 |
+
resolved_src = src;
|
26 |
+
|
27 |
+
latest_src = src;
|
28 |
+
const resolving_src = src;
|
29 |
+
resolve_wasm_src(resolving_src).then((s) => {
|
30 |
+
if (latest_src === resolving_src) {
|
31 |
+
resolved_src = s;
|
32 |
+
}
|
33 |
+
});
|
34 |
+
}
|
35 |
+
|
36 |
+
const dispatch = createEventDispatcher();
|
37 |
+
</script>
|
38 |
+
|
39 |
+
<audio
|
40 |
+
src={resolved_src}
|
41 |
+
{...$$restProps}
|
42 |
+
on:play={dispatch.bind(null, "play")}
|
43 |
+
on:pause={dispatch.bind(null, "pause")}
|
44 |
+
on:ended={dispatch.bind(null, "ended")}
|
45 |
+
/>
|
sourceviewer/frontend/shared/DeviceSelect.svelte
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<script lang="ts">
|
2 |
+
import RecordPlugin from "wavesurfer.js/dist/plugins/record.js";
|
3 |
+
import type { I18nFormatter } from "@gradio/utils";
|
4 |
+
import { createEventDispatcher } from "svelte";
|
5 |
+
|
6 |
+
export let i18n: I18nFormatter;
|
7 |
+
export let micDevices: MediaDeviceInfo[] = [];
|
8 |
+
|
9 |
+
const dispatch = createEventDispatcher<{
|
10 |
+
error: string;
|
11 |
+
}>();
|
12 |
+
|
13 |
+
$: if (typeof window !== "undefined") {
|
14 |
+
try {
|
15 |
+
let tempDevices: MediaDeviceInfo[] = [];
|
16 |
+
RecordPlugin.getAvailableAudioDevices().then(
|
17 |
+
(devices: MediaDeviceInfo[]) => {
|
18 |
+
micDevices = devices;
|
19 |
+
devices.forEach((device) => {
|
20 |
+
if (device.deviceId) {
|
21 |
+
tempDevices.push(device);
|
22 |
+
}
|
23 |
+
});
|
24 |
+
micDevices = tempDevices;
|
25 |
+
}
|
26 |
+
);
|
27 |
+
} catch (err) {
|
28 |
+
if (err instanceof DOMException && err.name == "NotAllowedError") {
|
29 |
+
dispatch("error", i18n("audio.allow_recording_access"));
|
30 |
+
}
|
31 |
+
throw err;
|
32 |
+
}
|
33 |
+
}
|
34 |
+
</script>
|
35 |
+
|
36 |
+
<select
|
37 |
+
class="mic-select"
|
38 |
+
aria-label="Select input device"
|
39 |
+
disabled={micDevices.length === 0}
|
40 |
+
>
|
41 |
+
{#if micDevices.length === 0}
|
42 |
+
<option value="">{i18n("audio.no_microphone")}</option>
|
43 |
+
{:else}
|
44 |
+
{#each micDevices as micDevice}
|
45 |
+
<option value={micDevice.deviceId}>{micDevice.label}</option>
|
46 |
+
{/each}
|
47 |
+
{/if}
|
48 |
+
</select>
|
49 |
+
|
50 |
+
<style>
|
51 |
+
.mic-select {
|
52 |
+
height: var(--size-8);
|
53 |
+
background: var(--block-background-fill);
|
54 |
+
padding: 0px var(--spacing-xxl);
|
55 |
+
border-radius: var(--button-large-radius);
|
56 |
+
font-size: var(--text-md);
|
57 |
+
border: 1px solid var(--block-border-color);
|
58 |
+
gap: var(--size-1);
|
59 |
+
}
|
60 |
+
|
61 |
+
select {
|
62 |
+
text-overflow: ellipsis;
|
63 |
+
max-width: var(--size-40);
|
64 |
+
}
|
65 |
+
|
66 |
+
@media (max-width: 375px) {
|
67 |
+
select {
|
68 |
+
width: 100%;
|
69 |
+
}
|
70 |
+
}
|
71 |
+
</style>
|
sourceviewer/frontend/shared/VolumeControl.svelte
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<script lang="ts">
|
2 |
+
import { onMount } from "svelte";
|
3 |
+
import WaveSurfer from "wavesurfer.js";
|
4 |
+
|
5 |
+
export let currentVolume = 1;
|
6 |
+
export let show_volume_slider = false;
|
7 |
+
export let waveform: WaveSurfer | undefined;
|
8 |
+
|
9 |
+
let volumeElement: HTMLInputElement;
|
10 |
+
|
11 |
+
onMount(() => {
|
12 |
+
adjustSlider();
|
13 |
+
});
|
14 |
+
|
15 |
+
const adjustSlider = (): void => {
|
16 |
+
let slider = volumeElement;
|
17 |
+
if (!slider) return;
|
18 |
+
|
19 |
+
slider.style.background = `linear-gradient(to right, var(--color-accent) ${
|
20 |
+
currentVolume * 100
|
21 |
+
}%, var(--neutral-400) ${currentVolume * 100}%)`;
|
22 |
+
};
|
23 |
+
|
24 |
+
$: currentVolume, adjustSlider();
|
25 |
+
</script>
|
26 |
+
|
27 |
+
<input
|
28 |
+
bind:this={volumeElement}
|
29 |
+
id="volume"
|
30 |
+
class="volume-slider"
|
31 |
+
type="range"
|
32 |
+
min="0"
|
33 |
+
max="1"
|
34 |
+
step="0.01"
|
35 |
+
value={currentVolume}
|
36 |
+
on:focusout={() => (show_volume_slider = false)}
|
37 |
+
on:input={(e) => {
|
38 |
+
if (e.target instanceof HTMLInputElement) {
|
39 |
+
currentVolume = parseFloat(e.target.value);
|
40 |
+
waveform?.setVolume(currentVolume);
|
41 |
+
}
|
42 |
+
}}
|
43 |
+
/>
|
44 |
+
|
45 |
+
<style>
|
46 |
+
.volume-slider {
|
47 |
+
-webkit-appearance: none;
|
48 |
+
appearance: none;
|
49 |
+
width: var(--size-20);
|
50 |
+
accent-color: var(--color-accent);
|
51 |
+
height: 4px;
|
52 |
+
cursor: pointer;
|
53 |
+
outline: none;
|
54 |
+
border-radius: 15px;
|
55 |
+
background-color: var(--neutral-400);
|
56 |
+
}
|
57 |
+
|
58 |
+
input[type="range"]::-webkit-slider-thumb {
|
59 |
+
-webkit-appearance: none;
|
60 |
+
appearance: none;
|
61 |
+
height: 15px;
|
62 |
+
width: 15px;
|
63 |
+
background-color: var(--color-accent);
|
64 |
+
border-radius: 50%;
|
65 |
+
border: none;
|
66 |
+
transition: 0.2s ease-in-out;
|
67 |
+
}
|
68 |
+
|
69 |
+
input[type="range"]::-moz-range-thumb {
|
70 |
+
height: 15px;
|
71 |
+
width: 15px;
|
72 |
+
background-color: var(--color-accent);
|
73 |
+
border-radius: 50%;
|
74 |
+
border: none;
|
75 |
+
transition: 0.2s ease-in-out;
|
76 |
+
}
|
77 |
+
</style>
|
sourceviewer/frontend/shared/VolumeLevels.svelte
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<script lang="ts">
|
2 |
+
import { VolumeMuted, VolumeHigh, VolumeLow } from "@gradio/icons";
|
3 |
+
export let currentVolume: number;
|
4 |
+
</script>
|
5 |
+
|
6 |
+
{#if currentVolume == 0}
|
7 |
+
<VolumeMuted />
|
8 |
+
{:else if currentVolume < 0.5}
|
9 |
+
<VolumeLow />
|
10 |
+
{:else if currentVolume >= 0.5}
|
11 |
+
<VolumeHigh />
|
12 |
+
{/if}
|
sourceviewer/frontend/shared/WaveformControls.svelte
ADDED
@@ -0,0 +1,406 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<script lang="ts">
|
2 |
+
import { Play, Pause, Forward, Backward, Undo, Trim } from "@gradio/icons";
|
3 |
+
import { get_skip_rewind_amount } from "../shared/utils";
|
4 |
+
import type { I18nFormatter } from "@gradio/utils";
|
5 |
+
import WaveSurfer from "wavesurfer.js";
|
6 |
+
import RegionsPlugin, {
|
7 |
+
type Region
|
8 |
+
} from "wavesurfer.js/dist/plugins/regions.js";
|
9 |
+
import type { WaveformOptions } from "./types";
|
10 |
+
import VolumeLevels from "./VolumeLevels.svelte";
|
11 |
+
import VolumeControl from "./VolumeControl.svelte";
|
12 |
+
|
13 |
+
export let waveform: WaveSurfer | undefined;
|
14 |
+
export let audio_duration: number;
|
15 |
+
export let i18n: I18nFormatter;
|
16 |
+
export let playing: boolean;
|
17 |
+
export let show_redo = false;
|
18 |
+
export let interactive = false;
|
19 |
+
export let handle_trim_audio: (start: number, end: number) => void;
|
20 |
+
export let mode = "";
|
21 |
+
export let container: HTMLDivElement;
|
22 |
+
export let handle_reset_value: () => void;
|
23 |
+
export let waveform_options: WaveformOptions = {};
|
24 |
+
export let trim_region_settings: WaveformOptions = {};
|
25 |
+
export let show_volume_slider = false;
|
26 |
+
export let editable = true;
|
27 |
+
|
28 |
+
export let trimDuration = 0;
|
29 |
+
|
30 |
+
let playbackSpeeds = [0.5, 1, 1.5, 2];
|
31 |
+
let playbackSpeed = playbackSpeeds[1];
|
32 |
+
|
33 |
+
let trimRegion: RegionsPlugin | null = null;
|
34 |
+
let activeRegion: Region | null = null;
|
35 |
+
|
36 |
+
let leftRegionHandle: HTMLDivElement | null;
|
37 |
+
let rightRegionHandle: HTMLDivElement | null;
|
38 |
+
let activeHandle = "";
|
39 |
+
|
40 |
+
let currentVolume = 1;
|
41 |
+
|
42 |
+
$: trimRegion =
|
43 |
+
container && waveform
|
44 |
+
? waveform.registerPlugin(RegionsPlugin.create())
|
45 |
+
: null;
|
46 |
+
|
47 |
+
$: trimRegion?.on("region-out", (region) => {
|
48 |
+
region.play();
|
49 |
+
});
|
50 |
+
|
51 |
+
$: trimRegion?.on("region-updated", (region) => {
|
52 |
+
trimDuration = region.end - region.start;
|
53 |
+
});
|
54 |
+
|
55 |
+
$: trimRegion?.on("region-clicked", (region, e) => {
|
56 |
+
e.stopPropagation(); // prevent triggering a click on the waveform
|
57 |
+
activeRegion = region;
|
58 |
+
region.play();
|
59 |
+
});
|
60 |
+
|
61 |
+
const addTrimRegion = (): void => {
|
62 |
+
if (!trimRegion) return;
|
63 |
+
activeRegion = trimRegion?.addRegion({
|
64 |
+
start: audio_duration / 4,
|
65 |
+
end: audio_duration / 2,
|
66 |
+
...trim_region_settings
|
67 |
+
});
|
68 |
+
|
69 |
+
trimDuration = activeRegion.end - activeRegion.start;
|
70 |
+
};
|
71 |
+
|
72 |
+
$: if (activeRegion) {
|
73 |
+
const shadowRoot = container.children[0]!.shadowRoot!;
|
74 |
+
|
75 |
+
rightRegionHandle = shadowRoot.querySelector('[data-resize="right"]');
|
76 |
+
leftRegionHandle = shadowRoot.querySelector('[data-resize="left"]');
|
77 |
+
|
78 |
+
if (leftRegionHandle && rightRegionHandle) {
|
79 |
+
leftRegionHandle.setAttribute("role", "button");
|
80 |
+
rightRegionHandle.setAttribute("role", "button");
|
81 |
+
leftRegionHandle?.setAttribute("aria-label", "Drag to adjust start time");
|
82 |
+
rightRegionHandle?.setAttribute("aria-label", "Drag to adjust end time");
|
83 |
+
leftRegionHandle?.setAttribute("tabindex", "0");
|
84 |
+
rightRegionHandle?.setAttribute("tabindex", "0");
|
85 |
+
|
86 |
+
leftRegionHandle.addEventListener("focus", () => {
|
87 |
+
if (trimRegion) activeHandle = "left";
|
88 |
+
});
|
89 |
+
|
90 |
+
rightRegionHandle.addEventListener("focus", () => {
|
91 |
+
if (trimRegion) activeHandle = "right";
|
92 |
+
});
|
93 |
+
}
|
94 |
+
}
|
95 |
+
|
96 |
+
const trimAudio = (): void => {
|
97 |
+
if (waveform && trimRegion) {
|
98 |
+
if (activeRegion) {
|
99 |
+
const start = activeRegion.start;
|
100 |
+
const end = activeRegion.end;
|
101 |
+
handle_trim_audio(start, end);
|
102 |
+
mode = "";
|
103 |
+
activeRegion = null;
|
104 |
+
}
|
105 |
+
}
|
106 |
+
};
|
107 |
+
|
108 |
+
const clearRegions = (): void => {
|
109 |
+
trimRegion?.getRegions().forEach((region) => {
|
110 |
+
region.remove();
|
111 |
+
});
|
112 |
+
trimRegion?.clearRegions();
|
113 |
+
};
|
114 |
+
|
115 |
+
const toggleTrimmingMode = (): void => {
|
116 |
+
clearRegions();
|
117 |
+
if (mode === "edit") {
|
118 |
+
mode = "";
|
119 |
+
} else {
|
120 |
+
mode = "edit";
|
121 |
+
addTrimRegion();
|
122 |
+
}
|
123 |
+
};
|
124 |
+
|
125 |
+
const adjustRegionHandles = (handle: string, key: string): void => {
|
126 |
+
let newStart;
|
127 |
+
let newEnd;
|
128 |
+
|
129 |
+
if (!activeRegion) return;
|
130 |
+
if (handle === "left") {
|
131 |
+
if (key === "ArrowLeft") {
|
132 |
+
newStart = activeRegion.start - 0.05;
|
133 |
+
newEnd = activeRegion.end;
|
134 |
+
} else {
|
135 |
+
newStart = activeRegion.start + 0.05;
|
136 |
+
newEnd = activeRegion.end;
|
137 |
+
}
|
138 |
+
} else {
|
139 |
+
if (key === "ArrowLeft") {
|
140 |
+
newStart = activeRegion.start;
|
141 |
+
newEnd = activeRegion.end - 0.05;
|
142 |
+
} else {
|
143 |
+
newStart = activeRegion.start;
|
144 |
+
newEnd = activeRegion.end + 0.05;
|
145 |
+
}
|
146 |
+
}
|
147 |
+
|
148 |
+
activeRegion.setOptions({
|
149 |
+
start: newStart,
|
150 |
+
end: newEnd
|
151 |
+
});
|
152 |
+
|
153 |
+
trimDuration = activeRegion.end - activeRegion.start;
|
154 |
+
};
|
155 |
+
|
156 |
+
$: trimRegion &&
|
157 |
+
window.addEventListener("keydown", (e) => {
|
158 |
+
if (e.key === "ArrowLeft") {
|
159 |
+
adjustRegionHandles(activeHandle, "ArrowLeft");
|
160 |
+
} else if (e.key === "ArrowRight") {
|
161 |
+
adjustRegionHandles(activeHandle, "ArrowRight");
|
162 |
+
}
|
163 |
+
});
|
164 |
+
</script>
|
165 |
+
|
166 |
+
<div class="controls" data-testid="waveform-controls">
|
167 |
+
<div class="control-wrapper">
|
168 |
+
<button
|
169 |
+
class="action icon volume"
|
170 |
+
style:color={show_volume_slider
|
171 |
+
? "var(--color-accent)"
|
172 |
+
: "var(--neutral-400)"}
|
173 |
+
aria-label="Adjust volume"
|
174 |
+
on:click={() => (show_volume_slider = !show_volume_slider)}
|
175 |
+
>
|
176 |
+
<VolumeLevels {currentVolume} />
|
177 |
+
</button>
|
178 |
+
|
179 |
+
{#if show_volume_slider}
|
180 |
+
<VolumeControl bind:currentVolume bind:show_volume_slider {waveform} />
|
181 |
+
{/if}
|
182 |
+
|
183 |
+
<button
|
184 |
+
class:hidden={show_volume_slider}
|
185 |
+
class="playback icon"
|
186 |
+
aria-label={`Adjust playback speed to ${
|
187 |
+
playbackSpeeds[
|
188 |
+
(playbackSpeeds.indexOf(playbackSpeed) + 1) % playbackSpeeds.length
|
189 |
+
]
|
190 |
+
}x`}
|
191 |
+
on:click={() => {
|
192 |
+
playbackSpeed =
|
193 |
+
playbackSpeeds[
|
194 |
+
(playbackSpeeds.indexOf(playbackSpeed) + 1) % playbackSpeeds.length
|
195 |
+
];
|
196 |
+
|
197 |
+
waveform?.setPlaybackRate(playbackSpeed);
|
198 |
+
}}
|
199 |
+
>
|
200 |
+
<span>{playbackSpeed}x</span>
|
201 |
+
</button>
|
202 |
+
</div>
|
203 |
+
|
204 |
+
<div class="play-pause-wrapper">
|
205 |
+
<button
|
206 |
+
class="rewind icon"
|
207 |
+
aria-label={`Skip backwards by ${get_skip_rewind_amount(
|
208 |
+
audio_duration,
|
209 |
+
waveform_options.skip_length
|
210 |
+
)} seconds`}
|
211 |
+
on:click={() =>
|
212 |
+
waveform?.skip(
|
213 |
+
get_skip_rewind_amount(audio_duration, waveform_options.skip_length) *
|
214 |
+
-1
|
215 |
+
)}
|
216 |
+
>
|
217 |
+
<Backward />
|
218 |
+
</button>
|
219 |
+
<button
|
220 |
+
class="play-pause-button icon"
|
221 |
+
on:click={() => waveform?.playPause()}
|
222 |
+
aria-label={playing ? i18n("audio.pause") : i18n("audio.play")}
|
223 |
+
>
|
224 |
+
{#if playing}
|
225 |
+
<Pause />
|
226 |
+
{:else}
|
227 |
+
<Play />
|
228 |
+
{/if}
|
229 |
+
</button>
|
230 |
+
<button
|
231 |
+
class="skip icon"
|
232 |
+
aria-label="Skip forward by {get_skip_rewind_amount(
|
233 |
+
audio_duration,
|
234 |
+
waveform_options.skip_length
|
235 |
+
)} seconds"
|
236 |
+
on:click={() =>
|
237 |
+
waveform?.skip(
|
238 |
+
get_skip_rewind_amount(audio_duration, waveform_options.skip_length)
|
239 |
+
)}
|
240 |
+
>
|
241 |
+
<Forward />
|
242 |
+
</button>
|
243 |
+
</div>
|
244 |
+
|
245 |
+
<div class="settings-wrapper">
|
246 |
+
{#if editable && interactive}
|
247 |
+
{#if show_redo && mode === ""}
|
248 |
+
<button
|
249 |
+
class="action icon"
|
250 |
+
aria-label="Reset audio"
|
251 |
+
on:click={() => {
|
252 |
+
handle_reset_value();
|
253 |
+
clearRegions();
|
254 |
+
mode = "";
|
255 |
+
}}
|
256 |
+
>
|
257 |
+
<Undo />
|
258 |
+
</button>
|
259 |
+
{/if}
|
260 |
+
|
261 |
+
{#if mode === ""}
|
262 |
+
<button
|
263 |
+
class="action icon"
|
264 |
+
aria-label="Trim audio to selection"
|
265 |
+
on:click={toggleTrimmingMode}
|
266 |
+
>
|
267 |
+
<Trim />
|
268 |
+
</button>
|
269 |
+
{:else}
|
270 |
+
<button class="text-button" on:click={trimAudio}>Trim</button>
|
271 |
+
<button class="text-button" on:click={toggleTrimmingMode}>Cancel</button
|
272 |
+
>
|
273 |
+
{/if}
|
274 |
+
{/if}
|
275 |
+
</div>
|
276 |
+
</div>
|
277 |
+
|
278 |
+
<style>
|
279 |
+
.settings-wrapper {
|
280 |
+
display: flex;
|
281 |
+
justify-self: self-end;
|
282 |
+
align-items: center;
|
283 |
+
grid-area: editing;
|
284 |
+
}
|
285 |
+
.text-button {
|
286 |
+
border: 1px solid var(--neutral-400);
|
287 |
+
border-radius: var(--radius-sm);
|
288 |
+
font-weight: 300;
|
289 |
+
font-size: var(--size-3);
|
290 |
+
text-align: center;
|
291 |
+
color: var(--neutral-400);
|
292 |
+
height: var(--size-5);
|
293 |
+
font-weight: bold;
|
294 |
+
padding: 0 5px;
|
295 |
+
margin-left: 5px;
|
296 |
+
}
|
297 |
+
|
298 |
+
.text-button:hover,
|
299 |
+
.text-button:focus {
|
300 |
+
color: var(--color-accent);
|
301 |
+
border-color: var(--color-accent);
|
302 |
+
}
|
303 |
+
|
304 |
+
.controls {
|
305 |
+
display: grid;
|
306 |
+
grid-template-columns: 1fr 1fr 1fr;
|
307 |
+
grid-template-areas: "controls playback editing";
|
308 |
+
margin-top: 5px;
|
309 |
+
align-items: center;
|
310 |
+
position: relative;
|
311 |
+
flex-wrap: wrap;
|
312 |
+
justify-content: space-between;
|
313 |
+
}
|
314 |
+
.controls div {
|
315 |
+
margin: var(--size-1) 0;
|
316 |
+
}
|
317 |
+
|
318 |
+
@media (max-width: 600px) {
|
319 |
+
.controls {
|
320 |
+
grid-template-columns: 1fr 1fr;
|
321 |
+
grid-template-rows: auto auto;
|
322 |
+
grid-template-areas:
|
323 |
+
"playback playback"
|
324 |
+
"controls editing";
|
325 |
+
}
|
326 |
+
}
|
327 |
+
|
328 |
+
@media (max-width: 319px) {
|
329 |
+
.controls {
|
330 |
+
overflow-x: scroll;
|
331 |
+
}
|
332 |
+
}
|
333 |
+
|
334 |
+
.hidden {
|
335 |
+
display: none;
|
336 |
+
}
|
337 |
+
|
338 |
+
.control-wrapper {
|
339 |
+
display: flex;
|
340 |
+
justify-self: self-start;
|
341 |
+
align-items: center;
|
342 |
+
justify-content: space-between;
|
343 |
+
grid-area: controls;
|
344 |
+
}
|
345 |
+
|
346 |
+
.action {
|
347 |
+
width: var(--size-5);
|
348 |
+
color: var(--neutral-400);
|
349 |
+
margin-left: var(--spacing-md);
|
350 |
+
}
|
351 |
+
.icon:hover,
|
352 |
+
.icon:focus {
|
353 |
+
color: var(--color-accent);
|
354 |
+
}
|
355 |
+
.play-pause-wrapper {
|
356 |
+
display: flex;
|
357 |
+
justify-self: center;
|
358 |
+
grid-area: playback;
|
359 |
+
}
|
360 |
+
|
361 |
+
@media (max-width: 600px) {
|
362 |
+
.play-pause-wrapper {
|
363 |
+
margin: var(--spacing-md);
|
364 |
+
}
|
365 |
+
}
|
366 |
+
.playback {
|
367 |
+
border: 1px solid var(--neutral-400);
|
368 |
+
border-radius: var(--radius-sm);
|
369 |
+
width: 5.5ch;
|
370 |
+
font-weight: 300;
|
371 |
+
font-size: var(--size-3);
|
372 |
+
text-align: center;
|
373 |
+
color: var(--neutral-400);
|
374 |
+
height: var(--size-5);
|
375 |
+
font-weight: bold;
|
376 |
+
}
|
377 |
+
|
378 |
+
.playback:hover,
|
379 |
+
.playback:focus {
|
380 |
+
color: var(--color-accent);
|
381 |
+
border-color: var(--color-accent);
|
382 |
+
}
|
383 |
+
|
384 |
+
.rewind,
|
385 |
+
.skip {
|
386 |
+
margin: 0 10px;
|
387 |
+
color: var(--neutral-400);
|
388 |
+
}
|
389 |
+
|
390 |
+
.play-pause-button {
|
391 |
+
width: var(--size-8);
|
392 |
+
display: flex;
|
393 |
+
align-items: center;
|
394 |
+
justify-content: center;
|
395 |
+
color: var(--neutral-400);
|
396 |
+
fill: var(--neutral-400);
|
397 |
+
}
|
398 |
+
|
399 |
+
.volume {
|
400 |
+
position: relative;
|
401 |
+
display: flex;
|
402 |
+
justify-content: center;
|
403 |
+
margin-right: var(--spacing-xl);
|
404 |
+
width: var(--size-5);
|
405 |
+
}
|
406 |
+
</style>
|
sourceviewer/frontend/shared/WaveformRecordControls.svelte
ADDED
@@ -0,0 +1,279 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<script lang="ts">
|
2 |
+
import { Pause } from "@gradio/icons";
|
3 |
+
import type { I18nFormatter } from "@gradio/utils";
|
4 |
+
import RecordPlugin from "wavesurfer.js/dist/plugins/record.js";
|
5 |
+
import DeviceSelect from "./DeviceSelect.svelte";
|
6 |
+
|
7 |
+
export let record: RecordPlugin;
|
8 |
+
export let i18n: I18nFormatter;
|
9 |
+
export let recording = false;
|
10 |
+
|
11 |
+
let micDevices: MediaDeviceInfo[] = [];
|
12 |
+
let recordButton: HTMLButtonElement;
|
13 |
+
let pauseButton: HTMLButtonElement;
|
14 |
+
let resumeButton: HTMLButtonElement;
|
15 |
+
let stopButton: HTMLButtonElement;
|
16 |
+
let stopButtonPaused: HTMLButtonElement;
|
17 |
+
let recording_ongoing = false;
|
18 |
+
|
19 |
+
export let record_time: string;
|
20 |
+
export let show_recording_waveform: boolean | undefined;
|
21 |
+
export let timing = false;
|
22 |
+
|
23 |
+
$: record.on("record-start", () => {
|
24 |
+
record.startMic();
|
25 |
+
|
26 |
+
recordButton.style.display = "none";
|
27 |
+
stopButton.style.display = "flex";
|
28 |
+
pauseButton.style.display = "block";
|
29 |
+
});
|
30 |
+
|
31 |
+
$: record.on("record-end", () => {
|
32 |
+
if (record.isPaused()) {
|
33 |
+
record.resumeRecording();
|
34 |
+
record.stopRecording();
|
35 |
+
}
|
36 |
+
record.stopMic();
|
37 |
+
|
38 |
+
recordButton.style.display = "flex";
|
39 |
+
stopButton.style.display = "none";
|
40 |
+
pauseButton.style.display = "none";
|
41 |
+
recordButton.disabled = false;
|
42 |
+
});
|
43 |
+
|
44 |
+
$: record.on("record-pause", () => {
|
45 |
+
pauseButton.style.display = "none";
|
46 |
+
resumeButton.style.display = "block";
|
47 |
+
stopButton.style.display = "none";
|
48 |
+
stopButtonPaused.style.display = "flex";
|
49 |
+
});
|
50 |
+
|
51 |
+
$: record.on("record-resume", () => {
|
52 |
+
pauseButton.style.display = "block";
|
53 |
+
resumeButton.style.display = "none";
|
54 |
+
recordButton.style.display = "none";
|
55 |
+
stopButton.style.display = "flex";
|
56 |
+
stopButtonPaused.style.display = "none";
|
57 |
+
});
|
58 |
+
|
59 |
+
$: if (recording && !recording_ongoing) {
|
60 |
+
record.startRecording();
|
61 |
+
recording_ongoing = true;
|
62 |
+
} else {
|
63 |
+
record.stopRecording();
|
64 |
+
recording_ongoing = false;
|
65 |
+
}
|
66 |
+
</script>
|
67 |
+
|
68 |
+
<div class="controls">
|
69 |
+
<div class="wrapper">
|
70 |
+
<button
|
71 |
+
bind:this={recordButton}
|
72 |
+
class="record record-button"
|
73 |
+
on:click={() => record.startRecording()}>{i18n("audio.record")}</button
|
74 |
+
>
|
75 |
+
|
76 |
+
<button
|
77 |
+
bind:this={stopButton}
|
78 |
+
class="stop-button {record.isPaused() ? 'stop-button-paused' : ''}"
|
79 |
+
on:click={() => {
|
80 |
+
if (record.isPaused()) {
|
81 |
+
record.resumeRecording();
|
82 |
+
record.stopRecording();
|
83 |
+
}
|
84 |
+
|
85 |
+
record.stopRecording();
|
86 |
+
}}>{i18n("audio.stop")}</button
|
87 |
+
>
|
88 |
+
|
89 |
+
<button
|
90 |
+
bind:this={stopButtonPaused}
|
91 |
+
id="stop-paused"
|
92 |
+
class="stop-button-paused"
|
93 |
+
on:click={() => {
|
94 |
+
if (record.isPaused()) {
|
95 |
+
record.resumeRecording();
|
96 |
+
record.stopRecording();
|
97 |
+
}
|
98 |
+
|
99 |
+
record.stopRecording();
|
100 |
+
}}>{i18n("audio.stop")}</button
|
101 |
+
>
|
102 |
+
|
103 |
+
<button
|
104 |
+
aria-label="pause"
|
105 |
+
bind:this={pauseButton}
|
106 |
+
class="pause-button"
|
107 |
+
on:click={() => record.pauseRecording()}><Pause /></button
|
108 |
+
>
|
109 |
+
<button
|
110 |
+
bind:this={resumeButton}
|
111 |
+
class="resume-button"
|
112 |
+
on:click={() => record.resumeRecording()}>{i18n("audio.resume")}</button
|
113 |
+
>
|
114 |
+
{#if timing && !show_recording_waveform}
|
115 |
+
<time class="duration-button duration">{record_time}</time>
|
116 |
+
{/if}
|
117 |
+
</div>
|
118 |
+
<DeviceSelect bind:micDevices {i18n} />
|
119 |
+
</div>
|
120 |
+
|
121 |
+
<style>
|
122 |
+
.controls {
|
123 |
+
display: flex;
|
124 |
+
align-items: center;
|
125 |
+
justify-content: space-between;
|
126 |
+
flex-wrap: wrap;
|
127 |
+
}
|
128 |
+
|
129 |
+
.wrapper {
|
130 |
+
display: flex;
|
131 |
+
align-items: center;
|
132 |
+
flex-wrap: wrap;
|
133 |
+
}
|
134 |
+
|
135 |
+
.record {
|
136 |
+
margin-right: var(--spacing-md);
|
137 |
+
}
|
138 |
+
|
139 |
+
.stop-button-paused {
|
140 |
+
display: none;
|
141 |
+
height: var(--size-8);
|
142 |
+
width: var(--size-20);
|
143 |
+
background-color: var(--block-background-fill);
|
144 |
+
border-radius: var(--button-large-radius);
|
145 |
+
align-items: center;
|
146 |
+
border: 1px solid var(--block-border-color);
|
147 |
+
margin: var(--size-1) var(--size-1) 0 0;
|
148 |
+
}
|
149 |
+
|
150 |
+
.stop-button-paused::before {
|
151 |
+
content: "";
|
152 |
+
height: var(--size-4);
|
153 |
+
width: var(--size-4);
|
154 |
+
border-radius: var(--radius-full);
|
155 |
+
background: var(--primary-600);
|
156 |
+
margin: 0 var(--spacing-xl);
|
157 |
+
}
|
158 |
+
.stop-button::before {
|
159 |
+
content: "";
|
160 |
+
height: var(--size-4);
|
161 |
+
width: var(--size-4);
|
162 |
+
border-radius: var(--radius-full);
|
163 |
+
background: var(--primary-600);
|
164 |
+
margin: 0 var(--spacing-xl);
|
165 |
+
animation: scaling 1800ms infinite;
|
166 |
+
}
|
167 |
+
|
168 |
+
.stop-button {
|
169 |
+
display: none;
|
170 |
+
height: var(--size-8);
|
171 |
+
width: var(--size-20);
|
172 |
+
background-color: var(--block-background-fill);
|
173 |
+
border-radius: var(--button-large-radius);
|
174 |
+
align-items: center;
|
175 |
+
border: 1px solid var(--primary-600);
|
176 |
+
margin: var(--size-1) var(--size-1) 0 0;
|
177 |
+
}
|
178 |
+
|
179 |
+
.record-button::before {
|
180 |
+
content: "";
|
181 |
+
height: var(--size-4);
|
182 |
+
width: var(--size-4);
|
183 |
+
border-radius: var(--radius-full);
|
184 |
+
background: var(--primary-600);
|
185 |
+
margin: 0 var(--spacing-xl);
|
186 |
+
}
|
187 |
+
|
188 |
+
.record-button {
|
189 |
+
height: var(--size-8);
|
190 |
+
width: var(--size-24);
|
191 |
+
background-color: var(--block-background-fill);
|
192 |
+
border-radius: var(--button-large-radius);
|
193 |
+
display: flex;
|
194 |
+
align-items: center;
|
195 |
+
border: 1px solid var(--block-border-color);
|
196 |
+
}
|
197 |
+
|
198 |
+
.stop-button:disabled {
|
199 |
+
cursor: not-allowed;
|
200 |
+
}
|
201 |
+
|
202 |
+
.record-button:disabled {
|
203 |
+
cursor: not-allowed;
|
204 |
+
opacity: 0.5;
|
205 |
+
}
|
206 |
+
|
207 |
+
@keyframes scaling {
|
208 |
+
0% {
|
209 |
+
background-color: var(--primary-600);
|
210 |
+
scale: 1;
|
211 |
+
}
|
212 |
+
50% {
|
213 |
+
background-color: var(--primary-600);
|
214 |
+
scale: 1.2;
|
215 |
+
}
|
216 |
+
100% {
|
217 |
+
background-color: var(--primary-600);
|
218 |
+
scale: 1;
|
219 |
+
}
|
220 |
+
}
|
221 |
+
|
222 |
+
.pause-button {
|
223 |
+
display: none;
|
224 |
+
height: var(--size-8);
|
225 |
+
width: var(--size-20);
|
226 |
+
border: 1px solid var(--block-border-color);
|
227 |
+
border-radius: var(--button-large-radius);
|
228 |
+
padding: var(--spacing-md);
|
229 |
+
margin: var(--size-1) var(--size-1) 0 0;
|
230 |
+
}
|
231 |
+
|
232 |
+
.resume-button {
|
233 |
+
display: none;
|
234 |
+
height: var(--size-8);
|
235 |
+
width: var(--size-20);
|
236 |
+
border: 1px solid var(--block-border-color);
|
237 |
+
border-radius: var(--button-large-radius);
|
238 |
+
padding: var(--spacing-xl);
|
239 |
+
line-height: 1px;
|
240 |
+
font-size: var(--text-md);
|
241 |
+
margin: var(--size-1) var(--size-1) 0 0;
|
242 |
+
}
|
243 |
+
|
244 |
+
.duration {
|
245 |
+
display: flex;
|
246 |
+
height: var(--size-8);
|
247 |
+
width: var(--size-20);
|
248 |
+
border: 1px solid var(--block-border-color);
|
249 |
+
padding: var(--spacing-md);
|
250 |
+
align-items: center;
|
251 |
+
justify-content: center;
|
252 |
+
margin: var(--size-1) var(--size-1) 0 0;
|
253 |
+
}
|
254 |
+
|
255 |
+
:global(::part(region)) {
|
256 |
+
border-radius: var(--radius-md);
|
257 |
+
height: 98% !important;
|
258 |
+
border: 1px solid var(--trim-region-color);
|
259 |
+
background-color: unset;
|
260 |
+
border-width: 1px 3px;
|
261 |
+
}
|
262 |
+
|
263 |
+
:global(::part(region))::after {
|
264 |
+
content: "";
|
265 |
+
position: absolute;
|
266 |
+
top: 0;
|
267 |
+
left: 0;
|
268 |
+
width: 100%;
|
269 |
+
height: 100%;
|
270 |
+
background: var(--trim-region-color);
|
271 |
+
opacity: 0.2;
|
272 |
+
border-radius: var(--radius-md);
|
273 |
+
}
|
274 |
+
|
275 |
+
:global(::part(region-handle)) {
|
276 |
+
width: 5px !important;
|
277 |
+
border: none;
|
278 |
+
}
|
279 |
+
</style>
|
sourceviewer/frontend/shared/audioBufferToWav.ts
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
export function audioBufferToWav(audioBuffer: AudioBuffer): Uint8Array {
|
2 |
+
const numOfChan = audioBuffer.numberOfChannels;
|
3 |
+
const length = audioBuffer.length * numOfChan * 2 + 44;
|
4 |
+
const buffer = new ArrayBuffer(length);
|
5 |
+
const view = new DataView(buffer);
|
6 |
+
let offset = 0;
|
7 |
+
|
8 |
+
// Write WAV header
|
9 |
+
const writeString = function (
|
10 |
+
view: DataView,
|
11 |
+
offset: number,
|
12 |
+
string: string
|
13 |
+
): void {
|
14 |
+
for (let i = 0; i < string.length; i++) {
|
15 |
+
view.setUint8(offset + i, string.charCodeAt(i));
|
16 |
+
}
|
17 |
+
};
|
18 |
+
|
19 |
+
writeString(view, offset, "RIFF");
|
20 |
+
offset += 4;
|
21 |
+
view.setUint32(offset, length - 8, true);
|
22 |
+
offset += 4;
|
23 |
+
writeString(view, offset, "WAVE");
|
24 |
+
offset += 4;
|
25 |
+
writeString(view, offset, "fmt ");
|
26 |
+
offset += 4;
|
27 |
+
view.setUint32(offset, 16, true);
|
28 |
+
offset += 4; // Sub-chunk size, 16 for PCM
|
29 |
+
view.setUint16(offset, 1, true);
|
30 |
+
offset += 2; // PCM format
|
31 |
+
view.setUint16(offset, numOfChan, true);
|
32 |
+
offset += 2;
|
33 |
+
view.setUint32(offset, audioBuffer.sampleRate, true);
|
34 |
+
offset += 4;
|
35 |
+
view.setUint32(offset, audioBuffer.sampleRate * 2 * numOfChan, true);
|
36 |
+
offset += 4;
|
37 |
+
view.setUint16(offset, numOfChan * 2, true);
|
38 |
+
offset += 2;
|
39 |
+
view.setUint16(offset, 16, true);
|
40 |
+
offset += 2;
|
41 |
+
writeString(view, offset, "data");
|
42 |
+
offset += 4;
|
43 |
+
view.setUint32(offset, audioBuffer.length * numOfChan * 2, true);
|
44 |
+
offset += 4;
|
45 |
+
|
46 |
+
// Write PCM audio data
|
47 |
+
for (let i = 0; i < audioBuffer.length; i++) {
|
48 |
+
for (let channel = 0; channel < numOfChan; channel++) {
|
49 |
+
const sample = Math.max(
|
50 |
+
-1,
|
51 |
+
Math.min(1, audioBuffer.getChannelData(channel)[i])
|
52 |
+
);
|
53 |
+
view.setInt16(offset, sample * 0x7fff, true);
|
54 |
+
offset += 2;
|
55 |
+
}
|
56 |
+
}
|
57 |
+
|
58 |
+
return new Uint8Array(buffer);
|
59 |
+
}
|
sourceviewer/frontend/shared/index.ts
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
export { default as Audio } from "./Audio.svelte";
|
sourceviewer/frontend/shared/types.ts
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
export type WaveformOptions = {
|
2 |
+
waveform_color?: string;
|
3 |
+
waveform_progress_color?: string;
|
4 |
+
show_controls?: boolean;
|
5 |
+
skip_length?: number;
|
6 |
+
trim_region_color?: string;
|
7 |
+
show_recording_waveform?: boolean;
|
8 |
+
sample_rate?: number;
|
9 |
+
};
|
sourceviewer/frontend/shared/utils.ts
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import type WaveSurfer from "wavesurfer.js";
|
2 |
+
import { audioBufferToWav } from "./audioBufferToWav";
|
3 |
+
|
4 |
+
export interface LoadedParams {
|
5 |
+
autoplay?: boolean;
|
6 |
+
}
|
7 |
+
|
8 |
+
export function blob_to_data_url(blob: Blob): Promise<string> {
|
9 |
+
return new Promise((fulfill, reject) => {
|
10 |
+
let reader = new FileReader();
|
11 |
+
reader.onerror = reject;
|
12 |
+
reader.onload = () => fulfill(reader.result as string);
|
13 |
+
reader.readAsDataURL(blob);
|
14 |
+
});
|
15 |
+
}
|
16 |
+
|
17 |
+
export const process_audio = async (
|
18 |
+
audioBuffer: AudioBuffer,
|
19 |
+
start?: number,
|
20 |
+
end?: number,
|
21 |
+
waveform_sample_rate?: number
|
22 |
+
): Promise<Uint8Array> => {
|
23 |
+
const audioContext = new AudioContext({
|
24 |
+
sampleRate: waveform_sample_rate || audioBuffer.sampleRate
|
25 |
+
});
|
26 |
+
const numberOfChannels = audioBuffer.numberOfChannels;
|
27 |
+
const sampleRate = waveform_sample_rate || audioBuffer.sampleRate;
|
28 |
+
|
29 |
+
let trimmedLength = audioBuffer.length;
|
30 |
+
let startOffset = 0;
|
31 |
+
|
32 |
+
if (start && end) {
|
33 |
+
startOffset = Math.round(start * sampleRate);
|
34 |
+
const endOffset = Math.round(end * sampleRate);
|
35 |
+
trimmedLength = endOffset - startOffset;
|
36 |
+
}
|
37 |
+
|
38 |
+
const trimmedAudioBuffer = audioContext.createBuffer(
|
39 |
+
numberOfChannels,
|
40 |
+
trimmedLength,
|
41 |
+
sampleRate
|
42 |
+
);
|
43 |
+
|
44 |
+
for (let channel = 0; channel < numberOfChannels; channel++) {
|
45 |
+
const channelData = audioBuffer.getChannelData(channel);
|
46 |
+
const trimmedData = trimmedAudioBuffer.getChannelData(channel);
|
47 |
+
for (let i = 0; i < trimmedLength; i++) {
|
48 |
+
trimmedData[i] = channelData[startOffset + i];
|
49 |
+
}
|
50 |
+
}
|
51 |
+
|
52 |
+
return audioBufferToWav(trimmedAudioBuffer);
|
53 |
+
};
|
54 |
+
|
55 |
+
export function loaded(
|
56 |
+
node: HTMLAudioElement,
|
57 |
+
{ autoplay }: LoadedParams = {}
|
58 |
+
): void {
|
59 |
+
async function handle_playback(): Promise<void> {
|
60 |
+
if (!autoplay) return;
|
61 |
+
node.pause();
|
62 |
+
await node.play();
|
63 |
+
}
|
64 |
+
}
|
65 |
+
|
66 |
+
export const skip_audio = (waveform: WaveSurfer, amount: number): void => {
|
67 |
+
if (!waveform) return;
|
68 |
+
waveform.skip(amount);
|
69 |
+
};
|
70 |
+
|
71 |
+
export const get_skip_rewind_amount = (
|
72 |
+
audio_duration: number,
|
73 |
+
skip_length?: number | null
|
74 |
+
): number => {
|
75 |
+
if (!skip_length) {
|
76 |
+
skip_length = 5;
|
77 |
+
}
|
78 |
+
return (audio_duration / 100) * skip_length || 5;
|
79 |
+
};
|
sourceviewer/frontend/static/StaticAudio.svelte
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<script lang="ts">
|
2 |
+
import { uploadToHuggingFace } from "@gradio/utils";
|
3 |
+
import { Empty } from "@gradio/atoms";
|
4 |
+
import {
|
5 |
+
ShareButton,
|
6 |
+
IconButton,
|
7 |
+
BlockLabel,
|
8 |
+
IconButtonWrapper
|
9 |
+
} from "@gradio/atoms";
|
10 |
+
import { Download, Music } from "@gradio/icons";
|
11 |
+
import type { I18nFormatter } from "@gradio/utils";
|
12 |
+
import AudioPlayer from "../player/AudioPlayer.svelte";
|
13 |
+
import { createEventDispatcher } from "svelte";
|
14 |
+
import type { FileData } from "@gradio/client";
|
15 |
+
import { DownloadLink } from "@gradio/wasm/svelte";
|
16 |
+
import type { WaveformOptions } from "../shared/types";
|
17 |
+
|
18 |
+
export let value: null | FileData = null;
|
19 |
+
export let label: string;
|
20 |
+
export let show_label = true;
|
21 |
+
export let show_download_button = true;
|
22 |
+
export let show_share_button = false;
|
23 |
+
export let i18n: I18nFormatter;
|
24 |
+
export let waveform_settings: Record<string, any>;
|
25 |
+
export let waveform_options: WaveformOptions;
|
26 |
+
export let editable = true;
|
27 |
+
export let loop: boolean;
|
28 |
+
|
29 |
+
const dispatch = createEventDispatcher<{
|
30 |
+
change: FileData;
|
31 |
+
play: undefined;
|
32 |
+
pause: undefined;
|
33 |
+
end: undefined;
|
34 |
+
stop: undefined;
|
35 |
+
}>();
|
36 |
+
|
37 |
+
$: value && dispatch("change", value);
|
38 |
+
</script>
|
39 |
+
|
40 |
+
<BlockLabel
|
41 |
+
{show_label}
|
42 |
+
Icon={Music}
|
43 |
+
float={false}
|
44 |
+
label={label || i18n("audio.audio")}
|
45 |
+
/>
|
46 |
+
|
47 |
+
{#if value !== null}
|
48 |
+
<IconButtonWrapper>
|
49 |
+
{#if show_download_button}
|
50 |
+
<DownloadLink
|
51 |
+
href={value.is_stream
|
52 |
+
? value.url?.replace("playlist.m3u8", "playlist-file")
|
53 |
+
: value.url}
|
54 |
+
download={value.orig_name || value.path}
|
55 |
+
>
|
56 |
+
<IconButton Icon={Download} label={i18n("common.download")} />
|
57 |
+
</DownloadLink>
|
58 |
+
{/if}
|
59 |
+
{#if show_share_button}
|
60 |
+
<ShareButton
|
61 |
+
{i18n}
|
62 |
+
on:error
|
63 |
+
on:share
|
64 |
+
formatter={async (value) => {
|
65 |
+
if (!value) return "";
|
66 |
+
let url = await uploadToHuggingFace(value.url, "url");
|
67 |
+
return `<audio controls src="${url}"></audio>`;
|
68 |
+
}}
|
69 |
+
{value}
|
70 |
+
/>
|
71 |
+
{/if}
|
72 |
+
</IconButtonWrapper>
|
73 |
+
|
74 |
+
<AudioPlayer
|
75 |
+
{value}
|
76 |
+
{label}
|
77 |
+
{i18n}
|
78 |
+
{waveform_settings}
|
79 |
+
{waveform_options}
|
80 |
+
{editable}
|
81 |
+
{loop}
|
82 |
+
on:pause
|
83 |
+
on:play
|
84 |
+
on:stop
|
85 |
+
on:load
|
86 |
+
/>
|
87 |
+
{:else}
|
88 |
+
<Empty size="small">
|
89 |
+
<Music />
|
90 |
+
</Empty>
|
91 |
+
{/if}
|
sourceviewer/frontend/streaming/StreamAudio.svelte
ADDED
@@ -0,0 +1,205 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<script lang="ts">
|
2 |
+
import { onMount } from "svelte";
|
3 |
+
import type { I18nFormatter } from "@gradio/utils";
|
4 |
+
import { Spinner } from "@gradio/icons";
|
5 |
+
import WaveSurfer from "wavesurfer.js";
|
6 |
+
import RecordPlugin from "wavesurfer.js/dist/plugins/record.js";
|
7 |
+
import type { WaveformOptions } from "../shared/types";
|
8 |
+
import DeviceSelect from "../shared/DeviceSelect.svelte";
|
9 |
+
|
10 |
+
export let recording = false;
|
11 |
+
export let paused_recording = false;
|
12 |
+
export let stop: () => void;
|
13 |
+
export let record: () => void;
|
14 |
+
export let i18n: I18nFormatter;
|
15 |
+
export let waveform_settings: Record<string, any>;
|
16 |
+
export let waveform_options: WaveformOptions = {
|
17 |
+
show_recording_waveform: true
|
18 |
+
};
|
19 |
+
export let waiting = false;
|
20 |
+
|
21 |
+
let micWaveform: WaveSurfer;
|
22 |
+
let waveformRecord: RecordPlugin;
|
23 |
+
|
24 |
+
let microphoneContainer: HTMLDivElement;
|
25 |
+
|
26 |
+
let micDevices: MediaDeviceInfo[] = [];
|
27 |
+
|
28 |
+
onMount(() => {
|
29 |
+
create_mic_waveform();
|
30 |
+
});
|
31 |
+
|
32 |
+
const create_mic_waveform = (): void => {
|
33 |
+
if (micWaveform !== undefined) micWaveform.destroy();
|
34 |
+
if (!microphoneContainer) return;
|
35 |
+
micWaveform = WaveSurfer.create({
|
36 |
+
...waveform_settings,
|
37 |
+
height: 100,
|
38 |
+
container: microphoneContainer
|
39 |
+
});
|
40 |
+
|
41 |
+
waveformRecord = micWaveform.registerPlugin(RecordPlugin.create());
|
42 |
+
};
|
43 |
+
</script>
|
44 |
+
|
45 |
+
<div class="mic-wrap">
|
46 |
+
{#if waveform_options.show_recording_waveform}
|
47 |
+
<div
|
48 |
+
bind:this={microphoneContainer}
|
49 |
+
style:display={recording ? "block" : "none"}
|
50 |
+
/>
|
51 |
+
{/if}
|
52 |
+
<div class="controls">
|
53 |
+
{#if recording && !waiting}
|
54 |
+
<button
|
55 |
+
class={paused_recording ? "stop-button-paused" : "stop-button"}
|
56 |
+
on:click={() => {
|
57 |
+
waveformRecord?.stopMic();
|
58 |
+
stop();
|
59 |
+
}}
|
60 |
+
>
|
61 |
+
<span class="record-icon">
|
62 |
+
<span class="pinger" />
|
63 |
+
<span class="dot" />
|
64 |
+
</span>
|
65 |
+
{paused_recording ? i18n("audio.pause") : i18n("audio.stop")}
|
66 |
+
</button>
|
67 |
+
{:else if recording && waiting}
|
68 |
+
<button
|
69 |
+
class="spinner-button"
|
70 |
+
on:click={() => {
|
71 |
+
stop();
|
72 |
+
}}
|
73 |
+
>
|
74 |
+
<div class="icon">
|
75 |
+
<Spinner />
|
76 |
+
</div>
|
77 |
+
{i18n("audio.waiting")}
|
78 |
+
</button>
|
79 |
+
{:else}
|
80 |
+
<button
|
81 |
+
class="record-button"
|
82 |
+
on:click={() => {
|
83 |
+
waveformRecord?.startMic();
|
84 |
+
record();
|
85 |
+
}}
|
86 |
+
>
|
87 |
+
<span class="record-icon">
|
88 |
+
<span class="dot" />
|
89 |
+
</span>
|
90 |
+
{i18n("audio.record")}
|
91 |
+
</button>
|
92 |
+
{/if}
|
93 |
+
|
94 |
+
<DeviceSelect bind:micDevices {i18n} />
|
95 |
+
</div>
|
96 |
+
</div>
|
97 |
+
|
98 |
+
<style>
|
99 |
+
.controls {
|
100 |
+
display: flex;
|
101 |
+
align-items: center;
|
102 |
+
justify-content: space-between;
|
103 |
+
flex-wrap: wrap;
|
104 |
+
}
|
105 |
+
|
106 |
+
.mic-wrap {
|
107 |
+
display: block;
|
108 |
+
align-items: center;
|
109 |
+
margin: var(--spacing-xl);
|
110 |
+
}
|
111 |
+
|
112 |
+
.icon {
|
113 |
+
width: var(--size-4);
|
114 |
+
height: var(--size-4);
|
115 |
+
fill: var(--primary-600);
|
116 |
+
stroke: var(--primary-600);
|
117 |
+
}
|
118 |
+
|
119 |
+
.stop-button-paused {
|
120 |
+
display: none;
|
121 |
+
height: var(--size-8);
|
122 |
+
width: var(--size-20);
|
123 |
+
background-color: var(--block-background-fill);
|
124 |
+
border-radius: var(--button-large-radius);
|
125 |
+
align-items: center;
|
126 |
+
border: 1px solid var(--block-border-color);
|
127 |
+
margin-right: 5px;
|
128 |
+
}
|
129 |
+
|
130 |
+
.stop-button-paused::before {
|
131 |
+
content: "";
|
132 |
+
height: var(--size-4);
|
133 |
+
width: var(--size-4);
|
134 |
+
border-radius: var(--radius-full);
|
135 |
+
background: var(--primary-600);
|
136 |
+
margin: 0 var(--spacing-xl);
|
137 |
+
}
|
138 |
+
|
139 |
+
.stop-button::before {
|
140 |
+
content: "";
|
141 |
+
height: var(--size-4);
|
142 |
+
width: var(--size-4);
|
143 |
+
border-radius: var(--radius-full);
|
144 |
+
background: var(--primary-600);
|
145 |
+
margin: 0 var(--spacing-xl);
|
146 |
+
animation: scaling 1800ms infinite;
|
147 |
+
}
|
148 |
+
|
149 |
+
.stop-button {
|
150 |
+
height: var(--size-8);
|
151 |
+
width: var(--size-20);
|
152 |
+
background-color: var(--block-background-fill);
|
153 |
+
border-radius: var(--button-large-radius);
|
154 |
+
align-items: center;
|
155 |
+
border: 1px solid var(--primary-600);
|
156 |
+
margin-right: 5px;
|
157 |
+
display: flex;
|
158 |
+
}
|
159 |
+
|
160 |
+
.spinner-button {
|
161 |
+
height: var(--size-8);
|
162 |
+
width: var(--size-24);
|
163 |
+
background-color: var(--block-background-fill);
|
164 |
+
border-radius: var(--radius-3xl);
|
165 |
+
align-items: center;
|
166 |
+
border: 1px solid var(--primary-600);
|
167 |
+
margin: 0 var(--spacing-xl);
|
168 |
+
display: flex;
|
169 |
+
justify-content: space-evenly;
|
170 |
+
}
|
171 |
+
|
172 |
+
.record-button::before {
|
173 |
+
content: "";
|
174 |
+
height: var(--size-4);
|
175 |
+
width: var(--size-4);
|
176 |
+
border-radius: var(--radius-full);
|
177 |
+
background: var(--primary-600);
|
178 |
+
margin: 0 var(--spacing-xl);
|
179 |
+
}
|
180 |
+
|
181 |
+
.record-button {
|
182 |
+
height: var(--size-8);
|
183 |
+
width: var(--size-24);
|
184 |
+
background-color: var(--block-background-fill);
|
185 |
+
border-radius: var(--button-large-radius);
|
186 |
+
display: flex;
|
187 |
+
align-items: center;
|
188 |
+
border: 1px solid var(--block-border-color);
|
189 |
+
}
|
190 |
+
|
191 |
+
@keyframes scaling {
|
192 |
+
0% {
|
193 |
+
background-color: var(--primary-600);
|
194 |
+
scale: 1;
|
195 |
+
}
|
196 |
+
50% {
|
197 |
+
background-color: var(--primary-600);
|
198 |
+
scale: 1.2;
|
199 |
+
}
|
200 |
+
100% {
|
201 |
+
background-color: var(--primary-600);
|
202 |
+
scale: 1;
|
203 |
+
}
|
204 |
+
}
|
205 |
+
</style>
|
sourceviewer/pyproject.toml
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[build-system]
|
2 |
+
requires = [
|
3 |
+
"hatchling",
|
4 |
+
"hatch-requirements-txt",
|
5 |
+
"hatch-fancy-pypi-readme>=22.5.0",
|
6 |
+
]
|
7 |
+
build-backend = "hatchling.build"
|
8 |
+
|
9 |
+
[project]
|
10 |
+
name = "gradio_sourceviewer"
|
11 |
+
version = "0.0.1"
|
12 |
+
description = "Python library for easily interacting with trained machine learning models"
|
13 |
+
readme = "README.md"
|
14 |
+
license = "Apache-2.0"
|
15 |
+
requires-python = ">=3.8"
|
16 |
+
authors = [{ name = "YOUR NAME", email = "YOUREMAIL@domain.com" }]
|
17 |
+
keywords = [
|
18 |
+
"gradio-custom-component",
|
19 |
+
"gradio-template-Audio"
|
20 |
+
]
|
21 |
+
# Add dependencies here
|
22 |
+
dependencies = ["gradio>=4.0,<6.0"]
|
23 |
+
classifiers = [
|
24 |
+
'Development Status :: 3 - Alpha',
|
25 |
+
'Operating System :: OS Independent',
|
26 |
+
'Programming Language :: Python :: 3',
|
27 |
+
'Programming Language :: Python :: 3 :: Only',
|
28 |
+
'Programming Language :: Python :: 3.8',
|
29 |
+
'Programming Language :: Python :: 3.9',
|
30 |
+
'Programming Language :: Python :: 3.10',
|
31 |
+
'Programming Language :: Python :: 3.11',
|
32 |
+
'Topic :: Scientific/Engineering',
|
33 |
+
'Topic :: Scientific/Engineering :: Artificial Intelligence',
|
34 |
+
'Topic :: Scientific/Engineering :: Visualization',
|
35 |
+
]
|
36 |
+
|
37 |
+
# The repository and space URLs are optional, but recommended.
|
38 |
+
# Adding a repository URL will create a badge in the auto-generated README that links to the repository.
|
39 |
+
# Adding a space URL will create a badge in the auto-generated README that links to the space.
|
40 |
+
# This will make it easy for people to find your deployed demo or source code when they
|
41 |
+
# encounter your project in the wild.
|
42 |
+
|
43 |
+
# [project.urls]
|
44 |
+
# repository = "your github repository"
|
45 |
+
# space = "your space url"
|
46 |
+
|
47 |
+
[project.optional-dependencies]
|
48 |
+
dev = ["build", "twine"]
|
49 |
+
|
50 |
+
[tool.hatch.build]
|
51 |
+
artifacts = ["/backend/gradio_sourceviewer/templates", "*.pyi"]
|
52 |
+
|
53 |
+
[tool.hatch.build.targets.wheel]
|
54 |
+
packages = ["/backend/gradio_sourceviewer"]
|