Automatically cut audio books
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

98 строки
3.1KB

  1. module Main (main) where
  2. import Lib
  3. import Parser
  4. import Driver
  5. import Types
  6. runDehum :: Flags -> FilePath -> FilePath -> IO ()
  7. runDehum flags input output =
  8. withSound flags input $ \fmtIn params sig ->
  9. SoxLib.withWrite
  10. (writerInfoFromFormat fmtIn params)
  11. output $ \fmtOut ->
  12. SoxLib.writeStorableVectorLazy fmtOut $
  13. SVL.interleaveFirstPattern $
  14. map
  15. (Causal.apply
  16. (arr (Bin.fromCanonicalWith Real.roundSimple)
  17. <<<
  18. dehum params
  19. <<<
  20. arr Bin.toCanonical)) $
  21. SVL.deinterleave (numChannels params) sig
  22. runEnvelope :: Flags -> FilePath -> FilePath -> IO ()
  23. runEnvelope flags input output =
  24. withSound flags input $ \fmtIn params sig ->
  25. SoxLib.withWrite
  26. (monoInfoFromFormat fmtIn params)
  27. output $ \fmtOut ->
  28. SoxLib.writeStorableVectorLazy fmtOut $
  29. Causal.apply
  30. (arr (Bin.fromCanonicalWith Real.roundSimple)) $
  31. trackEnvelope params $
  32. map
  33. (Causal.apply
  34. (arr (^2)
  35. <<<
  36. dehum params
  37. <<<
  38. arr Bin.toCanonical)) $
  39. SVL.deinterleave (numChannels params) sig
  40. runSizes :: Flags -> FilePath -> IO ()
  41. runSizes flags input =
  42. withSound flags input $ \_fmt params sig ->
  43. mapM_ print $ pieceDurations params sig
  44. runLabels :: Flags -> FilePath -> IO ()
  45. runLabels flags input =
  46. withSound flags input $ \_fmt params sig ->
  47. mapM_ (\(n, (from,to)) -> printf "%s\t%s\t%d\n" from to n) $
  48. zip [(0::Int) ..] $
  49. ListHT.mapAdjacent (,) $
  50. map (\t ->
  51. case divMod (Real.roundSimple (fromIntegral t * 10^6 / sampleRate params)) (10^6) of
  52. -- FIXME: the comma is certainly only correct in German locale
  53. (seconds,micros) ->
  54. printf "%d,%06d" seconds (micros::Integer) :: String) $
  55. scanl (+) 0 $
  56. prefetch (preStart params) $
  57. pieceDurations params sig
  58. {- |
  59. > runChop flags "in.wav" "%03d.wav"
  60. -}
  61. runChop :: Flags -> FilePath -> FilePath -> IO ()
  62. runChop flags input output =
  63. withSound flags input $ \fmtIn params sig ->
  64. forM_ (zip [(0::Int)..] $ chopLazy params sig) $ \(n,piece) ->
  65. SoxLib.withWrite
  66. (writerInfoFromFormat fmtIn params)
  67. (printf output n) $ \fmtOut ->
  68. SoxLib.writeStorableVectorLazy fmtOut piece
  69. main :: IO ()
  70. main = SoxLib.formatWith $ do
  71. argv <- getArgs
  72. let (opts, files, errors) =
  73. getOpt Opt.RequireOrder description argv
  74. when (not $ null errors) $
  75. exitFailureMsg (init (concat errors))
  76. flags <- foldl (>>=) (return defltFlags) opts
  77. if flagComputeEnvelope flags
  78. then
  79. case files of
  80. [input,output] -> runEnvelope flags input output
  81. [] -> exitFailureMsg "need input and output file envelope computation"
  82. _ -> exitFailureMsg "more than two file names given"
  83. else
  84. case files of
  85. [input,output] -> runChop flags input output
  86. [input] -> runLabels flags input
  87. [] -> exitFailureMsg "no input or output given"
  88. _ -> exitFailureMsg "more than two file names given"