Use embedded loop tags as the first choice and add a lossless trim command - #77
Closed
Xehanort88 wants to merge 3 commits into
Closed
Xehanort88 wants to merge 3 commits into
Xehanort88 wants to merge 3 commits into
Conversation
Tests generate their own audio: an intro followed by a repeated 8-note pattern, so the correct loop points are known in advance and loop detection can be checked for correctness, not just for running. Covers audio loading, loop detection and scoring, zero-crossing snapping, split/extend/tag/txt exports, the interactive picker, batch file discovery and every CLI command. Known bugs are marked as strict xfail, so each fix is flagged until its marker is removed. Also adds pytest as a dev dependency and a CI workflow running the tests on Ubuntu and Windows with Python 3.10 and 3.13.
- Mono tracks were played and exported louder than the original: the analysis-only normalization was applied in place to the audio array that is also used for playback, since to_mono returns its input as-is for mono audio. - Longer loops were never preferred among near-identical scores: _prioritize_duration ran before loop_start/loop_end were set, so every duration it compared was 0. - Loops starting in the first seconds of a track were underscored: the truncated look-behind window was zero-padded on the side nearest the loop point, where the weights are heaviest. - Interactive mode discarded the choice made after 'more', 'all' or 'reset' and prompted again. - extend with fade_length=0 crashed (x[-0:] selects the whole array). - export_tags() without output_dir used the file path as the directory. - The txt export message named loop.txt instead of loops.txt.
…mmand Embedded loop tags: - Loop points already stored in a file's metadata (LOOP_START/LOOP_END, LOOPSTART/LOOPLENGTH and the other known tag names) are now returned first by find_loop_pairs, ahead of the detected candidates. They are still returned if detection finds no loop, and are skipped when an approximate loop position is given or when they are out of range. - LoopPair gains a from_metadata flag; interactive mode shows these points as "from tags" and play reports them as such. - New --ignore-tags option to skip them. trim command: - Cuts the audio a given number of samples (--keep-after) after the loop end, keeping the original format, bit depth and tags. - WAV and FLAC are rewritten bit-exact from their native sample format. - Ogg Vorbis is cut at the container level without re-encoding: the audio packets after the one containing the end sample are dropped and the granule position of the final page is lowered to the exact end, so decoders that only trim the last packet (e.g. FFmpeg) also end on the exact sample. Falls back to a page-level cut if the packet durations cannot be determined. - Other formats are rejected, since trimming them would require lossy re-encoding. Tests cover both features; an optional test checks trimmed Ogg files with FFmpeg as an independent decoder when it is available.
3 tasks done
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two related features for tracks that already have loop metadata, which is common for game music.
Embedded loop tags as the first choice
If a file already has loop tags (
LOOP_START/LOOP_END,LOOPSTART/LOOPLENGTH, or any other name recognized by the tag auto-detection from #66), those loop points now come first in every command (play,export-points,tag,split-audio,extend,trim), ahead of the detected ones.-istill lets you pick another one. The tagged loop shows as "from tags" in the interactive table, andplayreports it as such.LoopNotFoundError.--approx-loop-positionis given (the user asked for loops near a specific position) and when they fall outside the audio (with a warning).--ignore-tagsoption to skip them. From Python:find_loop_pairs(use_embedded_tags=False)andread_embedded_loop_pair().LoopPairgains afrom_metadatafield (defaults toFalse, so existing code is unaffected).trimcommand: cut after the loop end without re-encodingpymusiclooper -i trim --path "TRACK.ogg" --keep-after 500Writes
TRACK-trimmed.ext, keeping everything up to the loop end plus--keep-after Nsamples (default 0), with the original format, bit depth and tags.pymusiclooper/ogg.py)How the Ogg Vorbis cut works: pages before the cut are copied byte for byte. On the last kept page, the audio packets after the one containing the end sample are dropped, then its granule position is lowered to the exact end sample and the page is flagged as end of stream. The Vorbis spec (section A.2) defines this as how to end a stream on a sample that isn't on a block boundary. Dropping the extra packets keeps the trimmed samples within the final packet, the same layout encoders produce. That matters because FFmpeg only trims within the last packet. Packet durations come from the mode block flags at the end of the setup header, found by scanning it backwards the same way FFmpeg's
vorbis_parser.cand liboggz do, and they're checked against the page granule positions. If they don't add up, the cut falls back to page granularity, which is still valid per the spec.extend's existing tag-copying code is moved into a shared_copy_tagshelper, whichtrimreuses.Test plan
uv run pytest: 72 passed (20 new tests covering embedded tags, bit-exact WAV/FLAC/Ogg trims, tag preservation, the Ogg packet durations and fallback, and rejection of unsupported formats)tests/test_ogg_ffmpeg.pydecodes trimmed Ogg files with FFmpeg as an independent decoder. It usesPML_FFMPEG,tools/ffmpeg/orPATH, and skips when FFmpeg is missing or too old to honor Vorbis end trimming. It passes with FFmpeg 7.0.2 and 9.0.2.