1 |
schoenebeck |
411 |
/*************************************************************************** |
2 |
|
|
* * |
3 |
|
|
* LinuxSampler - modular, streaming capable sampler * |
4 |
|
|
* * |
5 |
iliev |
2012 |
* Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck * |
6 |
schoenebeck |
3733 |
* Copyright (C) 2005 - 2020 Christian Schoenebeck * |
7 |
|
|
* Copyright (C) 2009 - 2012 Grigor Iliev * |
8 |
|
|
* Copyright (C) 2012 - 2016 Andreas Persson * |
9 |
schoenebeck |
411 |
* * |
10 |
|
|
* This program is free software; you can redistribute it and/or modify * |
11 |
|
|
* it under the terms of the GNU General Public License as published by * |
12 |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
13 |
|
|
* (at your option) any later version. * |
14 |
|
|
* * |
15 |
|
|
* This program is distributed in the hope that it will be useful, * |
16 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
17 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
18 |
|
|
* GNU General Public License for more details. * |
19 |
|
|
* * |
20 |
|
|
* You should have received a copy of the GNU General Public License * |
21 |
|
|
* along with this program; if not, write to the Free Software * |
22 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
23 |
|
|
* MA 02111-1307 USA * |
24 |
|
|
***************************************************************************/ |
25 |
|
|
|
26 |
|
|
#include "EngineChannel.h" |
27 |
iliev |
2012 |
#include "Engine.h" |
28 |
schoenebeck |
411 |
|
29 |
persson |
438 |
namespace LinuxSampler { namespace gig { |
30 |
iliev |
2012 |
EngineChannel::EngineChannel() { |
31 |
schoenebeck |
2902 |
CurrentGigScript = NULL; |
32 |
schoenebeck |
411 |
} |
33 |
|
|
|
34 |
|
|
EngineChannel::~EngineChannel() { |
35 |
schoenebeck |
460 |
DisconnectAudioOutputDevice(); |
36 |
iliev |
1826 |
// In case the channel was removed before the instrument was |
37 |
|
|
// fully loaded, try to give back instrument again (see bug #113) |
38 |
iliev |
2012 |
InstrumentChangeCmd< ::gig::DimensionRegion, ::gig::Instrument>& cmd = ChangeInstrument(NULL); |
39 |
iliev |
1826 |
if (cmd.pInstrument) { |
40 |
persson |
2127 |
Engine::instruments.HandBack(cmd.pInstrument, this); |
41 |
iliev |
1826 |
} |
42 |
|
|
/////// |
43 |
schoenebeck |
411 |
} |
44 |
|
|
|
45 |
iliev |
2012 |
AbstractEngine::Format EngineChannel::GetEngineFormat() { return AbstractEngine::GIG; } |
46 |
schoenebeck |
660 |
|
47 |
iliev |
2012 |
/** This method is not thread safe! */ |
48 |
schoenebeck |
2871 |
void EngineChannel::ResetInternal(bool bResetEngine) { |
49 |
schoenebeck |
411 |
CurrentKeyDimension = 0; |
50 |
schoenebeck |
2871 |
EngineChannelBase<Voice, ::gig::DimensionRegion, ::gig::Instrument>::ResetInternal(bResetEngine); |
51 |
schoenebeck |
411 |
} |
52 |
|
|
|
53 |
|
|
/** |
54 |
iliev |
2012 |
* Will be called by the MIDIIn Thread to signal that a program |
55 |
|
|
* change should be performed. As a program change isn't |
56 |
|
|
* real-time safe, the actual change is performed by the disk |
57 |
|
|
* thread. |
58 |
schoenebeck |
411 |
* |
59 |
iliev |
2012 |
* @param Program - MIDI program change number |
60 |
schoenebeck |
411 |
*/ |
61 |
iliev |
2012 |
void EngineChannel::SendProgramChange(uint8_t Program) { |
62 |
persson |
2277 |
SetMidiProgram(Program); |
63 |
iliev |
2012 |
Engine* engine = dynamic_cast<Engine*>(pEngine); |
64 |
|
|
if(engine == NULL) return; |
65 |
|
|
|
66 |
|
|
if(engine->GetDiskThread()) { |
67 |
persson |
2277 |
uint32_t merged = (GetMidiBankMsb() << 16) | (GetMidiBankLsb() << 8) | Program; |
68 |
|
|
engine->GetDiskThread()->OrderProgramChange(merged, this); |
69 |
iliev |
2012 |
} else { |
70 |
|
|
// TODO: |
71 |
|
|
} |
72 |
schoenebeck |
411 |
} |
73 |
|
|
|
74 |
|
|
/** |
75 |
|
|
* Load an instrument from a .gig file. PrepareLoadInstrument() has to |
76 |
|
|
* be called first to provide the information which instrument to load. |
77 |
|
|
* This method will then actually start to load the instrument and block |
78 |
|
|
* the calling thread until loading was completed. |
79 |
|
|
* |
80 |
|
|
* @see PrepareLoadInstrument() |
81 |
|
|
*/ |
82 |
|
|
void EngineChannel::LoadInstrument() { |
83 |
iliev |
2012 |
InstrumentResourceManager* pInstrumentManager = dynamic_cast<InstrumentResourceManager*>(pEngine->GetInstrumentManager()); |
84 |
|
|
|
85 |
persson |
1646 |
// make sure we don't trigger any new notes with an old |
86 |
|
|
// instrument |
87 |
schoenebeck |
2902 |
InstrumentChangeCmd< ::gig::DimensionRegion, ::gig::Instrument>& cmd = ChangeInstrument(NULL); |
88 |
persson |
1646 |
if (cmd.pInstrument) { |
89 |
|
|
// give old instrument back to instrument manager, but |
90 |
|
|
// keep the dimension regions and samples that are in use |
91 |
iliev |
2012 |
pInstrumentManager->HandBackInstrument(cmd.pInstrument, this, cmd.pRegionsInUse); |
92 |
schoenebeck |
411 |
} |
93 |
schoenebeck |
2612 |
if (cmd.pScript) { |
94 |
|
|
// give old instrument script back to instrument resource manager |
95 |
|
|
cmd.pScript->resetAll(); |
96 |
schoenebeck |
2902 |
CurrentGigScript = NULL; |
97 |
schoenebeck |
2612 |
} |
98 |
iliev |
2012 |
cmd.pRegionsInUse->clear(); |
99 |
schoenebeck |
411 |
|
100 |
|
|
// delete all key groups |
101 |
persson |
2114 |
DeleteGroupEventLists(); |
102 |
schoenebeck |
411 |
|
103 |
|
|
// request gig instrument from instrument manager |
104 |
persson |
1038 |
::gig::Instrument* newInstrument; |
105 |
schoenebeck |
411 |
try { |
106 |
schoenebeck |
947 |
InstrumentManager::instrument_id_t instrid; |
107 |
|
|
instrid.FileName = InstrumentFile; |
108 |
|
|
instrid.Index = InstrumentIdx; |
109 |
iliev |
2012 |
|
110 |
|
|
newInstrument = pInstrumentManager->Borrow(instrid, this); |
111 |
persson |
1038 |
if (!newInstrument) { |
112 |
schoenebeck |
1212 |
throw InstrumentManagerException("resource was not created"); |
113 |
schoenebeck |
411 |
} |
114 |
schoenebeck |
2594 |
|
115 |
schoenebeck |
2611 |
if (newInstrument->ScriptSlotCount() > 1) { |
116 |
|
|
std::cerr << "WARNING: Executing more than one real-time instrument script slot is not implemented yet!\n"; |
117 |
|
|
} |
118 |
schoenebeck |
2594 |
::gig::Script* script = newInstrument->GetScriptOfSlot(0); |
119 |
|
|
if (script) { |
120 |
|
|
String sourceCode = script->GetScriptAsText(); |
121 |
schoenebeck |
3733 |
std::map<String,String> patchVars = newInstrument->GetScriptPatchVariables(0); |
122 |
|
|
LoadInstrumentScript(sourceCode, patchVars); |
123 |
schoenebeck |
2594 |
} |
124 |
schoenebeck |
2902 |
CurrentGigScript = script; |
125 |
schoenebeck |
411 |
} |
126 |
|
|
catch (RIFF::Exception e) { |
127 |
|
|
InstrumentStat = -2; |
128 |
iliev |
1309 |
StatusChanged(true); |
129 |
schoenebeck |
411 |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message; |
130 |
schoenebeck |
880 |
throw Exception(msg); |
131 |
schoenebeck |
411 |
} |
132 |
schoenebeck |
1212 |
catch (InstrumentManagerException e) { |
133 |
schoenebeck |
411 |
InstrumentStat = -3; |
134 |
iliev |
1309 |
StatusChanged(true); |
135 |
schoenebeck |
411 |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message(); |
136 |
schoenebeck |
880 |
throw Exception(msg); |
137 |
schoenebeck |
411 |
} |
138 |
|
|
catch (...) { |
139 |
|
|
InstrumentStat = -4; |
140 |
iliev |
1309 |
StatusChanged(true); |
141 |
schoenebeck |
880 |
throw Exception("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file."); |
142 |
schoenebeck |
411 |
} |
143 |
|
|
|
144 |
persson |
2043 |
RoundRobinIndex = 0; |
145 |
|
|
for (int i = 0 ; i < 128 ; i++) pMIDIKeyInfo[i].pRoundRobinIndex = NULL; |
146 |
|
|
|
147 |
|
|
// rebuild ActiveKeyGroups map with key groups of current |
148 |
|
|
// instrument and set the round robin pointers to use one |
149 |
|
|
// counter for each region |
150 |
|
|
int region = 0; |
151 |
|
|
for (::gig::Region* pRegion = newInstrument->GetFirstRegion(); pRegion; pRegion = newInstrument->GetNextRegion()) { |
152 |
persson |
2114 |
AddGroup(pRegion->KeyGroup); |
153 |
schoenebeck |
411 |
|
154 |
persson |
2043 |
RoundRobinIndexes[region] = 0; |
155 |
|
|
for (int iKey = pRegion->KeyRange.low; iKey <= pRegion->KeyRange.high; iKey++) { |
156 |
|
|
pMIDIKeyInfo[iKey].pRoundRobinIndex = &RoundRobinIndexes[region]; |
157 |
|
|
} |
158 |
|
|
region++; |
159 |
|
|
} |
160 |
|
|
|
161 |
persson |
1038 |
InstrumentIdxName = newInstrument->pInfo->Name; |
162 |
schoenebeck |
411 |
InstrumentStat = 100; |
163 |
|
|
|
164 |
schoenebeck |
2614 |
{ |
165 |
|
|
InstrumentChangeCmd< ::gig::DimensionRegion, ::gig::Instrument>& cmd = |
166 |
|
|
ChangeInstrument(newInstrument); |
167 |
|
|
if (cmd.pScript) { |
168 |
|
|
// give old instrument script back to instrument resource manager |
169 |
|
|
cmd.pScript->resetAll(); |
170 |
|
|
} |
171 |
schoenebeck |
2612 |
} |
172 |
persson |
1646 |
|
173 |
iliev |
1298 |
StatusChanged(true); |
174 |
schoenebeck |
411 |
} |
175 |
|
|
|
176 |
schoenebeck |
2902 |
/** |
177 |
|
|
* Called by instrument editor API to inform this engine channel that the |
178 |
|
|
* passed @a script has been modified by the instrument editor and that |
179 |
|
|
* this engine channel should thus reload the instrument script (that is |
180 |
|
|
* that it should re-parse the scripts new source code). |
181 |
|
|
*/ |
182 |
|
|
void EngineChannel::reloadScript(::gig::Script* script) { |
183 |
|
|
dmsg(3,("gig::EngineChannel::realoadScript()\n")); |
184 |
|
|
// make sure this engine channel is actually using the requested |
185 |
|
|
// modified gig::Script object (because the internal script resource |
186 |
|
|
// manager will i.e. provide the same VM object for different |
187 |
|
|
// ::gig::Script objects with same source code though. |
188 |
|
|
if (!script || CurrentGigScript != script) return; |
189 |
|
|
|
190 |
|
|
//TODO: reloading the entire instrument is currently a very lazy (and slow) solution just for reloading the instrument script |
191 |
|
|
try { |
192 |
|
|
LoadInstrument(); |
193 |
|
|
} catch (Exception e) { |
194 |
|
|
std::cerr << e.Message() << std::endl; |
195 |
|
|
} catch (...) { |
196 |
|
|
String msg = "gig::Engine error: Failed to reload instrument script, cause: Unknown exception while trying to reload gig file."; |
197 |
|
|
std::cerr << msg << std::endl; |
198 |
|
|
} |
199 |
|
|
} |
200 |
|
|
|
201 |
iliev |
2012 |
void EngineChannel::ProcessKeySwitchChange(int key) { |
202 |
|
|
// Change key dimension value if key is in keyswitching area |
203 |
persson |
1646 |
{ |
204 |
iliev |
2012 |
if (key >= pInstrument->DimensionKeyRange.low && key <= pInstrument->DimensionKeyRange.high) |
205 |
|
|
CurrentKeyDimension = float(key - pInstrument->DimensionKeyRange.low) / |
206 |
|
|
(pInstrument->DimensionKeyRange.high - pInstrument->DimensionKeyRange.low + 1); |
207 |
persson |
1646 |
} |
208 |
schoenebeck |
411 |
} |
209 |
schoenebeck |
2330 |
|
210 |
|
|
String EngineChannel::InstrumentFileName() { |
211 |
|
|
return EngineChannelBase<Voice, ::gig::DimensionRegion, ::gig::Instrument>::InstrumentFileName(); |
212 |
|
|
} |
213 |
|
|
|
214 |
|
|
String EngineChannel::InstrumentFileName(int index) { |
215 |
|
|
if (index == 0) return InstrumentFileName(); |
216 |
|
|
if (!pInstrument || !pInstrument->GetParent()) return ""; |
217 |
|
|
DLS::File* pMainFile = dynamic_cast<DLS::File*>(pInstrument->GetParent()); |
218 |
|
|
if (!pMainFile) return ""; |
219 |
|
|
RIFF::File* pExtensionFile = pMainFile->GetExtensionFile(index); |
220 |
|
|
return (pExtensionFile) ? pExtensionFile->GetFileName() : ""; |
221 |
|
|
} |
222 |
schoenebeck |
411 |
|
223 |
|
|
}} // namespace LinuxSampler::gig |