Skip to content

Commit

Permalink
v0.7.3 (#90)
Browse files Browse the repository at this point in the history
* 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
pseeth and prem authored Sep 14, 2023
1 parent 49b8b6b commit 784f87b
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 157 deletions.
21 changes: 21 additions & 0 deletions LICENSE
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.
2 changes: 1 addition & 1 deletion audiotools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.7.2"
__version__ = "0.7.3"
from .core import AudioSignal
from .core import STFTParams
from .core import Meter
Expand Down
16 changes: 10 additions & 6 deletions audiotools/preference.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import copy
import csv
import random
import sys
import traceback
from collections import defaultdict
from pathlib import Path
from typing import List
Expand Down Expand Up @@ -123,11 +125,11 @@
console.log("Created WaveSurfer object.")
}
load_script('https://unpkg.com/wavesurfer.js')
load_script('https://unpkg.com/wavesurfer.js@6.6.4')
.then(() => {
load_script("https://unpkg.com/wavesurfer.js/dist/plugin/wavesurfer.timeline.min.js")
load_script("https://unpkg.com/wavesurfer.js@6.6.4/dist/plugin/wavesurfer.timeline.min.js")
.then(() => {
load_script('https://unpkg.com/wavesurfer.js/dist/plugin/wavesurfer.regions.min.js')
load_script('https://unpkg.com/wavesurfer.js@6.6.4/dist/plugin/wavesurfer.regions.min.js')
.then(() => {
console.log("Loaded regions");
create_wavesurfer();
Expand Down Expand Up @@ -535,7 +537,7 @@ def __init__(self, folder: str, shuffle: bool = True, n_samples: int = None):
if shuffle:
random.shuffle(self.names)

self.n_samples = n_samples
self.n_samples = len(self.names) if n_samples is None else n_samples

def get_updates(self, idx, order):
key = self.names[idx]
Expand All @@ -544,7 +546,7 @@ def get_updates(self, idx, order):
def progress(self):
try:
pct = self.current / len(self) * 100
except:
except: # pragma: no cover
pct = 100
text = f"On {self.current} / {len(self)} samples"
pbar = (
Expand All @@ -555,7 +557,7 @@ def progress(self):
return gr.update(value=pbar)

def __len__(self):
return len(self.names)
return self.n_samples

def filter_completed(self, user, save_path):
if not self.filtered:
Expand All @@ -565,6 +567,7 @@ def filter_completed(self, user, save_path):
reader = csv.DictReader(f)
done = [r["sample"] for r in reader if r["user"] == user]
self.names = [k for k in self.names if k not in done]
self.names = self.names[: self.n_samples]
self.filtered = True # Avoid filtering more than once per session.

def get_next_sample(self, reference, conditions):
Expand All @@ -580,6 +583,7 @@ def get_next_sample(self, reference, conditions):
done = gr.update(interactive=True)
pbar = self.progress()
except:
traceback.print_exc()
updates = [gr.update() for _ in range(len(self.order))]
done = gr.update(value="No more samples!", interactive=False)
self.current = len(self)
Expand Down
183 changes: 90 additions & 93 deletions examples/mushra.py
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)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name="descript-audiotools",
version="0.7.2",
version="0.7.3",
classifiers=[
"Intended Audience :: Developers",
"Intended Audience :: Education",
Expand Down
68 changes: 12 additions & 56 deletions tests/test_preference.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def build(user, samples, *ratings):
samples.filter_completed(user, save_path)

# Write results to CSV
if samples.current > 0:
if samples.current > 0 and len(samples.names) > 0:
start_idx = 1 if reference is not None else 0
name = samples.names[samples.current - 1]
result = {"sample": name, "user": user}
Expand All @@ -107,60 +107,6 @@ def build(user, samples, *ratings):
build("test", samples, 95, 85)


def _test_abx(app, config):
"Launches a preference test"
save_path = config.save_path
samples = gr.State(pr.Samples(config.folder))

reference = None
conditions = config.conditions
assert len(conditions) == 2, "Preference tests take only two conditions!"

player = pr.Player(app)
player.create()
if reference is not None:
player.add("Play Reference")

user = pr.create_tracker(app)

with gr.Row().style(equal_height=True):
for i in range(len(conditions)):
x = string.ascii_uppercase[i]
player.add(f"Play {x}")

rating = gr.Slider(value=50, interactive=True)
gr.HTML(pr.slider_abx)

def build(user, samples, rating):
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}

result[samples.order[start_idx]] = 100 - rating
result[samples.order[start_idx + 1]] = rating
pr.save_result(result, save_path)

updates, done, pbar = samples.get_next_sample(reference, conditions)
return updates + [gr.update(value=50), done, samples, pbar]

progress = gr.HTML()
begin = gr.Button("Submit", elem_id="start-survey")
begin.click(
fn=build,
inputs=[user, samples, rating],
outputs=player.to_list() + [rating, begin, samples, progress],
).then(None, _js=pr.reset_player)

# Call build to simulate a button click
samples = pr.Samples(config.folder)
for i in range(len(samples) + 1):
build("test", samples, 100)


def test_preference():
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = Path(tmpdir)
Expand All @@ -174,6 +120,16 @@ def test_preference():
create_data(config.folder)
with gr.Blocks() as app:
_test_mushra(app, config)
_test_mushra(app, config)

with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = Path(tmpdir)
config = Config(
folder=tmpdir,
save_path=tmpdir / "results.csv",
conditions=["condition_a", "condition_b"],
)

create_data(config.folder)
with gr.Blocks() as app:
_test_abx(app, config)
_test_mushra(app, config)

0 comments on commit 784f87b

Please sign in to comment.