]> git.basschouten.com Git - openhab-addons.git/blob
442f4acdc5cf3c0e2609eb3a727262519f4f3a73
[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.Collections;
16 import java.util.Locale;
17 import java.util.Set;
18 import java.util.stream.Collectors;
19 import java.util.stream.Stream;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.core.audio.AudioException;
24 import org.openhab.core.audio.AudioFormat;
25 import org.openhab.core.audio.AudioStream;
26 import org.openhab.core.voice.TTSException;
27 import org.openhab.core.voice.TTSService;
28 import org.openhab.core.voice.Voice;
29 import org.osgi.service.component.annotations.Component;
30
31 /**
32  * @author Florian Schmidt - Initial Contribution
33  */
34 @Component
35 @NonNullByDefault
36 public class PicoTTSService implements TTSService {
37     private final Set<Voice> voices = Stream
38             .of(new PicoTTSVoice("de-DE"), new PicoTTSVoice("en-US"), new PicoTTSVoice("en-GB"),
39                     new PicoTTSVoice("es-ES"), new PicoTTSVoice("fr-FR"), new PicoTTSVoice("it-IT"))
40             .collect(Collectors.toSet());
41
42     private final Set<AudioFormat> audioFormats = Collections.singleton(
43             new AudioFormat(AudioFormat.CONTAINER_WAVE, AudioFormat.CODEC_PCM_SIGNED, false, 16, null, 16000L));
44
45     @Override
46     public Set<Voice> getAvailableVoices() {
47         return this.voices;
48     }
49
50     @Override
51     public Set<AudioFormat> getSupportedFormats() {
52         return this.audioFormats;
53     }
54
55     @Override
56     public AudioStream synthesize(String text, Voice voice, AudioFormat requestedFormat) throws TTSException {
57         if (text.isEmpty()) {
58             throw new TTSException("The passed text can not be empty");
59         }
60
61         if (!this.voices.contains(voice)) {
62             throw new TTSException("The passed voice is unsupported");
63         }
64
65         boolean isAudioFormatSupported = this.audioFormats.stream().anyMatch(audioFormat -> {
66             return audioFormat.isCompatible(requestedFormat);
67         });
68
69         if (!isAudioFormatSupported) {
70             throw new TTSException("The passed AudioFormat is unsupported");
71         }
72
73         try {
74             return new PicoTTSAudioStream(text, voice, requestedFormat);
75         } catch (AudioException e) {
76             throw new TTSException(e);
77         }
78     }
79
80     @Override
81     public String getId() {
82         return "picotts";
83     }
84
85     @Override
86     public String getLabel(@Nullable Locale locale) {
87         return "PicoTTS";
88     }
89 }