2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.voice.voicerss.internal;
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;
21 import java.util.HashMap;
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;
33 * Tests for {@link VoiceRSSTTSService}.
35 * @author Andreas Brenk - Initial contribution
37 public class VoiceRSSTTSServiceTest {
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);
50 private StorageService storageService;
53 * The {@link VoiceRSSTTSService} under test.
55 private TTSService ttsService;
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);
65 this.ttsService = ttsService;
69 public void testSupportedFormats() {
70 final Set<AudioFormat> supportedFormats = ttsService.getSupportedFormats();
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)));
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)));
85 // check specific formats with additional constraints
86 assertThat(supportedFormats, hasItem(compatibleAudioFormat(bitRate(WAV, 705_600)))); // 44.1 kHz 16-bit
88 // check unsupported formats
89 assertThat(supportedFormats, not(hasItem(compatibleAudioFormat(bitDepth(WAV, 24)))));
92 private AudioFormat bitDepth(AudioFormat format, Integer bitDepth) {
93 return new AudioFormat(format.getContainer(), format.getCodec(), format.isBigEndian(), bitDepth,
94 format.getBitRate(), format.getFrequency());
97 private AudioFormat bitRate(AudioFormat format, Integer bitRate) {
98 return new AudioFormat(format.getContainer(), format.getCodec(), format.isBigEndian(), format.getBitDepth(),
99 bitRate, format.getFrequency());