FxMixer.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * FxMixer.h - effect-mixer for LMMS
  3. *
  4. * Copyright (c) 2008-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 FX_MIXER_H
  25. #define FX_MIXER_H
  26. #include "Model.h"
  27. #include "EffectChain.h"
  28. #include "JournallingObject.h"
  29. #include "ThreadableJob.h"
  30. #include <atomic>
  31. class FxRoute;
  32. typedef QVector<FxRoute *> FxRouteVector;
  33. class FxChannel : public ThreadableJob
  34. {
  35. public:
  36. FxChannel( int idx, Model * _parent );
  37. virtual ~FxChannel();
  38. EffectChain m_fxChain;
  39. // set to true when input fed from mixToChannel or child channel
  40. bool m_hasInput;
  41. // set to true if any effect in the channel is enabled and running
  42. bool m_stillRunning;
  43. float m_peakLeft;
  44. float m_peakRight;
  45. sampleFrame * m_buffer;
  46. bool m_muteBeforeSolo;
  47. BoolModel m_muteModel;
  48. BoolModel m_soloModel;
  49. FloatModel m_volumeModel;
  50. QString m_name;
  51. QMutex m_lock;
  52. int m_channelIndex; // what channel index are we
  53. bool m_queued; // are we queued up for rendering yet?
  54. bool m_muted; // are we muted? updated per period so we don't have to call m_muteModel.value() twice
  55. // pointers to other channels that this one sends to
  56. FxRouteVector m_sends;
  57. // pointers to other channels that send to this one
  58. FxRouteVector m_receives;
  59. bool requiresProcessing() const override { return true; }
  60. void unmuteForSolo();
  61. std::atomic_int m_dependenciesMet;
  62. void incrementDeps();
  63. void processed();
  64. private:
  65. void doProcessing() override;
  66. };
  67. class FxRoute : public QObject
  68. {
  69. Q_OBJECT
  70. public:
  71. FxRoute( FxChannel * from, FxChannel * to, float amount );
  72. virtual ~FxRoute();
  73. fx_ch_t senderIndex() const
  74. {
  75. return m_from->m_channelIndex;
  76. }
  77. fx_ch_t receiverIndex() const
  78. {
  79. return m_to->m_channelIndex;
  80. }
  81. FloatModel * amount()
  82. {
  83. return &m_amount;
  84. }
  85. FxChannel * sender() const
  86. {
  87. return m_from;
  88. }
  89. FxChannel * receiver() const
  90. {
  91. return m_to;
  92. }
  93. void updateName();
  94. private:
  95. FxChannel * m_from;
  96. FxChannel * m_to;
  97. FloatModel m_amount;
  98. };
  99. class LMMS_EXPORT FxMixer : public Model, public JournallingObject
  100. {
  101. Q_OBJECT
  102. public:
  103. FxMixer();
  104. virtual ~FxMixer();
  105. void mixToChannel( const sampleFrame * _buf, fx_ch_t _ch );
  106. void prepareMasterMix();
  107. void masterMix( sampleFrame * _buf );
  108. void saveSettings( QDomDocument & _doc, QDomElement & _parent ) override;
  109. void loadSettings( const QDomElement & _this ) override;
  110. QString nodeName() const override
  111. {
  112. return "fxmixer";
  113. }
  114. FxChannel * effectChannel( int _ch )
  115. {
  116. return m_fxChannels[_ch];
  117. }
  118. // make the output of channel fromChannel go to the input of channel toChannel
  119. // it is safe to call even if the send already exists
  120. FxRoute * createChannelSend(fx_ch_t fromChannel, fx_ch_t toChannel,
  121. float amount = 1.0f);
  122. FxRoute * createRoute( FxChannel * from, FxChannel * to, float amount );
  123. // delete the connection made by createChannelSend
  124. void deleteChannelSend(fx_ch_t fromChannel, fx_ch_t toChannel);
  125. void deleteChannelSend( FxRoute * route );
  126. // determine if adding a send from sendFrom to
  127. // sendTo would result in an infinite mixer loop.
  128. bool isInfiniteLoop(fx_ch_t fromChannel, fx_ch_t toChannel);
  129. bool checkInfiniteLoop( FxChannel * from, FxChannel * to );
  130. // return the FloatModel of fromChannel sending its output to the input of
  131. // toChannel. NULL if there is no send.
  132. FloatModel * channelSendModel(fx_ch_t fromChannel, fx_ch_t toChannel);
  133. // add a new channel to the Fx Mixer.
  134. // returns the index of the channel that was just added
  135. int createChannel();
  136. // delete a channel from the FX mixer.
  137. void deleteChannel(int index);
  138. // delete all the mixer channels except master and remove all effects
  139. void clear();
  140. // re-arrange channels
  141. void moveChannelLeft(int index);
  142. void moveChannelRight(int index);
  143. // reset a channel's name, fx, sends, etc
  144. void clearChannel(fx_ch_t channelIndex);
  145. // rename channels when moving etc. if they still have their original name
  146. void validateChannelName( int index, int oldIndex );
  147. void toggledSolo();
  148. void activateSolo();
  149. void deactivateSolo();
  150. inline fx_ch_t numChannels() const
  151. {
  152. return m_fxChannels.size();
  153. }
  154. FxRouteVector m_fxRoutes;
  155. private:
  156. // the fx channels in the mixer. index 0 is always master.
  157. QVector<FxChannel *> m_fxChannels;
  158. // make sure we have at least num channels
  159. void allocateChannelsTo(int num);
  160. int m_lastSoloed;
  161. } ;
  162. #endif