2 * Copyright (c) 2010-2020 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.mactts.internal;
16 import java.io.FileInputStream;
17 import java.io.FileNotFoundException;
18 import java.io.IOException;
19 import java.io.InputStream;
21 import org.openhab.core.audio.AudioException;
22 import org.openhab.core.audio.AudioFormat;
23 import org.openhab.core.audio.AudioStream;
24 import org.openhab.core.audio.FixedLengthAudioStream;
25 import org.openhab.core.voice.Voice;
28 * Implementation of the {@link AudioStream} interface for the {@link MacTTSService}
30 * @author Kelly Davis - Initial contribution and API
31 * @author Kai Kreuzer - Refactored to use AudioStream and fixed audio format to produce
33 class MacTTSAudioStream extends FixedLengthAudioStream {
36 * {@link Voice} this {@link AudioStream} speaks in
38 private final Voice voice;
41 * Text spoken in this {@link AudioStream}
43 private final String text;
46 * {@link AudioFormat} of this {@link AudioStream}
48 private final AudioFormat audioFormat;
51 * The raw input stream
53 private InputStream inputStream;
59 * Constructs an instance with the passed properties.
61 * It is assumed that the passed properties have been validated.
63 * @param text The text spoken in this {@link AudioStream}
64 * @param voice The {@link Voice} used to speak this instance's text
65 * @param audioFormat The {@link AudioFormat} of this {@link AudioStream}
66 * @throws AudioException if stream cannot be created
68 public MacTTSAudioStream(String text, Voice voice, AudioFormat audioFormat) throws AudioException {
71 this.audioFormat = audioFormat;
72 this.inputStream = createInputStream();
76 public AudioFormat getFormat() {
80 private InputStream createInputStream() throws AudioException {
81 String outputFile = generateOutputFilename();
82 String command = getCommand(outputFile);
85 Process process = Runtime.getRuntime().exec(command);
87 file = new File(outputFile);
88 this.length = file.length();
89 return getFileInputStream(file);
90 } catch (IOException e) {
91 throw new AudioException("Error while executing '" + command + "'", e);
92 } catch (InterruptedException e) {
93 throw new AudioException("The '" + command + "' has been interrupted", e);
97 private InputStream getFileInputStream(File file) throws AudioException {
99 throw new IllegalArgumentException("file must not be null");
103 return new FileInputStream(file);
104 } catch (FileNotFoundException e) {
105 throw new AudioException("Cannot open temporary audio file '" + file.getName() + ".");
108 throw new AudioException("Temporary file '" + file.getName() + "' not found!");
113 * Generates a unique, absolute output filename
115 * @return Unique, absolute output filename
117 private String generateOutputFilename() throws AudioException {
120 tempFile = File.createTempFile(Integer.toString(text.hashCode()), ".wav");
121 tempFile.deleteOnExit();
122 } catch (IOException e) {
123 throw new AudioException("Unable to create temp file.", e);
125 return tempFile.getAbsolutePath();
129 * Gets the command used to generate an audio file {@code outputFile}
131 * @param outputFile The absolute filename of the command's output
132 * @return The command used to generate the audio file {@code outputFile}
134 private String getCommand(String outputFile) {
135 StringBuffer stringBuffer = new StringBuffer();
137 stringBuffer.append("say");
139 stringBuffer.append(" --voice=" + this.voice.getLabel());
140 stringBuffer.append(" --output-file=" + outputFile);
141 stringBuffer.append(" --file-format=" + this.audioFormat.getContainer());
142 stringBuffer.append(" --data-format=LEI" + audioFormat.getBitDepth() + "@" + audioFormat.getFrequency());
143 stringBuffer.append(" --channels=1"); // Mono
144 stringBuffer.append(" " + this.text);
146 return stringBuffer.toString();
150 public int read() throws IOException {
151 return inputStream.read();
155 public long length() {
160 public InputStream getClonedStream() throws AudioException {
162 return getFileInputStream(file);
164 throw new AudioException("No temporary audio file available.");