/[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 2127 - (hide annotations) (download)
Wed Sep 22 18:59:16 2010 UTC (13 years, 8 months ago) by persson
File size: 7067 byte(s)
* fixed crash when deleting a sampler channel or changing engine type
  while an instrument load was in progress

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 persson 2127 Engine::instruments.HandBack(cmd.pInstrument, this);
43 iliev 2012 }
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     Engine* engine = dynamic_cast<Engine*>(pEngine);
66     if(engine == NULL) return;
67    
68     if(engine->GetDiskThread()) {
69     engine->GetDiskThread()->OrderProgramChange(Program, this);
70     } else {
71     // TODO:
72     }
73     }
74    
75     /**
76     * Load an instrument from a .sfz 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< ::sfz::Region, ::sfz::Instrument>& 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 persson 2114 DeleteGroupEventLists();
98 iliev 2012
99     // request sfz instrument from instrument manager
100     ::sfz::Instrument* 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 = "sfz::Engine error: Failed to load instrument, cause: " + e.Message();
115     throw Exception(msg);
116     }
117     catch (::sfz::Exception e) {
118     InstrumentStat = -3;
119     StatusChanged(true);
120     String msg = "sfz::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 = "sfz::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("sfz::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse sfz file.");
134     }
135    
136     // rebuild ActiveKeyGroups map with key groups of current instrument
137 persson 2101 for (std::vector< ::sfz::Region*>::iterator itRegion = newInstrument->regions.begin() ;
138     itRegion != newInstrument->regions.end() ; ++itRegion) {
139 persson 2114 AddGroup((*itRegion)->group);
140     AddGroup((*itRegion)->off_by);
141 persson 2101 }
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