move audio enable/disable in ws connect/discconect

This commit is contained in:
Terrence
2024-11-05 16:50:29 +08:00
parent 8fccef2c52
commit bc4b0a0bb1
11 changed files with 30 additions and 16 deletions

View File

@@ -70,6 +70,7 @@ Application::~Application() {
void Application::CheckNewVersion() { void Application::CheckNewVersion() {
// Check if there is a new firmware version available // Check if there is a new firmware version available
firmware_upgrade_.SetPostData(Board::GetInstance().GetJson());
firmware_upgrade_.CheckVersion(); firmware_upgrade_.CheckVersion();
if (firmware_upgrade_.HasNewVersion()) { if (firmware_upgrade_.HasNewVersion()) {
// Wait for the chat state to be idle // Wait for the chat state to be idle
@@ -106,6 +107,7 @@ void Application::Alert(const std::string&& title, const std::string&& message)
void Application::PlayLocalFile(const char* data, size_t size) { void Application::PlayLocalFile(const char* data, size_t size) {
ESP_LOGI(TAG, "PlayLocalFile: %zu bytes", size); ESP_LOGI(TAG, "PlayLocalFile: %zu bytes", size);
SetDecodeSampleRate(16000); SetDecodeSampleRate(16000);
audio_device_->EnableOutput(true);
{ {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
@@ -135,8 +137,9 @@ void Application::Start() {
audio_device_ = board.CreateAudioDevice(); audio_device_ = board.CreateAudioDevice();
audio_device_->Initialize(); audio_device_->Initialize();
audio_device_->EnableOutput(true);
audio_device_->EnableInput(true); audio_device_->EnableInput(true);
audio_device_->EnableOutput(true);
audio_device_->EnableOutput(false);
audio_device_->OnInputData([this](std::vector<int16_t>&& data) { audio_device_->OnInputData([this](std::vector<int16_t>&& data) {
if (16000 != AUDIO_INPUT_SAMPLE_RATE) { if (16000 != AUDIO_INPUT_SAMPLE_RATE) {
if (audio_device_->input_channels() == 2) { if (audio_device_->input_channels() == 2) {
@@ -195,7 +198,6 @@ void Application::Start() {
}, "play_audio", 4096 * 4, this, 4, NULL); }, "play_audio", 4096 * 4, this, 4, NULL);
board.StartNetwork(); board.StartNetwork();
firmware_upgrade_.SetPostData(board.GetJson());
// Blink the LED to indicate the device is running // Blink the LED to indicate the device is running
builtin_led.SetGreen(); builtin_led.SetGreen();
builtin_led.BlinkOnce(); builtin_led.BlinkOnce();
@@ -339,7 +341,7 @@ void Application::Start() {
}); });
#endif #endif
SetChatState(kChatStateIdle); chat_state_ = kChatStateIdle;
display_.UpdateDisplay(); display_.UpdateDisplay();
} }
@@ -403,7 +405,6 @@ void Application::SetChatState(ChatState state) {
case kChatStateUnknown: case kChatStateUnknown:
case kChatStateIdle: case kChatStateIdle:
builtin_led.TurnOff(); builtin_led.TurnOff();
audio_device_->EnableOutput(false);
break; break;
case kChatStateConnecting: case kChatStateConnecting:
builtin_led.SetBlue(); builtin_led.SetBlue();
@@ -416,7 +417,6 @@ void Application::SetChatState(ChatState state) {
case kChatStateSpeaking: case kChatStateSpeaking:
builtin_led.SetGreen(); builtin_led.SetGreen();
builtin_led.TurnOn(); builtin_led.TurnOn();
audio_device_->EnableOutput(true);
break; break;
case kChatStateWakeWordDetected: case kChatStateWakeWordDetected:
builtin_led.SetBlue(); builtin_led.SetBlue();
@@ -686,6 +686,7 @@ void Application::StartWebSocketClient() {
ws_client_->OnDisconnected([this]() { ws_client_->OnDisconnected([this]() {
ESP_LOGI(TAG, "Websocket disconnected"); ESP_LOGI(TAG, "Websocket disconnected");
Schedule([this]() { Schedule([this]() {
audio_device_->EnableOutput(false);
#ifdef CONFIG_USE_AFE_SR #ifdef CONFIG_USE_AFE_SR
audio_processor_.Stop(); audio_processor_.Stop();
#endif #endif
@@ -699,4 +700,7 @@ void Application::StartWebSocketClient() {
ESP_LOGE(TAG, "Failed to connect to websocket server"); ESP_LOGE(TAG, "Failed to connect to websocket server");
return; return;
} }
}
// 建立语音通道后打开音频输出,避免待机时喇叭底噪
audio_device_->EnableOutput(true);
}

View File

@@ -1,18 +1,18 @@
#ifndef _APPLICATION_H_ #ifndef _APPLICATION_H_
#define _APPLICATION_H_ #define _APPLICATION_H_
#include <freertos/FreeRTOS.h>
#include <freertos/event_groups.h>
#include <freertos/task.h>
#include <opus.h>
#include <mutex>
#include <list>
#include <condition_variable>
#include <OpusEncoder.h> #include <OpusEncoder.h>
#include <OpusResampler.h> #include <OpusResampler.h>
#include <WebSocket.h> #include <WebSocket.h>
#include <opus.h>
#include <resampler_structs.h>
#include <freertos/event_groups.h>
#include <freertos/task.h>
#include <mutex>
#include <list>
#include <condition_variable>
#include "AudioDevice.h" #include "AudioDevice.h"
#include "Display.h" #include "Display.h"
#include "Board.h" #include "Board.h"

View File

@@ -8,6 +8,8 @@
#include <string> #include <string>
#include <functional> #include <functional>
#include "Board.h"
class AudioDevice { class AudioDevice {
public: public:
AudioDevice(); AudioDevice();
@@ -44,7 +46,7 @@ protected:
int output_sample_rate_ = 0; int output_sample_rate_ = 0;
int input_channels_ = 1; int input_channels_ = 1;
int output_channels_ = 1; int output_channels_ = 1;
int output_volume_ = 70; int output_volume_ = AUDIO_DEFAULT_OUTPUT_VOLUME;
i2s_chan_handle_t tx_handle_ = nullptr; i2s_chan_handle_t tx_handle_ = nullptr;
i2s_chan_handle_t rx_handle_ = nullptr; i2s_chan_handle_t rx_handle_ = nullptr;

View File

@@ -4,10 +4,10 @@
#include "config.h" #include "config.h"
#include <Http.h> #include <Http.h>
#include <WebSocket.h> #include <WebSocket.h>
#include <AudioDevice.h>
#include <string> #include <string>
void* create_board(); void* create_board();
class AudioDevice;
class Board { class Board {
public: public:

View File

@@ -37,8 +37,10 @@ void Ml307Board::StartNetwork() {
int result = modem_.WaitForNetworkReady(); int result = modem_.WaitForNetworkReady();
if (result == -1) { if (result == -1) {
application.Alert("Error", "PIN is not ready"); application.Alert("Error", "PIN is not ready");
return;
} else if (result == -2) { } else if (result == -2) {
application.Alert("Error", "Registration denied"); application.Alert("Error", "Registration denied");
return;
} }
// Print the ML307 modem information // Print the ML307 modem information

View File

@@ -5,6 +5,7 @@
#define AUDIO_INPUT_SAMPLE_RATE 16000 #define AUDIO_INPUT_SAMPLE_RATE 16000
#define AUDIO_OUTPUT_SAMPLE_RATE 24000 #define AUDIO_OUTPUT_SAMPLE_RATE 24000
#define AUDIO_DEFAULT_OUTPUT_VOLUME 70
#define AUDIO_I2S_METHOD_SIMPLEX #define AUDIO_I2S_METHOD_SIMPLEX

View File

@@ -5,6 +5,7 @@
#define AUDIO_INPUT_SAMPLE_RATE 16000 #define AUDIO_INPUT_SAMPLE_RATE 16000
#define AUDIO_OUTPUT_SAMPLE_RATE 24000 #define AUDIO_OUTPUT_SAMPLE_RATE 24000
#define AUDIO_DEFAULT_OUTPUT_VOLUME 70
#define AUDIO_I2S_METHOD_SIMPLEX #define AUDIO_I2S_METHOD_SIMPLEX

View File

@@ -5,6 +5,7 @@
#define AUDIO_INPUT_SAMPLE_RATE 16000 #define AUDIO_INPUT_SAMPLE_RATE 16000
#define AUDIO_OUTPUT_SAMPLE_RATE 16000 #define AUDIO_OUTPUT_SAMPLE_RATE 16000
#define AUDIO_DEFAULT_OUTPUT_VOLUME 70
#define AUDIO_INPUT_REFERENCE true #define AUDIO_INPUT_REFERENCE true

View File

@@ -5,6 +5,7 @@
#define AUDIO_INPUT_SAMPLE_RATE 24000 #define AUDIO_INPUT_SAMPLE_RATE 24000
#define AUDIO_OUTPUT_SAMPLE_RATE 24000 #define AUDIO_OUTPUT_SAMPLE_RATE 24000
#define AUDIO_DEFAULT_OUTPUT_VOLUME 70
#define AUDIO_INPUT_REFERENCE true #define AUDIO_INPUT_REFERENCE true

View File

@@ -5,6 +5,7 @@
#define AUDIO_INPUT_SAMPLE_RATE 24000 #define AUDIO_INPUT_SAMPLE_RATE 24000
#define AUDIO_OUTPUT_SAMPLE_RATE 24000 #define AUDIO_OUTPUT_SAMPLE_RATE 24000
#define AUDIO_DEFAULT_OUTPUT_VOLUME 70
#define AUDIO_INPUT_REFERENCE true #define AUDIO_INPUT_REFERENCE true

View File

@@ -5,6 +5,7 @@
#define AUDIO_INPUT_SAMPLE_RATE 24000 #define AUDIO_INPUT_SAMPLE_RATE 24000
#define AUDIO_OUTPUT_SAMPLE_RATE 24000 #define AUDIO_OUTPUT_SAMPLE_RATE 24000
#define AUDIO_DEFAULT_OUTPUT_VOLUME 80
#define AUDIO_INPUT_REFERENCE true #define AUDIO_INPUT_REFERENCE true