mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-22 14:55:36 +08:00
refactor: MacOS input initialization
This commit is contained in:
@@ -101,34 +101,31 @@ namespace swift::core::afv::audio
|
||||
if (!m_audioInputBuffer) { m_audioInputBuffer = new CAudioInputBuffer(this); }
|
||||
else { m_audioInputBuffer->disconnect(); } // make sure disconnected in any case
|
||||
m_audioInputBuffer->start(m_inputFormat);
|
||||
|
||||
#ifdef Q_OS_MAC
|
||||
CMacOSMicrophoneAccess::AuthorizationStatus status = m_micAccess.getAuthorizationStatus();
|
||||
if (status == CMacOSMicrophoneAccess::Authorized)
|
||||
{
|
||||
// void
|
||||
// Audio start will be handled below
|
||||
}
|
||||
else if (status == CMacOSMicrophoneAccess::NotDetermined)
|
||||
{
|
||||
connect(&m_micAccess, &CMacOSMicrophoneAccess::permissionRequestAnswered, this,
|
||||
&CInput::delayedInitMicrophone);
|
||||
m_micAccess.requestAccess();
|
||||
CLogMessage(this).info(u"MacOS requested input device");
|
||||
}
|
||||
else
|
||||
{
|
||||
CLogMessage(this).error(u"Microphone access not granted. Voice input will not work.");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
m_audioInput->start(m_audioInputBuffer);
|
||||
connect(m_audioInputBuffer, &CAudioInputBuffer::frameAvailable, this, &CInput::audioInDataAvailable);
|
||||
m_started = true;
|
||||
const QString format = toQString(m_inputFormat);
|
||||
CLogMessage(this).info(u"Starting: '%1' with: %2") << selectedDevice.description() << format;
|
||||
|
||||
#ifdef Q_OS_MAC
|
||||
this->initMicrophoneMacOS();
|
||||
#else
|
||||
this->initMicrophone();
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef Q_OS_MAC
|
||||
void CInput::initMicrophoneMacOS()
|
||||
{
|
||||
CMacOSMicrophoneAccess::AuthorizationStatus status = m_micAccess.getAuthorizationStatus();
|
||||
if (status == CMacOSMicrophoneAccess::Authorized) { this->initMicrophone(); }
|
||||
else if (status == CMacOSMicrophoneAccess::NotDetermined)
|
||||
{
|
||||
connect(&m_micAccess, &CMacOSMicrophoneAccess::permissionRequestAnswered, this, &CInput::initMicrophone);
|
||||
m_micAccess.requestAccess();
|
||||
CLogMessage(this).info(u"Request macOS permission for microphone");
|
||||
}
|
||||
else { CLogMessage(this).error(u"Microphone access not granted. Voice input will not work."); }
|
||||
}
|
||||
#endif
|
||||
|
||||
void CInput::stop()
|
||||
{
|
||||
if (!m_started) { return; }
|
||||
@@ -187,13 +184,12 @@ namespace swift::core::afv::audio
|
||||
emit opusDataAvailable(opusDataAvailableArgs);
|
||||
}
|
||||
|
||||
#ifdef Q_OS_MAC
|
||||
void CInput::delayedInitMicrophone()
|
||||
void CInput::initMicrophone()
|
||||
{
|
||||
m_audioInput->start(m_audioInputBuffer);
|
||||
connect(m_audioInputBuffer, &CAudioInputBuffer::frameAvailable, this, &CInput::audioInDataAvailable);
|
||||
m_started = true;
|
||||
CLogMessage(this).info(u"Started input");
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace swift::core::afv::audio
|
||||
|
||||
@@ -129,6 +129,8 @@ namespace swift::core::afv::audio
|
||||
private:
|
||||
void audioInDataAvailable(const QByteArray &frame);
|
||||
|
||||
void initMicrophone();
|
||||
|
||||
static constexpr qint64 c_frameSize = 960;
|
||||
int m_sampleRate = 0;
|
||||
|
||||
@@ -152,7 +154,7 @@ namespace swift::core::afv::audio
|
||||
|
||||
#ifdef Q_OS_MAC
|
||||
swift::misc::CMacOSMicrophoneAccess m_micAccess;
|
||||
void delayedInitMicrophone();
|
||||
void initMicrophoneMacOS();
|
||||
#endif
|
||||
};
|
||||
} // namespace swift::core::afv::audio
|
||||
|
||||
Reference in New Issue
Block a user