]> git.basschouten.com Git - openhab-addons.git/blob
aadbd97611b8823ac530ab197c4c6be0a2ea1cc1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.voice.picotts.internal;
14
15 import java.util.Locale;
16 import java.util.Set;
17 import java.util.stream.Collectors;
18 import java.util.stream.Stream;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.audio.AudioException;
23 import org.openhab.core.audio.AudioFormat;
24 import org.openhab.core.audio.AudioStream;
25 import org.openhab.core.voice.AbstractCachedTTSService;
26 import org.openhab.core.voice.TTSCache;
27 import org.openhab.core.voice.TTSException;
28 import org.openhab.core.voice.TTSService;
29 import org.openhab.core.voice.Voice;
30 import org.osgi.service.component.annotations.Activate;
31 import org.osgi.service.component.annotations.Component;
32 import org.osgi.service.component.annotations.Reference;
33
34 /**
35  * @author Florian Schmidt - Initial Contribution
36  */
37 @Component(service = TTSService.class)
38 @NonNullByDefault
39 public class PicoTTSService extends AbstractCachedTTSService {
40
41     @Activate
42     public PicoTTSService(@Reference TTSCache ttsCache) {
43         super(ttsCache);
44     }
45
46     private final Set<Voice> voices = Stream
47             .of(new PicoTTSVoice("de-DE"), new PicoTTSVoice("en-US"), new PicoTTSVoice("en-GB"),
48                     new PicoTTSVoice("es-ES"), new PicoTTSVoice("fr-FR"), new PicoTTSVoice("it-IT"))
49             .collect(Collectors.toSet());
50
51     private final Set<AudioFormat> audioFormats = Set
52             .of(new AudioFormat(AudioFormat.CONTAINER_WAVE, AudioFormat.CODEC_PCM_SIGNED, false, 16, null, 16000L));
53
54     @Override
55     public Set<Voice> getAvailableVoices() {
56         return this.voices;
57     }
58
59     @Override
60     public Set<AudioFormat> getSupportedFormats() {
61         return this.audioFormats;
62     }
63
64     @Override
65     public AudioStream synthesizeForCache(String text, Voice voice, AudioFormat requestedFormat) throws TTSException {
66         if (text.isEmpty()) {
67             throw new TTSException("The passed text can not be empty");
68         }
69
70         if (!this.voices.contains(voice)) {
71             throw new TTSException("The passed voice is unsupported");
72         }
73
74         boolean isAudioFormatSupported = this.audioFormats.stream()
75                 .anyMatch(audioFormat -> audioFormat.isCompatible(requestedFormat));
76
77         if (!isAudioFormatSupported) {
78             throw new TTSException("The passed AudioFormat is unsupported");
79         }
80
81         try {
82             return new PicoTTSAudioStream(text, voice, requestedFormat);
83         } catch (AudioException e) {
84             throw new TTSException(e);
85         }
86     }
87
88     @Override
89     public String getId() {
90         return "picotts";
91     }
92
93     @Override
94     public String getLabel(@Nullable Locale locale) {
95         return "PicoTTS";
96     }
97 }