tests: playlist_import, log_archiver, sound_stats.SoxStats

This commit is contained in:
bkfox
2023-06-22 23:29:51 +02:00
parent a289e1ef05
commit b2f4bce717
8 changed files with 282 additions and 82 deletions

View File

@ -24,16 +24,30 @@ class SoxStats:
"Length s",
]
def __init__(self, path, **kwargs):
values = None
def __init__(self, path=None, **kwargs):
"""If path is given, call analyse with path and kwargs."""
self.values = {}
if path:
self.analyse(path, **kwargs)
def get(self, attr):
return self.values.get(attr)
def analyse(self, path, at=None, length=None):
"""If at and length are given use them as excerpt to analyse."""
args = ["sox", path, "-n"]
if at is not None and length is not None:
args += ["trim", str(at), str(length)]
args.append("stats")
p = subprocess.Popen(
args, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
# sox outputs to stderr (my god WHYYYY)
out_, out = p.communicate()
self.values = self.parse(str(out, encoding="utf-8"))
def parse(self, output):
"""Parse sox output, settubg values from it."""
values = {}
for attr in self.attributes:
value = re.search(attr + r"\s+(?P<value>\S+)", output)
value = value and value.groupdict()
@ -42,24 +56,12 @@ class SoxStats:
value = float(value.get("value"))
except ValueError:
value = None
self.values[attr] = value
self.values["length"] = self.values["Length s"]
values[attr] = value
values["length"] = values.pop("Length s", None)
return values
def analyse(self, path, at=None, length=None):
"""If at and length are given use them as excerpt to analyse."""
args = ["sox", path, "-n"]
if at is not None and length is not None:
args += ["trim", str(at), str(length)]
args.append("stats")
p = subprocess.Popen(
args, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
# sox outputs to stderr (my god WHYYYY)
out_, out = p.communicate()
self.parse(str(out, encoding="utf-8"))
def get(self, attr):
return self.values.get(attr)
class SoundStats: