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

adapted MacTTS to use FixedLengthAudioStream #2311

Merged
merged 3 commits into from
Oct 13, 2016
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,33 @@ 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);
if (file != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new File(...) should never return null but always a reference (if no exception is thrown by the construction).

If the file does not exist 0L is returned, so I think we do not need any check at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, even better. Updated.

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 +147,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.");
}
}
}