1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
/*
* RemoteZynAddSubFx.cpp - ZynAddSubFx-embedding plugin
*
* Copyright (c) 2008-2010 Tobias Doerffel <tobydox/at/users.sourceforge.net>
*
* This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/
#include <lmmsconfig.h>
#ifdef LMMS_BUILD_WIN32
#include <winsock2.h>
#endif
#include <queue>
#define BUILD_REMOTE_PLUGIN_CLIENT
#include "note.h"
#include "RemotePlugin.h"
#include "RemoteZynAddSubFx.h"
#include "LocalZynAddSubFx.h"
#include "src/UI/MasterUI.h"
#include <FL/x.H>
class RemoteZynAddSubFx : public RemotePluginClient, public LocalZynAddSubFx
{
public:
RemoteZynAddSubFx( int _shm_in, int _shm_out ) :
RemotePluginClient( _shm_in, _shm_out ),
LocalZynAddSubFx(),
m_guiSleepTime( 100 ),
m_guiExit( false )
{
setInputCount( 0 );
sendMessage( IdInitDone );
waitForMessage( IdInitDone );
pthread_mutex_init( &m_guiMutex, NULL );
pthread_create( &m_guiThreadHandle, NULL, guiThread, this );
}
virtual ~RemoteZynAddSubFx()
{
m_guiExit = true;
#ifdef LMMS_BUILD_WIN32
Sleep( m_guiSleepTime * 2 );
#else
usleep( m_guiSleepTime * 2 * 1000 );
#endif
}
virtual void updateSampleRate()
{
LocalZynAddSubFx::setSampleRate( sampleRate() );
}
virtual void updateBufferSize()
{
LocalZynAddSubFx::setBufferSize( bufferSize() );
}
void run()
{
message m;
while( ( m = receiveMessage() ).id != IdQuit )
{
pthread_mutex_lock( &m_master->mutex );
processMessage( m );
pthread_mutex_unlock( &m_master->mutex );
}
}
virtual bool processMessage( const message & _m )
{
switch( _m.id )
{
case IdQuit:
break;
case IdShowUI:
case IdHideUI:
case IdLoadSettingsFromFile:
case IdLoadPresetFromFile:
pthread_mutex_lock( &m_guiMutex );
m_guiMessages.push( _m );
pthread_mutex_unlock( &m_guiMutex );
break;
case IdSaveSettingsToFile:
{
LocalZynAddSubFx::saveXML( _m.getString() );
sendMessage( IdSaveSettingsToFile );
break;
}
case IdZasfPresetDirectory:
LocalZynAddSubFx::setPresetDir( _m.getString() );
break;
case IdZasfLmmsWorkingDirectory:
LocalZynAddSubFx::setLmmsWorkingDir( _m.getString() );
break;
default:
return RemotePluginClient::processMessage( _m );
}
return true;
}
// all functions are called while m_master->mutex is held
virtual void processMidiEvent( const midiEvent & _e,
const f_cnt_t /* _offset */ )
{
LocalZynAddSubFx::processMidiEvent( _e );
}
virtual void process( const sampleFrame * _in, sampleFrame * _out )
{
LocalZynAddSubFx::processAudio( _out );
}
static void * guiThread( void * _arg )
{
RemoteZynAddSubFx * _this =
static_cast<RemoteZynAddSubFx *>( _arg );
_this->guiThread();
return NULL;
}
private:
void guiThread();
const int m_guiSleepTime;
pthread_t m_guiThreadHandle;
pthread_mutex_t m_guiMutex;
std::queue<RemotePluginClient::message> m_guiMessages;
bool m_guiExit;
} ;
void RemoteZynAddSubFx::guiThread()
{
int exitProgram;
MasterUI * ui = NULL;
while( !m_guiExit )
{
if( ui )
{
Fl::wait( m_guiSleepTime / 1000.0 );
}
else
{
#ifdef LMMS_BUILD_WIN32
Sleep( m_guiSleepTime );
#else
usleep( m_guiSleepTime*1000 );
#endif
}
if( exitProgram == 1 )
{
pthread_mutex_lock( &m_master->mutex );
sendMessage( IdHideUI );
exitProgram = 0;
pthread_mutex_unlock( &m_master->mutex );
}
pthread_mutex_lock( &m_guiMutex );
while( m_guiMessages.size() )
{
RemotePluginClient::message m = m_guiMessages.front();
m_guiMessages.pop();
switch( m.id )
{
case IdShowUI:
// we only create GUI
if( !ui )
{
Fl::scheme( "plastic" );
ui = new MasterUI( m_master, &exitProgram );
}
ui->showUI();
ui->refresh_master_ui();
break;
case IdLoadSettingsFromFile:
{
LocalZynAddSubFx::loadXML( m.getString() );
if( ui )
{
ui->refresh_master_ui();
}
pthread_mutex_lock( &m_master->mutex );
sendMessage( IdLoadSettingsFromFile );
pthread_mutex_unlock( &m_master->mutex );
break;
}
case IdLoadPresetFromFile:
{
LocalZynAddSubFx::loadPreset( m.getString(), ui ?
ui->npartcounter->value()-1 : 0 );
if( ui )
{
ui->npartcounter->do_callback();
ui->updatepanel();
ui->refresh_master_ui();
}
pthread_mutex_lock( &m_master->mutex );
sendMessage( IdLoadPresetFromFile );
pthread_mutex_unlock( &m_master->mutex );
break;
}
default:
break;
}
}
pthread_mutex_unlock( &m_guiMutex );
}
Fl::flush();
delete ui;
}
int main( int _argc, char * * _argv )
{
if( _argc < 3 )
{
fprintf( stderr, "not enough arguments\n" );
return -1;
}
#ifdef LMMS_BUILD_WIN32
// (non-portable) initialization of statically linked pthread library
pthread_win32_process_attach_np();
pthread_win32_thread_attach_np();
#endif
RemoteZynAddSubFx * remoteZASF =
new RemoteZynAddSubFx( atoi( _argv[1] ), atoi( _argv[2] ) );
remoteZASF->run();
delete remoteZASF;
#ifdef LMMS_BUILD_WIN32
pthread_win32_thread_detach_np();
pthread_win32_process_detach_np();
#endif
return 0;
}
|