Index: /trunk/tools/fix-cmc-patterns.pl
===================================================================
--- /trunk/tools/fix-cmc-patterns.pl	(revision 1746)
+++ /trunk/tools/fix-cmc-patterns.pl	(revision 1747)
@@ -7,23 +7,26 @@
 		if (/^TYPE C\r\nMUSIC (\w{4})\r\n/m) {
 			my $music = hex($1);
-			my $ff = index $_, "\xff\xff";
-			my ($start, $end) = unpack("x${ff}xxvv", $_);
-			if ($start == $music) {
-#				printf "MUSIC %04X START %04X END %04X %s\n", $music, $start, $end, $f;
-				my $o = $_;
-				for my $p (0 .. 63) {
-					my ($l, $h) = unpack("x${ff}x26x${p}Cx63C", $_);
-					if ($h == 0xff && $l != 0xff) {
-						printf "pattern %02x at %02x%02x %s\n", $p, $h, $l, $f;
-						die if substr($_, $ff + 26 + $p, 1) ne chr($l);
-						substr($_, $ff + 26 + $p, 1, "\xff");
+			my $i = 2 + index $_, "\xff\xff";
+			while ($i < length) {
+				my ($start, $end) = unpack("x${i}vv", $_);
+				# printf "MUSIC %04X START %04X END %04X %s\n", $music, $start, $end, $f;
+				if ($start == $music) {
+					my $o = $_;
+					for my $p (0 .. 63) {
+						my ($l, $h) = unpack("x${i}x24x${p}Cx63C", $_);
+						if ($h == 0xff && $l != 0xff) {
+							printf "pattern %02x at %02x%02x %s\n", $p, $h, $l, $f;
+							die if substr($_, $i + 24 + $p, 1) ne chr($l);
+							substr($_, $i + 24 + $p, 1, "\xff");
+						}
 					}
+					if ($_ ne $o) {
+						open F, ">../$f" and binmode F and print F $_ or die;
+					}
+					last;
 				}
-				if ($_ ne $o) {
-					open F, ">../aasma/$f" and binmode F and print F $_;
-				}
-			}
-			else {
 				printf "MUSIC %04X START %04X END %04X %s\n", $music, $start, $end, $f;
+				$i += 5 + $end - $start;
+				print "MUSIC block not found in $f\n" if $i >= length;
 			}
 		}
