sf2_player.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * sf2_player.h - a soundfont2 player using fluidSynth
  3. *
  4. * Copyright (c) 2008 Paul Giblock <drfaygo/at/gmail/dot/com>
  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. #ifndef SF2_PLAYER_H
  26. #define SF2_PLAYER_H
  27. #include <QMutex>
  28. #include <samplerate.h>
  29. #include "Instrument.h"
  30. #include "PixmapButton.h"
  31. #include "InstrumentView.h"
  32. #include "Knob.h"
  33. #include "LcdSpinBox.h"
  34. #include "LedCheckbox.h"
  35. #include "fluidsynthshims.h"
  36. #include "MemoryManager.h"
  37. class sf2InstrumentView;
  38. class sf2Font;
  39. class NotePlayHandle;
  40. class patchesDialog;
  41. class QLabel;
  42. struct SF2PluginData;
  43. class sf2Instrument : public Instrument
  44. {
  45. Q_OBJECT
  46. mapPropertyFromModel(int,getBank,setBank,m_bankNum);
  47. mapPropertyFromModel(int,getPatch,setPatch,m_patchNum);
  48. public:
  49. sf2Instrument( InstrumentTrack * _instrument_track );
  50. virtual ~sf2Instrument();
  51. virtual void play( sampleFrame * _working_buffer );
  52. virtual void playNote( NotePlayHandle * _n,
  53. sampleFrame * _working_buffer );
  54. virtual void deleteNotePluginData( NotePlayHandle * _n );
  55. virtual void saveSettings( QDomDocument & _doc, QDomElement & _parent );
  56. virtual void loadSettings( const QDomElement & _this );
  57. virtual void loadFile( const QString & _file );
  58. virtual AutomatableModel * childModel( const QString & _modelName );
  59. virtual QString nodeName() const;
  60. virtual f_cnt_t desiredReleaseFrames() const
  61. {
  62. return 0;
  63. }
  64. virtual Flags flags() const
  65. {
  66. return IsSingleStreamed;
  67. }
  68. virtual PluginView * instantiateView( QWidget * _parent );
  69. QString getCurrentPatchName();
  70. void setParameter( const QString & _param, const QString & _value );
  71. public slots:
  72. void openFile( const QString & _sf2File, bool updateTrackName = true );
  73. void updatePatch();
  74. void reloadSynth();
  75. // We can't really support sample-exact with the way IPH and FS work.
  76. // So, sig/slots work just fine for the synth settings right now.
  77. void updateReverbOn();
  78. void updateReverb();
  79. void updateChorusOn();
  80. void updateChorus();
  81. void updateGain();
  82. private:
  83. static QMutex s_fontsMutex;
  84. static QMap<QString, sf2Font*> s_fonts;
  85. static int (* s_origFree)( fluid_sfont_t * );
  86. SRC_STATE * m_srcState;
  87. fluid_settings_t* m_settings;
  88. fluid_synth_t* m_synth;
  89. sf2Font* m_font;
  90. int m_fontId;
  91. QString m_filename;
  92. // Protect the array of active notes
  93. QMutex m_notesRunningMutex;
  94. // Protect synth when we are re-creating it.
  95. QMutex m_synthMutex;
  96. QMutex m_loadMutex;
  97. int m_notesRunning[128];
  98. sample_rate_t m_internalSampleRate;
  99. int m_lastMidiPitch;
  100. int m_lastMidiPitchRange;
  101. int m_channel;
  102. LcdSpinBoxModel m_bankNum;
  103. LcdSpinBoxModel m_patchNum;
  104. FloatModel m_gain;
  105. BoolModel m_reverbOn;
  106. FloatModel m_reverbRoomSize;
  107. FloatModel m_reverbDamping;
  108. FloatModel m_reverbWidth;
  109. FloatModel m_reverbLevel;
  110. BoolModel m_chorusOn;
  111. FloatModel m_chorusNum;
  112. FloatModel m_chorusLevel;
  113. FloatModel m_chorusSpeed;
  114. FloatModel m_chorusDepth;
  115. QVector<NotePlayHandle *> m_playingNotes;
  116. QMutex m_playingNotesMutex;
  117. private:
  118. void freeFont();
  119. void noteOn( SF2PluginData * n );
  120. void noteOff( SF2PluginData * n );
  121. void renderFrames( f_cnt_t frames, sampleFrame * buf );
  122. friend class sf2InstrumentView;
  123. signals:
  124. void fileLoading();
  125. void fileChanged();
  126. void patchChanged();
  127. } ;
  128. // A soundfont in our font-map
  129. class sf2Font
  130. {
  131. MM_OPERATORS
  132. public:
  133. sf2Font( fluid_sfont_t * f ) :
  134. fluidFont( f ),
  135. refCount( 1 )
  136. {};
  137. fluid_sfont_t * fluidFont;
  138. int refCount;
  139. };
  140. class sf2InstrumentView : public InstrumentViewFixedSize
  141. {
  142. Q_OBJECT
  143. public:
  144. sf2InstrumentView( Instrument * _instrument,
  145. QWidget * _parent );
  146. virtual ~sf2InstrumentView();
  147. private:
  148. virtual void modelChanged();
  149. PixmapButton * m_fileDialogButton;
  150. PixmapButton * m_patchDialogButton;
  151. LcdSpinBox * m_bankNumLcd;
  152. LcdSpinBox * m_patchNumLcd;
  153. QLabel * m_filenameLabel;
  154. QLabel * m_patchLabel;
  155. Knob * m_gainKnob;
  156. PixmapButton * m_reverbButton;
  157. Knob * m_reverbRoomSizeKnob;
  158. Knob * m_reverbDampingKnob;
  159. Knob * m_reverbWidthKnob;
  160. Knob * m_reverbLevelKnob;
  161. PixmapButton * m_chorusButton;
  162. Knob * m_chorusNumKnob;
  163. Knob * m_chorusLevelKnob;
  164. Knob * m_chorusSpeedKnob;
  165. Knob * m_chorusDepthKnob;
  166. static patchesDialog * s_patchDialog;
  167. protected slots:
  168. void invalidateFile();
  169. void showFileDialog();
  170. void showPatchDialog();
  171. void updateFilename();
  172. void updatePatchName();
  173. } ;
  174. #endif