Update audioengine.cpp

Better Audio Handling when switching to Bluetooth devices

Signed-off-by: rohithzmoi <166651631+rohithzmoi@users.noreply.github.com>
This commit is contained in:
rohithzmoi 2024-09-06 16:24:36 +05:30 committed by GitHub
parent 9152655956
commit 6839dfcb77
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 25 additions and 37 deletions

View File

@ -1,7 +1,7 @@
/*
Copyright (C) 2019-2021 Doug McLain
Modified Copyright (C) 2024 Rohith Namboothiri
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
@ -17,9 +17,9 @@
*/
#include "audioengine.h"
#include "AudioSessionManager.h"
#include <QDebug>
#include <cmath>
#include "AudioSessionManager.h"
#if defined (Q_OS_MACOS) || defined(Q_OS_IOS)
#define MACHAK 1
@ -27,13 +27,31 @@
#define MACHAK 0
#endif
AudioEngine *audioEngineInstance = nullptr;
// Extern C functions for Objective-C++ to call
extern "C" void AudioEngine_start_playback() {
if (audioEngineInstance != nullptr) {
audioEngineInstance->start_playback();
} else {
qDebug() << "AudioEngine instance is not initialized.";
}
}
extern "C" void AudioEngine_stop_playback() {
if (audioEngineInstance != nullptr) {
audioEngineInstance->stop_playback();
} else {
qDebug() << "AudioEngine instance is not initialized.";
}
}
AudioEngine::AudioEngine(QString in, QString out) :
m_outputdevice(out),
m_inputdevice(in),
m_out(nullptr),
m_in(nullptr),
m_srm(1),
m_dataWritten(false)
m_srm(1)
{
m_audio_out_temp_buf_p = m_audio_out_temp_buf;
memset(m_aout_max_buf, 0, sizeof(float) * 200);
@ -138,7 +156,6 @@ void AudioEngine::init()
void AudioEngine::start_capture()
{
m_audioinq.clear();
// setupAVAudioSession();
if(m_in != nullptr){
m_indev = m_in->start();
if(MACHAK) m_srm = (float)(m_in->format().sampleRate()) / 8000.0;
@ -154,12 +171,11 @@ void AudioEngine::stop_capture()
}
}
void AudioEngine::start_playback()
{
m_outdev = m_out->start();
setupAVAudioSession();
setupBackgroundAudio();
m_outdev = m_out->start();
}
void AudioEngine::stop_playback()
@ -167,11 +183,6 @@ void AudioEngine::stop_playback()
//m_outdev->reset();
m_out->reset();
m_out->stop();
static bool isSessionActive = false;
if (isSessionActive) {
deactivateAVAudioSession();
isSessionActive = false; // Reset session state before reactivation
}
}
void AudioEngine::input_data_received()
@ -220,10 +231,6 @@ void AudioEngine::write(int16_t *pcm, size_t s)
}
size_t l = m_outdev->write((const char *) pcm, sizeof(int16_t) * s);
if (l > 0) { // Data was written
m_dataWritten = true;
}
if (l*2 < s){
qDebug() << "AudioEngine::write() " << s << ":" << l << ":" << (int)m_out->bytesFree() << ":" << m_out->bufferSize() << ":" << m_out->error();
@ -373,40 +380,21 @@ void AudioEngine::process_audio(int16_t *pcm, size_t s)
void AudioEngine::handleStateChanged(QAudio::State newState)
{
static bool isSessionActive = false;
switch (newState) {
case QAudio::ActiveState:
qDebug() << "AudioOut state active";
if (!isSessionActive) {
setupAVAudioSession();
isSessionActive = true;
}
break;
case QAudio::SuspendedState:
qDebug() << "AudioOut state suspended";
if (isSessionActive) {
deactivateAVAudioSession();
isSessionActive = false;
}
break;
case QAudio::IdleState:
qDebug() << "AudioOut state idle";
setupBackgroundAudio();
setupBackgroundAudio();
break;
case QAudio::StoppedState:
qDebug() << "AudioOut state stopped";
if (isSessionActive) {
deactivateAVAudioSession();
isSessionActive = false;
}
break;
default:
break;
}
}