trueflac 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- trueflac-0.1.0/.gitignore +12 -0
- trueflac-0.1.0/CHANGELOG.md +30 -0
- trueflac-0.1.0/LICENSE +21 -0
- trueflac-0.1.0/PKG-INFO +286 -0
- trueflac-0.1.0/README.md +259 -0
- trueflac-0.1.0/bench/README.md +33 -0
- trueflac-0.1.0/pyproject.toml +60 -0
- trueflac-0.1.0/src/trueflac/__init__.py +13 -0
- trueflac-0.1.0/src/trueflac/analysis.py +242 -0
- trueflac-0.1.0/src/trueflac/cli.py +50 -0
- trueflac-0.1.0/src/trueflac/grid.py +201 -0
- trueflac-0.1.0/src/trueflac/mp3grid.py +135 -0
- trueflac-0.1.0/src/trueflac/py.typed +0 -0
- trueflac-0.1.0/src/trueflac/verdict.py +102 -0
- trueflac-0.1.0/tests/test_analysis.py +87 -0
- trueflac-0.1.0/tests/test_grid.py +58 -0
- trueflac-0.1.0/tests/test_mp3grid.py +47 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
# [0.1.0](https://github.com/opalehq/trueflac/compare/v0.0.0...v0.1.0) (2026-12-05)
|
|
4
|
+
|
|
5
|
+
* feat(detector)!: return structured verdicts and accept a cancel check ([41f3316](https://github.com/opalehq/trueflac/commit/41f33162f0365a0c1d4221d0ddd0cfdf0bc65feb))
|
|
6
|
+
|
|
7
|
+
### Bug Fixes
|
|
8
|
+
|
|
9
|
+
* **detector:** look for a cut only in the CD band ([8dd478f](https://github.com/opalehq/trueflac/commit/8dd478f0a1cdc65720bcc2a901504e5ac121f070))
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
* **bench:** add dithered and 24-bit decodes, and dithered genuine clips ([94890f5](https://github.com/opalehq/trueflac/commit/94890f544a45f8f6afcbd01ca382da3be63aa31b))
|
|
14
|
+
* **bench:** add hi-res, mono, short, WAV and AIFF clips ([5399354](https://github.com/opalehq/trueflac/commit/539935472187c0641f120dc5aeaf8b4b321c4308))
|
|
15
|
+
* **bench:** rebuild and score genuine and lossy-made clips ([585d72a](https://github.com/opalehq/trueflac/commit/585d72a6c88010e0f080a1616afc701bcb6fab35))
|
|
16
|
+
* **bench:** show the highest grid contrast of each group ([2142b05](https://github.com/opalehq/trueflac/commit/2142b05c543e109c72d7740fbf7a7d3f61844e53))
|
|
17
|
+
* **cli:** add --version ([6723a43](https://github.com/opalehq/trueflac/commit/6723a4382b9a84fd1b811135d1a3267368704170))
|
|
18
|
+
* **detector:** add AAC's KBD window to the codec grid ([987365a](https://github.com/opalehq/trueflac/commit/987365aea07f3f15a1469a10261c343b063ad089))
|
|
19
|
+
* **detector:** find the block grid of AAC and Vorbis encoders ([c9b2f0d](https://github.com/opalehq/trueflac/commit/c9b2f0d4a823fa5a5df4d804d8d3e6a55df78423))
|
|
20
|
+
* **detector:** find the granule grid of MP3 encoders ([4572869](https://github.com/opalehq/trueflac/commit/4572869d683391ef2c05a34a262808c7c605edae))
|
|
21
|
+
* **detector:** flag highs cut by a lossy encoder ([a7fbce5](https://github.com/opalehq/trueflac/commit/a7fbce5557d66ca68819ee5144b6ee85cada4d04))
|
|
22
|
+
* **detector:** see through dither and 24-bit decoding ([d9af276](https://github.com/opalehq/trueflac/commit/d9af27679e64fd2e5f3e084187c271efbd744c73))
|
|
23
|
+
|
|
24
|
+
### Performance Improvements
|
|
25
|
+
|
|
26
|
+
* **detector:** confirm only the two leading grid candidates ([2e87724](https://github.com/opalehq/trueflac/commit/2e877243640be7e1f37078186a4e29b08ecc26c5))
|
|
27
|
+
|
|
28
|
+
### BREAKING CHANGES
|
|
29
|
+
|
|
30
|
+
* Verdict lost reason and drop_db; status is a Status.
|
trueflac-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Opale
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
trueflac-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: trueflac
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Tells whether a FLAC (or WAV, AIFF) file was made from a lossy source: MP3, AAC, Opus, Vorbis.
|
|
5
|
+
Project-URL: Homepage, https://github.com/opalehq/trueflac
|
|
6
|
+
Project-URL: Issues, https://github.com/opalehq/trueflac/issues
|
|
7
|
+
Project-URL: Changelog, https://github.com/opalehq/trueflac/blob/main/CHANGELOG.md
|
|
8
|
+
Author: Opale
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: aac,audio,flac,lossless,mp3,music,spectrum,transcode
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Environment :: Console
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Intended Audience :: End Users/Desktop
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Topic :: Multimedia :: Sound/Audio :: Analysis
|
|
22
|
+
Classifier: Typing :: Typed
|
|
23
|
+
Requires-Python: >=3.11
|
|
24
|
+
Requires-Dist: numpy>=1.26
|
|
25
|
+
Requires-Dist: soundfile>=0.12
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
<div align="center">
|
|
29
|
+
|
|
30
|
+
# trueflac
|
|
31
|
+
|
|
32
|
+
<picture>
|
|
33
|
+
<source media="(prefers-color-scheme: dark)" srcset="docs/assets/logo-dark.svg">
|
|
34
|
+
<img src="docs/assets/logo-light.svg" alt="trueflac logo" height="72">
|
|
35
|
+
</picture>
|
|
36
|
+
|
|
37
|
+
**Tells whether a FLAC file was made from an MP3, an AAC, an Opus or a Vorbis file.**
|
|
38
|
+
|
|
39
|
+
[](https://github.com/opalehq/trueflac/actions/workflows/ci.yml)
|
|
40
|
+
[](https://pypi.org/project/trueflac/)
|
|
41
|
+

|
|
42
|
+

|
|
43
|
+
|
|
44
|
+
[Why](#-why-trueflac) β’ [Install](#-install) β’ [Command line](#-command-line) β’ [Python](#-python) β’
|
|
45
|
+
[How it works](#-how-it-works) β’ [Results](#-results) β’ [References](#-references) β’
|
|
46
|
+
[Documentation](https://opalehq.github.io/trueflac/)
|
|
47
|
+
|
|
48
|
+
</div>
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## π Why trueflac?
|
|
53
|
+
|
|
54
|
+
A FLAC file promises the sound of the CD. Converting an MP3 to FLAC keeps that promise on the
|
|
55
|
+
label only: the file is bigger, the sound is the MP3's. Music shared online, old downloads and
|
|
56
|
+
"lossless" packs often hold such files, and nothing in their tags says so.
|
|
57
|
+
|
|
58
|
+
trueflac looks at the sound itself and answers **suspect** or **ok** for each file. It never says
|
|
59
|
+
"fake": an old recording or a filtered master can look like a lossy file. When in doubt it keeps
|
|
60
|
+
quiet, so a genuine file is never accused on its word: on its bench, no genuine file is flagged.
|
|
61
|
+
|
|
62
|
+
It is the engine of the fake FLAC check in [S.O.K](https://github.com/opalehq/S.O.K), and works on
|
|
63
|
+
its own as a command and a Python library.
|
|
64
|
+
|
|
65
|
+
## β¨ Features
|
|
66
|
+
|
|
67
|
+
- **Two independent clues.** The highs cut like an encoder does, or the block grid of the encoder
|
|
68
|
+
still in the sound. The second one catches what the first cannot see: MP3 V0 and 320 kbit/s,
|
|
69
|
+
AAC 256, Vorbis.
|
|
70
|
+
- **Names the codec** when it finds its grid: MP3, AAC or Vorbis.
|
|
71
|
+
- **FLAC, WAV and AIFF**, 16 or 24 bits, 44.1 to 96 kHz, mono or stereo.
|
|
72
|
+
- **Read only.** It never writes, moves or deletes a file, and never goes on the network.
|
|
73
|
+
- **Light.** numpy and soundfile, nothing else. About 1.5 s for a genuine track on an Apple
|
|
74
|
+
Silicon Mac, half that on four threads.
|
|
75
|
+
- **Made for applications.** Verdicts are data that an app words in its own language,
|
|
76
|
+
analyses can be canceled, and it is safe to run on several threads.
|
|
77
|
+
|
|
78
|
+
## π₯ Install
|
|
79
|
+
|
|
80
|
+
```console
|
|
81
|
+
pip install trueflac
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Or as a command only, with [uv](https://docs.astral.sh/uv/): `uv tool install trueflac`.
|
|
85
|
+
|
|
86
|
+
Python 3.11 or later. soundfile ships its own libsndfile on Linux, macOS and Windows.
|
|
87
|
+
|
|
88
|
+
## π» Command line
|
|
89
|
+
|
|
90
|
+
```console
|
|
91
|
+
$ trueflac check Album/
|
|
92
|
+
ok 01 - Echoplex (CD).flac No sign of a lossy source
|
|
93
|
+
suspect 02 - Echoplex (from MP3 128).flac Highs cut at 16.6 kHz, like a lossy file
|
|
94
|
+
suspect 03 - Echoplex (from MP3 V0).flac Block grid of a lossy encoder (MP3)
|
|
95
|
+
suspect 04 - Echoplex (from AAC 256).flac Block grid of a lossy encoder (AAC)
|
|
96
|
+
suspect 05 - Echoplex (from Vorbis q6).flac Block grid of a lossy encoder (Vorbis)
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
This is a real run on one clip of the bench (Nine Inch Nails, *The Slip*) and four fakes made
|
|
100
|
+
from it. Only the MP3 128 lost its highs; the three others keep them and are seen through their
|
|
101
|
+
grid.
|
|
102
|
+
|
|
103
|
+
```console
|
|
104
|
+
trueflac check <file or folder>... [--json]
|
|
105
|
+
trueflac --version
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Folders are searched for `.flac`, `.wav`, `.aiff` and `.aif` files. `--json` prints one JSON object
|
|
109
|
+
per file:
|
|
110
|
+
|
|
111
|
+
```console
|
|
112
|
+
$ trueflac check --json "Album/02 - Echoplex (from MP3 128).flac"
|
|
113
|
+
{"path": "Album/02 - Echoplex (from MP3 128).flac", "status": "suspect", "clue": "cut", "codec": null, "cutoff_hz": 16618.24951171875, "grid_contrast": null, "sample_rate": 44100, "bit_depth": 16, "channels": 2, "seconds": 60.0, "error": null}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
The exit code is 1 when a file is suspect, 0 otherwise.
|
|
117
|
+
|
|
118
|
+
## π Python
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
from trueflac import Clue, Status, analyze
|
|
122
|
+
|
|
123
|
+
verdict = analyze("02 - Single.flac")
|
|
124
|
+
if verdict.status is Status.SUSPECT:
|
|
125
|
+
if verdict.clue is Clue.CUT:
|
|
126
|
+
print(f"highs cut at {verdict.cutoff_hz:.0f} Hz")
|
|
127
|
+
else: # Clue.GRID
|
|
128
|
+
print(f"made from {verdict.codec}") # "MP3", "AAC" or "Vorbis"
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
A `Verdict` holds data, not sentences, so an application can word it in its own language:
|
|
132
|
+
|
|
133
|
+
| Field | Meaning |
|
|
134
|
+
| --- | --- |
|
|
135
|
+
| `status` | `Status.OK`, `Status.SUSPECT` or `Status.UNREADABLE` |
|
|
136
|
+
| `clue` | why it is suspect: `Clue.CUT` (the highs stop like after an encoder) or `Clue.GRID` (the encoder's block grid is still there) |
|
|
137
|
+
| `codec` | `"MP3"`, `"AAC"` or `"Vorbis"`, for a grid |
|
|
138
|
+
| `cutoff_hz` | where the highs end, when they end sharply |
|
|
139
|
+
| `grid_contrast` | strength of the best block grid (suspect from 10) |
|
|
140
|
+
| `sample_rate`, `bit_depth`, `channels`, `seconds` | the file |
|
|
141
|
+
| `error` | why the file could not be read |
|
|
142
|
+
|
|
143
|
+
`describe(verdict)` gives the English line the command line prints.
|
|
144
|
+
|
|
145
|
+
`analyze()` never prints, never raises on a broken file (the verdict is `unreadable`) and is safe
|
|
146
|
+
to call from several threads. It takes an optional `cancel` callable, checked between its stages;
|
|
147
|
+
when it returns `True`, `analyze()` raises `Cancelled`:
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
import threading
|
|
151
|
+
from trueflac import Cancelled, analyze
|
|
152
|
+
|
|
153
|
+
stop = threading.Event()
|
|
154
|
+
try:
|
|
155
|
+
verdict = analyze(path, cancel=stop.is_set)
|
|
156
|
+
except Cancelled:
|
|
157
|
+
...
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## π¬ How it works
|
|
161
|
+
|
|
162
|
+
**1. Cut highs.** trueflac averages the spectrum at eight points of the track. An encoder throws
|
|
163
|
+
away the highs it finds inaudible: above its low-pass, a decoded file holds nothing but the
|
|
164
|
+
rounding noise of its bit depth, while a recording, even a 78 rpm disc or a quiet piano, keeps a
|
|
165
|
+
little noise of its own. A sharp fall to that floor below 20.3 kHz is suspect.
|
|
166
|
+
|
|
167
|
+
**2. Codec grid**, when the highs tell nothing. An encoder cuts the sound into blocks, moves each
|
|
168
|
+
block to frequencies with a fixed transform and sets to zero what it finds inaudible. Analyzed
|
|
169
|
+
with the same transform, at the same block start, the decoded file shows those zeros again.
|
|
170
|
+
A recording has no preferred start: every start looks the same. This idea comes from the
|
|
171
|
+
"inverse decoder" of Herre and Schug and from the detectors of Derrien and his co-authors (see
|
|
172
|
+
[References](#-references)).
|
|
173
|
+
|
|
174
|
+
trueflac tries every start:
|
|
175
|
+
|
|
176
|
+
- **MP3**: its 576 lines per granule are rebuilt from the standard (polyphase filter bank, 18-point
|
|
177
|
+
MDCT, alias butterflies) and checked against the minimp3 decoder.
|
|
178
|
+
- **AAC and Vorbis**: a 2048-point MDCT with the windows their encoders use (sine and
|
|
179
|
+
Kaiser-Bessel derived for AAC, the Vorbis power window).
|
|
180
|
+
|
|
181
|
+
A grid counts only when it stands far above all other starts (a contrast of 10, where genuine
|
|
182
|
+
tracks stay under 6) and when two separate parts of the track agree on where it starts.
|
|
183
|
+
|
|
184
|
+
## π Results
|
|
185
|
+
|
|
186
|
+
On a bench of 22 genuine clips (rock, solo piano, 78 rpm transfers) and the same clips encoded and
|
|
187
|
+
decoded back, with [flac-detective](https://github.com/Guillain-RDCDE/FLAC_Detective) 2.6.1 run on
|
|
188
|
+
the same files for reference:
|
|
189
|
+
|
|
190
|
+
| Files flagged (out of 22) | trueflac | flac-detective |
|
|
191
|
+
| --- | --- | --- |
|
|
192
|
+
| **Genuine** (false accusations) | **0** | 0 |
|
|
193
|
+
| MP3 128 / 192 / 256 / 320 kbit/s | **22 / 22 / 21 / 21** | 4 / 5 / 0 / 0 |
|
|
194
|
+
| MP3 V0 | **22** | 0 |
|
|
195
|
+
| AAC 256 (ffmpeg / Apple) | **19 / 18** | 12 / 0 |
|
|
196
|
+
| Opus 192 | **17** | 12 |
|
|
197
|
+
| Vorbis q6 | **22** | 21 |
|
|
198
|
+
| Time per file | **0.9 s** | 2.7 s |
|
|
199
|
+
|
|
200
|
+
Also measured, with no genuine file flagged: decodes with dither or to 24 bits, genuine clips
|
|
201
|
+
with dither, 48 and 96 kHz, mono, 3-second clips, WAV and AIFF. Times are on an Apple Silicon Mac
|
|
202
|
+
(average over the bench; a genuine track takes the longest, about 1.5 s, since every test runs).
|
|
203
|
+
|
|
204
|
+
**Known limits**
|
|
205
|
+
|
|
206
|
+
- **Opus** is only seen through its cut: it works at 48 kHz, and the resampling back to 44.1 kHz
|
|
207
|
+
blurs its grid.
|
|
208
|
+
- **Noise-shaped dither** fills the highs and can hide an AAC (Apple AAC: 11 of 22).
|
|
209
|
+
- **Very quiet highs** (solo piano) leave little to measure.
|
|
210
|
+
- **Lossy at the source**: a phone recording or a master made from MP3 is reported suspect, and
|
|
211
|
+
rightly so, even when the shop sells it as lossless.
|
|
212
|
+
- A bench of 22 clips is small. Report a wrong verdict: it is how the bench grows.
|
|
213
|
+
|
|
214
|
+
## π Documentation
|
|
215
|
+
|
|
216
|
+
The full guide, how the detection works in detail, every result, the sources and the Python API
|
|
217
|
+
reference are on **[opalehq.github.io/trueflac](https://opalehq.github.io/trueflac/)**.
|
|
218
|
+
|
|
219
|
+
## π§ͺ Bench
|
|
220
|
+
|
|
221
|
+
[`bench/`](bench/README.md) rebuilds every clip above from recordings freely available on the
|
|
222
|
+
Internet Archive and scores trueflac on them. No audio is committed.
|
|
223
|
+
|
|
224
|
+
## π§βπ» For developers
|
|
225
|
+
|
|
226
|
+
```console
|
|
227
|
+
uv sync
|
|
228
|
+
uv run pytest
|
|
229
|
+
uv run ruff check src tests bench scripts
|
|
230
|
+
uv run mypy src
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
[uv](https://docs.astral.sh/uv/) installs Python 3.13 and the development tools from `uv.lock`.
|
|
234
|
+
Rules, the bench and commit conventions are in [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
235
|
+
|
|
236
|
+
## π οΈ Roadmap
|
|
237
|
+
|
|
238
|
+
- More genuine music in the bench, from more styles and eras.
|
|
239
|
+
- Opus through its own grid, before resampling.
|
|
240
|
+
- Fake high resolution: a CD file blown up to 96 kHz or 24 bits.
|
|
241
|
+
|
|
242
|
+
## π References
|
|
243
|
+
|
|
244
|
+
Detection of lossy coding in decoded audio:
|
|
245
|
+
|
|
246
|
+
- J. Herre, M. Schug, *Analysis of Decompressed Audio: The "Inverse Decoder"*, AES 109th
|
|
247
|
+
Convention, paper 5256, 2000. [AES E-Library](https://aes.org/publications/elibrary-page/?id=9082)
|
|
248
|
+
- S. Moehrs, J. Herre, R. Geiger, *Analysing Decompressed Audio with the "Inverse Decoder":
|
|
249
|
+
Towards an Operative Algorithm*, AES 112th Convention, paper 5576, 2002.
|
|
250
|
+
[AES E-Library](https://aes.org/publications/elibrary-page/?id=11346)
|
|
251
|
+
- J. Lacroix, Y. Prime, A. Remy, O. Derrien, *Lossless Audio Checker: A Software for the
|
|
252
|
+
Detection of Upscaling, Upsampling, and Transcoding in Lossless Musical Tracks*, AES 139th
|
|
253
|
+
Convention, 2015. [AES E-Library](https://aes.org/publications/elibrary-page/?id=17972)
|
|
254
|
+
- O. Derrien, *Detection of Genuine Lossless Audio Files: Application to the MPEG-AAC Codec*,
|
|
255
|
+
Journal of the Audio Engineering Society, vol. 67, no. 3, pp. 116β123, 2019.
|
|
256
|
+
[AES E-Library](https://aes.org/publications/elibrary/elibrary-page/?id=19892)
|
|
257
|
+
|
|
258
|
+
trueflac follows the idea of these papers (look for the coder's transform grid in the decoded
|
|
259
|
+
sound); its code is written from the codec specifications below, not from theirs.
|
|
260
|
+
|
|
261
|
+
Codecs:
|
|
262
|
+
|
|
263
|
+
- ISO/IEC 11172-3:1993, *Coding of moving pictures and associated audio, Part 3: Audio* (MPEG-1
|
|
264
|
+
Layer III). The synthesis window was read back from [minimp3](https://github.com/lieff/minimp3)
|
|
265
|
+
(CC0), and the line shapes checked against it.
|
|
266
|
+
- ISO/IEC 13818-7, *Advanced Audio Coding (AAC)*: sine and Kaiser-Bessel derived windows. The
|
|
267
|
+
ffmpeg AAC encoder uses the KBD window with alpha 4 for long blocks
|
|
268
|
+
([libavcodec/aactab.c](https://github.com/FFmpeg/FFmpeg/blob/master/libavcodec/aactab.c)).
|
|
269
|
+
- Xiph.Org, [*Vorbis I specification*](https://www.xiph.org/vorbis/doc/Vorbis_I_spec.html),
|
|
270
|
+
section 4.3.1: the window slope y = sin(Ο/2 Β· sinΒ²((x + 0.5)/n Β· Ο)).
|
|
271
|
+
|
|
272
|
+
Tools:
|
|
273
|
+
|
|
274
|
+
- [flac-detective](https://github.com/Guillain-RDCDE/FLAC_Detective), the reference in the
|
|
275
|
+
results above.
|
|
276
|
+
- [libsndfile](https://libsndfile.github.io/libsndfile/) through
|
|
277
|
+
[soundfile](https://github.com/bastibe/python-soundfile), to read the files.
|
|
278
|
+
|
|
279
|
+
## π€ Contribution
|
|
280
|
+
|
|
281
|
+
Issues and pull requests are welcome: see [CONTRIBUTING.md](CONTRIBUTING.md). A wrong verdict is
|
|
282
|
+
the most useful report. Security problems go through [SECURITY.md](SECURITY.md).
|
|
283
|
+
|
|
284
|
+
## π License
|
|
285
|
+
|
|
286
|
+
MIT, see [LICENSE](LICENSE). trueflac is part of [Opale](https://github.com/opalehq).
|
trueflac-0.1.0/README.md
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# trueflac
|
|
4
|
+
|
|
5
|
+
<picture>
|
|
6
|
+
<source media="(prefers-color-scheme: dark)" srcset="docs/assets/logo-dark.svg">
|
|
7
|
+
<img src="docs/assets/logo-light.svg" alt="trueflac logo" height="72">
|
|
8
|
+
</picture>
|
|
9
|
+
|
|
10
|
+
**Tells whether a FLAC file was made from an MP3, an AAC, an Opus or a Vorbis file.**
|
|
11
|
+
|
|
12
|
+
[](https://github.com/opalehq/trueflac/actions/workflows/ci.yml)
|
|
13
|
+
[](https://pypi.org/project/trueflac/)
|
|
14
|
+

|
|
15
|
+

|
|
16
|
+
|
|
17
|
+
[Why](#-why-trueflac) β’ [Install](#-install) β’ [Command line](#-command-line) β’ [Python](#-python) β’
|
|
18
|
+
[How it works](#-how-it-works) β’ [Results](#-results) β’ [References](#-references) β’
|
|
19
|
+
[Documentation](https://opalehq.github.io/trueflac/)
|
|
20
|
+
|
|
21
|
+
</div>
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## π Why trueflac?
|
|
26
|
+
|
|
27
|
+
A FLAC file promises the sound of the CD. Converting an MP3 to FLAC keeps that promise on the
|
|
28
|
+
label only: the file is bigger, the sound is the MP3's. Music shared online, old downloads and
|
|
29
|
+
"lossless" packs often hold such files, and nothing in their tags says so.
|
|
30
|
+
|
|
31
|
+
trueflac looks at the sound itself and answers **suspect** or **ok** for each file. It never says
|
|
32
|
+
"fake": an old recording or a filtered master can look like a lossy file. When in doubt it keeps
|
|
33
|
+
quiet, so a genuine file is never accused on its word: on its bench, no genuine file is flagged.
|
|
34
|
+
|
|
35
|
+
It is the engine of the fake FLAC check in [S.O.K](https://github.com/opalehq/S.O.K), and works on
|
|
36
|
+
its own as a command and a Python library.
|
|
37
|
+
|
|
38
|
+
## β¨ Features
|
|
39
|
+
|
|
40
|
+
- **Two independent clues.** The highs cut like an encoder does, or the block grid of the encoder
|
|
41
|
+
still in the sound. The second one catches what the first cannot see: MP3 V0 and 320 kbit/s,
|
|
42
|
+
AAC 256, Vorbis.
|
|
43
|
+
- **Names the codec** when it finds its grid: MP3, AAC or Vorbis.
|
|
44
|
+
- **FLAC, WAV and AIFF**, 16 or 24 bits, 44.1 to 96 kHz, mono or stereo.
|
|
45
|
+
- **Read only.** It never writes, moves or deletes a file, and never goes on the network.
|
|
46
|
+
- **Light.** numpy and soundfile, nothing else. About 1.5 s for a genuine track on an Apple
|
|
47
|
+
Silicon Mac, half that on four threads.
|
|
48
|
+
- **Made for applications.** Verdicts are data that an app words in its own language,
|
|
49
|
+
analyses can be canceled, and it is safe to run on several threads.
|
|
50
|
+
|
|
51
|
+
## π₯ Install
|
|
52
|
+
|
|
53
|
+
```console
|
|
54
|
+
pip install trueflac
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Or as a command only, with [uv](https://docs.astral.sh/uv/): `uv tool install trueflac`.
|
|
58
|
+
|
|
59
|
+
Python 3.11 or later. soundfile ships its own libsndfile on Linux, macOS and Windows.
|
|
60
|
+
|
|
61
|
+
## π» Command line
|
|
62
|
+
|
|
63
|
+
```console
|
|
64
|
+
$ trueflac check Album/
|
|
65
|
+
ok 01 - Echoplex (CD).flac No sign of a lossy source
|
|
66
|
+
suspect 02 - Echoplex (from MP3 128).flac Highs cut at 16.6 kHz, like a lossy file
|
|
67
|
+
suspect 03 - Echoplex (from MP3 V0).flac Block grid of a lossy encoder (MP3)
|
|
68
|
+
suspect 04 - Echoplex (from AAC 256).flac Block grid of a lossy encoder (AAC)
|
|
69
|
+
suspect 05 - Echoplex (from Vorbis q6).flac Block grid of a lossy encoder (Vorbis)
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
This is a real run on one clip of the bench (Nine Inch Nails, *The Slip*) and four fakes made
|
|
73
|
+
from it. Only the MP3 128 lost its highs; the three others keep them and are seen through their
|
|
74
|
+
grid.
|
|
75
|
+
|
|
76
|
+
```console
|
|
77
|
+
trueflac check <file or folder>... [--json]
|
|
78
|
+
trueflac --version
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Folders are searched for `.flac`, `.wav`, `.aiff` and `.aif` files. `--json` prints one JSON object
|
|
82
|
+
per file:
|
|
83
|
+
|
|
84
|
+
```console
|
|
85
|
+
$ trueflac check --json "Album/02 - Echoplex (from MP3 128).flac"
|
|
86
|
+
{"path": "Album/02 - Echoplex (from MP3 128).flac", "status": "suspect", "clue": "cut", "codec": null, "cutoff_hz": 16618.24951171875, "grid_contrast": null, "sample_rate": 44100, "bit_depth": 16, "channels": 2, "seconds": 60.0, "error": null}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
The exit code is 1 when a file is suspect, 0 otherwise.
|
|
90
|
+
|
|
91
|
+
## π Python
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
from trueflac import Clue, Status, analyze
|
|
95
|
+
|
|
96
|
+
verdict = analyze("02 - Single.flac")
|
|
97
|
+
if verdict.status is Status.SUSPECT:
|
|
98
|
+
if verdict.clue is Clue.CUT:
|
|
99
|
+
print(f"highs cut at {verdict.cutoff_hz:.0f} Hz")
|
|
100
|
+
else: # Clue.GRID
|
|
101
|
+
print(f"made from {verdict.codec}") # "MP3", "AAC" or "Vorbis"
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
A `Verdict` holds data, not sentences, so an application can word it in its own language:
|
|
105
|
+
|
|
106
|
+
| Field | Meaning |
|
|
107
|
+
| --- | --- |
|
|
108
|
+
| `status` | `Status.OK`, `Status.SUSPECT` or `Status.UNREADABLE` |
|
|
109
|
+
| `clue` | why it is suspect: `Clue.CUT` (the highs stop like after an encoder) or `Clue.GRID` (the encoder's block grid is still there) |
|
|
110
|
+
| `codec` | `"MP3"`, `"AAC"` or `"Vorbis"`, for a grid |
|
|
111
|
+
| `cutoff_hz` | where the highs end, when they end sharply |
|
|
112
|
+
| `grid_contrast` | strength of the best block grid (suspect from 10) |
|
|
113
|
+
| `sample_rate`, `bit_depth`, `channels`, `seconds` | the file |
|
|
114
|
+
| `error` | why the file could not be read |
|
|
115
|
+
|
|
116
|
+
`describe(verdict)` gives the English line the command line prints.
|
|
117
|
+
|
|
118
|
+
`analyze()` never prints, never raises on a broken file (the verdict is `unreadable`) and is safe
|
|
119
|
+
to call from several threads. It takes an optional `cancel` callable, checked between its stages;
|
|
120
|
+
when it returns `True`, `analyze()` raises `Cancelled`:
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
import threading
|
|
124
|
+
from trueflac import Cancelled, analyze
|
|
125
|
+
|
|
126
|
+
stop = threading.Event()
|
|
127
|
+
try:
|
|
128
|
+
verdict = analyze(path, cancel=stop.is_set)
|
|
129
|
+
except Cancelled:
|
|
130
|
+
...
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## π¬ How it works
|
|
134
|
+
|
|
135
|
+
**1. Cut highs.** trueflac averages the spectrum at eight points of the track. An encoder throws
|
|
136
|
+
away the highs it finds inaudible: above its low-pass, a decoded file holds nothing but the
|
|
137
|
+
rounding noise of its bit depth, while a recording, even a 78 rpm disc or a quiet piano, keeps a
|
|
138
|
+
little noise of its own. A sharp fall to that floor below 20.3 kHz is suspect.
|
|
139
|
+
|
|
140
|
+
**2. Codec grid**, when the highs tell nothing. An encoder cuts the sound into blocks, moves each
|
|
141
|
+
block to frequencies with a fixed transform and sets to zero what it finds inaudible. Analyzed
|
|
142
|
+
with the same transform, at the same block start, the decoded file shows those zeros again.
|
|
143
|
+
A recording has no preferred start: every start looks the same. This idea comes from the
|
|
144
|
+
"inverse decoder" of Herre and Schug and from the detectors of Derrien and his co-authors (see
|
|
145
|
+
[References](#-references)).
|
|
146
|
+
|
|
147
|
+
trueflac tries every start:
|
|
148
|
+
|
|
149
|
+
- **MP3**: its 576 lines per granule are rebuilt from the standard (polyphase filter bank, 18-point
|
|
150
|
+
MDCT, alias butterflies) and checked against the minimp3 decoder.
|
|
151
|
+
- **AAC and Vorbis**: a 2048-point MDCT with the windows their encoders use (sine and
|
|
152
|
+
Kaiser-Bessel derived for AAC, the Vorbis power window).
|
|
153
|
+
|
|
154
|
+
A grid counts only when it stands far above all other starts (a contrast of 10, where genuine
|
|
155
|
+
tracks stay under 6) and when two separate parts of the track agree on where it starts.
|
|
156
|
+
|
|
157
|
+
## π Results
|
|
158
|
+
|
|
159
|
+
On a bench of 22 genuine clips (rock, solo piano, 78 rpm transfers) and the same clips encoded and
|
|
160
|
+
decoded back, with [flac-detective](https://github.com/Guillain-RDCDE/FLAC_Detective) 2.6.1 run on
|
|
161
|
+
the same files for reference:
|
|
162
|
+
|
|
163
|
+
| Files flagged (out of 22) | trueflac | flac-detective |
|
|
164
|
+
| --- | --- | --- |
|
|
165
|
+
| **Genuine** (false accusations) | **0** | 0 |
|
|
166
|
+
| MP3 128 / 192 / 256 / 320 kbit/s | **22 / 22 / 21 / 21** | 4 / 5 / 0 / 0 |
|
|
167
|
+
| MP3 V0 | **22** | 0 |
|
|
168
|
+
| AAC 256 (ffmpeg / Apple) | **19 / 18** | 12 / 0 |
|
|
169
|
+
| Opus 192 | **17** | 12 |
|
|
170
|
+
| Vorbis q6 | **22** | 21 |
|
|
171
|
+
| Time per file | **0.9 s** | 2.7 s |
|
|
172
|
+
|
|
173
|
+
Also measured, with no genuine file flagged: decodes with dither or to 24 bits, genuine clips
|
|
174
|
+
with dither, 48 and 96 kHz, mono, 3-second clips, WAV and AIFF. Times are on an Apple Silicon Mac
|
|
175
|
+
(average over the bench; a genuine track takes the longest, about 1.5 s, since every test runs).
|
|
176
|
+
|
|
177
|
+
**Known limits**
|
|
178
|
+
|
|
179
|
+
- **Opus** is only seen through its cut: it works at 48 kHz, and the resampling back to 44.1 kHz
|
|
180
|
+
blurs its grid.
|
|
181
|
+
- **Noise-shaped dither** fills the highs and can hide an AAC (Apple AAC: 11 of 22).
|
|
182
|
+
- **Very quiet highs** (solo piano) leave little to measure.
|
|
183
|
+
- **Lossy at the source**: a phone recording or a master made from MP3 is reported suspect, and
|
|
184
|
+
rightly so, even when the shop sells it as lossless.
|
|
185
|
+
- A bench of 22 clips is small. Report a wrong verdict: it is how the bench grows.
|
|
186
|
+
|
|
187
|
+
## π Documentation
|
|
188
|
+
|
|
189
|
+
The full guide, how the detection works in detail, every result, the sources and the Python API
|
|
190
|
+
reference are on **[opalehq.github.io/trueflac](https://opalehq.github.io/trueflac/)**.
|
|
191
|
+
|
|
192
|
+
## π§ͺ Bench
|
|
193
|
+
|
|
194
|
+
[`bench/`](bench/README.md) rebuilds every clip above from recordings freely available on the
|
|
195
|
+
Internet Archive and scores trueflac on them. No audio is committed.
|
|
196
|
+
|
|
197
|
+
## π§βπ» For developers
|
|
198
|
+
|
|
199
|
+
```console
|
|
200
|
+
uv sync
|
|
201
|
+
uv run pytest
|
|
202
|
+
uv run ruff check src tests bench scripts
|
|
203
|
+
uv run mypy src
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
[uv](https://docs.astral.sh/uv/) installs Python 3.13 and the development tools from `uv.lock`.
|
|
207
|
+
Rules, the bench and commit conventions are in [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
208
|
+
|
|
209
|
+
## π οΈ Roadmap
|
|
210
|
+
|
|
211
|
+
- More genuine music in the bench, from more styles and eras.
|
|
212
|
+
- Opus through its own grid, before resampling.
|
|
213
|
+
- Fake high resolution: a CD file blown up to 96 kHz or 24 bits.
|
|
214
|
+
|
|
215
|
+
## π References
|
|
216
|
+
|
|
217
|
+
Detection of lossy coding in decoded audio:
|
|
218
|
+
|
|
219
|
+
- J. Herre, M. Schug, *Analysis of Decompressed Audio: The "Inverse Decoder"*, AES 109th
|
|
220
|
+
Convention, paper 5256, 2000. [AES E-Library](https://aes.org/publications/elibrary-page/?id=9082)
|
|
221
|
+
- S. Moehrs, J. Herre, R. Geiger, *Analysing Decompressed Audio with the "Inverse Decoder":
|
|
222
|
+
Towards an Operative Algorithm*, AES 112th Convention, paper 5576, 2002.
|
|
223
|
+
[AES E-Library](https://aes.org/publications/elibrary-page/?id=11346)
|
|
224
|
+
- J. Lacroix, Y. Prime, A. Remy, O. Derrien, *Lossless Audio Checker: A Software for the
|
|
225
|
+
Detection of Upscaling, Upsampling, and Transcoding in Lossless Musical Tracks*, AES 139th
|
|
226
|
+
Convention, 2015. [AES E-Library](https://aes.org/publications/elibrary-page/?id=17972)
|
|
227
|
+
- O. Derrien, *Detection of Genuine Lossless Audio Files: Application to the MPEG-AAC Codec*,
|
|
228
|
+
Journal of the Audio Engineering Society, vol. 67, no. 3, pp. 116β123, 2019.
|
|
229
|
+
[AES E-Library](https://aes.org/publications/elibrary/elibrary-page/?id=19892)
|
|
230
|
+
|
|
231
|
+
trueflac follows the idea of these papers (look for the coder's transform grid in the decoded
|
|
232
|
+
sound); its code is written from the codec specifications below, not from theirs.
|
|
233
|
+
|
|
234
|
+
Codecs:
|
|
235
|
+
|
|
236
|
+
- ISO/IEC 11172-3:1993, *Coding of moving pictures and associated audio, Part 3: Audio* (MPEG-1
|
|
237
|
+
Layer III). The synthesis window was read back from [minimp3](https://github.com/lieff/minimp3)
|
|
238
|
+
(CC0), and the line shapes checked against it.
|
|
239
|
+
- ISO/IEC 13818-7, *Advanced Audio Coding (AAC)*: sine and Kaiser-Bessel derived windows. The
|
|
240
|
+
ffmpeg AAC encoder uses the KBD window with alpha 4 for long blocks
|
|
241
|
+
([libavcodec/aactab.c](https://github.com/FFmpeg/FFmpeg/blob/master/libavcodec/aactab.c)).
|
|
242
|
+
- Xiph.Org, [*Vorbis I specification*](https://www.xiph.org/vorbis/doc/Vorbis_I_spec.html),
|
|
243
|
+
section 4.3.1: the window slope y = sin(Ο/2 Β· sinΒ²((x + 0.5)/n Β· Ο)).
|
|
244
|
+
|
|
245
|
+
Tools:
|
|
246
|
+
|
|
247
|
+
- [flac-detective](https://github.com/Guillain-RDCDE/FLAC_Detective), the reference in the
|
|
248
|
+
results above.
|
|
249
|
+
- [libsndfile](https://libsndfile.github.io/libsndfile/) through
|
|
250
|
+
[soundfile](https://github.com/bastibe/python-soundfile), to read the files.
|
|
251
|
+
|
|
252
|
+
## π€ Contribution
|
|
253
|
+
|
|
254
|
+
Issues and pull requests are welcome: see [CONTRIBUTING.md](CONTRIBUTING.md). A wrong verdict is
|
|
255
|
+
the most useful report. Security problems go through [SECURITY.md](SECURITY.md).
|
|
256
|
+
|
|
257
|
+
## π License
|
|
258
|
+
|
|
259
|
+
MIT, see [LICENSE](LICENSE). trueflac is part of [Opale](https://github.com/opalehq).
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Bench
|
|
2
|
+
|
|
3
|
+
Rebuilds a set of genuine clips and lossy-made fakes, then scores the detector.
|
|
4
|
+
Nothing in `bench/data/` is committed.
|
|
5
|
+
|
|
6
|
+
```
|
|
7
|
+
uv run bench/build.py # ~4 GB, needs ffmpeg (libmp3lame, libopus, aac_at) and oggenc
|
|
8
|
+
uv run bench/score.py
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
- **Genuine**: 22 clips of 60 s, streamed from the Internet Archive and converted to
|
|
12
|
+
44.1 kHz/16 bits: 10 from Nine Inch Nails, *The Slip* (CC BY-NC-SA 3.0), 6 from Kimiko
|
|
13
|
+
Ishizaka, *The Open Goldberg Variations* (CC0), and 6 transfers of 78 rpm discs from the
|
|
14
|
+
[Great 78 Project](https://great78.archive.org/). The list is `SOURCES` in `build.py`.
|
|
15
|
+
- **Fakes**: each genuine clip encoded with MP3 128/192/256/320/V0 (LAME), AAC 256 (ffmpeg
|
|
16
|
+
and Apple), Opus 192, Vorbis q6, then decoded back to FLAC 44.1/16 by ffmpeg (no dither).
|
|
17
|
+
- **Other decoders** (`*_tpdf`, `*_shaped`, `*_24bit`): the same fakes decoded with
|
|
18
|
+
triangular dither, noise-shaped dither (shibata) or to 24 bits, as players and
|
|
19
|
+
converters do.
|
|
20
|
+
- **Dithered genuine** (`genuine_tpdf`, `genuine_shaped`): genuine clips with a small
|
|
21
|
+
gain change and dither, as a CD master leaves them. Every `genuine*` group counts
|
|
22
|
+
for false accusations.
|
|
23
|
+
|
|
24
|
+
- **Other formats** (`extra/`, 8 clips each): genuine clips at 48 kHz/24 bits,
|
|
25
|
+
96 kHz/24 bits, mono, 3 seconds, WAV and AIFF, and fakes in the same shapes
|
|
26
|
+
(MP3 at 48 kHz, blown up to 96 kHz, mono, 3 s, WAV; AAC at 48 kHz, AIFF; Opus
|
|
27
|
+
decoded at 48 kHz).
|
|
28
|
+
|
|
29
|
+
Known limit: noise-shaped dither fills the highs and hides a cut; the codec grid
|
|
30
|
+
still finds the MP3 (MP3 192 shaped: 22 of 22) but not always the AAC (Apple AAC
|
|
31
|
+
shaped: 11 of 22).
|
|
32
|
+
|
|
33
|
+
A genuine clip marked suspect is a false accusation: the target is zero.
|