1717#include <rtos/alloc.h>
1818#include <rtos/init.h>
1919#include <sof/lib/memory.h>
20- #include <sof/lib/notifier.h>
2120#include <rtos/wait.h>
2221#include <sof/lib/uuid.h>
2322#include <sof/list.h>
4140#include <stdint.h>
4241#include <stdlib.h>
4342#include <sof/samples/audio/kwd_nn_detect_test.h>
43+ #if CONFIG_AMS
44+ #include <sof/lib/ams.h>
45+ #else
46+ #include <sof/lib/notifier.h>
47+ #endif
4448
4549#define ACTIVATION_DEFAULT_SHIFT 3
4650#define ACTIVATION_DEFAULT_COEF 0.05
@@ -85,19 +89,25 @@ struct comp_data {
8589 uint32_t keyphrase_samples ; /**< keyphrase length in samples */
8690 uint32_t drain_req ; /** defines draining size in bytes. */
8791 uint16_t sample_valid_bytes ;
88- struct kpb_event_data event_data ;
8992 struct kpb_client client_data ;
9093
9194#if CONFIG_KWD_NN_SAMPLE_KEYPHRASE
9295 int16_t * input ;
9396 size_t input_size ;
9497#endif
95-
9698 struct ipc_msg * msg ; /**< host notification */
9799
98100 void (* detect_func )(struct comp_dev * dev ,
99101 const struct audio_stream __sparse_cache * source , uint32_t frames );
100102 struct sof_ipc_comp_event event ;
103+
104+ #if CONFIG_AMS
105+ /* AMS related data */
106+ uint32_t ams_uuid_id ;
107+ struct ams_message_payload ams_payload ;
108+ #else
109+ struct kpb_event_data event_data ;
110+ #endif /* CONFIG_AMS */
101111};
102112
103113static inline bool detector_is_sample_width_supported (enum sof_ipc_frame sf )
@@ -140,6 +150,25 @@ static void notify_host(const struct comp_dev *dev)
140150#endif /* CONFIG_IPC_MAJOR_4 */
141151}
142152
153+ #if CONFIG_AMS
154+ static int ams_notify_kpb (const struct comp_dev * dev )
155+ {
156+ struct comp_data * cd = comp_get_drvdata (dev );
157+
158+ cd -> client_data .r_ptr = NULL ;
159+ cd -> client_data .sink = NULL ;
160+ cd -> client_data .id = 0 ; /**< TODO: acquire proper id from kpb */
161+ /* time in milliseconds */
162+ cd -> client_data .drain_req = (cd -> drain_req != 0 ) ?
163+ cd -> drain_req :
164+ cd -> config .drain_req ;
165+
166+ cd -> ams_payload .message_length = sizeof (struct kpb_client );
167+ cd -> ams_payload .message = (uint8_t * )& cd -> client_data ;
168+
169+ return ams_send (& cd -> ams_payload );
170+ }
171+ #else
143172static void notify_kpb (const struct comp_dev * dev )
144173{
145174 struct comp_data * cd = comp_get_drvdata (dev );
@@ -160,11 +189,16 @@ static void notify_kpb(const struct comp_dev *dev)
160189 NOTIFIER_TARGET_CORE_ALL_MASK , & cd -> event_data ,
161190 sizeof (cd -> event_data ));
162191}
192+ #endif /* CONFIG_AMS */
163193
164194void detect_test_notify (const struct comp_dev * dev )
165195{
166196 notify_host (dev );
197+ #if CONFIG_AMS
198+ ams_notify_kpb (dev );
199+ #else
167200 notify_kpb (dev );
201+ #endif
168202}
169203
170204static void default_detect_test (struct comp_dev * dev ,
@@ -719,6 +753,15 @@ static void test_keyword_free(struct comp_dev *dev)
719753
720754 comp_info (dev , "test_keyword_free()" );
721755
756+ #if CONFIG_AMS
757+ int ret ;
758+
759+ /* Unregister KD as AMS producer */
760+ ret = kpb_unregister_ams_producer (dev , cd -> ams_uuid_id );
761+ if (ret )
762+ comp_err (dev , "test_keyword_free(): unregister ams error %d" , ret );
763+ #endif
764+
722765 ipc_msg_free (cd -> msg );
723766 comp_data_blob_handler_free (cd -> model_handler );
724767 rfree (cd );
@@ -798,6 +841,10 @@ static int test_keyword_params(struct comp_dev *dev,
798841
799842 cd -> config .activation_threshold = err ;
800843
844+ #if CONFIG_AMS
845+ cd -> ams_uuid_id = AMS_INVALID_MSG_TYPE ;
846+ #endif
847+
801848 return 0 ;
802849}
803850
@@ -874,6 +921,7 @@ static int test_keyword_prepare(struct comp_dev *dev)
874921 struct comp_data * cd = comp_get_drvdata (dev );
875922 uint16_t valid_bits = cd -> sample_valid_bytes * 8 ;
876923 uint16_t sample_width ;
924+ int ret ;
877925
878926#if CONFIG_IPC_MAJOR_4
879927 sample_width = cd -> base_cfg .audio_fmt .depth ;
@@ -887,7 +935,7 @@ static int test_keyword_prepare(struct comp_dev *dev)
887935 /* Default threshold value has to be changed
888936 * according to host new format.
889937 */
890- int ret = test_keyword_get_threshold (dev , valid_bits );
938+ ret = test_keyword_get_threshold (dev , valid_bits );
891939
892940 if (ret < 0 ) {
893941 comp_err (dev , "test_keyword_prepare(): unsupported sample width %u" ,
@@ -902,6 +950,14 @@ static int test_keyword_prepare(struct comp_dev *dev)
902950 & cd -> data_blob_size ,
903951 & cd -> data_blob_crc );
904952
953+ #if CONFIG_AMS
954+ /* Register KD as AMS producer */
955+ ret = kpb_register_ams_producer (dev , & cd -> ams_payload , ams_kpd_msg_uuid ,
956+ cd -> ams_uuid_id );
957+ if (ret )
958+ return ret ;
959+ #endif
960+
905961 return comp_set_state (dev , COMP_TRIGGER_PREPARE );
906962}
907963
0 commit comments