LocalZynAddSubFx.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * LocalZynAddSubFx.cpp - local implementation of ZynAddSubFx plugin
  3. *
  4. * Copyright (c) 2009-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  5. *
  6. * This file is part of LMMS - https://lmms.io
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program (see COPYING); if not, write to the
  20. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301 USA.
  22. *
  23. */
  24. #include <lmmsconfig.h>
  25. #include "zynaddsubfx/src/Misc/Util.h"
  26. #include <unistd.h>
  27. #include <ctime>
  28. #include "LocalZynAddSubFx.h"
  29. #include "zynaddsubfx/src/Nio/NulEngine.h"
  30. #include "zynaddsubfx/src/Misc/Master.h"
  31. #include "zynaddsubfx/src/Misc/Part.h"
  32. #include "zynaddsubfx/src/Misc/Dump.h"
  33. SYNTH_T* synth = NULL;
  34. int LocalZynAddSubFx::s_instanceCount = 0;
  35. LocalZynAddSubFx::LocalZynAddSubFx() :
  36. m_master( NULL ),
  37. m_ioEngine( NULL )
  38. {
  39. for( int i = 0; i < NumKeys; ++i )
  40. {
  41. m_runningNotes[i] = 0;
  42. }
  43. if( s_instanceCount == 0 )
  44. {
  45. #ifdef LMMS_BUILD_WIN32
  46. #ifndef __WINPTHREADS_VERSION
  47. // (non-portable) initialization of statically linked pthread library
  48. pthread_win32_process_attach_np();
  49. pthread_win32_thread_attach_np();
  50. #endif
  51. #endif
  52. initConfig();
  53. synth = new SYNTH_T;
  54. synth->oscilsize = config.cfg.OscilSize;
  55. synth->alias();
  56. srand( time( NULL ) );
  57. denormalkillbuf = new float[synth->buffersize];
  58. for( int i = 0; i < synth->buffersize; ++i )
  59. {
  60. denormalkillbuf[i] = (RND-0.5)*1e-16;
  61. }
  62. }
  63. ++s_instanceCount;
  64. m_ioEngine = new NulEngine;
  65. m_master = new Master();
  66. m_master->swaplr = 0;
  67. }
  68. LocalZynAddSubFx::~LocalZynAddSubFx()
  69. {
  70. delete m_master;
  71. delete m_ioEngine;
  72. if( --s_instanceCount == 0 )
  73. {
  74. delete[] denormalkillbuf;
  75. }
  76. }
  77. void LocalZynAddSubFx::initConfig()
  78. {
  79. config.init();
  80. config.cfg.GzipCompression = 0;
  81. }
  82. void LocalZynAddSubFx::setSampleRate( int sampleRate )
  83. {
  84. synth->samplerate = sampleRate;
  85. synth->alias();
  86. }
  87. void LocalZynAddSubFx::setBufferSize( int bufferSize )
  88. {
  89. synth->buffersize = bufferSize;
  90. synth->alias();
  91. }
  92. void LocalZynAddSubFx::saveXML( const std::string & _filename )
  93. {
  94. char * name = strdup( _filename.c_str() );
  95. m_master->saveXML( name );
  96. free( name );
  97. }
  98. void LocalZynAddSubFx::loadXML( const std::string & _filename )
  99. {
  100. char * f = strdup( _filename.c_str() );
  101. pthread_mutex_lock( &m_master->mutex );
  102. m_master->defaults();
  103. m_master->loadXML( f );
  104. pthread_mutex_unlock( &m_master->mutex );
  105. m_master->applyparameters();
  106. unlink( f );
  107. free( f );
  108. }
  109. void LocalZynAddSubFx::loadPreset( const std::string & _filename, int _part )
  110. {
  111. char * f = strdup( _filename.c_str() );
  112. pthread_mutex_lock( &m_master->mutex );
  113. m_master->part[_part]->defaultsinstrument();
  114. m_master->part[_part]->loadXMLinstrument( f );
  115. pthread_mutex_unlock( &m_master->mutex );
  116. m_master->applyparameters();
  117. free( f );
  118. }
  119. void LocalZynAddSubFx::setPresetDir( const std::string & _dir )
  120. {
  121. m_presetsDir = _dir;
  122. for( int i = 0; i < MAX_BANK_ROOT_DIRS; ++i )
  123. {
  124. if( config.cfg.bankRootDirList[i].empty() )
  125. {
  126. config.cfg.bankRootDirList[i] = m_presetsDir;
  127. break;
  128. }
  129. else if( config.cfg.bankRootDirList[i] == m_presetsDir )
  130. {
  131. break;
  132. }
  133. }
  134. }
  135. void LocalZynAddSubFx::setLmmsWorkingDir( const std::string & _dir )
  136. {
  137. if( config.workingDir != NULL )
  138. {
  139. free( config.workingDir );
  140. }
  141. config.workingDir = strdup( _dir.c_str() );
  142. initConfig();
  143. }
  144. void LocalZynAddSubFx::setPitchWheelBendRange( int semitones )
  145. {
  146. for( int i = 0; i < NUM_MIDI_PARTS; ++i )
  147. {
  148. m_master->part[i]->ctl.setpitchwheelbendrange( semitones * 100 );
  149. }
  150. }
  151. void LocalZynAddSubFx::processMidiEvent( const MidiEvent& event )
  152. {
  153. switch( event.type() )
  154. {
  155. case MidiNoteOn:
  156. if( event.velocity() > 0 )
  157. {
  158. if( event.key() < 0 || event.key() > MidiMaxKey )
  159. {
  160. break;
  161. }
  162. if( m_runningNotes[event.key()] > 0 )
  163. {
  164. m_master->noteOff( event.channel(), event.key() );
  165. }
  166. ++m_runningNotes[event.key()];
  167. m_master->noteOn( event.channel(), event.key(), event.velocity() );
  168. break;
  169. }
  170. case MidiNoteOff:
  171. if( event.key() < 0 || event.key() > MidiMaxKey )
  172. {
  173. break;
  174. }
  175. if( --m_runningNotes[event.key()] <= 0 )
  176. {
  177. m_master->noteOff( event.channel(), event.key() );
  178. }
  179. break;
  180. case MidiPitchBend:
  181. m_master->setController( event.channel(), C_pitchwheel, event.pitchBend()-8192 );
  182. break;
  183. case MidiControlChange:
  184. m_master->setController( event.channel(), event.controllerNumber(), event.controllerValue() );
  185. break;
  186. default:
  187. break;
  188. }
  189. }
  190. void LocalZynAddSubFx::processAudio( sampleFrame * _out )
  191. {
  192. float outputl[synth->buffersize];
  193. float outputr[synth->buffersize];
  194. m_master->GetAudioOutSamples( synth->buffersize, synth->samplerate, outputl, outputr );
  195. // TODO: move to MixHelpers
  196. for( int f = 0; f < synth->buffersize; ++f )
  197. {
  198. _out[f][0] = outputl[f];
  199. _out[f][1] = outputr[f];
  200. }
  201. }