diff --git a/TiMidity++-2.13.2.original/./configure b/TiMidity++-2.13.2/./configure
index 717344d..aa323bc 100755
--- a/TiMidity++-2.13.2.original/./configure
+++ b/TiMidity++-2.13.2/./configure
@@ -21397,5 +21397,44 @@ if test "$no_create" != yes; then
   # Use ||, not &&, to avoid exiting from the if with $? = 1, which
   # would make configure fail if this is the last instruction.
   $ac_cs_success || { (exit 1); exit 1; }
 fi
 
+case "$cross_compiling" in
+ no ) ;;
+ *)
+   echo "N O T I C E !"
+   TO_EDIT=`grep -n '^AR = ar' Makefile */Makefile`
+   echo "$TO_EDIT"
+   echo "You may need to replace manually"
+   echo "  AR = ar"
+   echo "with"
+   echo "  AR = ${ac_tool_prefix}ar"
+   echo "in some Makefile(s) if you are cross compiling!"
+   echo 'E.g. execute `grep '"'AR = ar'"' Makefile */Makefile` to find them.'
+   TO_EDIT_COUNT=`echo "$TO_EDIT" | wc -w`
+   if [ $TO_EDIT_COUNT -gt 0 ]
+    then
+     echo -n "try automated editing? (y/n) "
+     read DoOrNot
+     case "$DoOrNot" in
+      [yY]* )
+       for this_file in `echo "$TO_EDIT" | cut -d : -f 1`
+        do
+         ( echo "1,$ s/^AR = ar$/AR = ${ac_tool_prefix}ar/"
+           echo "w"
+           echo "q" ) | ed $this_file
+        done
+       grep -n '^AR = ' Makefile */Makefile
+       EDIT_CHECK=`grep -n '^AR = ar' Makefile */Makefile`
+       EDIT_CHECK_COUNT=`echo "$EDIT_CHECK" | wc -w`
+       if [ $EDIT_CHECK_COUNT -gt 0 ]
+        then
+         echo "$EDIT_CHECK"
+         echo "EDIT_CHECK_COUNT = $EDIT_CHECK_COUNT"
+         echo -n "press ENTER to continue ... "
+         line
+       fi ;;
+     esac
+   fi
+  ;;
+esac
diff --git a/TiMidity++-2.13.2.original/./timidity/readmidi.h b/TiMidity++-2.13.2/./timidity/readmidi.h
index 224b144..6c662db 100755
--- a/TiMidity++-2.13.2.original/./timidity/readmidi.h
+++ b/TiMidity++-2.13.2/./timidity/readmidi.h
@@ -140,10 +140,11 @@ extern int dump_current_timesig(MidiEvent *codes, int maxlen);
 extern ChannelBitMask quietchannels;
 extern struct midi_file_info *current_file_info;
 extern int opt_trace_text_meta_event;
 extern int opt_default_mid;
 extern int opt_system_mid;
+extern int opt_list_channels_per_track;
 extern int ignore_midi_error;
 extern int readmidi_error_flag;
 extern int readmidi_wrd_mode;
 extern int play_system_mode;
 
@@ -170,6 +171,7 @@ extern void free_userinst();
 
 extern void init_channel_layer(int);
 extern void add_channel_layer(int, int);
 extern void remove_channel_layer(int);
 
+extern int (*port_by_tracknumber)(int track);
 #endif /* ___READMIDI_H_ */
diff --git a/TiMidity++-2.13.2.original/./timidity/tables.c b/TiMidity++-2.13.2/./timidity/tables.c
index bb37994..d292743 100755
--- a/TiMidity++-2.13.2.original/./timidity/tables.c
+++ b/TiMidity++-2.13.2/./timidity/tables.c
@@ -33,10 +33,11 @@
 #include <stdio.h>
 #include <math.h>
 #include "timidity.h"
 #include "common.h"
 #include "mt19937ar.h"
+#define THIS_IS_TABLES_C
 #include "tables.h"
 
 int32 freq_table[128];
 int32 freq_table_zapped[128];
 int32 freq_table_tuning[128][128];
@@ -48,11 +49,11 @@ int32 freq_table_user[4][48][128];
 void init_freq_table(void)
 {
 	int i;
 	
 	for (i = 0; i < 128; i++) {
-		freq_table[i] = 440 * pow(2.0, (i - 69) / 12.0) * 1000 + 0.5;
+		freq_table[i] = ChamberToneFreq * pow(2.0, (i - 69) / 12.0) * 1000 + 0.5;
 		freq_table_zapped[i] = freq_table[i];
 	}
 }
 
 void init_freq_table_tuning(void)
@@ -61,11 +62,11 @@ void init_freq_table_tuning(void)
 	double f;
 	
 	for (i = 0; i < 128; i++)
 			freq_table_tuning[0][i] = freq_table_zapped[i];
 	for (i = 0; i < 128; i++) {
-		f = 440 * pow(2.0, (i - 69) / 12.0);
+		f = ChamberToneFreq * pow(2.0, (i - 69) / 12.0);
 		for (p = 1; p < 128; p++)
 			freq_table_tuning[p][i] = f * 1000 + 0.5;
 	}
 }
 
@@ -84,11 +85,11 @@ void init_freq_table_pytha(void)
 		6561.0 / 4096,   27.0 /   16,  16.0 /   9,   243.0 /   128
 	};
 	
 	for (i = 0; i < 12; i++)
 		for (j = -1; j < 11; j++) {
-			f = 440 * pow(2.0, (i - 9) / 12.0 + j - 5);
+			f = ChamberToneFreq * pow(2.0, (i - 9) / 12.0 + j - 5);
 			for (k = 0; k < 12; k++) {
 				l = i + j * 12 + k;
 				if (l < 0 || l >= 128)
 					continue;
 				freq_table_pytha[i][l] = f * major_ratio[k] * 1000 + 0.5;
@@ -128,11 +129,11 @@ void init_freq_table_meantone(void)
 	minor_ratio[9] = 5.0 / 3;
 	minor_ratio[10] = 4 / pow(10.0 / 3, 2.0 / 3);
 	minor_ratio[11] = pow(10.0 / 3, 5.0 / 3) / 4;
 	for (i = 0; i < 12; i++)
 		for (j = -1; j < 11; j++) {
-			f = 440 * pow(2.0, (i - 9) / 12.0 + j - 5);
+			f = ChamberToneFreq * pow(2.0, (i - 9) / 12.0 + j - 5);
 			for (k = 0; k < 12; k++) {
 				l = i + j * 12 + k;
 				if (l < 0 || l >= 128)
 					continue;
 				freq_table_meantone[i][l] =
@@ -161,11 +162,11 @@ void init_freq_table_pureint(void)
 	};
 	static const double sc = 81.0 / 80;
 	
 	for (i = 0; i < 12; i++)
 		for (j = -1; j < 11; j++) {
-			f = 440 * pow(2.0, (i - 9) / 12.0 + j - 5);
+			f = ChamberToneFreq * pow(2.0, (i - 9) / 12.0 + j - 5);
 			for (k = 0; k < 12; k++) {
 				l = i + j * 12 + k;
 				if (l < 0 || l >= 128)
 					continue;
 				freq_table_pureint[i][l] =
@@ -186,11 +187,11 @@ void init_freq_table_user(void)
 	double f;
 	
 	for (p = 0; p < 4; p++)
 		for (i = 0; i < 12; i++)
 			for (j = -1; j < 11; j++) {
-				f = 440 * pow(2.0, (i - 9) / 12.0 + j - 5);
+				f = ChamberToneFreq * pow(2.0, (i - 9) / 12.0 + j - 5);
 				for (k = 0; k < 12; k++) {
 					l = i + j * 12 + k;
 					if (l < 0 || l >= 128)
 						continue;
 					freq_table_user[p][i][l] = f * 1000 + 0.5;
@@ -1680,6 +1681,6 @@ float lofi_sampling_freq_table_xg[] =
 	496.0, 490.0, 485.0, 479.0, 474.0, 469.0, 464.0, 459.0,
 	455.0, 450.0, 445.0, 441.0, 437.0, 432.0, 428.0, 424.0,
 	420.0, 416.0, 412.0, 408.0, 405.0, 401.0, 397.0, 394.0,
 	390.0, 387.0, 383.0, 380.0, 377.0, 374.0, 371.0, 368.0,
 	364.0, 361.0, 359.0, 356.0, 353.0, 350.0, 347.0, 345.0,
-};
\ No newline at end of file
+};
diff --git a/TiMidity++-2.13.2.original/./timidity/tables.h b/TiMidity++-2.13.2/./timidity/tables.h
index 82439ea..2d85470 100755
--- a/TiMidity++-2.13.2.original/./timidity/tables.h
+++ b/TiMidity++-2.13.2/./timidity/tables.h
@@ -109,6 +109,15 @@ extern void init_def_vol_table(void);
 extern void init_gs_vol_table(void);
 extern void init_perceived_vol_table(void);
 extern void init_gm2_vol_table(void);
 extern void init_user_vol_table(FLOAT_T power);
 
+#ifndef THIS_IS_TABLES_C
+extern
+#endif
+double ChamberToneFreq
+#ifdef THIS_IS_TABLES_C
+ = 440.0
+#endif
+;
+
 #endif /* ___TABLES_H_ */
diff --git a/TiMidity++-2.13.2.original/./timidity/timidity.c b/TiMidity++-2.13.2/./timidity/timidity.c
index a53a367..b8d4119 100755
--- a/TiMidity++-2.13.2.original/./timidity/timidity.c
+++ b/TiMidity++-2.13.2/./timidity/timidity.c
@@ -203,14 +203,17 @@ enum {
 	TIM_OPT_VERSION,
 	TIM_OPT_WRD,
 	TIM_OPT_RCPCV_DLL,
 	TIM_OPT_CONFIG_STR,
 	TIM_OPT_FREQ_TABLE,
+        TIM_OPT_LCPT, /* list the used channels per track when reading in the file */
+        TIM_OPT_LILY, /* port by track for Lilypond MIDI files without MIDI PORT events */
+	TIM_OPT_TUNE, /* Tuning to a4 if different to 440 Hz */
 	TIM_OPT_PURE_INT,
 	TIM_OPT_MODULE,
 	/* last entry */
-	TIM_OPT_LAST = TIM_OPT_PURE_INT
+	TIM_OPT_LAST = TIM_OPT_PURE_INT /*??? Why not the last option TIM_OPT_MODULE? */
 };
 
 static const char *optcommands =
 		"4A:aB:b:C:c:D:d:E:eFfg:H:hI:i:jK:k:L:M:m:N:"
 		"O:o:P:p:Q:q:R:S:s:T:t:UV:vW:"
@@ -344,11 +347,16 @@ static const struct option longopts[] = {
 #ifdef __W32__
 	{ "rcpcv-dll",              required_argument, NULL, TIM_OPT_RCPCV_DLL },
 #endif
 	{ "config-string",          required_argument, NULL, TIM_OPT_CONFIG_STR },
 	{ "freq-table",             required_argument, NULL, TIM_OPT_FREQ_TABLE },
+	{ "tune",                   required_argument, NULL, TIM_OPT_TUNE },
 	{ "pure-intonation",        optional_argument, NULL, TIM_OPT_PURE_INT },
+        { "list-channels-per-track",optional_argument, NULL, TIM_OPT_LCPT },
+        { "lcpt",                   no_argument,       NULL, TIM_OPT_LCPT },
+        { "no-lcpt",                no_argument,       NULL, TIM_OPT_LCPT },
+        { "lily",                   no_argument,       NULL, TIM_OPT_LILY },
 	{ "module",                 required_argument, NULL, TIM_OPT_MODULE },
 	{ NULL,                     no_argument,       NULL, '\0'     }
 };
 #define INTERACTIVE_INTERFACE_IDS "kmqagrwAWP"
 
@@ -485,10 +493,13 @@ static inline int parse_opt_w(const char *);
 static inline int parse_opt_x(char *);
 static inline void expand_escape_string(char *);
 static inline int parse_opt_Z(char *);
 static inline int parse_opt_Z1(const char *);
 static inline int parse_opt_default_module(const char *);
+static inline int parse_opt_lcpt(const char *);
+static inline int parse_opt_lily(const char *);
+static inline int parse_opt_tune(const char *);
 __attribute__((noreturn))
 static inline int parse_opt_fail(const char *);
 static inline int set_value(int *, int, int, int, char *);
 static inline int set_val_i32(int32 *, int32, int32, int32, char *);
 static inline int set_channel_flag(ChannelBitMask *, int32, char *);
@@ -564,10 +575,20 @@ static char *dynamic_lib_root = SHARED_LIB_PATH;
 int free_instruments_afterwards=0;
 int def_prog = -1;
 char def_instr_name[256]="";
 VOLATILE int intr = 0;
 
+int port_by_tracknumber_for_Lilypond(int track)
+{ int port_num, tn;
+  port_num = 0; tn = track;
+  do {
+    if (tn <= 15) return port_num;
+    ++port_num; tn -= 15;
+    if (16 * (port_num + 1) > MAX_CHANNELS) port_num = 0;
+  } while (1);
+}
+
 #ifdef __W32__
 CRITICAL_SECTION critSect;
 
 #pragma argsused
 static BOOL WINAPI handler(DWORD dw)
@@ -2848,19 +2869,55 @@ MAIN_INTERFACE int set_tim_opt_long(int c, char *optarg, int index)
 		return parse_opt_x(arg);
 	case TIM_OPT_FREQ_TABLE:
 		return parse_opt_Z(arg);
 	case TIM_OPT_PURE_INT:
 		return parse_opt_Z1(arg);
+        case TIM_OPT_LCPT:
+                return parse_opt_lcpt(arg);
+        case TIM_OPT_LILY:
+                return parse_opt_lily(arg);
+        case TIM_OPT_TUNE:
+                return parse_opt_tune(arg);
 	case TIM_OPT_MODULE:
 		return parse_opt_default_module(arg);
 	default:
 		ctl->cmsg(CMSG_FATAL, VERB_NORMAL,
 				"[BUG] Inconceivable case branch %d", c);
 		abort();
 	}
 }
 
+static inline int parse_opt_tune(const char *arg)
+{
+	sscanf(arg, "%lf", &ChamberToneFreq);
+        if ((ChamberToneFreq > 475.0) || (ChamberToneFreq < 410.0)) {
+	  ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
+		    "The option --tune does only support the range 410 .. 475 Hz");
+          ChamberToneFreq = 440.0;
+        }
+	init_freq_table();
+	init_freq_table_tuning();
+	init_freq_table_pytha();
+	init_freq_table_meantone();
+	init_freq_table_pureint();
+	init_freq_table_user();
+	return 0;
+}
+
+static inline int parse_opt_lily(const char *arg)
+{
+	port_by_tracknumber = port_by_tracknumber_for_Lilypond;
+	return 0;
+}
+
+static inline int parse_opt_lcpt(const char *arg)
+{
+	/* list used channels per track while reading a midi file to memory */
+	opt_list_channels_per_track = y_or_n_p(arg); 
+	return 0;
+}
+
 static inline int parse_opt_A(const char *arg)
 {
 	/* amplify volume by n percent */
 	return set_val_i32(&amplification, atoi(arg), 0, MAX_AMPLIFICATION,
 			"Amplification");
@@ -3831,10 +3888,13 @@ static inline int parse_opt_h(const char *arg)
 "  --[no-]temper-control" NLS
 "  --default-mid=<HH>" NLS
 "  --system-mid=<HH>" NLS
 "  --default-bank=n" NLS
 "  --force-bank=n" NLS
+"  --list-channels-per-track / --[no-]lcps" NLS
+"  --lily  Wrap after 15-tracks to a new default port (60 channels for Lilypond's midi)" NLS
+"  --tune=f  Frequency of a4 in Hz" NLS
 "  --default-program=n/m" NLS
 "  --force-program=n/m" NLS
 "  --delay=(d|l|r|b)[,msec]" NLS
 "  --chorus=(d|n|s)[,level]" NLS
 "  --reverb=(d|n|g|f|G)[,level]" NLS
diff --git a/TiMidity++-2.13.2.original/./timidity/timidity.h b/TiMidity++-2.13.2/./timidity/timidity.h
index 3978a31..2ad32a0 100755
--- a/TiMidity++-2.13.2.original/./timidity/timidity.h
+++ b/TiMidity++-2.13.2/./timidity/timidity.h
@@ -96,11 +96,12 @@
    10 is the standard percussion channel.
    Some files (notably C:\WINDOWS\CANYON.MID) think that 16 is one too.
    On the other hand, some files know that 16 is not a drum channel and
    try to play music on it. This is now a runtime option, so this isn't
    a critical choice anymore. */
-#define DEFAULT_DRUMCHANNELS {10, -1}
+/*#define DEFAULT_DRUMCHANNELS {10, -1} */
+#define DEFAULT_DRUMCHANNELS {10, 26, 42, 58, -1}
 /* #define DEFAULT_DRUMCHANNELS {10, 16, -1} */
 
 /* type of floating point number */
 typedef double FLOAT_T;
 /* typedef float FLOAT_T; */
@@ -121,11 +122,12 @@ typedef double FLOAT_T;
    All but the last can be overridden from the command line. */
 #ifndef DEFAULT_RATE
 #define DEFAULT_RATE	44100
 #endif /* DEFAULT_RATE */
 
-#define DEFAULT_VOICES	256
+/* #define DEFAULT_VOICES	256 */
+#define DEFAULT_VOICES	512
 
 
 /* The size of the internal buffer is 2^AUDIO_BUFFER_BITS samples.
    This determines maximum number of samples ever computed in a row.
 
@@ -407,12 +409,14 @@ typedef double FLOAT_T;
 /* These affect general volume */
 #define GUARD_BITS 3
 #define AMP_BITS (15-GUARD_BITS)
 
 #define MAX_AMPLIFICATION 800
-#define MAX_CHANNELS 32
+/*#define MAX_CHANNELS 32 */
+#define MAX_CHANNELS 64
 /*#define MAX_CHANNELS 256*/
+/* use a multiple of 16 only for MAX_CHANNELS */
 #define MAXMIDIPORT 16
 
 /* Vibrato and tremolo Choices of the Day */
 #define SWEEP_TUNING 38
 #define VIBRATO_AMPLITUDE_TUNING 1.0L
@@ -436,11 +440,10 @@ typedef double FLOAT_T;
 /* malloc's limit */
 #define MAX_SAFE_MALLOC_SIZE (1<<23) /* 8M */
 
 #define DEFAULT_SOUNDFONT_ORDER 0
 
-
 /*****************************************************************************\
  section 3: include other headers
 \*****************************************************************************/
 
 #ifdef HAVE_CONFIG_H
diff --git a/TiMidity++-2.13.2.original/./timidity/playmidi.c b/TiMidity++-2.13.2/./timidity/playmidi.c
index aa8276c..ff4e5cf 100755
--- a/TiMidity++-2.13.2.original/./timidity/playmidi.c
+++ b/TiMidity++-2.13.2/./timidity/playmidi.c
@@ -1969,11 +1969,11 @@ static int select_play_sample(Sample *splist,
 			}
 		else
 			f = freq_table[*note];
 		if (! opt_pure_intonation && opt_temper_control
 				&& tt == 0 && f != freq_table[*note]) {
-			*note = log(f / 440000.0) / log(2) * 12 + 69.5;
+			*note = log(f /  (ChamberToneFreq * 1000.0)) / log(2) * 12 + 69.5;
 			*note = (*note < 0) ? 0 : ((*note > 127) ? 127 : *note);
 			fs = freq_table[*note];
 		} else
 			fs = freq_table[*note];
 	}
@@ -8086,11 +8086,11 @@ static void set_single_note_tuning(int part, int a, int b, int rt)
 		kn = a, st = b;
 		break;
 	case 2:
 		if (st == 0x7f && a == 0x7f && b == 0x7f)	/* no change */
 			break;
-		f = 440 * pow(2.0, (st - 69) / 12.0);
+		f = ChamberToneFreq * pow(2.0, (st - 69) / 12.0);
 		fst = pow(2.0, (a << 7 | b) / 196608.0);
 		freq_table_tuning[tp][kn] = f * fst * 1000 + 0.5;
 		if (rt)
 			for (i = 0; i < upper_voices; i++)
 				if (voice[i].status != VOICE_FREE) {
@@ -8167,11 +8167,11 @@ static void set_user_temper_entry(int part, int a, int b)
 				ratio[n] *= 2;
 		}
 		sc = 27 / ratio[9] / 16;	/* syntonic comma */
 		for (i = 0; i < 12; i++)
 			for (j = -1; j < 11; j++) {
-				f = 440 * pow(2.0, (i - 9) / 12.0 + j - 5);
+				f = ChamberToneFreq * pow(2.0, (i - 9) / 12.0 + j - 5);
 				for (k = 0; k < 12; k++) {
 					l = i + j * 12 + k;
 					if (l < 0 || l >= 128)
 						continue;
 					if (! (fh & 0x40)) {	/* major */
diff --git a/TiMidity++-2.13.2.original/./timidity/readmidi.c b/TiMidity++-2.13.2/./timidity/readmidi.c
index af9b8f7..b727337 100755
--- a/TiMidity++-2.13.2.original/./timidity/readmidi.c
+++ b/TiMidity++-2.13.2/./timidity/readmidi.c
@@ -73,10 +73,11 @@ enum
 int opt_trace_text_meta_event = 1;
 #else
 int opt_trace_text_meta_event = 0;
 #endif /* ALWAYS_TRACE_TEXT_META_EVENT */
 
+int opt_list_channels_per_track = 0;
 int opt_default_mid = 0;
 int opt_system_mid = 0;
 int ignore_midi_error = 1;
 ChannelBitMask quietchannels;
 struct midi_file_info *current_file_info = NULL;
@@ -104,10 +105,15 @@ void init_insertion_effect_gs(void);
 void init_multi_eq_xg(void);
 static void init_all_effect_xg(void);
 
 /* MIDI ports will be merged in several channels in the future. */
 int midi_port_number;
+int midi_port_is_fixed;
+int preselected_midi_port;
+char channel_usage[MAX_CHANNELS + 16];
+void clear_channel_usage(void);
+unsigned int set_channel_usage(unsigned int channel);
 
 /* These would both fit into 32 bits, but they are often added in
    large multiples, so it's simpler to have two roomy ints */
 static int32 sample_increment, sample_correction; /*samples per MIDI delta-t*/
 
@@ -178,20 +184,69 @@ UserInstrument *userinst_last = (UserInstrument *)NULL;
 
 void init_userinst();
 UserInstrument *get_userinst(int bank, int prog);
 void recompute_userinst(int bank, int prog);
 void recompute_userinst_altassign(int bank,int group);
+int (*port_by_tracknumber)(int track) = NULL;
+
+void clear_channel_usage(void)
+{
+    unsigned int ct;
+    for (ct = 0; ct < MAX_CHANNELS + 16; ct++) channel_usage[ct] = 0;
+}
+
+unsigned int set_channel_usage(unsigned int channel)
+{
+    if (channel < MAX_CHANNELS) {
+      if (midi_port_is_fixed)
+        channel_usage[channel + 16] = 1;
+       else
+        channel_usage[channel & 0x0f] = 1;
+    }
+    return(channel);
+}
+
+void report_used_channels(int trackno)
+{
+    int ct, port;
+    int found = 0, last_port = -1;
+    fprintf(stderr, "track %d uses channel(s)", trackno);
+    for (ct = 0; ct < MAX_CHANNELS + 16; ct++) if (channel_usage[ct])  {
+      port = ct >> 4;
+      fprintf(stderr, "%s", found ? "," : "");
+      if (port != last_port) {
+        if (port)
+          fprintf(stderr, NLS "  from port %d 'MIDI Out %d':", port, port);
+        else if (preselected_midi_port)
+          fprintf(stderr, NLS "  from preselected port %d 'MIDI Out %d':",
+            preselected_midi_port, preselected_midi_port);
+        else
+          fprintf(stderr, NLS "  from default port, i.e. 'MIDI Out 1':");
+        last_port = port;
+      }
+      fprintf(stderr, " %d", (ct & 0x0f) + 1);
+      if (port > 1)
+        fprintf(stderr, " (%d)", ct - 15);
+      else if (!port && preselected_midi_port)
+        fprintf(stderr, " (%d)", ct + 16 * preselected_midi_port - 15);
+      found = 1;
+    }
+    if (!found) fprintf(stderr, ": none");
+    fprintf(stderr, NLS);
+}
 
 int32 readmidi_set_track(int trackno, int rewindp)
 {
     current_read_track = trackno;
     memset(&chorus_status_gs.text, 0, sizeof(struct chorus_text_gs_t));
     if(karaoke_format == 1 && current_read_track == 2)
 	karaoke_format = 2; /* Start karaoke lyric */
     else if(karaoke_format == 2 && current_read_track == 3)
 	karaoke_format = 3; /* End karaoke lyric */
-    midi_port_number = 0;
+    /* midi_port_number = 0; ### I think this is misplaced here! 
+                             Moved it into read_smf_track()
+                             Arnold */
 
     if(evlist == NULL)
 	return 0;
     if(rewindp)
 	current_midi_point = evlist;
@@ -580,11 +635,11 @@ static int block_to_part(int block, int port)
 {
 	int p;
 	p = block & 0x0F;
 	if(p == 0) {p = 9;}
 	else if(p <= 9) {p--;}
-	return MERGE_CHANNEL_PORT2(p, port);
+	return set_channel_usage(MERGE_CHANNEL_PORT2(p, port));
 }
 
 /* Map XG types onto GS types.  XG should eventually have its own tables */
 static int set_xg_reverb_type(int msb, int lsb)
 {
@@ -1770,12 +1825,12 @@ int parse_sysex_event_multi(uint8 *val, int32 len, MidiEvent *evm)
 								midi_port_number ^ port), 0x80, 0x45);
 					} else {
 						SETMIDIEVENT(evm[0], 0, ME_SYSEX_GS_LSB,
 								block_to_part(val[5],
 								midi_port_number ^ port),
-								MERGE_CHANNEL_PORT2(val[7],
-								midi_port_number ^ port), 0x45);
+								set_channel_usage(MERGE_CHANNEL_PORT2(val[7],
+								midi_port_number ^ port)), 0x45);
 					}
 					num_events++;
 					break;
 				case 0x03:	/* Rx. Pitch Bend */
 					SETMIDIEVENT(evm[0], 0, ME_SYSEX_LSB, p, val[7], 0x48);
@@ -2803,11 +2858,11 @@ int parse_sysex_event_multi(uint8 *val, int32 len, MidiEvent *evm)
 					num_events++;
 				} else {
 					for (i = j = 0; i < MAX_CHANNELS; i++)
 						if (channel_tt & 1 << i) {
 							SETMIDIEVENT(evm[j], 0, ME_TEMPER_TYPE,
-									MERGE_CHANNEL_PORT(i),
+									set_channel_usage(MERGE_CHANNEL_PORT(i)),
 									val[7], (val[0] == 0x7f));
 							j++;
 						}
 					num_events += j;
 				}
@@ -2921,11 +2976,11 @@ int parse_sysex_event(uint8 *val, int32 len, MidiEvent *ev)
 	p = (uint8)((addr >> 8) & 0xF);
 	if(p == 0)
 	    p = 9;
 	else if(p <= 9)
 	    p--;
-	p = MERGE_CHANNEL_PORT(p);
+	p = set_channel_usage(MERGE_CHANNEL_PORT(p));
 
 	if(val[4] == 0x50) {	/* for double module mode */
 		p += 16;
 		addr = (((int32)0x40)<<16 |
 			((int32)val[5])<<8 |
@@ -3122,11 +3177,11 @@ int parse_sysex_event(uint8 *val, int32 len, MidiEvent *ev)
 				if (val[1] == 0x7f) {
 					SETMIDIEVENT(*ev, 0, ME_MASTER_VOLUME, 0,
 							vol & 0xff, vol >> 8 & 0xff);
 				} else {
 					SETMIDIEVENT(*ev, 0, ME_MAINVOLUME,
-							MERGE_CHANNEL_PORT(val[1]),
+							set_channel_usage(MERGE_CHANNEL_PORT(val[1])),
 							vol >> 8 & 0xff, 0);
 				}
 				return 1;
 			}
 			break;
@@ -3359,10 +3414,16 @@ static int read_smf_track(struct timidity_file *tf, int trackno, int rewindp)
     int lastchan, laststatus;
     int me, type, a, b, c;
     int i;
     int32 smf_at_time;
 
+    if(opt_list_channels_per_track) clear_channel_usage();
+    midi_port_number = midi_port_is_fixed = preselected_midi_port = 0;
+    if (port_by_tracknumber != NULL) {
+      preselected_midi_port = (midi_port_number = port_by_tracknumber(trackno)) + 1;
+    }
+
     smf_at_time = readmidi_set_track(trackno, rewindp);
 
     /* Check the formalities */
     if((tf_read(tmp, 1, 4, tf) != 4) || (tf_read(&len, 4, 1, tf) != 1))
     {
@@ -3418,11 +3479,12 @@ static int read_smf_track(struct timidity_file *tf, int trackno, int rewindp)
 	    if(type > 0 && type < 16)
 	    {
 		static char *label[] =
 		{
 		    "Text event: ", "Text: ", "Copyright: ", "Track name: ",
-		    "Instrument: ", "Lyric: ", "Marker: ", "Cue point: "
+		    "Instrument: ", "Lyric: ", "Marker: ", "Cue point: " ,
+                    "Program name", "MIDI port name: "
 		};
 
 		if(type == 5 || /* Lyric */
 		   (type == 1 && (opt_trace_text_meta_event ||
 				  karaoke_format == 2 ||
@@ -3533,12 +3595,28 @@ static int read_smf_track(struct timidity_file *tf, int trackno, int rewindp)
 			  current_read_track == 0))) {
 		  char *name = dumpstring(1, len, "Text: ", 1, tf);
 		  current_file_info->first_text = safe_strdup(fix_string(name));
 		  free(name);
 		}
+		else if(type == 9) /* MIDI Device (Port) Name */
+                { 
+		  char *name = dumpstring(1, len, "MIDI port name: ", 1, tf);
+                  if (!strnicmp(name, "MIDI Out ", 9)) {
+                    int num;
+                    num = -1;
+                    sscanf(&(name[9]), "%d", &num);
+                    if (num > 0) {
+                      midi_port_number = num - 1;
+                      midi_port_is_fixed = 1;
+			ctl->cmsg(CMSG_INFO, VERB_DEBUG,
+				  "(MIDI port '%s' = number %d)", name,  midi_port_number);
+                    }
+                  }
+                  free(name);
+		}
 		else
-		    dumpstring(type, len, label[(type>7) ? 0 : type], 0, tf);
+		    dumpstring(type, len, label[(type>9) ? 0 : type], 0, tf);
 	    }
 	    else
 	    {
 		switch(type)
 		{
@@ -3557,10 +3635,11 @@ static int read_smf_track(struct timidity_file *tf, int trackno, int rewindp)
 
 		  case 0x2F: /* End of Track */
 		    pos = tf_tell(tf);
 		    if(pos < next_pos)
 			tf_seek(tf, next_pos - pos, SEEK_CUR);
+                    if(opt_list_channels_per_track) report_used_channels(trackno);
 		    return 0;
 
 		  case 0x51: /* Tempo */
 		    a = tf_getc(tf);
 		    b = tf_getc(tf);
@@ -3624,10 +3703,11 @@ static int read_smf_track(struct timidity_file *tf, int trackno, int rewindp)
 				      "Warning: %s: Too shorten midi file.",
 				      current_filename);
 			    return -1;
 			}
 			midi_port_number &= 0xF;
+                        midi_port_is_fixed = 1;
 			ctl->cmsg(CMSG_INFO, VERB_DEBUG,
 				  "(MIDI port number %d)", midi_port_number);
 		    }
 		    else
 			skip(tf, len);
@@ -3645,11 +3725,11 @@ static int read_smf_track(struct timidity_file *tf, int trackno, int rewindp)
 	else /* MIDI event */
 	{
 	    a = me;
 	    if(a & 0x80) /* status byte */
 	    {
-		lastchan = MERGE_CHANNEL_PORT(a & 0x0F);
+		lastchan = set_channel_usage(MERGE_CHANNEL_PORT(a & 0x0F));
 		laststatus = (a >> 4) & 0x07;
 		if(laststatus != 7)
 		    a = tf_getc(tf) & 0x7F;
 	    }
 	    switch(laststatus)
