tidal 0.3.5 → 0.3.6
raw patch · 4 files changed
+36/−18 lines, 4 files
Files
- README.md +1/−1
- Sound/Tidal/Tempo.hs +27/−16
- Sound/Tidal/Utils.hs +7/−0
- tidal.cabal +1/−1
README.md view
@@ -3,7 +3,7 @@ Language for live coding of pattern -(c) Alex McLean 2013+(c) Alex McLean 2014 Distributed under the terms of the GNU Public license version 3 (or later).
Sound/Tidal/Tempo.hs view
@@ -1,8 +1,9 @@+{-# LANGUAGE ScopedTypeVariables #-} module Sound.Tidal.Tempo where import Data.Time (getCurrentTime, UTCTime, diffUTCTime) import Data.Time.Clock.POSIX-import Control.Monad (forM_, forever)+import Control.Monad (forM_, forever, void) import Control.Monad.IO.Class (liftIO) import Control.Concurrent (forkIO, threadDelay) import Control.Concurrent.MVar@@ -27,10 +28,12 @@ instance Show Tempo where show x = show (at x) ++ "," ++ show (beat x) ++ "," ++ show (bps x) -getClockIp :: IO (String)-getClockIp = do addr <- E.try (getEnv "TEMPO_ADDR")- return $ either (const "127.0.0.1") (id) (addr :: Either E.IOException String)+getClockIp :: IO String+getClockIp = getEnvDefault "127.0.0.1" "TIDAL_TEMPO_IP" +getServerPort :: IO Int+getServerPort = fmap read (getEnvDefault "9160" "TIDAL_TEMPO_PORT")+ readTempo :: String -> Tempo readTempo x = Tempo (read a) (read b) (read c) where (a:b:c:_) = wordsBy (== ',') x@@ -72,23 +75,30 @@ bps <- takeMVar mBps WS.sendTextData conn (T.pack $ show bps) -connectClient clockip mTempo mBps = do - E.handle- ((\err -> - do startServer- threadDelay 500000- cx "127.0.0.1"- ) :: E.SomeException -> IO ())- (cx clockip)- where cx ip = WS.runClient ip 9160 "/tempo" (clientApp mTempo mBps)- --where cx ip = WS.connect ip 9160 "/tempo" (clientApp mTempo mBps)+connectClient :: Bool -> String -> MVar Tempo -> MVar Double -> IO ()+connectClient secondTry ip mTempo mBps = do + let errMsg = "Failed to connect to tidal server. Try specifying a " +++ "different port (default is 9160) setting the " +++ "environment variable TIDAL_TEMPO_PORT"+ serverPort <- getServerPort+ WS.runClient ip serverPort "/tempo" (clientApp mTempo mBps) `E.catch` + \(_ :: E.SomeException) -> do+ case secondTry of+ True -> error errMsg+ _ -> do+ res <- E.try (void startServer)+ case res of+ Left (_ :: E.SomeException) -> error errMsg+ Right _ -> do+ threadDelay 500000+ connectClient True ip mTempo mBps runClient :: IO ((MVar Tempo, MVar Double)) runClient = do clockip <- getClockIp mTempo <- newEmptyMVar mBps <- newEmptyMVar - forkIO $ connectClient clockip mTempo mBps+ forkIO $ connectClient False clockip mTempo mBps return (mTempo, mBps) bpsSetter :: IO (Double -> IO ())@@ -161,10 +171,11 @@ startServer :: IO (ThreadId) startServer = do+ serverPort <- getServerPort start <- getCurrentTime tempoState <- newMVar (Tempo start 0 1) clientState <- newMVar []- forkIO $ WS.runServer "0.0.0.0" 9160 $ serverApp tempoState clientState+ forkIO $ WS.runServer "0.0.0.0" serverPort $ serverApp tempoState clientState serverApp :: MVar Tempo -> MVar ClientState -> WS.ServerApp serverApp tempoState clientState pending = do
Sound/Tidal/Utils.hs view
@@ -1,6 +1,8 @@ module Sound.Tidal.Utils where +import System.Environment (getEnv) import Data.Maybe (listToMaybe)+import Control.Exception enumerate :: [a] -> [(Int, a)] enumerate = zip [0..]@@ -51,3 +53,8 @@ mapArcs :: (a -> a) -> [(a, a, x)] -> [(a, a, x)] mapArcs f = (mapFsts' f) . (mapSnds' f)++getEnvDefault :: String -> String -> IO String+getEnvDefault defValue var = do+ res <- try . getEnv $ var+ return $ either (const defValue) id (res :: Either IOException String)
tidal.cabal view
@@ -1,5 +1,5 @@ name: tidal-version: 0.3.5+version: 0.3.6 synopsis: Pattern language for improvised music -- description: homepage: http://yaxu.org/tidal/