View Single Post
  #4  
Old February 18th, 2008, 12:34 AM
lch's Avatar

lch lch is offline
General
 
Join Date: Feb 2007
Location: R'lyeh
Posts: 3,861
Thanks: 144
Thanked 403 Times in 176 Posts
lch is on a distinguished road
Default Music modding solved

Somebody asked about this on another forum which made me curious to have a look at the problem again, wasted 3 hours of sleep on this... Dealing with the programming from the game before, I expected that it would probably be faithful to the specs - JK is pretty straight forward in this regard, and that all those programs that I used for a-law encoding were to blame for not really writing raw data.

Turns out that my hunch was right. Following a link from the Wikipedia article on A-law encoding (Finland is in Europe, and *.al is a suggestive filename) I had a look at the compression tutorial at http://hazelware.luggle.com/tutorial...mpression.html and then looked if somebody had already implemented something in that direction since I didn't want to write a C/C++ program from scratch. Then I found the audioop module in Python which has A-law since Python 2.5, so I only had to learn Python which was easier than expected with copy&paste from examples. Here's the straight-forward approach to playback the game audio files:

Code:
#! /bin/env python
import audioop
alaw_data = open('/opt/dominions3/rawsound/draam13.al', 'r')
pcm = audioop.alaw2lin(alaw_data.read(), 2)

from ossaudiodev import open as ossOpen
dsp = ossOpen('/dev/dsp','w')
try:
from ossaudiodev import AFMT_S16_NE
except ImportError:
if byteorder == "little":
AFMT_S16_NE = ossaudiodev.AFMT_S16_LE
else:
AFMT_S16_NE = ossaudiodev.AFMT_S16_BE

dsp.setparameters(AFMT_S16_NE, 2, 11025)

dsp.write(pcm)
dsp.close()


I'm not sure if the game audio files are mono at 22050 Hz or stereo at 11025 Hz, I guess it's the latter. The playback is pretty faithful to in-game.

If you convert your source material to raw PCM data, then you can use the following Python script to convert it to raw A-law data:

Code:
#! /bin/env python

import sys, string
if len(sys.argv)!= 2:
print "Usage: "+sys.argv[0]+" <filename>"
sys.exit()
else:
inputfile = sys.argv[1]
name1 = str.split(inputfile, '.')
outputfile = string.join(name1[:len(name1)-1])+'.al'

import audioop
pcm_data = open(inputfile, 'r')
alaw_data = audioop.lin2alaw(pcm_data.read(), 2)

alaw = open(outputfile, 'w')
alaw.write(alaw_data)


Now I looked for some demo input, MP3s on the net and found some Old Tatar Songs which are already at 11kHz. Converting them to *.al format:
Code:
$ mpg123 -v -y --stereo --rate 11025 -w kukkuger.pcm kukkuger.mp3
$ python2.5 convert.py kukkuger.pcm


Then copied it over /opt/dominions3/rawsound/draam13.al - sounds good!
Here's the demo file: http://www.mediafire.com/?9h2xnxprb1t

Somebody make a better Python program out of this, maybe with a nice GUI. I learned Python just 2 hours ago!
__________________
Come to the Dom3 Wiki and help us to build the biggest Dominions-centered knowledge base on the net.
Visit my personal user page there, too!
Pretender file password recovery
Emergency comic relief
Reply With Quote