/[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 2871 - (show annotations) (download)
Sun Apr 10 18:22:23 2016 UTC (8 years ago) by schoenebeck
File size: 7297 byte(s)
* All engines: Implemented scheduler for delayed MIDI events and for
  suspended real-time instrument scripts.
* Real-Time instrument scripts: Implemented support for built-in "wait()"
  function's "duration-us" argument, thus scripts using this function are
  now correctly resumed after the requested amount of microseconds.
* Real-Time instrument scripts: Implemented support for built-in
  "play_note()" function's "duration-us" argument, thus notes triggered
  with this argument are now correctly released after the requested amount
  of microseconds.
* Real-Time instrument scripts: Fixed crash which happened when trying to
  reference an undeclared script variable.
* Real-Time instrument scripts: Script events were not cleared when
  engine channel was reset, potentially causing undefined behavior.
* All engines: Attempt to partly fix resetting engine channels vs.
  resetting engine, an overall cleanup of the Reset*(),
  ConnectAudioDevice(), DisconnectAudioDevice() API methods would still be
  desirable though, because the current situation is still inconsistent
  and error prone.
* Bumped version (2.0.0.svn2).

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

  ViewVC Help
Powered by ViewVC