Wednesday, July 10, 2013

Perl Script - Convert Reaper ReaBANK to M4L

Reaper uses patch files (xyz.ReaBank).  Max 4 Live has drop down menus (aka enums) that can act as erstwhile patch selection menus.  Sadly, Max4Live menu population requires space delimited quoted names, and ReaBanks look like:


// Roland MKS-50 programs.  Transcribed by ZoeB.

Bank 0 0 MKS-50 Bank

0 Poly synth 1
1 Jazz guitar
2 Xylophone
3 Low strings
4 Lead synth 1
5 Chorus guitar
6 Synth bass 1
7 Electro drum
8 High strings
9 Techno strings
...

Man! Well, in the prior posting, we setup our OSX environment to run perl scripts setup in a special folder.  So what I did was create a real simple, brief perl script that will convert a given .ReaBANK file into a format suitable for copying and pasting into Max4Live.  We'll give it a filename via a command line parameter, and create a ".m4l.txt" version that is ready to copy and paste into a max4live drop down menu aka enum.  Naturally, the original .ReaBank file is left alone.

So, as usuall--fire up TextEdit, make a new file, ensure it is plain text.  I saved this to Documents\Projects\Perl, and called it reaBank_to_m4l_enum.pl

1. Create reaBank_to_m4l_enum.pl

#!/usr/bin/perl

$file=$ARGV[0];
$incl_ctr_in_patch_name=1;

if (-e $file) {
$ctr=0;
$out_file=$file.".m4l.txt";
open(IN,$file);
open(OUT,">".$out_file);
$patch_num="";
while() {
chomp;
if (/^\s*(\d+)\s+(.*)$/) {
$patch_name=$2;
$patch_name=~s/\s$//;

if ($incl_ctr_in_patch_name) {
     $patch_num=sprintf("%03d",$ctr)." - ";
}

print OUT '"'.$patch_num.$patch_name.'" ';
$ctr++;
}
}
close IN;
print OUT "\n";
close OUT;
print `cat "$out_file"`;
}
else {
print "Couldn't find $file\n";
exit 1;
}

2.  Start Terminal and set perl script as executable

After launching terminal I type the following commands:
$ cd Documents/Projects/Perl
$ chmod a+x reaBank_to_m4l_enum.pl

now, we can run this program from anywhere in a Terminal session, and convert ReaBank files to something else.  This is not the most user friendly way....but...we'll get to that in a later post via Automator (maybe).

3.  Example use

$ cd wherever/we/keep/our/ReaBank/files
$ reaBank_to_m4l_enum.pl  

For example, suppose we downloaded Roland_MKS-50.ReaBank from the Reaper Stash (a repository of stuff for Reaper:
// Roland MKS-50 programs.  Transcribed by ZoeB.

Bank 0 0 MKS-50 Bank

0 Poly synth 1
1 Jazz guitar
2 Xylophone
3 Low strings
4 Lead synth 1
5 Chorus guitar
6 Synth bass 1
7 Electro drum
8 High strings
9 Techno strings
10 String organ
11 Fast strings
12 Long strings
13 Cello
14 Solo violin
15 Pizzicato
16 Piano 1
17 Electric piano 1
18 Electric piano 2
19 Piano 2
20 Electric piano 3
21 Clav.
22 Harpsichord
23 Piano pad
24 Organ 1
25 Organ 2
26 Cheesy organ
27 Pipe organ 1
28 Pipe organ 2
29 Voice pad
30 Sinusoidal
31 Voices 1
32 Brass 1
33 Syn rise
34 Spit valve
35 Fat synth
36 Arpeggiator
37 Velo-reso 1
38 Big brass
39 Pad 1
40 Lead synth 2
41 Lead synth 3
42 Flute
43 Lead synth 4
44 Sax 
45 Electric bass 1
46 Synth bass 2
47 Sequencer bass
48 Bells 1
49 Bell chime 1
50 Bell chime 2
51 Syn-bello
52 Marimba
53 Synth koto
54 Steel drum band
55 Harp
56 Tron blast
57 Noise shots
58 Twilight Zone
59 Scratchin'
60 Syn echo
61 Pole position
62 UFO
63 Timps
64 Brass 2
65 Brass 3
66 Brass horns
67 Fat brass 1
68 Trumpets
69 Brass swell
70 Poly synth 2
71 Poly synth 3
72 Bowed strings
73 Rich strings
74 Orchestra
75 Syn orchestra
76 String sweep
77 Solo violin 2
78 Double basses
79 Ominous
80 Piano 3
81 Electric piano 4
82 Loud piano
83 Piano FX
84 Clavichord
85 Harpsichord 2
86 Acoustic guitar
87 Bass piano
88 Organ 3
89 Organ 4
90 Chowa organ
91 Pipe organ 3
92 Accordion
93 Vocoder
94 Voices 2
95 Harmonica
96 Synth sweep
97 Poly pulse
98 Cosmo sweep
99 Chorus pluck
100 Bells 2
101 Vibe
102 Koto
103 Bell chime 3
104 Lead synth 5
105 Lead synth 6
106 Inv-solo
107 Clarinet
108 Oboe
109 Synth bass 3
110 Synth bass 4
111 Upright bass
112 Machines
113 Echo explosion
114 Ooops
115 Jet chord
116 Take-off
117 Whistle
118 Surprise
119 Oct jump
120 Jet
121 Helicopter
122 Dogs bark
123 WET
124 Oooh SCARY
125 What the..?
126 Synth toms
127 Kick

Now, we cd /to/the/dir that has this file, and run our perl script:
$ reaBank_to_m4l_enum.pl Roland_MKS-50.ReaBank
"000 - Poly synth 1" "001 - Jazz guitar" "002 - Xylophone" "003 - Low strings" "004 - Lead synth 1" "005 - Chorus guitar" "006 - Synth bass 1" "007 - Electro drum" "008 - High strings" "009 - Techno strings" "010 - String organ" "011 - Fast strings" "012 - Long strings" "013 - Cello" "014 - Solo violin" "015 - Pizzicato" "016 - Piano 1" "017 - Electric piano 1" "018 - Electric piano 2" "019 - Piano 2" "020 - Electric piano 3" "021 - Clav." "022 - Harpsichord" "023 - Piano pad" "024 - Organ 1" "025 - Organ 2" "026 - Cheesy organ" "027 - Pipe organ 1" "028 - Pipe organ 2" "029 - Voice pad" "030 - Sinusoidal" "031 - Voices 1" "032 - Brass 1" "033 - Syn rise" "034 - Spit valve" "035 - Fat synth" "036 - Arpeggiator" "037 - Velo-reso 1" "038 - Big brass" "039 - Pad 1" "040 - Lead synth 2" "041 - Lead synth 3" "042 - Flute" "043 - Lead synth 4" "044 - Sax" "045 - Electric bass 1" "046 - Synth bass 2" "047 - Sequencer bass" "048 - Bells 1" "049 - Bell chime 1" "050 - Bell chime 2" "051 - Syn-bello" "052 - Marimba" "053 - Synth koto" "054 - Steel drum band" "055 - Harp" "056 - Tron blast" "057 - Noise shots" "058 - Twilight Zone" "059 - Scratchin'" "060 - Syn echo" "061 - Pole position" "062 - UFO" "063 - Timps" "064 - Brass 2" "065 - Brass 3" "066 - Brass horns" "067 - Fat brass 1" "068 - Trumpets" "069 - Brass swell" "070 - Poly synth 2" "071 - Poly synth 3" "072 - Bowed strings" "073 - Rich strings" "074 - Orchestra" "075 - Syn orchestra" "076 - String sweep" "077 - Solo violin 2" "078 - Double basses" "079 - Ominous" "080 - Piano 3" "081 - Electric piano 4" "082 - Loud piano" "083 - Piano FX" "084 - Clavichord" "085 - Harpsichord 2" "086 - Acoustic guitar" "087 - Bass piano" "088 - Organ 3" "089 - Organ 4" "090 - Chowa organ" "091 - Pipe organ 3" "092 - Accordion" "093 - Vocoder" "094 - Voices 2" "095 - Harmonica" "096 - Synth sweep" "097 - Poly pulse" "098 - Cosmo sweep" "099 - Chorus pluck" "100 - Bells 2" "101 - Vibe" "102 - Koto" "103 - Bell chime 3" "104 - Lead synth 5" "105 - Lead synth 6" "106 - Inv-solo" "107 - Clarinet" "108 - Oboe" "109 - Synth bass 3" "110 - Synth bass 4" "111 - Upright bass" "112 - Machines" "113 - Echo explosion" "114 - Ooops" "115 - Jet chord" "116 - Take-off" "117 - Whistle" "118 - Surprise" "119 - Oct jump" "120 - Jet" "121 - Helicopter" "122 - Dogs bark" "123 - WET" "124 - Oooh SCARY" "125 - What the..?" "126 - Synth toms" "127 - Kick" 

Now we can select that long line of output and simply paste into a max 4 live enum!  In addition, a file Roland_MKS-50.ReaBank.m4l.txt, was created; this file contains identical output to above, so if for some reason we forget to copy and paste right away, we can easily get back to the converted ReaBank output.  The original .ReaBank file is, of course, untouched.  Now, I can go and leech all the .ReaBank files I need for any custom max 4 live patch selection midi devices!

Note

This perl script, like all perl scripts, is flaky and not 100% fool proof.  It might not do the right thing every time--for example, comments could really jack up the output. Yes we could accommodate them, some kind of s/\/\/.*//g search and replace, but honestly--it can be just as easy to delete the offending lines out of a problematic .ReaBank!  The goal is not to perfectly convert every file to something Max4Live can use, just to make it easier to convert.