1 module RMCA.Translator.Message where
4 import Sound.MIDI.Controller (volume)
5 import qualified Sound.MIDI.Message as Message
6 import qualified Sound.MIDI.Message.Channel as Channel
7 import qualified Sound.MIDI.Message.Channel.Voice as Voice
9 -- We might want to move that to Semantics.
12 type RawMessage = Message.T
14 type MidiVoice = Voice.T
16 type Channel = Channel.Channel
18 type ControllerIdx = Voice.Controller
22 -- Each channel is linked to a particular translation signal function
23 -- itself linked to a particular layer. Therefore we will dispose of
24 -- the channel number as soon as possible.
26 -- /!\ This is dangerous as it only treats unipolar control values.
27 data Message = NoteOn Channel Pitch Strength
28 | NoteOff Channel Pitch Strength
29 | Instrument Channel Voice.Program
31 -- | Control Channel ControllerIdx UCtrl
34 getChannel :: Message -> Int
35 getChannel (NoteOn c _ _) = Channel.fromChannel c
36 getChannel (NoteOff c _ _) = Channel.fromChannel c
37 getChannel (Volume c _) = Channel.fromChannel c
38 --getChannel (Control c _) = Channel.fromChannel c
39 getChannel (Instrument c _ ) = Channel.fromChannel c
41 mkChannel :: Int -> Channel
42 mkChannel = Channel.toChannel
44 mkProgram :: Int -> Channel.Program
45 mkProgram = Channel.toProgram
47 -- Function to go back and forth with the representations of pitches,
48 -- as they are different in our model and in the Jack API model.
49 fromRawPitch :: Voice.Pitch -> Pitch
50 fromRawPitch p = Pitch $ Voice.fromPitch p
52 toRawPitch :: Pitch -> Voice.Pitch
53 toRawPitch (Pitch p) = Voice.toPitch p
55 isNoteOn :: Message -> Bool
56 isNoteOn NoteOn {} = True
59 isNoteOff :: Message -> Bool
60 isNoteOff NoteOff {} = True
64 isControl :: Message -> Bool
65 isControl Volume {} = True
68 switchOnOff :: Message -> Message
69 switchOnOff (NoteOn c p v) = NoteOff c p v
70 switchOnOff (NoteOff c p v) = NoteOn c p v
71 switchOnOff m = error $ "The message " ++ show m ++ " is not a note message"
73 fromRawMessage :: RawMessage -> Maybe Message
74 fromRawMessage (Message.Channel (Channel.Cons c
75 (Channel.Voice (Voice.NoteOn p v)))) =
76 Just $ NoteOn c (fromRawPitch p) (toUCtrl $ Voice.fromVelocity v)
77 fromRawMessage (Message.Channel (Channel.Cons c
78 (Channel.Voice (Voice.NoteOff p v)))) =
79 Just $ NoteOff c (fromRawPitch p) (toUCtrl $ Voice.fromVelocity v)
80 fromRawMessage (Message.Channel (Channel.Cons c
81 (Channel.Voice (Voice.ProgramChange p)))) =
83 fromRawMessage (Message.Channel (Channel.Cons c
84 (Channel.Voice (Voice.Control n v))))
85 | n == volume = Just $ Volume c v
87 fromRawMessage _ = Nothing
89 toRawMessage :: Message -> RawMessage
90 toRawMessage (NoteOn c p v) =
91 Message.Channel $ Channel.Cons c
92 (Channel.Voice $ Voice.NoteOn (toRawPitch p) (Voice.toVelocity $ fromUCtrl v))
93 toRawMessage (NoteOff c p v) =
94 Message.Channel $ Channel.Cons c
95 (Channel.Voice $ Voice.NoteOff (toRawPitch p) (Voice.toVelocity $ fromUCtrl v))
96 toRawMessage (Volume c v) =
97 Message.Channel (Channel.Cons c
98 (Channel.Voice (Voice.Control volume v)))
99 toRawMessage (Instrument c p) =
100 Message.Channel (Channel.Cons c
101 (Channel.Voice (Voice.ProgramChange p)))