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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2277 - (show annotations) (download)
Sat Oct 1 08:23:02 2011 UTC (12 years, 6 months ago) by persson
File size: 7191 byte(s)
* fixed handling of rapid bank select and program change messages sent
  to the same sampler channel (patch from the Open Octave project,
  slightly adjusted)

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 sfz {
29 EngineChannel::EngineChannel() {
30 for(int i = 0; i < 128; i++) PressedKeys[i] = false;
31 LastKey = LastKeySwitch = -1;
32 AddMidiKeyboardListener(this);
33 }
34
35 EngineChannel::~EngineChannel() {
36 DisconnectAudioOutputDevice();
37 RemoveMidiKeyboardListener(this);
38 // In case the channel was removed before the instrument was
39 // fully loaded, try to give back instrument again (see bug #113)
40 InstrumentChangeCmd< ::sfz::Region, ::sfz::Instrument>& cmd = ChangeInstrument(NULL);
41 if (cmd.pInstrument) {
42 Engine::instruments.HandBack(cmd.pInstrument, this);
43 }
44 ///////
45 }
46
47 AbstractEngine::Format EngineChannel::GetEngineFormat() { return AbstractEngine::SFZ; }
48
49 /** This method is not thread safe! */
50 void EngineChannel::ResetInternal() {
51 CurrentKeyDimension = 0;
52 EngineChannelBase<Voice, ::sfz::Region, ::sfz::Instrument>::ResetInternal();
53 for(int i = 0; i < 128; i++) PressedKeys[i] = false;
54 }
55
56 /**
57 * Will be called by the MIDIIn Thread to signal that a program
58 * change should be performed. As a program change isn't
59 * real-time safe, the actual change is performed by the disk
60 * thread.
61 *
62 * @param Program - MIDI program change number
63 */
64 void EngineChannel::SendProgramChange(uint8_t Program) {
65 SetMidiProgram(Program);
66 Engine* engine = dynamic_cast<Engine*>(pEngine);
67 if(engine == NULL) return;
68
69 if(engine->GetDiskThread()) {
70 uint32_t merged = (GetMidiBankMsb() << 16) | (GetMidiBankLsb() << 8) | Program;
71 engine->GetDiskThread()->OrderProgramChange(merged, this);
72 } else {
73 // TODO:
74 }
75 }
76
77 /**
78 * Load an instrument from a .sfz file. PrepareLoadInstrument() has to
79 * be called first to provide the information which instrument to load.
80 * This method will then actually start to load the instrument and block
81 * the calling thread until loading was completed.
82 *
83 * @see PrepareLoadInstrument()
84 */
85 void EngineChannel::LoadInstrument() {
86 InstrumentResourceManager* pInstrumentManager = dynamic_cast<InstrumentResourceManager*>(pEngine->GetInstrumentManager());
87
88 // make sure we don't trigger any new notes with an old
89 // instrument
90 InstrumentChangeCmd< ::sfz::Region, ::sfz::Instrument>& cmd = ChangeInstrument(0);
91 if (cmd.pInstrument) {
92 // give old instrument back to instrument manager, but
93 // keep the dimension regions and samples that are in use
94 pInstrumentManager->HandBackInstrument(cmd.pInstrument, this, cmd.pRegionsInUse);
95 }
96 cmd.pRegionsInUse->clear();
97
98 // delete all key groups
99 DeleteGroupEventLists();
100
101 // request sfz instrument from instrument manager
102 ::sfz::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 catch (InstrumentManagerException e) {
114 InstrumentStat = -3;
115 StatusChanged(true);
116 String msg = "sfz::Engine error: Failed to load instrument, cause: " + e.Message();
117 throw Exception(msg);
118 }
119 catch (::sfz::Exception e) {
120 InstrumentStat = -3;
121 StatusChanged(true);
122 String msg = "sfz::Engine error: Failed to load instrument, cause: " + e.Message();
123 throw Exception(msg);
124 }
125 catch (::std::runtime_error e) {
126 InstrumentStat = -3;
127 StatusChanged(true);
128 String msg = "sfz::Engine error: Failed to load instrument, cause: ";
129 msg += e.what();
130 throw Exception(msg);
131 }
132 catch (...) {
133 InstrumentStat = -4;
134 StatusChanged(true);
135 throw Exception("sfz::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse sfz file.");
136 }
137
138 // rebuild ActiveKeyGroups map with key groups of current instrument
139 for (std::vector< ::sfz::Region*>::iterator itRegion = newInstrument->regions.begin() ;
140 itRegion != newInstrument->regions.end() ; ++itRegion) {
141 AddGroup((*itRegion)->group);
142 AddGroup((*itRegion)->off_by);
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 void EngineChannel::PreProcessNoteOn(uint8_t key, uint8_t velocity) {
156 if(pInstrument != NULL && pInstrument->HasKeySwitchBinding(key)) LastKeySwitch = key;
157 PressedKeys[key] = true;
158 }
159
160 void EngineChannel::PostProcessNoteOn(uint8_t key, uint8_t velocity) {
161 LastKey = key;
162 }
163
164 void EngineChannel::PreProcessNoteOff(uint8_t key, uint8_t velocity) {
165 PressedKeys[key] = false;
166 }
167
168 }} // namespace LinuxSampler::sfz

  ViewVC Help
Powered by ViewVC