Skip to content

Commit

Permalink
Text2waveSpeakGenerator: use a temporary file instead of stdout
Browse files Browse the repository at this point in the history
stdout wasn't working (data seemed corrupted)
  • Loading branch information
nafg committed Oct 17, 2018
1 parent 875d0e9 commit b4c4ed2
Showing 1 changed file with 4 additions and 15 deletions.
19 changes: 4 additions & 15 deletions core/src/main/scala/simpleivr/Text2waveSpeakGenerator.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package simpleivr

import java.nio.ByteBuffer
import java.nio.channels.Channels
import java.nio.file.Files

import scala.io.Source

Expand All @@ -17,26 +17,15 @@ object Text2waveSpeakGenerator extends SpeakGenerator {
.write { writeChan =>
IO {
println("Falling back to text2wave because audio file does not exist: " + speak.files.supportedAudioFiles)
val text2wave = Runtime.getRuntime.exec("/usr/bin/text2wave -scale 1.5 -F 8000")
val tmpFile = Files.createTempFile("chavrusa-text2wave", ".wav")
val text2wave = Runtime.getRuntime.exec("/usr/bin/text2wave -scale 1.5 -F 8000 -o " + tmpFile.toString)
val stdin = text2wave.getOutputStream
stdin.write(speak.msg.getBytes())
stdin.flush()
val stdout = Channels.newChannel(text2wave.getInputStream)
val buf = ByteBuffer.allocate(8192)
Iterator.continually {
buf.clear()
stdout.read(buf)
}
.takeWhile(_ >= 0)
.foreach { _ =>
buf.flip()
writeChan.write(buf)
}
text2wave.getInputStream
stdin.close()
stdout.close()
text2wave.waitFor()
Source.fromInputStream(text2wave.getErrorStream).getLines() foreach println
Files.copy(tmpFile, Channels.newOutputStream(writeChan))
}
}
.flatMap { either =>
Expand Down

0 comments on commit b4c4ed2

Please sign in to comment.