Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
adapted MacTTS to use FixedLengthAudioStream (#2311)
Browse files Browse the repository at this point in the history
* adapted MacTTS to use FixedLengthAudioStream

Signed-off-by: Kai Kreuzer <kai@openhab.org>

* set length only once

Signed-off-by: Kai Kreuzer <kai@openhab.org>

* removed null check

Signed-off-by: Kai Kreuzer <kai@openhab.org>
  • Loading branch information
kaikreuzer authored and maggu2810 committed Oct 13, 2016
1 parent b81d33e commit 01fc899
Showing 1 changed file with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.eclipse.smarthome.core.audio.AudioException;
import org.eclipse.smarthome.core.audio.AudioFormat;
import org.eclipse.smarthome.core.audio.AudioStream;
import org.eclipse.smarthome.core.audio.FixedLengthAudioStream;
import org.eclipse.smarthome.core.voice.Voice;

/**
Expand All @@ -23,7 +25,7 @@
* @author Kelly Davis - Initial contribution and API
* @author Kai Kreuzer - Refactored to use AudioStream and fixed audio format to produce
*/
class MacTTSAudioStream extends AudioStream {
class MacTTSAudioStream extends FixedLengthAudioStream {

/**
* {@link Voice} this {@link AudioStream} speaks in
Expand All @@ -45,6 +47,9 @@ class MacTTSAudioStream extends AudioStream {
*/
private InputStream inputStream;

private long length;
private File file;

/**
* Constructs an instance with the passed properties.
*
Expand Down Expand Up @@ -74,19 +79,31 @@ private InputStream createInputStream() throws AudioException {
try {
Process process = Runtime.getRuntime().exec(command);
process.waitFor();
File file = new File(outputFile);
if (file.exists()) {
return new FileInputStream(file);
} else {
throw new AudioException("Temporary file '" + outputFile + "' not found!");
}
file = new File(outputFile);
this.length = file.length();
return getFileInputStream(file);
} catch (IOException e) {
throw new AudioException("Error while executing '" + command + "'", e);
} catch (InterruptedException e) {
throw new AudioException("The '" + command + "' has been interrupted", e);
}
}

private InputStream getFileInputStream(File file) throws AudioException {
if (file == null) {
throw new IllegalArgumentException("file must not be null");
}
if (file.exists()) {
try {
return new FileInputStream(file);
} catch (FileNotFoundException e) {
throw new AudioException("Cannot open temporary audio file '" + file.getName() + ".");
}
} else {
throw new AudioException("Temporary file '" + file.getName() + "' not found!");
}
}

/**
* Generates a unique, absolute output filename
*
Expand Down Expand Up @@ -128,4 +145,18 @@ private String getCommand(String outputFile) {
public int read() throws IOException {
return inputStream.read();
}

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

@Override
public InputStream getClonedStream() throws AudioException {
if (file != null) {
return getFileInputStream(file);
} else {
throw new AudioException("No temporary audio file available.");
}
}
}

0 comments on commit 01fc899

Please sign in to comment.