summaryrefslogtreecommitdiff
path: root/synth/util.cpp
blob: 2862fdb3c2078da246920e2e3faf16f2175d7761 (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
#include <iostream>
#include <sstream>

#include "util.h"
#include "globals.h"

string IntToStr(int i)
{
	ostringstream s;
	s<<i;
	return s.str();
}

string IntToStrHex(int i)
{
	ostringstream s;
	s<<std::hex << i;
	return s.str();
}

bool isnum(string s)
{
	for (size_t i=0;i<s.length();i++)
		if (!isdigit(s[i]))
			return false;
	
	return true;
}

bool isfloat(string s)
{
	return (s.find_first_not_of("0123456789.")==string::npos);
}


string remove_all_spaces(string s)
{
	string result;
	
	for (size_t i=0; i<s.length(); i++)
		if ((s[i]!=' ') && (s[i]!='\t'))
			result+=s[i];
	
	return result;
}

string trim_spaces(string s)
{
	string result;
	int i;
	
	for (i=0;i<s.length();i++)
		if ((s[i]!=' ') && (s[i]!='\t'))
			break;
	
	if (i!=s.length())
	{
		result=s.substr(i);
		for (i=result.length()-1;i>=0;i--)
			if ((result[i]!=' ') && (result[i]!='\t'))
				break;
		
		if (i>=0)
			return result.substr(0,i+1);
		else
			return "";
	}
	else
	{
		return "";
	}
}

void output_warning(string s)
{
	cout << s << endl;
	if (fatal_warnings) throw string(s);
}

void output_note(string s)
{
	if (!quiet) cout << s << endl;
}

void output_verbose(string s)
{
	if (verbose) cout << s << endl;
}

bool param_needs_index(parameter_enum p)
{
	switch (p)
	{
		case FILTER_ENABLED:

		case FILTER_ATTACK:
		case FILTER_DECAY:
		case FILTER_SUSTAIN:
		case FILTER_RELEASE:
		case FILTER_HOLD:
		case FILTER_ENV_AMOUNT:

		case FILTER_OFFSET:
		case FILTER_RESONANCE:

		case FILTER_TREMOLO:
		case FILTER_TREM_LFO:
		
		case SYNC_FACTOR:
			return false;
		
		default: return true;
	}
}

parameter_enum param_to_enum(string param)
{
	if (param=="mod")
		return MODULATION;
	else if (param=="out")
		return OUTPUT;
	else if (param=="waveform")
		return WAVEFORM;
	else if (param=="sync")
		return SYNC;
	else if (param=="factor")
		return FACTOR;
	else if (param=="trem")
		return TREMOLO;
	else if (param=="trem_lfo")
		return TREM_LFO;
	else if (param=="vib")
		return VIBRATO;
	else if (param=="vib_lfo")
		return VIB_LFO;
	else if (param=="attack") 
		return ATTACK;
	else if (param=="decay")
		return DECAY;
	else if (param=="sustain")
		return SUSTAIN;
	else if (param=="release")
		return RELEASE;
	else if (param=="hold")
		return HOLD;
	else if (param=="ksl")
		return KSL;
	else if (param=="ksr")
		return KSR;
	else if (param=="filter.enabled")
		return FILTER_ENABLED;
	else if (param=="filter.env_amount")
		return FILTER_ENV_AMOUNT;
	else if (param=="filter.attack")
		return FILTER_ATTACK;
	else if (param=="filter.decay")
		return FILTER_DECAY;
	else if (param=="filter.sustain")
		return FILTER_SUSTAIN;
	else if (param=="filter.release")
		return FILTER_RELEASE;
	else if (param=="filter.hold")
		return FILTER_HOLD;
	else if (param=="filter.offset")
		return FILTER_OFFSET;
	else if (param=="filter.resonance")
		return FILTER_RESONANCE;
	else if (param=="filter.trem")
		return FILTER_TREMOLO;
	else if (param=="filter.trem_lfo")
		return FILTER_TREM_LFO;
	else if (param=="sync_factor")
		return SYNC_FACTOR;
	else
		return UNKNOWN;
}

string str_before(string s, char delim, string onfail)
{
	size_t p;
	p=s.find(delim);
	if (p!=string::npos)
		return s.substr(0,p);
	else
		return onfail;
}

string str_after(string s, char delim, string onfail)
{
	size_t p;
	p=s.find(delim);
	if (p!=string::npos)
		return s.substr(p+1);
	else
		return onfail;
}

string extract_var(string s)
{
	return str_before(s,'=',"");
}

string extract_val(string s)
{
	return str_after(s,'=',s);
}

string fileext(string f)
{
	size_t pos;
	pos=f.rfind('.');
	if (pos!=string::npos)
		return f.substr(pos+1);
	else
		return "";
}