/[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 2101 - (hide annotations) (download)
Sun May 30 11:40:31 2010 UTC (13 years, 11 months ago) by persson
File size: 7181 byte(s)
* sfz/sf2 engines: RT-safeness: avoid malloc in audio thread
* sfz/sf2 engines: fixed a bug that could cause voice stealing to fail

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 persson 2101 * Copyright (C) 2009-2010 Grigor Iliev *
8 iliev 2012 * *
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     InstrumentResourceManager* instrs = dynamic_cast<InstrumentResourceManager*>(pEngine->GetInstrumentManager());
43     instrs->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() {
52     CurrentKeyDimension = 0;
53     EngineChannelBase<Voice, ::sfz::Region, ::sfz::Instrument>::ResetInternal();
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     Engine* engine = dynamic_cast<Engine*>(pEngine);
67     if(engine == NULL) return;
68    
69     if(engine->GetDiskThread()) {
70     engine->GetDiskThread()->OrderProgramChange(Program, this);
71     } else {
72     // TODO:
73     }
74     }
75    
76     /**
77     * Load an instrument from a .sfz file. PrepareLoadInstrument() has to
78     * be called first to provide the information which instrument to load.
79     * This method will then actually start to load the instrument and block
80     * the calling thread until loading was completed.
81     *
82     * @see PrepareLoadInstrument()
83     */
84     void EngineChannel::LoadInstrument() {
85     InstrumentResourceManager* pInstrumentManager = dynamic_cast<InstrumentResourceManager*>(pEngine->GetInstrumentManager());
86    
87     // make sure we don't trigger any new notes with an old
88     // instrument
89     InstrumentChangeCmd< ::sfz::Region, ::sfz::Instrument>& cmd = ChangeInstrument(0);
90     if (cmd.pInstrument) {
91     // give old instrument back to instrument manager, but
92     // keep the dimension regions and samples that are in use
93     pInstrumentManager->HandBackInstrument(cmd.pInstrument, this, cmd.pRegionsInUse);
94     }
95     cmd.pRegionsInUse->clear();
96    
97     // delete all key groups
98     ActiveKeyGroups.clear();
99    
100     // request sfz instrument from instrument manager
101     ::sfz::Instrument* newInstrument;
102     try {
103     InstrumentManager::instrument_id_t instrid;
104     instrid.FileName = InstrumentFile;
105     instrid.Index = InstrumentIdx;
106    
107     newInstrument = pInstrumentManager->Borrow(instrid, this);
108     if (!newInstrument) {
109     throw InstrumentManagerException("resource was not created");
110     }
111     }
112     catch (InstrumentManagerException e) {
113     InstrumentStat = -3;
114     StatusChanged(true);
115     String msg = "sfz::Engine error: Failed to load instrument, cause: " + e.Message();
116     throw Exception(msg);
117     }
118     catch (::sfz::Exception e) {
119     InstrumentStat = -3;
120     StatusChanged(true);
121     String msg = "sfz::Engine error: Failed to load instrument, cause: " + e.Message();
122     throw Exception(msg);
123     }
124     catch (::std::runtime_error e) {
125     InstrumentStat = -3;
126     StatusChanged(true);
127     String msg = "sfz::Engine error: Failed to load instrument, cause: ";
128     msg += e.what();
129     throw Exception(msg);
130     }
131     catch (...) {
132     InstrumentStat = -4;
133     StatusChanged(true);
134     throw Exception("sfz::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse sfz file.");
135     }
136    
137     // rebuild ActiveKeyGroups map with key groups of current instrument
138 persson 2101 for (std::vector< ::sfz::Region*>::iterator itRegion = newInstrument->regions.begin() ;
139     itRegion != newInstrument->regions.end() ; ++itRegion) {
140     if ((*itRegion)->group) ActiveKeyGroups[(*itRegion)->group] = NULL;
141     }
142 iliev 2012
143     InstrumentIdxName = newInstrument->GetName();
144     InstrumentStat = 100;
145    
146     ChangeInstrument(newInstrument);
147    
148     StatusChanged(true);
149     }
150    
151     void EngineChannel::ProcessKeySwitchChange(int key) { }
152    
153     void EngineChannel::PreProcessNoteOn(uint8_t key, uint8_t velocity) {
154     if(pInstrument != NULL && pInstrument->HasKeySwitchBinding(key)) LastKeySwitch = key;
155     PressedKeys[key] = true;
156     }
157    
158 persson 2058 void EngineChannel::PostProcessNoteOn(uint8_t key, uint8_t velocity) {
159     LastKey = key;
160     }
161    
162 iliev 2012 void EngineChannel::PreProcessNoteOff(uint8_t key, uint8_t velocity) {
163     PressedKeys[key] = false;
164     }
165    
166     }} // namespace LinuxSampler::sfz

  ViewVC Help
Powered by ViewVC