Stumped - Decoding Tinsel/Discworld 2 Music - Python Script

General chat related to ScummVM, adventure gaming, and so on.

Moderator: ScummVM Team

Post Reply
User avatar
nivardus
Posts: 3
Joined: Thu Oct 07, 2010 1:24 am

Stumped - Decoding Tinsel/Discworld 2 Music - Python Script

Post by nivardus »

Threads have been posted on this topic before; however, I've yet to find the fruits of anyone's labors and have written an ugly python script to decode a Discworld 2 music file and convert it into uncompressed *.wav

Unfortunately, my implimentation of the decoder from Scummvm's adpcm.cpp generates a skewed output with lots of outliers:

Image

(Wave seems offset from the y-axis, and there should be much less rubbish exceeding 32767 (int16 limit) on the same axis)

The script, with decoding loop highlighted: http://pastebin.com/7Apu6uQ5

Scummvm isn't a Python project (clearly) but I've stumped myself for a couple days: over something that's clearly a small fix. If the flaw is obvious to anyone I'd appreciate input. The script is free for anyone to use. Thanks :D
User avatar
bobdevis
Posts: 567
Joined: Fri Jan 16, 2009 10:52 am

Re: Stumped - Decoding Tinsel/Discworld 2 Music - Python Scr

Post by bobdevis »

nivardus wrote:Wave seems offset from the y-axis,
The way the y-offset is bad in your graph suggests a conversion mistake between unsigned and two's complement. Especially the middle part where zero tends to be the floor value instead of the average value.

8bit/sample wav's use unsigned values and 16bit/sample wav's use two's complement values.

Great reading material about wav's
http://www.lightlink.com/tjweber/StripWav/WAVE.html
http://www.lightlink.com/tjweber/StripWav/Canon.html

Explanation of unsigned and two's complement
http://en.wikipedia.org/wiki/Two's_complement
http://www.rwc.uc.edu/koehler/comath/13.html

-----

If you want to convert 8bit/sample to 16bit/sample, preferably use a 'real' audio editor (like Audacity) and not just a byte-by-byte script.
Audacity will will not only take care of the signed/unsigned problem but also take into account the shape of the sound wave, getting you a much smoother result.

Just figure out how to output an 8bit/sample wav with Python and then convert to 16bit/sample with Audacity.
Last edited by bobdevis on Thu Oct 07, 2010 8:43 am, edited 1 time in total.
User avatar
nivardus
Posts: 3
Joined: Thu Oct 07, 2010 1:24 am

Post by nivardus »

Thanks, bobdevis. This is great reading material: I will probably need to trace down the problem among python's implicit types and casting.

A point of clarification: the graph is the raw sample output from the conversion, and not from the encoded *.WAV. (The error lies in the decoding of the ADPCM data.)
User avatar
bobdevis
Posts: 567
Joined: Fri Jan 16, 2009 10:52 am

Post by bobdevis »

nivardus wrote: A point of clarification: the graph is the raw sample output from the conversion, and not from the encoded *.WAV. (The error lies in the decoding of the ADPCM data.)
This may or may not be relevant. Officially wav is just a container format. In many cases it can be just PCM with a header slapped on it.

In any case, it's hard to see what is going on exactly because you use external tools to format the file that I am not familiar with.

So yeah. Either you find out exactly what those tools do, or you manually format your output 8bit/sample canonical wav file, byte for byte.
Seldon
Posts: 101
Joined: Sun Feb 24, 2008 9:53 am
Location: Poland

Re: Stumped - Decoding Tinsel/Discworld 2 Music - Python Scr

Post by Seldon »

nivardus wrote:Threads have been posted on this topic before; however, I've yet to find the fruits of anyone's labors
You missed that one :)
User avatar
nivardus
Posts: 3
Joined: Thu Oct 07, 2010 1:24 am

Re: Stumped - Decoding Tinsel/Discworld 2 Music - Python Scr

Post by nivardus »

Seldon wrote:You missed that one :)
Yep I did 8)
Files converted.
Post Reply