gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Audio > XM instruments - solved!

#40923 - Quirky - Sat Apr 23, 2005 3:54 pm

I've implemented a basic XM player that does mostly what I need - play some MIDI files converted to XM using modplug tracker - but I have a couple of doubts regarding samples and instruments...

In Mods, the samples are stored at the default rate of 8363 Hz for C4 (or C2, or "middle C"). In the XM format, things are a bit more complex; you have an 'instrument' that contains samples. For example you have instrument 1 - "Strings" - that may have the samples "String.1" "String.2" etc. Each sample has a signed byte finetune value and relative note value. Then in the pattern, you have an entry that would be "note 64, instrument 1".

In order for the track to sound correct, I have to offset the given pattern note by the sample's relative note - I only seem to need sample 1 in each instrument (??). But occasionaly it sounds wonky and I imagine it's because of the one sample fudge.

For example, note 64 (E 4)comes up in the pattern, the sample has a relative note of 10, so I play note 64+10 = 74 (or 'D 5'). I have a LUT that uses equal temperament semitones (C4 = 261.1Hz) converted to the increment value for the mixer.
The table is built externally using:

Code:
table[i] = ((note_in_hz*default_sample_freq/middle_C_in_hz)<<12 ) / mixer_freq

where mixer_freq = 18157
middle_C = 261.6
default_sample_freq = 8363
note_in_hz = the frequency of this note (0-127 on the midi scale)


My question is: why are there multiple samples in each instrument? And how does the finetune value fit in? I imagine that it's something like "If the note is in the range of a sample, then play that sample, else play the next higher up sample" and so on - so rather than offsetting the pattern note by 10, I would play the sample that covers that range. But I haven't found any docs that really explain it. Can anyone shed some light on the subject?

EDIT: Well it seems that Modplug Tracker adds in extra samples that aren't actually used - it groups them together in the Windows program and just outputs the group to the XM file whether the samples are used or not. Fair enough.

And the finetune is straightforward too: I saw Deku's post lower down where he interpolates between 2 notes, copied the idea, and BAM! everything sounds spot on. Wonderful.

#50329 - pan69 - Mon Aug 08, 2005 7:19 am

It's good that you post your findings. I think more people should do things like this instead of writing elaborated tutorials for the beginners which we don't need. This stuff is valuable!

So cheers to you!

#54454 - thegamefreak0134 - Mon Sep 19, 2005 5:22 pm

Ahhh... MIDI files are so fun to work with. Yes, I believe you are right about the multiple sample per instrument thingy. A typical XG midi card (refering to the most common, the MS wavetable) has several samples per instrument I guess because it sounds wierd (or "wonky" as you put it) when you modify one sample too much for the note. The best way to figure out the changes is to fire up your favorite sequencer and play all the notes for each instrument listening for the changes.

Also note that with some MIDI formats of sound it is possible to include your own samples.

You are writing a MIDI player for the GBA correct? This sounds awesome, and I would like to see it when finished.

If I soundwrong on a subject, please tell me. I work with midi almost constantly, but i still may be shakey on the basics of the hardware.
_________________
What if the hokey-pokey really is what it's all about?

[url=http:/www.darknovagames.com/index.php?action=recruit&clanid=1]Support Zeta on DarkNova![/url]