1 #include "engine/channels/enginemicrophone.h"
2 
3 #include <QtDebug>
4 
5 #include "control/control.h"
6 #include "control/controlaudiotaperpot.h"
7 #include "effects/effectsmanager.h"
8 #include "engine/effects/engineeffectsmanager.h"
9 #include "moc_enginemicrophone.cpp"
10 #include "preferences/usersettings.h"
11 #include "util/sample.h"
12 
EngineMicrophone(const ChannelHandleAndGroup & handle_group,EffectsManager * pEffectsManager)13 EngineMicrophone::EngineMicrophone(const ChannelHandleAndGroup& handle_group,
14         EffectsManager* pEffectsManager)
15         : EngineChannel(handle_group, EngineChannel::CENTER, pEffectsManager,
16                   /*isTalkoverChannel*/ true,
17                   /*isPrimaryDeck*/ false),
18           m_pInputConfigured(new ControlObject(ConfigKey(getGroup(), "input_configured"))),
19           m_pPregain(new ControlAudioTaperPot(ConfigKey(getGroup(), "pregain"), -12, 12, 0.5)),
20           m_wasActive(false) {
21     // Make input_configured read-only.
22     m_pInputConfigured->setReadOnly();
23     ControlDoublePrivate::insertAlias(ConfigKey(getGroup(), "enabled"),
24                                       ConfigKey(getGroup(), "input_configured"));
25 
26     setMaster(false); // Use "talkover" button to enable microphones
27 }
28 
~EngineMicrophone()29 EngineMicrophone::~EngineMicrophone() {
30     delete m_pPregain;
31 }
32 
isActive()33 bool EngineMicrophone::isActive() {
34     bool enabled = m_pInputConfigured->toBool();
35     if (enabled && m_sampleBuffer) {
36         m_wasActive = true;
37     } else if (m_wasActive) {
38         m_vuMeter.reset();
39         m_wasActive = false;
40     }
41     return m_wasActive;
42 }
43 
onInputConfigured(const AudioInput & input)44 void EngineMicrophone::onInputConfigured(const AudioInput& input) {
45     if (input.getType() != AudioPath::MICROPHONE) {
46         // This is an error!
47         qWarning() << "EngineMicrophone connected to AudioInput for a non-Microphone type!";
48         return;
49     }
50     m_sampleBuffer = nullptr;
51     m_pInputConfigured->forceSet(1.0);
52 }
53 
onInputUnconfigured(const AudioInput & input)54 void EngineMicrophone::onInputUnconfigured(const AudioInput& input) {
55     if (input.getType() != AudioPath::MICROPHONE) {
56         // This is an error!
57         qWarning() << "EngineMicrophone connected to AudioInput for a non-Microphone type!";
58         return;
59     }
60     m_sampleBuffer = nullptr;
61     m_pInputConfigured->forceSet(0.0);
62 }
63 
receiveBuffer(const AudioInput & input,const CSAMPLE * pBuffer,unsigned int nFrames)64 void EngineMicrophone::receiveBuffer(
65         const AudioInput& input, const CSAMPLE* pBuffer, unsigned int nFrames) {
66     Q_UNUSED(input);
67     Q_UNUSED(nFrames);
68     m_sampleBuffer = pBuffer;
69 }
70 
process(CSAMPLE * pOut,const int iBufferSize)71 void EngineMicrophone::process(CSAMPLE* pOut, const int iBufferSize) {
72     // If configured read into the output buffer.
73     // Otherwise, skip the appropriate number of samples to throw them away.
74     const CSAMPLE* sampleBuffer = m_sampleBuffer; // save pointer on stack
75     CSAMPLE_GAIN pregain = static_cast<CSAMPLE_GAIN>(m_pPregain->get());
76     if (sampleBuffer) {
77         SampleUtil::copyWithGain(pOut, sampleBuffer, pregain, iBufferSize);
78         EngineEffectsManager* pEngineEffectsManager = m_pEffectsManager->getEngineEffectsManager();
79         if (pEngineEffectsManager != nullptr) {
80             pEngineEffectsManager->processPreFaderInPlace(
81                     m_group.handle(), m_pEffectsManager->getMasterHandle(), pOut, iBufferSize,
82                     // TODO(jholthuis): Use mixxx::audio::SampleRate instead
83                     static_cast<unsigned int>(m_pSampleRate->get()));
84         }
85     } else {
86         SampleUtil::clear(pOut, iBufferSize);
87     }
88     m_sampleBuffer = nullptr;
89 
90     // Update VU meter
91     m_vuMeter.process(pOut, iBufferSize);
92 }
93 
collectFeatures(GroupFeatureState * pGroupFeatures) const94 void EngineMicrophone::collectFeatures(GroupFeatureState* pGroupFeatures) const {
95     m_vuMeter.collectFeatures(pGroupFeatures);
96 }
97