Search in sources :

Example 1 with TTSService

use of org.openhab.core.voice.TTSService in project openhab-core by openhab.

the class SayCommandTest method testSayCommand.

@ParameterizedTest
@MethodSource("data")
public void testSayCommand(boolean shouldItemsBePassed, boolean shouldItemsBeRegistered, boolean shouldMultipleItemsBeRegistered, @Nullable TTSService defaultTTSService, boolean ttsServiceMockShouldBeRegistered, boolean shouldStreamBeExpected) throws IOException {
    String[] methodParameters = new String[2];
    methodParameters[0] = SUBCMD_SAY;
    if (defaultTTSService != null) {
        ConfigurationAdmin configAdmin = super.getService(ConfigurationAdmin.class);
        Dictionary<String, Object> audioConfig = new Hashtable<>();
        audioConfig.put("defaultSink", sink.getId());
        Configuration configuration = configAdmin.getConfiguration("org.openhab.audio", null);
        configuration.update(audioConfig);
        Dictionary<String, Object> voiceConfig = new Hashtable<>();
        voiceConfig.put(CONFIG_DEFAULT_TTS, defaultTTSService);
        configuration = configAdmin.getConfiguration(VoiceManagerImpl.CONFIGURATION_PID);
        configuration.update(voiceConfig);
    }
    TTSService ttsService = SayCommandTest.ttsService;
    if (ttsServiceMockShouldBeRegistered && ttsService != null) {
        registerService(ttsService);
    }
    if (shouldItemsBePassed) {
        VolatileStorageService volatileStorageService = new VolatileStorageService();
        registerService(volatileStorageService);
        ManagedThingProvider managedThingProvider = getService(ThingProvider.class, ManagedThingProvider.class);
        assertNotNull(managedThingProvider);
        ItemRegistry itemRegistry = getService(ItemRegistry.class);
        assertNotNull(itemRegistry);
        Item item = new StringItem("itemName");
        if (shouldItemsBeRegistered) {
            itemRegistry.add(item);
        }
        methodParameters[1] = "%" + item.getName() + "%";
        if (shouldMultipleItemsBeRegistered) {
            Item item1 = new StringItem("itemName1");
            itemRegistry.add(item1);
            Item item2 = new StringItem("itemName2");
            itemRegistry.add(item2);
            Item item3 = new StringItem("itemName3");
            itemRegistry.add(item3);
            methodParameters[1] = "%itemName.%";
        }
    } else {
        methodParameters[1] = "hello";
    }
    extensionService.execute(methodParameters, console);
    assertThat(sink.isStreamProcessed(), is(shouldStreamBeExpected));
}
Also used : Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable) TTSService(org.openhab.core.voice.TTSService) ItemRegistry(org.openhab.core.items.ItemRegistry) StringItem(org.openhab.core.library.items.StringItem) Item(org.openhab.core.items.Item) StringItem(org.openhab.core.library.items.StringItem) ManagedThingProvider(org.openhab.core.thing.ManagedThingProvider) VolatileStorageService(org.openhab.core.test.storage.VolatileStorageService) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 2 with TTSService

use of org.openhab.core.voice.TTSService in project openhab-core by openhab.

the class VoiceResource method startDialog.

@POST
@Path("/dialog/start")
@Consumes(MediaType.TEXT_PLAIN)
@Operation(operationId = "startDialog", summary = "Start dialog processing for a given audio source.", responses = { @ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "404", description = "One of the given ids is wrong."), @ApiResponse(responseCode = "400", description = "Services are missing or language is not supported by services or dialog processing is already started for the audio source.") })
public Response startDialog(@HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) @Parameter(description = "language") @Nullable String language, @QueryParam("sourceId") @Parameter(description = "source ID") @Nullable String sourceId, @QueryParam("ksId") @Parameter(description = "keywork spotter ID") @Nullable String ksId, @QueryParam("sttId") @Parameter(description = "Speech-to-Text ID") @Nullable String sttId, @QueryParam("ttsId") @Parameter(description = "Text-to-Speech ID") @Nullable String ttsId, @QueryParam("hliId") @Parameter(description = "interpreter ID") @Nullable String hliId, @QueryParam("sinkId") @Parameter(description = "audio sink ID") @Nullable String sinkId, @QueryParam("keyword") @Parameter(description = "keyword") @Nullable String keyword, @QueryParam("listeningItem") @Parameter(description = "listening item") @Nullable String listeningItem) {
    AudioSource source = null;
    if (sourceId != null) {
        source = audioManager.getSource(sourceId);
        if (source == null) {
            return JSONResponse.createErrorResponse(Status.NOT_FOUND, "Audio source not found");
        }
    }
    KSService ks = null;
    if (ksId != null) {
        ks = voiceManager.getKS(ksId);
        if (ks == null) {
            return JSONResponse.createErrorResponse(Status.NOT_FOUND, "Keyword spotter not found");
        }
    }
    STTService stt = null;
    if (sttId != null) {
        stt = voiceManager.getSTT(sttId);
        if (stt == null) {
            return JSONResponse.createErrorResponse(Status.NOT_FOUND, "Speech-to-Text not found");
        }
    }
    TTSService tts = null;
    if (ttsId != null) {
        tts = voiceManager.getTTS(ttsId);
        if (tts == null) {
            return JSONResponse.createErrorResponse(Status.NOT_FOUND, "Text-to-Speech not found");
        }
    }
    HumanLanguageInterpreter hli = null;
    if (hliId != null) {
        hli = voiceManager.getHLI(hliId);
        if (hli == null) {
            return JSONResponse.createErrorResponse(Status.NOT_FOUND, "Interpreter not found");
        }
    }
    AudioSink sink = null;
    if (sinkId != null) {
        sink = audioManager.getSink(sinkId);
        if (sink == null) {
            return JSONResponse.createErrorResponse(Status.NOT_FOUND, "Audio sink not found");
        }
    }
    final Locale locale = localeService.getLocale(language);
    try {
        voiceManager.startDialog(ks, stt, tts, hli, source, sink, locale, keyword, listeningItem);
        return Response.ok(null, MediaType.TEXT_PLAIN).build();
    } catch (IllegalStateException e) {
        return JSONResponse.createErrorResponse(Status.BAD_REQUEST, e.getMessage());
    }
}
Also used : AudioSource(org.openhab.core.audio.AudioSource) AudioSink(org.openhab.core.audio.AudioSink) Locale(java.util.Locale) STTService(org.openhab.core.voice.STTService) KSService(org.openhab.core.voice.KSService) TTSService(org.openhab.core.voice.TTSService) HumanLanguageInterpreter(org.openhab.core.voice.text.HumanLanguageInterpreter) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Operation(io.swagger.v3.oas.annotations.Operation)

Example 3 with TTSService

use of org.openhab.core.voice.TTSService in project openhab-core by openhab.

the class VoiceManagerImpl method getVoice.

@Nullable
private Voice getVoice(String id) {
    if (id.contains(":")) {
        // it is a fully qualified unique id
        String[] segments = id.split(":");
        TTSService tts = getTTS(segments[0]);
        if (tts != null) {
            return getVoice(tts.getAvailableVoices(), segments[1]);
        }
    } else {
        // voiceId is not fully qualified
        TTSService tts = getTTS();
        if (tts != null) {
            return getVoice(tts.getAvailableVoices(), id);
        }
    }
    return null;
}
Also used : TTSService(org.openhab.core.voice.TTSService) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 4 with TTSService

use of org.openhab.core.voice.TTSService in project openhab-core by openhab.

the class VoiceManagerImpl method startDialog.

@Override
public void startDialog(@Nullable KSService ks, @Nullable STTService stt, @Nullable TTSService tts, @Nullable HumanLanguageInterpreter hli, @Nullable AudioSource source, @Nullable AudioSink sink, @Nullable Locale locale, @Nullable String keyword, @Nullable String listeningItem) throws IllegalStateException {
    // use defaults, if null
    KSService ksService = (ks == null) ? getKS() : ks;
    STTService sttService = (stt == null) ? getSTT() : stt;
    TTSService ttsService = (tts == null) ? getTTS() : tts;
    HumanLanguageInterpreter interpreter = (hli == null) ? getHLI() : hli;
    AudioSource audioSource = (source == null) ? audioManager.getSource() : source;
    AudioSink audioSink = (sink == null) ? audioManager.getSink() : sink;
    Locale loc = (locale == null) ? localeProvider.getLocale() : locale;
    String kw = (keyword == null) ? this.keyword : keyword;
    String item = (listeningItem == null) ? this.listeningItem : listeningItem;
    Bundle b = bundle;
    if (ksService == null || sttService == null || ttsService == null || interpreter == null || audioSource == null || audioSink == null || b == null) {
        throw new IllegalStateException("Cannot start dialog as services are missing.");
    } else if (!checkLocales(ksService.getSupportedLocales(), loc) || !checkLocales(sttService.getSupportedLocales(), loc) || !checkLocales(interpreter.getSupportedLocales(), loc)) {
        throw new IllegalStateException("Cannot start dialog as provided locale is not supported by all services.");
    } else {
        DialogProcessor processor = dialogProcessors.get(audioSource.getId());
        if (processor == null) {
            logger.debug("Starting a new dialog for source {} ({})", audioSource.getLabel(null), audioSource.getId());
            processor = new DialogProcessor(ksService, sttService, ttsService, interpreter, audioSource, audioSink, loc, kw, item, this.eventPublisher, this.i18nProvider, b);
            dialogProcessors.put(audioSource.getId(), processor);
            processor.start();
        } else {
            throw new IllegalStateException(String.format("Cannot start dialog as a dialog is already started for audio source '%s'.", audioSource.getLabel(null)));
        }
    }
}
Also used : AudioSource(org.openhab.core.audio.AudioSource) AudioSink(org.openhab.core.audio.AudioSink) Locale(java.util.Locale) STTService(org.openhab.core.voice.STTService) Bundle(org.osgi.framework.Bundle) KSService(org.openhab.core.voice.KSService) TTSService(org.openhab.core.voice.TTSService) HumanLanguageInterpreter(org.openhab.core.voice.text.HumanLanguageInterpreter)

Example 5 with TTSService

use of org.openhab.core.voice.TTSService in project openhab-core by openhab.

the class VoiceConsoleCommandExtension method execute.

@Override
public void execute(String[] args, Console console) {
    if (args.length > 0) {
        String subCommand = args[0];
        switch(subCommand) {
            case SUBCMD_SAY:
                if (args.length > 1) {
                    say(Arrays.copyOfRange(args, 1, args.length), console);
                } else {
                    console.println("Specify text to say (e.g. 'say hello')");
                }
                return;
            case SUBCMD_INTERPRET:
                if (args.length > 1) {
                    interpret(Arrays.copyOfRange(args, 1, args.length), console);
                } else {
                    console.println("Specify text to interpret (e.g. 'interpret turn all lights off')");
                }
                return;
            case SUBCMD_VOICES:
                Locale locale = localeProvider.getLocale();
                Voice defaultVoice = getDefaultVoice();
                for (Voice voice : voiceManager.getAllVoices()) {
                    TTSService ttsService = voiceManager.getTTS(voice.getUID().split(":")[0]);
                    if (ttsService != null) {
                        console.println(String.format("%s %s - %s - %s (%s)", voice.equals(defaultVoice) ? "*" : " ", ttsService.getLabel(locale), voice.getLocale().getDisplayName(locale), voice.getLabel(), voice.getUID()));
                    }
                }
                return;
            case SUBCMD_START_DIALOG:
                try {
                    AudioSource source = args.length < 2 ? null : audioManager.getSource(args[1]);
                    AudioSink sink = args.length < 3 ? null : audioManager.getSink(args[2]);
                    HumanLanguageInterpreter hli = args.length < 4 ? null : voiceManager.getHLI(args[3]);
                    TTSService tts = args.length < 5 ? null : voiceManager.getTTS(args[4]);
                    STTService stt = args.length < 6 ? null : voiceManager.getSTT(args[5]);
                    KSService ks = args.length < 7 ? null : voiceManager.getKS(args[6]);
                    String keyword = args.length < 8 ? null : args[7];
                    voiceManager.startDialog(ks, stt, tts, hli, source, sink, null, keyword, null);
                } catch (IllegalStateException e) {
                    console.println(Objects.requireNonNullElse(e.getMessage(), "An error occurred while starting the dialog"));
                }
                break;
            case SUBCMD_STOP_DIALOG:
                try {
                    voiceManager.stopDialog(args.length < 2 ? null : audioManager.getSource(args[1]));
                } catch (IllegalStateException e) {
                    console.println(Objects.requireNonNullElse(e.getMessage(), "An error occurred while stopping the dialog"));
                }
                break;
            case SUBCMD_INTERPRETERS:
                listInterpreters(console);
                return;
            case SUBCMD_KEYWORD_SPOTTERS:
                listKeywordSpotters(console);
                return;
            case SUBCMD_STT_SERVICES:
                listSTTs(console);
                return;
            case SUBCMD_TTS_SERVICES:
                listTTSs(console);
                return;
            default:
                break;
        }
    } else {
        printUsage(console);
    }
}
Also used : Locale(java.util.Locale) AudioSource(org.openhab.core.audio.AudioSource) AudioSink(org.openhab.core.audio.AudioSink) STTService(org.openhab.core.voice.STTService) KSService(org.openhab.core.voice.KSService) TTSService(org.openhab.core.voice.TTSService) HumanLanguageInterpreter(org.openhab.core.voice.text.HumanLanguageInterpreter) Voice(org.openhab.core.voice.Voice)

Aggregations

TTSService (org.openhab.core.voice.TTSService)8 Locale (java.util.Locale)6 AudioSink (org.openhab.core.audio.AudioSink)5 AudioSource (org.openhab.core.audio.AudioSource)5 KSService (org.openhab.core.voice.KSService)5 STTService (org.openhab.core.voice.STTService)5 HumanLanguageInterpreter (org.openhab.core.voice.text.HumanLanguageInterpreter)5 Nullable (org.eclipse.jdt.annotation.Nullable)2 Voice (org.openhab.core.voice.Voice)2 Operation (io.swagger.v3.oas.annotations.Operation)1 IOException (java.io.IOException)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Hashtable (java.util.Hashtable)1 LinkedHashSet (java.util.LinkedHashSet)1