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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2043 - (hide annotations) (download)
Sat Jan 9 09:37:01 2010 UTC (14 years, 3 months ago) by persson
File size: 7106 byte(s)
* gig engine: implemented the "round robin keyboard" dimension
* gig engine: fixed round robin and random dimensions for cases when
  number of dimension zones is not a power of two
* gig engine: made round robin use a counter for each region instead
  of each key
* fixed building with libgig installed in a non-standard directory

1 schoenebeck 411 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5 iliev 2012 * Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck *
6     * Copyright (C) 2005-2009 Christian Schoenebeck *
7     * Copyright (C) 2009 Grigor Iliev *
8 schoenebeck 411 * *
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 iliev 2012 #include "Engine.h"
27 schoenebeck 411
28 persson 438 namespace LinuxSampler { namespace gig {
29 iliev 2012 EngineChannel::EngineChannel() {
30    
31 schoenebeck 411 }
32    
33     EngineChannel::~EngineChannel() {
34 schoenebeck 460 DisconnectAudioOutputDevice();
35 iliev 1826 // In case the channel was removed before the instrument was
36     // fully loaded, try to give back instrument again (see bug #113)
37 iliev 2012 InstrumentChangeCmd< ::gig::DimensionRegion, ::gig::Instrument>& cmd = ChangeInstrument(NULL);
38 iliev 1826 if (cmd.pInstrument) {
39 iliev 2012 InstrumentResourceManager* instrs = dynamic_cast<InstrumentResourceManager*>(pEngine->GetInstrumentManager());
40     instrs->HandBack(cmd.pInstrument, this);
41 iliev 1826 }
42     ///////
43 schoenebeck 411 }
44    
45 iliev 2012 AbstractEngine::Format EngineChannel::GetEngineFormat() { return AbstractEngine::GIG; }
46 schoenebeck 660
47 iliev 2012 /** This method is not thread safe! */
48 schoenebeck 411 void EngineChannel::ResetInternal() {
49     CurrentKeyDimension = 0;
50 iliev 2012 EngineChannelBase<Voice, ::gig::DimensionRegion, ::gig::Instrument>::ResetInternal();
51 schoenebeck 411 }
52    
53     /**
54 iliev 2012 * Will be called by the MIDIIn Thread to signal that a program
55     * change should be performed. As a program change isn't
56     * real-time safe, the actual change is performed by the disk
57     * thread.
58 schoenebeck 411 *
59 iliev 2012 * @param Program - MIDI program change number
60 schoenebeck 411 */
61 iliev 2012 void EngineChannel::SendProgramChange(uint8_t Program) {
62     Engine* engine = dynamic_cast<Engine*>(pEngine);
63     if(engine == NULL) return;
64    
65     if(engine->GetDiskThread()) {
66     engine->GetDiskThread()->OrderProgramChange(Program, this);
67     } else {
68     // TODO:
69     }
70 schoenebeck 411 }
71    
72     /**
73     * Load an instrument from a .gig file. PrepareLoadInstrument() has to
74     * be called first to provide the information which instrument to load.
75     * This method will then actually start to load the instrument and block
76     * the calling thread until loading was completed.
77     *
78     * @see PrepareLoadInstrument()
79     */
80     void EngineChannel::LoadInstrument() {
81 iliev 2012 InstrumentResourceManager* pInstrumentManager = dynamic_cast<InstrumentResourceManager*>(pEngine->GetInstrumentManager());
82    
83 persson 1646 // make sure we don't trigger any new notes with an old
84     // instrument
85 iliev 2012 InstrumentChangeCmd< ::gig::DimensionRegion, ::gig::Instrument>& cmd = ChangeInstrument(0);
86 persson 1646 if (cmd.pInstrument) {
87     // give old instrument back to instrument manager, but
88     // keep the dimension regions and samples that are in use
89 iliev 2012 pInstrumentManager->HandBackInstrument(cmd.pInstrument, this, cmd.pRegionsInUse);
90 schoenebeck 411 }
91 iliev 2012 cmd.pRegionsInUse->clear();
92 schoenebeck 411
93     // delete all key groups
94     ActiveKeyGroups.clear();
95    
96     // request gig instrument from instrument manager
97 persson 1038 ::gig::Instrument* newInstrument;
98 schoenebeck 411 try {
99 schoenebeck 947 InstrumentManager::instrument_id_t instrid;
100     instrid.FileName = InstrumentFile;
101     instrid.Index = InstrumentIdx;
102 iliev 2012
103     newInstrument = pInstrumentManager->Borrow(instrid, this);
104 persson 1038 if (!newInstrument) {
105 schoenebeck 1212 throw InstrumentManagerException("resource was not created");
106 schoenebeck 411 }
107     }
108     catch (RIFF::Exception e) {
109     InstrumentStat = -2;
110 iliev 1309 StatusChanged(true);
111 schoenebeck 411 String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message;
112 schoenebeck 880 throw Exception(msg);
113 schoenebeck 411 }
114 schoenebeck 1212 catch (InstrumentManagerException e) {
115 schoenebeck 411 InstrumentStat = -3;
116 iliev 1309 StatusChanged(true);
117 schoenebeck 411 String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message();
118 schoenebeck 880 throw Exception(msg);
119 schoenebeck 411 }
120     catch (...) {
121     InstrumentStat = -4;
122 iliev 1309 StatusChanged(true);
123 schoenebeck 880 throw Exception("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file.");
124 schoenebeck 411 }
125    
126 persson 2043 RoundRobinIndex = 0;
127     for (int i = 0 ; i < 128 ; i++) pMIDIKeyInfo[i].pRoundRobinIndex = NULL;
128    
129     // rebuild ActiveKeyGroups map with key groups of current
130     // instrument and set the round robin pointers to use one
131     // counter for each region
132     int region = 0;
133     for (::gig::Region* pRegion = newInstrument->GetFirstRegion(); pRegion; pRegion = newInstrument->GetNextRegion()) {
134 schoenebeck 411 if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL;
135    
136 persson 2043 RoundRobinIndexes[region] = 0;
137     for (int iKey = pRegion->KeyRange.low; iKey <= pRegion->KeyRange.high; iKey++) {
138     pMIDIKeyInfo[iKey].pRoundRobinIndex = &RoundRobinIndexes[region];
139     }
140     region++;
141     }
142    
143 persson 1038 InstrumentIdxName = newInstrument->pInfo->Name;
144 schoenebeck 411 InstrumentStat = 100;
145    
146 persson 1646 ChangeInstrument(newInstrument);
147    
148 iliev 1298 StatusChanged(true);
149 schoenebeck 411 }
150    
151 iliev 2012 void EngineChannel::ProcessKeySwitchChange(int key) {
152     // Change key dimension value if key is in keyswitching area
153 persson 1646 {
154 iliev 2012 if (key >= pInstrument->DimensionKeyRange.low && key <= pInstrument->DimensionKeyRange.high)
155     CurrentKeyDimension = float(key - pInstrument->DimensionKeyRange.low) /
156     (pInstrument->DimensionKeyRange.high - pInstrument->DimensionKeyRange.low + 1);
157 persson 1646 }
158 schoenebeck 411 }
159    
160     }} // namespace LinuxSampler::gig

  ViewVC Help
Powered by ViewVC