summaryrefslogtreecommitdiff
path: root/midisep.pl
blob: ebd2f9d10e083cc6fb4d89b934f09ae270323d5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/perl 
use Data::Dumper;
use Cwd;

our $DEBUG = 1;

our $MIDICSV_BIN = Cwd::abs_path("midicsv-1.1/midicsv");
our $CSVMIDI_BIN = Cwd::abs_path("midicsv-1.1/csvmidi");

our $CHANNEL_EVENTS = [ 
  'Note_on_c', 'Note_off_c', 'Pitch_bend_c', 'Control_c', 
  'Program_c', 'Channel_aftertouch_c', 'Poly_aftertouch_c',
];

our $SYSEX_EVENTS = [
  'System_exclusive', 'System_exclusive_packet'
];


sub usage{
  print STDERR sprintf('$>%s midifile [output_dir]'."\n", $0);
  exit 1;
}



sub error{
  my $msg = shift;
  print STDERR $msg . "\n";
  exit(1);
}

sub warn_sysex_found{
  my $event = shift;
  my $warning = sprintf("Sysex event found on channel %s\n", $event->{channel});
  print STDERR $warning;
}


sub correct_bad_note_ons{
  my $event = shift;
  if ($event->{type} =~ /Note_on/i && $event->{data}->[-1] == 0) {
    $event->{type} = "Note_off_c";
    $event->{data}->[2] =~ s/on/off/i; 
  }
  return $event;
}

sub file_to_list{
  my $file = shift;
  my $total_channels = shift;
  my $csvdata = `$MIDICSV_BIN $file`;
  my $list = [ 
    map { 
      chomp($_); 
      my $ctx = {};

      my $line = [ split(',', $_ ) ]; 
      $ctx->{data} = $line;
      $ctx->{type} = $line->[2];
      if (grep { $ctx->{type} =~ /$_/i } @$CHANNEL_EVENTS ){
        $ctx->{channel} = ($line->[3] =~ m/([0-9]+)/g)[0];
        if (!(grep { $ctx->{channel} == $_ } @$total_channels )){
          push(@$total_channels, $ctx->{channel});
        }
      }
#      [ @line ]; 
      $ctx;
    } split('\n', $csvdata) ];
  return $list;
}

sub add_to_all_outputs{
  my $event = shift;
  my $output_files = shift;
  foreach my $events (values %$output_files){
    push (@$events, join(',' , @{$event->{data}}));
  };
}

sub add_to_single_output{
  my $event = shift;
  my $output_files = shift;
  push(@{$output_files->{$event->{channel}}}, join(',', @{$event->{data}}));
}


sub process_event_list{
  my $event_list = shift;
  my $total_channels = shift;
  my $output_data = {};

  if ($event_list->[0]->{type} !~ /header/i){
    error("Bad header");
  }
  #marker header as type0 
  $event_list->[0]->{data}->[3] =~ s/[0-9]+/0/g;

  #check if file has more than one track/composition
  my $tracks_count = $header->{data}->[4];
  if ($tracks_count > 1){
    error("Multiple tracks in same file, not yet implemented.");
  }
  foreach my $channel(@$total_channels){
    $output_data->{$channel} = [];
  }

  #main loop
  foreach $event(@$event_list){
    if ( grep { $event->{type} =~ /$_/i } @$CHANNEL_EVENTS ){
      $event = correct_bad_note_ons($event);
      add_to_single_output( $event, $output_data );
    }
    elsif ( grep { $event->{type} =~ /$_/i} @$SYSEX_EVENTS ){
      #consider adding special handle for sysex events
      #currently just ignores them
      warn_sysex_found ($event); 
    }
    else {
      add_to_all_outputs( $event, $output_data);
    }
  };

  return $output_data;
}


sub data_to_files{
  my $output_data = shift;
  my $filename_prefix = shift;
  foreach my $channel (keys %$output_data){
    my $midi_filename = sprintf("%s-%s.mid", $filename_prefix, $channel);
    my $csv_filename = sprintf("/tmp/%s-%s.csv", $filename_prefix, $channel);
    open F, ">$csv_filename" or die $!;
    print F (join("\n", @{$output_data->{$channel}}));
    close(F);
    system("$CSVMIDI_BIN $csv_filename $midi_filename");
    unlink($csv_filename);
  }
}

sub main{
  if (scalar(@ARGV) < 1){
    if ($DEBUG){
      print STDERR "DEBUG: using example.mid as default midifile\n";
    }else{
      usage;
    }
  }
  
  my $file = $ARGV[0] || "example.mid";
  my $output_dir = $ARGV[2] || "./";
  our $total_channels = [];

  our $filename_prefix = $output_dir . ($file =~ m/^(.+)\./g)[0];
  my $event_list = file_to_list ($file, $total_channels);
  my $output_data = process_event_list ($event_list, $total_channels); 
  data_to_files($output_data, $filename_prefix);
#  
};
main;