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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1777 - (show annotations) (download)
Mon Sep 15 16:58:10 2008 UTC (15 years, 6 months ago) by persson
File size: 7663 byte(s)
* added experimental support for running LinuxSampler as a DSSI, LV2
  and VST plugin

1 /***************************************************************************
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
67 delete pEventThread;
68 delete pSampler;
69 delete pLSCPServer;
70 }
71
72
73 // *************** EventThread ***************
74 // *
75
76
77 EventThread::EventThread(Sampler* pSampler) :
78 Thread(false, false, 0, 0),
79 pSampler(pSampler) {
80 }
81
82 int EventThread::Main() {
83 for (;;) {
84 sleep(1);
85 pSampler->fireStatistics();
86 }
87 }
88
89
90 // *************** Plugin ***************
91 // *
92
93 PluginGlobal* Plugin::global = 0;
94
95 Plugin::Plugin() :
96 pAudioDevice(0),
97 pMidiDevice(0) {
98 if (!global) {
99 global = new PluginGlobal;
100 }
101 global->RefCount++;
102 }
103
104 void Plugin::Init(int SampleRate, int FragmentSize) {
105 std::map<String, String> params;
106 params["SAMPLERATE"] = ToString(SampleRate);
107 params["FRAGMENTSIZE"] = ToString(FragmentSize);
108 pAudioDevice = dynamic_cast<AudioOutputDevicePlugin*>(
109 global->pSampler->CreateAudioOutputDevice(AudioOutputDevicePlugin::Name(), params));
110
111 pMidiDevice = dynamic_cast<MidiInputDevicePlugin*>(
112 global->pSampler->CreateMidiInputDevice(MidiInputDevicePlugin::Name(),
113 std::map<String,String>()));
114 }
115
116 Plugin::~Plugin() {
117 RemoveChannels();
118 if (pAudioDevice) global->pSampler->DestroyAudioOutputDevice(pAudioDevice);
119 if (pMidiDevice) global->pSampler->DestroyMidiInputDevice(pMidiDevice);
120 if (--global->RefCount == 0) {
121 delete global;
122 global = 0;
123 }
124 }
125
126 void Plugin::InitState() {
127 SamplerChannel* channel = global->pSampler->AddSamplerChannel();
128 channel->SetEngineType("gig");
129 channel->SetAudioOutputDevice(pAudioDevice);
130 channel->SetMidiInputDevice(pMidiDevice);
131 channel->SetMidiInputChannel(midi_chan_1);
132 }
133
134
135 String Plugin::GetState() {
136 std::stringstream s;
137
138 s << GLOBAL_VOLUME << '\n';
139 std::map<uint, SamplerChannel*> channels = global->pSampler->GetSamplerChannels();
140 for (std::map<uint, SamplerChannel*>::iterator iter = channels.begin() ;
141 iter != channels.end() ; iter++) {
142 SamplerChannel* channel = iter->second;
143 if (channel->GetAudioOutputDevice() == pAudioDevice) {
144 EngineChannel* engine_channel = channel->GetEngineChannel();
145 String filename = engine_channel->InstrumentFileName();
146 s << channel->GetMidiInputChannel() << ' ' <<
147 engine_channel->Volume() << ' ' <<
148 filename << '\n' <<
149 engine_channel->InstrumentIndex() << ' ' <<
150 engine_channel->GetSolo() << ' ' <<
151 (engine_channel->GetMute() == 1) << '\n';
152 }
153 }
154 return s.str();
155 }
156
157 void Plugin::RemoveChannels() {
158 std::map<uint, SamplerChannel*> channels = global->pSampler->GetSamplerChannels();
159 for (std::map<uint, SamplerChannel*>::iterator iter = channels.begin() ;
160 iter != channels.end() ; iter++) {
161 if (iter->second->GetAudioOutputDevice() == pAudioDevice) {
162 global->pSampler->RemoveSamplerChannel(iter->second);
163 }
164 }
165 }
166
167 bool Plugin::SetState(String State) {
168 RemoveChannels();
169
170 std::stringstream s(State);
171 s >> GLOBAL_VOLUME;
172
173 int midiChannel;
174 float volume;
175 while (s >> midiChannel >> volume) {
176 s.ignore();
177 String filename;
178 std::getline(s, filename);
179 int index;
180 bool solo;
181 bool mute;
182 s >> index >> solo >> mute;
183
184 SamplerChannel* channel = global->pSampler->AddSamplerChannel();
185 channel->SetEngineType("gig");
186 channel->SetAudioOutputDevice(pAudioDevice);
187 channel->SetMidiInputDevice(pMidiDevice);
188 channel->SetMidiInputChannel(midi_chan_t(midiChannel));
189
190 EngineChannel* engine_channel = channel->GetEngineChannel();
191 engine_channel->Volume(volume);
192 if (!filename.empty() && index != -1) {
193 engine_channel->PrepareLoadInstrument(filename.c_str(), index);
194 engine_channel->LoadInstrument();
195 }
196 if (solo) engine_channel->SetSolo(solo);
197 if (mute) engine_channel->SetMute(1);
198 }
199
200 return true;
201 }
202 }

  ViewVC Help
Powered by ViewVC