-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Pin wavesurfer to 6.6.4. * Fixing n_samples * Adding traceback * Fixing the mushra script. * fixing linting * test mushra only * Bumping version * adding a license, fixing tests * Fixing linting. --------- Co-authored-by: prem <[email protected]>
- Loading branch information
Showing
6 changed files
with
135 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023-Present, Descript | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,105 @@ | ||
import math | ||
import string | ||
from dataclasses import dataclass | ||
from pathlib import Path | ||
from typing import List | ||
|
||
import argbind | ||
import gradio as gr | ||
import numpy as np | ||
import soundfile as sf | ||
|
||
from audiotools import preference as pr | ||
|
||
|
||
@argbind.bind(without_prefix=True) | ||
@dataclass | ||
class Config: | ||
folder: str = None | ||
save_path: str = "results.csv" | ||
conditions: list = None | ||
conditions: List[str] = None | ||
reference: str = None | ||
seed: int = 0 | ||
|
||
|
||
def random_sine(f): | ||
fs = 44100 # sampling rate, Hz, must be integer | ||
duration = 5.0 # in seconds, may be float | ||
|
||
# generate samples, note conversion to float32 array | ||
volume = 0.1 | ||
num_samples = int(fs * duration) | ||
samples = volume * np.sin(2 * math.pi * (f / fs) * np.arange(num_samples)) | ||
|
||
return samples, fs | ||
|
||
|
||
def create_data(path): | ||
path = Path(path) | ||
hz = [110, 140, 180] | ||
|
||
for i in range(6): | ||
name = f"condition_{string.ascii_lowercase[i]}" | ||
for j in range(3): | ||
sample_path = path / name / f"sample_{j}.wav" | ||
sample_path.parent.mkdir(exist_ok=True, parents=True) | ||
audio, sr = random_sine(hz[j] * (2**i)) | ||
sf.write(sample_path, audio, sr) | ||
|
||
|
||
config = Config( | ||
folder="/tmp/pref/audio/", | ||
save_path="/tmp/pref/results.csv", | ||
conditions=["condition_a", "condition_b"], | ||
reference="condition_c", | ||
) | ||
|
||
create_data(config.folder) | ||
|
||
with gr.Blocks() as app: | ||
save_path = config.save_path | ||
samples = gr.State(pr.Samples(config.folder)) | ||
|
||
reference = config.reference | ||
conditions = config.conditions | ||
|
||
player = pr.Player(app) | ||
player.create() | ||
if reference is not None: | ||
player.add("Play Reference") | ||
|
||
user = pr.create_tracker(app) | ||
ratings = [] | ||
|
||
with gr.Row(): | ||
gr.HTML("") | ||
with gr.Column(scale=9): | ||
gr.HTML(pr.slider_mushra) | ||
|
||
for i in range(len(conditions)): | ||
with gr.Row().style(equal_height=True): | ||
x = string.ascii_uppercase[i] | ||
player.add(f"Play {x}") | ||
with gr.Column(scale=9): | ||
ratings.append(gr.Slider(value=50, interactive=True)) | ||
|
||
def build(user, samples, *ratings): | ||
# Filter out samples user has done already, by looking in the CSV. | ||
samples.filter_completed(user, save_path) | ||
|
||
# Write results to CSV | ||
if samples.current > 0: | ||
start_idx = 1 if reference is not None else 0 | ||
name = samples.names[samples.current - 1] | ||
result = {"sample": name, "user": user} | ||
for k, r in zip(samples.order[start_idx:], ratings): | ||
result[k] = r | ||
pr.save_result(result, save_path) | ||
|
||
updates, done, pbar = samples.get_next_sample(reference, conditions) | ||
return updates + [gr.update(value=50) for _ in ratings] + [done, samples, pbar] | ||
|
||
progress = gr.HTML() | ||
begin = gr.Button("Submit", elem_id="start-survey") | ||
begin.click( | ||
fn=build, | ||
inputs=[user, samples] + ratings, | ||
outputs=player.to_list() + ratings + [begin, samples, progress], | ||
).then(None, _js=pr.reset_player) | ||
|
||
# Comment this back in to actually launch the script. | ||
app.launch() | ||
share: bool = False | ||
n_samples: int = 10 | ||
|
||
|
||
def get_text(wav_file: str): | ||
txt_file = Path(wav_file).with_suffix(".txt") | ||
if Path(txt_file).exists(): | ||
with open(txt_file, "r") as f: | ||
txt = f.read() | ||
else: | ||
txt = "" | ||
return f"""<div style="text-align:center;font-size:large;">{txt}</div>""" | ||
|
||
|
||
def main(config: Config): | ||
with gr.Blocks() as app: | ||
save_path = config.save_path | ||
samples = gr.State(pr.Samples(config.folder, n_samples=config.n_samples)) | ||
|
||
reference = config.reference | ||
conditions = config.conditions | ||
|
||
player = pr.Player(app) | ||
player.create() | ||
if reference is not None: | ||
player.add("Play Reference") | ||
|
||
user = pr.create_tracker(app) | ||
ratings = [] | ||
|
||
with gr.Row(): | ||
txt = gr.HTML("") | ||
|
||
with gr.Row(): | ||
gr.Button("Rate audio quality", interactive=False) | ||
with gr.Column(scale=8): | ||
gr.HTML(pr.slider_mushra) | ||
|
||
for i in range(len(conditions)): | ||
with gr.Row().style(equal_height=True): | ||
x = string.ascii_uppercase[i] | ||
player.add(f"Play {x}") | ||
with gr.Column(scale=9): | ||
ratings.append(gr.Slider(value=50, interactive=True)) | ||
|
||
def build(user, samples, *ratings): | ||
# Filter out samples user has done already, by looking in the CSV. | ||
samples.filter_completed(user, save_path) | ||
|
||
# Write results to CSV | ||
if samples.current > 0: | ||
start_idx = 1 if reference is not None else 0 | ||
name = samples.names[samples.current - 1] | ||
result = {"sample": name, "user": user} | ||
for k, r in zip(samples.order[start_idx:], ratings): | ||
result[k] = r | ||
pr.save_result(result, save_path) | ||
|
||
updates, done, pbar = samples.get_next_sample(reference, conditions) | ||
wav_file = updates[0]["value"] | ||
|
||
txt_update = gr.update(value=get_text(wav_file)) | ||
|
||
return ( | ||
updates | ||
+ [gr.update(value=50) for _ in ratings] | ||
+ [done, samples, pbar, txt_update] | ||
) | ||
|
||
progress = gr.HTML() | ||
begin = gr.Button("Submit", elem_id="start-survey") | ||
begin.click( | ||
fn=build, | ||
inputs=[user, samples] + ratings, | ||
outputs=player.to_list() + ratings + [begin, samples, progress, txt], | ||
).then(None, _js=pr.reset_player) | ||
|
||
# Comment this back in to actually launch the script. | ||
app.launch(share=config.share) | ||
|
||
|
||
if __name__ == "__main__": | ||
args = argbind.parse_args() | ||
with argbind.scope(args): | ||
config = Config() | ||
main(config) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters