From baa2bc02f143354da029c1c427b5e3e72cc9c873 Mon Sep 17 00:00:00 2001 From: Adrian King Legaspi Date: Mon, 7 Jan 2019 18:33:39 +0800 Subject: [PATCH] Convert audio names to md5 digest --- komposition.cabal | 2 ++ src/Komposition/Import/Audio/Sox.hs | 11 +++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/komposition.cabal b/komposition.cabal index c3d0318..41511bf 100644 --- a/komposition.cabal +++ b/komposition.cabal @@ -89,6 +89,7 @@ library build-depends: base >=4.10 && <5 , async , binary + , bytestring , deepseq , directory , exceptions @@ -119,6 +120,7 @@ library , pipes-safe , primitive , protolude + , pureMD5 , typed-process >= 0.2 && < 0.3 , row-types , safe-exceptions diff --git a/src/Komposition/Import/Audio/Sox.hs b/src/Komposition/Import/Audio/Sox.hs index b037230..1b9d23d 100644 --- a/src/Komposition/Import/Audio/Sox.hs +++ b/src/Komposition/Import/Audio/Sox.hs @@ -19,6 +19,8 @@ import Control.Effect.Carrier import Control.Effect.Sum import Control.Monad.Catch hiding (bracket) import qualified Data.Char as Char +import qualified Data.ByteString.Lazy.Char8 as Lazy +import Data.Digest.Pure.MD5 import qualified Data.Text as Text import Data.Time.Clock import Pipes @@ -167,8 +169,7 @@ transcodeAudioFileToWav -> FilePath -> Producer ProgressUpdate m FilePath transcodeAudioFileToWav tempDir fullLength inPath = do - -- TODO: use md5 digest to avoid collisions - let outPath = tempDir takeBaseName inPath <> ".wav" + let outPath = tempDir createMD5BaseName inPath <> ".wav" cmd = Command { output = Command.FileOutput outPath @@ -200,12 +201,11 @@ instance (MonadIO m, Carrier sig m) => Carrier (AudioImport :+: sig) (SoxAudioIm (liftIO . removeDirectoryRecursive) $ \tempDir -> do fullLength <- getAudioFileDuration srcFile - -- TODO: use file md5 digest in filename (or for a subdirectory) to avoid collisions chunks <- divideProgress4 (transcodeAudioFileToWav tempDir fullLength srcFile) (normalizeAudio tempDir) - (splitAudioBySilence (outDir "audio-chunks") (takeBaseName srcFile <> "-%5n.wav")) + (splitAudioBySilence (outDir "audio-chunks") (createMD5BaseName srcFile <> "-%5n.wav")) dropSilentChunks lift (mapM (filePathToAudioAsset outDir) chunks) Unclassified -> k $ do @@ -225,6 +225,9 @@ instance (MonadIO m, Carrier sig m) => Carrier (AudioImport :+: sig) (SoxAudioIm -- TODO: Check that it can be processed, not just checking the extension k (takeExtension p `elem` [".wav", ".mp3", ".m4a", ".aiff", ".aac"]) +createMD5BaseName :: [Char] -> [Char] +createMD5BaseName = show .md5 . Lazy.pack . takeBaseName + runSoxAudioImport :: (MonadIO m, Carrier sig m) => Eff (SoxAudioImportC m) a -> m a runSoxAudioImport = runSoxAudioImportC . interpret