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