~ubuntu-branches/ubuntu/jaunty/midge/jaunty

« back to all changes in this revision

Viewing changes to midi2mg.pl

  • Committer: Bazaar Package Importer
  • Author(s): Mario Lang
  • Date: 2006-12-29 16:37:55 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061229163755-zebx8cfb2rt3gdhx
Tags: 0.2.41-2
Update debian/watch as well.

Show diffs side-by-side

added added

removed removed

Lines of Context:
61
61
my @track_ignore_list;
62
62
my $outfile_specified = 0;
63
63
 
64
 
my $tuplet_factor = 1;
65
64
my $progress_length = 30;
66
65
 
67
66
my $quiet = 0;
79
78
        die "missing output file after $arg\n" unless defined $mg_file;
80
79
        $outfile_specified = 1;
81
80
    }
82
 
    elsif ($arg =~ /^(-t|--tuplet-factor)$/) {
83
 
        $tuplet_factor = shift;
84
 
        die "bad or missing tuplet factor `$tuplet_factor'\n"
85
 
            unless $tuplet_factor > 0;
86
 
    }
87
81
    elsif ($arg =~ /^(-w|--ignore-wrong-track)$/) {
88
82
        $ignore_wrong_track = 1;
89
83
    }
142
136
       track numbers starting from 1.
143
137
   -N or --exclude-tracks i[,j...]
144
138
       As `-n' but excludes the listed tracks
145
 
   -t n or --tuplet-factor n
146
 
       An additional factor for files with odd note lengths,
147
 
       to prevent them being translated as decimals (n should
148
 
       be a prime number >3).
149
139
   -F or --no-factorise
150
140
       Do not factorise time values.
151
141
EOF
245
235
            $track_name = $event->[2];
246
236
            next;
247
237
        }
248
 
                
249
 
        my @event_tokens = split ' ', &get_event_string($event);
 
238
 
 
239
                my $tokens = &get_event_string($event);
 
240
                next unless defined $tokens;
 
241
        my @event_tokens = split ' ', $tokens;
250
242
        @track_tokens = (@track_tokens, @event_tokens)
251
243
            unless $event_tokens[0] eq '$ctrl' and
252
244
               $event_tokens[1] eq '121,0' and
463
455
        }
464
456
        $patch++;
465
457
        if ($dtime > 0) {
466
 
            return sprintf "/l%s/r \$patch $patch",
467
 
            &clicks_to_time($dtime);
 
458
            return sprintf "/l%s/r \$patch $patch", &clicks_to_time($dtime);
468
459
        }
469
460
        else {
470
461
            return "\$patch $patch";
480
471
        $value++ if $value == 8191;
481
472
        $value = int((8192.5 + $value) / 128);
482
473
        if ($dtime > 0) {
483
 
            return sprintf "/l%s/r \$pitch $value",
484
 
            &clicks_to_time($dtime);
 
474
            return sprintf "/l%s/r \$pitch $value", &clicks_to_time($dtime);
485
475
        }
486
476
        else {
487
477
            return "\$pitch $value";
491
481
        my (undef, $dtime, $tempo) = @$event;
492
482
        if ($dtime > 0) {
493
483
            return sprintf "/l%s/r \$tempo %s",
494
 
            &clicks_to_time($dtime), int(60000000 / $tempo);
 
484
                           &clicks_to_time($dtime), int(60000000 / $tempo);
495
485
        }
496
486
        else {
497
487
            return sprintf "\$tempo %s", int(60000000 / $tempo);
501
491
        my (undef, $dtime, $num, $den) = @$event;
502
492
        if ($dtime > 0) {
503
493
            return sprintf "/l%s/r \$time_sig $num/%s",
504
 
            &clicks_to_time($dtime), $den * $den;
 
494
                           &clicks_to_time($dtime), $den * $den;
505
495
        }
506
496
        else {
507
497
            return sprintf "\$time_sig $num/%s", $den * $den;
520
510
    }
521
511
    elsif ($event->[0] eq 'text_event') {
522
512
        my (undef, $dtime, $text) = @$event;
 
513
                return undef if length $text == 0;
523
514
 
524
515
        if ($dtime > 0) {
525
516
            return sprintf "/l%s/r \$text $text", &clicks_to_time($dtime);
572
563
        }
573
564
        if ($dtime > 0) {
574
565
            return sprintf "/l%s/r \$ctrl $ctrl,$value",
575
 
                &clicks_to_time($dtime);
 
566
                           &clicks_to_time($dtime);
576
567
        }
577
568
        else {
578
569
            return "\$ctrl $ctrl,$value";
580
571
    }
581
572
    elsif ($event->[0] eq 'marker') {
582
573
        my (undef, $dtime, $text) = @$event;
 
574
                return undef if length $text == 0;
583
575
 
584
576
        if ($dtime > 0) {
585
577
            return sprintf "/l%s/r \$marker \"$text\"",
586
 
                &clicks_to_time($dtime);
 
578
                           &clicks_to_time($dtime);
587
579
        }
588
580
        else {
589
581
            return "\$marker \"$text\"";
1171
1163
##
1172
1164
 
1173
1165
sub clicks_to_time {
1174
 
    my $clicks = shift; # time in clicks
1175
 
    my $time;           # n:d value to return
1176
 
    my $res = 960;      # resolution to work to (n => (1/n)th note)
1177
 
 
1178
 
    $res *= $tuplet_factor;
1179
 
    $time = ($clicks * $res) / (4 * $resolution);
1180
 
 
1181
 
    if ($res / $time == int($res / $time)) {
1182
 
        $time = $res / $time;
1183
 
    }
1184
 
    else {
1185
 
        if ($ctt_do_factorise) {
1186
 
            foreach my $i (2, 3, 5, 7, 11, 13, 17, 19) {
1187
 
                while (($time / $i == int($time / $i))
1188
 
                       and ($res / $i == int($res / $i))) {
1189
 
                
1190
 
                    $time /= $i;
1191
 
                    $res /= $i;
1192
 
                }
1193
 
            }
1194
 
        }
1195
 
 
1196
 
        # try to deal with fractions
1197
 
        unless ($time == int $time) {
1198
 
            my $factor = 1 / ($time - int $time);
1199
 
 
1200
 
            if ($factor > 1 and $factor == int $factor) {
1201
 
                $time *= $factor;
1202
 
                $res *= $factor;
1203
 
            }
1204
 
            else {
1205
 
                if ($factor - 3 < .0000001 and
1206
 
                    3 - $factor < .0000001) {
1207
 
 
1208
 
                    $time = int(3 * time);
1209
 
                    $res *= 3;
1210
 
                }
1211
 
                elsif (($factor * 2) - 3 < .0000001 and
1212
 
                       3 - ($factor * 2) < .0000001) {
1213
 
 
1214
 
                    $time = int(3 * $time);
1215
 
                    $res = int(3 * $res);
1216
 
                }
1217
 
            }
1218
 
        }
1219
 
 
1220
 
        $time = "$time:$res";
1221
 
    }
1222
 
 
1223
 
    return $time;
 
1166
    my $clicks = $_[0]; # time in clicks
 
1167
 
 
1168
    my $whole = 4 * $resolution;
 
1169
        my $hcf = &get_hcf($clicks, $whole);
 
1170
 
 
1171
        $clicks /= $hcf;
 
1172
        $whole /= $hcf;
 
1173
 
 
1174
        return ($clicks == 1)? $whole : "$clicks:$whole";
 
1175
}
 
1176
 
 
1177
##
 
1178
## Return the highest common factor of the two integer arguments.
 
1179
##
 
1180
 
 
1181
sub get_hcf {
 
1182
        my ($v1, $v2) = @_;
 
1183
 
 
1184
        die "Error: get_hcf(): both arguments must be integers"
 
1185
                unless $v1 == int $v1 and $v2 == int $v2;
 
1186
        
 
1187
        my $f1 = &get_factors($v1);
 
1188
        my $f2 = &get_factors($v2);
 
1189
 
 
1190
        my $hcf = 1;
 
1191
        my @fa;
 
1192
        
 
1193
        foreach my $f (@$f1) {
 
1194
                shift @$f2 while @$f2 > 0 and $f2->[0] < $f;
 
1195
 
 
1196
                if (@$f2 > 0 and $f2->[0] == $f) {
 
1197
                        $hcf *= $f;
 
1198
                        shift @$f2
 
1199
                }
 
1200
        }
 
1201
 
 
1202
        return $hcf;
 
1203
}
 
1204
 
 
1205
##
 
1206
## Return the prime factors of the integer argument.
 
1207
##
 
1208
 
 
1209
sub get_factors {
 
1210
        my $v = shift;
 
1211
        die "Error: get_factors(): argument must be an integer\n"
 
1212
                unless $v == int $v;
 
1213
 
 
1214
        my @factors;
 
1215
 
 
1216
        for my $prime (qw(2 3 5 7 11 13 17 19 23 29 31)) {
 
1217
                last if $v == 1;
 
1218
 
 
1219
                while ($v / $prime == int($v / $prime)) {
 
1220
                        last if $v == 1;
 
1221
                        $v /= $prime;
 
1222
                        push @factors, $prime;
 
1223
                }
 
1224
        }
 
1225
 
 
1226
        push @factors, $v unless $v == 1;
 
1227
        return \@factors;
1224
1228
}