From 55ba67190d118ead7e792ecaa4586bea0be41cbf Mon Sep 17 00:00:00 2001 From: Tim Gromeyer Date: Wed, 26 Mar 2025 14:42:31 +0100 Subject: [PATCH] [Linux] Implement adaptive audio noice --- linux/Main.qml | 36 ++++++++++++++++++++++++++++++++++++ linux/airpods_packets.h | 11 +++++++++++ linux/main.cpp | 18 ++++++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/linux/Main.qml b/linux/Main.qml index d980272..5e86f2b 100644 --- a/linux/Main.qml +++ b/linux/Main.qml @@ -36,5 +36,41 @@ ApplicationWindow { checked: airPodsTrayApp.conversationalAwareness onCheckedChanged: airPodsTrayApp.conversationalAwareness = checked } + + Slider { + visible: airPodsTrayApp.adaptiveModeActive + from: 0 + to: 100 + stepSize: 1 + value: airPodsTrayApp.adaptiveNoiseLevel + + property Timer debounceTimer: Timer { + interval: 500 // 500ms delay after last change + onTriggered: { + if (!parent.pressed) { + airPodsTrayApp.setAdaptiveNoiseLevel(parent.value) + } + } + } + + onPressedChanged: { + if (!pressed) { + debounceTimer.stop() + airPodsTrayApp.setAdaptiveNoiseLevel(value) + } + } + + onValueChanged: { + if (pressed) { + debounceTimer.restart() + } + } + + Label { + text: "Adaptive Noise Level: " + parent.value + color: "#ffffff" + anchors.top: parent.bottom + } + } } } \ No newline at end of file diff --git a/linux/airpods_packets.h b/linux/airpods_packets.h index 6a4082a..2f71555 100644 --- a/linux/airpods_packets.h +++ b/linux/airpods_packets.h @@ -63,6 +63,17 @@ namespace AirPodsPackets static const QByteArray DISCONNECT_REQUEST = QByteArray::fromHex("00020000"); } + // Adaptive Noise Packets + namespace AdaptiveNoise + { + const QByteArray HEADER = QByteArray::fromHex("0400040009002E"); + + inline QByteArray getPacket(int level) + { + return HEADER + static_cast(level) + QByteArray::fromHex("000000"); + } + } + // Parsing Headers namespace Parse { diff --git a/linux/main.cpp b/linux/main.cpp index 7d50774..8851dc9 100644 --- a/linux/main.cpp +++ b/linux/main.cpp @@ -18,6 +18,8 @@ class AirPodsTrayApp : public QObject { Q_PROPERTY(QString earDetectionStatus READ earDetectionStatus NOTIFY earDetectionStatusChanged) Q_PROPERTY(int noiseControlMode READ noiseControlMode WRITE setNoiseControlMode NOTIFY noiseControlModeChanged) Q_PROPERTY(bool conversationalAwareness READ conversationalAwareness WRITE setConversationalAwareness NOTIFY conversationalAwarenessChanged) + Q_PROPERTY(int adaptiveNoiseLevel READ adaptiveNoiseLevel WRITE setAdaptiveNoiseLevel NOTIFY adaptiveNoiseLevelChanged) + Q_PROPERTY(bool adaptiveModeActive READ adaptiveModeActive NOTIFY noiseControlModeChanged) public: AirPodsTrayApp(bool debugMode) : debugMode(debugMode) { @@ -96,6 +98,8 @@ public: QString earDetectionStatus() const { return m_earDetectionStatus; } int noiseControlMode() const { return static_cast(m_noiseControlMode); } bool conversationalAwareness() const { return m_conversationalAwareness; } + bool adaptiveModeActive() const { return m_noiseControlMode == NoiseControlMode::Adaptive; } + int adaptiveNoiseLevel() const { return m_adaptiveNoiseLevel; } private: bool debugMode; @@ -251,6 +255,18 @@ public slots: saveConversationalAwarenessState(); } + void setAdaptiveNoiseLevel(int level) + { + level = qBound(0, level, 100); + if (m_adaptiveNoiseLevel != level && adaptiveModeActive()) + { + m_adaptiveNoiseLevel = level; + QByteArray packet = AirPodsPackets::AdaptiveNoise::getPacket(level); + writePacketToSocket(packet, "Adaptive noise level packet written: "); + emit adaptiveNoiseLevelChanged(level); + } + } + bool writePacketToSocket(const QByteArray &packet, const QString &logMessage) { if (socket && socket->isOpen()) @@ -692,6 +708,7 @@ signals: void earDetectionStatusChanged(const QString &status); void batteryStatusChanged(const QString &status); void conversationalAwarenessChanged(bool enabled); + void adaptiveNoiseLevelChanged(int level); private: QSystemTrayIcon *trayIcon; @@ -710,6 +727,7 @@ private: QString m_earDetectionStatus; NoiseControlMode m_noiseControlMode = NoiseControlMode::Off; bool m_conversationalAwareness = false; + int m_adaptiveNoiseLevel = 50; }; int main(int argc, char *argv[]) {