[voice] Add interface to represent a remote keyword spotter (#3564)

Signed-off-by: Miguel Álvarez <miguelwork92@gmail.com>
This commit is contained in:
GiviMAD 2023-12-16 02:22:38 -08:00 committed by GitHub
parent c8a6cf2603
commit 4ace1557f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 3 deletions

View File

@ -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");
}
}

View File

@ -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());