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.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.common.Disposable;
26 import org.openhab.core.voice.Voice;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Implementation of the {@link AudioStream} interface for the {@link MacTTSService}
33 * @author Kelly Davis - Initial contribution and API
34 * @author Kai Kreuzer - Refactored to use AudioStream and fixed audio format to produce
35 * @author Laurent Garnier - Add dispose method to delete the temporary file
37 class MacTTSAudioStream extends FixedLengthAudioStream implements Disposable {
39 private final Logger logger = LoggerFactory.getLogger(MacTTSAudioStream.class);
42 * {@link Voice} this {@link AudioStream} speaks in
44 private final Voice voice;
47 * Text spoken in this {@link AudioStream}
49 private final String text;
52 * {@link AudioFormat} of this {@link AudioStream}
54 private final AudioFormat audioFormat;
57 * The raw input stream
59 private InputStream inputStream;
65 * Constructs an instance with the passed properties.
67 * It is assumed that the passed properties have been validated.
69 * @param text The text spoken in this {@link AudioStream}
70 * @param voice The {@link Voice} used to speak this instance's text
71 * @param audioFormat The {@link AudioFormat} of this {@link AudioStream}
72 * @throws AudioException if stream cannot be created
74 public MacTTSAudioStream(String text, Voice voice, AudioFormat audioFormat) throws AudioException {
77 this.audioFormat = audioFormat;
78 this.inputStream = createInputStream();
82 public AudioFormat getFormat() {
86 private InputStream createInputStream() throws AudioException {
87 String outputFile = generateOutputFilename();
88 String command = getCommand(outputFile);
89 logger.debug("Executing on command line: {}", command);
92 Process process = Runtime.getRuntime().exec(command);
94 file = new File(outputFile);
96 throw new AudioException("Generated file '" + outputFile + "' does not exist.'");
98 this.length = file.length();
99 if (this.length == 0) {
100 throw new AudioException("Generated file '" + outputFile + "' has no content.'");
102 return getFileInputStream(file);
103 } catch (IOException e) {
104 throw new AudioException("Error while executing '" + command + "'", e);
105 } catch (InterruptedException e) {
106 throw new AudioException("The '" + command + "' has been interrupted", e);
110 private InputStream getFileInputStream(File file) throws AudioException {
112 throw new IllegalArgumentException("file must not be null");
116 return new FileInputStream(file);
117 } catch (FileNotFoundException e) {
118 throw new AudioException("Cannot open temporary audio file '" + file.getName() + ".");
121 throw new AudioException("Temporary file '" + file.getName() + "' not found!");
126 * Generates a unique, absolute output filename
128 * @return Unique, absolute output filename
130 private String generateOutputFilename() throws AudioException {
133 tempFile = File.createTempFile(Integer.toString(text.hashCode()), ".wav");
134 tempFile.deleteOnExit();
135 } catch (IOException e) {
136 throw new AudioException("Unable to create temp file.", e);
138 return tempFile.getAbsolutePath();
142 * Gets the command used to generate an audio file {@code outputFile}
144 * @param outputFile The absolute filename of the command's output
145 * @return The command used to generate the audio file {@code outputFile}
147 private String getCommand(String outputFile) {
148 StringBuffer stringBuffer = new StringBuffer();
150 stringBuffer.append("say");
152 stringBuffer.append(" --voice=" + this.voice.getLabel());
153 stringBuffer.append(" --output-file=" + outputFile);
154 stringBuffer.append(" --file-format=" + this.audioFormat.getContainer());
155 stringBuffer.append(" --data-format=LEI" + audioFormat.getBitDepth() + "@" + audioFormat.getFrequency());
156 stringBuffer.append(" --channels=1"); // Mono
157 stringBuffer.append(" " + this.text);
159 return stringBuffer.toString();
163 public int read() throws IOException {
164 return inputStream.read();
168 public long length() {
173 public InputStream getClonedStream() throws AudioException {
175 return getFileInputStream(file);
177 throw new AudioException("No temporary audio file available.");
182 public void dispose() throws IOException {
183 if (file != null && file.exists()) {
185 if (!file.delete()) {
186 logger.warn("Failed to delete the file {}", file.getAbsolutePath());
188 } catch (SecurityException e) {
189 logger.warn("Failed to delete the file {}: {}", file.getAbsolutePath(), e.getMessage());