module Sound.Jammit.Export
( Library
, fuzzySearchBy
, exactSearchBy
, loadLibrary
, getAudioParts
, getSheetParts
, runAudio
, runSheet
) where
import Control.Applicative (liftA2)
import Control.Monad (forM)
import Data.Char (toLower)
import Data.List (isInfixOf, sort, isPrefixOf)
import Data.Maybe (catMaybes)
import System.Directory (getDirectoryContents)
import System.FilePath ((</>), splitFileName, takeFileName)
import Sound.Jammit.Internal.Image
import Sound.Jammit.Base
import Sound.Jammit.Internal.AudioExpr
import Sound.Jammit.Internal.TempIO
type Library = [(FilePath, Info, [Track])]
fuzzySearchBy :: (Info -> String) -> String -> Library -> Library
fuzzySearchBy f str = let str' = map toLower str in
filter $ \(_, info, _) -> str' `isInfixOf` map toLower (f info)
exactSearchBy :: (Info -> String) -> String -> Library -> Library
exactSearchBy f str = filter $ \(_, info, _) -> f info == str
loadLibrary :: FilePath -> IO Library
loadLibrary jmt = do
dirs <- songSubdirs jmt
fmap catMaybes $ forM dirs $ \d -> do
maybeInfo <- loadInfo d
maybeTrks <- loadTracks d
return $ liftA2 (\i t -> (d, i, t)) maybeInfo maybeTrks
getAudioParts :: Library -> [(AudioPart, FilePath)]
getAudioParts lib = do
(dir, info, trks) <- lib
trk <- trks
case trackTitle trk >>= \t -> titleToAudioPart t (instrument info) of
Nothing -> []
Just ap -> [(ap, dir </> (identifier trk ++ "_jcfx"))]
getSheetParts :: Library -> [(SheetPart, (FilePath, Integer))]
getSheetParts lib = do
(dir, _info, trks) <- lib
trk <- trks
case (trackTitle trk >>= \t -> titleToPart t, scoreSystemInterval trk) of
(Just p, Just ht) -> let
sheet = (Notation p, (dir </> (identifier trk ++ "_jcfn"), ht))
tab = (Tab p, (dir </> (identifier trk ++ "_jcft"), ht))
in if elem (partToInstrument p) [Guitar, Bass]
then [sheet, tab]
else [sheet]
_ -> []
runAudio
:: [FilePath]
-> [FilePath]
-> FilePath
-> IO ()
runAudio pos neg = let
tttDrums = "793EAAE0-6761-44D7-9A9A-1FB451A2A438_jcfx"
tttDrumsBack = "37EE5AA5-4049-4CED-844A-D34F6B165F67_jcfx"
aifcFile a = if takeFileName a `elem` [tttDrums, tttDrumsBack]
then Pad (Samples 38) $ File a
else File a
posAifcs = map (\f -> ( 1, aifcFile f)) pos
negAifcs = map (\f -> (1, aifcFile f)) neg
in renderAudio $ optimize $ Mix $ posAifcs ++ negAifcs
runSheet
:: [(FilePath, Integer)]
-> Int
-> FilePath
-> IO ()
runSheet trks lns fout = runTempIO fout $ do
trkLns <- liftIO $ forM trks $ \(fp, ht) -> do
let (dir, file) = splitFileName fp
ls <- getDirectoryContents dir
return (map (dir </>) $ sort $ filter (file `isPrefixOf`) ls, ht)
jpegs <- partsToPages trkLns lns
pdf <- newTempFile "pages.pdf"
liftIO $ jpegsToPDF jpegs pdf
return pdf