Skip to content

Fix ADPCM encoder buffer overflow causing silent AWC build failure#10

Open
Prompt-Coder wants to merge 1 commit intoRenewed-Scripts:mainfrom
Prompt-Coder:fix/adpcm-encoder-buffer-overflow
Open

Fix ADPCM encoder buffer overflow causing silent AWC build failure#10
Prompt-Coder wants to merge 1 commit intoRenewed-Scripts:mainfrom
Prompt-Coder:fix/adpcm-encoder-buffer-overflow

Conversation

@Prompt-Coder
Copy link
Copy Markdown

Problem

Building an AWC resource with certain audio files (especially 48 kHz sources or larger banks) silently fails — Audiotool retries 5 times then shows:

Failed to build AWC with Codewalker in 5 attempts! Please manually build

No error details. The real exception is swallowed by the empty catch (Exception) in BuildAWC.

Root cause

CodeWalker.Core's ADPCMCodec.EncodeADPCM allocates its output buffer as data.Length / 4 and processes input in 8192-byte blocks. When the PCM data chunk is not a multiple of 8192 bytes, the final partial block writes a 4-byte block header past the end of the output buffer → IndexOutOfRangeException.

AwcFile.ReadXml catches that exception silently, leaving DataChunk.Data null. BuildPeakChunks then NREs on data.Length, which is what the user sees as "Object reference not set to an instance of an object".

Fix

WavConverter.cs — after each WAV is written, pad the PCM data chunk to the next 8192-byte boundary with zero (silent) bytes before CodeWalker reads it. Also recomputes audio.Samples from the actual post-padding byte count so the generated <Samples> value in the XML stays accurate.

AWCBuilder.cs — replace the empty catch (Exception) with one that logs the full exception type, message, and stack trace to awc_build_error.log and shows the error text in the failure dialog. Silently swallowing the exception made every AWC build error impossible to diagnose.

Testing

Verified against a 25-stream, 48 kHz audio bank that previously failed on every build attempt. After the fix it builds cleanly on the first try.

CodeWalker's ADPCMCodec.EncodeADPCM sizes its output buffer as
data.Length / 4 and processes input in 8192-byte blocks. When the PCM
data chunk is not a multiple of 8192 bytes the final partial block
writes a 4-byte block header past the end of the output buffer, raising
IndexOutOfRangeException. That exception is swallowed by AwcFile.ReadXml,
leaving DataChunk.Data null; BuildPeakChunks then NREs on data.Length,
showing the user "Object reference not set to an instance of an object".

Fix 1 (WavConverter): pad every converted WAV's PCM data chunk to the
next 8192-byte boundary with zero (silent) bytes before passing it to
CodeWalker. Recompute audio.Samples from the actual padded byte count so
the XML <Samples> value stays accurate.

Fix 2 (AWCBuilder): replace the empty catch(Exception) with one that logs
the full exception type, message, and stack trace to awc_build_error.log
and surfaces the error text in the failure dialog. Previously all build
errors were silently swallowed, forcing users to "manually build" with no
indication of what went wrong.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant