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