/[svn]/linuxsampler/trunk/src/engines/common/SampleManager.h
ViewVC logotype

Annotation of /linuxsampler/trunk/src/engines/common/SampleManager.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2012 - (hide annotations) (download) (as text)
Fri Oct 23 17:53:17 2009 UTC (14 years, 6 months ago) by iliev
File MIME type: text/x-c++hdr
File size: 7363 byte(s)
* Refactoring: moved the independent code from
  the Gigasampler format engine to base classes
* SFZ format engine: experimental code (not usable yet)
* SoundFont format engine: experimental code (not usable yet)
* Fixed crash which may occur when MIDI key + transpose is out of range

1 iliev 2012 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5     * Copyright (C) 2009 Grigor Iliev *
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_SAMPLEMANAGER_H__
24     #define __LS_SAMPLEMANAGER_H__
25    
26     #include <map>
27     #include <vector>
28     #include <set>
29    
30     #include "../../common/Exception.h"
31    
32     namespace LinuxSampler {
33    
34     /**
35     * Used to determine and manage the relations between samples and consumers (e.g. regions)
36     */
37     template <class S /* Sample */, class C /* Sample Consumer */>
38     class SampleManager {
39     public:
40     /**
41     * Adds the specified sample to the sample manager
42     */
43     void AddSample(S* pSample) {
44     if (pSample == NULL) return;
45     sampleMap[pSample];
46     }
47    
48     void RemoveSample(S* pSample) throw (Exception) {
49     if (sampleMap.find(pSample) == sampleMap.end()) return;
50     if (!sampleMap[pSample].empty()) {
51     throw Exception("Can't remove. Sample has consumers");
52     }
53    
54     sampleMap.erase(sampleMap.find(pSample));
55     }
56    
57     /**
58     * Adds pConsumer as consumer to the specified sample.
59     * The sample is automatically added to the sample manager if
60     * it is not added yet. This method does nothing if pConsumer is
61     * already added as consumer to pSample.
62     */
63     void AddSampleConsumer(S* pSample, C* pConsumer) {
64     if (pSample == NULL || pConsumer == NULL) return;
65     if(sampleMap[pSample].find(pConsumer) != sampleMap[pSample].end()) return;
66     sampleMap[pSample].insert(pConsumer);
67     }
68    
69     std::vector<C*> GetConsumers(S* pSample) throw (Exception) {
70     if (sampleMap.find(pSample) == sampleMap.end()) {
71     throw Exception("SampleManager::GetConsumers: unknown sample");
72     }
73     std::set<C*>* pConsumers = &sampleMap[pSample];
74     std::vector<C*> v;
75     v.insert(pConsumers->begin(), pConsumers->end());
76     return v ;
77     }
78    
79     /**
80     * @return true if the specified consumer was in the list
81     * of consumers for the specified sample.
82     */
83     bool RemoveSampleConsumer(S* pSample, C* pConsumer) throw (Exception) {
84     if (sampleMap.find(pSample) == sampleMap.end()) {
85     throw Exception("SampleManager::RemoveConsumer: unknown sample");
86     }
87    
88     std::set<C*>* consumers = &sampleMap[pSample];
89     typename std::set<C*>::iterator it = consumers->find(pConsumer);
90     if (it != consumers->end()) {
91     consumers->erase(it);
92     return true;
93     }
94     return false;
95     }
96    
97     /**
98     * Determines whether pSample is managed by this sample manager
99     */
100     bool HasSample(S* pSample) {
101     return sampleMap.find(pSample) != sampleMap.end();
102     }
103    
104     bool HasSampleConsumers(S* pSample) throw (Exception) {
105     if (sampleMap.find(pSample) == sampleMap.end()) {
106     throw Exception("SampleManager::HasConsumers: unknown sample");
107     }
108    
109     return !sampleMap[pSample].empty();
110     }
111    
112     /**
113     * Determines whether pConsumer is consumer of pSample.
114     */
115     bool IsSampleConsumerOf(S* pSample, C* pConsumer) {
116     if (sampleMap.find(pSample) == sampleMap.end()) {
117     throw Exception("SampleManager::IsSampleConsumerOf: unknown sample");
118     }
119    
120     typename std::set<C*>::iterator it = sampleMap[pSample].find(pConsumer);
121     return it != sampleMap[pSample].end();
122     }
123    
124     /**
125     * Sets that pSample is now in use by pConsumer.
126     */
127     void SetSampleInUse(S* pSample, C* pConsumer) {
128     verifyPair(pSample, pConsumer, "SampleManager::SetSampleInUse");
129    
130     bool inUse = !samplesInUseMap[pSample].empty();
131     samplesInUseMap[pSample].insert(pConsumer);
132     if(!inUse) OnSampleInUse(pSample);
133     }
134    
135     /**
136     * Sets that pSample is now not in use by pConsumer.
137     */
138     void SetSampleNotInUse(S* pSample, C* pConsumer) {
139     verifyPair(pSample, pConsumer, "SampleManager::SetSampleNotInUse");
140    
141     bool inUse = !samplesInUseMap[pSample].empty();
142     samplesInUseMap[pSample].erase(pConsumer);
143     bool inUseNew = !samplesInUseMap[pSample].empty();
144     if(inUse && !inUseNew) OnSampleNotInUse(pSample);
145     }
146    
147     protected:
148     std::map<S*, std::set<C*> > sampleMap;
149     std::map<S*, std::set<C*> > samplesInUseMap;
150    
151     void verifyPair(S* pSample, C* pConsumer, String caller) {
152     if(!HasSample(pSample)) {
153     throw Exception(caller + ": unknown sample");
154     }
155    
156     if(!IsSampleConsumerOf(pSample, pConsumer)) {
157     throw Exception("SampleManager::SetSampleInUse: unknown consumer");
158     }
159     }
160    
161     /**
162     * Override this method to handle the state change (not in use -> in use)
163     * of the specified sample.
164     */
165     virtual void OnSampleInUse(S* pSample) { }
166    
167     /**
168     * Override this method to handle the state change (in use -> not in use)
169     * of the specified sample.
170     */
171     virtual void OnSampleNotInUse(S* pSample) { }
172     };
173     } // namespace LinuxSampler
174    
175     #endif /* __LS_SAMPLEMANAGER_H__ */
176    

  ViewVC Help
Powered by ViewVC