1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* * |
7 |
* This program is free software; you can redistribute it and/or modify * |
8 |
* it under the terms of the GNU General Public License as published by * |
9 |
* the Free Software Foundation; either version 2 of the License, or * |
10 |
* (at your option) any later version. * |
11 |
* * |
12 |
* This program is distributed in the hope that it will be useful, * |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
15 |
* GNU General Public License for more details. * |
16 |
* * |
17 |
* You should have received a copy of the GNU General Public License * |
18 |
* along with this program; if not, write to the Free Software * |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
20 |
* MA 02111-1307 USA * |
21 |
***************************************************************************/ |
22 |
|
23 |
#ifndef __LS_GIG_INSTRUMENTRESOURCEMANAGER_H__ |
24 |
#define __LS_GIG_INSTRUMENTRESOURCEMANAGER_H__ |
25 |
|
26 |
#include "../../common/global.h" |
27 |
|
28 |
#if DEBUG_HEADERS |
29 |
# warning InstrumentResourceManager.h included |
30 |
#endif // DEBUG_HEADERS |
31 |
|
32 |
#include "../../common/global.h" |
33 |
#include "../../common/LinuxSamplerException.h" |
34 |
#include "../../common/ResourceManager.h" |
35 |
#include "../../drivers/audio/AudioOutputDevice.h" |
36 |
#include "../../lib/fileloader/libgig/gig.h" |
37 |
|
38 |
// preload 64k samples = 128kB of data in RAM for 16 bit mono samples |
39 |
#define NUM_RAM_PRELOAD_SAMPLES 32768 |
40 |
|
41 |
//namespace libgig = gig; |
42 |
|
43 |
namespace LinuxSampler { namespace gig { |
44 |
|
45 |
typedef ResourceConsumer< ::gig::Instrument> InstrumentConsumer; |
46 |
|
47 |
}} // namespace LinuxSampler::gig |
48 |
|
49 |
#include "Engine.h" |
50 |
|
51 |
namespace LinuxSampler { namespace gig { |
52 |
|
53 |
// just symbol prototyping |
54 |
class Engine; |
55 |
|
56 |
/** |
57 |
* Explicitly identifies a Gigasampler instrument. |
58 |
*/ |
59 |
struct instrument_id_t { |
60 |
String FileName; ///< name of the file which contains the instrument |
61 |
uint iInstrument; ///< index of the instrument in the instrument file |
62 |
|
63 |
// TODO: we should extend operator<() so it will be able to detect that file x and file y are actually the same files, e.g. because one of them is a symlink / share the same inode |
64 |
bool operator<(const instrument_id_t& o) const { |
65 |
return (iInstrument < o.iInstrument || (iInstrument == o.iInstrument && FileName < o.FileName)); |
66 |
} |
67 |
}; |
68 |
|
69 |
/** Gig instrument manager |
70 |
* |
71 |
* Manager to share gig instruments between multiple Gigasampler |
72 |
* engines. The engines Borrow() instruments when they need them and |
73 |
* HandBack() when they don't need them anymore. The |
74 |
* InstrumentResourceManager loads the corresponding gig file and gig |
75 |
* instrument if needed, if it's already in use by another engine, then |
76 |
* it just returns the same resource, if an gig instrument / file is not |
77 |
* in use by any engine anymore, then it will be freed from memory. |
78 |
*/ |
79 |
class InstrumentResourceManager : public ResourceManager<instrument_id_t, ::gig::Instrument> { |
80 |
protected: |
81 |
virtual ::gig::Instrument* Create(instrument_id_t Key, InstrumentConsumer* pConsumer, void*& pArg); |
82 |
virtual void Destroy(::gig::Instrument* pResource, void* pArg); |
83 |
virtual void OnBorrow(::gig::Instrument* pResource, InstrumentConsumer* pConsumer, void*& pArg); |
84 |
private: |
85 |
struct instr_entry_t { |
86 |
::gig::File* pGig; |
87 |
uint iInstrument; |
88 |
uint MaxSamplesPerCycle; ///< if some engine requests an already allocated instrument with a higher value, we have to reallocate the instrument |
89 |
}; |
90 |
|
91 |
typedef ResourceConsumer< ::gig::File> GigConsumer; |
92 |
|
93 |
class GigResourceManager : public ResourceManager<String, ::gig::File> { |
94 |
protected: |
95 |
virtual ::gig::File* Create(String Key, GigConsumer* pConsumer, void*& pArg); |
96 |
virtual void Destroy(::gig::File* pResource, void* pArg); |
97 |
virtual void OnBorrow(::gig::File* pResource, GigConsumer* pConsumer, void*& pArg) {} // ignore |
98 |
} Gigs; |
99 |
|
100 |
void CacheInitialSamples(::gig::Sample* pSample, gig::Engine* pEngine); |
101 |
}; |
102 |
|
103 |
/** |
104 |
* Will be thrown by the InstrumentResourceManager on errors. |
105 |
*/ |
106 |
class InstrumentResourceManagerException : public LinuxSamplerException { |
107 |
public: |
108 |
InstrumentResourceManagerException(String msg) : LinuxSamplerException(msg) {} |
109 |
}; |
110 |
|
111 |
}} // namespace LinuxSampler::gig |
112 |
|
113 |
#endif // __LS_GIG_INSTRUMENTRESOURCEMANAGER_H__ |