|
| 1 | +module Sound.Tidal.Stream.Listen where |
| 2 | + |
| 3 | +import Data.Maybe (fromJust, catMaybes, isJust) |
| 4 | +import Control.Concurrent.MVar |
| 5 | +import Control.Monad (when) |
| 6 | +import System.IO (hPutStrLn, stderr) |
| 7 | +import qualified Data.Map as Map |
| 8 | +import qualified Sound.Osc.Fd as O |
| 9 | +import qualified Sound.Osc.Time.Timeout as O |
| 10 | +import qualified Network.Socket as N |
| 11 | +import qualified Control.Exception as E |
| 12 | + |
| 13 | +import Sound.Tidal.ID |
| 14 | +import Sound.Tidal.Pattern |
| 15 | + |
| 16 | +import Sound.Tidal.Stream.Config |
| 17 | +import Sound.Tidal.Stream.Types |
| 18 | +import Sound.Tidal.Stream.UI |
| 19 | + |
| 20 | +{- |
| 21 | + Listen.hs - logic for listening and acting on incoming OSC messages |
| 22 | + Copyright (C) 2020, Alex McLean and contributors |
| 23 | +
|
| 24 | + This library is free software: you can redistribute it and/or modify |
| 25 | + it under the terms of the GNU General Public License as published by |
| 26 | + the Free Software Foundation, either version 3 of the License, or |
| 27 | + (at your option) any later version. |
| 28 | +
|
| 29 | + This library is distributed in the hope that it will be useful, |
| 30 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 31 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 32 | + GNU General Public License for more details. |
| 33 | +
|
| 34 | + You should have received a copy of the GNU General Public License |
| 35 | + along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 36 | +-} |
| 37 | + |
| 38 | + |
| 39 | +openListener :: Config -> IO (Maybe O.Udp) |
| 40 | +openListener c |
| 41 | + | cCtrlListen c = catchAny run (\_ -> do verbose c "That port isn't available, perhaps another Tidal instance is already listening on that port?" |
| 42 | + return Nothing |
| 43 | + ) |
| 44 | + | otherwise = return Nothing |
| 45 | + where |
| 46 | + run = do sock <- O.udpServer (cCtrlAddr c) (cCtrlPort c) |
| 47 | + when (cCtrlBroadcast c) $ N.setSocketOption (O.udpSocket sock) N.Broadcast 1 |
| 48 | + return $ Just sock |
| 49 | + catchAny :: IO a -> (E.SomeException -> IO a) -> IO a |
| 50 | + catchAny = E.catch |
| 51 | + |
| 52 | +-- Listen to and act on OSC control messages |
| 53 | +ctrlResponder :: Int -> Config -> Stream -> IO () |
| 54 | +ctrlResponder waits c (stream@(Stream {sListen = Just sock})) |
| 55 | + = do ms <- recvMessagesTimeout 2 sock |
| 56 | + if (null ms) |
| 57 | + then do checkHandshake -- there was a timeout, check handshake |
| 58 | + ctrlResponder (waits+1) c stream |
| 59 | + else do mapM_ act ms |
| 60 | + ctrlResponder 0 c stream |
| 61 | + where |
| 62 | + checkHandshake = do busses <- readMVar (sBusses stream) |
| 63 | + when (null busses) $ do when (waits == 0) $ verbose c $ "Waiting for SuperDirt (v.1.7.2 or higher).." |
| 64 | + sendHandshakes stream |
| 65 | + |
| 66 | + act (O.Message "/dirt/hello" _) = sendHandshakes stream |
| 67 | + act (O.Message "/dirt/handshake/reply" xs) = do prev <- swapMVar (sBusses stream) $ bufferIndices xs |
| 68 | + -- Only report the first time.. |
| 69 | + when (null prev) $ verbose c $ "Connected to SuperDirt." |
| 70 | + return () |
| 71 | + where |
| 72 | + bufferIndices [] = [] |
| 73 | + bufferIndices (x:xs') | x == (O.AsciiString $ O.ascii "&controlBusIndices") = catMaybes $ takeWhile isJust $ map O.datum_integral xs' |
| 74 | + | otherwise = bufferIndices xs' |
| 75 | + -- External controller commands |
| 76 | + act (O.Message "/ctrl" (O.Int32 k:v:[])) |
| 77 | + = act (O.Message "/ctrl" [O.string $ show k,v]) |
| 78 | + act (O.Message "/ctrl" (O.AsciiString k:v@(O.Float _):[])) |
| 79 | + = add (O.ascii_to_string k) (VF (fromJust $ O.datum_floating v)) |
| 80 | + act (O.Message "/ctrl" (O.AsciiString k:O.AsciiString v:[])) |
| 81 | + = add (O.ascii_to_string k) (VS (O.ascii_to_string v)) |
| 82 | + act (O.Message "/ctrl" (O.AsciiString k:O.Int32 v:[])) |
| 83 | + = add (O.ascii_to_string k) (VI (fromIntegral v)) |
| 84 | + -- Stream playback commands |
| 85 | + act (O.Message "/mute" (k:[])) |
| 86 | + = withID k $ streamMute stream |
| 87 | + act (O.Message "/unmute" (k:[])) |
| 88 | + = withID k $ streamUnmute stream |
| 89 | + act (O.Message "/solo" (k:[])) |
| 90 | + = withID k $ streamSolo stream |
| 91 | + act (O.Message "/unsolo" (k:[])) |
| 92 | + = withID k $ streamUnsolo stream |
| 93 | + act (O.Message "/muteAll" []) |
| 94 | + = streamMuteAll stream |
| 95 | + act (O.Message "/unmuteAll" []) |
| 96 | + = streamUnmuteAll stream |
| 97 | + act (O.Message "/unsoloAll" []) |
| 98 | + = streamUnsoloAll stream |
| 99 | + act (O.Message "/hush" []) |
| 100 | + = streamHush stream |
| 101 | + act (O.Message "/silence" (k:[])) |
| 102 | + = withID k $ streamSilence stream |
| 103 | + act m = hPutStrLn stderr $ "Unhandled OSC: " ++ show m |
| 104 | + add :: String -> Value -> IO () |
| 105 | + add k v = do sMap <- takeMVar (sStateMV stream) |
| 106 | + putMVar (sStateMV stream) $ Map.insert k v sMap |
| 107 | + return () |
| 108 | + withID :: O.Datum -> (ID -> IO ()) -> IO () |
| 109 | + withID (O.AsciiString k) func = func $ (ID . O.ascii_to_string) k |
| 110 | + withID (O.Int32 k) func = func $ (ID . show) k |
| 111 | + withID _ _ = return () |
| 112 | +ctrlResponder _ _ _ = return () |
| 113 | + |
| 114 | +verbose :: Config -> String -> IO () |
| 115 | +verbose c s = when (cVerbose c) $ putStrLn s |
| 116 | + |
| 117 | +recvMessagesTimeout :: (O.Transport t) => Double -> t -> IO [O.Message] |
| 118 | +recvMessagesTimeout n sock = fmap (maybe [] O.packetMessages) $ O.recvPacketTimeout n sock |
0 commit comments