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

Contents of /linuxsampler/trunk/src/engines/gig/EngineChannel.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2612 - (show annotations) (download)
Tue Jun 10 13:32:16 2014 UTC (9 years, 10 months ago) by schoenebeck
File size: 8418 byte(s)
* Fixed crashes when exiting the sampler.
* Bumped version (1.0.0.svn47).

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

  ViewVC Help
Powered by ViewVC