/[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 1889 - (hide annotations) (download)
Sun Apr 26 12:19:00 2009 UTC (15 years ago) by persson
File size: 8062 byte(s)
* fixed a memory management error which could cause a crash when a
  plugin was unloaded
* minor fixes in ASIO and MME drivers for win64

1 persson 1777 /***************************************************************************
2     * *
3 persson 1889 * Copyright (C) 2008 - 2009 Andreas Persson *
4 persson 1777 * *
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 persson 1889 AudioOutputDeviceFactory::Unregister("ASIO");
37 persson 1777
38     REGISTER_AUDIO_OUTPUT_DRIVER(AudioOutputDevicePlugin);
39     REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDevicePlugin, ParameterActive);
40     REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDevicePlugin, ParameterSampleRate);
41     REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDevicePlugin, ParameterChannels);
42     REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDevicePlugin, ParameterFragmentSize);
43    
44     REGISTER_MIDI_INPUT_DRIVER(MidiInputDevicePlugin);
45     REGISTER_MIDI_INPUT_DRIVER_PARAMETER(MidiInputDevicePlugin, ParameterActive);
46     REGISTER_MIDI_INPUT_DRIVER_PARAMETER(MidiInputDevicePlugin, ParameterPorts);
47    
48     pSampler = new Sampler;
49    
50     // using LOOPBACK instead of ANY to prevent windows firewall
51     // warnings
52     pLSCPServer = new LSCPServer(pSampler, htonl(INADDR_LOOPBACK),
53     htons(LSCP_PORT));
54     pLSCPServer->StartThread();
55     pLSCPServer->WaitUntilInitialized();
56    
57     pEventThread = new EventThread(pSampler);
58     pEventThread->StartThread();
59     }
60    
61    
62     PluginGlobal::~PluginGlobal() {
63     pEventThread->StopThread();
64     pLSCPServer->StopThread();
65 iliev 1835 pLSCPServer->RemoveListeners();
66 persson 1777
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 iliev 1835 Plugin::Plugin(bool bDoPreInit) :
96 persson 1777 pAudioDevice(0),
97     pMidiDevice(0) {
98 iliev 1835 bPreInitDone = false;
99     if (bDoPreInit) PreInit();
100     }
101    
102     void Plugin::PreInit() {
103     if (bPreInitDone) return;
104    
105     bPreInitDone = true;
106 persson 1777 if (!global) {
107     global = new PluginGlobal;
108     }
109     global->RefCount++;
110     }
111    
112 iliev 1835 void Plugin::Init(int SampleRate, int FragmentSize, int Channels) {
113 persson 1777 std::map<String, String> params;
114     params["SAMPLERATE"] = ToString(SampleRate);
115     params["FRAGMENTSIZE"] = ToString(FragmentSize);
116 iliev 1835 if (Channels > 0) params["CHANNELS"] = ToString(Channels);
117 persson 1777 pAudioDevice = dynamic_cast<AudioOutputDevicePlugin*>(
118     global->pSampler->CreateAudioOutputDevice(AudioOutputDevicePlugin::Name(), params));
119    
120     pMidiDevice = dynamic_cast<MidiInputDevicePlugin*>(
121     global->pSampler->CreateMidiInputDevice(MidiInputDevicePlugin::Name(),
122     std::map<String,String>()));
123     }
124    
125     Plugin::~Plugin() {
126     RemoveChannels();
127     if (pAudioDevice) global->pSampler->DestroyAudioOutputDevice(pAudioDevice);
128     if (pMidiDevice) global->pSampler->DestroyMidiInputDevice(pMidiDevice);
129 iliev 1835 if (bPreInitDone) {
130     if (--global->RefCount == 0) {
131     delete global;
132     global = 0;
133     }
134 persson 1777 }
135     }
136    
137     void Plugin::InitState() {
138     SamplerChannel* channel = global->pSampler->AddSamplerChannel();
139     channel->SetEngineType("gig");
140     channel->SetAudioOutputDevice(pAudioDevice);
141     channel->SetMidiInputDevice(pMidiDevice);
142     channel->SetMidiInputChannel(midi_chan_1);
143     }
144    
145    
146     String Plugin::GetState() {
147     std::stringstream s;
148    
149     s << GLOBAL_VOLUME << '\n';
150     std::map<uint, SamplerChannel*> channels = global->pSampler->GetSamplerChannels();
151     for (std::map<uint, SamplerChannel*>::iterator iter = channels.begin() ;
152     iter != channels.end() ; iter++) {
153     SamplerChannel* channel = iter->second;
154     if (channel->GetAudioOutputDevice() == pAudioDevice) {
155     EngineChannel* engine_channel = channel->GetEngineChannel();
156     String filename = engine_channel->InstrumentFileName();
157     s << channel->GetMidiInputChannel() << ' ' <<
158     engine_channel->Volume() << ' ' <<
159     filename << '\n' <<
160     engine_channel->InstrumentIndex() << ' ' <<
161     engine_channel->GetSolo() << ' ' <<
162     (engine_channel->GetMute() == 1) << '\n';
163     }
164     }
165     return s.str();
166     }
167    
168     void Plugin::RemoveChannels() {
169 iliev 1835 if(global == NULL) return;
170    
171 persson 1777 std::map<uint, SamplerChannel*> channels = global->pSampler->GetSamplerChannels();
172 iliev 1835
173 persson 1777 for (std::map<uint, SamplerChannel*>::iterator iter = channels.begin() ;
174     iter != channels.end() ; iter++) {
175     if (iter->second->GetAudioOutputDevice() == pAudioDevice) {
176     global->pSampler->RemoveSamplerChannel(iter->second);
177     }
178     }
179     }
180    
181     bool Plugin::SetState(String State) {
182     RemoveChannels();
183    
184     std::stringstream s(State);
185     s >> GLOBAL_VOLUME;
186    
187     int midiChannel;
188     float volume;
189     while (s >> midiChannel >> volume) {
190     s.ignore();
191     String filename;
192     std::getline(s, filename);
193     int index;
194     bool solo;
195     bool mute;
196     s >> index >> solo >> mute;
197    
198     SamplerChannel* channel = global->pSampler->AddSamplerChannel();
199     channel->SetEngineType("gig");
200     channel->SetAudioOutputDevice(pAudioDevice);
201     channel->SetMidiInputDevice(pMidiDevice);
202     channel->SetMidiInputChannel(midi_chan_t(midiChannel));
203    
204     EngineChannel* engine_channel = channel->GetEngineChannel();
205     engine_channel->Volume(volume);
206     if (!filename.empty() && index != -1) {
207 iliev 1882 InstrumentManager::instrument_id_t id;
208     id.FileName = filename;
209     id.Index = index;
210     InstrumentManager::LoadInstrumentInBackground(id, engine_channel);
211 persson 1777 }
212     if (solo) engine_channel->SetSolo(solo);
213     if (mute) engine_channel->SetMute(1);
214     }
215    
216     return true;
217     }
218     }

  ViewVC Help
Powered by ViewVC