/[svn]/linuxsampler/trunk/src/engines/EngineChannelFactory.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/engines/EngineChannelFactory.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2324 - (show annotations) (download)
Sun Mar 4 09:01:32 2012 UTC (12 years, 1 month ago) by persson
File size: 6102 byte(s)
* plugin bugfix: instrument loading hang when the plugin was loaded a
  second time (this time it's for Linux and Mac, previous similar fix
  was for Windows)
* thread safety fixes for the instrument loading thread
* MME driver: removed compiler warning
* LV2: fixed invalid realtime statement in plugin metadata

1 /***************************************************************************
2 * *
3 * Copyright (C) 2005 - 2012 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 Mutex EngineChannelFactory::EngineChannelsMutex;
93
94 void EngineChannelFactory::SetDeleteEnabled(const EngineChannel* pEngineChannel, bool enable) {
95 LockedChannelsMutex.Lock();
96 if (!enable) {
97 if (!lockedChannels.Contains(pEngineChannel)) lockedChannels.Add(pEngineChannel);
98 LockedChannelsMutex.Unlock();
99 } else {
100 bool b = lockedChannels.IsDestroyed(pEngineChannel);
101 lockedChannels.Remove(pEngineChannel);
102 LockedChannelsMutex.Unlock();
103
104 if (b) delete pEngineChannel;
105 }
106 }
107
108 // all currently existing engine channel instances
109 static std::set<LinuxSampler::EngineChannel*> engineChannels;
110
111 LinuxSampler::EngineChannel* EngineChannelFactory::Create(String EngineType) throw (Exception) {
112 LinuxSampler::EngineChannel* pEngineChannel;
113 if (!strcasecmp(EngineType.c_str(),"GigEngine") || !strcasecmp(EngineType.c_str(),"gig")) {
114 pEngineChannel = new gig::EngineChannel;
115 } else if (!strcasecmp(EngineType.c_str(),"sf2")) {
116 #if HAVE_SF2
117 pEngineChannel = new sf2::EngineChannel;
118 #else
119 throw Exception("LinuxSampler is not compiled with SF2 support");
120 #endif
121 } else if (!strcasecmp(EngineType.c_str(),"sfz")) {
122 pEngineChannel = new sfz::EngineChannel;
123 } else {
124 throw Exception("Unknown engine type");
125 }
126 EngineChannelsMutex.Lock();
127 engineChannels.insert(pEngineChannel);
128 EngineChannelsMutex.Unlock();
129 return pEngineChannel;
130 }
131
132 void EngineChannelFactory::Destroy(LinuxSampler::EngineChannel* pEngineChannel) {
133 pEngineChannel->RemoveAllFxSendCountListeners();
134 EngineChannelsMutex.Lock();
135 engineChannels.erase(pEngineChannel);
136 EngineChannelsMutex.Unlock();
137
138 // Postpone the deletion of the specified EngineChannel if needed (bug #113)
139 LockedChannelsMutex.Lock();
140 if (lockedChannels.Contains(pEngineChannel)) {
141 lockedChannels.SetDestroyed(pEngineChannel);
142 pEngineChannel->SetSamplerChannel(NULL);
143 LockedChannelsMutex.Unlock();
144 return;
145 }
146 LockedChannelsMutex.Unlock();
147 ///////
148
149 delete pEngineChannel;
150 }
151
152 const std::set<LinuxSampler::EngineChannel*>& EngineChannelFactory::EngineChannelInstances() {
153 return engineChannels;
154 }
155
156 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC