awc_opus.py: OpusHead pre-skip is 0, so exported .ogg starts with encoder priming and loses the real tail (312 samples with libopus)

Abierto
#1,392 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
Medio día
Aptitud para principiantes
42/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
python
Área
cli, data

Línea de trabajo

Start in tools/awc_opus.py around line 83, where OpusHead is packed with pre-skip 0, and in write_ogg_opus where granule positions are computed. Reproduce by encoding a sine with libopus, extracting packets, and checking the decoded offset against the reference. Done when the header carries the encoder pre-skip and granule positions include it, with the trimmed last granule as min(granule, pre_skip + length).

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

What happens

awc_opus.py writes OpusHead with pre-skip 0, so every exported .ogg plays the encoder's priming samples at the start, and the end trim (length) then cuts the same amount of real audio off the end. with a libopus stream that's 312 samples (6.5 ms) shifted on both ends.

Commit

c2967a5e (main)

System

Linux, Python 3.10, ffmpeg 4.4 (libopus). no game files.

Where

A tool run by hand (tools/, scripts/)

Command and steps

tools/awc_opus.py:83:

head = b"OpusHead" + struct.pack("<BBHIhB", 1, channels, 0, 48000, 0, 0)
#                                                       ^ pre-skip = 0

every Opus encoder has lookahead, so its first ~312 output samples (libopus default) are priming and not audio. RFC 7845 §4.2 says the decoder drops pre-skip samples, and the granule positions count them too (§4.4: last granule = pre-skip + real samples).

repro without game data: encode a 1 s sine with libopus, pull the raw packets out of the ogg, and push them through write_ogg_opus(..., length=48000):

ref pre-skip 312, 51 packets
decoded samples: ref 48000, ours 48000
first non-silent sample: ref 5, ours 317
best alignment of ours vs ref: shifted by 312 samples

so the length matches, but the content is 312 samples late and the last 312 real samples are gone.

Fix (probably)
  • write the real pre-skip into OpusHead, and add it to every granule position (including the trimmed last one: min(granule, pre_skip + length))
  • where does the value come from: for the music streams the per-channel skip already drops whole repeated packets, which is a different thing. for plain streams the AWC might store the encoder delay somewhere (rpf9-explorer's OpusPreview would be the place to check). if it doesn't, 312 (libopus at 48 kHz) is the safe default, since that's what any libopus encoder would have used.

i couldn't check what the game's own encoder used without game files, so whoever has a bank could compare one sfx's onset against the in-game sound, or check whether its packet count × frame size exceeds samples by about 312.

Output

n/a (wrong result, no error)


TL;DR: exported game audio (.ogg) starts about 6.5 ms late and loses the same amount at the end, because the Opus header says pre-skip 0. fix: write the encoder's pre-skip (probably 312) and count it in the granule positions. shown with a libopus round-trip, not yet with real game banks. AI-assisted (Claude Code).

Lenguaje dominante
Python
Estrellas
14.9k
Forks
1.2k
Merge medio
1 h 58 min
PR fusionados (30 d)
40

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de ItsNotRexed/Leonida

Todos los issues de ItsNotRexed/Leonida

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.