From 4ace1557f453d72571c3cedd929a0c24fe7e3ddc Mon Sep 17 00:00:00 2001 From: GiviMAD Date: Sat, 16 Dec 2023 02:22:38 -0800 Subject: [PATCH] [voice] Add interface to represent a remote keyword spotter (#3564) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miguel Álvarez --- .../org/openhab/core/voice/KSEdgeService.java | 44 +++++++++++++++++++ .../core/voice/internal/DialogProcessor.java | 11 +++-- 2 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/KSEdgeService.java diff --git a/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/KSEdgeService.java b/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/KSEdgeService.java new file mode 100644 index 000000000..959e047d4 --- /dev/null +++ b/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/KSEdgeService.java @@ -0,0 +1,44 @@ +/** + * Copyright (c) 2010-2023 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.core.voice; + +import java.util.Locale; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.openhab.core.audio.AudioStream; + +/** + * This is the interface that an edge keyword spotting service has to implement. + * Used to register a keyword spotting service that is running on a remote device. + * + * @author Miguel Álvarez - Initial contribution + */ +@NonNullByDefault +public interface KSEdgeService extends KSService { + + /** + * This method links the remote keyword spotting process to a consumer. + * + * The method is supposed to return fast. + * + * @param ksListener Non-null {@link KSListener} that {@link KSEvent} events target + * @throws KSException if any parameter is invalid or a problem occurs + */ + KSServiceHandle spot(KSListener ksListener) throws KSException; + + @Override + default KSServiceHandle spot(KSListener ksListener, AudioStream audioStream, Locale locale, String keyword) + throws KSException { + throw new KSException("An edge keyword spotter is not meant to process audio in the server"); + } +} diff --git a/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/internal/DialogProcessor.java b/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/internal/DialogProcessor.java index 2df6b01c7..625d6180c 100644 --- a/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/internal/DialogProcessor.java +++ b/bundles/org.openhab.core.voice/src/main/java/org/openhab/core/voice/internal/DialogProcessor.java @@ -34,6 +34,7 @@ import org.openhab.core.items.ItemUtil; import org.openhab.core.items.events.ItemEventFactory; import org.openhab.core.library.types.OnOffType; import org.openhab.core.voice.DialogContext; +import org.openhab.core.voice.KSEdgeService; import org.openhab.core.voice.KSErrorEvent; import org.openhab.core.voice.KSEvent; import org.openhab.core.voice.KSException; @@ -164,9 +165,13 @@ public class DialogProcessor implements KSListener, STTListener { return; } try { - AudioStream stream = dialogContext.source().getInputStream(fmt); - streamKS = stream; - ksServiceHandle = ksService.spot(this, stream, dialogContext.locale(), keyword); + if (ksService instanceof KSEdgeService) { + ((KSEdgeService) ksService).spot(this); + } else { + AudioStream stream = dialogContext.source().getInputStream(fmt); + streamKS = stream; + ksServiceHandle = ksService.spot(this, stream, dialogContext.locale(), keyword); + } playStartSound(); } catch (AudioException e) { logger.warn("Encountered audio error: {}", e.getMessage());