Mixer.h 5.3 KB

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