]> Git — Sourcephile - tmp/julm/arpeggigon.git/blob - RCMA/Translator/Jack.hs
hlint suggestions.
[tmp/julm/arpeggigon.git] / RCMA / Translator / Jack.hs
1 -- Contains all the information and functions necessary to run a Jack
2 -- port and exchange information through reactive values and Yampa.
3 module RCMA.Translator.Jack ( jackSetup
4 ) where
5
6 import Control.Applicative ((<**>))
7 import qualified Control.Monad.Exception.Synchronous as Sync
8 import qualified Control.Monad.Trans.Class as Trans
9 import qualified Data.Bifunctor as BF
10 import Data.CBMVar
11 import qualified Data.EventList.Absolute.TimeBody as EventListAbs
12 import Data.ReactiveValue
13 import qualified Foreign.C.Error as E
14 import Hails.Yampa
15 import RCMA.Semantics
16 import RCMA.Translator.Filter
17 import RCMA.Translator.Message
18 import RCMA.Translator.RV
19 import RCMA.Translator.Translator
20 import qualified Sound.JACK as Jack
21 import qualified Sound.JACK.Exception as JExc
22 import qualified Sound.JACK.MIDI as JMIDI
23
24 import Debug.Trace
25
26 rcmaName :: String
27 rcmaName = "RCMA"
28
29 inPortName :: String
30 inPortName = "input"
31
32 outPortName :: String
33 outPortName = "output"
34
35 -- Starts a default client with an input and an output port. Doesn't
36 -- do anything as such.
37 jackSetup :: ReactiveFieldRead IO (LTempo, Int, [Note])
38 -> IO ()
39 jackSetup boardInRV = Jack.handleExceptions $ do
40 toProcessRV <- Trans.lift $ toProcess <$> newCBMVar []
41 Jack.withClientDefault rcmaName $ \client ->
42 Jack.withPort client outPortName $ \output ->
43 Jack.withPort client inPortName $ \input ->
44 Jack.withProcess client (jackCallBack client input output
45 toProcessRV boardInRV) $
46 Jack.withActivation client $ Trans.lift $ do
47 putStrLn $ "Started " ++ rcmaName ++ " JACK client."
48 Jack.waitForBreak
49
50 {-
51 -- Loop that does nothing except setting up a callback function
52 -- (called when Jack is ready to take new inputs).
53 jackRun :: (JExc.ThrowsErrno e) =>
54 Jack.Client
55 -> (Jack.NFrames -> Sync.ExceptionalT E.Errno IO ())
56 -> Sync.ExceptionalT e IO ()
57 jackRun client callback =
58 Jack.withProcess client callback $ do
59 Trans.lift $ putStrLn $ "Startedbbb " ++ rcmaName
60 Trans.lift $ Jack.waitForBreak
61 -}
62 defaultTempo :: Tempo
63 defaultTempo = 96
64
65 -- The callback function. It pumps value out of the input port, mix
66 -- them with value coming from the machine itself and stuff them into
67 -- the output port. When this function is not running, events are
68 -- processed.
69 jackCallBack :: Jack.Client
70 -> JMIDI.Port Jack.Input
71 -> JMIDI.Port Jack.Output
72 -> ReactiveFieldReadWrite IO [(Frames, RawMessage)]
73 -> ReactiveFieldRead IO (LTempo, Int, [Note])
74 -> Jack.NFrames
75 -> Sync.ExceptionalT E.Errno IO ()
76 jackCallBack client input output toProcessRV boardInRV
77 nframes@(Jack.NFrames nframesInt') = do
78 let inMIDIRV = inMIDIEvent input nframes
79 outMIDIRV = outMIDIEvent output nframes
80 nframesInt = fromIntegral nframesInt' :: Int
81 -- This gets the sample rate of the client and the last frame number
82 -- it processed. We then use it to calculate the current absolute time
83 sr <- Trans.lift $ Jack.getSampleRate client
84 (Jack.NFrames lframeInt) <- Trans.lift $ Jack.lastFrameTime client
85 --Trans.lift (reactiveValueRead inMIDIRV >>= (print . map (fst)))
86 -- We write the content of the input buffer to the input of a
87 -- translation signal function.
88 -- /!\ Should maybe be moved elsewhere
89 (inRaw, outPure) <- Trans.lift $ yampaReactiveDual [] readMessages
90 Trans.lift (inMIDIRV =:> inRaw)
91 (tempo, chan, boardIn') <- Trans.lift $ reactiveValueRead boardInRV
92 let boardIn = (zip (repeat 0) boardIn',[],[])
93 outMIDI <- Trans.lift $ reactiveValueRead outPure
94 -- We translate all signals to be sent into low level signals and
95 -- write them to the output buffer.
96 (inPure, outRaw) <- Trans.lift $ yampaReactiveDual
97 (defaultTempo, sr, chan, ([],[],[])) gatherMessages
98 -- This should all go in its own IO action
99 Trans.lift $ do
100 reactiveValueWrite inPure (tempo, sr, chan, boardIn `mappend` outMIDI)
101 reactiveValueRead outRaw <**>
102 (mappend <$> reactiveValueRead toProcessRV) >>=
103 reactiveValueWrite toProcessRV
104 (go, old') <- schedule nframesInt <$> reactiveValueRead toProcessRV
105 let old = map (BF.first (+ (- nframesInt))) old'
106 reactiveValueWrite outMIDIRV go
107 reactiveValueWrite toProcessRV old
108 --------------