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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>NeL: sample_bank.cpp Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.3.6 -->
<div class="qindex"> <form class="search" action="search.php" method="get">
<a class="qindex" href="main.html">Main Page</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical List</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="namespacemembers.html">Namespace Members</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related Pages</a> | <span class="search"><u>S</u>earch for <input class="search" type="text" name="query" value="" size="20" accesskey="s"/></span></form></div>
<h1>sample_bank.cpp</h1><a href="a04923.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001
00007 <span class="comment">/* Copyright, 2001 Nevrax Ltd.</span>
00008 <span class="comment"> *</span>
00009 <span class="comment"> * This file is part of NEVRAX NEL.</span>
00010 <span class="comment"> * NEVRAX NEL is free software; you can redistribute it and/or modify</span>
00011 <span class="comment"> * it under the terms of the GNU General Public License as published by</span>
00012 <span class="comment"> * the Free Software Foundation; either version 2, or (at your option)</span>
00013 <span class="comment"> * any later version.</span>
00014 <span class="comment"></span>
00015 <span class="comment"> * NEVRAX NEL is distributed in the hope that it will be useful, but</span>
00016 <span class="comment"> * WITHOUT ANY WARRANTY; without even the implied warranty of</span>
00017 <span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU</span>
00018 <span class="comment"> * General Public License for more details.</span>
00019 <span class="comment"></span>
00020 <span class="comment"> * You should have received a copy of the GNU General Public License</span>
00021 <span class="comment"> * along with NEVRAX NEL; see the file COPYING. If not, write to the</span>
00022 <span class="comment"> * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,</span>
00023 <span class="comment"> * MA 02111-1307, USA.</span>
00024 <span class="comment"> */</span>
00025
00026 <span class="preprocessor">#include "<a class="code" href="a05031.html">stdsound.h</a>"</span>
00027
00028 <span class="preprocessor">#include "<a class="code" href="a04924.html">sample_bank.h</a>"</span>
00029 <span class="preprocessor">#include "<a class="code" href="a04994.html">driver/sound_driver.h</a>"</span>
00030 <span class="preprocessor">#include "<a class="code" href="a04094.html">driver/buffer.h</a>"</span>
00031 <span class="preprocessor">#include "<a class="code" href="a04745.html">nel/misc/path.h</a>"</span>
00032 <span class="preprocessor">#include "<a class="code" href="a04286.html">nel/misc/file.h</a>"</span>
00033 <span class="preprocessor">#include "<a class="code" href="a04037.html">async_file_manager_sound.h</a>"</span>
00034 <span class="preprocessor">#include "<a class="code" href="a04049.html">background_sound_manager.h</a>"</span>
00035 <span class="preprocessor">#include "<a class="code" href="a04992.html">sound_bank.h</a>"</span>
00036
00037
00038 <span class="keyword">using</span> <span class="keyword">namespace </span>std;
00039 <span class="keyword">using</span> <span class="keyword">namespace </span>NLMISC;
00040
00041
00042 <span class="keyword">namespace </span>NLSOUND {
00043
<a name="l00045"></a><a class="code" href="a05381.html#a49">00045</a> <a class="code" href="a04558.html#a11">uint32</a> <a class="code" href="a05381.html#a49">ASYNC_LOADING_SPLIT</a> = 10; <span class="comment">// 10 file by 10 file</span>
00046
<a name="l00047"></a><a class="code" href="a03346.html#NLSOUND_1_1CSampleBankv0">00047</a> <a class="code" href="a03346.html#NLSOUND_1_1CSampleBanky0">CSampleBank::TSampleBankContainer</a> CSampleBank::_Banks;
<a name="l00048"></a><a class="code" href="a03346.html#NLSOUND_1_1CSampleBankv1">00048</a> <a class="code" href="a04558.html#a15">uint</a> CSampleBank::_LoadedSize = 0;
00049
<a name="l00050"></a><a class="code" href="a03346.html#NLSOUND_1_1CSampleBankv2">00050</a> <a class="code" href="a03346.html#NLSOUND_1_1CSampleBanky1">CSampleBank::TVirtualBankCont</a> CSampleBank::_VirtualBanks;
00051
00052
00053
00054
<a name="l00055"></a><a class="code" href="a03346.html#NLSOUND_1_1CSampleBanke4">00055</a> <span class="keywordtype">void</span> CSampleBank::init(<a class="code" href="a02551.html">NLGEORGES::UFormElm</a> *mixerConfig)
00056 {
00057 NL_ALLOC_CONTEXT(NLSOUND_CSampleBank);
00058 <span class="keywordflow">if</span> (mixerConfig == 0)
00059 <span class="keywordflow">return</span>;
00060
00061 <a class="code" href="a02551.html">NLGEORGES::UFormElm</a> *virtualBanks;
00062 mixerConfig-><a class="code" href="a02551.html#NLGEORGES_1_1UFormElma17">getNodeByName</a>(&virtualBanks, <span class="stringliteral">".VirtualBanks"</span>);
00063 <span class="keywordflow">if</span> (virtualBanks == 0)
00064 <span class="keywordflow">return</span>;
00065
00066 <a class="code" href="a04558.html#a15">uint</a> <a class="code" href="a04223.html#a587">size</a>;
00067 virtualBanks-><a class="code" href="a02551.html#NLGEORGES_1_1UFormElma3">getArraySize</a>(<a class="code" href="a04223.html#a587">size</a>);
00068
00069 <span class="keywordflow">for</span> (<a class="code" href="a04558.html#a15">uint</a> i=0; i<<a class="code" href="a04223.html#a587">size</a>; ++i)
00070 {
00071 <a class="code" href="a02551.html">NLGEORGES::UFormElm</a> *virtualBank;
00072 virtualBanks-><a class="code" href="a02551.html#NLGEORGES_1_1UFormElma1">getArrayNode</a>(&virtualBank, i);
00073
00074 <span class="keywordflow">if</span> (virtualBank != 0)
00075 {
00076 std::vector<TFilteredBank> vfb;
00077 std::string virtualName;
00078 virtualBank-><a class="code" href="a02551.html#NLGEORGES_1_1UFormElma44">getValueByName</a>(virtualName, <span class="stringliteral">".VirtualName"</span>);
00079 <a class="code" href="a02551.html">NLGEORGES::UFormElm</a> *realBanks;
00080 virtualBank-><a class="code" href="a02551.html#NLGEORGES_1_1UFormElma17">getNodeByName</a>(&realBanks, <span class="stringliteral">".FilteredBank"</span>);
00081 <span class="keywordflow">if</span> (realBanks != 0)
00082 {
00083 <a class="code" href="a04558.html#a15">uint</a> size2;
00084 realBanks-><a class="code" href="a02551.html#NLGEORGES_1_1UFormElma3">getArraySize</a>(size2);
00085
00086 <span class="keywordflow">for</span> (<a class="code" href="a04558.html#a15">uint</a> j=0; j<size2; ++j)
00087 {
00088 <a class="code" href="a03347.html">TFilteredBank</a> fb;
00089 std::string bankName;
00090 <a class="code" href="a02551.html">NLGEORGES::UFormElm</a> *realBank;
00091 realBank-><a class="code" href="a02551.html#NLGEORGES_1_1UFormElma1">getArrayNode</a>(&realBank, j);
00092
00093 realBank-><a class="code" href="a02551.html#NLGEORGES_1_1UFormElma44">getValueByName</a>(bankName, <span class="stringliteral">".SampleBank"</span>);
00094 fb.<a class="code" href="a03347.html#NLSOUND_1_1CSampleBank_1_1TFilteredBanko0">BankName</a> = CStringMapper::map(bankName);
00095 realBank-><a class="code" href="a02551.html#NLGEORGES_1_1UFormElma44">getValueByName</a>(fb.<a class="code" href="a03347.html#NLSOUND_1_1CSampleBank_1_1TFilteredBanko1">Filter</a>, <span class="stringliteral">".Filter"</span>);
00096
00097 vfb.push_back(fb);
00098 }
00099 }
00100
00101 <span class="keywordflow">if</span> (!vfb.empty())
00102 {
00103 <a class="code" href="a03346.html#NLSOUND_1_1CSampleBankv2">_VirtualBanks</a>.insert(std::make_pair(NLMISC::CStringMapper::map(virtualName), vfb));
00104 <span class="comment">// create the sample bank</span>
00105 <span class="keyword">new</span> <a class="code" href="a03346.html#NLSOUND_1_1CSampleBanka1">CSampleBank</a>(virtualName, NULL);
00106 }
00107 }
00108 }
00109 }
00110
00111
00112
00113
00114 <span class="comment">// ********************************************************</span>
<a name="l00115"></a><a class="code" href="a03346.html#NLSOUND_1_1CSampleBanke0">00115</a> <a class="code" href="a03346.html">CSampleBank</a> *CSampleBank::findSampleBank(<span class="keyword">const</span> NLMISC::TStringId &filename)
00116 {
00117 TSampleBankContainer::iterator it(_Banks.find(filename));
00118
00119 <span class="keywordflow">if</span> (it != _Banks.end())
00120 <span class="keywordflow">return</span> it->second;
00121
00122 <span class="keywordflow">return</span> NULL;
00123 }
00124
00125 <span class="comment">// ********************************************************</span>
00126
<a name="l00127"></a><a class="code" href="a03346.html#NLSOUND_1_1CSampleBanke1">00127</a> <a class="code" href="a02298.html">IBuffer</a>* CSampleBank::get(<span class="keyword">const</span> NLMISC::TStringId &name)
00128 {
00129 <a class="code" href="a02298.html">IBuffer</a>* <a class="code" href="a04223.html#a589">buffer</a>;
00130 TSampleBankContainer::iterator iter;
00131
00132 <span class="keywordflow">for</span> (iter = _Banks.begin(); iter != _Banks.end(); ++iter)
00133 {
00134 <a class="code" href="a04223.html#a589">buffer</a> = iter->second->getSample(name);
00135 <span class="keywordflow">if</span> (<a class="code" href="a04223.html#a589">buffer</a> != 0)
00136 {
00137 <span class="keywordflow">return</span> <a class="code" href="a04223.html#a589">buffer</a>;
00138 }
00139 }
00140
00141 <span class="comment">//nlwarning ("Try to get an unknown sample '%s'", name);</span>
00142 <span class="keywordflow">return</span> 0;
00143 }
00144
<a name="l00145"></a><a class="code" href="a03346.html#NLSOUND_1_1CSampleBanke6">00145</a> <span class="keywordtype">void</span> CSampleBank::reload(<span class="keywordtype">bool</span> async)
00146 {
00147 TSampleBankContainer::iterator first(_Banks.begin()), last(_Banks.end());
00148
00149 <span class="keywordflow">for</span> (; first != last; ++first)
00150 {
00151 first->second->unload();
00152 first->second->load(async);
00153 }
00154 }
00155
00156
<a name="l00157"></a><a class="code" href="a03346.html#NLSOUND_1_1CSampleBanke2">00157</a> <span class="keywordtype">void</span> CSampleBank::getLoadedSampleBankInfo(std::vector<std::pair<std::string, uint> > &result)
00158 {
00159 result.clear();
00160
00161 TSampleBankContainer::iterator first(_Banks.begin()), last(_Banks.end());
00162 <span class="keywordflow">for</span> (; first != last; ++first)
00163 {
00164 std::pair<std::string, uint> p;
00165 <span class="keywordflow">if</span> (first->second->isLoaded())
00166 {
00167 p.first = <a class="code" href="a03450.html#NLMISC_1_1CStringMappere4">NLMISC::CStringMapper::unmap</a>(first->first);
00168 p.second = first->second->getSize();
00169 result.push_back(p);
00170 }
00171 }
00172 }
00173
00174
00175
00176
00177
00178 <span class="comment">// ********************************************************</span>
00179
<a name="l00180"></a><a class="code" href="a03346.html#NLSOUND_1_1CSampleBanka1">00180</a> CSampleBank::CSampleBank(<span class="keyword">const</span> std::string& name, <a class="code" href="a03419.html">ISoundDriver</a> *sd)
00181 : _SoundDriver(sd), _Name(CStringMapper::map(name)), _Loaded(false), _LoadingDone(true), _ByteSize(0)
00182 {
00183 <span class="comment">// _Name = CFile::getFilenameWithoutExtension(_Path);</span>
00184 _Banks.insert(make_pair(_Name, <span class="keyword">this</span>));
00185 }
00186
00187
00188 <span class="comment">// ********************************************************</span>
00189
<a name="l00190"></a><a class="code" href="a03346.html#NLSOUND_1_1CSampleBanka8">00190</a> CSampleBank::~CSampleBank()
00191 {
00192 CAudioMixerUser::instance()->unregisterUpdate(<span class="keyword">this</span>);
00193 <span class="keywordflow">while</span> (!<a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr2">_LoadingDone</a>)
00194 {
00195 <span class="comment">// need to wait for loading end.</span>
00196 <a class="code" href="a05378.html#a256">nlSleep</a>(100);
00197 }
00198
00199 <span class="keywordflow">if</span> (_Loaded)
00200 <a class="code" href="a03346.html#NLSOUND_1_1CSampleBanka7">unload</a>();
00201
00202 <span class="comment">// remove the bank from the list of known banks</span>
00203 TSampleBankContainer::iterator iter(_Banks.begin()), end(_Banks.end());
00204
00205 <span class="keywordflow">for</span> (; iter != end; ++iter)
00206 {
00207 <span class="keywordflow">if</span> (iter->second == <span class="keyword">this</span>)
00208 {
00209 _Banks.erase(iter);
00210 <span class="keywordflow">break</span>;
00211 }
00212 }
00213
00214
00215 <span class="comment">// delete all the samples.</span>
00216 <span class="keywordflow">while</span> (!<a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr5">_Samples</a>.empty())
00217 {
00218 <span class="keyword">delete</span> <a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr5">_Samples</a>.begin()->second;
00219 <a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr5">_Samples</a>.erase(<a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr5">_Samples</a>.begin());
00220 }
00221
00222 <a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr5">_Samples</a>.clear();
00223 }
00224
00225
00226 <span class="comment">// ********************************************************</span>
00227
<a name="l00228"></a><a class="code" href="a03346.html#NLSOUND_1_1CSampleBanka6">00228</a> <span class="keywordtype">void</span> CSampleBank::load(<span class="keywordtype">bool</span> async)
00229 {
00230 <span class="comment">// TODO : add async loading support !</span>
00231
00232 TVirtualBankCont::iterator it(<a class="code" href="a03346.html#NLSOUND_1_1CSampleBankv2">_VirtualBanks</a>.find(_Name));
00233 <span class="keywordflow">if</span> (it != <a class="code" href="a03346.html#NLSOUND_1_1CSampleBankv2">_VirtualBanks</a>.end())
00234 {
00235 <span class="comment">// this is a virtual sample bank !</span>
00236 <a class="code" href="a04199.html#a1">nlinfo</a>(<span class="stringliteral">"Loading virtual sample bank %s"</span>, CStringMapper::unmap(_Name).c_str());
00237
00238 <a class="code" href="a02235.html">CAudioMixerUser</a> *mixer = CAudioMixerUser::instance();
00239 <span class="keyword">const</span> CAudioMixerUser::TBackgroundFlags &flags = mixer-><a class="code" href="a02235.html#NLSOUND_1_1CAudioMixerUsera20">getBackgroundFlags</a>();
00240
00241 <span class="keywordflow">for</span> (<a class="code" href="a04558.html#a15">uint</a> i=0; i<it->second.size(); ++i)
00242 {
00243 <span class="keywordflow">if</span> (flags.Flags[it->second[i].Filter])
00244 {
00245 <a class="code" href="a03346.html">CSampleBank</a> *bank = <a class="code" href="a03346.html#NLSOUND_1_1CSampleBanke0">findSampleBank</a>(it->second[i].BankName);
00246 <span class="keywordflow">if</span> (bank)
00247 bank-><a class="code" href="a03346.html#NLSOUND_1_1CSampleBanka6">load</a>(async);
00248 }
00249 }
00250 }
00251
00252 <a class="code" href="a04199.html#a1">nlinfo</a>(<span class="stringliteral">"Loading sample bank %s %"</span>, CStringMapper::unmap(_Name).c_str(), async?<span class="stringliteral">""</span>:<span class="stringliteral">"Asynchronously"</span>);
00253
00254 vector<string> filenames;
00255 <span class="comment">// vector<string>::iterator iter;</span>
00256
00257 <span class="keywordflow">if</span> (_Loaded)
00258 {
00259 <a class="code" href="a04199.html#a2">nlwarning</a>(<span class="stringliteral">"Trying to load an already loaded bank : %s"</span>, CStringMapper::unmap(_Name).c_str ());
00260 <span class="keywordflow">return</span>;
00261 }
00262
00263
00264 <span class="comment">// Load the sample bank from the builded sample_bank file.</span>
00265 string bankName(CStringMapper::unmap(_Name)+<span class="stringliteral">".sample_bank"</span>);
00266 string filename = CPath::lookup(bankName);
00267 <span class="keywordflow">if</span> (filename.empty())
00268 {
00269 <a class="code" href="a04199.html#a2">nlwarning</a>(<span class="stringliteral">"Trying to load an unknown sample bank [%s]"</span>, bankName.c_str());
00270 <span class="keywordflow">return</span>;
00271 }
00272
00273 <span class="keywordflow">try</span>
00274 {
00275
00276 <a class="code" href="a02653.html">CIFile</a> sampleBank(filename);
00277
00278 <a class="code" href="a02240.html">CAudioMixerUser::TSampleBankHeader</a> sbh;
00279 sampleBank.<a class="code" href="a02270.html#NLMISC_1_1IStreama5">serial</a>(sbh);
00280 <a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr2">_LoadingDone</a> = <span class="keyword">false</span>;
00281
00282 <a class="code" href="a04558.html#a10">sint32</a> seekStart = sampleBank.<a class="code" href="a02653.html#NLMISC_1_1CIFilea8">getPos</a>();
00283
00284
00285 <a class="code" href="a04558.html#a7">uint8</a> *<a class="code" href="a04223.html#a637">data</a> = 0;
00286 <a class="code" href="a04558.html#a15">uint</a> i;
00287 <span class="keywordflow">for</span> (i=0; i<sbh.<a class="code" href="a02240.html#NLSOUND_1_1CAudioMixerUser_1_1TSampleBankHeadero1">Name</a>.size(); ++i)
00288 {
00289 <a class="code" href="a02298.html">IBuffer</a> *ibuffer = _SoundDriver-><a class="code" href="a03419.html#NLSOUND_1_1ISoundDrivera2">createBuffer</a>();
00290 <a class="code" href="a04199.html#a6">nlassert</a>(ibuffer);
00291
00292 TStringId nameId = CStringMapper::map(CFile::getFilenameWithoutExtension(sbh.<a class="code" href="a02240.html#NLSOUND_1_1CAudioMixerUser_1_1TSampleBankHeadero1">Name</a>[i]));
00293 ibuffer-><a class="code" href="a02298.html#NLSOUND_1_1IBuffera9">presetName</a>(nameId);
00294
00295 <span class="comment">/* {</span>
00296 <span class="comment"> sint16 *data16 = new sint16[sbh.NbSample[i]];</span>
00297 <span class="comment"> IBuffer::TADPCMState state;</span>
00298 <span class="comment"> state.PreviousSample = 0;</span>
00299 <span class="comment"> state.StepIndex = 0;</span>
00300 <span class="comment"> uint count =0;</span>
00301 <span class="comment"> for (count=0; count+1024<sbh.NbSample[i]; count+=1024)</span>
00302 <span class="comment"> {</span>
00303 <span class="comment"> IBuffer::decodeADPCM(data+count/2, data16+count, 1024, state);</span>
00304 <span class="comment"> }</span>
00305 <span class="comment"> IBuffer::decodeADPCM(data+count/2, data16+count, sbh.NbSample[i]-count, state);</span>
00306 <span class="comment"></span>
00307 <span class="comment"> state.PreviousSample = 0;</span>
00308 <span class="comment"> state.StepIndex = 0;</span>
00309 <span class="comment"> sint16 *data16_2 = new sint16[sbh.NbSample[i]];</span>
00310 <span class="comment"> IBuffer::decodeADPCM(data, data16_2, sbh.NbSample[i], state);</span>
00311 <span class="comment"></span>
00312 <span class="comment"> for (uint j=0; j<sbh.NbSample[i]; ++j)</span>
00313 <span class="comment"> {</span>
00314 <span class="comment"> if (data16[j] != data16_2[j])</span>
00315 <span class="comment"> {</span>
00316 <span class="comment"> nlwarning("Sample differ at %u", j);</span>
00317 <span class="comment"> }</span>
00318 <span class="comment"> }</span>
00319 <span class="comment"></span>
00320 <span class="comment"> _SoundDriver->readRawBuffer(ibuffer, sbh.Name[i], (uint8*)data16, sbh.NbSample[i]*2, Mono16, sbh.Freq[i]);</span>
00321 <span class="comment"> delete [] data16;</span>
00322 <span class="comment"> delete [] data16_2;</span>
00323 <span class="comment"> }</span>
00324 <span class="comment"> */</span>
00325
00326 <span class="keywordflow">if</span> (CAudioMixerUser::instance()->useAPDCM())
00327 {
00328 <a class="code" href="a04223.html#a637">data</a> = (<a class="code" href="a04558.html#a7">uint8</a>*) realloc(<a class="code" href="a04223.html#a637">data</a>, sbh.<a class="code" href="a02240.html#NLSOUND_1_1CAudioMixerUser_1_1TSampleBankHeadero5">SizeAdpcm</a>[i]);
00329 sampleBank.<a class="code" href="a02653.html#NLMISC_1_1CIFilea13">seek</a>(seekStart + sbh.<a class="code" href="a02240.html#NLSOUND_1_1CAudioMixerUser_1_1TSampleBankHeadero3">OffsetAdpcm</a>[i], CIFile::begin);
00330 sampleBank.<a class="code" href="a02653.html#NLMISC_1_1CIFilea15">serialBuffer</a>(<a class="code" href="a04223.html#a637">data</a>, sbh.<a class="code" href="a02240.html#NLSOUND_1_1CAudioMixerUser_1_1TSampleBankHeadero5">SizeAdpcm</a>[i]);
00331 _SoundDriver-><a class="code" href="a03419.html#NLSOUND_1_1ISoundDrivera7">readRawBuffer</a>(ibuffer, sbh.<a class="code" href="a02240.html#NLSOUND_1_1CAudioMixerUser_1_1TSampleBankHeadero1">Name</a>[i], <a class="code" href="a04223.html#a637">data</a>, sbh.<a class="code" href="a02240.html#NLSOUND_1_1CAudioMixerUser_1_1TSampleBankHeadero5">SizeAdpcm</a>[i], <a class="code" href="a05381.html#a80a18">Mono16ADPCM</a>, sbh.<a class="code" href="a02240.html#NLSOUND_1_1CAudioMixerUser_1_1TSampleBankHeadero0">Freq</a>[i]);
00332 }
00333 <span class="keywordflow">else</span>
00334 {
00335 <a class="code" href="a04223.html#a637">data</a> = (<a class="code" href="a04558.html#a7">uint8</a>*) realloc(<a class="code" href="a04223.html#a637">data</a>, sbh.<a class="code" href="a02240.html#NLSOUND_1_1CAudioMixerUser_1_1TSampleBankHeadero6">SizeMono16</a>[i]);
00336 sampleBank.<a class="code" href="a02653.html#NLMISC_1_1CIFilea13">seek</a>(seekStart + sbh.<a class="code" href="a02240.html#NLSOUND_1_1CAudioMixerUser_1_1TSampleBankHeadero4">OffsetMono16</a>[i], CIFile::begin);
00337 sampleBank.<a class="code" href="a02653.html#NLMISC_1_1CIFilea15">serialBuffer</a>(<a class="code" href="a04223.html#a637">data</a>, sbh.<a class="code" href="a02240.html#NLSOUND_1_1CAudioMixerUser_1_1TSampleBankHeadero6">SizeMono16</a>[i]);
00338 _SoundDriver-><a class="code" href="a03419.html#NLSOUND_1_1ISoundDrivera7">readRawBuffer</a>(ibuffer, sbh.<a class="code" href="a02240.html#NLSOUND_1_1CAudioMixerUser_1_1TSampleBankHeadero1">Name</a>[i], <a class="code" href="a04223.html#a637">data</a>, sbh.<a class="code" href="a02240.html#NLSOUND_1_1CAudioMixerUser_1_1TSampleBankHeadero6">SizeMono16</a>[i], <a class="code" href="a05381.html#a80a19">Mono16</a>, sbh.<a class="code" href="a02240.html#NLSOUND_1_1CAudioMixerUser_1_1TSampleBankHeadero0">Freq</a>[i]);
00339 }
00340
00341 <a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr0">_ByteSize</a> += ibuffer-><a class="code" href="a02298.html#NLSOUND_1_1IBuffera5">getSize</a>();
00342
00343 <a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr5">_Samples</a>[nameId] = ibuffer;
00344
00345 <span class="comment">// Warn the sound bank that the sample are available.</span>
00346 CSoundBank::instance()->bufferLoaded(nameId, ibuffer);
00347 }
00348 free(<a class="code" href="a04223.html#a637">data</a>);
00349
00350 <a class="code" href="a03346.html#NLSOUND_1_1CSampleBankv1">_LoadedSize</a> += <a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr0">_ByteSize</a>;
00351 }
00352 <span class="keywordflow">catch</span>(<a class="code" href="a02482.html">Exception</a> &e)
00353 {
00354 <span class="comment">// loading failed !</span>
00355 <a class="code" href="a04199.html#a2">nlwarning</a>(<span class="stringliteral">"Exception %s during loading of sample bank %s"</span>, e.what(), filename.c_str());
00356 <a class="code" href="a02235.html">CAudioMixerUser</a> *mixer = CAudioMixerUser::instance();
00357
00358 <span class="keywordflow">if</span> (mixer-><a class="code" href="a02235.html#NLSOUND_1_1CAudioMixerUsera31">getPackedSheetUpdate</a>())
00359 {
00360 <a class="code" href="a04199.html#a1">nlinfo</a>(<span class="stringliteral">"Deleting offending sound bank, you need to restart to recreate it!"</span>);
00361 CFile::deleteFile(filename);
00362 }
00363 }
00364
00365 _Loaded = <span class="keyword">true</span>;
00366 <a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr2">_LoadingDone</a> = <span class="keyword">true</span>;
00367
00368
00369
00371 <span class="comment">/*</span>
00372 <span class="comment"></span>
00373 <span class="comment"> std::string list = CPath::lookup(CStringMapper::unmap(_Name)+CAudioMixerUser::SampleBankListExt, false);</span>
00374 <span class="comment"> if (list.empty())</span>
00375 <span class="comment"> {</span>
00376 <span class="comment"> nlwarning("File %s not found to load sample bank %s", (CStringMapper::unmap(_Name)+CAudioMixerUser::SampleBankListExt).c_str(), CStringMapper::unmap(_Name).c_str());</span>
00377 <span class="comment"> return;</span>
00378 <span class="comment"> }</span>
00379 <span class="comment"></span>
00380 <span class="comment"></span>
00381 <span class="comment"> NLMISC::CIFile sampleBankList(list);</span>
00382 <span class="comment"> sampleBankList.serialCont(filenames);</span>
00383 <span class="comment"></span>
00384 <span class="comment"> for (iter = filenames.begin(); iter != filenames.end(); iter++)</span>
00385 <span class="comment"> {</span>
00386 <span class="comment"> IBuffer* ibuffer = NULL;</span>
00387 <span class="comment"> try</span>
00388 <span class="comment"> {</span>
00389 <span class="comment"> ibuffer = _SoundDriver->createBuffer();</span>
00390 <span class="comment"> nlassert(ibuffer);</span>
00391 <span class="comment"></span>
00392 <span class="comment">// std::string sampleName(CFile::getFilenameWithoutExtension(*iter));</span>
00393 <span class="comment"> NLMISC::TStringId sampleName(CStringMapper::map(CFile::getFilenameWithoutExtension(*iter)));</span>
00394 <span class="comment"></span>
00395 <span class="comment"> if (async)</span>
00396 <span class="comment"> {</span>
00397 <span class="comment"> ibuffer->presetName(sampleName);</span>
00398 <span class="comment"> nldebug("Preloading sample [%s]", CStringMapper::unmap(sampleName).c_str());</span>
00399 <span class="comment"> }</span>
00400 <span class="comment"> else</span>
00401 <span class="comment"> {</span>
00402 <span class="comment"> std::string fullName = NLMISC::CPath::lookup(*iter, false);</span>
00403 <span class="comment"> if (!fullName.empty())</span>
00404 <span class="comment"> {</span>
00405 <span class="comment"> NLMISC::CIFile ifile(fullName);</span>
00406 <span class="comment"> uint size = ifile.getFileSize();</span>
00407 <span class="comment"> uint8 *buffer = new uint8[ifile.getFileSize()];</span>
00408 <span class="comment"> ifile.serialBuffer(buffer, size);</span>
00409 <span class="comment"></span>
00410 <span class="comment"> _SoundDriver->readWavBuffer(ibuffer, fullName, buffer, size);</span>
00411 <span class="comment"> _ByteSize += ibuffer->getSize();</span>
00412 <span class="comment"></span>
00413 <span class="comment"> delete [] buffer;</span>
00414 <span class="comment"> }</span>
00415 <span class="comment"> }</span>
00416 <span class="comment"> _Samples[sampleName] = ibuffer ;</span>
00417 <span class="comment"></span>
00418 <span class="comment"> // Warn the sound bank that the sample are available.</span>
00419 <span class="comment"> CSoundBank::instance()->bufferLoaded(sampleName, ibuffer);</span>
00420 <span class="comment"> }</span>
00421 <span class="comment"> catch (ESoundDriver &e)</span>
00422 <span class="comment"> {</span>
00423 <span class="comment"> if (ibuffer != NULL) {</span>
00424 <span class="comment"> delete ibuffer;</span>
00425 <span class="comment"> ibuffer = NULL;</span>
00426 <span class="comment"> }</span>
00427 <span class="comment"> nlwarning("Problem with file '%s': %s", (*iter).c_str(), e.what());</span>
00428 <span class="comment"> }</span>
00429 <span class="comment"> }</span>
00430 <span class="comment"></span>
00431 <span class="comment"> _Loaded = true;</span>
00432 <span class="comment"></span>
00433 <span class="comment"> if (!async)</span>
00434 <span class="comment"> {</span>
00435 <span class="comment"> _LoadingDone = true;</span>
00436 <span class="comment"> // compute the sample bank size.</span>
00437 <span class="comment"> _LoadedSize += _ByteSize;</span>
00438 <span class="comment"> }</span>
00439 <span class="comment"> else</span>
00440 <span class="comment"> {</span>
00441 <span class="comment"> // fill the loading list.</span>
00442 <span class="comment"> TSampleTable::iterator first(_Samples.begin()), last(_Samples.end());</span>
00443 <span class="comment"> for (; first != last; ++first)</span>
00444 <span class="comment"> {</span>
00445 <span class="comment"> _LoadList.push_back(make_pair(first->second, first->first));</span>
00446 <span class="comment"> }</span>
00447 <span class="comment"> _SplitLoadDone = false;</span>
00448 <span class="comment"> // send the first files</span>
00449 <span class="comment"> for (uint i=0; i<ASYNC_LOADING_SPLIT && !_LoadList.empty(); ++i)</span>
00450 <span class="comment"> {</span>
00451 <span class="comment"> CAsyncFileManagerSound::getInstance().loadWavFile(_LoadList.front().first, CStringMapper::unmap(_LoadList.front().second)+".wav");</span>
00452 <span class="comment"> _LoadList.pop_front();</span>
00453 <span class="comment"> }</span>
00454 <span class="comment"> // add a end loading event...</span>
00455 <span class="comment"> CAsyncFileManagerSound::getInstance().signal(&_SplitLoadDone);</span>
00456 <span class="comment"> // and register for update on the mixer</span>
00457 <span class="comment"> CAudioMixerUser::instance()->registerUpdate(this);</span>
00458 <span class="comment"> }</span>
00459 <span class="comment"> */</span>
00460 }
00461
<a name="l00462"></a><a class="code" href="a03346.html#NLSOUND_1_1CSampleBankd0">00462</a> <span class="keywordtype">void</span> CSampleBank::onUpdate()
00463 {
00464 <span class="keywordflow">if</span> (<a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr7">_SplitLoadDone</a>)
00465 {
00466 <a class="code" href="a04199.html#a0">nldebug</a>(<span class="stringliteral">"Some samples have been loaded"</span>);
00467 <span class="keywordflow">if</span> (<a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr3">_LoadList</a>.empty())
00468 {
00469 <span class="comment">// all the samples are loaded, we can compute the bank size.</span>
00470 TSampleTable::iterator first(<a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr5">_Samples</a>.begin()), last(<a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr5">_Samples</a>.end());
00471 <span class="keywordflow">for</span> (; first != last; ++first)
00472 {
00473 <a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr0">_ByteSize</a> += first->second->getSize();
00474 }
00475
00476 <a class="code" href="a03346.html#NLSOUND_1_1CSampleBankv1">_LoadedSize</a> += <a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr0">_ByteSize</a>;
00477
00478 <span class="comment">// stop the update.</span>
00479 CAudioMixerUser::instance()->unregisterUpdate(<span class="keyword">this</span>);
00480 <a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr2">_LoadingDone</a> = <span class="keyword">true</span>;
00481
00482 <span class="comment">// Force an update in the background manager (can restar stoped sound).</span>
00483 CAudioMixerUser::instance()->getBackgroundSoundManager()->updateBackgroundStatus();
00484
00485 <a class="code" href="a04199.html#a1">nlinfo</a>(<span class="stringliteral">"Sample bank %s loaded."</span>, CStringMapper::unmap(_Name).c_str());
00486 }
00487 <span class="keywordflow">else</span>
00488 {
00489 <a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr7">_SplitLoadDone</a> = <span class="keyword">false</span>;
00490 <span class="keywordflow">for</span> (<a class="code" href="a04558.html#a15">uint</a> i=0; i<<a class="code" href="a05381.html#a49">ASYNC_LOADING_SPLIT</a> && !<a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr3">_LoadList</a>.empty(); ++i)
00491 {
00492 CAsyncFileManagerSound::getInstance().loadWavFile(<a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr3">_LoadList</a>.front().first, CStringMapper::unmap(<a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr3">_LoadList</a>.front().second)+<span class="stringliteral">".wav"</span>);
00493 <a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr3">_LoadList</a>.pop_front();
00494 }
00495 <span class="comment">// add a end loading event...</span>
00496 CAsyncFileManagerSound::getInstance().signal(&<a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr7">_SplitLoadDone</a>);
00497 }
00498 }
00499 }
00500
00501 <span class="comment">// ********************************************************</span>
00502
<a name="l00503"></a><a class="code" href="a03346.html#NLSOUND_1_1CSampleBanka7">00503</a> <span class="keywordtype">bool</span> CSampleBank::unload()
00504 {
00505 vector<IBuffer*> vec;
00506 TSampleTable::iterator it;
00507
00508 <span class="keywordflow">if</span> (!_Loaded)
00509 {
00510 <a class="code" href="a04199.html#a2">nlwarning</a>(<span class="stringliteral">"Trying to unload an already unloaded bank : %s"</span>, CStringMapper::unmap(_Name).c_str ());
00511 <span class="keywordflow">return</span> <span class="keyword">true</span>;
00512 }
00513
00514 <span class="comment">// need to wait end of load ?</span>
00515 <span class="keywordflow">if</span> (!<a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr2">_LoadingDone</a>)
00516 <span class="keywordflow">return</span> <span class="keyword">false</span>;
00517
00518 <a class="code" href="a04199.html#a1">nlinfo</a>(<span class="stringliteral">"Unloading sample bank %s"</span>, CStringMapper::unmap(_Name).c_str());
00519
00520 <span class="keywordflow">for</span> (it = <a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr5">_Samples</a>.begin(); it != <a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr5">_Samples</a>.end(); ++it)
00521 {
00522 <a class="code" href="a02298.html">IBuffer</a> *<a class="code" href="a04223.html#a589">buffer</a> = it->second;
00523 <span class="keywordflow">if</span> (<a class="code" href="a04223.html#a589">buffer</a>)
00524 {
00525 <span class="keyword">const</span> NLMISC::TStringId & bufferName = <a class="code" href="a04223.html#a589">buffer</a>->getName();
00526
00527 <span class="comment">// Warn the mixer to stop any track playing this buffer.</span>
00528 CAudioMixerUser::instance()->bufferUnloaded(<a class="code" href="a04223.html#a589">buffer</a>);
00529 <span class="comment">// Warn the sound banks abount this buffer.</span>
00530 CSoundBank::instance()->bufferUnloaded(bufferName);
00531
00532 <span class="comment">// delete</span>
00533 it->second = NULL;
00534 <span class="keyword">delete</span> <a class="code" href="a04223.html#a589">buffer</a>;
00535 }
00536 }
00537
00538 _Loaded = <span class="keyword">false</span>;
00539
00540 <a class="code" href="a03346.html#NLSOUND_1_1CSampleBankv1">_LoadedSize</a> -= <a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr0">_ByteSize</a>;
00541 <a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr0">_ByteSize</a> = 0;
00542
00543 <span class="keywordflow">return</span> <span class="keyword">true</span>;
00544 }
00545
00546 <span class="comment">// ********************************************************</span>
00547
<a name="l00548"></a><a class="code" href="a03346.html#NLSOUND_1_1CSampleBanka5">00548</a> <span class="keywordtype">bool</span> CSampleBank::isLoaded()
00549 {
00550 <span class="keywordflow">return</span> _Loaded;
00551 }
00552
00553 <span class="comment">// ********************************************************</span>
00554
<a name="l00555"></a><a class="code" href="a03346.html#NLSOUND_1_1CSampleBanka3">00555</a> <a class="code" href="a02298.html">IBuffer</a>* CSampleBank::getSample(<span class="keyword">const</span> NLMISC::TStringId &name)
00556 {
00557 {
00558 <span class="comment">/* // dump the sample list.</span>
00559 <span class="comment"> TSampleTable::iterator it (_Samples.begin()), last(_Samples.end());</span>
00560 <span class="comment"> std::string s;</span>
00561 <span class="comment"></span>
00562 <span class="comment">// while (first != last)</span>
00563 <span class="comment"> for (it = _Samples.begin(); it != _Samples.end(); ++it)</span>
00564 <span class="comment"> {</span>
00565 <span class="comment"> s += std::string(" [")+it->first+"] ";</span>
00566 <span class="comment"> //first++;</span>
00567 <span class="comment"> }</span>
00568 <span class="comment"></span>
00569 <span class="comment"> nldebug("getSample(%s) : sample list = [%s]", name, s.c_str());</span>
00570 <span class="comment">*/</span>
00571 }
00572
00573 <span class="comment">// Find sound</span>
00574 TSampleTable::iterator iter = <a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr5">_Samples</a>.find(name);
00575 <span class="keywordflow">if</span> ( iter == <a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr5">_Samples</a>.end() )
00576 {
00577 <span class="keywordflow">return</span> 0;
00578 }
00579 <span class="keywordflow">else</span>
00580 {
00581 <span class="keywordflow">return</span> (*iter).second;
00582 }
00583 }
00584
00585 <span class="comment">// ********************************************************</span>
00586
<a name="l00587"></a><a class="code" href="a03346.html#NLSOUND_1_1CSampleBanka0">00587</a> <a class="code" href="a04558.html#a15">uint</a> CSampleBank::countSamples()
00588 {
00589 <span class="keywordflow">return</span> <a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr5">_Samples</a>.size();
00590 }
00591
00592 <span class="comment">// ********************************************************</span>
00593
<a name="l00594"></a><a class="code" href="a03346.html#NLSOUND_1_1CSampleBanka4">00594</a> <a class="code" href="a04558.html#a15">uint</a> CSampleBank::getSize()
00595 {
00596 <a class="code" href="a04558.html#a15">uint</a> <a class="code" href="a04223.html#a587">size</a> = 0;
00597
00598 TSampleTable::const_iterator iter;
00599 <span class="keywordflow">for</span> (iter = <a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr5">_Samples</a>.begin(); iter != <a class="code" href="a03346.html#NLSOUND_1_1CSampleBankr5">_Samples</a>.end(); iter++)
00600 {
00601 <a class="code" href="a04223.html#a587">size</a> += (*iter).second->getSize();
00602 }
00603
00604 <span class="keywordflow">return</span> <a class="code" href="a04223.html#a587">size</a>;
00605 }
00606
<a name="l00607"></a><a class="code" href="a03346.html#NLSOUND_1_1CSampleBanke5">00607</a> <span class="keywordtype">void</span> CSampleBank::releaseAll()
00608 {
00609 <a class="code" href="a04199.html#a0">nldebug</a>( <span class="stringliteral">"SampleBanks: Releasing..."</span> );
00610
00611 <span class="keywordflow">while</span> (!_Banks.empty())
00612 {
00613 <span class="keyword">delete</span> _Banks.begin()->second;
00614 }
00615 <a class="code" href="a04199.html#a0">nldebug</a>( <span class="stringliteral">"SampleBanks: Released"</span> );
00616 }
00617
00618
00619
00620 } <span class="comment">// namespace NLSOUND</span>
00621
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Tue Mar 16 06:35:55 2004 for NeL by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0 >
</a>1.3.6 </small></address>
</body>
</html>
|