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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3082 - (hide annotations) (download)
Mon Jan 9 18:39:35 2017 UTC (7 years, 3 months ago) by schoenebeck
File size: 8169 byte(s)
* Added support for sfz extension opcode 'script' which may be used to
  load real-time instrument script file (NKSP script language).
* Removed code duplication in SFZ file loading code.
* Bumped version (2.0.0.svn37).

1 iliev 2012 /***************************************************************************
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 schoenebeck 2871 * Copyright (C) 2009 - 2012 Christian Schoenebeck and Grigor Iliev *
8     * Copyright (C) 2012 - 2016 Christian Schoenebeck and Andreas Persson *
9 iliev 2012 * *
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 persson 2127 Engine::instruments.HandBack(cmd.pInstrument, this);
44 iliev 2012 }
45     ///////
46     }
47    
48     AbstractEngine::Format EngineChannel::GetEngineFormat() { return AbstractEngine::SFZ; }
49    
50     /** This method is not thread safe! */
51 schoenebeck 2871 void EngineChannel::ResetInternal(bool bResetEngine) {
52 iliev 2012 CurrentKeyDimension = 0;
53 schoenebeck 2871 EngineChannelBase<Voice, ::sfz::Region, ::sfz::Instrument>::ResetInternal(bResetEngine);
54 iliev 2012 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 persson 2277 SetMidiProgram(Program);
67 iliev 2012 Engine* engine = dynamic_cast<Engine*>(pEngine);
68     if(engine == NULL) return;
69    
70     if(engine->GetDiskThread()) {
71 persson 2277 uint32_t merged = (GetMidiBankMsb() << 16) | (GetMidiBankLsb() << 8) | Program;
72     engine->GetDiskThread()->OrderProgramChange(merged, this);
73 iliev 2012 } 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 schoenebeck 3082 if (cmd.pScript) {
98     // give old instrument script back to instrument resource manager
99     cmd.pScript->resetAll();
100     }
101 iliev 2012 cmd.pRegionsInUse->clear();
102    
103     // delete all key groups
104 persson 2114 DeleteGroupEventLists();
105 iliev 2012
106     // request sfz instrument from instrument manager
107     ::sfz::Instrument* newInstrument;
108     try {
109     InstrumentManager::instrument_id_t instrid;
110     instrid.FileName = InstrumentFile;
111     instrid.Index = InstrumentIdx;
112    
113     newInstrument = pInstrumentManager->Borrow(instrid, this);
114     if (!newInstrument) {
115     throw InstrumentManagerException("resource was not created");
116     }
117 schoenebeck 3082
118     if (newInstrument->scripts.size() > 1) {
119     std::cerr << "WARNING: Executing more than one real-time instrument script slot is not implemented yet!\n";
120     }
121     ::sfz::Script* script = (!newInstrument->scripts.empty()) ? &newInstrument->scripts[0] : NULL;
122     if (script) {
123     String sourceCode = script->GetSourceCode();
124     LoadInstrumentScript(sourceCode);
125     }
126 iliev 2012 }
127     catch (InstrumentManagerException e) {
128     InstrumentStat = -3;
129     StatusChanged(true);
130     String msg = "sfz::Engine error: Failed to load instrument, cause: " + e.Message();
131     throw Exception(msg);
132     }
133     catch (::sfz::Exception e) {
134     InstrumentStat = -3;
135     StatusChanged(true);
136     String msg = "sfz::Engine error: Failed to load instrument, cause: " + e.Message();
137     throw Exception(msg);
138     }
139     catch (::std::runtime_error e) {
140     InstrumentStat = -3;
141     StatusChanged(true);
142     String msg = "sfz::Engine error: Failed to load instrument, cause: ";
143     msg += e.what();
144     throw Exception(msg);
145     }
146     catch (...) {
147     InstrumentStat = -4;
148     StatusChanged(true);
149     throw Exception("sfz::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse sfz file.");
150     }
151    
152     // rebuild ActiveKeyGroups map with key groups of current instrument
153 persson 2101 for (std::vector< ::sfz::Region*>::iterator itRegion = newInstrument->regions.begin() ;
154     itRegion != newInstrument->regions.end() ; ++itRegion) {
155 persson 2114 AddGroup((*itRegion)->group);
156     AddGroup((*itRegion)->off_by);
157 persson 2101 }
158 iliev 2012
159     InstrumentIdxName = newInstrument->GetName();
160     InstrumentStat = 100;
161    
162 schoenebeck 3082 {
163     InstrumentChangeCmd< ::sfz::Region, ::sfz::Instrument>& cmd =
164     ChangeInstrument(newInstrument);
165     if (cmd.pScript) {
166     // give old instrument script back to instrument resource manager
167     cmd.pScript->resetAll();
168     }
169     }
170 iliev 2012
171     StatusChanged(true);
172     }
173    
174     void EngineChannel::ProcessKeySwitchChange(int key) { }
175    
176     void EngineChannel::PreProcessNoteOn(uint8_t key, uint8_t velocity) {
177     if(pInstrument != NULL && pInstrument->HasKeySwitchBinding(key)) LastKeySwitch = key;
178     PressedKeys[key] = true;
179     }
180    
181 persson 2058 void EngineChannel::PostProcessNoteOn(uint8_t key, uint8_t velocity) {
182     LastKey = key;
183     }
184    
185 iliev 2012 void EngineChannel::PreProcessNoteOff(uint8_t key, uint8_t velocity) {
186     PressedKeys[key] = false;
187     }
188    
189     }} // namespace LinuxSampler::sfz

  ViewVC Help
Powered by ViewVC