Mixer.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * Mixer.h - audio-device-independent mixer for LMMS
  3. *
  4. * Copyright (c) 2004-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. #ifndef MIXER_H
  25. #define MIXER_H
  26. #include <QtCore/QMutex>
  27. #include <QtCore/QThread>
  28. #include <QtCore/QVector>
  29. #include <QtCore/QWaitCondition>
  30. #include <samplerate.h>
  31. #include "lmms_basics.h"
  32. #include "LocklessList.h"
  33. #include "Note.h"
  34. #include "fifo_buffer.h"
  35. #include "MixerProfiler.h"
  36. class AudioDevice;
  37. class MidiClient;
  38. class AudioPort;
  39. const fpp_t MINIMUM_BUFFER_SIZE = 32;
  40. const fpp_t DEFAULT_BUFFER_SIZE = 256;
  41. const int BYTES_PER_SAMPLE = sizeof( sample_t );
  42. const int BYTES_PER_INT_SAMPLE = sizeof( int_sample_t );
  43. const int BYTES_PER_FRAME = sizeof( sampleFrame );
  44. const int BYTES_PER_SURROUND_FRAME = sizeof( surroundSampleFrame );
  45. const float OUTPUT_SAMPLE_MULTIPLIER = 32767.0f;
  46. const float BaseFreq = 440.0f;
  47. const Keys BaseKey = Key_A;
  48. const Octaves BaseOctave = DefaultOctave;
  49. #include "PlayHandle.h"
  50. class MixerWorkerThread;
  51. class LMMS_EXPORT Mixer : public QObject
  52. {
  53. Q_OBJECT
  54. public:
  55. struct qualitySettings
  56. {
  57. enum Mode
  58. {
  59. Mode_Draft,
  60. Mode_HighQuality,
  61. Mode_FinalMix
  62. } ;
  63. enum Interpolation
  64. {
  65. Interpolation_Linear,
  66. Interpolation_SincFastest,
  67. Interpolation_SincMedium,
  68. Interpolation_SincBest
  69. } ;
  70. enum Oversampling
  71. {
  72. Oversampling_None,
  73. Oversampling_2x,
  74. Oversampling_4x,
  75. Oversampling_8x
  76. } ;
  77. Interpolation interpolation;
  78. Oversampling oversampling;
  79. qualitySettings( Mode _m )
  80. {
  81. switch( _m )
  82. {
  83. case Mode_Draft:
  84. interpolation = Interpolation_Linear;
  85. oversampling = Oversampling_None;
  86. break;
  87. case Mode_HighQuality:
  88. interpolation =
  89. Interpolation_SincFastest;
  90. oversampling = Oversampling_2x;
  91. break;
  92. case Mode_FinalMix:
  93. interpolation = Interpolation_SincBest;
  94. oversampling = Oversampling_8x;
  95. break;
  96. }
  97. }
  98. qualitySettings( Interpolation _i, Oversampling _o ) :
  99. interpolation( _i ),
  100. oversampling( _o )
  101. {
  102. }
  103. int sampleRateMultiplier() const
  104. {
  105. switch( oversampling )
  106. {
  107. case Oversampling_None: return 1;
  108. case Oversampling_2x: return 2;
  109. case Oversampling_4x: return 4;
  110. case Oversampling_8x: return 8;
  111. }
  112. return 1;
  113. }
  114. int libsrcInterpolation() const
  115. {
  116. switch( interpolation )
  117. {
  118. case Interpolation_Linear:
  119. return SRC_ZERO_ORDER_HOLD;
  120. case Interpolation_SincFastest:
  121. return SRC_SINC_FASTEST;
  122. case Interpolation_SincMedium:
  123. return SRC_SINC_MEDIUM_QUALITY;
  124. case Interpolation_SincBest:
  125. return SRC_SINC_BEST_QUALITY;
  126. }
  127. return SRC_LINEAR;
  128. }
  129. } ;
  130. void initDevices();
  131. void clear();
  132. void clearNewPlayHandles();
  133. // audio-device-stuff
  134. // Returns the current audio device's name. This is not necessarily
  135. // the user's preferred audio device, in case you were thinking that.
  136. inline const QString & audioDevName() const
  137. {
  138. return m_audioDevName;
  139. }
  140. inline bool audioDevStartFailed() const
  141. {
  142. return m_audioDevStartFailed;
  143. }
  144. //! Set new audio device. Old device will be deleted,
  145. //! unless it's stored using storeAudioDevice
  146. void setAudioDevice( AudioDevice * _dev , bool startNow );
  147. //! See overloaded function
  148. void setAudioDevice( AudioDevice * _dev,
  149. const struct qualitySettings & _qs,
  150. bool _needs_fifo,
  151. bool startNow );
  152. void storeAudioDevice();
  153. void restoreAudioDevice();
  154. inline AudioDevice * audioDev()
  155. {
  156. return m_audioDev;
  157. }
  158. // audio-port-stuff
  159. inline void addAudioPort( AudioPort * _port )
  160. {
  161. requestChangeInModel();
  162. m_audioPorts.push_back( _port );
  163. doneChangeInModel();
  164. }
  165. void removeAudioPort( AudioPort * _port );
  166. // MIDI-client-stuff
  167. inline const QString & midiClientName() const
  168. {
  169. return m_midiClientName;
  170. }
  171. inline MidiClient * midiClient()
  172. {
  173. return m_midiClient;
  174. }
  175. // play-handle stuff
  176. bool addPlayHandle( PlayHandle* handle );
  177. void removePlayHandle( PlayHandle* handle );
  178. inline PlayHandleList& playHandles()
  179. {
  180. return m_playHandles;
  181. }
  182. void removePlayHandlesOfTypes( Track * _track, const quint8 types );
  183. // methods providing information for other classes
  184. inline fpp_t framesPerPeriod() const
  185. {
  186. return m_framesPerPeriod;
  187. }
  188. MixerProfiler& profiler()
  189. {
  190. return m_profiler;
  191. }
  192. int cpuLoad() const
  193. {
  194. return m_profiler.cpuLoad();
  195. }
  196. const qualitySettings & currentQualitySettings() const
  197. {
  198. return m_qualitySettings;
  199. }
  200. sample_rate_t baseSampleRate() const;
  201. sample_rate_t outputSampleRate() const;
  202. sample_rate_t inputSampleRate() const;
  203. sample_rate_t processingSampleRate() const;
  204. inline float masterGain() const
  205. {
  206. return m_masterGain;
  207. }
  208. inline void setMasterGain( const float _mo )
  209. {
  210. m_masterGain = _mo;
  211. }
  212. static inline sample_t clip( const sample_t _s )
  213. {
  214. if( _s > 1.0f )
  215. {
  216. return 1.0f;
  217. }
  218. else if( _s < -1.0f )
  219. {
  220. return -1.0f;
  221. }
  222. return _s;
  223. }
  224. struct StereoSample
  225. {
  226. StereoSample(sample_t _left, sample_t _right) : left(_left), right(_right) {}
  227. sample_t left;
  228. sample_t right;
  229. };
  230. StereoSample getPeakValues(sampleFrame * _ab, const f_cnt_t _frames) const;
  231. bool criticalXRuns() const;
  232. inline bool hasFifoWriter() const
  233. {
  234. return m_fifoWriter != NULL;
  235. }
  236. void pushInputFrames( sampleFrame * _ab, const f_cnt_t _frames );
  237. inline const sampleFrame * inputBuffer()
  238. {
  239. return m_inputBuffer[ m_inputBufferRead ];
  240. }
  241. inline f_cnt_t inputBufferFrames() const
  242. {
  243. return m_inputBufferFrames[ m_inputBufferRead ];
  244. }
  245. inline const surroundSampleFrame * nextBuffer()
  246. {
  247. return hasFifoWriter() ? m_fifo->read() : renderNextBuffer();
  248. }
  249. void changeQuality( const struct qualitySettings & _qs );
  250. inline bool isMetronomeActive() const { return m_metronomeActive; }
  251. inline void setMetronomeActive(bool value = true) { m_metronomeActive = value; }
  252. //! Block until a change in model can be done (i.e. wait for audio thread)
  253. void requestChangeInModel();
  254. void doneChangeInModel();
  255. static bool isAudioDevNameValid(QString name);
  256. static bool isMidiDevNameValid(QString name);
  257. signals:
  258. void qualitySettingsChanged();
  259. void sampleRateChanged();
  260. void nextAudioBuffer( const surroundSampleFrame * buffer );
  261. private:
  262. typedef fifoBuffer<surroundSampleFrame *> fifo;
  263. class fifoWriter : public QThread
  264. {
  265. public:
  266. fifoWriter( Mixer * _mixer, fifo * _fifo );
  267. void finish();
  268. private:
  269. Mixer * m_mixer;
  270. fifo * m_fifo;
  271. volatile bool m_writing;
  272. void run() override;
  273. void write( surroundSampleFrame * buffer );
  274. } ;
  275. Mixer( bool renderOnly );
  276. virtual ~Mixer();
  277. void startProcessing( bool _needs_fifo = true );
  278. void stopProcessing();
  279. AudioDevice * tryAudioDevices();
  280. MidiClient * tryMidiClients();
  281. const surroundSampleFrame * renderNextBuffer();
  282. void clearInternal();
  283. //! Called by the audio thread to give control to other threads,
  284. //! such that they can do changes in the model (like e.g. removing effects)
  285. void runChangesInModel();
  286. bool m_renderOnly;
  287. QVector<AudioPort *> m_audioPorts;
  288. fpp_t m_framesPerPeriod;
  289. sampleFrame * m_inputBuffer[2];
  290. f_cnt_t m_inputBufferFrames[2];
  291. f_cnt_t m_inputBufferSize[2];
  292. int m_inputBufferRead;
  293. int m_inputBufferWrite;
  294. surroundSampleFrame * m_readBuf;
  295. surroundSampleFrame * m_writeBuf;
  296. QVector<surroundSampleFrame *> m_bufferPool;
  297. int m_readBuffer;
  298. int m_writeBuffer;
  299. int m_poolDepth;
  300. // worker thread stuff
  301. QVector<MixerWorkerThread *> m_workers;
  302. int m_numWorkers;
  303. // playhandle stuff
  304. PlayHandleList m_playHandles;
  305. // place where new playhandles are added temporarily
  306. LocklessList<PlayHandle *> m_newPlayHandles;
  307. ConstPlayHandleList m_playHandlesToRemove;
  308. struct qualitySettings m_qualitySettings;
  309. float m_masterGain;
  310. bool m_isProcessing;
  311. // audio device stuff
  312. void doSetAudioDevice( AudioDevice *_dev );
  313. AudioDevice * m_audioDev;
  314. AudioDevice * m_oldAudioDev;
  315. QString m_audioDevName;
  316. bool m_audioDevStartFailed;
  317. // MIDI device stuff
  318. MidiClient * m_midiClient;
  319. QString m_midiClientName;
  320. // FIFO stuff
  321. fifo * m_fifo;
  322. fifoWriter * m_fifoWriter;
  323. MixerProfiler m_profiler;
  324. bool m_metronomeActive;
  325. bool m_clearSignal;
  326. bool m_changesSignal;
  327. unsigned int m_changes;
  328. QMutex m_changesMutex;
  329. QMutex m_doChangesMutex;
  330. QMutex m_waitChangesMutex;
  331. QWaitCondition m_changesMixerCondition;
  332. QWaitCondition m_changesRequestCondition;
  333. bool m_waitingForWrite;
  334. friend class LmmsCore;
  335. friend class MixerWorkerThread;
  336. friend class ProjectRenderer;
  337. } ;
  338. #endif