patman.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /*
  2. * patman.cpp - a GUS-compatible patch instrument plugin
  3. *
  4. * Copyright (c) 2007-2008 Javier Serrano Polo <jasp00/at/users.sourceforge.net>
  5. * Copyright (c) 2009-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  6. *
  7. * This file is part of LMMS - https://lmms.io
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public
  20. * License along with this program (see COPYING); if not, write to the
  21. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. * Boston, MA 02110-1301 USA.
  23. *
  24. */
  25. #include "patman.h"
  26. #include <QDragEnterEvent>
  27. #include <QPainter>
  28. #include <QDomElement>
  29. #include "endian_handling.h"
  30. #include "Engine.h"
  31. #include "gui_templates.h"
  32. #include "InstrumentTrack.h"
  33. #include "NotePlayHandle.h"
  34. #include "PixmapButton.h"
  35. #include "Song.h"
  36. #include "StringPairDrag.h"
  37. #include "ToolTip.h"
  38. #include "FileDialog.h"
  39. #include "ConfigManager.h"
  40. #include "embed.cpp"
  41. extern "C"
  42. {
  43. Plugin::Descriptor PLUGIN_EXPORT patman_plugin_descriptor =
  44. {
  45. STRINGIFY( PLUGIN_NAME ),
  46. "PatMan",
  47. QT_TRANSLATE_NOOP( "pluginBrowser",
  48. "GUS-compatible patch instrument" ),
  49. "Javier Serrano Polo <jasp00/at/users.sourceforge.net>",
  50. 0x0100,
  51. Plugin::Instrument,
  52. new PluginPixmapLoader( "logo" ),
  53. "pat",
  54. NULL
  55. } ;
  56. // necessary for getting instance out of shared lib
  57. Plugin * PLUGIN_EXPORT lmms_plugin_main( Model *, void * _data )
  58. {
  59. return new patmanInstrument( static_cast<InstrumentTrack *>( _data ) );
  60. }
  61. }
  62. patmanInstrument::patmanInstrument( InstrumentTrack * _instrument_track ) :
  63. Instrument( _instrument_track, &patman_plugin_descriptor ),
  64. m_patchFile( QString::null ),
  65. m_loopedModel( true, this ),
  66. m_tunedModel( true, this )
  67. {
  68. }
  69. patmanInstrument::~patmanInstrument()
  70. {
  71. unloadCurrentPatch();
  72. }
  73. void patmanInstrument::saveSettings( QDomDocument & _doc, QDomElement & _this )
  74. {
  75. _this.setAttribute( "src", m_patchFile );
  76. m_loopedModel.saveSettings( _doc, _this, "looped" );
  77. m_tunedModel.saveSettings( _doc, _this, "tuned" );
  78. }
  79. void patmanInstrument::loadSettings( const QDomElement & _this )
  80. {
  81. setFile( _this.attribute( "src" ), false );
  82. m_loopedModel.loadSettings( _this, "looped" );
  83. m_tunedModel.loadSettings( _this, "tuned" );
  84. }
  85. void patmanInstrument::loadFile( const QString & _file )
  86. {
  87. setFile( _file );
  88. }
  89. QString patmanInstrument::nodeName( void ) const
  90. {
  91. return( patman_plugin_descriptor.name );
  92. }
  93. void patmanInstrument::playNote( NotePlayHandle * _n,
  94. sampleFrame * _working_buffer )
  95. {
  96. if( m_patchFile == "" )
  97. {
  98. return;
  99. }
  100. const fpp_t frames = _n->framesLeftForCurrentPeriod();
  101. const f_cnt_t offset = _n->noteOffset();
  102. if( !_n->m_pluginData )
  103. {
  104. selectSample( _n );
  105. }
  106. handle_data * hdata = (handle_data *)_n->m_pluginData;
  107. float play_freq = hdata->tuned ? _n->frequency() :
  108. hdata->sample->frequency();
  109. if( hdata->sample->play( _working_buffer + offset, hdata->state, frames,
  110. play_freq, m_loopedModel.value() ? SampleBuffer::LoopOn : SampleBuffer::LoopOff ) )
  111. {
  112. applyRelease( _working_buffer, _n );
  113. instrumentTrack()->processAudioBuffer( _working_buffer,
  114. frames + offset, _n );
  115. }
  116. else
  117. {
  118. memset( _working_buffer, 0, ( frames + offset ) * sizeof( sampleFrame ) );
  119. }
  120. }
  121. void patmanInstrument::deleteNotePluginData( NotePlayHandle * _n )
  122. {
  123. handle_data * hdata = (handle_data *)_n->m_pluginData;
  124. sharedObject::unref( hdata->sample );
  125. delete hdata->state;
  126. delete hdata;
  127. }
  128. void patmanInstrument::setFile( const QString & _patch_file, bool _rename )
  129. {
  130. if( _patch_file.size() <= 0 )
  131. {
  132. m_patchFile = QString::null;
  133. return;
  134. }
  135. // is current instrument-track-name equal to previous-filename??
  136. if( _rename &&
  137. ( instrumentTrack()->name() ==
  138. QFileInfo( m_patchFile ).fileName() ||
  139. m_patchFile == "" ) )
  140. {
  141. // then set it to new one
  142. instrumentTrack()->setName( QFileInfo( _patch_file
  143. ).fileName() );
  144. }
  145. // else we don't touch the instrument-track-name, because the user
  146. // named it self
  147. m_patchFile = SampleBuffer::tryToMakeRelative( _patch_file );
  148. LoadErrors error = loadPatch( SampleBuffer::tryToMakeAbsolute( _patch_file ) );
  149. if( error )
  150. {
  151. printf("Load error\n");
  152. }
  153. emit fileChanged();
  154. }
  155. patmanInstrument::LoadErrors patmanInstrument::loadPatch(
  156. const QString & _filename )
  157. {
  158. unloadCurrentPatch();
  159. FILE * fd = fopen( _filename.toUtf8().constData() , "rb" );
  160. if( !fd )
  161. {
  162. perror( "fopen" );
  163. return( LoadOpen );
  164. }
  165. unsigned char header[239];
  166. if( fread( header, 1, 239, fd ) != 239 ||
  167. ( memcmp( header, "GF1PATCH110\0ID#000002", 22 )
  168. && memcmp( header, "GF1PATCH100\0ID#000002", 22 ) ) )
  169. {
  170. fclose( fd );
  171. return( LoadNotGUS );
  172. }
  173. if( header[82] != 1 && header[82] != 0 )
  174. {
  175. fclose( fd );
  176. return( LoadInstruments );
  177. }
  178. if( header[151] != 1 && header[151] != 0 )
  179. {
  180. fclose( fd );
  181. return( LoadLayers );
  182. }
  183. int sample_count = header[198];
  184. for( int i = 0; i < sample_count; ++i )
  185. {
  186. unsigned short tmpshort;
  187. #define SKIP_BYTES( x ) \
  188. if ( fseek( fd, x, SEEK_CUR ) == -1 ) \
  189. { \
  190. fclose( fd ); \
  191. return( LoadIO ); \
  192. }
  193. #define READ_SHORT( x ) \
  194. if ( fread( &tmpshort, 2, 1, fd ) != 1 ) \
  195. { \
  196. fclose( fd ); \
  197. return( LoadIO ); \
  198. } \
  199. x = (unsigned short)swap16IfBE( tmpshort );
  200. #define READ_LONG( x ) \
  201. if ( fread( &x, 4, 1, fd ) != 1 ) \
  202. { \
  203. fclose( fd ); \
  204. return( LoadIO ); \
  205. } \
  206. x = (unsigned)swap32IfBE( x );
  207. // skip wave name, fractions
  208. SKIP_BYTES( 7 + 1 );
  209. unsigned data_length;
  210. READ_LONG( data_length );
  211. unsigned loop_start;
  212. READ_LONG( loop_start );
  213. unsigned loop_end;
  214. READ_LONG( loop_end );
  215. unsigned sample_rate;
  216. READ_SHORT( sample_rate );
  217. // skip low_freq, high_freq
  218. SKIP_BYTES( 4 + 4 );
  219. unsigned root_freq;
  220. READ_LONG( root_freq );
  221. // skip tuning, panning, envelope, tremolo, vibrato
  222. SKIP_BYTES( 2 + 1 + 12 + 3 + 3 );
  223. unsigned char modes;
  224. if ( fread( &modes, 1, 1, fd ) != 1 )
  225. {
  226. fclose( fd );
  227. return( LoadIO );
  228. }
  229. // skip scale frequency, scale factor, reserved space
  230. SKIP_BYTES( 2 + 2 + 36 );
  231. f_cnt_t frames;
  232. sample_t * wave_samples;
  233. if( modes & MODES_16BIT )
  234. {
  235. frames = data_length >> 1;
  236. wave_samples = new sample_t[frames];
  237. for( f_cnt_t frame = 0; frame < frames; ++frame )
  238. {
  239. short sample;
  240. if ( fread( &sample, 2, 1, fd ) != 1 )
  241. {
  242. delete[] wave_samples;
  243. fclose( fd );
  244. return( LoadIO );
  245. }
  246. sample = swap16IfBE( sample );
  247. if( modes & MODES_UNSIGNED )
  248. {
  249. sample ^= 0x8000;
  250. }
  251. wave_samples[frame] = sample / 32767.0f;
  252. }
  253. loop_start >>= 1;
  254. loop_end >>= 1;
  255. }
  256. else
  257. {
  258. frames = data_length;
  259. wave_samples = new sample_t[frames];
  260. for( f_cnt_t frame = 0; frame < frames; ++frame )
  261. {
  262. char sample;
  263. if ( fread( &sample, 1, 1, fd ) != 1 )
  264. {
  265. delete[] wave_samples;
  266. fclose( fd );
  267. return( LoadIO );
  268. }
  269. if( modes & MODES_UNSIGNED )
  270. {
  271. sample ^= 0x80;
  272. }
  273. wave_samples[frame] = sample / 127.0f;
  274. }
  275. }
  276. sampleFrame * data = new sampleFrame[frames];
  277. for( f_cnt_t frame = 0; frame < frames; ++frame )
  278. {
  279. for( ch_cnt_t chnl = 0; chnl < DEFAULT_CHANNELS;
  280. ++chnl )
  281. {
  282. data[frame][chnl] = wave_samples[frame];
  283. }
  284. }
  285. SampleBuffer* psample = new SampleBuffer( data, frames );
  286. psample->setFrequency( root_freq / 1000.0f );
  287. psample->setSampleRate( sample_rate );
  288. if( modes & MODES_LOOPING )
  289. {
  290. psample->setLoopStartFrame( loop_start );
  291. psample->setLoopEndFrame( loop_end );
  292. }
  293. m_patchSamples.push_back( psample );
  294. delete[] wave_samples;
  295. delete[] data;
  296. }
  297. fclose( fd );
  298. return( LoadOK );
  299. }
  300. void patmanInstrument::unloadCurrentPatch( void )
  301. {
  302. while( !m_patchSamples.empty() )
  303. {
  304. sharedObject::unref( m_patchSamples.back() );
  305. m_patchSamples.pop_back();
  306. }
  307. }
  308. void patmanInstrument::selectSample( NotePlayHandle * _n )
  309. {
  310. const float freq = _n->frequency();
  311. float min_dist = HUGE_VALF;
  312. SampleBuffer* sample = NULL;
  313. for( QVector<SampleBuffer *>::iterator it = m_patchSamples.begin(); it != m_patchSamples.end(); ++it )
  314. {
  315. float patch_freq = ( *it )->frequency();
  316. float dist = freq >= patch_freq ? freq / patch_freq :
  317. patch_freq / freq;
  318. if( dist < min_dist )
  319. {
  320. min_dist = dist;
  321. sample = *it;
  322. }
  323. }
  324. handle_data * hdata = new handle_data;
  325. hdata->tuned = m_tunedModel.value();
  326. if( sample )
  327. {
  328. hdata->sample = sharedObject::ref( sample );
  329. }
  330. else
  331. {
  332. hdata->sample = new SampleBuffer( NULL, 0 );
  333. }
  334. hdata->state = new SampleBuffer::handleState( _n->hasDetuningInfo() );
  335. _n->m_pluginData = hdata;
  336. }
  337. PluginView * patmanInstrument::instantiateView( QWidget * _parent )
  338. {
  339. return( new PatmanView( this, _parent ) );
  340. }
  341. PatmanView::PatmanView( Instrument * _instrument, QWidget * _parent ) :
  342. InstrumentView( _instrument, _parent ),
  343. m_pi( NULL )
  344. {
  345. setAutoFillBackground( true );
  346. QPalette pal;
  347. pal.setBrush( backgroundRole(),
  348. PLUGIN_NAME::getIconPixmap( "artwork" ) );
  349. setPalette( pal );
  350. m_openFileButton = new PixmapButton( this, NULL );
  351. m_openFileButton->setObjectName( "openFileButton" );
  352. m_openFileButton->setCursor( QCursor( Qt::PointingHandCursor ) );
  353. m_openFileButton->move( 227, 86 );
  354. m_openFileButton->setActiveGraphic( PLUGIN_NAME::getIconPixmap(
  355. "select_file_on" ) );
  356. m_openFileButton->setInactiveGraphic( PLUGIN_NAME::getIconPixmap(
  357. "select_file" ) );
  358. connect( m_openFileButton, SIGNAL( clicked() ),
  359. this, SLOT( openFile() ) );
  360. ToolTip::add( m_openFileButton, tr( "Open other patch" ) );
  361. m_openFileButton->setWhatsThis(
  362. tr( "Click here to open another patch-file. Loop and Tune "
  363. "settings are not reset." ) );
  364. m_loopButton = new PixmapButton( this, tr( "Loop" ) );
  365. m_loopButton->setObjectName("loopButton");
  366. m_loopButton->setCheckable( true );
  367. m_loopButton->move( 195, 138 );
  368. m_loopButton->setActiveGraphic( PLUGIN_NAME::getIconPixmap(
  369. "loop_on" ) );
  370. m_loopButton->setInactiveGraphic( PLUGIN_NAME::getIconPixmap(
  371. "loop_off" ) );
  372. ToolTip::add( m_loopButton, tr( "Loop mode" ) );
  373. m_loopButton->setWhatsThis(
  374. tr( "Here you can toggle the Loop mode. If enabled, PatMan "
  375. "will use the loop information available in the "
  376. "file." ) );
  377. m_tuneButton = new PixmapButton( this, tr( "Tune" ) );
  378. m_tuneButton->setObjectName("tuneButton");
  379. m_tuneButton->setCheckable( true );
  380. m_tuneButton->move( 223, 138 );
  381. m_tuneButton->setActiveGraphic( PLUGIN_NAME::getIconPixmap(
  382. "tune_on" ) );
  383. m_tuneButton->setInactiveGraphic( PLUGIN_NAME::getIconPixmap(
  384. "tune_off" ) );
  385. ToolTip::add( m_tuneButton, tr( "Tune mode" ) );
  386. m_tuneButton->setWhatsThis(
  387. tr( "Here you can toggle the Tune mode. If enabled, PatMan "
  388. "will tune the sample to match the note's "
  389. "frequency." ) );
  390. m_displayFilename = tr( "No file selected" );
  391. setAcceptDrops( true );
  392. }
  393. PatmanView::~PatmanView()
  394. {
  395. }
  396. void PatmanView::openFile( void )
  397. {
  398. FileDialog ofd( NULL, tr( "Open patch file" ) );
  399. ofd.setFileMode( FileDialog::ExistingFiles );
  400. QStringList types;
  401. types << tr( "Patch-Files (*.pat)" );
  402. ofd.setNameFilters( types );
  403. if( m_pi->m_patchFile == "" )
  404. {
  405. if( QDir( "/usr/share/midi/freepats" ).exists() )
  406. {
  407. ofd.setDirectory( "/usr/share/midi/freepats" );
  408. }
  409. else
  410. {
  411. ofd.setDirectory(
  412. ConfigManager::inst()->userSamplesDir() );
  413. }
  414. }
  415. else if( QFileInfo( m_pi->m_patchFile ).isRelative() )
  416. {
  417. QString f = ConfigManager::inst()->userSamplesDir()
  418. + m_pi->m_patchFile;
  419. if( QFileInfo( f ).exists() == false )
  420. {
  421. f = ConfigManager::inst()->factorySamplesDir()
  422. + m_pi->m_patchFile;
  423. }
  424. ofd.selectFile( f );
  425. }
  426. else
  427. {
  428. ofd.selectFile( m_pi->m_patchFile );
  429. }
  430. if( ofd.exec() == QDialog::Accepted && !ofd.selectedFiles().isEmpty() )
  431. {
  432. QString f = ofd.selectedFiles()[0];
  433. if( f != "" )
  434. {
  435. m_pi->setFile( f );
  436. Engine::getSong()->setModified();
  437. }
  438. }
  439. }
  440. void PatmanView::updateFilename( void )
  441. {
  442. m_displayFilename = "";
  443. int idx = m_pi->m_patchFile.length();
  444. QFontMetrics fm( pointSize<8>( font() ) );
  445. // simple algorithm for creating a text from the filename that
  446. // matches in the white rectangle
  447. while( idx > 0 && fm.size( Qt::TextSingleLine,
  448. m_displayFilename + "..." ).width() < 225 )
  449. {
  450. m_displayFilename = m_pi->m_patchFile[--idx] +
  451. m_displayFilename;
  452. }
  453. if( idx > 0 )
  454. {
  455. m_displayFilename = "..." + m_displayFilename;
  456. }
  457. update();
  458. }
  459. void PatmanView::dragEnterEvent( QDragEnterEvent * _dee )
  460. {
  461. if( _dee->mimeData()->hasFormat( StringPairDrag::mimeType() ) )
  462. {
  463. QString txt = _dee->mimeData()->data(
  464. StringPairDrag::mimeType() );
  465. if( txt.section( ':', 0, 0 ) == "samplefile" )
  466. {
  467. _dee->acceptProposedAction();
  468. }
  469. else
  470. {
  471. _dee->ignore();
  472. }
  473. }
  474. else
  475. {
  476. _dee->ignore();
  477. }
  478. }
  479. void PatmanView::dropEvent( QDropEvent * _de )
  480. {
  481. QString type = StringPairDrag::decodeKey( _de );
  482. QString value = StringPairDrag::decodeValue( _de );
  483. if( type == "samplefile" )
  484. {
  485. m_pi->setFile( value );
  486. _de->accept();
  487. return;
  488. }
  489. _de->ignore();
  490. }
  491. void PatmanView::paintEvent( QPaintEvent * )
  492. {
  493. QPainter p( this );
  494. p.setFont( pointSize<8>( font() ) );
  495. p.drawText( 8, 116, 235, 16,
  496. Qt::AlignLeft | Qt::TextSingleLine | Qt::AlignVCenter,
  497. m_displayFilename );
  498. }
  499. void PatmanView::modelChanged( void )
  500. {
  501. m_pi = castModel<patmanInstrument>();
  502. m_loopButton->setModel( &m_pi->m_loopedModel );
  503. m_tuneButton->setModel( &m_pi->m_tunedModel );
  504. connect( m_pi, SIGNAL( fileChanged() ),
  505. this, SLOT( updateFilename() ) );
  506. }