This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 603
/
AudioInputProcessor.cpp
969 lines (843 loc) · 40 KB
/
AudioInputProcessor.cpp
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
/*
* Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#include <sstream>
#include <AVSCommon/AVS/CapabilityConfiguration.h>
#include <AVSCommon/AVS/FocusState.h>
#include <AVSCommon/AVS/MessageRequest.h>
#include <AVSCommon/Utils/JSON/JSONUtils.h>
#include <AVSCommon/Utils/Logger/Logger.h>
#include <AVSCommon/Utils/Memory/Memory.h>
#include <AVSCommon/Utils/Metrics.h>
#include <AVSCommon/Utils/String/StringUtils.h>
#include <AVSCommon/Utils/UUIDGeneration/UUIDGeneration.h>
#include <AVSCommon/AVS/Attachment/AttachmentUtils.h>
#include "AIP/AudioInputProcessor.h"
namespace alexaClientSDK {
namespace capabilityAgents {
namespace aip {
using namespace avsCommon::avs;
using namespace avsCommon::utils;
using namespace avsCommon::utils::logger;
/// SpeechRecognizer capability constants
/// SpeechRecognizer interface type
static const std::string SPEECHRECOGNIZER_CAPABILITY_INTERFACE_TYPE = "AlexaInterface";
/// SpeechRecognizer interface name
static const std::string SPEECHRECOGNIZER_CAPABILITY_INTERFACE_NAME = "SpeechRecognizer";
/// SpeechRecognizer interface version
static const std::string SPEECHRECOGNIZER_CAPABILITY_INTERFACE_VERSION = "2.1";
/// String to identify log entries originating from this file.
static const std::string TAG("AudioInputProcessor");
/**
* Create a LogEntry using this file's TAG and the specified event string.
*
* @param The event string for this @c LogEntry.
*/
#define LX(event) alexaClientSDK::avsCommon::utils::logger::LogEntry(TAG, event)
/// The name of the @c FocusManager channel used by @c AudioInputProvider.
static const std::string CHANNEL_NAME = avsCommon::sdkInterfaces::FocusManagerInterface::DIALOG_CHANNEL_NAME;
/// The namespace for this capability agent.
static const std::string NAMESPACE = "SpeechRecognizer";
/// The StopCapture directive signature.
static const avsCommon::avs::NamespaceAndName STOP_CAPTURE{NAMESPACE, "StopCapture"};
/// The ExpectSpeech directive signature.
static const avsCommon::avs::NamespaceAndName EXPECT_SPEECH{NAMESPACE, "ExpectSpeech"};
/// The SetEndOfSpeechOffset directive signature.
static const avsCommon::avs::NamespaceAndName SET_END_OF_SPEECH_OFFSET{NAMESPACE, "SetEndOfSpeechOffset"};
/// The SpeechRecognizer context state signature.
static const avsCommon::avs::NamespaceAndName RECOGNIZER_STATE{NAMESPACE, "RecognizerState"};
/// The field identifying the initiator.
static const std::string INITIATOR_KEY = "initiator";
/// The field name for the user voice attachment.
static const std::string AUDIO_ATTACHMENT_FIELD_NAME = "audio";
/// The field name for the wake word engine metadata.
static const std::string KWD_METADATA_FIELD_NAME = "wakewordEngineMetadata";
/// The field name for the start of speech timestamp, reported in milliseconds since epoch. This field is provided to
/// the Recognize event and it's sent back as part of SetEndOfSpeechOffset payload.
static const std::string START_OF_SPEECH_OFFSET_FIELD_NAME = "startOfSpeechTimestamp";
/// The field name for the end of speech offset, reported in milliseconds, as part of SetEndOfSpeechOffset payload.
static const std::string END_OF_SPEECH_OFFSET_FIELD_NAME = "endOfSpeechOffsetInMilliseconds";
/**
* Creates the SpeechRecognizer capability configuration.
*
* @return The SpeechRecognizer capability configuration.
*/
static std::shared_ptr<avsCommon::avs::CapabilityConfiguration> getSpeechRecognizerCapabilityConfiguration();
std::shared_ptr<AudioInputProcessor> AudioInputProcessor::create(
std::shared_ptr<avsCommon::sdkInterfaces::DirectiveSequencerInterface> directiveSequencer,
std::shared_ptr<avsCommon::sdkInterfaces::MessageSenderInterface> messageSender,
std::shared_ptr<avsCommon::sdkInterfaces::ContextManagerInterface> contextManager,
std::shared_ptr<avsCommon::sdkInterfaces::FocusManagerInterface> focusManager,
std::shared_ptr<avsCommon::avs::DialogUXStateAggregator> dialogUXStateAggregator,
std::shared_ptr<avsCommon::sdkInterfaces::ExceptionEncounteredSenderInterface> exceptionEncounteredSender,
std::shared_ptr<avsCommon::sdkInterfaces::UserInactivityMonitorInterface> userInactivityMonitor,
AudioProvider defaultAudioProvider) {
if (!directiveSequencer) {
ACSDK_ERROR(LX("createFailed").d("reason", "nullDirectiveSequencer"));
return nullptr;
} else if (!messageSender) {
ACSDK_ERROR(LX("createFailed").d("reason", "nullMessageSender"));
return nullptr;
} else if (!contextManager) {
ACSDK_ERROR(LX("createFailed").d("reason", "nullContextManager"));
return nullptr;
} else if (!focusManager) {
ACSDK_ERROR(LX("createFailed").d("reason", "nullFocusManager"));
return nullptr;
} else if (!dialogUXStateAggregator) {
ACSDK_ERROR(LX("createFailed").d("reason", "nullDialogUXStateAggregator"));
return nullptr;
} else if (!exceptionEncounteredSender) {
ACSDK_ERROR(LX("createFailed").d("reason", "nullExceptionEncounteredSender"));
return nullptr;
} else if (!userInactivityMonitor) {
ACSDK_ERROR(LX("createFailed").d("reason", "nullUserInctivityMonitor"));
return nullptr;
}
auto aip = std::shared_ptr<AudioInputProcessor>(new AudioInputProcessor(
directiveSequencer,
messageSender,
contextManager,
focusManager,
exceptionEncounteredSender,
userInactivityMonitor,
defaultAudioProvider));
if (aip) {
dialogUXStateAggregator->addObserver(aip);
}
return aip;
}
avsCommon::avs::DirectiveHandlerConfiguration AudioInputProcessor::getConfiguration() const {
avsCommon::avs::DirectiveHandlerConfiguration configuration;
configuration[STOP_CAPTURE] = avsCommon::avs::BlockingPolicy::NON_BLOCKING;
configuration[EXPECT_SPEECH] = avsCommon::avs::BlockingPolicy::NON_BLOCKING;
configuration[SET_END_OF_SPEECH_OFFSET] = avsCommon::avs::BlockingPolicy::NON_BLOCKING;
return configuration;
}
void AudioInputProcessor::addObserver(std::shared_ptr<ObserverInterface> observer) {
if (!observer) {
ACSDK_ERROR(LX("addObserverFailed").d("reason", "nullObserver"));
return;
}
m_executor.submit([this, observer]() { m_observers.insert(observer); });
}
void AudioInputProcessor::removeObserver(std::shared_ptr<ObserverInterface> observer) {
if (!observer) {
ACSDK_ERROR(LX("removeObserverFailed").d("reason", "nullObserver"));
return;
}
m_executor.submit([this, observer]() { m_observers.erase(observer); }).wait();
}
std::future<bool> AudioInputProcessor::recognize(
AudioProvider audioProvider,
Initiator initiator,
std::chrono::steady_clock::time_point startOfSpeechTimestamp,
avsCommon::avs::AudioInputStream::Index begin,
avsCommon::avs::AudioInputStream::Index keywordEnd,
std::string keyword,
const ESPData espData,
std::shared_ptr<const std::vector<char>> KWDMetadata) {
ACSDK_METRIC_IDS(TAG, "Recognize", "", "", Metrics::Location::AIP_RECEIVE);
// If no begin index was provided, grab the current index ASAP so that we can start streaming from the time this
// call was made.
if (audioProvider.stream && INVALID_INDEX == begin) {
static const bool startWithNewData = true;
auto reader = audioProvider.stream->createReader(
avsCommon::avs::AudioInputStream::Reader::Policy::NONBLOCKING, startWithNewData);
if (!reader) {
ACSDK_ERROR(LX("recognizeFailed").d("reason", "createReaderFailed"));
std::promise<bool> ret;
ret.set_value(false);
return ret.get_future();
}
begin = reader->tell();
}
if (!espData.isEmpty()) {
m_executor.submit([this, espData]() { executePrepareEspPayload(espData); });
}
return m_executor.submit(
[this, audioProvider, initiator, startOfSpeechTimestamp, begin, keywordEnd, keyword, KWDMetadata]() {
return executeRecognize(
audioProvider, initiator, startOfSpeechTimestamp, begin, keywordEnd, keyword, KWDMetadata);
});
}
std::future<bool> AudioInputProcessor::stopCapture() {
return m_executor.submit([this]() { return executeStopCapture(); });
}
std::future<void> AudioInputProcessor::resetState() {
return m_executor.submit([this]() { executeResetState(); });
}
void AudioInputProcessor::onContextAvailable(const std::string& jsonContext) {
m_executor.submit([this, jsonContext]() { executeOnContextAvailable(jsonContext); });
}
void AudioInputProcessor::onContextFailure(const avsCommon::sdkInterfaces::ContextRequestError error) {
m_executor.submit([this, error]() { executeOnContextFailure(error); });
}
void AudioInputProcessor::handleDirectiveImmediately(std::shared_ptr<avsCommon::avs::AVSDirective> directive) {
handleDirective(std::make_shared<DirectiveInfo>(directive, nullptr));
}
void AudioInputProcessor::preHandleDirective(std::shared_ptr<DirectiveInfo> info) {
}
void AudioInputProcessor::handleDirective(std::shared_ptr<DirectiveInfo> info) {
if (!info) {
ACSDK_ERROR(LX("handleDirectiveFailed").d("reason", "nullDirectiveInfo"));
return;
}
if (info->directive->getName() == STOP_CAPTURE.name) {
ACSDK_METRIC_MSG(TAG, info->directive, Metrics::Location::AIP_RECEIVE);
handleStopCaptureDirective(info);
} else if (info->directive->getName() == EXPECT_SPEECH.name) {
handleExpectSpeechDirective(info);
} else if (info->directive->getName() == SET_END_OF_SPEECH_OFFSET.name) {
handleSetEndOfSpeechOffsetDirective(info);
} else {
std::string errorMessage =
"unexpected directive " + info->directive->getNamespace() + ":" + info->directive->getName();
m_exceptionEncounteredSender->sendExceptionEncountered(
info->directive->getUnparsedDirective(),
avsCommon::avs::ExceptionErrorType::UNEXPECTED_INFORMATION_RECEIVED,
errorMessage);
if (info->result) {
info->result->setFailed(errorMessage);
}
ACSDK_ERROR(LX("handleDirectiveFailed")
.d("reason", "unknownDirective")
.d("namespace", info->directive->getNamespace())
.d("name", info->directive->getName()));
}
}
void AudioInputProcessor::cancelDirective(std::shared_ptr<DirectiveInfo> info) {
removeDirective(info);
}
void AudioInputProcessor::onDeregistered() {
resetState();
}
void AudioInputProcessor::onFocusChanged(avsCommon::avs::FocusState newFocus) {
ACSDK_DEBUG9(LX("onFocusChanged").d("newFocus", newFocus));
m_executor.submit([this, newFocus]() { executeOnFocusChanged(newFocus); });
}
void AudioInputProcessor::onDialogUXStateChanged(
avsCommon::sdkInterfaces::DialogUXStateObserverInterface::DialogUXState newState) {
m_executor.submit([this, newState]() { executeOnDialogUXStateChanged(newState); });
}
AudioInputProcessor::AudioInputProcessor(
std::shared_ptr<avsCommon::sdkInterfaces::DirectiveSequencerInterface> directiveSequencer,
std::shared_ptr<avsCommon::sdkInterfaces::MessageSenderInterface> messageSender,
std::shared_ptr<avsCommon::sdkInterfaces::ContextManagerInterface> contextManager,
std::shared_ptr<avsCommon::sdkInterfaces::FocusManagerInterface> focusManager,
std::shared_ptr<avsCommon::sdkInterfaces::ExceptionEncounteredSenderInterface> exceptionEncounteredSender,
std::shared_ptr<avsCommon::sdkInterfaces::UserInactivityMonitorInterface> userInactivityMonitor,
AudioProvider defaultAudioProvider) :
CapabilityAgent{NAMESPACE, exceptionEncounteredSender},
RequiresShutdown{"AudioInputProcessor"},
m_directiveSequencer{directiveSequencer},
m_messageSender{messageSender},
m_contextManager{contextManager},
m_focusManager{focusManager},
m_userInactivityMonitor{userInactivityMonitor},
m_defaultAudioProvider{defaultAudioProvider},
m_lastAudioProvider{AudioProvider::null()},
m_KWDMetadataReader{nullptr},
m_state{ObserverInterface::State::IDLE},
m_focusState{avsCommon::avs::FocusState::NONE},
m_preparingToSend{false},
m_initialDialogUXStateReceived{false},
m_localStopCapturePerformed{false},
m_precedingExpectSpeechInitiator{nullptr} {
m_capabilityConfigurations.insert(getSpeechRecognizerCapabilityConfiguration());
}
std::shared_ptr<avsCommon::avs::CapabilityConfiguration> getSpeechRecognizerCapabilityConfiguration() {
std::unordered_map<std::string, std::string> configMap;
configMap.insert({CAPABILITY_INTERFACE_TYPE_KEY, SPEECHRECOGNIZER_CAPABILITY_INTERFACE_TYPE});
configMap.insert({CAPABILITY_INTERFACE_NAME_KEY, SPEECHRECOGNIZER_CAPABILITY_INTERFACE_NAME});
configMap.insert({CAPABILITY_INTERFACE_VERSION_KEY, SPEECHRECOGNIZER_CAPABILITY_INTERFACE_VERSION});
return std::make_shared<avsCommon::avs::CapabilityConfiguration>(configMap);
}
void AudioInputProcessor::doShutdown() {
m_executor.shutdown();
executeResetState();
m_directiveSequencer.reset();
m_messageSender.reset();
m_contextManager.reset();
m_focusManager.reset();
m_userInactivityMonitor.reset();
m_observers.clear();
}
std::future<bool> AudioInputProcessor::expectSpeechTimedOut() {
return m_executor.submit([this]() { return executeExpectSpeechTimedOut(); });
}
void AudioInputProcessor::handleStopCaptureDirective(std::shared_ptr<DirectiveInfo> info) {
m_executor.submit([this, info]() {
bool stopImmediately = true;
executeStopCapture(stopImmediately, info);
});
}
void AudioInputProcessor::handleExpectSpeechDirective(std::shared_ptr<DirectiveInfo> info) {
int64_t timeout;
bool found = avsCommon::utils::json::jsonUtils::retrieveValue(
info->directive->getPayload(), "timeoutInMilliseconds", &timeout);
if (!found) {
static const char* errorMessage = "missing/invalid timeoutInMilliseconds";
m_exceptionEncounteredSender->sendExceptionEncountered(
info->directive->getUnparsedDirective(),
avsCommon::avs::ExceptionErrorType::UNSUPPORTED_OPERATION,
errorMessage);
if (info->result) {
info->result->setFailed(errorMessage);
}
ACSDK_ERROR(LX("handleExpectSpeechDirectiveFailed")
.d("reason", "missingJsonField")
.d("field", "timeoutInMilliseconds"));
return;
}
m_executor.submit([this, timeout, info]() { executeExpectSpeech(std::chrono::milliseconds{timeout}, info); });
}
void AudioInputProcessor::handleSetEndOfSpeechOffsetDirective(std::shared_ptr<DirectiveInfo> info) {
auto payload = info->directive->getPayload();
int64_t endOfSpeechOffset = 0;
int64_t startOfSpeechTimestamp = 0;
bool foundEnd = json::jsonUtils::retrieveValue(payload, END_OF_SPEECH_OFFSET_FIELD_NAME, &endOfSpeechOffset);
bool foundStart =
json::jsonUtils::retrieveValue(payload, START_OF_SPEECH_OFFSET_FIELD_NAME, &startOfSpeechTimestamp);
if (foundEnd && foundStart) {
ACSDK_DEBUG0(LX("handleSetEndOfSpeechOffsetDirective")
.d("startTimeSpeech(ms)", startOfSpeechTimestamp)
.d("endTimeSpeech(ms)", startOfSpeechTimestamp + endOfSpeechOffset));
info->result->setCompleted();
} else if (foundEnd) {
// TODO(ACSDK-1917): Remove this case after AVS bug fix.
ACSDK_DEBUG0(LX("handleSetEndOfSpeechOffsetDirective")
.d("startTimeSpeech(ms)", "missing")
.d("endTimeSpeech(ms)", endOfSpeechOffset));
info->result->setCompleted();
} else {
std::string missing;
if (!foundEnd && !foundStart) {
missing = END_OF_SPEECH_OFFSET_FIELD_NAME + " and " + START_OF_SPEECH_OFFSET_FIELD_NAME;
} else if (!foundEnd) {
missing = END_OF_SPEECH_OFFSET_FIELD_NAME;
} else {
missing = START_OF_SPEECH_OFFSET_FIELD_NAME;
}
ACSDK_ERROR(LX("handleSetEndOfSpeechOffsetDirective").d("missing", missing));
info->result->setFailed("Missing parameter(s): " + missing);
}
}
void AudioInputProcessor::executePrepareEspPayload(const ESPData espData) {
m_espPayload.clear();
if (!espData.verify()) {
// Log an error as the values are invalid, but we should continue to send the recognize event.
ACSDK_ERROR(LX("executeRecognizeFailed")
.d("reason", "invalidEspData")
.d("voiceEnergy", espData.getVoiceEnergy())
.d("ambientEnergy", espData.getAmbientEnergy()));
} else {
// Assemble the event payload.
std::ostringstream payload;
// clang-format off
payload << R"({)"
R"("voiceEnergy":)" << espData.getVoiceEnergy() << R"(,)"
R"("ambientEnergy":)" << espData.getAmbientEnergy() << R"()"
<< R"(})";
// clang-format on
// Record the ReportEchoSpatialPerceptionData event payload for later use by executeContextAvailable().
m_espPayload = payload.str();
}
m_espRequest.reset();
}
bool AudioInputProcessor::executeRecognize(
AudioProvider provider,
Initiator initiator,
std::chrono::steady_clock::time_point startOfSpeechTimestamp,
avsCommon::avs::AudioInputStream::Index begin,
avsCommon::avs::AudioInputStream::Index end,
const std::string& keyword,
std::shared_ptr<const std::vector<char>> KWDMetadata) {
// Make sure we have a keyword if this is a wakeword initiator.
if (Initiator::WAKEWORD == initiator && keyword.empty()) {
ACSDK_ERROR(LX("executeRecognizeFailed").d("reason", "emptyKeywordWithWakewordInitiator"));
return false;
}
// 500ms preroll.
avsCommon::avs::AudioInputStream::Index preroll = provider.format.sampleRateHz / 2;
// Check if we have everything we need to enable false wakeword detection.
// TODO: Consider relaxing the hard requirement for a full 500ms preroll - ACSDK-276.
bool falseWakewordDetection =
Initiator::WAKEWORD == initiator && begin != INVALID_INDEX && begin >= preroll && end != INVALID_INDEX;
// If we will be enabling false wakeword detection, add preroll and build the initiator payload.
std::ostringstream initiatorPayloadJson;
// TODO: Consider reworking this code to use RapidJSON - ACSDK-279.
if (falseWakewordDetection) {
// clang-format off
initiatorPayloadJson
<< R"("wakeWordIndices":{)"
R"("startIndexInSamples":)" << preroll << R"(,)"
R"("endIndexInSamples":)" << preroll + end - begin
<< R"(})";
// clang-format on
begin -= preroll;
}
if (!keyword.empty()) {
if (falseWakewordDetection) {
initiatorPayloadJson << ",";
}
initiatorPayloadJson << R"("wakeWord":")" << string::stringToUpperCase(keyword) << R"(")";
}
// Build the initiator json.
std::ostringstream initiatorJson;
// clang-format off
initiatorJson
<< R"(")" + INITIATOR_KEY + R"(":{)"
R"("type":")" << initiatorToString(initiator) << R"(",)"
R"("payload":{)"
<< initiatorPayloadJson.str()
<< R"(})"
R"(})";
// clang-format on
return executeRecognize(provider, initiatorJson.str(), startOfSpeechTimestamp, begin, keyword, KWDMetadata);
}
bool AudioInputProcessor::executeRecognize(
AudioProvider provider,
const std::string& initiatorJson,
std::chrono::steady_clock::time_point startOfSpeechTimestamp,
avsCommon::avs::AudioInputStream::Index begin,
const std::string& keyword,
std::shared_ptr<const std::vector<char>> KWDMetadata) {
if (!provider.stream) {
ACSDK_ERROR(LX("executeRecognizeFailed").d("reason", "nullAudioInputStream"));
return false;
}
std::unordered_map<int, std::string> mapSampleRatesAVSEncoding = {{32000, "OPUS"}};
std::string avsEncodingFormat;
std::unordered_map<int, std::string>::iterator itSampleRateAVSEncoding;
switch (provider.format.encoding) {
case avsCommon::utils::AudioFormat::Encoding::LPCM:
if (provider.format.sampleRateHz != 16000) {
ACSDK_ERROR(LX("executeRecognizeFailed")
.d("reason", "unsupportedSampleRateForPCM")
.d("sampleRate", provider.format.sampleRateHz));
return false;
} else if (provider.format.sampleSizeInBits != 16) {
ACSDK_ERROR(LX("executeRecognizeFailed")
.d("reason", "unsupportedSampleSize")
.d("sampleSize", provider.format.sampleSizeInBits));
return false;
}
avsEncodingFormat = "AUDIO_L16_RATE_16000_CHANNELS_1";
break;
case avsCommon::utils::AudioFormat::Encoding::OPUS:
itSampleRateAVSEncoding = mapSampleRatesAVSEncoding.find(provider.format.sampleRateHz);
if (itSampleRateAVSEncoding == mapSampleRatesAVSEncoding.end()) {
ACSDK_ERROR(LX("executeRecognizeFailed")
.d("reason", "unsupportedSampleRateForOPUS")
.d("sampleRate", provider.format.sampleRateHz));
return false;
}
avsEncodingFormat = itSampleRateAVSEncoding->second;
break;
default:
ACSDK_ERROR(LX("executeRecognizeFailed")
.d("reason", "unsupportedEncoding")
.d("encoding", provider.format.encoding));
return false;
}
if (provider.format.endianness != avsCommon::utils::AudioFormat::Endianness::LITTLE) {
ACSDK_ERROR(LX("executeRecognizeFailed")
.d("reason", "unsupportedEndianness")
.d("endianness", provider.format.endianness));
return false;
} else if (provider.format.numChannels != 1) {
ACSDK_ERROR(LX("executeRecognizeFailed")
.d("reason", "unsupportedNumChannels")
.d("channels", provider.format.numChannels));
return false;
}
// If this is a barge-in, verify that it is permitted.
switch (m_state) {
case ObserverInterface::State::IDLE:
case ObserverInterface::State::EXPECTING_SPEECH:
break;
case ObserverInterface::State::RECOGNIZING:
// Barge-in is only permitted if the audio providers have compatible policies.
if (!m_lastAudioProvider.canBeOverridden) {
ACSDK_ERROR(LX("executeRecognizeFailed").d("reason", "Active audio provider can not be overridden"));
return false;
}
if (!provider.canOverride) {
ACSDK_ERROR(LX("executeRecognizeFailed").d("reason", "New audio provider can not override"));
return false;
}
// For barge-in, we should close the previous reader before creating another one.
if (m_reader) {
m_reader->close(
avsCommon::avs::attachment::AttachmentReader::ClosePoint::AFTER_DRAINING_CURRENT_BUFFER);
m_reader.reset();
}
break;
case ObserverInterface::State::BUSY:
ACSDK_ERROR(LX("executeRecognizeFailed").d("reason", "Barge-in is not permitted while busy"));
return false;
}
// Assemble the event payload.
std::ostringstream payload;
// clang-format off
payload << R"({)"
R"("profile":")" << provider.profile << R"(",)"
R"("format":")" << avsEncodingFormat << R"(",)";
// The initiator (or lack thereof) from a previous ExpectSpeech has precedence.
if (m_precedingExpectSpeechInitiator) {
if (!m_precedingExpectSpeechInitiator->empty()) {
payload << R"(")" << INITIATOR_KEY << R"(":)" << *m_precedingExpectSpeechInitiator << ",";
}
m_precedingExpectSpeechInitiator.reset();
} else if (!initiatorJson.empty()) {
payload << initiatorJson << ",";
}
payload << R"(")" << START_OF_SPEECH_OFFSET_FIELD_NAME << R"(":")"
<< startOfSpeechTimestamp.time_since_epoch().count()
<< R"(")" << R"(})";
// clang-format on
// Set up an attachment reader for the event.
avsCommon::avs::attachment::InProcessAttachmentReader::SDSTypeIndex offset = 0;
avsCommon::avs::attachment::InProcessAttachmentReader::SDSTypeReader::Reference reference =
avsCommon::avs::attachment::InProcessAttachmentReader::SDSTypeReader::Reference::BEFORE_WRITER;
if (INVALID_INDEX != begin) {
offset = begin;
reference = avsCommon::avs::attachment::InProcessAttachmentReader::SDSTypeReader::Reference::ABSOLUTE;
}
m_reader = avsCommon::avs::attachment::InProcessAttachmentReader::create(
sds::ReaderPolicy::NONBLOCKING, provider.stream, offset, reference);
if (!m_reader) {
ACSDK_ERROR(LX("executeRecognizeFailed").d("reason", "Failed to create attachment reader"));
return false;
}
if (KWDMetadata) {
m_KWDMetadataReader = avsCommon::avs::attachment::AttachmentUtils::createAttachmentReader(*KWDMetadata);
if (!m_KWDMetadataReader) {
ACSDK_ERROR(LX("sendingKWDMetadataFailed").d("reason", "Failed to create attachment reader"));
}
}
// Code below this point changes the state of AIP. Formally update state now, and don't error out without calling
// executeResetState() after this point.
setState(ObserverInterface::State::RECOGNIZING);
// Note that we're preparing to send a Recognize event.
m_preparingToSend = true;
// Reset flag when we send a new recognize event.
m_localStopCapturePerformed = false;
// Start assembling the context; we'll service the callback after assembling our Recognize event.
m_contextManager->getContext(shared_from_this());
// Stop the ExpectSpeech timer so we don't get a timeout.
m_expectingSpeechTimer.stop();
// Record provider as the last-used AudioProvider so it can be used in the event of an ExpectSpeech directive.
m_lastAudioProvider = provider;
// Record the Recognize event payload for later use by executeContextAvailable().
m_recognizePayload = payload.str();
// We can't assemble the MessageRequest until we receive the context.
m_recognizeRequest.reset();
return true;
}
void AudioInputProcessor::executeOnContextAvailable(const std::string jsonContext) {
ACSDK_DEBUG(LX("executeOnContextAvailable").sensitive("jsonContext", jsonContext));
// Should already be RECOGNIZING if we get here.
if (m_state != ObserverInterface::State::RECOGNIZING) {
ACSDK_ERROR(
LX("executeOnContextAvailableFailed").d("reason", "Not permitted in current state").d("state", m_state));
return;
}
// Should already have a reader.
if (!m_reader) {
ACSDK_ERROR(LX("executeOnContextAvailableFailed").d("reason", "nullReader"));
executeResetState();
return;
}
// Recognize payload should not be empty.
if (m_recognizePayload.empty()) {
ACSDK_ERROR(LX("executeOnContextAvailableFailed").d("reason", "payloadEmpty"));
executeResetState();
return;
}
// Start acquiring the channel right away; we'll service the callback after assembling our Recognize event.
if (m_focusState != avsCommon::avs::FocusState::FOREGROUND) {
if (!m_focusManager->acquireChannel(CHANNEL_NAME, shared_from_this(), NAMESPACE)) {
ACSDK_ERROR(LX("executeOnContextAvailableFailed").d("reason", "Unable to acquire channel"));
executeResetState();
return;
}
}
// Assemble the MessageRequest. It will be sent by executeOnFocusChanged when we acquire the channel.
auto dialogRequestId = avsCommon::utils::uuidGeneration::generateUUID();
m_directiveSequencer->setDialogRequestId(dialogRequestId);
if (!m_espPayload.empty()) {
auto msgIdAndESPJsonEvent =
buildJsonEventString("ReportEchoSpatialPerceptionData", dialogRequestId, m_espPayload);
m_espPayload.clear();
m_espRequest = std::make_shared<avsCommon::avs::MessageRequest>(msgIdAndESPJsonEvent.second);
}
auto msgIdAndJsonEvent = buildJsonEventString("Recognize", dialogRequestId, m_recognizePayload, jsonContext);
m_recognizeRequest = std::make_shared<avsCommon::avs::MessageRequest>(msgIdAndJsonEvent.second);
if (m_KWDMetadataReader) {
m_recognizeRequest->addAttachmentReader(KWD_METADATA_FIELD_NAME, m_KWDMetadataReader);
}
m_recognizeRequest->addAttachmentReader(AUDIO_ATTACHMENT_FIELD_NAME, m_reader);
// Release ownership of the metadata so it can be released once ACL will finish sending the message.
m_KWDMetadataReader.reset();
m_recognizeRequest->addObserver(shared_from_this());
// If we already have focus, there won't be a callback to send the message, so send it now.
if (avsCommon::avs::FocusState::FOREGROUND == m_focusState) {
sendRequestNow();
}
}
void AudioInputProcessor::executeOnContextFailure(const avsCommon::sdkInterfaces::ContextRequestError error) {
ACSDK_ERROR(LX("executeOnContextFailure").d("error", error));
executeResetState();
}
void AudioInputProcessor::executeOnFocusChanged(avsCommon::avs::FocusState newFocus) {
ACSDK_DEBUG(LX("executeOnFocusChanged").d("newFocus", newFocus));
// Note new focus state.
m_focusState = newFocus;
// If we're losing focus, stop using the channel.
if (newFocus != avsCommon::avs::FocusState::FOREGROUND) {
ACSDK_DEBUG(LX("executeOnFocusChanged").d("reason", "Lost focus"));
executeResetState();
return;
}
// We're not losing the channel (m_focusState == avsCommon::avs::FocusState::FOREGROUND). For all
// states except RECOGNIZING, there's nothing more to do here.
if (m_state != ObserverInterface::State::RECOGNIZING) {
return;
}
// For a focus change to FOREGROUND in the Recognizing state, we may have a message queued up to send. If we do,
// we can safely send it now.
sendRequestNow();
}
bool AudioInputProcessor::executeStopCapture(bool stopImmediately, std::shared_ptr<DirectiveInfo> info) {
if (info && info->isCancelled) {
ACSDK_DEBUG(LX("stopCaptureIgnored").d("reason", "isCancelled"));
return true;
}
if (m_state != ObserverInterface::State::RECOGNIZING) {
static const char* errorMessage = "StopCapture only allowed in RECOGNIZING state.";
auto returnValue = false;
if (info) {
if (info->result) {
if (m_localStopCapturePerformed) {
// Since a local StopCapture was performed, we can safely ignore the StopCapture from AVS.
m_localStopCapturePerformed = false;
returnValue = true;
info->result->setCompleted();
ACSDK_INFO(LX("executeStopCapture")
.m("StopCapture directive ignored because local StopCapture was performed."));
} else {
info->result->setFailed(errorMessage);
ACSDK_ERROR(LX("executeStopCaptureFailed")
.d("reason", "invalidState")
.d("expectedState", "RECOGNIZING")
.d("state", m_state));
}
}
removeDirective(info);
}
return returnValue;
}
// If info is nullptr, this indicates that a local StopCapture is performed.
if (nullptr == info) {
m_localStopCapturePerformed = true;
}
// Create a lambda to do the StopCapture.
std::function<void()> stopCapture = [=] {
ACSDK_DEBUG(LX("stopCapture").d("stopImmediately", stopImmediately));
if (stopImmediately) {
m_reader->close(avsCommon::avs::attachment::AttachmentReader::ClosePoint::IMMEDIATELY);
} else {
m_reader->close(avsCommon::avs::attachment::AttachmentReader::ClosePoint::AFTER_DRAINING_CURRENT_BUFFER);
}
m_reader.reset();
setState(ObserverInterface::State::BUSY);
if (info) {
if (info->result) {
info->result->setCompleted();
}
removeDirective(info);
}
};
if (m_preparingToSend) {
// If we're still preparing to send, we'll save the lambda and call it after we start sending.
m_deferredStopCapture = stopCapture;
} else {
// Otherwise, we can call the lambda now.
stopCapture();
}
return true;
}
void AudioInputProcessor::executeResetState() {
// Irrespective of current state, clean up and go back to idle.
m_expectingSpeechTimer.stop();
m_precedingExpectSpeechInitiator.reset();
if (m_reader) {
m_reader->close();
}
m_reader.reset();
m_KWDMetadataReader.reset();
m_recognizeRequest.reset();
m_espRequest.reset();
m_preparingToSend = false;
m_deferredStopCapture = nullptr;
if (m_focusState != avsCommon::avs::FocusState::NONE) {
m_focusManager->releaseChannel(CHANNEL_NAME, shared_from_this());
}
m_focusState = avsCommon::avs::FocusState::NONE;
setState(ObserverInterface::State::IDLE);
}
bool AudioInputProcessor::executeExpectSpeech(std::chrono::milliseconds timeout, std::shared_ptr<DirectiveInfo> info) {
if (info && info->isCancelled) {
ACSDK_DEBUG(LX("expectSpeechIgnored").d("reason", "isCancelled"));
return true;
}
if (m_state != ObserverInterface::State::IDLE && m_state != ObserverInterface::State::BUSY) {
static const char* errorMessage = "ExpectSpeech only allowed in IDLE or BUSY state.";
if (info->result) {
info->result->setFailed(errorMessage);
}
removeDirective(info);
ACSDK_ERROR(LX("executeExpectSpeechFailed")
.d("reason", "invalidState")
.d("expectedState", "IDLE/BUSY")
.d("state", m_state));
return false;
}
if (m_precedingExpectSpeechInitiator) {
ACSDK_ERROR(LX(__func__)
.d("reason", "precedingExpectSpeechInitiatorUnconsumed")
.d(INITIATOR_KEY.c_str(), *m_precedingExpectSpeechInitiator));
}
m_precedingExpectSpeechInitiator = memory::make_unique<std::string>("");
bool found = json::jsonUtils::retrieveValue(
info->directive->getPayload(), INITIATOR_KEY, m_precedingExpectSpeechInitiator.get());
if (found) {
ACSDK_DEBUG(LX(__func__).d("initiatorFound", *m_precedingExpectSpeechInitiator));
} else {
*m_precedingExpectSpeechInitiator = "";
}
// Start the ExpectSpeech timer.
if (!m_expectingSpeechTimer.start(timeout, std::bind(&AudioInputProcessor::expectSpeechTimedOut, this)).valid()) {
ACSDK_ERROR(LX("executeExpectSpeechFailed").d("reason", "startTimerFailed"));
}
setState(ObserverInterface::State::EXPECTING_SPEECH);
if (info->result) {
info->result->setCompleted();
}
removeDirective(info);
// If possible, start recognizing immediately.
if (m_lastAudioProvider && m_lastAudioProvider.alwaysReadable) {
return executeRecognize(m_lastAudioProvider, "");
} else if (m_defaultAudioProvider && m_defaultAudioProvider.alwaysReadable) {
return executeRecognize(m_defaultAudioProvider, "");
}
return true;
}
bool AudioInputProcessor::executeExpectSpeechTimedOut() {
if (m_state != ObserverInterface::State::EXPECTING_SPEECH) {
ACSDK_ERROR(LX("executeExpectSpeechTimedOutFailure")
.d("reason", "invalidState")
.d("expectedState", "EXPECTING_SPEECH")
.d("state", m_state));
return false;
}
m_precedingExpectSpeechInitiator.reset();
auto msgIdAndJsonEvent = buildJsonEventString("ExpectSpeechTimedOut");
auto request = std::make_shared<avsCommon::avs::MessageRequest>(msgIdAndJsonEvent.second);
request->addObserver(shared_from_this());
m_messageSender->sendMessage(request);
setState(ObserverInterface::State::IDLE);
ACSDK_ERROR(LX("executeExpectSpeechFailed").d("reason", "Timed Out"));
return true;
}
void AudioInputProcessor::executeOnDialogUXStateChanged(
avsCommon::sdkInterfaces::DialogUXStateObserverInterface::DialogUXState newState) {
if (!m_initialDialogUXStateReceived) {
// The initial dialog UX state change call comes from simply registering as an observer; it is not a deliberate
// change to the dialog state which should interrupt a recognize event.
m_initialDialogUXStateReceived = true;
return;
}
if (newState != avsCommon::sdkInterfaces::DialogUXStateObserverInterface::DialogUXState::IDLE) {
return;
}
if (m_focusState != avsCommon::avs::FocusState::NONE) {
m_focusManager->releaseChannel(CHANNEL_NAME, shared_from_this());
m_focusState = avsCommon::avs::FocusState::NONE;
}
setState(ObserverInterface::State::IDLE);
}
void AudioInputProcessor::setState(ObserverInterface::State state) {
if (m_state == state) {
return;
}
// Reset the user inactivity if transitioning to or from `RECOGNIZING` state.
if (ObserverInterface::State::RECOGNIZING == m_state || ObserverInterface::State::RECOGNIZING == state) {
m_userInactivityMonitor->onUserActive();
}
ACSDK_DEBUG(LX("setState").d("from", m_state).d("to", state));
m_state = state;
for (auto observer : m_observers) {
observer->onStateChanged(m_state);
}
}
void AudioInputProcessor::removeDirective(std::shared_ptr<DirectiveInfo> info) {
// Check result too, to catch cases where DirectiveInfo was created locally, without a nullptr result.
// In those cases there is no messageId to remove because no result was expected.
if (info->directive && info->result) {
CapabilityAgent::removeDirective(info->directive->getMessageId());
}
}
void AudioInputProcessor::sendRequestNow() {
if (m_espRequest) {
m_messageSender->sendMessage(m_espRequest);
m_espRequest.reset();
}
if (m_recognizeRequest) {
ACSDK_METRIC_IDS(TAG, "Recognize", "", "", Metrics::Location::AIP_SEND);
m_messageSender->sendMessage(m_recognizeRequest);
m_recognizeRequest.reset();
m_preparingToSend = false;
if (m_deferredStopCapture) {
m_deferredStopCapture();
m_deferredStopCapture = nullptr;
}
}
}
void AudioInputProcessor::onExceptionReceived(const std::string& exceptionMessage) {
ACSDK_ERROR(LX("onExceptionReceived").d("exception", exceptionMessage));
resetState();
}
void AudioInputProcessor::onSendCompleted(avsCommon::sdkInterfaces::MessageRequestObserverInterface::Status status) {
ACSDK_DEBUG(LX("onSendCompleted").d("status", status));
if (status == avsCommon::sdkInterfaces::MessageRequestObserverInterface::Status::SUCCESS ||
status == avsCommon::sdkInterfaces::MessageRequestObserverInterface::Status::SUCCESS_NO_CONTENT ||
status == avsCommon::sdkInterfaces::MessageRequestObserverInterface::Status::PENDING) {
// Stop listening from audio input source when the recognize event steam is closed.
ACSDK_DEBUG5(LX("stopCapture").d("reason", "streamClosed"));
stopCapture();
return;
}
ACSDK_DEBUG(LX("resetState").d("dueToStatus", status));
resetState();
}
std::unordered_set<std::shared_ptr<avsCommon::avs::CapabilityConfiguration>> AudioInputProcessor::
getCapabilityConfigurations() {
return m_capabilityConfigurations;
}
} // namespace aip
} // namespace capabilityAgents
} // namespace alexaClientSDK