packages feed

sound-collage-0.1: src/Main.hs

module Main where

import qualified SoundCollage
import qualified Option
import qualified Options.Applicative as OP

import qualified System.Exit as Exit
import qualified System.IO as IO
import System.FilePath ((</>), )
import System.IO.Temp (withSystemTempDirectory, )


exitFailureMsg :: String -> IO a
exitFailureMsg msg = do
   IO.hPutStrLn IO.stderr msg
   Exit.exitFailure

setChunkSize :: Int -> SoundCollage.Parameters -> SoundCollage.Parameters
setChunkSize chunkSize params =
   params {
      SoundCollage.paramShift =
         div chunkSize (SoundCollage.paramOverlap params)
   }

makeParams :: Maybe Int -> SoundCollage.Parameters
makeParams maybeChunkSize =
   maybe
      SoundCollage.defltParams
      (flip setChunkSize SoundCollage.defltParams)
      maybeChunkSize

main :: IO ()
main = do
   Option.Cons maybeChunkSize action src dst <- OP.execParser Option.info
   case action of
      Option.Decompose ->
         SoundCollage.runDecompose (makeParams maybeChunkSize) src dst
      Option.Compose ->
         SoundCollage.runCompose (makeParams maybeChunkSize) src dst
      Option.Associate pool ->
         SoundCollage.runAssociate (makeParams maybeChunkSize) pool src dst
      Option.Batch pool ->
         withSystemTempDirectory "sound-chunks" $ \chunkDir ->
         withSystemTempDirectory "sound-collage" $ \collDir -> do
            chunkSize <-
               maybe
                  (SoundCollage.chunkSizeFromPool pool)
                  return
                  maybeChunkSize
            let params = setChunkSize chunkSize SoundCollage.defltParams
            putStrLn $ "determined chunk size: " ++ show chunkSize
            putStrLn $ "decompose to " ++ chunkDir
            SoundCollage.runDecompose params src
               (chunkDir </> "%06d/%06d")
            putStrLn $ "associate to " ++ collDir
            SoundCollage.runAssociate params pool chunkDir collDir
            putStrLn "compose"
            SoundCollage.runCompose params collDir dst