PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/scala/dk/mehmedbasic/audiobook/conversion/JoiningAudioConverter.scala

https://bitbucket.org/mehmedbasic/scalaudiobookconverter
Scala | 56 lines | 37 code | 11 blank | 8 comment | 1 complexity | be38d57ebbeb5e7071dc8798eb9a8d11 MD5 | raw file
  1. package dk.mehmedbasic.audiobook.conversion
  2. import dk.mehmedbasic.audiobook.execution._
  3. import java.io._
  4. import scala.Some
  5. import dk.mehmedbasic.audiobook.io.{CountingInputStream, StreamReader}
  6. import javazoom.jl.player.Player
  7. /**
  8. * Joining audio converter. Handles n files piped to one output file.
  9. *
  10. * @author Jesenko Mehmedbasic
  11. * created 09-01-13, 21:06
  12. *
  13. * @param callback Gives the file being converted, the current frame and the total frame count.
  14. */
  15. class JoiningAudioConverter(
  16. inputFile: AudioFile,
  17. output: AudioConfig,
  18. callback: (String, File) => Unit) extends Runnable {
  19. val system: OperatingSystem = OperatingSystem.get()
  20. val factory: ConversionCommandFactory = new ConversionCommandFactory(system, inputFile.getFile, output)
  21. val processOption: Option[Process] = createProcess
  22. def createProcess: Option[Process] = {
  23. val executor: CommandExecutor = system.getExecutor
  24. val process: Process = executor.execute(factory.createCommand())
  25. val errorStream: InputStream = process.getErrorStream
  26. new Thread(new StreamReader(errorStream)).start()
  27. Some(process)
  28. }
  29. def run() {
  30. if (processOption.isEmpty) {
  31. println(new File(".").getAbsoluteFile)
  32. System.err.println("An error occurred during faac execution\nCheck your $PATH for the faac executable")
  33. return
  34. }
  35. new StreamReader(processOption.get.getInputStream).start()
  36. val stream: OutputStream = new BufferedOutputStream(processOption.get.getOutputStream)
  37. val device: OutputStreamAudioDevice = new OutputStreamAudioDevice(stream, inputFile.getFile)
  38. val player: Player = new Player(new CountingInputStream(new FileInputStream(inputFile.getFile)), device)
  39. device.setListener(
  40. (fileName: String, pos: Int, len: Long) => {
  41. })
  42. player.play()
  43. callback.apply(inputFile.getFile.getAbsolutePath, factory.getOutput)
  44. }
  45. }