summaryrefslogtreecommitdiff
path: root/synth/load.cpp
blob: 05538730eb598abe87ec6fe2d4936e14c227ce52 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
    Copyright (C) 2010-2012 Florian Jung
     
    This file is part of flo's FM synth.

    flo's FM synth is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    flo's FM synth is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with flo's FM synth.  If not, see <http://www.gnu.org/licenses/>.
*/


#include <string>
#include <fstream>
#include <cstdlib>
#include <sys/types.h>
#include <dirent.h>


#include "util.h"
#include "globals.h"
#include "parser.h"
#include "note_loader.h"


using namespace std;

void add_dir(string directory, bool complain=true)
{
	DIR *dir;
	dir=opendir(directory.c_str());
	if (dir)
	{
		string n, snum;
		int num;
		dirent *entry=NULL;
		
		while ( (entry=readdir(dir)) != NULL )
		{
			n=entry->d_name;
			if (fileext(n)=="prog")
			{
				snum=n.substr(0,3);
				if (isnum(snum))
				{
					num=atoi(snum.c_str());
					if ((num>=0) && (num<=127))
					{
						if (programfile[num]=="")
							programfile[num]=n;
						else
							output_verbose ("NOTE: found two or more .prog files with same number. ignoring '"+n+"'");
					}
					else
					{
						output_note ("NOTE: found .prog file with invalid number, ignoring it... ('"+n+"')");
					}
				}
				else
				{
					output_note ("NOTE: found .prog file which does not start with a number, ignoring it... ('"+n+"')");
				}
			}
		}
		closedir(dir);
	}
	else
	{
		if (complain)
			output_warning("WARNING: could not open directory '"+directory+"'!\n"
										 "         this is not fatal, ignoring and proceeding...");
	}
}

void read_config(const char *cfg, bool complain=true)
{
	char buf[2000];
	
	ifstream f;
	f.open(cfg);
	
	if (f.good())
	{
		string line;
		while (!f.eof())
		{
			f.getline(buf,sizeof(buf)/sizeof(*buf)-1);
			line=buf;
			line=trim_spaces(line);

			if ((line!="") && (line[0]!='#')) //ignore comments and empty lines
			{
				if (line.substr(0,string("include ").length())=="include ")
				{
					add_dir(trim_spaces(line.substr(string("include ").length())));
				}
				else
				{
					string var,val;
					var=trim_spaces(extract_var(line));
					val=trim_spaces(extract_val(line));

					if (isnum(var)) //programmzuweisung
					{
						int num=atoi(var.c_str());
						if ((num>=0) && (num<=127))
						{
							if (programfile[num]=="")
								programfile[num]=val;
							else
								output_verbose ("NOTE: program #"+IntToStr(num)+" has already been defined. ignoring it...");
						}
						else
						{
							output_warning("WARNING: number out of range (0..127) in program assignment. ignoring it...");
						}
					}
					else
					{
						float valf=atof(val.c_str());
							
						if (var=="frameskip")
						{
							#ifdef FRAMESKIP
								if (valf<0)
									output_warning("WARNING: invalid value for '"+var+"' ("+val+"). ignoring it...");
									
								if (frameskip==-1)
									frameskip=valf;
								else
									output_verbose("NOTE: ignoring value for frameskip, another setting overrides this.");
							#else
								output_warning("WARNING: while parsing conf: support for frameskipping isn't compiled in!");
							#endif
						}
						else
						{
							if (valf<=0)
								output_warning("WARNING: invalid value for '"+var+"' ("+val+"). ignoring it...");
							
							if ((var=="snh_freq") || (var=="sample_and_hold_freq"))
							{
								if (snh_freq_hz==0)
									snh_freq_hz=valf;
								else
									output_verbose("NOTE: ignoring value for sample_and_hold_freq, another setting overrides this.");
							}
							if (var=="lfo0_freq") //FINDLFO
							{
								if (lfo_freq_hz[0]==0)
									lfo_freq_hz[0]=valf;
								else
									output_verbose("NOTE: ignoring value for "+var+", another setting overrides this.");
							}
							if (var=="lfo1_freq")
							{
								if (lfo_freq_hz[1]==0)
									lfo_freq_hz[1]=valf;
								else
									output_verbose("NOTE: ignoring value for "+var+", another setting overrides this.");
							}
							if (var=="lfo2_freq")
							{
								if (lfo_freq_hz[2]==0)
									lfo_freq_hz[2]=valf;
								else
									output_verbose("NOTE: ignoring value for "+var+", another setting overrides this.");
							}
							else if ((var=="cleanup-interval") || (var=="clean"))
							{
								if (cleanup_interval_sec==0)
									cleanup_interval_sec=valf;
								else
									output_verbose("NOTE: ignoring value for cleanup-interval, another setting overrides this.");
							}
							else if ((var=="max_port") || (var=="max_port_time") || (var=="max_portamento_time"))
							{
								if (max_port_time_sec==0)
									max_port_time_sec=valf;
								else
									output_verbose("NOTE: ignoring value for max-portamento-time, another setting overrides this.");
							}
							else if (var=="lfo_update_freq")
							{
								if (lfo_update_freq_hz==0)
									lfo_update_freq_hz=valf;
								else
									output_verbose("NOTE: ignoring value for lfo_update_freq, another setting overrides this.");
							}
							else if (var=="filter_update_freq")
							{
								if (filter_update_freq_hz==0)
									filter_update_freq_hz=valf;
								else
									output_verbose("NOTE: ignoring value for filter_update_freq, another setting overrides this.");
							}
							else if ((var=="envelope_update_freq") || (var=="env_update_freq"))
							{
								if (envelope_update_freq_hz==0)
									envelope_update_freq_hz=valf;
								else
									output_verbose("NOTE: ignoring value for envelope_update_freq, another setting overrides this.");
							}
							else
								output_warning("WARNING: unknown variable '"+var+"'. ignoring it...");
						}
					}
				}
			}
		}
	}
	else
	{
		output_warning("WARNING: could not open config file '"+string(cfg)+"'.\nignoring this file...");
	}
}

bool load_program(string file, program_t& prog)
{
	if (file!="")
	{
		try
		{
			prog=parse(file);
			
			// try to load the appropriate .so file
			if (access(  (file+".so").c_str(), R_OK ) == 0)
			{
				try
				{
					load_note_from_so(file+".so", prog);
					output_verbose("NOTE: loaded shared object for program '"+file+"'");
				}
				catch (string err)
				{
					output_note("NOTE: could not load shared object '"+file+".so"+"':\n"
											"  "+err+"\n"
											"  this is not fatal, the note has been loaded properly, but generic\n"
											"  unoptimized (slow) code will be used.");
				}        
			}
			
			return true;
		}
		catch (string err)
		{
			output_warning("WARNING: error parsing '"+file+"': "+err+"\n"
										 "  this is not fatal, but the program has NOT been loaded! defaulting to a\n"
										 "  simple program and going on...");
			prog=default_program;
			
			return false;
		}
	}
	else
	{
		prog=default_program;
		
		return true;
	}
}