-
Notifications
You must be signed in to change notification settings - Fork 0
/
zsp.cpp
404 lines (379 loc) · 11.6 KB
/
zsp.cpp
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/**
* mit -std=c++11 kompilieren !
*/
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <string>
#include "zsp.h"
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include "unquote.hpp"
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string/replace.hpp>
#include "sound.h"
#include "utils.h"
const char *overrideConfigNames[] = {"sound.boil", "sound.glocke", "sound.brake", "sound.entwaessern", "sound.abfahrt", "sound.horn", NULL };
const char* ablNames[CFG_FUNC_SOUND_N] = {"Sieden", "Glocke", "Bremsquietschen", "Entwässern", "Anfahrpfiff", "??", "E-Motor", "??", "Schaltwerk",
"??", "??", "??", "??", "??", "E Bremse", "Kurve", "??", "??"};
SoundType *cfg_soundFiles=NULL;
//SteamSoundType cfg_steamSoundFiles;
ZSPDataType ZSPData;
std::string soundsetPath;
/**
* sucht nach dem dateinamen für sample NUMBER=1234
* @param number
* @return filename
*/
Sample getSample(std::string number) {
// printf(" ---- %s \n",number.c_str());
for(ZSPDataType::iterator it = ZSPData.find("Sample"); it!=ZSPData.end(); it++) {
// it->second->dump();
// printf("sampe\n");
SectionValuesPtr sp = it->second;
std::string sampleNumber = sp->operator[]("NUMMER");
// printf(" sample -- number: %s\n", spIt->second.c_str());
if(sampleNumber == number) {
// printf(" ----------------- sample --------------\n");
// sp->dump();
std::string tmp = sp->operator[]("PFAD");
std::string pfad=boost::algorithm::unquote(tmp,'"','\b');
// printf(" %s/", pfad.c_str());
tmp = sp->operator[]("NAME");
std::string name=boost::algorithm::unquote(tmp,'"','\b');
// printf(" %s\n", name.c_str());
int loopStartPos=utils::stoi(sp->operator[]("L1"));
int loopEndPos=utils::stoi(sp->operator[]("L2"));
printf("getSample(%s) = '%s/%s' loop:%d-%d\n",number.c_str(), pfad.c_str(), name.c_str(), loopStartPos, loopEndPos);
return Sample(soundsetPath+"/"+boost::replace_all_copy(pfad, "\\", "/") + name, loopStartPos, loopEndPos);
}
}
throw std::runtime_error("invalid sample number ("+number+")");
}
void Sample::load(int volumeLevel) {
Sound::loadSoundFile(fileName, wav, volumeLevel);
if(this->loopEndPos==0) {
this->loopEndPos=this->wav.length();
}
}
SoundType *loadZSP() {
// std::multimap< int,std::string > map_data;
// map_data.emplace(5,"sdfsdf");
// map_data[7]["hello"] = 3.1415926;
printf("zimo sound projekt test\n");
std::string soundsetFile;
soundsetFile=config.get("soundset");
if(soundsetFile == NOT_SET) {
printf("no soundset configured ... disabling sound\n");
return NULL;
}
size_t slash=soundsetFile.find_last_of('/');
if(slash != std::string::npos) {
soundsetPath=soundsetFile.substr(0,slash);
}
FILE *f=fopen(soundsetFile.c_str(), "r");
if(!f) {
fprintf(stderr,"error loading sound fileset [%s] %s\n", soundsetFile.c_str(), strerror(errno));
abort();
}
char buffer[1024];
std::string section="";
SectionValuesPtr sp;
while(fgets(buffer, sizeof(buffer), f)) {
std::string line=buffer;
boost::algorithm::trim(line);
if(boost::starts_with(line,"\"/")) {
// printf("end section\n");
ZSPData.insert(std::pair<std::string, SectionValuesPtr>(section,sp) );
section="";
continue;
}
if(section == "" ) {
line=boost::algorithm::unquote(line);
if(line == "") {
continue;
}
sp.reset(new SectionValues()); // macht der ein auto free?
printf("new section:%s\n",line.c_str());
section=line;
continue;
}
std::string subsection, value;
size_t komma=line.find_first_of(',');
if(komma != std::string::npos) {
subsection=line.substr(0,komma);
subsection=boost::algorithm::unquote(subsection);
value=line.substr(komma+1);
} else {
subsection=line;
value="";
}
if(sp == NULL) {
printf("sp == null skipping\n");
continue;
}
// printf("add %s=%s\n", subsection.c_str(), value.c_str());
sp->push_back(std::pair<std::string, std::string>(subsection, value) );
continue;
/*
try {
if (boost::starts_with(argv[1], "--foo="))
int foo_value = boost::lexical_cast<int>(argv[1]+6);
} catch (boost::bad_lexical_cast) {
// bad parameter
}
// data[ */
}
bool found=false;
std::string soundSetName=config.get("sound.name");
if(soundSetName == NOT_SET) {
printf("sound.name not set in config file\n");
abort();
}
auto diSets=ZSPData.equal_range("DiSet");
for(ZSPDataType::iterator it=diSets.first; it!=diSets.second; ++it) {
//it->second->dump();
std::string setName=it->second->getName();
printf("DiSet SetName: %s\n", setName.c_str());
if(setName == soundSetName) {
printf("searching SCHWELLE, SAMPLE @ DiSet\n");
cfg_soundFiles=it->second->parseDiSet();
found=true;
break;
} else
printf("no match\n");
}
if(found) {
} else {
diSets=ZSPData.equal_range("DSet");
for(ZSPDataType::iterator it = diSets.first; it!=diSets.second; ++it) {
std::string setName=it->second->getName();
printf("DSet SetName: %s\n", setName.c_str());
if(setName == soundSetName) {
printf("searching SAMPLE @ DSet\n");
cfg_soundFiles=it->second->parseDSet();
found=true;
break;
}
}
}
if(!found) {
printf("Error: D*Set Name %s not found\n", soundSetName.c_str());
abort();
}
/*
printf("searching Function sounds\n");
auto range = ZSPData.equal_range("Func");
for(ZSPDataType::iterator it = range.first; it!=range.second; ++it) {
printf("############### [%s]\n",it->first.c_str());
it->second->dump();
std::string tmp=it->second->operator[]("SAMPLE");
printf(" SAMPLE=%s\n",tmp.c_str());
std::string fileName=getSampleFilename(tmp);
printf(" filename=%s\n", fileName.c_str());
}
*/
// Ablauf Sounds laden:
auto abl=ZSPData.equal_range("Abl");
for(ZSPDataType::iterator it=abl.first; it!=abl.second; ++it) {
it->second->dump();
printf("Abl\n");
SectionValuesPtr sp = it->second;
int ablLok=utils::stoi(sp->operator[]("LOK"));
if(ablLok >= 32) { // 0...31 dampfloks, 32...63 e-lok
ablLok-=32;
}
if(ablLok == cfg_soundFiles->lok) {
int nummer=utils::stoi(sp->operator[]("NUMMER"));
if(nummer < 0 || nummer >= CFG_FUNC_SOUND_N) {
printf("Error: invalid Abl::NUMMER:%d\n", nummer);
abort();
}
sp->dump();
cfg_soundFiles->funcSound[nummer] = getSample(sp->operator[]("SAMPLE"));
printf(" ----------------- Ablauf %i (%s) => %s --------------\n", nummer, ablNames[nummer], cfg_soundFiles->funcSound[nummer].fileName.c_str());
cfg_soundFiles->funcSoundVolume[nummer]=utils::stoi(sp->operator[]("LAUTST"));
} else {
printf(" ... skipping LOK=%d\n", ablLok);
}
}
// Ablauf Sound overrides:
printf("Sound override config:\n");
for(size_t i = 0 ; overrideConfigNames[i] ; i++) {
std::string overrideConfig=config.get(overrideConfigNames[i]);
if(overrideConfig != NOT_SET) {
cfg_soundFiles->funcSound[i].fileName = config.get(overrideConfigNames[i]);
}
}
cfg_soundFiles->dump();
/*
Sound s(cfg_soundFiles);
s.init();
s.run();
sleep(1);
s.setFahrstufe(0);
sleep(2);
s.setFahrstufe(1);
sleep(13);
s.setFahrstufe(2);
sleep(10);
s.setFahrstufe(0);
sleep(10);
s.setFahrstufe(-1);
sleep(15);
*/
/*
die Func sounds sind bei jedem Soundset bei einer anderen Fx Taste, daher verwenden wirs nicht
"Func"
"LOK",32
"NUMMER",2
"SAMPLE",16
"LAUTST",0
"LOOP",0
"SHORT",0
"/Func"
"Sample"
"NUMMER",16
"PFAD",""
"NAME","Hupe_kurz.wav"
"ART",12
"SIZE",18128
"L1",0
"L2",18127
"SR",2
"INFO","Pfeife kurz"
"LOOP",0
"SHORT",0
"FNR",34
"/Sample"
*/
/*
try {
cfg_funcSound[CFG_FUNC_SOUND_HORN] = config.get("sound.horn");
} catch(std::exception &e) {
printf("unable to get config/sound.horn\n");
}
try {
cfg_funcSound[CFG_FUNC_SOUND_ABFAHRT] = config.get("sound.abfahrt");
} catch(std::exception &e) {
printf("unable to get config/sound.abfahrt\n");
}
try {
cfg_funcSound[CFG_FUNC_SOUND_BOIL] = config.get("sound.boil");
} catch(std::exception &e) {
printf("unable to get config/sound.boil\n");
}
try {
cfg_funcSound[CFG_FUNC_SOUND_BRAKE] = config.get("sound.brake");
} catch(std::exception &e) {
printf("unable to get config/sound.brake\n");
}
*/
return cfg_soundFiles;
}
void SectionValues::dump() {
for(SectionValues::const_iterator it=this->begin(); it!=this->end(); it++) {
printf(" -- '%s' '%s'\n", it->first.c_str(), it->second.c_str());
}
}
std::string SectionValues::getName() {
std::string ret=this->operator[]("NAME");
if(ret != NOT_SET ) {
return boost::algorithm::unquote(ret,'"','\b');
} else
return NOT_SET;
}
/**
* FIXME: das setzt globale variablen!!!!!!
*/
DiSoundType *SectionValues::parseDiSet() {
assert(cfg_soundFiles == NULL);
DiSoundType *soundFiles=new DiSoundType();
printf("SectionValues::parseDiSet()\n");
soundFiles->lok=utils::stoi(this->operator[]("NUMMER"));
soundFiles->nsteps=utils::stoi(this->operator[]("STUFEN"))+1;
for(SectionValues::const_iterator it=this->begin(); it!=this->end(); it++) {
printf(" -- '%s' '%s'\n", it->first.c_str(), it->second.c_str());
if(it->first=="SAMPLE") {
size_t komma=it->second.find_first_of(',');
if(komma != std::string::npos) {
std::string nr = it->second.substr(0,komma);
Sample sample = getSample(it->second.substr(komma+1));
int n=atol(nr.c_str());
int fahrstufe=n/3;
assert(fahrstufe < soundFiles->nsteps);
printf(" -- Fahrstufe: %d/%d %s (curr limit %d) \n", fahrstufe,n%3, sample.fileName.c_str(), soundFiles->steps[fahrstufe].limit);
switch(n%3) {
case 0: soundFiles->steps[fahrstufe].up = sample; break;
case 1: soundFiles->steps[fahrstufe].run = sample; break;
case 2: soundFiles->steps[fahrstufe].down = sample; break;
}
}
}
if(it->first=="SCHWELLE") {
size_t komma=it->second.find_first_of(',');
int fahrstufe=atol(it->second.substr(0,komma).c_str());
if(fahrstufe < soundFiles->nsteps+1) {
size_t komma2=it->second.find_first_of(',',komma+1);
soundFiles->steps[fahrstufe].limit=atol(it->second.substr(komma+1,komma2).c_str());
printf("SCHWELLE: %d\n", soundFiles->steps[fahrstufe].limit);
} else {
printf(ANSI_RED "Error: invaid ZSP Fahrstufe/Schwelle [%d]\n" ANSI_DEFAULT, soundFiles->nsteps);
}
}
}
// Workaround: Fahrstufe 0 hat keine Schwelle
soundFiles->steps[0].limit=1;
return soundFiles;
}
/**
* "DSet"
"NUMMER",0
"NAME","BR01"
"STUFEN",2
"SLOTS",3 <- +1
(
slots+1 'ch' sample normal
slots+1 'ch' sample beim bremsen
slots+1 'ch' sample hoch last
zeit in ms
) STUFEN#
*/
SteamSoundType *SectionValues::parseDSet() {
SteamSoundType *soundFiles=new SteamSoundType();
printf("SectionValues::parseDSet()\n");
soundFiles->nslots=utils::stoi(this->operator[]("SLOTS"))+1;
assert(soundFiles->nslots <= SteamSoundStepType::maxSlots);
soundFiles->nsteps=utils::stoi(this->operator[]("STUFEN"));
assert(soundFiles->nsteps <= SteamSoundType::maxSteps);
soundFiles->lok=utils::stoi(this->operator[]("NUMMER"));
printf(" nslots:%d, nsteps:%d, lok #%d\n", soundFiles->nslots, soundFiles->nsteps, soundFiles->lok);
SectionValues::const_iterator it=this->begin();
while(true) {
if(it->second=="") {
break;
}
++it;
}
printf("stufen: %d slots:%d\n", soundFiles->nsteps, soundFiles->nslots);
for(int step = 0 ; step < soundFiles->nsteps; step++) {
for(int hml = 0 ; hml < 3 ; hml ++) {
for(int slot = 0 ; slot < soundFiles->nslots ; slot++) {
// SectionValues::const_iterator it=this->begin(); it!=this->end(); it++) {
printf(" -- '%s' '%s'\n", it->first.c_str(), it->second.c_str());
try {
//check if it->first is an int
utils::stoi(it->first);
soundFiles->steps[step].ch[hml][slot] = getSample(it->first);
} catch(...) {
printf("<<< invalid sound\n");
}
++it;
}
}
printf("reading ms\n");
soundFiles->steps[step].ms=utils::stoi(it->first);
++it;
}
return soundFiles;
}