|
| 1 | +#include "settings.h" |
| 2 | +#include "MuseAI.h" |
| 3 | +#include "museWrover.h" |
| 4 | +#include "Audio.h" |
| 5 | +#include "wav_header.h" |
| 6 | +#include <ESP_I2S.h> |
| 7 | + |
| 8 | + |
| 9 | +extern "C" { |
| 10 | +#include "driver/gpio.h" |
| 11 | +#include "soc/gpio_sig_map.h" |
| 12 | +#include "esp_rom_gpio.h" |
| 13 | +} |
| 14 | + |
| 15 | + |
| 16 | +I2SClass i2s; |
| 17 | +ES8388 codec; |
| 18 | +Audio *audio = nullptr; |
| 19 | +Adafruit_NeoPixel pixels(NUMPIXELS, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800); |
| 20 | + |
| 21 | + |
| 22 | +#define PTT_PIN BUTTON_PAUSE |
| 23 | +#define MAX_DURATION 15 |
| 24 | +#define T 1024 |
| 25 | +#define RATE 16000 |
| 26 | +uint8_t* wav_buffer; |
| 27 | +int max_size; |
| 28 | +#define maxVol 100 |
| 29 | +int volume = 60; |
| 30 | +int microVol = 80; |
| 31 | +uint8_t phase = 0; |
| 32 | + |
| 33 | +// WiFi settings |
| 34 | +const char* ssid = "xhkap"; |
| 35 | +const char* password = "12345678"; |
| 36 | +// OpenAI API key |
| 37 | +const char* apiKey = MuseAISettings::OPENAI_API_KEY; |
| 38 | +bool gettingResponse = false; |
| 39 | +// Initialize MuseAI instance |
| 40 | +MuseAI museAI(apiKey); |
| 41 | + |
| 42 | +//Task Core 0 audio loop |
| 43 | +void audioLoop(void* x) { |
| 44 | + while (true) { |
| 45 | + if(audio != nullptr) audio->loop(); |
| 46 | + |
| 47 | + |
| 48 | + delay(1); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +void modVol(void* x) { |
| 53 | +while(true) |
| 54 | +{ |
| 55 | + if(gpio_get_level(BUTTON_VOL_PLUS) == 0) |
| 56 | + { |
| 57 | + delay(50); |
| 58 | + volume += 5; |
| 59 | + if(volume > maxVol) volume = maxVol; |
| 60 | + } |
| 61 | + if(gpio_get_level(BUTTON_VOL_MINUS) == 0) |
| 62 | + { |
| 63 | + delay(50); |
| 64 | + volume -= 5; |
| 65 | + if(volume < 0) volume = 0; |
| 66 | + } |
| 67 | +delay(1); |
| 68 | +} |
| 69 | + |
| 70 | +} |
| 71 | +static inline void release_mclk_pin() { |
| 72 | + #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0) |
| 73 | + esp_rom_gpio_connect_out_signal((gpio_num_t)I2S_MCLK, SIG_GPIO_OUT_IDX, false, false); |
| 74 | + #else |
| 75 | + gpio_matrix_out((gpio_num_t)I2S_MCLK, SIG_GPIO_OUT_IDX, false, false); |
| 76 | + #endif |
| 77 | + gpio_reset_pin((gpio_num_t)I2S_MCLK); |
| 78 | + pinMode(I2S_MCLK, INPUT); |
| 79 | +} |
| 80 | + |
| 81 | +void audioOpen() { |
| 82 | + if (!audio) { |
| 83 | + audio = new Audio(); |
| 84 | + audio->setPinout(I2S_BCLK, I2S_LRCK, I2S_SDOUT, I2S_MCLK); |
| 85 | + audio->setVolume(21); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +void audioClose() { |
| 90 | + if (audio) { |
| 91 | + audio->stopSong(); |
| 92 | + delay(10); |
| 93 | + delete audio; // force la libération des canaux I2S internes |
| 94 | + audio = nullptr; |
| 95 | + release_mclk_pin(); // au cas où la lib laisse MCLK mappé |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +void setup() { |
| 100 | + // Initialize serial port |
| 101 | + Serial.begin(115200); |
| 102 | + delay(1000); // Give serial port some time to initialize |
| 103 | + |
| 104 | + Serial.println("\n\n----- Voice Assistant System Starting -----"); |
| 105 | + pixels.begin(); |
| 106 | + pixels.setPixelColor(0, pixels.Color(255, 0, 0)); // Red ==> init |
| 107 | + pixels.show(); |
| 108 | + // Connect to WiFi network |
| 109 | + WiFi.mode(WIFI_STA); |
| 110 | + WiFi.begin(ssid, password); |
| 111 | + // Reduce WiFi power-save latency |
| 112 | + WiFi.setSleep(false); |
| 113 | + Serial.println("Connecting to WiFi..."); |
| 114 | + |
| 115 | + int attempt = 0; |
| 116 | + while (WiFi.status() != WL_CONNECTED && attempt < 20) { |
| 117 | + Serial.print('.'); |
| 118 | + delay(1000); |
| 119 | + attempt++; |
| 120 | + } |
| 121 | + |
| 122 | + if (WiFi.status() == WL_CONNECTED) { |
| 123 | + Serial.println("\nWiFi connected successfully!"); |
| 124 | + Serial.print("IP address: "); |
| 125 | + Serial.println(WiFi.localIP()); |
| 126 | + } |
| 127 | + |
| 128 | + while (not codec.begin(IIC_DATA, IIC_CLK)) |
| 129 | + { |
| 130 | + Serial.printf("Failed!\n"); |
| 131 | + delay(1000); |
| 132 | + } |
| 133 | + codec.volume(ES8388::ES_MAIN, 100); |
| 134 | + codec.volume(ES8388::ES_OUT1, volume); |
| 135 | + codec.ALC(false); |
| 136 | + codec.microphone_volume(microVol); |
| 137 | +// codec.select_internal_microphone(); |
| 138 | + codec.write_reg(ES8388_ADDR, 12, 0x4C); //16b internal microphone |
| 139 | + |
| 140 | + // power enable |
| 141 | + gpio_reset_pin(GPIO_PA_EN); |
| 142 | + gpio_set_direction(GPIO_PA_EN, GPIO_MODE_OUTPUT); |
| 143 | + gpio_set_level(GPIO_PA_EN, HIGH); |
| 144 | + |
| 145 | + // PTT button |
| 146 | + gpio_reset_pin(PTT_PIN); |
| 147 | + gpio_set_direction(PTT_PIN, GPIO_MODE_INPUT); |
| 148 | + gpio_set_pull_mode(PTT_PIN, GPIO_PULLUP_ONLY); |
| 149 | + // BUTTON_VOL_MINUS |
| 150 | + gpio_reset_pin(BUTTON_VOL_MINUS); |
| 151 | + gpio_set_direction(BUTTON_VOL_MINUS, GPIO_MODE_INPUT); |
| 152 | + gpio_set_pull_mode(BUTTON_VOL_MINUS, GPIO_PULLUP_ONLY); |
| 153 | + // BUTTON_VOL_PLUS |
| 154 | + gpio_reset_pin(BUTTON_VOL_PLUS); |
| 155 | + gpio_set_direction(BUTTON_VOL_PLUS, GPIO_MODE_INPUT); |
| 156 | + gpio_set_pull_mode(BUTTON_VOL_PLUS, GPIO_PULLUP_ONLY); |
| 157 | + // i2s |
| 158 | + i2s.setPins(I2S_BCLK, I2S_LRCK, I2S_SDOUT, I2S_SDIN, I2S_MCLK); |
| 159 | + |
| 160 | + // audio input buffer (MAX_DURATION) |
| 161 | + max_size = MAX_DURATION * RATE * 2 + 44; |
| 162 | + wav_buffer = (uint8_t*)ps_malloc(max_size); |
| 163 | + |
| 164 | + xTaskCreatePinnedToCore(audioLoop, "audioLoop", 8192, nullptr, 4, nullptr, 0); |
| 165 | + xTaskCreatePinnedToCore(modVol, "modVol", 8192, nullptr, 4, nullptr, 1); |
| 166 | + |
| 167 | + pixels.setPixelColor(0, pixels.Color(0, 0, 255)); // Blue ==> waiting for question |
| 168 | + pixels.show(); |
| 169 | + Serial.println("A question?"); |
| 170 | +} |
| 171 | + |
| 172 | +void loop() { |
| 173 | + |
| 174 | + size_t wav_size; |
| 175 | + int p, t; |
| 176 | + pcm_wav_header_t H = PCM_WAV_HEADER_DEFAULT(0, 16, RATE, 1); |
| 177 | + |
| 178 | + if(phase == 0) |
| 179 | + { |
| 180 | + pixels.setPixelColor(0, pixels.Color(0, 0, 255)); // Blue ==> waiting for question |
| 181 | + pixels.show(); |
| 182 | + } |
| 183 | + |
| 184 | + if (gpio_get_level(PTT_PIN) == 0) { |
| 185 | + |
| 186 | + pixels.setPixelColor(0, pixels.Color(0, 255, 0)); // Green ==> question |
| 187 | + pixels.show(); |
| 188 | + phase = 1; |
| 189 | + audioClose(); |
| 190 | + if (!i2s.begin(I2S_MODE_STD, RATE, I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_MONO, I2S_STD_SLOT_LEFT)) { |
| 191 | + printf("Failed to initialize I2S!\n"); |
| 192 | + for(;;); // do nothing |
| 193 | + } |
| 194 | + Serial.println("TALK! (for 15sec max)"); |
| 195 | + p = 44; |
| 196 | + // Record audio while PTT_PIN pushed |
| 197 | + while ((gpio_get_level(PTT_PIN) == 0) && (p < max_size)) { |
| 198 | + t = i2s.readBytes((char*)wav_buffer + p, T); |
| 199 | + p += t; |
| 200 | + } |
| 201 | + // record amp |
| 202 | + uint16_t* q = (uint16_t*) wav_buffer; |
| 203 | + for(int i=0; i<p/2; i++) q[i] = q[i] << 4; |
| 204 | + |
| 205 | + pixels.setPixelColor(0, pixels.Color(255, 165, 0)); // Orange ==> transfering |
| 206 | + pixels.show(); |
| 207 | + |
| 208 | + // adding header as a .wav record |
| 209 | + H.descriptor_chunk.chunk_size = p; |
| 210 | + H.data_chunk.subchunk_size = p - PCM_WAV_HEADER_SIZE; |
| 211 | + uint8_t* e = (uint8_t*)&H; |
| 212 | + for (int i = 0; i < 44; i++) wav_buffer[i] = e[i]; |
| 213 | + wav_size = p; |
| 214 | + Serial.println(p); |
| 215 | + i2s.playWAV(wav_buffer, wav_size); |
| 216 | + i2s.end(); |
| 217 | + |
| 218 | + // Convert speech to text |
| 219 | + unsigned long t_ptt_release = millis(); |
| 220 | + Serial.println("PTT released. Starting timing..."); |
| 221 | + unsigned long t_stt_start = millis(); |
| 222 | + String command = museAI.speechToTextFromBuffer(wav_buffer, wav_size); |
| 223 | + unsigned long t_stt_end = millis(); |
| 224 | + Serial.print("User: "); |
| 225 | + Serial.println(command); |
| 226 | + // sending question to chatgpt |
| 227 | + Serial.println("Sending request to ChatGPT..."); |
| 228 | + gettingResponse = true; |
| 229 | + unsigned long t_llm_start = millis(); |
| 230 | + String response = museAI.sendMessage(command); |
| 231 | + unsigned long t_llm_end = millis(); |
| 232 | + unsigned long t_tts_start = 0; |
| 233 | + unsigned long t_first_audio = 0; |
| 234 | + if (response.length() > 0) { |
| 235 | + Serial.println(response); |
| 236 | + |
| 237 | + t_tts_start = millis(); |
| 238 | + |
| 239 | + // tts and playing answer |
| 240 | + audioOpen(); |
| 241 | + codec.volume(ES8388::ES_OUT1, volume); |
| 242 | + // audio.connecttohost("http://direct.fipradio.fr/live/fip-midfi.mp3"); |
| 243 | + audio->openai_speech(apiKey, "tts-1", response, "", "alloy", "mp3", "1"); |
| 244 | + |
| 245 | + pixels.setPixelColor(0, pixels.Color(255, 255, 0)); // Yellow ==> answering |
| 246 | + pixels.show(); |
| 247 | + phase = 2; |
| 248 | + unsigned long wait_deadline = millis() + 15000; |
| 249 | + while (millis() < wait_deadline) { |
| 250 | + if (audio->isRunning()) { |
| 251 | + t_first_audio = millis(); |
| 252 | + break; |
| 253 | + } |
| 254 | + delay(10); |
| 255 | + } |
| 256 | + } else |
| 257 | + Serial.println("Failed to get ChatGPT response"); |
| 258 | + unsigned long stt_ms = t_stt_end - t_stt_start; |
| 259 | + H.data_chunk.subchunk_size = p - PCM_WAV_HEADER_SIZE; |
| 260 | + unsigned long llm_ms = t_llm_end - t_llm_start; |
| 261 | + unsigned long tts_to_start_ms = (t_first_audio && t_tts_start) ? (t_first_audio - t_tts_start) : 0; |
| 262 | + unsigned long total_ms = t_first_audio ? (t_first_audio - t_ptt_release) : (millis() - t_ptt_release); |
| 263 | + Serial.printf("Timing (ms): STT=%lu, LLM=%lu, TTS->first_audio=%lu, TOTAL(from PTT release)=%lu\n", stt_ms, llm_ms, tts_to_start_ms, total_ms); |
| 264 | + // waiting end of response (speech) |
| 265 | + while(phase != 0) delay(10); |
| 266 | + Serial.println("Any more questions?"); |
| 267 | + } |
| 268 | + |
| 269 | +} |
| 270 | + |
| 271 | +void audio_id3data(const char *info) { //id3 metadata |
| 272 | + //Serial.print("id3data ");Serial.println(info); |
| 273 | +} |
| 274 | +void audio_eof_mp3(const char *info) { //end of file |
| 275 | + Serial.print("eof_mp3 ");Serial.println(info); |
| 276 | +} |
| 277 | +void audio_showstation(const char *info) { |
| 278 | + //Serial.print("station ");Serial.println(info); |
| 279 | +} |
| 280 | +void audio_showstreaminfo(const char *info) { |
| 281 | + // Serial.print("streaminfo ");Serial.println(info); |
| 282 | +} |
| 283 | +void audio_showstreamtitle(const char *info) { |
| 284 | + |
| 285 | +} |
| 286 | +void audio_bitrate(const char *info) { |
| 287 | + // Serial.print("bitrate ");Serial.println(info); |
| 288 | +} |
| 289 | +void audio_commercial(const char *info) { //duration in sec |
| 290 | + // Serial.print("commercial ");Serial.println(info); |
| 291 | +} |
| 292 | +void audio_icyurl(const char *info) { //homepage |
| 293 | + // Serial.print("icyurl ");Serial.println(info); |
| 294 | +} |
| 295 | +void audio_lasthost(const char *info) { //stream URL played |
| 296 | + //Serial.print("lasthost ");Serial.println(info); |
| 297 | +} |
| 298 | +void audio_eof_speech(const char *info) { |
| 299 | + Serial.print("eof_speech ");Serial.println(info); |
| 300 | + phase = 0; |
| 301 | +} |
0 commit comments