-
Notifications
You must be signed in to change notification settings - Fork 117
/
VoiceRecognitionL1.ino
241 lines (218 loc) · 7.08 KB
/
VoiceRecognitionL1.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/**
@file VoiceRecognitionL1.ino
@author rakwireless.com
@brief This example shows that use the WisCore and microphone to recognize the specific simple commands.
@note Running this example requires WisCore written with a Cyberon license.
First you need select one command group, the group value can be 0-4.
eg:If selected group 1 need to set "#define COMMAND_GROUP_CHOOSE 1" in the code.
The trigger words can be "Hey RAK Star"、"Hey Helium" or "Hey RAK Cloud".
All command words will be printed on the serial port at the beginning.
@version 0.1
@date 2022-06-6
@copyright Copyright (c) 2022
*/
#include "audio.h" // Click here to install the library: http://librarymanager/All#RAKwireless-Audio
//Audio
SemaphoreHandle_t event_audio = NULL;
//hardcode model in SDK for testing
#include <CDSpotter.h>
#include "RAKwireless_Demo_pack_WithTxt_Lv1.h"
#define DSPOTTER_MODEL g_lpdwRAKwireless_Demo_pack_WithTxt_Lv1
#define COMMAND_GROUP_CHOOSE 4 //group value can be 0-4
#define COMMAND_STAGE_TIMEOUT 6000 // The minimum recording time in ms when there is no result at command stage.
#define COMMAND_STAGE_REPEAT 1 // When it is 1, sample code will recognize repeatly at command stage till to timeout.
// Otherwise, it will switch to trigger stage immediately after command recognized.
#define USE_STEREO //ESP32 using mono record audio quality is not enough, set stereo will be better
// default number of output channels
#ifdef USE_STEREO
static const char channels = 2;
#else
static const char channels = 1;
#endif
// default PCM output frequency
static const int frequency = 16000;
// Buffer to read samples into, each sample is 16-bits
short sampleBuffer[256];
// Number of audio samples read
volatile int samplesRead;
//VR
static CDSpotter g_hDSpotter;
int g_nCurrentStage = 0;
int g_nBlink = 0;
int g_nAudioDrop = 0;
uint32_t *g_ptr;//For license
void setup() {
int nMempoolSize = 0, nErr = -1;
byte *pMemPool = 0;
int nActiveCommandGroup = COMMAND_GROUP_CHOOSE;
pinMode(WB_IO2, OUTPUT);
digitalWrite(WB_IO2, HIGH);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_BLUE, OUTPUT);
digitalWrite(LED_BLUE, LOW);
digitalWrite(LED_GREEN, LOW);
delay(500);
// Initialize Serial for debug output
time_t timeout = millis();
Serial.begin(115200);
while (!Serial)
{
if ((millis() - timeout) < 3000)
{
delay(100);
}
else
{
break;
}
}
Serial.println("Before I2S init");
//Get license from flash
Audio_Handle.begin();
g_ptr = Audio_Handle.GetLicenseAddr();
Serial.println(CDSpotter::GetSerialNumber());
//DSpotter
nMempoolSize = CDSpotter::GetMemoryUsage(DSPOTTER_MODEL, 60);
Serial.print("DSpotter mem usage");
Serial.println(nMempoolSize);
pMemPool = (byte*)malloc(sizeof(byte) * nMempoolSize);
if (!pMemPool)
Serial.print("allocate DSpotter mempool failed");
//nErr = g_hDSpotter.Init(g_lpdwLicense,sizeof(g_lpdwLicense),DSPOTTER_MODEL,60,pMemPool,nMempoolSize);
nErr = g_hDSpotter.Init(g_ptr, LICEENSE_LENGTH * sizeof(uint32_t), DSPOTTER_MODEL, 60, pMemPool, nMempoolSize);
if (nErr != CDSpotter::Success)
{
Serial.print("DSpotter err: ");
Serial.println(nErr);
if (nErr == CDSpotter::LicenseFailed)
{
Serial.println("License err, please register under device ID for License and paste it in g_lpdwLicense");
Serial.println(CDSpotter::GetSerialNumber());
}
}
else
{
Serial.println("DSpotter init success");
}
//Add Set Group model API
nErr = g_hDSpotter.SetActiveCommandGroup(nActiveCommandGroup);
if (nErr != CDSpotter::Success) {
Serial.println("Set active commands group failed, using default");
nActiveCommandGroup = 1;
}
//show command
for (int nStage = 0; nStage < 2; nStage++)
{
char szCommand[64];
int nID;
int nGroupChoose;
if (nStage == 0)
{
Serial.println("The list of Trigger words: ");
nGroupChoose = 0;
}
else
{
Serial.println("The list of Command words: ");
nGroupChoose = nActiveCommandGroup;
}
for (int i = 0; i < g_hDSpotter.GetCommandCount(nGroupChoose); i++)
{
g_hDSpotter.GetCommand(nGroupChoose, i, szCommand, sizeof(szCommand), &nID);
Serial.print(szCommand);
Serial.print(" , ID = ");
Serial.println(nID);
}
Serial.println("");
}
Serial.println("");
//set 2 stage timeout
g_hDSpotter.SetCommandStageProperty(COMMAND_STAGE_TIMEOUT, COMMAND_STAGE_REPEAT == 1);
//Start VR
g_hDSpotter.Start();
Serial.println("Enter Trigger state");
//Create Audio Task otherwise audio data drop
if (!xTaskCreate(Audio_task, "Audio", 2048, NULL, 1, 0))
{
Serial.println("Ups, that should never happen");
}
}
void loop() {
int nRes = -1, nDataSize = 0;
int nFil, nSG, nScore, nID;
int nStage = 0;
short sSample[512];
char pCommand[64];
//Serial.println("loop");
//audio data lost resource not enough to do VR
if (g_hDSpotter.GetRecordLostCount() > g_nAudioDrop)
{
g_nAudioDrop = g_hDSpotter.GetRecordLostCount();
//Serial.println("drop data");
}
//Do VR
nRes = g_hDSpotter.DoRecognition(&nStage);
//VR return Key word get keyword info
if (nRes == CDSpotter::Success)
{
Serial.print("Detect ID: ");
g_hDSpotter.GetRecogResult(&nID, pCommand, sizeof(pCommand), &nScore, &nSG, &nFil);
Serial.print(pCommand);
Serial.print(" ");
Serial.println(nID);
Serial.print("SG: ");
Serial.print(nSG);
Serial.print(" Energy: ");
Serial.print(nFil);
Serial.print(" Score: ");
Serial.println(nScore);
}
//Check VR stage
if (nStage != g_nCurrentStage)
{
g_nCurrentStage = nStage;
if (nStage == CDSpotter::TriggerStage)
{
Serial.println("VR Switch to Trigger Stage");
digitalWrite(LED_BLUE, LOW);
digitalWrite(LED_GREEN, LOW);
}
else if (nStage == CDSpotter::CommandStage)
{
Serial.println("VR Switch to Command Stage");
digitalWrite(LED_BLUE, HIGH);
digitalWrite(LED_GREEN, HIGH);
}
}
}
void Audio_task(void *pvParameters)
{
size_t bytesRead = 0;
//int16_t buffer[512] = {0};
// Read from the DAC. This comes in as signed data with an extra byte.
Serial.println("I2S Thread Start");
if (!PDM.begin(channels, frequency)) {
Serial.println("Failed to start PDM!");
while (1) yield();
}
samplesRead = sizeof(sampleBuffer) / 2;
while (1)
{
bytesRead = PDM.read((void*)sampleBuffer, sizeof(sampleBuffer));
if (bytesRead > 0)
{
for (int i = 0; i < samplesRead; i++)
sampleBuffer[i] <<= 2;
#ifdef USE_STEREO
//need move stereo data to mono
for (int i = 1; i < samplesRead; i++)
sampleBuffer[i] = sampleBuffer[2 * i];
g_hDSpotter.PutRecordData(sampleBuffer, samplesRead / 2);
#else
g_hDSpotter.PutRecordData(sampleBuffer, samplesRead);
#endif
}
//delay(10);
}
PDM.end();
}