From 0abd4aad70c95bfad0abc113b44462986a97c87b Mon Sep 17 00:00:00 2001 From: Tobias Doerffel Date: Thu, 9 Apr 2009 23:40:04 +0200 Subject: [PATCH] VstBase: initial support for saving/restoring chunks Some VST plugins save their state as chunks rather than parameters. According code to save/restore those chunks has been taken from FST 1.9. However restore currently is disabled as it mostly fails for whatever reason. --- plugins/vst_base/remote_vst_plugin.cpp | 54 ++++++++++++++++++++++++++ plugins/vst_base/vst_plugin.cpp | 51 ++++++++++++++++++++++++ plugins/vst_base/vst_plugin.h | 5 ++- 3 files changed, 109 insertions(+), 1 deletion(-) diff --git a/plugins/vst_base/remote_vst_plugin.cpp b/plugins/vst_base/remote_vst_plugin.cpp index 30a17ecbd..d1488ff1a 100644 --- a/plugins/vst_base/remote_vst_plugin.cpp +++ b/plugins/vst_base/remote_vst_plugin.cpp @@ -38,6 +38,10 @@ #include #endif +#ifdef LMMS_HAVE_FCNTL_H +#include +#endif + #ifdef LMMS_BUILD_LINUX #ifdef LMMS_HAVE_SCHED_H @@ -184,6 +188,12 @@ public: // post properties of specified parameter void getParameterProperties( const int _idx ); + // save settings chunk of plugin into file + void saveChunkToFile( const std::string & _file ); + + // restore settings chunk of plugin from file + void loadChunkFromFile( const std::string & _file, int _len ); + // number of inputs virtual int inputCount( void ) const { @@ -350,6 +360,16 @@ bool remoteVstPlugin::processMessage( const message & _m ) getParameterProperties( _m.getInt() ); break; + case IdSaveSettingsToFile: + saveChunkToFile( _m.getString() ); + sendMessage( IdSaveSettingsToFile ); + break; + + case IdLoadSettingsFromFile: + loadChunkFromFile( _m.getString( 0 ), _m.getInt( 1 ) ); + sendMessage( IdLoadSettingsFromFile ); + break; + default: return remotePluginClient::processMessage( _m ); } @@ -799,6 +819,40 @@ void remoteVstPlugin::getParameterProperties( const int _idx ) +void remoteVstPlugin::saveChunkToFile( const std::string & _file ) +{ + if( m_plugin->flags & 32 ) + { + void * chunk = NULL; + int len = m_plugin->dispatcher( m_plugin, 23, 0, 0, &chunk, 0 ); + if( len > 0 ) + { + int fd = open( _file.c_str(), O_WRONLY ); + write( fd, chunk, len ); + close( fd ); + } + } +} + + + + +void remoteVstPlugin::loadChunkFromFile( const std::string & _file, int _len ) +{ + char * buf = new char[_len]; + + const int fd = open( _file.c_str(), O_RDONLY ); + read( fd, buf, _len ); + close( fd ); + + // doesn't work for various plugins for some reason :-( + // m_plugin->dispatcher( m_plugin, 24, 0, _len, buf, 0 ); + delete[] buf; +} + + + + void remoteVstPlugin::updateInOutCount( void ) { delete[] m_inputs; diff --git a/plugins/vst_base/vst_plugin.cpp b/plugins/vst_base/vst_plugin.cpp index a7e51c1a6..ec04b5e34 100644 --- a/plugins/vst_base/vst_plugin.cpp +++ b/plugins/vst_base/vst_plugin.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -250,6 +251,12 @@ void vstPlugin::loadSettings( const QDomElement & _this ) } setParameterDump( dump ); } + + if( _this.hasAttribute( "chunk" ) ) + { + loadChunk( QByteArray::fromBase64( + _this.attribute( "chunk" ).toAscii() ) ); + } } @@ -268,6 +275,11 @@ void vstPlugin::saveSettings( QDomDocument & _doc, QDomElement & _this ) { _this.setAttribute( it.key(), it.value() ); } + QByteArray chunk = saveChunk(); + if( !chunk.isEmpty() ) + { + _this.setAttribute( "chunk", QString( chunk.toBase64() ) ); + } } @@ -393,5 +405,44 @@ bool vstPlugin::processMessage( const message & _m ) + +void vstPlugin::loadChunk( const QByteArray & _chunk ) +{ + QTemporaryFile tf; + if( tf.open() ) + { + tf.write( _chunk ); + lock(); + sendMessage( message( IdLoadSettingsFromFile ). + addString( QDir::toNativeSeparators( + tf.fileName() ).toStdString() ). + addInt( _chunk.size() ) ); + waitForMessage( IdLoadSettingsFromFile ); + unlock(); + } +} + + + + +QByteArray vstPlugin::saveChunk( void ) +{ + QByteArray a; + QTemporaryFile tf; + if( tf.open() ) + { + lock(); + sendMessage( message( IdSaveSettingsToFile ). + addString( QDir::toNativeSeparators( + tf.fileName() ).toStdString() ) ); + waitForMessage( IdSaveSettingsToFile ); + unlock(); + a = tf.readAll(); + } + + return a; +} + + #include "moc_vst_plugin.cxx" diff --git a/plugins/vst_base/vst_plugin.h b/plugins/vst_base/vst_plugin.h index d41a33a9c..af8094f6b 100644 --- a/plugins/vst_base/vst_plugin.h +++ b/plugins/vst_base/vst_plugin.h @@ -1,7 +1,7 @@ /* * vst_plugin.h - declaration of vstPlugin class * - * Copyright (c) 2005-2008 Tobias Doerffel + * Copyright (c) 2005-2009 Tobias Doerffel * * This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net * @@ -101,6 +101,9 @@ public slots: private: + void loadChunk( const QByteArray & _chunk ); + QByteArray saveChunk( void ); + QString m_plugin; QWidget * m_pluginWidget; int m_pluginWindowID;