Mixer.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 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. void setAudioDevice( AudioDevice * _dev );
  145. void setAudioDevice( AudioDevice * _dev,
  146. const struct qualitySettings & _qs,
  147. bool _needs_fifo );
  148. void restoreAudioDevice();
  149. inline AudioDevice * audioDev()
  150. {
  151. return m_audioDev;
  152. }
  153. // audio-port-stuff
  154. inline void addAudioPort( AudioPort * _port )
  155. {
  156. requestChangeInModel();
  157. m_audioPorts.push_back( _port );
  158. doneChangeInModel();
  159. }
  160. void removeAudioPort( AudioPort * _port );
  161. // MIDI-client-stuff
  162. inline const QString & midiClientName() const
  163. {
  164. return m_midiClientName;
  165. }
  166. inline MidiClient * midiClient()
  167. {
  168. return m_midiClient;
  169. }
  170. // play-handle stuff
  171. bool addPlayHandle( PlayHandle* handle );
  172. void removePlayHandle( PlayHandle* handle );
  173. inline PlayHandleList& playHandles()
  174. {
  175. return m_playHandles;
  176. }
  177. void removePlayHandlesOfTypes( Track * _track, const quint8 types );
  178. // methods providing information for other classes
  179. inline fpp_t framesPerPeriod() const
  180. {
  181. return m_framesPerPeriod;
  182. }
  183. MixerProfiler& profiler()
  184. {
  185. return m_profiler;
  186. }
  187. int cpuLoad() const
  188. {
  189. return m_profiler.cpuLoad();
  190. }
  191. const qualitySettings & currentQualitySettings() const
  192. {
  193. return m_qualitySettings;
  194. }
  195. sample_rate_t baseSampleRate() const;
  196. sample_rate_t outputSampleRate() const;
  197. sample_rate_t inputSampleRate() const;
  198. sample_rate_t processingSampleRate() const;
  199. inline float masterGain() const
  200. {
  201. return m_masterGain;
  202. }
  203. inline void setMasterGain( const float _mo )
  204. {
  205. m_masterGain = _mo;
  206. }
  207. static inline sample_t clip( const sample_t _s )
  208. {
  209. if( _s > 1.0f )
  210. {
  211. return 1.0f;
  212. }
  213. else if( _s < -1.0f )
  214. {
  215. return -1.0f;
  216. }
  217. return _s;
  218. }
  219. void getPeakValues( sampleFrame * _ab, const f_cnt_t _frames, float & peakLeft, float & peakRight ) const;
  220. bool criticalXRuns() const;
  221. inline bool hasFifoWriter() const
  222. {
  223. return m_fifoWriter != NULL;
  224. }
  225. void pushInputFrames( sampleFrame * _ab, const f_cnt_t _frames );
  226. inline const sampleFrame * inputBuffer()
  227. {
  228. return m_inputBuffer[ m_inputBufferRead ];
  229. }
  230. inline f_cnt_t inputBufferFrames() const
  231. {
  232. return m_inputBufferFrames[ m_inputBufferRead ];
  233. }
  234. inline const surroundSampleFrame * nextBuffer()
  235. {
  236. return hasFifoWriter() ? m_fifo->read() : renderNextBuffer();
  237. }
  238. void changeQuality( const struct qualitySettings & _qs );
  239. inline bool isMetronomeActive() const { return m_metronomeActive; }
  240. inline void setMetronomeActive(bool value = true) { m_metronomeActive = value; }
  241. void requestChangeInModel();
  242. void doneChangeInModel();
  243. signals:
  244. void qualitySettingsChanged();
  245. void sampleRateChanged();
  246. void nextAudioBuffer( const surroundSampleFrame * buffer );
  247. private:
  248. typedef fifoBuffer<surroundSampleFrame *> fifo;
  249. class fifoWriter : public QThread
  250. {
  251. public:
  252. fifoWriter( Mixer * _mixer, fifo * _fifo );
  253. void finish();
  254. private:
  255. Mixer * m_mixer;
  256. fifo * m_fifo;
  257. volatile bool m_writing;
  258. virtual void run();
  259. void write( surroundSampleFrame * buffer );
  260. } ;
  261. Mixer( bool renderOnly );
  262. virtual ~Mixer();
  263. void startProcessing( bool _needs_fifo = true );
  264. void stopProcessing();
  265. AudioDevice * tryAudioDevices();
  266. MidiClient * tryMidiClients();
  267. const surroundSampleFrame * renderNextBuffer();
  268. void clearInternal();
  269. void runChangesInModel();
  270. bool m_renderOnly;
  271. QVector<AudioPort *> m_audioPorts;
  272. fpp_t m_framesPerPeriod;
  273. sampleFrame * m_inputBuffer[2];
  274. f_cnt_t m_inputBufferFrames[2];
  275. f_cnt_t m_inputBufferSize[2];
  276. int m_inputBufferRead;
  277. int m_inputBufferWrite;
  278. surroundSampleFrame * m_readBuf;
  279. surroundSampleFrame * m_writeBuf;
  280. QVector<surroundSampleFrame *> m_bufferPool;
  281. int m_readBuffer;
  282. int m_writeBuffer;
  283. int m_poolDepth;
  284. // worker thread stuff
  285. QVector<MixerWorkerThread *> m_workers;
  286. int m_numWorkers;
  287. // playhandle stuff
  288. PlayHandleList m_playHandles;
  289. // place where new playhandles are added temporarily
  290. LocklessList<PlayHandle *> m_newPlayHandles;
  291. ConstPlayHandleList m_playHandlesToRemove;
  292. struct qualitySettings m_qualitySettings;
  293. float m_masterGain;
  294. bool m_isProcessing;
  295. // audio device stuff
  296. AudioDevice * m_audioDev;
  297. AudioDevice * m_oldAudioDev;
  298. QString m_audioDevName;
  299. bool m_audioDevStartFailed;
  300. // MIDI device stuff
  301. MidiClient * m_midiClient;
  302. QString m_midiClientName;
  303. // FIFO stuff
  304. fifo * m_fifo;
  305. fifoWriter * m_fifoWriter;
  306. MixerProfiler m_profiler;
  307. bool m_metronomeActive;
  308. bool m_clearSignal;
  309. bool m_changesSignal;
  310. unsigned int m_changes;
  311. QMutex m_changesMutex;
  312. QMutex m_doChangesMutex;
  313. QMutex m_waitChangesMutex;
  314. QWaitCondition m_changesMixerCondition;
  315. QWaitCondition m_changesRequestCondition;
  316. bool m_waitingForWrite;
  317. friend class LmmsCore;
  318. friend class MixerWorkerThread;
  319. friend class ProjectRenderer;
  320. } ;
  321. #endif