-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[mimictts] Fix ssml and playing from audiosinks using the audio servl…
…et (#14120) * [mimictts] Fix ssml and playing from an audiosink using the audio servlet Fix : - ssml not working - add an option to store the audio on a file before sending it to openhab. It enables audiosink based on the audio servlet to play the sound (the servlet requires the getClonedStream method, unavailable with a pure streaming approach). The files are stored in the user data directory and deleted as soon as possible (stream close detection). - fix error with voice name not encoded Signed-off-by: Gwendal Roulleau <[email protected]>
- Loading branch information
Showing
6 changed files
with
157 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...ce.mimictts/src/main/java/org/openhab/voice/mimic/internal/AutoDeleteFileAudioStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* 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.voice.mimic.internal; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.openhab.core.audio.AudioException; | ||
import org.openhab.core.audio.AudioFormat; | ||
import org.openhab.core.audio.FileAudioStream; | ||
|
||
/** | ||
* A FileAudioStream that autodelete after it and its clone are closed | ||
* Useful to not congest temporary directory | ||
* | ||
* @author Gwendal Roulleau - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class AutoDeleteFileAudioStream extends FileAudioStream { | ||
|
||
private final File file; | ||
private final AudioFormat audioFormat; | ||
private final List<ClonedFileInputStream> clonedAudioStreams = new ArrayList<>(1); | ||
private boolean isOpen = true; | ||
|
||
public AutoDeleteFileAudioStream(File file, AudioFormat format) throws AudioException { | ||
super(file, format); | ||
this.file = file; | ||
this.audioFormat = format; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
super.close(); | ||
this.isOpen = false; | ||
deleteIfPossible(); | ||
} | ||
|
||
protected void deleteIfPossible() { | ||
boolean aClonedStreamIsOpen = clonedAudioStreams.stream().anyMatch(as -> as.isOpen); | ||
if (!isOpen && !aClonedStreamIsOpen) { | ||
file.delete(); | ||
} | ||
} | ||
|
||
@Override | ||
public InputStream getClonedStream() throws AudioException { | ||
ClonedFileInputStream clonedInputStream = new ClonedFileInputStream(this, file, audioFormat); | ||
clonedAudioStreams.add(clonedInputStream); | ||
return clonedInputStream; | ||
} | ||
|
||
private static class ClonedFileInputStream extends FileAudioStream { | ||
protected boolean isOpen = true; | ||
private final AutoDeleteFileAudioStream parent; | ||
|
||
public ClonedFileInputStream(AutoDeleteFileAudioStream parent, File file, AudioFormat audioFormat) | ||
throws AudioException { | ||
super(file, audioFormat); | ||
this.parent = parent; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
super.close(); | ||
this.isOpen = false; | ||
parent.deleteIfPossible(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters