Tweaked CFLAGS detection in config.sh.
[ahxm.git] / main.c
blob27f6eead8c56b57f737cf2bcc113ab2bf2292cf7
1 /*
3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2011 Angel Ortega <angel@triptico.com>
6 main.c - Miscellaneous functions and startup
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 http://www.triptico.com
26 #include "config.h"
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <limits.h>
33 #include "ahxm.h"
36 /** data **/
38 /* output information */
40 static char *devfile = NULL;
41 static char *driver = "default";
43 /* seconds to skip */
44 static int skip_secs = 0;
47 /** code **/
49 static void read_config(void)
54 static int main_usage(void)
56 printf("Ann Hell Ex Machina %s - Music Writing Software\n", VERSION);
57 printf("Copyright (C) 2003-2011 Angel Ortega <angel@triptico.com>\n");
58 printf("This software is covered by the GPL license. NO WARRANTY.\n\n");
59 printf("Usage: ahxm [options] {script.ahs}\n\n");
60 printf("Options:\n\n");
61 printf("-o {output file or device} Set output file [default: driver dependent].\n");
62 printf("-d {driver} Output driver [default: 'default'] (see below).\n");
63 printf("-L {path} Add path to script / data library search path.\n");
64 printf("-r {sampling rate} Set sampling rate [default: %d]\n",
65 ss_frequency);
66 printf("-c {# of channels} Force number of channels [default: use all]\n");
67 printf("-v {master volume} Set master volume [default: %02.02f]\n",
68 ss_master_volume);
69 printf("-s {seconds to skip} Seconds to skip from the start of the song.\n");
70 printf(" [default: 0, start from the beginning]\n");
71 printf("-z {track number} Play track number in a solo\n");
72 printf("-i {interpolation} Set interpolation algorithm\n");
73 printf(" (0, none; 1, linear; 2, cubic spline,\n");
74 printf(" 3, Lagrange) [default: %d]\n",
75 ss_interpolation);
76 printf("-b {size in frames} Set maximum wave buffer size in frames, so\n");
77 printf(" wave files bigger than this will be paged\n");
78 printf(" (-1, load full wave in memory) [default: %d]\n",
79 ss_page_size);
80 printf("-D {number} Verbosity level (0-3) [default: %d].\n",
81 verbose);
82 printf("-C {cue file name} Generate .cue file.\n");
83 printf("-T Show tracing information.\n");
84 printf("-M Output song to MIDI device instead of softsynth.\n");
85 printf("-I Interactive; read from MIDI device and\n");
86 printf(" use {script} as instrument (UNIMPLEMENTED).\n");
87 printf("\n");
88 printf("Internal sample size: %d bytes (floating point)\n",
89 (int) sizeof(sample_t));
90 printf("Library path: ");
91 libpath_print();
92 printf("\n");
93 printf("Configured drivers: %s\n", CONFOPT_DRIVERS);
95 return 1;
99 static int main_interactive(char *script_or_ins)
101 printf("UNIMPLEMENTED; use the 'midiin' program instead.\n");
102 return 0;
106 static int main_midi(char *script)
108 if (compile_ahs(script))
109 return 3;
111 if (midi_device_open(devfile) < 0) {
112 printf("Error: can't open MIDI device\n");
113 return 2;
116 midi_song_play(skip_secs);
118 midi_device_close();
120 return 0;
124 static int main_ss(char *script)
126 char *ptr;
128 /* is it an .sf2? create an ad-hoc mini program to list it */
129 if ((ptr = strrchr(script, '.')) != NULL && strcmp(ptr, ".sf2") == 0) {
130 char tmp[4096];
132 snprintf(tmp, sizeof(tmp), "{ sf2 \"%s\" }", script);
133 if (compile_ahs_string(tmp))
134 return 4;
136 else
137 if (compile_ahs(script))
138 return 3;
140 ss_song_render(skip_secs, driver, devfile);
142 if (verbose >= 1)
143 printf("clipped: %d optimal volume: %f\n",
144 ss_output_clipped, ss_optimal_volume);
146 return 0;
150 static int ahxm_main(int argc, char *argv[])
152 int ret = 1;
153 int n = 1;
154 char *opt = NULL;
155 char *script = NULL;
156 int midi = 0;
157 int interactive = 0;
159 read_config();
161 /* default path */
162 libpath_add("~/ahxmlib", 0);
164 /* specific converters */
165 transconv_add(".flac", ".wav", "flac -d -s -o '%s' '%s'");
166 transconv_add(".mp3", ".wav", "mpg321 -q -w '%s' '%s'");
168 /* fallout converter */
169 transconv_add(".*", ".wav", "ffmpeg '%s' -i '%s'");
171 if (argc == 1)
172 return main_usage();
174 while (n < argc - 1) {
175 opt = argv[n++];
177 if (strcmp(opt, "-o") == 0) {
178 /* set output device or file */
179 devfile = argv[n++];
181 else
182 if (strcmp(opt, "-d") == 0) {
183 /* set driver */
184 driver = argv[n++];
186 else
187 if (strcmp(opt, "-L") == 0) {
188 /* add path to library */
189 libpath_add(argv[n++], 0);
191 else
192 if (strcmp(opt, "-r") == 0) {
193 /* set sampling rate */
194 ss_frequency = atoi(argv[n++]);
196 else
197 if (strcmp(opt, "-c") == 0) {
198 /* set number of channels */
199 ss_nchannels = atoi(argv[n++]);
201 else
202 if (strcmp(opt, "-v") == 0) {
203 /* set master volume */
204 ss_master_volume = (double) atof(argv[n++]);
206 else
207 if (strcmp(opt, "-i") == 0) {
208 /* set interpolation type */
209 ss_interpolation = atoi(argv[n++]);
211 else
212 if (strcmp(opt, "-s") == 0) {
213 /* seconds to skip */
214 skip_secs = atoi(argv[n++]);
216 else
217 if (strcmp(opt, "-I") == 0) {
218 /* activate interactive mode */
219 interactive = 1;
221 else
222 if (strcmp(opt, "-M") == 0) {
223 /* activate MIDI mode */
224 midi = 1;
226 else
227 if (strcmp(opt, "-z") == 0) {
228 /* set solo track */
229 solo_track = atoi(argv[n++]);
231 else
232 if (strcmp(opt, "-b") == 0) {
233 /* set page size */
234 ss_page_size = atoi(argv[n++]);
236 /* if it's -1, set as 'no limit' */
237 if (ss_page_size < 0)
238 ss_page_size = INT_MAX;
240 else
241 if (strcmp(opt, "-D") == 0) {
242 /* debug information */
243 verbose = atoi(argv[n++]);
245 else
246 if (strcmp(opt, "-C") == 0) {
247 /* set cue file name */
248 ss_cue_file_name = argv[n++];
250 else
251 if (strcmp(opt, "-T") == 0)
252 trace = 1;
253 else
254 return main_usage();
257 script = argv[argc - 1];
259 if (*script == '-')
260 return main_usage();
262 if (interactive)
263 ret = main_interactive(script);
264 else
265 if (midi)
266 ret = main_midi(script);
267 else
268 ret = main_ss(script);
270 return ret;
274 int main(int argc, char *argv[])
276 return ahxm_main(argc, argv);