/[svn]/linuxsampler/trunk/src/drivers/Plugin.cpp
ViewVC logotype

Annotation of /linuxsampler/trunk/src/drivers/Plugin.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1882 - (hide annotations) (download)
Wed Apr 1 15:21:13 2009 UTC (11 years, 8 months ago) by iliev
File size: 8140 byte(s)
* LSCP server: hard close of all sockets on exit
* Load instruments in background when restoring plugin state

1 persson 1777 /***************************************************************************
2     * *
3     * Copyright (C) 2008 Andreas Persson *
4     * *
5     * This program is free software; you can redistribute it and/or modify *
6     * it under the terms of the GNU General Public License as published by *
7     * the Free Software Foundation; either version 2 of the License, or *
8     * (at your option) any later version. *
9     * *
10     * This program is distributed in the hope that it will be useful, *
11     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13     * GNU General Public License for more details. *
14     * *
15     * You should have received a copy of the GNU General Public License *
16     * along with this program; if not, write to the Free Software *
17     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
18     * MA 02110-1301 USA *
19     ***************************************************************************/
20    
21     #include <sstream>
22    
23     #include "Plugin.h"
24     #include "audio/AudioOutputDeviceFactory.h"
25     #include "midi/MidiInputDeviceFactory.h"
26    
27     namespace LinuxSampler {
28    
29     // *************** PluginGlobal ***************
30     // *
31    
32     PluginGlobal::PluginGlobal() :
33     RefCount(0) {
34     // we need to remove the ASIO driver, otherwise the lscp info
35     // methods will lock up the audio device
36     AudioOutputDeviceFactory::InnerFactories.erase("ASIO");
37     AudioOutputDeviceFactory::ParameterFactories.erase("ASIO");
38    
39     REGISTER_AUDIO_OUTPUT_DRIVER(AudioOutputDevicePlugin);
40     REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDevicePlugin, ParameterActive);
41     REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDevicePlugin, ParameterSampleRate);
42     REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDevicePlugin, ParameterChannels);
43     REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDevicePlugin, ParameterFragmentSize);
44    
45     REGISTER_MIDI_INPUT_DRIVER(MidiInputDevicePlugin);
46     REGISTER_MIDI_INPUT_DRIVER_PARAMETER(MidiInputDevicePlugin, ParameterActive);
47     REGISTER_MIDI_INPUT_DRIVER_PARAMETER(MidiInputDevicePlugin, ParameterPorts);
48    
49     pSampler = new Sampler;
50    
51     // using LOOPBACK instead of ANY to prevent windows firewall
52     // warnings
53     pLSCPServer = new LSCPServer(pSampler, htonl(INADDR_LOOPBACK),
54     htons(LSCP_PORT));
55     pLSCPServer->StartThread();
56     pLSCPServer->WaitUntilInitialized();
57    
58     pEventThread = new EventThread(pSampler);
59     pEventThread->StartThread();
60     }
61    
62    
63     PluginGlobal::~PluginGlobal() {
64     pEventThread->StopThread();
65     pLSCPServer->StopThread();
66 iliev 1835 pLSCPServer->RemoveListeners();
67 persson 1777
68     delete pEventThread;
69     delete pSampler;
70     delete pLSCPServer;
71     }
72    
73    
74     // *************** EventThread ***************
75     // *
76    
77    
78     EventThread::EventThread(Sampler* pSampler) :
79     Thread(false, false, 0, 0),
80     pSampler(pSampler) {
81     }
82    
83     int EventThread::Main() {
84     for (;;) {
85     sleep(1);
86     pSampler->fireStatistics();
87     }
88     }
89    
90    
91     // *************** Plugin ***************
92     // *
93    
94     PluginGlobal* Plugin::global = 0;
95    
96 iliev 1835 Plugin::Plugin(bool bDoPreInit) :
97 persson 1777 pAudioDevice(0),
98     pMidiDevice(0) {
99 iliev 1835 bPreInitDone = false;
100     if (bDoPreInit) PreInit();
101     }
102    
103     void Plugin::PreInit() {
104     if (bPreInitDone) return;
105    
106     bPreInitDone = true;
107 persson 1777 if (!global) {
108     global = new PluginGlobal;
109     }
110     global->RefCount++;
111     }
112    
113 iliev 1835 void Plugin::Init(int SampleRate, int FragmentSize, int Channels) {
114 persson 1777 std::map<String, String> params;
115     params["SAMPLERATE"] = ToString(SampleRate);
116     params["FRAGMENTSIZE"] = ToString(FragmentSize);
117 iliev 1835 if (Channels > 0) params["CHANNELS"] = ToString(Channels);
118 persson 1777 pAudioDevice = dynamic_cast<AudioOutputDevicePlugin*>(
119     global->pSampler->CreateAudioOutputDevice(AudioOutputDevicePlugin::Name(), params));
120    
121     pMidiDevice = dynamic_cast<MidiInputDevicePlugin*>(
122     global->pSampler->CreateMidiInputDevice(MidiInputDevicePlugin::Name(),
123     std::map<String,String>()));
124     }
125    
126     Plugin::~Plugin() {
127     RemoveChannels();
128     if (pAudioDevice) global->pSampler->DestroyAudioOutputDevice(pAudioDevice);
129     if (pMidiDevice) global->pSampler->DestroyMidiInputDevice(pMidiDevice);
130 iliev 1835 if (bPreInitDone) {
131     if (--global->RefCount == 0) {
132     delete global;
133     global = 0;
134     }
135 persson 1777 }
136     }
137    
138     void Plugin::InitState() {
139     SamplerChannel* channel = global->pSampler->AddSamplerChannel();
140     channel->SetEngineType("gig");
141     channel->SetAudioOutputDevice(pAudioDevice);
142     channel->SetMidiInputDevice(pMidiDevice);
143     channel->SetMidiInputChannel(midi_chan_1);
144     }
145    
146    
147     String Plugin::GetState() {
148     std::stringstream s;
149    
150     s << GLOBAL_VOLUME << '\n';
151     std::map<uint, SamplerChannel*> channels = global->pSampler->GetSamplerChannels();
152     for (std::map<uint, SamplerChannel*>::iterator iter = channels.begin() ;
153     iter != channels.end() ; iter++) {
154     SamplerChannel* channel = iter->second;
155     if (channel->GetAudioOutputDevice() == pAudioDevice) {
156     EngineChannel* engine_channel = channel->GetEngineChannel();
157     String filename = engine_channel->InstrumentFileName();
158     s << channel->GetMidiInputChannel() << ' ' <<
159     engine_channel->Volume() << ' ' <<
160     filename << '\n' <<
161     engine_channel->InstrumentIndex() << ' ' <<
162     engine_channel->GetSolo() << ' ' <<
163     (engine_channel->GetMute() == 1) << '\n';
164     }
165     }
166     return s.str();
167     }
168    
169     void Plugin::RemoveChannels() {
170 iliev 1835 if(global == NULL) return;
171    
172 persson 1777 std::map<uint, SamplerChannel*> channels = global->pSampler->GetSamplerChannels();
173 iliev 1835
174 persson 1777 for (std::map<uint, SamplerChannel*>::iterator iter = channels.begin() ;
175     iter != channels.end() ; iter++) {
176     if (iter->second->GetAudioOutputDevice() == pAudioDevice) {
177     global->pSampler->RemoveSamplerChannel(iter->second);
178     }
179     }
180     }
181    
182     bool Plugin::SetState(String State) {
183     RemoveChannels();
184    
185     std::stringstream s(State);
186     s >> GLOBAL_VOLUME;
187    
188     int midiChannel;
189     float volume;
190     while (s >> midiChannel >> volume) {
191     s.ignore();
192     String filename;
193     std::getline(s, filename);
194     int index;
195     bool solo;
196     bool mute;
197     s >> index >> solo >> mute;
198    
199     SamplerChannel* channel = global->pSampler->AddSamplerChannel();
200     channel->SetEngineType("gig");
201     channel->SetAudioOutputDevice(pAudioDevice);
202     channel->SetMidiInputDevice(pMidiDevice);
203     channel->SetMidiInputChannel(midi_chan_t(midiChannel));
204    
205     EngineChannel* engine_channel = channel->GetEngineChannel();
206     engine_channel->Volume(volume);
207     if (!filename.empty() && index != -1) {
208 iliev 1882 InstrumentManager::instrument_id_t id;
209     id.FileName = filename;
210     id.Index = index;
211     InstrumentManager::LoadInstrumentInBackground(id, engine_channel);
212 persson 1777 }
213     if (solo) engine_channel->SetSolo(solo);
214     if (mute) engine_channel->SetMute(1);
215     }
216    
217     return true;
218     }
219     }

  ViewVC Help
Powered by ViewVC