]> git.basschouten.com Git - openhab-addons.git/blob
a2b5ce9bad70eb48755b58f1d2d47cd2be727ed6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.voicerss.internal;
14
15 import static org.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.core.IsIterableContaining.hasItem;
17 import static org.hamcrest.core.IsNot.not;
18 import static org.openhab.core.audio.AudioFormat.*;
19 import static org.openhab.voice.voicerss.internal.CompatibleAudioFormatMatcher.compatibleAudioFormat;
20
21 import java.util.Set;
22
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.openhab.core.audio.AudioFormat;
26 import org.openhab.core.voice.TTSService;
27
28 /**
29  * Tests for {@link VoiceRSSTTSService}.
30  *
31  * @author Andreas Brenk - Initial contribution
32  */
33 public class VoiceRSSTTSServiceTest {
34
35     private static final AudioFormat MP3_44KHZ_16BIT = new AudioFormat(AudioFormat.CONTAINER_NONE,
36             AudioFormat.CODEC_MP3, null, 16, null, 44_100L);
37     private static final AudioFormat OGG_44KHZ_16BIT = new AudioFormat(AudioFormat.CONTAINER_OGG,
38             AudioFormat.CODEC_VORBIS, null, 16, null, 44_100L);
39     private static final AudioFormat AAC_44KHZ_16BIT = new AudioFormat(AudioFormat.CONTAINER_NONE,
40             AudioFormat.CODEC_MP3, null, 16, null, 44_100L);
41     private static final AudioFormat WAV_22KHZ_8BIT = new AudioFormat(AudioFormat.CONTAINER_WAVE,
42             AudioFormat.CODEC_PCM_UNSIGNED, null, 8, null, 22_050L);
43     private static final AudioFormat WAV_48KHZ_16BIT = new AudioFormat(AudioFormat.CONTAINER_WAVE,
44             AudioFormat.CODEC_PCM_SIGNED, false, 16, null, 48_000L);
45
46     /**
47      * The {@link VoiceRSSTTSService} under test.
48      */
49     private TTSService ttsService;
50
51     @BeforeEach
52     public void setUp() {
53         final VoiceRSSTTSService ttsService = new VoiceRSSTTSService();
54         ttsService.activate(null);
55
56         this.ttsService = ttsService;
57     }
58
59     @Test
60     public void testSupportedFormats() {
61         final Set<AudioFormat> supportedFormats = ttsService.getSupportedFormats();
62
63         // check generic formats without any further constraints
64         assertThat(supportedFormats, hasItem(compatibleAudioFormat(MP3)));
65         assertThat(supportedFormats, hasItem(compatibleAudioFormat(WAV)));
66         assertThat(supportedFormats, hasItem(compatibleAudioFormat(OGG)));
67         assertThat(supportedFormats, hasItem(compatibleAudioFormat(AAC)));
68
69         // check specific formats with common constraints
70         assertThat(supportedFormats, hasItem(compatibleAudioFormat(MP3_44KHZ_16BIT)));
71         assertThat(supportedFormats, hasItem(compatibleAudioFormat(OGG_44KHZ_16BIT)));
72         assertThat(supportedFormats, hasItem(compatibleAudioFormat(AAC_44KHZ_16BIT)));
73         assertThat(supportedFormats, hasItem(compatibleAudioFormat(WAV_22KHZ_8BIT)));
74         assertThat(supportedFormats, hasItem(compatibleAudioFormat(WAV_48KHZ_16BIT)));
75
76         // check specific formats with additional constraints
77         assertThat(supportedFormats, hasItem(compatibleAudioFormat(bitRate(WAV, 705_600)))); // 44.1 kHz 16-bit
78
79         // check unsupported formats
80         assertThat(supportedFormats, not(hasItem(compatibleAudioFormat(bitDepth(WAV, 24)))));
81     }
82
83     private AudioFormat bitDepth(AudioFormat format, Integer bitDepth) {
84         return new AudioFormat(format.getContainer(), format.getCodec(), format.isBigEndian(), bitDepth,
85                 format.getBitRate(), format.getFrequency());
86     }
87
88     private AudioFormat bitRate(AudioFormat format, Integer bitRate) {
89         return new AudioFormat(format.getContainer(), format.getCodec(), format.isBigEndian(), format.getBitDepth(),
90                 bitRate, format.getFrequency());
91     }
92 }