Python
from trueflac import Clue, Status, analyze
verdict = analyze("02 - Single.flac")if verdict.status is Status.SUSPECT: if verdict.clue is Clue.CUT: print(f"highs cut at {verdict.cutoff_hz:.0f} Hz") else: # Clue.GRID print(f"made from {verdict.codec}") # "MP3", "AAC" or "Vorbis"analyze() takes a path (a string or a pathlib.Path) and returns a Verdict. It never prints and
never writes. A file it cannot read gives a verdict with Status.UNREADABLE and the reason in
error, not an exception. The full reference is in the Python API.
The verdict
Section titled “The verdict”A Verdict holds data, not sentences, so an application can word it in its own language:
| Field | Meaning |
|---|---|
path |
The file, as given to analyze(). |
status |
Status.OK, Status.SUSPECT or Status.UNREADABLE. |
clue |
Why it is suspect: Clue.CUT or Clue.GRID; None otherwise. |
codec |
"MP3", "AAC" or "Vorbis", for a grid. |
cutoff_hz |
Where the highs end, when they end sharply, also on a genuine file. |
grid_contrast |
Strength of the best block grid, when the grids were measured (suspect from 10). |
sample_rate, bit_depth, channels, seconds |
The file. bit_depth is None for float formats. |
error |
Why the file could not be read. |
verdict.suspect is a shortcut for verdict.status is Status.SUSPECT. Status and Clue are
string enums: verdict.status == "suspect" works too, and dataclasses.asdict(verdict) turns a
verdict into a dictionary ready for JSON.
describe(verdict) gives the English line the command line prints. An application in another
language builds its own sentence from the fields instead.
Canceling
Section titled “Canceling”analyze() takes an optional cancel callable. It is checked between the stages of the analysis,
each well under a second; when it returns True, analyze() raises Cancelled:
import threadingfrom trueflac import Cancelled, analyze
stop = threading.Event() # stop.set() from another thread, e.g. a Cancel buttontry: verdict = analyze(path, cancel=stop.is_set)except Cancelled: ...Several files at once
Section titled “Several files at once”analyze() keeps nothing between calls but read-only tables: it is safe to call from several
threads. numpy releases the interpreter lock in its heavy parts, so threads help:
from concurrent.futures import ThreadPoolExecutorfrom pathlib import Pathfrom trueflac import analyze
files = sorted(Path("~/Music").expanduser().rglob("*.flac"))with ThreadPoolExecutor(max_workers=4) as pool: for verdict in pool.map(analyze, files): if verdict.suspect: print(verdict.path, verdict.clue, verdict.codec)On the bench, four threads analyze a genuine track in 0.75 s instead of 1.5 s on an Apple Silicon Mac.