]> Git — Sourcephile - tmp/julm/arpeggigon.git/blob - src/RMCA/Translator/Message.hs
Corrections to the beat generation.
[tmp/julm/arpeggigon.git] / src / RMCA / Translator / Message.hs
1 module RMCA.Translator.Message where
2
3 import RMCA.Semantics
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
8
9 -- We might want to move that to Semantics.
10 type SampleRate = Int
11
12 type RawMessage = Message.T
13
14 type MidiVoice = Voice.T
15
16 type Channel = Channel.Channel
17
18 type ControllerIdx = Voice.Controller
19
20 type Frames = Int
21
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.
25 ---
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
30 | Volume Channel Int
31 -- | Control Channel ControllerIdx UCtrl
32 deriving(Show)
33
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
40
41 mkChannel :: Int -> Channel
42 mkChannel = Channel.toChannel
43
44 mkProgram :: Int -> Channel.Program
45 mkProgram = Channel.toProgram
46
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
51
52 toRawPitch :: Pitch -> Voice.Pitch
53 toRawPitch (Pitch p) = Voice.toPitch p
54
55 isNoteOn :: Message -> Bool
56 isNoteOn NoteOn {} = True
57 isNoteOn _ = False
58
59 isNoteOff :: Message -> Bool
60 isNoteOff NoteOff {} = True
61 isNoteOff _ = False
62
63
64 isControl :: Message -> Bool
65 isControl Volume {} = True
66 isControl _ = False
67
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"
72
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)))) =
82 Just $ Instrument c p
83 fromRawMessage (Message.Channel (Channel.Cons c
84 (Channel.Voice (Voice.Control n v))))
85 | n == volume = Just $ Volume c v
86 | otherwise = Nothing
87 fromRawMessage _ = Nothing
88
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)))