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