1 |
/*************************************************************************** |
2 |
* * |
3 |
* Copyright (C) 2005, 2006 Christian Schoenebeck * |
4 |
* * |
5 |
* This program is free software; you can redistribute it and/or modify * |
6 |
* it under the terms of the GNU General Public License as published by * |
7 |
* the Free Software Foundation; either version 2 of the License, or * |
8 |
* (at your option) any later version. * |
9 |
* * |
10 |
* This program is distributed in the hope that it will be useful, * |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 |
* GNU General Public License for more details. * |
14 |
* * |
15 |
* You should have received a copy of the GNU General Public License * |
16 |
* along with this program; if not, write to the Free Software * |
17 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
18 |
* MA 02111-1307 USA * |
19 |
***************************************************************************/ |
20 |
|
21 |
#include "EngineChannelFactory.h" |
22 |
|
23 |
#include <strings.h> |
24 |
|
25 |
#include "gig/EngineChannel.h" |
26 |
|
27 |
#if HAVE_SF2 |
28 |
#include "sf2/EngineChannel.h" |
29 |
#endif |
30 |
|
31 |
#include "sfz/EngineChannel.h" |
32 |
|
33 |
namespace LinuxSampler { |
34 |
class LockedChannel { |
35 |
public: |
36 |
const EngineChannel* pChannel; |
37 |
bool bDestroyed; |
38 |
|
39 |
LockedChannel(const EngineChannel* pChannel) { |
40 |
this->pChannel = pChannel; |
41 |
bDestroyed = false; |
42 |
} |
43 |
}; |
44 |
|
45 |
class LockedChannelList { |
46 |
public: |
47 |
void Add(const EngineChannel* pChannel) { |
48 |
vChannelList.push_back(LockedChannel(pChannel)); |
49 |
} |
50 |
|
51 |
bool IsDestroyed(const EngineChannel* pChannel) { |
52 |
LockedChannel* pLockedChannel = get(pChannel); |
53 |
if (pLockedChannel == NULL) return false; |
54 |
return pLockedChannel->bDestroyed; |
55 |
} |
56 |
|
57 |
void SetDestroyed(const EngineChannel* pChannel, bool bDestroyed = true) { |
58 |
LockedChannel* pLockedChannel = get(pChannel); |
59 |
if (pLockedChannel == NULL) return; |
60 |
pLockedChannel->bDestroyed = bDestroyed; |
61 |
} |
62 |
|
63 |
void Remove(const EngineChannel* pChannel) { |
64 |
std::vector<LockedChannel>::iterator it = vChannelList.begin(); |
65 |
for (; it != vChannelList.end(); it++) { |
66 |
if ((*it).pChannel == pChannel) { |
67 |
vChannelList.erase(it); |
68 |
return; |
69 |
} |
70 |
} |
71 |
} |
72 |
|
73 |
bool Contains(const EngineChannel* pChannel) { |
74 |
return get(pChannel) != NULL; |
75 |
} |
76 |
|
77 |
private: |
78 |
std::vector<LockedChannel> vChannelList; |
79 |
|
80 |
LockedChannel* get(const EngineChannel* pChannel) { |
81 |
for (int i = 0; i < vChannelList.size(); i++) { |
82 |
if (vChannelList[i].pChannel == pChannel) { |
83 |
return &vChannelList[i]; |
84 |
} |
85 |
} |
86 |
|
87 |
return NULL; |
88 |
} |
89 |
} lockedChannels; |
90 |
|
91 |
Mutex EngineChannelFactory::LockedChannelsMutex; |
92 |
|
93 |
void EngineChannelFactory::SetDeleteEnabled(const EngineChannel* pEngineChannel, bool enable) { |
94 |
LockedChannelsMutex.Lock(); |
95 |
if (!enable) { |
96 |
if (!lockedChannels.Contains(pEngineChannel)) lockedChannels.Add(pEngineChannel); |
97 |
LockedChannelsMutex.Unlock(); |
98 |
} else { |
99 |
bool b = lockedChannels.IsDestroyed(pEngineChannel); |
100 |
lockedChannels.Remove(pEngineChannel); |
101 |
LockedChannelsMutex.Unlock(); |
102 |
|
103 |
if (b) delete pEngineChannel; |
104 |
} |
105 |
} |
106 |
|
107 |
// all currently existing engine channel instances |
108 |
static std::set<LinuxSampler::EngineChannel*> engineChannels; |
109 |
|
110 |
LinuxSampler::EngineChannel* EngineChannelFactory::Create(String EngineType) throw (Exception) { |
111 |
if (!strcasecmp(EngineType.c_str(),"GigEngine") || !strcasecmp(EngineType.c_str(),"gig")) { |
112 |
LinuxSampler::EngineChannel* pEngineChannel = new gig::EngineChannel; |
113 |
engineChannels.insert(pEngineChannel); |
114 |
return pEngineChannel; |
115 |
} else if (!strcasecmp(EngineType.c_str(),"sf2")) { |
116 |
#if HAVE_SF2 |
117 |
LinuxSampler::EngineChannel* pEngineChannel = new sf2::EngineChannel; |
118 |
engineChannels.insert(pEngineChannel); |
119 |
return pEngineChannel; |
120 |
#else |
121 |
throw Exception("LinuxSampler is not compiled with SF2 support"); |
122 |
#endif |
123 |
} else if (!strcasecmp(EngineType.c_str(),"sfz")) { |
124 |
LinuxSampler::EngineChannel* pEngineChannel = new sfz::EngineChannel; |
125 |
engineChannels.insert(pEngineChannel); |
126 |
return pEngineChannel; |
127 |
} |
128 |
throw Exception("Unknown engine type"); |
129 |
} |
130 |
|
131 |
void EngineChannelFactory::Destroy(LinuxSampler::EngineChannel* pEngineChannel) { |
132 |
pEngineChannel->RemoveAllFxSendCountListeners(); |
133 |
engineChannels.erase(pEngineChannel); |
134 |
|
135 |
// Postpone the deletion of the specified EngineChannel if needed (bug #113) |
136 |
LockedChannelsMutex.Lock(); |
137 |
if (lockedChannels.Contains(pEngineChannel)) { |
138 |
lockedChannels.SetDestroyed(pEngineChannel); |
139 |
pEngineChannel->SetSamplerChannel(NULL); |
140 |
LockedChannelsMutex.Unlock(); |
141 |
return; |
142 |
} |
143 |
LockedChannelsMutex.Unlock(); |
144 |
/////// |
145 |
|
146 |
delete pEngineChannel; |
147 |
} |
148 |
|
149 |
const std::set<LinuxSampler::EngineChannel*>& EngineChannelFactory::EngineChannelInstances() { |
150 |
return engineChannels; |
151 |
} |
152 |
|
153 |
} // namespace LinuxSampler |