1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005-2009 Christian Schoenebeck * |
7 |
* Copyright (C) 2009-2011 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 sf2 { |
29 |
EngineChannel::EngineChannel() { |
30 |
for(int i = 0; i < 128; i++) PressedKeys[i] = false; |
31 |
LastKey = LastKeySwitch = -1; |
32 |
} |
33 |
|
34 |
EngineChannel::~EngineChannel() { |
35 |
DisconnectAudioOutputDevice(); |
36 |
// In case the channel was removed before the instrument was |
37 |
// fully loaded, try to give back instrument again (see bug #113) |
38 |
InstrumentChangeCmd< ::sf2::Region, ::sf2::Preset>& cmd = ChangeInstrument(NULL); |
39 |
if (cmd.pInstrument) { |
40 |
Engine::instruments.HandBack(cmd.pInstrument, this); |
41 |
} |
42 |
/////// |
43 |
} |
44 |
|
45 |
AbstractEngine::Format EngineChannel::GetEngineFormat() { return AbstractEngine::SF2; } |
46 |
|
47 |
/** This method is not thread safe! */ |
48 |
void EngineChannel::ResetInternal() { |
49 |
CurrentKeyDimension = 0; |
50 |
EngineChannelBase<Voice, ::sf2::Region, ::sf2::Preset>::ResetInternal(); |
51 |
for(int i = 0; i < 128; i++) PressedKeys[i] = false; |
52 |
} |
53 |
|
54 |
/** |
55 |
* Will be called by the MIDIIn Thread to signal that a program |
56 |
* change should be performed. As a program change isn't |
57 |
* real-time safe, the actual change is performed by the disk |
58 |
* thread. |
59 |
* |
60 |
* @param Program - MIDI program change number |
61 |
*/ |
62 |
void EngineChannel::SendProgramChange(uint8_t Program) { |
63 |
SetMidiProgram(Program); |
64 |
Engine* engine = dynamic_cast<Engine*>(pEngine); |
65 |
if(engine == NULL) return; |
66 |
|
67 |
if(engine->GetDiskThread()) { |
68 |
uint32_t merged = (GetMidiBankMsb() << 16) | (GetMidiBankLsb() << 8) | Program; |
69 |
engine->GetDiskThread()->OrderProgramChange(merged, this); |
70 |
} else { |
71 |
// TODO: |
72 |
} |
73 |
} |
74 |
|
75 |
/** |
76 |
* Load an instrument from a .sf2 file. PrepareLoadInstrument() has to |
77 |
* be called first to provide the information which instrument to load. |
78 |
* This method will then actually start to load the instrument and block |
79 |
* the calling thread until loading was completed. |
80 |
* |
81 |
* @see PrepareLoadInstrument() |
82 |
*/ |
83 |
void EngineChannel::LoadInstrument() { |
84 |
InstrumentResourceManager* pInstrumentManager = dynamic_cast<InstrumentResourceManager*>(pEngine->GetInstrumentManager()); |
85 |
|
86 |
// make sure we don't trigger any new notes with an old |
87 |
// instrument |
88 |
InstrumentChangeCmd< ::sf2::Region, ::sf2::Preset>& cmd = ChangeInstrument(0); |
89 |
if (cmd.pInstrument) { |
90 |
// give old instrument back to instrument manager, but |
91 |
// keep the dimension regions and samples that are in use |
92 |
pInstrumentManager->HandBackInstrument(cmd.pInstrument, this, cmd.pRegionsInUse); |
93 |
} |
94 |
cmd.pRegionsInUse->clear(); |
95 |
|
96 |
// delete all key groups |
97 |
DeleteGroupEventLists(); |
98 |
|
99 |
// request sf2 instrument from instrument manager |
100 |
::sf2::Preset* newInstrument; |
101 |
try { |
102 |
InstrumentManager::instrument_id_t instrid; |
103 |
instrid.FileName = InstrumentFile; |
104 |
instrid.Index = InstrumentIdx; |
105 |
|
106 |
newInstrument = pInstrumentManager->Borrow(instrid, this); |
107 |
if (!newInstrument) { |
108 |
throw InstrumentManagerException("resource was not created"); |
109 |
} |
110 |
} |
111 |
catch (InstrumentManagerException e) { |
112 |
InstrumentStat = -3; |
113 |
StatusChanged(true); |
114 |
String msg = "sf2::Engine error: Failed to load instrument, cause: " + e.Message(); |
115 |
throw Exception(msg); |
116 |
} |
117 |
catch (::sf2::Exception e) { |
118 |
InstrumentStat = -3; |
119 |
StatusChanged(true); |
120 |
String msg = "sf2::Engine error: Failed to load instrument, cause: " + e.Message; |
121 |
throw Exception(msg); |
122 |
} |
123 |
catch (::std::runtime_error e) { |
124 |
InstrumentStat = -3; |
125 |
StatusChanged(true); |
126 |
String msg = "sf2::Engine error: Failed to load instrument, cause: "; |
127 |
msg += e.what(); |
128 |
throw Exception(msg); |
129 |
} |
130 |
catch (...) { |
131 |
InstrumentStat = -4; |
132 |
StatusChanged(true); |
133 |
throw Exception("sf2::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse sf2 file."); |
134 |
} |
135 |
|
136 |
// rebuild ActiveKeyGroups map with key groups of current instrument |
137 |
for (int i = 0 ; i < newInstrument->GetRegionCount() ; i++) { |
138 |
::sf2::Region* pRegion = newInstrument->GetRegion(i); |
139 |
for (int j = 0 ; j < pRegion->pInstrument->GetRegionCount() ; j++) { |
140 |
::sf2::Region* pSubRegion = pRegion->pInstrument->GetRegion(j); |
141 |
AddGroup(pSubRegion->exclusiveClass); |
142 |
} |
143 |
} |
144 |
|
145 |
InstrumentIdxName = newInstrument->GetName(); |
146 |
InstrumentStat = 100; |
147 |
|
148 |
ChangeInstrument(newInstrument); |
149 |
|
150 |
StatusChanged(true); |
151 |
} |
152 |
|
153 |
void EngineChannel::ProcessKeySwitchChange(int key) { } |
154 |
|
155 |
}} // namespace LinuxSampler::sf2 |