Skip to content

Commit

Permalink
[tts] Make the LRU media cache generic
Browse files Browse the repository at this point in the history
Signed-off-by: Gwendal Roulleau <gwendal.roulleau@gmail.com>
  • Loading branch information
dalgwen committed Feb 6, 2023
1 parent d28f1c9 commit fea3693
Show file tree
Hide file tree
Showing 17 changed files with 1,822 additions and 1,535 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,5 @@ public interface TTSCache {
* are not supported or another error occurs while creating an
* {@link AudioStream}
*/
AudioStream get(CachedTTSService tts, String text, Voice voice, AudioFormat requestedFormat)
throws TTSException;
AudioStream get(CachedTTSService tts, String text, Voice voice, AudioFormat requestedFormat) throws TTSException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.voice.internal.cache;

import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.audio.AudioFormat;

/**
* Serializable AudioFormat storage class
* We cannot use a record yet (requires Gson v2.10)
*
* @author Gwendal Roulleau - Initial contribution
*/
public class AudioFormatInfo {
public final @Nullable Boolean bigEndian;
public final @Nullable Integer bitDepth;
public final @Nullable Integer bitRate;
public final @Nullable Long frequency;
public final @Nullable Integer channels;
public final @Nullable String codec;
public final @Nullable String container;

public AudioFormatInfo(String text, @Nullable Boolean bigEndian, @Nullable Integer bitDepth,
@Nullable Integer bitRate, @Nullable Long frequency, @Nullable Integer channels, @Nullable String codec,
@Nullable String container) {
super();
this.bigEndian = bigEndian;
this.bitDepth = bitDepth;
this.bitRate = bitRate;
this.frequency = frequency;
this.channels = channels;
this.codec = codec;
this.container = container;
}

public AudioFormatInfo(AudioFormat audioFormat) {
this.bigEndian = audioFormat.isBigEndian();
this.bitDepth = audioFormat.getBitDepth();
this.bitRate = audioFormat.getBitRate();
this.frequency = audioFormat.getFrequency();
this.channels = audioFormat.getChannels();
this.codec = audioFormat.getCodec();
this.container = audioFormat.getContainer();
}

public AudioFormat toAudioFormat() {
return new AudioFormat(container, codec, bigEndian, bitDepth, bitRate, frequency, channels);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.voice.internal.cache;

import java.io.IOException;
import java.io.InputStream;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.audio.AudioException;
import org.openhab.core.audio.AudioFormat;
import org.openhab.core.audio.FixedLengthAudioStream;
import org.openhab.core.cache.lru.InputStreamCacheWrapper;

/**
* Implements AudioStream methods, with an inner stream extracted from cache
*
* @author Gwendal Roulleau - Initial contribution
*/
@NonNullByDefault
public class AudioStreamFromCache extends FixedLengthAudioStream {

private InputStreamCacheWrapper inputStream;
private AudioFormat audioFormat;

public AudioStreamFromCache(InputStreamCacheWrapper inputStream, AudioFormatInfo audioFormat) {
this.inputStream = inputStream;
this.audioFormat = audioFormat.toAudioFormat();
}

@Override
public int read() throws IOException {
return inputStream.read();
}

@Override
public int read(byte @Nullable [] b, int off, int len) throws IOException {
return inputStream.read(b, off, len);
}

@Override
public AudioFormat getFormat() {
return audioFormat;
}

@Override
public long length() {
return inputStream.length();
}

@Override
public InputStream getClonedStream() throws AudioException {
try {
return inputStream.getClonedStream();
} catch (IOException e) {
throw new AudioException("Cannot get cloned AudioStream", e);
}
}

@Override
public void close() throws IOException {
inputStream.close();
}

@Override
public long skip(long n) throws IOException {
return inputStream.skip(n);
}

@Override
public void skipNBytes(long n) throws IOException {
inputStream.skipNBytes(n);
}

@Override
public int available() throws IOException {
return inputStream.available();
}

@Override
public synchronized void mark(int readlimit) {
inputStream.mark(readlimit);
}

@Override
public synchronized void reset() throws IOException {
inputStream.reset();
}

@Override
public boolean markSupported() {
return inputStream.markSupported();
}
}
Loading

0 comments on commit fea3693

Please sign in to comment.