AudioEngine.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * AudioEngine.h - device-independent audio engine 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 AUDIO_ENGINE_H
  25. #define AUDIO_ENGINE_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 "FifoBuffer.h"
  35. #include "AudioEngineProfiler.h"
  36. #include "PlayHandle.h"
  37. class AudioDevice;
  38. class MidiClient;
  39. class AudioPort;
  40. const fpp_t MINIMUM_BUFFER_SIZE = 32;
  41. const fpp_t DEFAULT_BUFFER_SIZE = 256;
  42. const int BYTES_PER_SAMPLE = sizeof( sample_t );
  43. const int BYTES_PER_INT_SAMPLE = sizeof( int_sample_t );
  44. const int BYTES_PER_FRAME = sizeof( sampleFrame );
  45. const int BYTES_PER_SURROUND_FRAME = sizeof( surroundSampleFrame );
  46. const float OUTPUT_SAMPLE_MULTIPLIER = 32767.0f;
  47. class AudioEngineWorkerThread;
  48. class LMMS_EXPORT AudioEngine : public QObject
  49. {
  50. Q_OBJECT
  51. public:
  52. struct qualitySettings
  53. {
  54. enum Mode
  55. {
  56. Mode_Draft,
  57. Mode_HighQuality,
  58. Mode_FinalMix
  59. } ;
  60. enum Interpolation
  61. {
  62. Interpolation_Linear,
  63. Interpolation_SincFastest,
  64. Interpolation_SincMedium,
  65. Interpolation_SincBest
  66. } ;
  67. enum Oversampling
  68. {
  69. Oversampling_None,
  70. Oversampling_2x,
  71. Oversampling_4x,
  72. Oversampling_8x
  73. } ;
  74. Interpolation interpolation;
  75. Oversampling oversampling;
  76. qualitySettings(Mode m)
  77. {
  78. switch (m)
  79. {
  80. case Mode_Draft:
  81. interpolation = Interpolation_Linear;
  82. oversampling = Oversampling_None;
  83. break;
  84. case Mode_HighQuality:
  85. interpolation =
  86. Interpolation_SincFastest;
  87. oversampling = Oversampling_2x;
  88. break;
  89. case Mode_FinalMix:
  90. interpolation = Interpolation_SincBest;
  91. oversampling = Oversampling_8x;
  92. break;
  93. }
  94. }
  95. qualitySettings(Interpolation i, Oversampling o) :
  96. interpolation(i),
  97. oversampling(o)
  98. {
  99. }
  100. int sampleRateMultiplier() const
  101. {
  102. switch( oversampling )
  103. {
  104. case Oversampling_None: return 1;
  105. case Oversampling_2x: return 2;
  106. case Oversampling_4x: return 4;
  107. case Oversampling_8x: return 8;
  108. }
  109. return 1;
  110. }
  111. int libsrcInterpolation() const
  112. {
  113. switch( interpolation )
  114. {
  115. case Interpolation_Linear:
  116. return SRC_ZERO_ORDER_HOLD;
  117. case Interpolation_SincFastest:
  118. return SRC_SINC_FASTEST;
  119. case Interpolation_SincMedium:
  120. return SRC_SINC_MEDIUM_QUALITY;
  121. case Interpolation_SincBest:
  122. return SRC_SINC_BEST_QUALITY;
  123. }
  124. return SRC_LINEAR;
  125. }
  126. } ;
  127. void initDevices();
  128. void clear();
  129. void clearNewPlayHandles();
  130. // audio-device-stuff
  131. // Returns the current audio device's name. This is not necessarily
  132. // the user's preferred audio device, in case you were thinking that.
  133. inline const QString & audioDevName() const
  134. {
  135. return m_audioDevName;
  136. }
  137. inline bool audioDevStartFailed() const
  138. {
  139. return m_audioDevStartFailed;
  140. }
  141. //! Set new audio device. Old device will be deleted,
  142. //! unless it's stored using storeAudioDevice
  143. void setAudioDevice( AudioDevice * _dev,
  144. const struct qualitySettings & _qs,
  145. bool _needs_fifo,
  146. bool startNow );
  147. void storeAudioDevice();
  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. AudioEngineProfiler& 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. struct StereoSample
  220. {
  221. StereoSample(sample_t _left, sample_t _right) : left(_left), right(_right) {}
  222. sample_t left;
  223. sample_t right;
  224. };
  225. StereoSample getPeakValues(sampleFrame * ab, const f_cnt_t _frames) const;
  226. bool criticalXRuns() const;
  227. inline bool hasFifoWriter() const
  228. {
  229. return m_fifoWriter != nullptr;
  230. }
  231. void pushInputFrames( sampleFrame * _ab, const f_cnt_t _frames );
  232. inline const sampleFrame * inputBuffer()
  233. {
  234. return m_inputBuffer[ m_inputBufferRead ];
  235. }
  236. inline f_cnt_t inputBufferFrames() const
  237. {
  238. return m_inputBufferFrames[ m_inputBufferRead ];
  239. }
  240. inline const surroundSampleFrame * nextBuffer()
  241. {
  242. return hasFifoWriter() ? m_fifo->read() : renderNextBuffer();
  243. }
  244. void changeQuality(const struct qualitySettings & qs);
  245. inline bool isMetronomeActive() const { return m_metronomeActive; }
  246. inline void setMetronomeActive(bool value = true) { m_metronomeActive = value; }
  247. //! Block until a change in model can be done (i.e. wait for audio thread)
  248. void requestChangeInModel();
  249. void doneChangeInModel();
  250. static bool isAudioDevNameValid(QString name);
  251. static bool isMidiDevNameValid(QString name);
  252. signals:
  253. void qualitySettingsChanged();
  254. void sampleRateChanged();
  255. void nextAudioBuffer( const surroundSampleFrame * buffer );
  256. private:
  257. typedef FifoBuffer<surroundSampleFrame *> Fifo;
  258. class fifoWriter : public QThread
  259. {
  260. public:
  261. fifoWriter( AudioEngine * audioEngine, Fifo * fifo );
  262. void finish();
  263. private:
  264. AudioEngine * m_audioEngine;
  265. Fifo * m_fifo;
  266. volatile bool m_writing;
  267. void run() override;
  268. void write( surroundSampleFrame * buffer );
  269. } ;
  270. AudioEngine( bool renderOnly );
  271. virtual ~AudioEngine();
  272. void startProcessing(bool needsFifo = true);
  273. void stopProcessing();
  274. AudioDevice * tryAudioDevices();
  275. MidiClient * tryMidiClients();
  276. const surroundSampleFrame * renderNextBuffer();
  277. void swapBuffers();
  278. void handleMetronome();
  279. void clearInternal();
  280. //! Called by the audio thread to give control to other threads,
  281. //! such that they can do changes in the model (like e.g. removing effects)
  282. void runChangesInModel();
  283. bool m_renderOnly;
  284. QVector<AudioPort *> m_audioPorts;
  285. fpp_t m_framesPerPeriod;
  286. sampleFrame * m_inputBuffer[2];
  287. f_cnt_t m_inputBufferFrames[2];
  288. f_cnt_t m_inputBufferSize[2];
  289. int m_inputBufferRead;
  290. int m_inputBufferWrite;
  291. surroundSampleFrame * m_outputBufferRead;
  292. surroundSampleFrame * m_outputBufferWrite;
  293. // worker thread stuff
  294. QVector<AudioEngineWorkerThread *> m_workers;
  295. int m_numWorkers;
  296. // playhandle stuff
  297. PlayHandleList m_playHandles;
  298. // place where new playhandles are added temporarily
  299. LocklessList<PlayHandle *> m_newPlayHandles;
  300. ConstPlayHandleList m_playHandlesToRemove;
  301. struct qualitySettings m_qualitySettings;
  302. float m_masterGain;
  303. bool m_isProcessing;
  304. // audio device stuff
  305. void doSetAudioDevice( AudioDevice *_dev );
  306. AudioDevice * m_audioDev;
  307. AudioDevice * m_oldAudioDev;
  308. QString m_audioDevName;
  309. bool m_audioDevStartFailed;
  310. // MIDI device stuff
  311. MidiClient * m_midiClient;
  312. QString m_midiClientName;
  313. // FIFO stuff
  314. Fifo * m_fifo;
  315. fifoWriter * m_fifoWriter;
  316. AudioEngineProfiler m_profiler;
  317. bool m_metronomeActive;
  318. bool m_clearSignal;
  319. bool m_changesSignal;
  320. unsigned int m_changes;
  321. QMutex m_changesMutex;
  322. QMutex m_doChangesMutex;
  323. QMutex m_waitChangesMutex;
  324. QWaitCondition m_changesAudioEngineCondition;
  325. QWaitCondition m_changesRequestCondition;
  326. bool m_waitingForWrite;
  327. friend class LmmsCore;
  328. friend class AudioEngineWorkerThread;
  329. friend class ProjectRenderer;
  330. } ;
  331. #endif