-
Notifications
You must be signed in to change notification settings - Fork 1
/
WriteNRTWin.hs
89 lines (81 loc) · 3.07 KB
/
WriteNRTWin.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
{-
Modification of WriteNRT to work with Windows.
This is the code from Vivid.Actions.NRT with just a few tweaks
for Windows compatibility.
-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE NoIncoherentInstances #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE NoUndecidableInstances #-}
module WriteNRTWin where
import qualified Vivid.SC.Server.Commands as SCCmd
import Vivid.Actions.NRT
import Vivid.Actions.Class
import Vivid.Actions.IO () -- maybe not in the future
import Vivid.OSC
import Vivid.OSC.Bundles (encodeOSCBundles)
import Vivid.SCServer
-- import Vivid.SCServer.State
import Vivid.SynthDef (encodeSD, sdToLiteral)
import Vivid.SynthDef.Types
import Control.Applicative
-- import Control.Arrow (first, second)
import Control.Monad
import Control.Monad.IO.Class (liftIO)
import Control.Monad.State (get, modify, execStateT, StateT)
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS (writeFile)
import qualified Data.ByteString.UTF8 as UTF8
import Data.Char (toLower)
import Data.Hashable (hash)
import qualified Data.Map as Map
import Data.Map (Map)
import Data.Monoid
import qualified Data.Set as Set
import System.Exit
import System.FilePath (takeExtension)
import System.Process (system)
import Prelude
writeNRTWin :: FilePath -> NRT a -> IO ()
writeNRTWin = writeNRTWith' defaultNRTArgs
writeNRTWith' :: NRTArgs -> FilePath -> NRT a -> IO ()
writeNRTWith' nrtArgs fPath nrtActions = do
when ('\'' `elem` fPath) $ error "Didnt have time to implement filepaths with single quotes"
contents <- encodeOSCBundles <$> runNRT nrtActions
-- ${SHELL}
--system "scsynth" >>= \case -- EDIT: couldn't get a Win-compatible version of this check to work
-- ExitSuccess -> return ()
-- ExitFailure _ -> error "No 'scsynth' found! Be sure to put it in your $PATH"
let tempFile = "vivid_nrt_" <> (show . hash) contents <> ".osc"
!fileType =
case Map.lookup (map toLower $ takeExtension fPath) extensionMap of
Just x -> x
Nothing -> error $
"The only file extensions we currently understand are: "
++ show (Map.keys extensionMap)
extensionMap = Map.fromList [
(".aif", "AIFF")
, (".aiff", "AIFF")
, (".wav", "WAV")
-- todo: these formats seem not to work:
-- ".flac" -> "FLAC"
-- ".ogg" -> "vorbis"
]
BS.writeFile tempFile contents
ExitSuccess <- system $ mconcat [
-- ${SHELL} -- EDIT: this doesn't exist on Win
-- "/bin/sh -c " -- EDIT: this doesn't exist on Win
--, " \"" -- Note these beginning and ending quotes -- EDIT: don't need quotes on Win
"scsynth"
, " -o ", show $ _nrtArgs_numChans nrtArgs
, " -N "
, tempFile
, " _ ", fPath, " " -- EDIT: single quotes removed for Windows version
, show $ _nrtArgs_sampleRate nrtArgs," ", fileType, " int16 "
--, " \"" -- EDIT: don't need quotes on Win
]
return ()