]> git.donarmstrong.com Git - lilypond.git/blob - mi2mu/main.cc
patch::: 0.1.19.jcn2: mi2mu pats
[lilypond.git] / mi2mu / main.cc
1 //
2 // main.cc -- implement silly main() entry point
3 // should have Root class.
4 //
5 // copyright 1997 Jan Nieuwenhuizen <jan@digicash.com>
6
7 #include <assert.h>
8 #include "string-convert.hh"
9 #include "lgetopt.hh"
10 #include "path.hh"
11 #include "duration-convert.hh"
12 #include "source.hh"
13
14 #include "mi2mu-global.hh"
15 #include "midi-score-parser.hh"
16 #include "mudela-item.hh"
17 #include "mudela-score.hh"
18 #include "version.hh"
19
20 // ugh
21 String filename_str_g;
22
23 // ugh
24 Mudela_score* mudela_score_l_g = 0;
25
26 Sources source;
27
28 static File_path path;
29
30 Verbose level_ver = NORMAL_ver;
31
32 /// just to make sure print_rat is linked in
33 static void (*rat_printer)(Moment const&);
34
35 void
36 usage()
37 {
38   LOGOUT(NORMAL_ver) <<
39         "Usage: mi2mu [options] midi-file\n"
40   "Translate midi-file to mudela\n"
41   "\n"
42   "Options:\n"
43   "  -b, --no-quantify      write exact durations, e.g.: a4*385/384\n"
44   "  -d, --debug            print lots of debugging stuff\n"
45   "  -h, --help             this help\n"
46   "  -I, --include=DIR      add DIR to search path\n"
47   "  -k, --key=ACC[:MINOR]  set key: ACC +sharps/-flats; :1 minor\n"
48   "  -n, --no-silly         assume no plets or double dots, smallest is 32\n"
49   "  -o, --output=FILE      set FILE as default output\n"
50   "  -p, --no-plets         assume no plets\n"
51   "  -q, --quiet            be quiet\n"
52   "  -s, --smallest=N       assume no shorter (reciprocal) durations than N\n"
53   "  -v, --verbose          be verbose\n"
54   "  -w, --warranty         show warranty and copyright\n"
55   "  -x, --no-double-dots   assume no double dotted notes\n"
56   ;
57 }
58
59 void
60 identify()
61 {
62   LOGOUT(NORMAL_ver) << mi2mu_version_str() << endl;
63 }
64   
65 void 
66 notice()
67 {
68   LOGOUT(NORMAL_ver) <<
69   "\n"
70   "Mi2mu, translate midi to mudela.\n"
71   "Copyright (C) 1997 by\n"
72   "  Jan Nieuwenhuizen <jan@digicash.com>\n"
73   "  Han-Wen Nienhuys <hanwen@stack.nl>\n"
74   "\n"
75   "    This program is free software; you can redistribute it and/or\n"
76   "modify it under the terms of the GNU General Public License version 2\n"
77   "as published by the Free Software Foundation.\n"
78   "\n"
79   "    This program is distributed in the hope that it will be useful,\n"
80   "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
81   "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
82   "General Public License for more details.\n"
83   "\n"
84   "    You should have received a copy (refer to the file COPYING) of the\n"
85   "GNU General Public License along with this program; if not, write to\n"
86   "the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,\n"
87   "USA.\n";
88 }
89
90 int
91 main (int argc_i, char* argv_sz_a[])
92 {
93   Mudela_key key (0, 0);
94   rat_printer = print_rat;      
95
96   Long_option_init long_option_init_a[] = 
97     {
98         {0, "no-quantify", 'b'},
99         {0, "debug", 'd'},
100         {0, "help", 'h'},
101         {1, "key", 'k'},
102         {0, "no-silly", 'n'},
103         {1, "output", 'o'},
104         {0, "no-plets", 'p'},
105         {0, "quiet", 'q'},
106         {1, "smallest", 's'},
107         {0, "verbose", 'v'},
108         {0, "warranty", 'w'},
109         {0, "no-double-dots", 'x'},
110         {0,0,0}
111   };
112   Getopt_long getopt_long (argc_i, argv_sz_a, long_option_init_a);
113
114   String output_str;
115   while (Long_option_init const* long_option_init_p = getopt_long())
116         switch (long_option_init_p->shortname) 
117           {
118         case 'b':
119             Duration_convert::no_quantify_b_s = true;
120             break;
121         case 'd':
122             level_ver = DEBUG_ver;
123             break;
124         case 'h':
125             identify();
126             usage();
127             exit (0);
128             break;
129 //      case 'I':
130 //          path->push (getopt_long.optional_argument_ch_C_);
131 //          break;
132         case 'k':
133           { 
134             String str = getopt_long.optional_argument_ch_C_;
135             int i = str.index_i (':');
136             i = (i >=0 ? i : str.length_i ());
137             key.accidentals_i_ = String_convert::dec2_i (str.left_str (i));
138             key.minor_i_ = (int)(bool)String_convert::dec2_i (str.mid_str (i + 1,1));
139             break;
140           }
141         case 'n':
142             Duration_convert::no_double_dots_b_s = true;
143             Duration_convert::no_triplets_b_s = true;
144             Duration_convert::no_smaller_than_i_s = 5;
145             break;
146         case 'o':
147             output_str = getopt_long.optional_argument_ch_C_;
148             break;
149         case 'p':
150             Duration_convert::no_triplets_b_s = true;
151             break;
152         case 'q':
153             level_ver = QUIET_ver;
154             break;
155         case 's': 
156           {
157                 int i = String_convert::dec2_i (getopt_long.optional_argument_ch_C_);
158                 if (!i) 
159                   {
160                     identify();
161                     usage();
162                     exit (2); //usage
163                   }
164                 Duration_convert::no_smaller_than_i_s = 
165                   Duration_convert::i2_type(i);
166               }
167             break;
168         case 'v':
169             level_ver = VERBOSE_ver;
170             break;
171         case 'w':
172             identify();
173             notice();
174             exit (0);
175             break;
176         case 'x':
177             Duration_convert::no_double_dots_b_s = true;
178             break;
179         default:
180             assert (0);
181             break;
182             }
183   
184   // flag -q must be checked first
185   identify();
186
187   path.add ("");
188   source.set_binary (true);
189   source.set_path (&path);
190
191   char const* arg_sz = 0;
192   while ( (arg_sz = getopt_long.get_next_arg ())) 
193     {
194         filename_str_g = arg_sz;
195         Midi_score_parser midi_parser;
196         Mudela_score* score_p = midi_parser.parse (arg_sz, &source);
197
198         if (!score_p)
199           return 1;
200
201         // if given on command line: override
202         score_p->mudela_key_l_ = &key;
203         mudela_score_l_g = score_p;
204         score_p->process();
205
206         if (!output_str.length_i ()) 
207           {
208             String d, dir, base, ext;
209             split_path (arg_sz, d, dir, base, ext);
210             output_str = base + ext + ".ly";
211           }
212
213         score_p->output (output_str);
214         delete score_p;
215     }
216   return 0;
217 }