Skip to content

Commit 132b607

Browse files
northdkHomalozoa
authored andcommitted
[Modify] some fixes & optimize memory & optimize online (#26)
1. fix some errors in cyberdog_audio 2. optimize memory using in cyberdog_audio while recording and playing 3. update audio online work mode Signed-off-by: northdk <[email protected]>
1 parent b35dc14 commit 132b607

File tree

15 files changed

+579
-315
lines changed

15 files changed

+579
-315
lines changed

CHANGELOG.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44

55
Xiaomi CyberDog's first release.
66

7-
## 1.0.1 (2021-09-16)
7+
### 1.0.0.1 (2021-09-16)
88

99
Move the repositories to GitHub and replace the athena keyword with cyberdog in batches.
1010

11-
## 1.0.2 (2021-09-27)
11+
### 1.0.0.2 (2021-09-27)
1212

1313
Add grpc_vendor, lcm_vendor, mpg123_vendor, sdl2main_vendor, sdl2mixer_vendor and opencv_vendor to constrain the building environment.
1414

15-
## 1.0.3 (2021-10-14)
15+
### 1.0.0.3 (2021-10-14)
1616

17-
Remove opencv_vendor, combine sdl2main_vendor & sdl2mixer_vendor into sdl2_vendor. Remove vision pkgs.
17+
- Remove opencv_vendor, combine sdl2main_vendor & sdl2mixer_vendor into sdl2_vendor. Remove vision pkgs.
18+
- Add behaviortreecppv3_vendor
19+
- Optimize audio pkgs

cyberdog_interaction/cyberdog_audio/audio_assistant/include/audio_assistant/combiner.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717

1818
#include <stdlib.h>
1919
#include <cstdlib>
20+
#include <memory>
2021

2122
#include "sys/stat.h"
2223
#include "sys/types.h"
2324

25+
#include "audio_base/audio_player.hpp"
26+
#include "audio_base/audio_queue.hpp"
2427

2528
#ifdef __cplusplus
2629
extern "C"
@@ -30,6 +33,9 @@ extern "C"
3033
#ifdef __cplusplus
3134
}
3235
#endif // __cplusplus
36+
37+
#define DELAY_TIMES 10
38+
3339
extern bool player_end;
3440

3541
void recorder_work_loop(void);

cyberdog_interaction/cyberdog_audio/audio_assistant/src/ai_keyword.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "rclcpp/rclcpp.hpp"
2020
#include "xiaoai_sdk/vpm/vpm_sdk.h"
2121
#include "audio_base/debug/ai_debugger.hpp"
22+
#include "audio_base/audio_queue.hpp"
2223
#include "audio_assistant/mp3decoder.hpp"
2324

2425
#define VPM_CONFIG_FILE_PATH "/opt/ros2/cyberdog/ai_conf/xaudio_engine.conf"
@@ -36,10 +37,9 @@ typedef struct
3637
{
3738
unsigned char * start;
3839
unsigned int length;
39-
std::mutex mutex;
4040
} vpm_input_buf_t;
4141

42-
extern vpm_input_buf_t vpm_input_buffer;
42+
extern athena_audio::MsgQueue<vpm_input_buf_t> vpm_msg_queue;
4343
extern bool vpm_buf_ready;
4444
extern void ai_nativeasr_data_handler(asr_msg_type_t msg, unsigned char * buf, int buf_size);
4545
void ai_vpm_work_loop();
@@ -154,9 +154,9 @@ static void vpm_wakeup_data_handle(
154154
return;
155155
}
156156

157+
// close interrupt while audio playing
157158
// std::shared_ptr<mp3decoder> mp3decoder_ptr = mp3decoder::GetInstance();
158-
// if ( mp3decoder_ptr->tts_player_is_playing() == true )
159-
// {
159+
// if ( mp3decoder_ptr->tts_player_is_playing() == true ) {
160160
// aivs_set_interrupt();
161161
// tts_stream_flush();
162162
// mp3decoder_ptr->tts_player_stop_now();
@@ -341,6 +341,7 @@ static int setParas(int type, int value)
341341

342342
void ai_vpm_work_loop()
343343
{
344+
fprintf(stdout, "vc: ai vp work loop on call\n");
344345
for (;; ) {
345346
if (getAiWakeupRunStatus() == false) {
346347
printf("vc:vpm: getAiWakeupRunStatus shitch off\n");
@@ -352,12 +353,12 @@ void ai_vpm_work_loop()
352353
if (vpm_buf_ready == false) {
353354
continue;
354355
}
355-
// will check here for open/close input to wakeup sdk
356-
vpm_input_buffer.mutex.lock();
357-
input.raw = reinterpret_cast<void *>(vpm_input_buffer.start);
358-
input.size = vpm_input_buffer.length;
356+
357+
vpm_input_buf_t vpm_data;
358+
vpm_msg_queue.DeQueue(vpm_data);
359+
input.raw = reinterpret_cast<void *>(vpm_data.start);
360+
input.size = vpm_data.length;
359361
vpm_process(&input);
360-
vpm_input_buffer.mutex.unlock();
361362
}
362363

363364
/*release vpm*/

cyberdog_interaction/cyberdog_audio/audio_assistant/src/ai_online/AivsMain.cpp

Lines changed: 60 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "xiaoai_sdk/aivs/ClientLoggerHooker.h"
5151
#include "xiaoai_sdk/audio_config/config.h"
5252
#include "audio_assistant/audio_assitant.hpp"
53+
#include "audio_base/audio_queue.hpp"
5354
#include "audio_assistant/mp3decoder.hpp"
5455

5556
#include "xiaoai_sdk/aivs/RobotController.h"
@@ -96,6 +97,13 @@ typedef struct tts_info
9697
tts_type_t type;
9798
unsigned char * address;
9899
unsigned int length;
100+
void free_mem()
101+
{
102+
if (address != nullptr) {
103+
fprintf(stdout, "vc: free ttsinfo memory\n");
104+
free(address);
105+
}
106+
}
99107
} tts_info_t;
100108

101109
typedef struct tts_queue
@@ -105,6 +113,8 @@ typedef struct tts_queue
105113
} tts_queue_t;
106114
static tts_queue_t mMsgQueueTts;
107115

116+
static athena_audio::MsgQueue<tts_info_t> TTSMsgQueue;
117+
108118
bool getAivsTtsRunStatus(void)
109119
{
110120
return mAivsTtsRunStatus;
@@ -139,12 +149,7 @@ int tts_player_decode_paly(void);
139149

140150
int tts_stream_flush(void)
141151
{
142-
// will check here
143-
std::cout << "vc: befor clear, queue.size " << mMsgQueueTts.queue.size() << std::endl;
144-
mMsgQueueTts.mutex.lock(); // queue lock
145-
mMsgQueueTts.queue.clear();
146-
mMsgQueueTts.mutex.unlock(); // queue unlock
147-
std::cout << "vc: befor clear, queue.size " << mMsgQueueTts.queue.size() << std::endl;
152+
TTSMsgQueue.Clear();
148153
return 0;
149154
}
150155

@@ -159,47 +164,38 @@ int aivs_set_interrupt(void)
159164
return 0;
160165
}
161166

162-
int ai_push_msg_playback(tts_info_t ttsInfo)
167+
int ai_push_msg_playback(const tts_info_t & ttsInfo)
163168
{
164-
std::cout << "vc: enter ai_push_msg_playback()" << std::endl;
165-
tts_info_t ttsInfoPlayback = {TTS_TYPE_INVALID, NULL, 0};
166-
tts_info_t ttsInfoPlayStart = {TTS_TYPE_INVALID, NULL, 0};
167-
tts_info_t ttsInfoPlayStop = {TTS_TYPE_INVALID, NULL, 0};
169+
std::cout << "vc: enter ai_push_msg_playback(), type: " << ttsInfo.type << std::endl;
170+
tts_info_t ttsInfoPlay = {ttsInfo.type, NULL, ttsInfo.length};
168171

169172
switch (ttsInfo.type) {
170173
case TTS_TYPE_STREAM:
171174
std::cout << "vc: TTS_TYPE_STREAM Case !!" << std::endl;
172175

173176
if (ttsInfo.address == NULL || ttsInfo.length == 0) {
174-
std::cout << "vc: ai_push_msg_playback invalid paramter !!!" << std::endl;
177+
std::cout << "vc: ai_push_msg_playback invalid paramter, stream addr is null !!!" <<
178+
std::endl;
175179
return -1;
176180
} else {
177-
mMsgQueueTts.mutex.lock(); // queue lock
178-
ttsInfoPlayback.type = ttsInfo.type;
179-
ttsInfoPlayback.length = ttsInfo.length;
180-
ttsInfoPlayback.address = (unsigned char *)malloc(ttsInfo.length);
181-
if (ttsInfoPlayback.address != NULL) {
182-
memcpy(ttsInfoPlayback.address, ttsInfo.address, ttsInfoPlayback.length);
183-
mMsgQueueTts.queue.push_front(ttsInfoPlayback);
181+
ttsInfoPlay.address = (unsigned char *)malloc(ttsInfo.length);
182+
if (ttsInfoPlay.address != NULL) {
183+
memcpy(ttsInfoPlay.address, ttsInfo.address, ttsInfoPlay.length);
184+
TTSMsgQueue.EnQueue(ttsInfoPlay);
185+
} else {
186+
std::cout << "vc: ai_push_msg_playback failed, cannot allocate memory!" << std::endl;
184187
}
185-
mMsgQueueTts.mutex.unlock(); // queue unlock
186188
}
187189
break;
188190

189191
case TTS_TYPE_SYNC_START:
190192
std::cout << "vc: TTS_TYPE_SYNC_START Case !!" << std::endl;
191-
mMsgQueueTts.mutex.lock(); // queue lock
192-
ttsInfoPlayStart.type = ttsInfo.type;
193-
mMsgQueueTts.queue.push_front(ttsInfoPlayStart);
194-
mMsgQueueTts.mutex.unlock(); // queue unlock
193+
TTSMsgQueue.EnQueue(ttsInfoPlay);
195194
break;
196195

197196
case TTS_TYPE_SYNC_STOP:
198197
std::cout << "vc: TTS_TYPE_SYNC_STOP Case !!" << std::endl;
199-
mMsgQueueTts.mutex.lock(); // queue lock
200-
ttsInfoPlayStop.type = ttsInfo.type;
201-
mMsgQueueTts.queue.push_front(ttsInfoPlayStop);
202-
mMsgQueueTts.mutex.unlock(); // queue unlock
198+
TTSMsgQueue.EnQueue(ttsInfoPlay);
203199
break;
204200

205201
default:
@@ -222,51 +218,46 @@ int aivsTtsHandler(void)
222218
continue;
223219
}
224220

225-
mMsgQueueTts.mutex.lock(); // queue lock
226-
if (!mMsgQueueTts.queue.empty()) {
227-
ttsInfoPlayback = mMsgQueueTts.queue.back();
228-
mMsgQueueTts.queue.pop_back();
229-
mMsgQueueTts.mutex.unlock(); // queue unlock
230-
switch (ttsInfoPlayback.type) {
231-
case TTS_TYPE_STREAM:
232-
if (ttsInfoPlayback.address == NULL || ttsInfoPlayback.length == 0) {
233-
usleep(1000 * 100);
234-
continue;
235-
} else {
236-
std::cout << "vc: Process TTS_TYPE_STREAM " << std::endl;
237-
cyberdog_audio::mp3decoder::GetInstance()->tts_player_accumulate(
238-
ttsInfoPlayback.address,
239-
ttsInfoPlayback.length);
240-
free(ttsInfoPlayback.address);
241-
}
242-
break;
221+
if (!TTSMsgQueue.DeQueue(ttsInfoPlayback)) {
222+
std::cout << "vc: aivsTtsHandler get msg with empty, skip once!" << std::endl;
223+
continue;
224+
}
243225

244-
case TTS_TYPE_SYNC_START:
245-
std::cout << "vc: Process TTS_TYPE_SYNC_START " << std::endl;
246-
cyberdog_audio::mp3decoder::GetInstance()->tts_player_init();
247-
ai_push_msg(101); /*led control for start-dialog status*/
248-
break;
226+
switch (ttsInfoPlayback.type) {
227+
case TTS_TYPE_STREAM:
228+
if (ttsInfoPlayback.address == NULL || ttsInfoPlayback.length == 0) {
229+
usleep(1000 * 100);
230+
continue;
231+
} else {
232+
std::cout << "vc: Process TTS_TYPE_STREAM " << std::endl;
233+
cyberdog_audio::mp3decoder::GetInstance()->tts_player_accumulate(
234+
ttsInfoPlayback.address,
235+
ttsInfoPlayback.length);
236+
free(ttsInfoPlayback.address);
237+
}
238+
break;
249239

250-
case TTS_TYPE_SYNC_STOP:
251-
std::cout << "vc: Process TTS_TYPE_SYNC_STOP " << std::endl;
240+
case TTS_TYPE_SYNC_START:
241+
std::cout << "vc: Process TTS_TYPE_SYNC_START " << std::endl;
242+
cyberdog_audio::mp3decoder::GetInstance()->tts_player_init();
243+
ai_push_msg(101); /*led control for start-dialog status*/
244+
break;
252245

253-
if (cyberdog_audio::mp3decoder::GetInstance() == nullptr) {
254-
fprintf(stderr, "Process TTS_TYPE_SYNC_STOP Error with invalid pointer!\n");
255-
break;
256-
}
246+
case TTS_TYPE_SYNC_STOP:
247+
std::cout << "vc: Process TTS_TYPE_SYNC_STOP " << std::endl;
257248

258-
cyberdog_audio::mp3decoder::GetInstance()->tts_player_decode_paly();
259-
// ai_push_msg(104);/*led control for end -dialog status*/
249+
if (cyberdog_audio::mp3decoder::GetInstance() == nullptr) {
250+
fprintf(stderr, "Process TTS_TYPE_SYNC_STOP Error with invalid pointer!\n");
260251
break;
252+
}
261253

262-
default:
263-
std::cout << "vc: Process default case !!" << std::endl;
264-
break;
265-
}
266-
} else {
267-
mMsgQueueTts.mutex.unlock(); // queue unlock
268-
usleep(1000 * 100);
269-
continue;
254+
cyberdog_audio::mp3decoder::GetInstance()->tts_player_decode_paly();
255+
// ai_push_msg(104);/*led control for end -dialog status*/
256+
break;
257+
258+
default:
259+
std::cout << "vc: Process default case !!" << std::endl;
260+
break;
270261
}
271262
}
272263
}
@@ -778,6 +769,8 @@ void initAivsDeviceOAuth()
778769
clientInfo->setDeviceId(DEVICE_OAUTH_DEVICE_ID);
779770

780771
auto config = getAudioConfig();
772+
config->putBoolean(aivs::AivsConfig::Auth::REQ_TOKEN_HYBRID, true);
773+
config->putBoolean(aivs::AivsConfig::Auth::REQ_TOKEN_BY_SDK, true);
781774
gEngine = aivs::Engine::create(config, clientInfo, aivs::Engine::ENGINE_AUTH_DEVICE_OAUTH);
782775
}
783776

cyberdog_interaction/cyberdog_audio/audio_assistant/src/ai_online/StorageCapabilityImpl.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@
2323

2424
StorageCapabilityImpl::StorageCapabilityImpl()
2525
{
26-
#ifndef __NuttX__
26+
/* #ifndef __NuttX__
2727
storageFilePath = "data.json";
2828
#else
2929
storageFilePath = "/tmp/data.json";
30-
#endif
30+
#endif */
31+
storageFilePath = "/opt/ros2/cyberdog/data/token.json";
3132
loadKeyValuesFromFile();
3233
}
3334

0 commit comments

Comments
 (0)