146 |
} |
} |
147 |
|
|
148 |
InstrumentResourceManager::instrument_info_t InstrumentResourceManager::GetInstrumentInfo(instrument_id_t ID) throw (InstrumentManagerException) { |
InstrumentResourceManager::instrument_info_t InstrumentResourceManager::GetInstrumentInfo(instrument_id_t ID) throw (InstrumentManagerException) { |
149 |
std::vector<instrument_id_t> result; |
Lock(); |
150 |
|
::gig::Instrument* pInstrument = Resource(ID, false); |
151 |
|
bool loaded = (pInstrument != NULL); |
152 |
|
if (!loaded) Unlock(); |
153 |
|
|
154 |
::RIFF::File* riff = NULL; |
::RIFF::File* riff = NULL; |
155 |
::gig::File* gig = NULL; |
::gig::File* gig = NULL; |
156 |
try { |
try { |
157 |
riff = new ::RIFF::File(ID.FileName); |
if(!loaded) { |
158 |
gig = new ::gig::File(riff); |
riff = new ::RIFF::File(ID.FileName); |
159 |
gig->SetAutoLoad(false); // avoid time consuming samples scanning |
gig = new ::gig::File(riff); |
160 |
::gig::Instrument* pInstrument = gig->GetInstrument(ID.Index); |
gig->SetAutoLoad(false); // avoid time consuming samples scanning |
161 |
|
pInstrument = gig->GetInstrument(ID.Index); |
162 |
|
} |
163 |
|
|
164 |
if (!pInstrument) throw InstrumentManagerException("There is no instrument " + ToString(ID.Index) + " in " + ID.FileName); |
if (!pInstrument) throw InstrumentManagerException("There is no instrument " + ToString(ID.Index) + " in " + ID.FileName); |
165 |
|
|
166 |
instrument_info_t info; |
instrument_info_t info; |
167 |
if (gig->pVersion) { |
for (int i = 0; i < 128; i++) { info.KeyBindings[i] = info.KeySwitchBindings[i] = 0; } |
168 |
info.FormatVersion = ToString(gig->pVersion->major); |
|
169 |
info.Product = gig->pInfo->Product; |
::gig::File* pFile = (::gig::File*) pInstrument->GetParent(); |
170 |
info.Artists = gig->pInfo->Artists; |
|
171 |
|
if (pFile->pVersion) { |
172 |
|
info.FormatVersion = ToString(pFile->pVersion->major); |
173 |
|
info.Product = pFile->pInfo->Product; |
174 |
|
info.Artists = pFile->pInfo->Artists; |
175 |
} |
} |
176 |
|
|
177 |
info.InstrumentName = pInstrument->pInfo->Name; |
info.InstrumentName = pInstrument->pInfo->Name; |
178 |
|
|
179 |
|
::gig::Region* pRegion = pInstrument->GetFirstRegion(); |
180 |
|
while (pRegion) { |
181 |
|
int low = pRegion->KeyRange.low; |
182 |
|
int high = pRegion->KeyRange.high; |
183 |
|
if (low < 0 || low > 127 || high < 0 || high > 127 || low > high) { |
184 |
|
std::cerr << "Invalid key range: " << low << " - " << high << std::endl; |
185 |
|
} else { |
186 |
|
for (int i = low; i <= high; i++) info.KeyBindings[i] = 1; |
187 |
|
} |
188 |
|
|
189 |
|
pRegion = pInstrument->GetNextRegion(); |
190 |
|
} |
191 |
|
|
192 |
|
if (loaded) { // retrieve keyswitching only if the instrument is fully loaded. |
193 |
|
|
194 |
|
// only return keyswitch range if keyswitching is used |
195 |
|
bool hasKeyswitches = false; |
196 |
|
for (::gig::Region* pRegion = pInstrument->GetFirstRegion() ; |
197 |
|
pRegion && !hasKeyswitches ; |
198 |
|
pRegion = pInstrument->GetNextRegion()) { |
199 |
|
for (int i = 0 ; i < pRegion->Dimensions ; i++) { |
200 |
|
if (pRegion->pDimensionDefinitions[i].dimension == ::gig::dimension_keyboard) { |
201 |
|
hasKeyswitches = true; |
202 |
|
break; |
203 |
|
} |
204 |
|
} |
205 |
|
} |
206 |
|
|
207 |
|
if (hasKeyswitches) { |
208 |
|
int low = pInstrument->DimensionKeyRange.low; |
209 |
|
int high = pInstrument->DimensionKeyRange.high; |
210 |
|
if (low < 0 || low > 127 || high < 0 || high > 127 || low > high) { |
211 |
|
std::cerr << "Invalid keyswitch range: " << low << " - " << high << std::endl; |
212 |
|
} else { |
213 |
|
for (int i = low; i <= high; i++) info.KeySwitchBindings[i] = 1; |
214 |
|
} |
215 |
|
} |
216 |
|
} |
217 |
|
|
218 |
|
if (loaded) Unlock(); |
219 |
|
|
220 |
if (gig) delete gig; |
if (gig) delete gig; |
221 |
if (riff) delete riff; |
if (riff) delete riff; |
222 |
return info; |
return info; |
223 |
} catch (::RIFF::Exception e) { |
} catch (::RIFF::Exception e) { |
224 |
|
if (loaded) Unlock(); |
225 |
if (gig) delete gig; |
if (gig) delete gig; |
226 |
if (riff) delete riff; |
if (riff) delete riff; |
227 |
throw InstrumentManagerException(e.Message); |
throw InstrumentManagerException(e.Message); |
228 |
} catch (...) { |
} catch (...) { |
229 |
|
if (loaded) Unlock(); |
230 |
if (gig) delete gig; |
if (gig) delete gig; |
231 |
if (riff) delete riff; |
if (riff) delete riff; |
232 |
throw InstrumentManagerException("Unknown exception while trying to parse '" + ID.FileName + "'"); |
throw InstrumentManagerException("Unknown exception while trying to parse '" + ID.FileName + "'"); |