Index: uspace/app/modplay/modplay.c
===================================================================
--- uspace/app/modplay/modplay.c	(revision 18cc83cf2ce837ccf57cc87f761ed36d8926b52f)
+++ uspace/app/modplay/modplay.c	(revision 43dd72b76b663dd908ac9a1b5c56863eca638ef3)
@@ -40,5 +40,4 @@
 #include <stdio.h>
 #include <stdlib.h>
-#include <protracker.h>
 #include <trackmod.h>
 
@@ -88,5 +87,5 @@
 	con = console_init(stdin, stdout);
 
-	rc = trackmod_protracker_load(argv[1], &mod);
+	rc = trackmod_module_load(argv[1], &mod);
 	if (rc != EOK) {
 		printf("Error loading %s.\n", argv[1]);
Index: uspace/lib/c/include/stddef.h
===================================================================
--- uspace/lib/c/include/stddef.h	(revision 18cc83cf2ce837ccf57cc87f761ed36d8926b52f)
+++ uspace/lib/c/include/stddef.h	(revision 43dd72b76b663dd908ac9a1b5c56863eca638ef3)
@@ -42,4 +42,5 @@
 #endif
 
+#define offsetof(type,member) ((size_t) &(((type *) 0)->member))
 
 #endif
Index: uspace/lib/trackmod/Makefile
===================================================================
--- uspace/lib/trackmod/Makefile	(revision 18cc83cf2ce837ccf57cc87f761ed36d8926b52f)
+++ uspace/lib/trackmod/Makefile	(revision 43dd72b76b663dd908ac9a1b5c56863eca638ef3)
@@ -32,5 +32,6 @@
 SOURCES = \
 	protracker.c \
-	trackmod.c
+	trackmod.c \
+	xm.c
 
 include $(USPACE_PREFIX)/Makefile.common
Index: uspace/lib/trackmod/protracker.c
===================================================================
--- uspace/lib/trackmod/protracker.c	(revision 18cc83cf2ce837ccf57cc87f761ed36d8926b52f)
+++ uspace/lib/trackmod/protracker.c	(revision 43dd72b76b663dd908ac9a1b5c56863eca638ef3)
@@ -113,4 +113,140 @@
 }
 
+
+/** Decode pattern cell.
+ *
+ * @param pattern Pattern
+ * @param row     Row number
+ * @param channel Channel number
+ * @param cell    Place to store decoded cell
+ */
+static void protracker_decode_cell(uint32_t cdata, trackmod_cell_t *cell)
+{
+	uint32_t code;
+
+	code = uint32_t_be2host(cdata);
+	cell->period = (code >> (4 * 4)) & 0xfff;
+	cell->instr = (((code >> (7 * 4)) & 0xf) << 4) |
+	    ((code >> (3 * 4)) & 0xf);
+	cell->effect = code & 0xfff;
+}
+
+/** Load Protracker patterns.
+ *
+ * @param f      File to read from
+ * @param module Module being loaded to
+ * @return       EOK on success, ENOMEM if out of memory, EIO on I/O error.
+ */
+static int protracker_load_patterns(FILE *f, trackmod_module_t *module)
+{
+	size_t cells;
+	size_t i, j;
+	int rc;
+	size_t nread;
+	uint32_t *buf = NULL;
+
+	cells = module->channels * protracker_pattern_rows;
+	buf = calloc(sizeof(uint32_t), cells);
+
+	if (buf == NULL) {
+		rc = ENOMEM;
+		goto error;
+	}
+
+	for (i = 0; i < module->patterns; i++) {
+		module->pattern[i].rows = protracker_pattern_rows;
+		module->pattern[i].channels = module->channels;
+		module->pattern[i].data = calloc(sizeof(trackmod_cell_t), cells);
+		if (module->pattern[i].data == NULL) {
+			rc = ENOMEM;
+			goto error;
+		}
+
+		nread = fread(buf, sizeof(uint32_t), cells, f);
+		if (nread != cells) {
+			printf("Error reading pattern.\n");
+			rc = EIO;
+			goto error;
+		}
+
+		/* Decode cells */
+		for (j = 0; j < cells; j++) {
+			protracker_decode_cell(buf[j],
+			    &module->pattern[i].data[j]);
+		}
+	}
+
+	free(buf);
+	return EOK;
+error:
+	free(buf);
+	return rc;
+}
+
+/** Load protracker samples.
+ *
+ * @param f      File being read from
+ * @param sample Sample header
+ * @param module Module being loaded to
+ * @return       EOk on success, ENOMEM if out of memory, EIO on I/O error.
+ */
+static int protracker_load_samples(FILE *f, protracker_smp_t *smp,
+    trackmod_module_t *module)
+{
+	int rc;
+	size_t i;
+	uint8_t ftval;
+	size_t nread;
+	trackmod_sample_t *sample;
+
+	for (i = 0; i < module->instrs; i++) {
+		module->instr[i].samples = 1;
+		module->instr[i].sample = calloc(1, sizeof(trackmod_sample_t));
+		if (module->instr[i].sample == NULL) {
+			printf("Error allocating sample.\n");
+			rc = ENOMEM;
+			goto error;
+		}
+
+		sample = &module->instr[i].sample[0];
+		sample->length =
+		    uint16_t_be2host(smp[i].length) * 2;
+		sample->bytes_smp = 1;
+		sample->data = calloc(1, sample->length);
+		if (sample->data == NULL) {
+			printf("Error allocating sample.\n");
+			rc = ENOMEM;
+			goto error;
+		}
+
+		nread = fread(sample->data, 1, sample->length, f);
+		if (nread != sample->length) {
+			printf("Error reading sample.\n");
+			rc = EIO;
+			goto error;
+		}
+
+		sample->def_vol = smp[i].def_vol;
+
+		sample->loop_start =
+		    uint16_t_be2host(smp[i].loop_start) * 2;
+		sample->loop_len =
+		    uint16_t_be2host(smp[i].loop_len) * 2;
+		if (sample->loop_len <= 2)
+			sample->loop_type = tl_no_loop;
+		else
+			sample->loop_type = tl_forward_loop;
+
+		/* Finetune is a 4-bit signed value. */
+		ftval = smp[i].finetune & 0x0f;
+		sample->finetune =
+			(ftval & 0x8) ? (ftval & 0x7) - 8 : ftval;
+	}
+
+	return EOK;
+error:
+	return rc;
+}
+
 /** Load protracker module.
  *
@@ -128,10 +264,9 @@
 	protracker_order_list_t *order_list;
 	protracker_smp_t *sample;
-	size_t nread;
 	size_t samples;
 	size_t channels;
 	size_t patterns;
-	size_t cells;
-	size_t i, j;
+	size_t i;
+	size_t nread;
 	int rc;
 
@@ -192,7 +327,7 @@
 	module->channels = channels;
 
-	module->samples = samples;
-	module->sample = calloc(sizeof(trackmod_sample_t), samples);
-	if (module->sample == NULL) {
+	module->instrs = samples;
+	module->instr = calloc(sizeof(trackmod_instr_t), samples);
+	if (module->instr == NULL) {
 		printf("Out of memory.\n");
 		rc = ENOMEM;
@@ -221,57 +356,23 @@
 	}
 
+	/* The 'mark' byte may or may not contain a valid restart position */
+	if (order_list->mark < order_list->order_list_len) {
+		module->restart_pos = order_list->mark;
+	}
+
 	/* Load patterns */
-
-	cells = channels * protracker_pattern_rows;
-
-	for (i = 0; i < patterns; i++) {
-		module->pattern[i].rows = protracker_pattern_rows;
-		module->pattern[i].channels = channels;
-		module->pattern[i].data = calloc(sizeof(uint32_t), cells);
-
-		nread = fread(module->pattern[i].data,
-		    sizeof(uint32_t), cells, f);
-		if (nread != cells) {
-			printf("Error reading pattern.\n");
-			rc = EIO;
-			goto error;
-		}
-
-		/* Convert byte order */
-		for (j = 0; j < cells; j++) {
-			module->pattern[i].data[j] = uint32_t_be2host(
-			    module->pattern[i].data[j]);
-		}
-	}
+	rc = protracker_load_patterns(f, module);
+	if (rc != EOK)
+		goto error;
 
 	/* Load samples */
-	for (i = 0; i < samples; i++) {
-		module->sample[i].length =
-		    uint16_t_be2host(sample[i].length) * 2;
-		module->sample[i].data = calloc(1, module->sample[i].length);
-		if (module->sample[i].data == NULL) {
-			printf("Error allocating sample.\n");
-			rc = ENOMEM;
-			goto error;
-		}
-
-		nread = fread(module->sample[i].data, 1, module->sample[i].length,
-			f);
-		if (nread != module->sample[i].length) {
-			printf("Error reading sample.\n");
-			rc = EIO;
-			goto error;
-		}
-
-		module->sample[i].def_vol = sample[i].def_vol;
-		module->sample[i].loop_start =
-		    uint16_t_be2host(sample[i].loop_start) * 2;
-		module->sample[i].loop_len =
-		    uint16_t_be2host(sample[i].loop_len) * 2;
-		if (module->sample[i].loop_len <= 2)
-			module->sample[i].loop_len = 0;
-	}
+	rc = protracker_load_samples(f, sample, module);
+	if (rc != EOK)
+		goto error;
 
 	(void) fclose(f);
+
+	module->def_bpm = protracker_def_bpm;
+	module->def_tpr = protracker_def_tpr;
 
 	*rmodule = module;
Index: uspace/lib/trackmod/trackmod.c
===================================================================
--- uspace/lib/trackmod/trackmod.c	(revision 18cc83cf2ce837ccf57cc87f761ed36d8926b52f)
+++ uspace/lib/trackmod/trackmod.c	(revision 43dd72b76b663dd908ac9a1b5c56863eca638ef3)
@@ -40,5 +40,7 @@
 
 #include "macros.h"
+#include "protracker.h"
 #include "trackmod.h"
+#include "xm.h"
 
 /** Tunables */
@@ -52,11 +54,35 @@
 	base_clock = 8363 * 428,
 	/** Maximum sample volume */
-	vol_max = 63,
-	/** Default TPR */
-	def_tpr = 6,
-	/** Default BPM */
-	def_bpm = 125
+	vol_max = 64,
+	/** Minimum period */
+	period_min = 113,
+	/** Maxium period */
+	period_max = 856
 };
 
+/** Table for finetune computation.
+  *
+  * Finetune is a number ft in [-8 .. 7]. The pitch should be adjusted by
+  * ft/8 semitones. To adjust pitch by 1/8 semitone down we can mutiply the
+  * period by 2^(1/12/8) =. 1.0072, one semitone up: 2^-(1/12/8) =. 0.9928,
+  * to adjust by ft/8 semitones, multiply by 2^(-ft/12/8).
+  *
+  * finetune_factor[ft] := 10000 * 2^(-ft/12/8)
+  * res_period = clip(period * fineture_factor[ft+8] / 10000)
+  */
+static unsigned finetune_factor[16] = {
+	10595, 10518, 10443, 10368, 10293, 10219, 10145, 10072,
+	10000,  9928,  9857,  9786,  9715,  9645,  9576,  9507
+};
+
+static unsigned period_table[12 * 8] = {
+     907,900,894,887,881,875,868,862,856,850,844,838,832,826,820,814,
+     808,802,796,791,785,779,774,768,762,757,752,746,741,736,730,725,
+     720,715,709,704,699,694,689,684,678,675,670,665,660,655,651,646,
+     640,636,632,628,623,619,614,610,604,601,597,592,588,584,580,575,
+     570,567,563,559,555,551,547,543,538,535,532,528,524,520,516,513,
+     508,505,502,498,494,491,487,484,480,477,474,470,467,463,460,457
+};
+
 static size_t trackmod_get_next_ord_idx(trackmod_modplay_t *);
 
@@ -70,4 +96,16 @@
 }
 
+/** Destroy instrument.
+ *
+ * @param instr Intrument
+ */
+static void trackmod_instr_destroy(trackmod_instr_t *instr)
+{
+	size_t i;
+
+	for (i = 0; i < instr->samples; i++)
+		trackmod_sample_destroy(&instr->sample[i]);
+}
+
 /** Destroy pattern.
  *
@@ -97,8 +135,8 @@
 
 	/* Destroy samples */
-	if (module->sample != NULL) {
-		for (i = 0; i < module->samples; i++)
-			trackmod_sample_destroy(&module->sample[i]);
-		free(module->sample);
+	if (module->instr != NULL) {
+		for (i = 0; i < module->instrs; i++)
+			trackmod_instr_destroy(&module->instr[i]);
+		free(module->instr);
 	}
 
@@ -114,4 +152,17 @@
 }
 
+int trackmod_module_load(char *fname, trackmod_module_t **rmodule)
+{
+	int rc;
+
+	rc = trackmod_xm_load(fname, rmodule);
+	if (rc == EOK)
+		return EOK;
+
+	rc = trackmod_protracker_load(fname, rmodule);
+	return rc;
+}
+
+
 /** Return current pattern.
  *
@@ -137,14 +188,34 @@
     size_t row, size_t channel, trackmod_cell_t *cell)
 {
-	uint32_t code;
-
-	code = pattern->data[row * pattern->channels + channel];
-	cell->period = (code >> (4 * 4)) & 0xfff;
-	cell->sample = (((code >> (7 * 4)) & 0xf) << 4) |
-	    ((code >> (3 * 4)) & 0xf);
-	cell->effect = code & 0xfff;
-}
-
-/** Process note (period, sample index)
+	*cell = pattern->data[row * pattern->channels + channel];
+}
+
+/** Compute floor(a / b), and the remainder.
+ *
+ * Unlike standard integer division this rounds towars negative infinity,
+ * not towards zero.
+ *
+ * @param a Dividend
+ * @param b Divisor
+ * @param quot Place to store 'quotient' (floor (a/b))
+ * @param rem Place to store 'remainder' (a - floor(a/b) * b)
+ */
+static void divmod_floor(int a, int b, int *quot, int *rem)
+{
+	if (b < 0) {
+		a = -a;
+		b = -b;
+	}
+
+	if (a >= 0) {
+		*quot = a / b;
+		*rem = a % b;
+	} else {
+		*quot = - (-a + (b - 1)) / b;
+		*rem = a - (*quot * b);
+	}
+}
+
+/** Process note (period)
  *
  * @param modplay Module playback
@@ -156,12 +227,75 @@
 {
 	trackmod_chan_t *chan = &modplay->chan[i];
-	size_t smpidx;
-
-	smpidx = (cell->sample - 1) % modplay->module->samples;
-	chan->sample = &modplay->module->sample[smpidx];
+	int period;
+	int pitch;
+	int octave;
+	int opitch;
+
+	if (chan->sample == NULL)
+		return;
+
+	if (cell->period == 0) {
+		pitch = 8 * (cell->note + chan->sample->rel_note) +
+		    chan->sample->finetune;
+		divmod_floor(pitch, 8 * 12, &octave, &opitch);
+
+		if (octave >= 0)
+			period = period_table[opitch] * 8 / (1 << octave);
+		else
+			period = period_table[opitch] * 8 * (1 << (-octave));
+	} else {
+		period = cell->period;
+		period = period *
+		    finetune_factor[chan->sample->finetune + 8] / 10000;
+		if (period > period_max)
+			period = period_max;
+		if (period < period_min)
+			period = period_min;
+	}
+
+	chan->period_new = period;
+}
+
+/** Process instrument number (this is what triggers the note playback)
+ *
+ * @param modplay Module playback
+ * @param i       Channel number
+ * @param cell    Cell
+ */
+static void trackmod_process_instr(trackmod_modplay_t *modplay, size_t i,
+    trackmod_cell_t *cell)
+{
+	trackmod_chan_t *chan = &modplay->chan[i];
+	trackmod_instr_t *instr;
+	size_t iidx;
+	size_t sidx;
+
+	if (cell->instr == 0)
+		return;
+
+	iidx = (cell->instr - 1) % modplay->module->instrs;
+	instr = &modplay->module->instr[iidx];
+	sidx = instr->key_smp[cell->note] % instr->samples;
+	chan->sample = &instr->sample[sidx];
 	chan->smp_pos = 0;
 	chan->lsmp = 0;
-	chan->period = cell->period;
+
 	chan->volume = modplay->chan[i].sample->def_vol;
+}
+
+/** Process keyoff note
+ *
+ * @param modplay Module playback
+ * @param i       Channel number
+ * @param cell    Cell
+ */
+static void trackmod_process_keyoff_note(trackmod_modplay_t *modplay, size_t i)
+{
+	trackmod_chan_t *chan = &modplay->chan[i];
+
+	chan->sample = NULL;
+	chan->period = 0;
+	chan->smp_pos = 0;
+	chan->lsmp = 0;
 }
 
@@ -175,5 +309,5 @@
     uint8_t param)
 {
-	modplay->chan[chan].volume = param & vol_max;
+	modplay->chan[chan].volume = param % (vol_max + 1);
 }
 
@@ -189,4 +323,8 @@
 	size_t next_idx;
 	trackmod_pattern_t *next_pat;
+	unsigned row;
+
+	/* Strangely the parameter is BCD */
+	row = (param >> 4) * 10 + (param & 0xf);
 
 	next_idx = trackmod_get_next_ord_idx(modplay);
@@ -194,5 +332,5 @@
 
 	modplay->pat_break = true;
-	modplay->pat_break_row = param % next_pat->rows;
+	modplay->pat_break_row = row % next_pat->rows;
 }
 
@@ -212,4 +350,190 @@
 }
 
+/** Process Fine volume slide down effect.
+ *
+ * @param modplay Module playback
+ * @param chan    Channel number
+ * @param param   Effect parameter
+ */
+static void trackmod_effect_fine_vol_slide_down(trackmod_modplay_t *modplay,
+    size_t chan, uint8_t param)
+{
+	int nv;
+
+	nv = modplay->chan[chan].volume - param;
+	if (nv < 0)
+		nv = 0;
+	modplay->chan[chan].volume = nv;
+}
+
+/** Process Fine volume slide up effect.
+ *
+ * @param modplay Module playback
+ * @param chan    Channel number
+ * @param param   Effect parameter
+ */
+static void trackmod_effect_fine_vol_slide_up(trackmod_modplay_t *modplay,
+    size_t chan, uint8_t param)
+{
+	int nv;
+
+	nv = modplay->chan[chan].volume + param;
+	if (nv > vol_max)
+		nv = vol_max;
+	modplay->chan[chan].volume = nv;
+}
+
+/** Process Volume slide effect.
+ *
+ * @param modplay Module playback
+ * @param chan    Channel number
+ * @param param   Effect parameter
+ */
+static void trackmod_effect_vol_slide(trackmod_modplay_t *modplay,
+    size_t chan, uint8_t param)
+{
+	if ((param & 0xf0) != 0)
+		modplay->chan[chan].vol_slide = param >> 4;
+	else
+		modplay->chan[chan].vol_slide = -(int)(param & 0xf);
+}
+
+/** Process Volume slide down effect.
+ *
+ * @param modplay Module playback
+ * @param chan    Channel number
+ * @param param   Effect parameter
+ */
+static void trackmod_effect_vol_slide_down(trackmod_modplay_t *modplay,
+    size_t chan, uint8_t param4)
+{
+	modplay->chan[chan].vol_slide = -(int)param4;
+}
+
+/** Process Volume slide up effect.
+ *
+ * @param modplay Module playback
+ * @param chan    Channel number
+ * @param param   Effect parameter
+ */
+static void trackmod_effect_vol_slide_up(trackmod_modplay_t *modplay,
+    size_t chan, uint8_t param4)
+{
+	modplay->chan[chan].vol_slide = param4;
+}
+
+/** Process Fine portamento down effect.
+ *
+ * @param modplay Module playback
+ * @param chan    Channel number
+ * @param param   Effect parameter
+ */
+static void trackmod_effect_fine_porta_down(trackmod_modplay_t *modplay,
+    size_t chan, uint8_t param)
+{
+	int np;
+
+	np = modplay->chan[chan].period + param;
+	if (np > period_max)
+		np = period_max;
+	modplay->chan[chan].period = np;
+}
+
+/** Process Fine portamento up effect.
+ *
+ * @param modplay Module playback
+ * @param chan    Channel number
+ * @param param   Effect parameter
+ */
+static void trackmod_effect_fine_porta_up(trackmod_modplay_t *modplay,
+    size_t chan, uint8_t param)
+{
+	int np;
+
+	np = modplay->chan[chan].period - param;
+	if (np < period_min)
+		np = period_min;
+	modplay->chan[chan].period = np;
+}
+
+/** Process Portamento down effect.
+ *
+ * @param modplay Module playback
+ * @param chan    Channel number
+ * @param param   Effect parameter
+ */
+static void trackmod_effect_porta_down(trackmod_modplay_t *modplay,
+    size_t chan, uint8_t param)
+{
+	modplay->chan[chan].portamento = -(int)param;
+}
+
+/** Process Portamento up effect.
+ *
+ * @param modplay Module playback
+ * @param chan    Channel number
+ * @param param   Effect parameter
+ */
+static void trackmod_effect_porta_up(trackmod_modplay_t *modplay,
+    size_t chan, uint8_t param)
+{
+	modplay->chan[chan].portamento = param;
+}
+
+/** Process Tone portamento effect.
+ *
+ * @param modplay Module playback
+ * @param chan    Channel number
+ * @param param   Effect parameter
+ */
+static void trackmod_effect_tone_porta(trackmod_modplay_t *modplay,
+    size_t chan, uint8_t param)
+{
+	/* Set up tone portamento effect */
+	modplay->chan[chan].portamento = param;
+	if (modplay->chan[chan].period_new != 0)
+		modplay->chan[chan].period_tgt = modplay->chan[chan].period_new;
+
+	/* Prevent going directly to new period */
+	modplay->chan[chan].period_new = 0;
+}
+
+/** Process volume column.
+ *
+ * @param modplay Module playback
+ * @param chan    Channel number
+ * @param cell    Cell
+ */
+static void trackmod_process_volume(trackmod_modplay_t *modplay, size_t chan,
+    trackmod_cell_t *cell)
+{
+	uint8_t param4;
+
+	if (cell->volume >= 0x10 && cell->volume <= 0x10 + vol_max)
+		trackmod_effect_set_volume(modplay, chan, cell->volume - 0x10);
+
+	param4 = cell->volume & 0xf;
+
+	switch (cell->volume & 0xf0) {
+	case 0x60:
+		trackmod_effect_vol_slide_down(modplay, chan, param4);
+		break;
+	case 0x70:
+		trackmod_effect_vol_slide_up(modplay, chan, param4);
+		break;
+	case 0x80:
+		trackmod_effect_fine_vol_slide_down(modplay, chan, param4);
+		break;
+	case 0x90:
+		trackmod_effect_fine_vol_slide_up(modplay, chan, param4);
+		break;
+	case 0xf0:
+		trackmod_effect_tone_porta(modplay, chan, param4 << 4);
+		break;
+	default:
+		break;
+	}
+}
+
 /** Process effect.
  *
@@ -222,8 +546,21 @@
 {
 	uint8_t param8;
+	uint8_t param4;
 
 	param8 = cell->effect & 0xff;
 
 	switch (cell->effect & 0xf00) {
+	case 0x100:
+		trackmod_effect_porta_up(modplay, chan, param8);
+		break;
+	case 0x200:
+		trackmod_effect_porta_down(modplay, chan, param8);
+		break;
+	case 0x300:
+		trackmod_effect_tone_porta(modplay, chan, param8);
+		break;
+	case 0xa00:
+		trackmod_effect_vol_slide(modplay, chan, param8);
+		break;
 	case 0xc00:
 		trackmod_effect_set_volume(modplay, chan, param8);
@@ -238,4 +575,21 @@
 		break;
 	}
+
+	param4 = cell->effect & 0xf;
+
+	switch (cell->effect & 0xff0) {
+	case 0xe10:
+		trackmod_effect_fine_porta_up(modplay, chan, param4);
+		break;
+	case 0xe20:
+		trackmod_effect_fine_porta_down(modplay, chan, param4);
+		break;
+	case 0xea0:
+		trackmod_effect_fine_vol_slide_up(modplay, chan, param4);
+		break;
+	case 0xeb0:
+		trackmod_effect_fine_vol_slide_down(modplay, chan, param4);
+		break;
+	}
 }
 
@@ -249,8 +603,19 @@
     trackmod_cell_t *cell)
 {
-	if (cell->period != 0 && cell->sample != 0)
+	modplay->chan[chan].period_new = 0;
+
+	trackmod_process_instr(modplay, chan, cell);
+
+	if (cell->period != 0 || (cell->note != 0 && cell->note != keyoff_note)) {
 		trackmod_process_note(modplay, chan, cell);
-
+	} else if (cell->note == keyoff_note && cell->instr == 0) {
+		trackmod_process_keyoff_note(modplay, chan);
+	}
+
+	trackmod_process_volume(modplay, chan, cell);
 	trackmod_process_effect(modplay, chan, cell);
+
+	if (modplay->chan[chan].period_new != 0)
+		modplay->chan[chan].period = modplay->chan[chan].period_new;
 }
 
@@ -267,8 +632,16 @@
 	pattern = trackmod_cur_pattern(modplay);
 
+	if (modplay->debug)
+		printf("%02zx: ", modplay->row);
+
 	for (i = 0; i < modplay->module->channels; i++) {
 		trackmod_pattern_get_cell(pattern, modplay->row, i, &cell);
-		if (modplay->debug)
-			printf("%4d %02x %03x |", cell.period, cell.sample, cell.effect);
+
+		if (modplay->debug) {
+			printf("%4d %02x %02x %03x |", cell.period ?
+			    cell.period : cell.note, cell.instr,
+			    cell.volume, cell.effect);
+		}
+
 		trackmod_process_cell(modplay, i, &cell);
 	}
@@ -289,5 +662,5 @@
 	ord_idx = modplay->ord_idx + 1;
 	if (ord_idx >= modplay->module->ord_list_len)
-		ord_idx = 0; /* XXX */
+		ord_idx = modplay->module->restart_pos;
 
 	return ord_idx;
@@ -313,4 +686,56 @@
 }
 
+/** Clear effects at end of row. */
+static void trackmod_clear_effects(trackmod_modplay_t *modplay)
+{
+	size_t i;
+
+	for (i = 0; i < modplay->module->channels; i++) {
+		modplay->chan[i].vol_slide = 0;
+		modplay->chan[i].portamento = 0;
+	}
+}
+
+/** Process effects at beginning of tick. */
+static void trackmod_process_tick(trackmod_modplay_t *modplay)
+{
+	trackmod_chan_t *chan;
+	size_t i;
+	int nv;
+	int np;
+
+	for (i = 0; i < modplay->module->channels; i++) {
+		chan = &modplay->chan[i];
+
+		/* Volume slides */
+		nv = (int)chan->volume + chan->vol_slide;
+		if (nv < 0)
+			nv = 0;
+		if (nv > vol_max)
+			nv = vol_max;
+
+		chan->volume = nv;
+
+		/* Portamentos */
+		if (chan->period_tgt == 0) {
+			/* Up or down portamento */
+			np = (int)chan->period - chan->portamento;
+		} else {
+			/* Tone portamento */
+			if (chan->period_tgt < chan->period)
+				np = max((int)chan->period_tgt, (int)chan->period - chan->portamento);
+			else
+				np = min((int)chan->period_tgt, (int)chan->period + chan->portamento);
+		}
+
+/*		if (np < period_min)
+			np = period_min;
+		if (np > period_max)
+			np = period_max;
+*/
+		modplay->chan[i].period = np;
+	}
+}
+
 /** Advance to next row.
  *
@@ -320,4 +745,7 @@
 {
 	trackmod_pattern_t *pattern;
+
+	/* Clear effect state at end of row */
+	trackmod_clear_effects(modplay);
 
 	pattern = trackmod_cur_pattern(modplay);
@@ -328,4 +756,5 @@
 		trackmod_next_pattern(modplay);
 
+	trackmod_process_tick(modplay);
 	trackmod_process_row(modplay);
 }
@@ -341,4 +770,6 @@
 	if (modplay->tick >= modplay->tpr)
 		trackmod_next_row(modplay);
+	else
+		trackmod_process_tick(modplay);
 }
 
@@ -366,6 +797,6 @@
 	modplay->smp = 0;
 
-	modplay->tpr = def_tpr;
-	modplay->bpm = def_bpm;
+	modplay->tpr = module->def_tpr;
+	modplay->bpm = module->def_bpm;
 
 	modplay->chan = calloc(module->channels,
@@ -374,4 +805,5 @@
 		goto error;
 
+	trackmod_process_tick(modplay);
 	trackmod_process_row(modplay);
 
@@ -416,4 +848,27 @@
 }
 
+/** Get sample frame.
+ *
+ * Get frame at the specified sample position.
+ *
+ * @param sample Sample
+ * @param pos	 Position (frame index)
+ * @return	 Frame value
+ */
+int trackmod_sample_get_frame(trackmod_sample_t *sample, size_t pos)
+{
+	int8_t *i8p;
+	int16_t *i16p;
+
+	if (sample->bytes_smp == 1) {
+		i8p = (int8_t *)sample->data;
+		return i8p[pos];
+	} else {
+		/* chan->sample->bytes_smp == 2 */
+		i16p = (int16_t *)sample->data;
+		return i16p[pos] / 256; /* XXX Retain full precision */
+	}
+}
+
 /** Advance sample position to next frame.
  *
@@ -422,15 +877,19 @@
 static void chan_smp_next_frame(trackmod_chan_t *chan)
 {
-	chan->lsmp = chan->sample->data[chan->smp_pos];
+	chan->lsmp = trackmod_sample_get_frame(chan->sample, chan->smp_pos);
 	++chan->smp_pos;
 
-	if (chan->sample->loop_len == 0) {
-		/* No looping */
+	switch (chan->sample->loop_type) {
+	case tl_pingpong_loop:
+		/** XXX Pingpong loop */
+	case tl_no_loop:
+		/* No loop */
 		if (chan->smp_pos >= chan->sample->length) {
 			chan->sample = NULL;
 			chan->smp_pos = 0;
 		}
-	} else {
-		/** Looping */
+		break;
+	case tl_forward_loop:
+		/** Forward loop */
 		if (chan->smp_pos >= chan->sample->loop_start +
 		    chan->sample->loop_len) {
@@ -455,5 +914,5 @@
 	trackmod_chan_t *chan = &modplay->chan[cidx];
 
-	if (chan->sample == NULL)
+	if (chan->sample == NULL || chan->period == 0)
 		return 0;
 
@@ -464,6 +923,6 @@
 	 */
 	sl = (int)chan->lsmp * amp_factor * chan->volume / vol_max;
-	sn = (int)chan->sample->data[chan->smp_pos] * amp_factor *
-	    chan->volume / vol_max;
+	sn = (int)trackmod_sample_get_frame(chan->sample, chan->smp_pos) *
+	    amp_factor * chan->volume / vol_max;
 
 	period = (int)chan->period;
Index: uspace/lib/trackmod/trackmod.h
===================================================================
--- uspace/lib/trackmod/trackmod.h	(revision 18cc83cf2ce837ccf57cc87f761ed36d8926b52f)
+++ uspace/lib/trackmod/trackmod.h	(revision 43dd72b76b663dd908ac9a1b5c56863eca638ef3)
@@ -40,4 +40,5 @@
 
 extern trackmod_module_t *trackmod_module_new(void);
+extern int trackmod_module_load(char *, trackmod_module_t **);
 extern void trackmod_module_destroy(trackmod_module_t *);
 extern int trackmod_modplay_create(trackmod_module_t *, unsigned,
@@ -45,4 +46,5 @@
 extern void trackmod_modplay_destroy(trackmod_modplay_t *);
 extern void trackmod_modplay_get_samples(trackmod_modplay_t *, void *, size_t);
+extern int trackmod_sample_get_frame(trackmod_sample_t *, size_t);
 
 #endif
Index: uspace/lib/trackmod/types/protracker.h
===================================================================
--- uspace/lib/trackmod/types/protracker.h	(revision 18cc83cf2ce837ccf57cc87f761ed36d8926b52f)
+++ uspace/lib/trackmod/types/protracker.h	(revision 43dd72b76b663dd908ac9a1b5c56863eca638ef3)
@@ -47,5 +47,9 @@
 	protracker_olist_len = 128,
 	/** Number of rows in a pattern */
-	protracker_pattern_rows = 64
+	protracker_pattern_rows = 64,
+	/** Default TPR */
+	protracker_def_tpr = 6,
+	/** Default BPM */
+	protracker_def_bpm = 125
 };
 
Index: uspace/lib/trackmod/types/trackmod.h
===================================================================
--- uspace/lib/trackmod/types/trackmod.h	(revision 18cc83cf2ce837ccf57cc87f761ed36d8926b52f)
+++ uspace/lib/trackmod/types/trackmod.h	(revision 43dd72b76b663dd908ac9a1b5c56863eca638ef3)
@@ -41,17 +41,63 @@
 #include <stdint.h>
 
+enum {
+	max_key = 96,
+	keyoff_note = 97
+};
+
+typedef enum {
+	/** No loop */
+	tl_no_loop,
+	/** Forward loop */
+	tl_forward_loop,
+	/** Pingpong loop */
+	tl_pingpong_loop
+} trackmod_looptype_t;
+
 /** Sample */
 typedef struct {
 	/** Length in frames */
 	size_t length;
+	/** Bytes per sample */
+	size_t bytes_smp;
 	/** Sample data */
-	int8_t *data;
+	void *data;
+	/** Loop type */
+	trackmod_looptype_t loop_type;
 	/** Loop start position in frames */
 	size_t loop_start;
-	/** Loop length in frames or 0 - no looping */
+	/** Loop length in frames (> 0) */
 	size_t loop_len;
 	/** Default volume (0..63) */
 	uint8_t def_vol;
+	/** Relative note */
+	int rel_note;
+	/** Finetune value (-8..7) in 1/8 semitones */
+	int finetune;
 } trackmod_sample_t;
+
+/** Instrument */
+typedef struct {
+	/** Number of samples */
+	size_t samples;
+	/** Samples */
+	trackmod_sample_t *sample;
+	/** Sample index for each key */
+	int key_smp[max_key];
+} trackmod_instr_t;
+
+/** Pattern cell */
+typedef struct {
+	/** Note */
+	unsigned note;
+	/** Sample period */
+	unsigned period;
+	/** Instrument number */
+	unsigned instr;
+	/** Volume */
+	uint8_t volume;
+	/** Effect */
+	uint16_t effect;
+} trackmod_cell_t;
 
 /** Pattern */
@@ -62,5 +108,5 @@
 	size_t channels;
 	/** Pattern data */
-	uint32_t *data;
+	trackmod_cell_t *data;
 } trackmod_pattern_t;
 
@@ -70,7 +116,7 @@
 	size_t channels;
 	/** Number of samples */
-	size_t samples;
-	/** Samples */
-	trackmod_sample_t *sample;
+	size_t instrs;
+	/** Instruments */
+	trackmod_instr_t *instr;
 	/** Number of patterns */
 	size_t patterns;
@@ -81,4 +127,10 @@
 	/** Order list */
 	size_t *ord_list;
+	/** Restart pos */
+	size_t restart_pos;
+	/** Default BPM */
+	unsigned def_bpm;
+	/** Default TPR */
+	unsigned def_tpr;
 } trackmod_module_t;
 
@@ -92,8 +144,17 @@
 	/** Sample position (clock ticks within frame) */
 	size_t smp_clk;
-	/** Period */
+	/** Current period */
 	unsigned period;
+	/** Period after note was processed, zero if no note */
+	unsigned period_new;
 	/** Volume */
 	uint8_t volume;
+	/** Volume slide amount */
+	int vol_slide;
+	/** Portamento amount (positive for tone and up portamento,
+	  * negative for down portamento. */
+	int portamento;
+	/** Tone portamento target period. */
+	unsigned period_tgt;
 } trackmod_chan_t;
 
@@ -132,14 +193,4 @@
 } trackmod_modplay_t;
 
-/** Pattern cell (decoded) */
-typedef struct {
-	/** Sample period */
-	unsigned period;
-	/** Sample number */
-	unsigned sample;
-	/** Effect */
-	unsigned effect;
-} trackmod_cell_t;
-
 #endif
 
Index: uspace/lib/trackmod/types/xm.h
===================================================================
--- uspace/lib/trackmod/types/xm.h	(revision 43dd72b76b663dd908ac9a1b5c56863eca638ef3)
+++ uspace/lib/trackmod/types/xm.h	(revision 43dd72b76b663dd908ac9a1b5c56863eca638ef3)
@@ -0,0 +1,216 @@
+/*
+ * Copyright (c) 2014 Jiri Svoboda
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup trackmod
+ * @{
+ */
+/**
+ * @file Extended Module (.xm) types.
+ */
+
+#ifndef TYPES_XM_H
+#define TYPES_XM_H
+
+#include <stdint.h>
+
+enum {
+	/** ID text (signature) */
+	xm_id_text_size = 17,
+	/** Module name size */
+	xm_mod_name_size = 20,
+	/** Tracker name size */
+	xm_tracker_name_size = 20,
+	/** Pattern order table size */
+	xm_pat_ord_table_size = 256,
+	/** Instrument name size */
+	xm_instr_name_size = 22,
+	/** Sample number for all notes table size */
+	xm_smp_note_size = 96,
+	/** Max number of volume envelope points */
+	xm_vol_env_points = 48,
+	/** Max number of panning envelope points */
+	xm_pan_env_points = 48,
+	/** Sample name size */
+	xm_smp_name_size = 22,
+	/** Keyoff note number */
+	xm_keyoff_note = 97
+};
+
+/** XM file header */
+typedef struct {
+	uint8_t id_text[xm_id_text_size];
+	/** Module name */
+	uint8_t name[xm_mod_name_size];
+	/** Text EOF mark */
+	uint8_t text_break;
+	/** Tracker name */
+	uint8_t tracker_name[xm_tracker_name_size];
+	/** File format version */
+	uint16_t version;
+	/** Header size */
+	uint32_t hdr_size;
+	/** Song length (in pattern order table) */
+	uint16_t song_len;
+	/** Restart position */
+	uint16_t restart_pos;
+	/** Number of channels */
+	uint16_t channels;
+	/** Number of patterns */
+	uint16_t patterns;
+	/** Number of intstruments */
+	uint16_t instruments;
+	/** Flags */
+	uint16_t flags;
+	/** Default tempo */
+	uint16_t def_tempo;
+	/** Default BPM */
+	uint16_t def_bpm;
+	uint8_t pat_ord_table[xm_pat_ord_table_size];
+} __attribute__((packed)) xm_hdr_t;
+
+typedef enum {
+	/** 1 = Linear frequency table, 0 = Amiga freq. table */
+	xmf_lftable = 0
+} xm_flags_bits_t;
+
+/** XM pattern header */
+typedef struct {
+	/** Pattern header size */
+	uint32_t hdr_size;
+	/** Packing type */
+	uint8_t pack_type;
+	/** Number of rows */
+	uint16_t rows;
+	/** Packed pattern data size */
+	uint16_t data_size;
+} __attribute__((packed)) xm_pattern_t;
+
+/** XM instrument header. */
+typedef struct {
+	/** Instrument size */
+	uint32_t size;
+	/** Instrument name */
+	uint8_t name[xm_instr_name_size];
+	/** Instrument type */
+	uint8_t instr_type;
+	/** Number of samples in instrument */
+	uint16_t samples;
+} __attribute__((packed)) xm_instr_t;
+
+/** XM additional instrument header if number of samples > 0 */
+typedef struct {
+	/** Sample header size */
+	uint32_t smp_hdr_size;
+	/** Sample number for all notes */
+	uint8_t smp_note[xm_smp_note_size];
+	/** Points for volume envelope */
+	uint8_t vol_point[xm_vol_env_points];
+	/** Points for panning envelope */
+	uint8_t pan_point[xm_pan_env_points];
+	/** Number of volume points */
+	uint8_t vol_points;
+	/** Number of panning points */
+	uint8_t pan_points;
+	/** Volume sustating point */
+	uint8_t vol_sustain;
+	/** Volume loop start point */
+	uint8_t vol_loop_start;
+	/** Volume loop end point */
+	uint8_t vol_loop_end;
+	/** Panning sustating point */
+	uint8_t pan_sustain;
+	/** Panning loop start point */
+	uint8_t pan_loop_start;
+	/** Panning loop end point */
+	uint8_t pan_loop_end;
+	/** Volume type */
+	uint8_t vol_type;
+	/** Panning type */
+	uint8_t pan_type;
+	/** Vibrato type */
+	uint8_t vibrato_type;
+	/** Vibrato sweep */
+	uint8_t vibrato_sweep;
+	/** Vibrato depth */
+	uint8_t vibrato_depth;
+	/** Vibrato rate */
+	uint8_t vibrato_rate;
+	/** Volume fadeout */
+	uint16_t vol_fadeout;
+	/** Reserved */
+	uint16_t res241;
+} __attribute__((packed)) xm_instr_ext_t;
+
+/** XM sample header */
+typedef struct {
+	/** Sample length */
+	uint32_t length;
+	/** Loop start */
+	uint32_t loop_start;
+	/** Loop length */
+	uint32_t loop_len;
+	/** Volume */
+	uint8_t volume;
+	/** Finetune */
+	int8_t finetune;
+	/** Sample type */
+	uint8_t smp_type;
+	/** Panning */
+	uint8_t panning;
+	/** Relative note number */
+	int8_t rel_note;
+	/** Reserved */
+	uint8_t res17;
+	/** Sample name */
+	uint8_t name[xm_smp_name_size];
+} __attribute__((packed)) xm_smp_t;
+
+/** XM sample type bits */
+typedef enum {
+	/** 16-bit sample data */
+	xmst_16_bit = 4,
+	/** Loop type (H) */
+	xmst_loop_type_h = 1,
+	/** Loop type (L) */
+	xmst_loop_type_l = 0
+} xm_smp_type_bits_t;
+
+/** Sample loop type */
+typedef enum {
+	/** No loop */
+	xmsl_no_loop = 0,
+	/** Forward loop */
+	xmsl_forward_loop = 1,
+	/** Ping-pong loop */
+	xmsl_pingpong_loop = 2
+} xm_smp_loop_type;
+
+#endif
+
+/** @}
+ */
Index: uspace/lib/trackmod/xm.c
===================================================================
--- uspace/lib/trackmod/xm.c	(revision 43dd72b76b663dd908ac9a1b5c56863eca638ef3)
+++ uspace/lib/trackmod/xm.c	(revision 43dd72b76b663dd908ac9a1b5c56863eca638ef3)
@@ -0,0 +1,497 @@
+/*
+ * Copyright (c) 2014 Jiri Svoboda
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup trackmod
+ * @{
+ */
+/**
+ * @file Extended Module (.xm).
+ */
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <mem.h>
+
+#include "byteorder.h"
+#include "xm.h"
+#include "trackmod.h"
+#include "types/xm.h"
+
+static char xm_id_text[] = "Extended Module: ";
+
+/** Load XM order list
+ *
+ * @param xm_hdr XM header
+ * @param module Module
+ * @return EOK on success, EIO on format error, ENOMEM if out of memory.
+ */
+static int trackmod_xm_load_order_list(xm_hdr_t *xm_hdr, trackmod_module_t *module)
+{
+	int rc;
+	size_t i;
+
+	/* Order list */
+	module->ord_list_len = uint16_t_le2host(xm_hdr->song_len);
+	if (module->ord_list_len > xm_pat_ord_table_size) {
+		/* Invalid song length */
+		rc = EIO;
+		goto error;
+	}
+
+	module->ord_list = calloc(sizeof(size_t), module->ord_list_len);
+	if (module->ord_list == NULL) {
+		printf("Out of memory.\n");
+		rc = ENOMEM;
+		goto error;
+	}
+
+	for (i = 0; i < module->ord_list_len; i++) {
+		module->ord_list[i] = xm_hdr->pat_ord_table[i];
+	}
+
+	module->restart_pos = uint16_t_le2host(xm_hdr->restart_pos);
+	if (module->restart_pos >= module->ord_list_len) {
+		rc = EIO;
+		goto error;
+	}
+
+	return EOK;
+error:
+	return rc;
+}
+
+/** Decode XM pattern.
+ *
+ * @param data    Packed pattern data
+ * @param pattern Pattern to load to
+ * @return	  EOK on success, EINVAL if there is error in the coded data.
+ */
+static int trackmod_xm_decode_pattern(uint8_t *data, size_t dsize,
+    trackmod_pattern_t *pattern)
+{
+	size_t cells;
+	size_t i;
+	size_t si;
+	uint8_t mask;
+
+	cells = pattern->rows * pattern->channels;
+	si = 0;
+
+	for (i = 0; i < cells; i++) {
+		if (si >= dsize)
+			return EINVAL;
+
+		if ((data[si] & 0x80) != 0) {
+			mask = data[si++] & 0x1f;
+		} else {
+			mask = 0x1f;
+		}
+
+		/* Note */
+		if ((mask & 0x1) != 0) {
+			if (si >= dsize)
+				return EINVAL;
+			pattern->data[i].note = data[si++] & 0x7f;
+		}
+
+		/* Instrument */
+		if ((mask & 0x2) != 0) {
+			if (si >= dsize)
+				return EINVAL;
+			pattern->data[i].instr = data[si++];
+		}
+
+		/* Volume */
+		if ((mask & 0x4) != 0) {
+			if (si >= dsize)
+				return EINVAL;
+			pattern->data[i].volume = data[si++];
+		}
+
+		/* Effect type */
+		if ((mask & 0x8) != 0) {
+			if (si >= dsize)
+				return EINVAL;
+			pattern->data[i].effect = (unsigned)data[si++] << 8;
+		}
+
+		/* Effect parameter */
+		if ((mask & 0x10) != 0) {
+			if (si >= dsize)
+				return EINVAL;
+			pattern->data[i].effect |= data[si++];
+		}
+	}
+
+	/* Note: Ignoring any extra data */
+
+	return EOK;
+}
+
+/** Load XM patterns
+ *
+ * @param file File
+ * @param module Module
+ * @return EOK on success, EIO on format error, ENOMEM if out of memory.
+ */
+static int trackmod_xm_load_patterns(FILE *f, trackmod_module_t *module)
+{
+	size_t i;
+	size_t hdr_size;
+	uint8_t pack_type;
+	size_t rows;
+	size_t data_size;
+	ssize_t nread;
+	xm_pattern_t pattern;
+	uint8_t *buf = NULL;
+	long seek_amount;
+	int rc;
+
+	module->pattern = calloc(sizeof(trackmod_pattern_t), module->patterns);
+	if (module->pattern == NULL) {
+		rc = ENOMEM;
+		goto error;
+	}
+
+	for (i = 0; i < module->patterns; i++) {
+		rc = fread(&pattern, 1, sizeof(xm_pattern_t), f);
+		if (rc != sizeof(xm_pattern_t)) {
+			rc = EIO;
+			goto error;
+		}
+
+		hdr_size = (size_t)uint32_t_le2host(pattern.hdr_size);
+		pack_type = pattern.pack_type;
+		rows = uint16_t_le2host(pattern.rows);
+		data_size = uint16_t_le2host(pattern.data_size);
+
+		if (pack_type != 0) {
+			rc = EIO;
+			goto error;
+		}
+
+		/* Jump to end of pattern header */
+		seek_amount = (long)hdr_size - (long)sizeof(xm_pattern_t);
+		if (fseek(f, seek_amount, SEEK_CUR) < 0) {
+			rc = EIO;
+			goto error;
+		}
+
+		module->pattern[i].rows = rows;
+		module->pattern[i].channels = module->channels;
+		module->pattern[i].data = calloc(sizeof(trackmod_cell_t),
+		    rows * module->channels);
+
+		if (module->pattern[i].data == NULL) {
+			rc = ENOMEM;
+			goto error;
+		}
+
+		buf = calloc(1, data_size);
+		if (buf == NULL) {
+			rc = ENOMEM;
+			goto error;
+		}
+
+		nread = fread(buf, 1, data_size, f);
+		if (nread != (ssize_t)data_size) {
+			rc = EIO;
+			goto error;
+		}
+
+		rc = trackmod_xm_decode_pattern(buf, data_size,
+		    &module->pattern[i]);
+		if (rc != EOK)
+			goto error;
+
+		free(buf);
+		buf = NULL;
+	}
+
+	return EOK;
+error:
+	free(buf);
+	return rc;
+}
+
+/** Decode XM sample data.
+ *
+ * XM sample data is delta-encoded. Undo the delta encoding and convert
+ * byte order.
+ */
+static void trackmod_xm_decode_sample_data(trackmod_sample_t *sample)
+{
+	size_t i;
+	int8_t *i8p;
+	int16_t *i16p;
+	int8_t cur8;
+	int16_t cur16;
+
+	if (sample->bytes_smp == 1) {
+		cur8 = 0;
+		i8p = (int8_t *)sample->data;
+
+		for (i = 0; i < sample->length; i++) {
+			cur8 = cur8 + i8p[i];
+			i8p[i] = cur8;
+		}
+	} else {
+		cur16 = 0;
+		i16p = (int16_t *)sample->data;
+
+		for (i = 0; i < sample->length; i++) {
+			cur16 = cur16 + (int16_t)uint16_t_le2host(i16p[i]);
+			i16p[i] = cur16;
+		}
+	}
+}
+
+/** Load XM instruments
+ *
+ * @param file File
+ * @param module Module
+ * @return EOK on success, EIO on format error, ENOMEM if out of memory.
+ */
+static int trackmod_xm_load_instruments(xm_hdr_t *xm_hdr, FILE *f,
+    trackmod_module_t *module)
+{
+	size_t i, j;
+	xm_instr_t instr;
+	xm_instr_ext_t instrx;
+	xm_smp_t smp;
+	size_t samples;
+	size_t instr_size;
+	size_t smp_size;
+	size_t smp_hdr_size;
+	ssize_t nread;
+	uint8_t ltype;
+	trackmod_sample_t *sample;
+	void *smp_data;
+	long pos;
+	int rc;
+
+	module->instrs = uint16_t_le2host(xm_hdr->instruments);
+	module->instr = calloc(module->instrs, sizeof(trackmod_instr_t));
+	if (module->instr == NULL)
+		return ENOMEM;
+
+	for (i = 0; i < module->instrs; i++) {
+		pos = ftell(f);
+		rc = fread(&instr, 1, sizeof(xm_instr_t), f);
+		if (rc != sizeof(xm_instr_t)) {
+			rc = EIO;
+			goto error;
+		}
+
+		samples = uint16_t_le2host(instr.samples);
+		instr_size = (size_t)uint32_t_le2host(instr.size);
+
+		if (samples > 0) {
+			rc = fread(&instrx, 1, sizeof(xm_instr_ext_t), f);
+			if (rc != sizeof(xm_instr_ext_t)) {
+				rc = EIO;
+				goto error;
+			}
+
+			smp_hdr_size = uint32_t_le2host(instrx.smp_hdr_size);
+
+			for (j = 0; j < xm_smp_note_size; j++) {
+				module->instr[i].key_smp[j] =
+				    instrx.smp_note[j];
+			}
+
+			module->instr[i].samples = samples;
+			module->instr[i].sample = calloc(samples,
+			    sizeof(trackmod_sample_t));
+			if (module->instr[i].sample == NULL) {
+				rc = ENOMEM;
+				goto error;
+			}
+		}
+
+		if (fseek(f, pos + instr_size, SEEK_SET) < 0) {
+			rc = EIO;
+			goto error;
+		}
+
+		for (j = 0; j < samples; j++) {
+			sample = &module->instr[i].sample[j];
+			pos = ftell(f);
+
+			rc = fread(&smp, 1, sizeof(xm_smp_t), f);
+			if (rc != sizeof(xm_smp_t)) {
+				rc = EIO;
+				goto error;
+			}
+
+			smp_size = (size_t)uint32_t_le2host(smp.length);
+
+			smp_data = calloc(smp_size, 1);
+			if (smp_data == NULL) {
+				rc = ENOMEM;
+				goto error;
+			}
+
+			if (fseek(f, pos + smp_hdr_size, SEEK_SET) < 0) {
+				rc = EIO;
+				goto error;
+			}
+
+			nread = fread(smp_data, 1, smp_size, f);
+			if (nread != (ssize_t)smp_size) {
+				rc = EIO;
+				goto error;
+			}
+
+			if (smp.smp_type & (1 << xmst_16_bit)) {
+				sample->bytes_smp = 2;
+			} else {
+				sample->bytes_smp = 1;
+			}
+
+			sample->data = smp_data;
+			sample->length = smp_size / sample->bytes_smp;
+
+			ltype = smp.smp_type & 0x3;
+			switch (ltype) {
+			case xmsl_no_loop:
+				sample->loop_type = tl_no_loop;
+				break;
+			case xmsl_forward_loop:
+				sample->loop_type = tl_forward_loop;
+				break;
+			case xmsl_pingpong_loop:
+				sample->loop_type = tl_pingpong_loop;
+				break;
+			default:
+				rc = EIO;
+				goto error;
+			}
+
+			sample->loop_start =
+			    uint32_t_le2host(smp.loop_start) / sample->bytes_smp;
+			sample->loop_len =
+			    uint32_t_le2host(smp.loop_len) / sample->bytes_smp;
+			sample->def_vol = 0x40;
+			sample->rel_note = smp.rel_note;
+			sample->finetune = smp.finetune / 2;
+
+			trackmod_xm_decode_sample_data(sample);
+		}
+	}
+
+	return EOK;
+error:
+	return rc;
+}
+
+/** Load extended module.
+ *
+ * @param fname   File name
+ * @param rmodule Place to store pointer to newly loaded module.
+ * @return        EOK on success, ENONEM if out of memory, EIO on I/O error
+ *                or if any error is found in the format of the file.
+ */
+int trackmod_xm_load(char *fname, trackmod_module_t **rmodule)
+{
+	FILE *f = NULL;
+	trackmod_module_t *module = NULL;
+	xm_hdr_t xm_hdr;
+	size_t nread;
+	size_t hdr_size;
+	int rc;
+
+	f = fopen(fname, "rb");
+	if (f == NULL) {
+		printf("Error opening file.\n");
+		rc = EIO;
+		goto error;
+	}
+
+	nread = fread(&xm_hdr, 1, sizeof(xm_hdr_t), f);
+	if (nread < sizeof(xm_hdr_t)) {
+		printf("File too small.\n");
+		rc = EIO;
+		goto error;
+	}
+
+	if (memcmp(xm_hdr.id_text, xm_id_text, xm_id_text_size) != 0) {
+		rc = EIO;
+		goto error;
+	}
+
+	module = trackmod_module_new();
+	if (module == NULL) {
+		printf("Out of memory.\n");
+		rc = ENOMEM;
+		goto error;
+	}
+
+	module->channels = uint16_t_le2host(xm_hdr.channels);
+	module->patterns = uint16_t_le2host(xm_hdr.patterns);
+	module->ord_list_len = uint16_t_le2host(xm_hdr.song_len);
+
+	hdr_size = (size_t)uint32_t_le2host(xm_hdr.hdr_size)
+	    + offsetof(xm_hdr_t, hdr_size);
+
+	module->def_bpm = uint16_t_le2host(xm_hdr.def_bpm);
+	module->def_tpr = uint16_t_le2host(xm_hdr.def_tempo);
+
+	/* Jump to end of file header */
+	if (fseek(f, hdr_size, SEEK_SET) < 0) {
+		rc = EIO;
+		goto error;
+	}
+
+	rc = trackmod_xm_load_order_list(&xm_hdr, module);
+	if (rc != EOK)
+		goto error;
+
+	rc = trackmod_xm_load_patterns(f, module);
+	if (rc != EOK)
+		goto error;
+
+	rc = trackmod_xm_load_instruments(&xm_hdr, f, module);
+	if (rc != EOK)
+		goto error;
+
+	(void) fclose(f);
+
+	*rmodule = module;
+	return EOK;
+error:
+	if (module != NULL)
+		trackmod_module_destroy(module);
+	if (f != NULL)
+		(void) fclose(f);
+	return rc;
+}
+
+/** @}
+ */
Index: uspace/lib/trackmod/xm.h
===================================================================
--- uspace/lib/trackmod/xm.h	(revision 43dd72b76b663dd908ac9a1b5c56863eca638ef3)
+++ uspace/lib/trackmod/xm.h	(revision 43dd72b76b663dd908ac9a1b5c56863eca638ef3)
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2014 Jiri Svoboda
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup trackmod
+ * @{
+ */
+/**
+ * @file Extended Module (.xm).
+ */
+
+#ifndef XM_H
+#define XM_H
+
+#include "types/trackmod.h"
+
+extern int trackmod_xm_load(char *, trackmod_module_t **);
+
+#endif
+
+/** @}
+ */
