/src/main/scala/dk/mehmedbasic/audiobook/conversion/JoiningAudioConverter.scala
Scala | 56 lines | 37 code | 11 blank | 8 comment | 1 complexity | be38d57ebbeb5e7071dc8798eb9a8d11 MD5 | raw file
- package dk.mehmedbasic.audiobook.conversion
- import dk.mehmedbasic.audiobook.execution._
- import java.io._
- import scala.Some
- import dk.mehmedbasic.audiobook.io.{CountingInputStream, StreamReader}
- import javazoom.jl.player.Player
- /**
- * Joining audio converter. Handles n files piped to one output file.
- *
- * @author Jesenko Mehmedbasic
- * created 09-01-13, 21:06
- *
- * @param callback Gives the file being converted, the current frame and the total frame count.
- */
- class JoiningAudioConverter(
- inputFile: AudioFile,
- output: AudioConfig,
- callback: (String, File) => Unit) extends Runnable {
- val system: OperatingSystem = OperatingSystem.get()
- val factory: ConversionCommandFactory = new ConversionCommandFactory(system, inputFile.getFile, output)
- val processOption: Option[Process] = createProcess
- def createProcess: Option[Process] = {
- val executor: CommandExecutor = system.getExecutor
- val process: Process = executor.execute(factory.createCommand())
- val errorStream: InputStream = process.getErrorStream
- new Thread(new StreamReader(errorStream)).start()
- Some(process)
- }
- def run() {
- if (processOption.isEmpty) {
- println(new File(".").getAbsoluteFile)
- System.err.println("An error occurred during faac execution\nCheck your $PATH for the faac executable")
- return
- }
- new StreamReader(processOption.get.getInputStream).start()
- val stream: OutputStream = new BufferedOutputStream(processOption.get.getOutputStream)
- val device: OutputStreamAudioDevice = new OutputStreamAudioDevice(stream, inputFile.getFile)
- val player: Player = new Player(new CountingInputStream(new FileInputStream(inputFile.getFile)), device)
- device.setListener(
- (fileName: String, pos: Int, len: Long) => {
- })
- player.play()
- callback.apply(inputFile.getFile.getAbsolutePath, factory.getOutput)
- }
- }