Index: uspace/lib/trackmod/trackmod.c
===================================================================
--- uspace/lib/trackmod/trackmod.c	(revision 7e69e0e9418526683ac074433809479bcfed9ea9)
+++ 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;
