File: Process.hs

package info (click to toggle)
haskell-ghc-exactprint 1.7.1.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,044 kB
  • sloc: haskell: 32,076; makefile: 7
file content (717 lines) | stat: -rw-r--r-- 20,510 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
{-# LANGUAGE ExistentialQuantification #-}
module Synthesizer.MIDI.CausalIO.Process (
   Events,

   slice,
   controllerLinear,
   controllerExponential,
   pitchBend,
   channelPressure,
   bendWheelPressure,
   constant,

   Instrument,
   Bank,
   GateChunk,
   noteEvents,
   embedPrograms,
   applyInstrument,
   applyModulatedInstrument,
   flattenControlSchedule,
   applyModulation,
   arrangeStorable,
   sequenceCore,
   sequenceModulated,
   sequenceModulatedMultiProgram,
   sequenceModulatedMultiProgramVelocityPitch,
   sequenceStorable,

   -- auxiliary function
   initWith,
   mapMaybe,
   ) where

import qualified Synthesizer.CausalIO.Gate as Gate
import qualified Synthesizer.CausalIO.Process as PIO

import qualified Synthesizer.MIDI.Value.BendModulation as BM
import qualified Synthesizer.MIDI.Value.BendWheelPressure as BWP
import qualified Synthesizer.MIDI.Value as MV

import qualified Synthesizer.MIDI.EventList as MIDIEv
import Synthesizer.MIDI.EventList (StrictTime, )

import qualified Synthesizer.PiecewiseConstant.Signal as PC
import qualified Synthesizer.Storable.Cut as CutSt
import qualified Synthesizer.Generic.Cut as CutG
import qualified Synthesizer.Zip as Zip

import qualified Sound.MIDI.Message.Class.Check as Check
import qualified Sound.MIDI.Message.Channel.Voice as VoiceMsg

import Control.DeepSeq (rnf, )

import qualified Data.EventList.Relative.TimeBody  as EventList
import qualified Data.EventList.Relative.BodyTime  as EventListBT
import qualified Data.EventList.Relative.TimeTime  as EventListTT
import qualified Data.EventList.Relative.TimeMixed as EventListTM
import qualified Data.EventList.Relative.MixedTime as EventListMT
import qualified Data.EventList.Absolute.TimeBody  as AbsEventList

import qualified Numeric.NonNegative.Wrapper as NonNegW
import qualified Numeric.NonNegative.Class as NonNeg

import qualified Algebra.Transcendental as Trans
import qualified Algebra.RealRing       as RealRing
import qualified Algebra.Field          as Field
import qualified Algebra.Additive       as Additive
import qualified Algebra.ToInteger      as ToInteger

import qualified Data.StorableVector as SV
import qualified Data.StorableVector.ST.Strict as SVST
import Foreign.Storable (Storable, )

import qualified Control.Monad.Trans.Writer as MW
import qualified Control.Monad.Trans.State as MS
import qualified Control.Monad.Trans.Class as MT
import Control.Monad.IO.Class (liftIO, )

import qualified Data.Traversable as Trav
import Data.Traversable (Traversable, )
import Data.Foldable (traverse_, )

import Control.Arrow (Arrow, arr, (^<<), (<<^), )
import Control.Category ((.), )

import qualified Data.Map as Map

import qualified Data.List.HT as ListHT
import qualified Data.Maybe as Maybe
import Data.Monoid (Monoid, mempty, mappend, )
import Data.Maybe (maybeToList, )
import Data.Tuple.HT (mapFst, mapPair, )

import NumericPrelude.Numeric
import NumericPrelude.Base hiding ((.), sequence, )
import Prelude ()


type Events event = EventListTT.T StrictTime [event]


initWith ::
   (y -> c) ->
   c ->
   PIO.T
      (Events y)
      (EventListBT.T PC.ShortStrictTime c)
initWith f initial =
   PIO.traverse initial $
      \evs0 -> do
         y0 <- MS.get
         fmap (PC.subdivideLongStrict . EventListMT.consBody y0) $
            Trav.traverse (\ys -> traverse_ (MS.put . f) ys >> MS.get) evs0

slice ::
   (Check.C event) =>
   (event -> Maybe Int) ->
   (Int -> y) -> y ->
   PIO.T
      (Events event)
      (EventListBT.T PC.ShortStrictTime y)
slice select f initial =
   initWith f initial . mapMaybe select



mapMaybe ::
   (Arrow arrow, Functor f) =>
   (a -> Maybe b) ->
   arrow (f [a]) (f [b])
mapMaybe f =
   arr $ fmap $ Maybe.mapMaybe f

catMaybes ::
   (Arrow arrow, Functor f) =>
   arrow (f [Maybe a]) (f [a])
catMaybes =
   arr $ fmap Maybe.catMaybes

traverse ::
   (Traversable f) =>
   s -> (a -> MS.State s b) ->
   PIO.T (f [a]) (f [b])
traverse initial f =
   PIO.traverse initial (Trav.traverse (Trav.traverse f))


controllerLinear ::
   (Check.C event, Field.C y) =>
   MIDIEv.Channel ->
   MIDIEv.Controller ->
   (y,y) -> y ->
   PIO.T
      (Events event)
      (EventListBT.T PC.ShortStrictTime y)
controllerLinear chan ctrl bnd initial =
   slice (Check.controller chan ctrl)
      (MV.controllerLinear bnd) initial

controllerExponential ::
   (Check.C event, Trans.C y) =>
   MIDIEv.Channel ->
   MIDIEv.Controller ->
   (y,y) -> y ->
   PIO.T
      (Events event)
      (EventListBT.T PC.ShortStrictTime y)
controllerExponential chan ctrl bnd initial =
   slice (Check.controller chan ctrl)
      (MV.controllerExponential bnd) initial

pitchBend ::
   (Check.C event, Trans.C y) =>
   MIDIEv.Channel ->
   y -> y ->
   PIO.T
      (Events event)
      (EventListBT.T PC.ShortStrictTime y)
pitchBend chan range center =
   slice (Check.pitchBend chan)
      (MV.pitchBend range center) center

channelPressure ::
   (Check.C event, Trans.C y) =>
   MIDIEv.Channel ->
   y -> y ->
   PIO.T
      (Events event)
      (EventListBT.T PC.ShortStrictTime y)
channelPressure chan maxVal initial =
   slice (Check.channelPressure chan)
      (MV.controllerLinear (zero,maxVal)) initial

bendWheelPressure ::
   (Check.C event, RealRing.C y, Trans.C y) =>
   MIDIEv.Channel ->
   Int -> y -> y ->
   PIO.T
      (Events event)
      (EventListBT.T PC.ShortStrictTime (BM.T y))
bendWheelPressure chan
      pitchRange wheelDepth pressDepth =
   let toBM = BM.fromBendWheelPressure pitchRange wheelDepth pressDepth
   in  initWith toBM (toBM BWP.deflt)
       .
       catMaybes
       .
       traverse BWP.deflt (BWP.check chan)


-- might be moved to synthesizer-core
constant ::
   (Arrow arrow) =>
   y -> arrow (Events event) (EventListBT.T PC.ShortStrictTime y)
constant y = arr $
   EventListBT.singleton y .
   NonNegW.fromNumberMsg "MIDI.CausalIO.constant" .
   fromIntegral .
   EventListTT.duration

_constant ::
   (Arrow arrow, CutG.Read input) =>
   y -> arrow input (EventListBT.T PC.ShortStrictTime y)
_constant y = arr $
   EventListBT.singleton y .
   NonNegW.fromNumberMsg "MIDI.CausalIO.constant" .
   CutG.length



noteEvents ::
   (Check.C event, Arrow arrow) =>
   MIDIEv.Channel ->
   arrow
      (Events event)
      (Events (Either MIDIEv.Program (MIDIEv.NoteBoundary Bool)))
noteEvents chan =
   mapMaybe $ MIDIEv.checkNoteEvent chan


embedPrograms ::
   MIDIEv.Program ->
   PIO.T
      (Events (Either MIDIEv.Program (MIDIEv.NoteBoundary Bool)))
      (Events (MIDIEv.NoteBoundary (Maybe MIDIEv.Program)))
embedPrograms initPgm =
   catMaybes .
   traverse initPgm MIDIEv.embedProgramState


type GateChunk = Gate.Chunk MIDIEv.Velocity
type Instrument y chunk = y -> y -> PIO.T GateChunk chunk
type Bank y chunk = MIDIEv.Program -> Instrument y chunk



{-
for distinction of notes with the same pitch

We must use Integer instead of Int, in order to avoid an overflow
that would invalidate the check for unmatched NoteOffs
that is based on comparison of the NoteIds.
We cannot re-use NoteIds easily,
since the events at one time point are handled out of order.
-}
newtype NoteId = NoteId Integer
   deriving (Show, Eq, Ord)

succNoteId :: NoteId -> NoteId
succNoteId (NoteId n) = NoteId (n+1)

flattenNoteIdRange :: (NoteId,NoteId) -> [NoteId]
flattenNoteIdRange (start,afterEnd) =
   takeWhile (<afterEnd) $ iterate succNoteId start


newtype NoteOffList =
   NoteOffList {
      unwrapNoteOffList :: Events (NoteBoundary NoteId)
   }


instance CutG.Read NoteOffList where
   null (NoteOffList evs) =
      EventListTT.isPause evs && EventListTT.duration evs == 0
   length = fromIntegral . EventListTT.duration . unwrapNoteOffList

instance CutG.NormalForm NoteOffList where
   evaluateHead =
      EventListMT.switchTimeL (\t _ -> rnf (NonNegW.toNumber t)) .
      unwrapNoteOffList

instance Monoid NoteOffList where
   mempty = NoteOffList (EventListTT.pause mempty)
   mappend (NoteOffList xs) (NoteOffList ys) =
      NoteOffList (mappend xs ys)

{- |
The function defined here are based on the interpretation
of event lists as piecewise constant signals.
They do not fit to the interpretation of atomic events.
Because e.g. it makes no sense to split an atomic event into two instances by splitAt,
and it is also not clear, whether dropping the first chunk
shall leave a chunk of length zero
or remove that chunk completely.
-}
instance CutG.Transform NoteOffList where
   take n (NoteOffList xs) =
      NoteOffList $
      EventListTT.takeTime
         (NonNegW.fromNumberMsg "NoteOffList.take" $ fromIntegral n) xs
   drop n (NoteOffList xs) =
      NoteOffList $
      EventListTT.dropTime
         (NonNegW.fromNumberMsg "NoteOffList.drop" $ fromIntegral n) xs
   splitAt n (NoteOffList xs) =
      mapPair (NoteOffList, NoteOffList) $
      EventListTT.splitAtTime
         (NonNegW.fromNumberMsg "NoteOffList.splitAtTime" $ fromIntegral n) xs

   -- cf. ChunkySize.dropMarginRem
   dropMarginRem =
      CutG.dropMarginRemChunky
         (fmap fromIntegral . EventListTT.getTimes . unwrapNoteOffList)

   reverse (NoteOffList xs) =
      NoteOffList . EventListTT.reverse $ xs


findEvent ::
   (a -> Bool) ->
   Events a -> (Events a, Maybe a)
findEvent p =
   EventListTT.foldr
      (\t -> mapFst (EventListMT.consTime t))
      (\evs rest ->
         case ListHT.break p evs of
            (prefix, suffix) ->
               mapFst (EventListMT.consBody prefix) $
               case suffix of
                  [] -> rest
                  ev:_ -> (EventListTT.pause mempty, Just ev))
      (EventListBT.empty, Nothing)

gateFromNoteOffs ::
   (MIDIEv.Pitch, NoteId) ->
   NoteOffList ->
   GateChunk
gateFromNoteOffs pitchNoteId (NoteOffList noteOffs) =
   let dur = EventListTT.duration noteOffs
       (sustain, mEnd) =
          findEvent
             (\bnd ->
                case bnd of
                   -- AllNotesOff -> True
                   NoteBoundary endPitch _ noteId ->
                      pitchNoteId == (endPitch, noteId))
          noteOffs
   in  Gate.chunk dur $
       flip fmap mEnd $ \end ->
       (EventListTT.duration sustain,
        case end of
           NoteBoundary _ endVel _ -> endVel
           {-
           AllNotesOff -> VoiceMsg.normalVelocity -} )


data NoteBoundary a =
     NoteBoundary VoiceMsg.Pitch VoiceMsg.Velocity a
--   | AllSoundOff
   deriving (Eq, Show)

{- |
We count NoteIds per pitch,
such that the pair (pitch,noteId) identifies a note.
We treat nested notes in a first-in-first-out order (FIFO).
E.g.

> On, On, On, Off, Off, Off

is interpreted as

> On 0, On 1, On 2, Off 0, Off 1, Off 2

NoteOffs without previous NoteOns are thrown away.
-}
assignNoteIds ::
   (Traversable f) =>
   PIO.T
      (f [MIDIEv.NoteBoundary (Maybe MIDIEv.Program)])
      (f [NoteBoundary (NoteId, Maybe MIDIEv.Program)])
assignNoteIds =
   fmap concat
   ^<<
   traverse Map.empty (\bnd ->
      case bnd of
         MIDIEv.AllNotesOff -> do
            notes <- MS.get
            MS.put Map.empty
            return $
               concatMap (\(pitch, range) ->
                  map
                     (\noteId ->
                        NoteBoundary pitch VoiceMsg.normalVelocity
                           (noteId, Nothing))
                     (flattenNoteIdRange range)) $
               Map.toList notes
         MIDIEv.NoteBoundary pitch vel mpgm ->
            fmap (fmap (\noteId ->
               NoteBoundary pitch vel (noteId,mpgm))) $
            case mpgm of
               Nothing -> do
                  mNoteId <- MS.gets (Map.lookup pitch)
                  case mNoteId of
                     Nothing -> return []
                     Just (nextNoteOffId, nextNoteOnId) ->
                        if nextNoteOffId >= nextNoteOnId
                          then return []
                          else do
                             MS.modify (Map.insert pitch (succNoteId nextNoteOffId, nextNoteOnId))
                             return [nextNoteOffId]
               Just _ -> do
                  mNoteId <- MS.gets (Map.lookup pitch)
                  let (nextNoteOffId, nextNoteOnId) =
                         case mNoteId of
                            Nothing -> (NoteId 0, NoteId 0)
                            Just ids -> ids

                  MS.modify (Map.insert pitch (nextNoteOffId, succNoteId nextNoteOnId))
                  return [nextNoteOnId])

{-# INLINE velFreqBank #-}
velFreqBank ::
   (Trans.C y) =>
   (MIDIEv.Program -> y -> y -> process) ->
   (MIDIEv.Program -> MIDIEv.Velocity -> MIDIEv.Pitch -> process)
velFreqBank bank pgm vel pitch =
   bank pgm (MV.velocity vel) (MV.frequencyFromPitch pitch)

applyInstrumentCore ::
   (Arrow arrow) =>
   ((MIDIEv.Pitch, NoteId) -> noteOffListCtrl -> gateCtrl) ->
   (MIDIEv.Program -> MIDIEv.Velocity -> MIDIEv.Pitch ->
    PIO.T gateCtrl chunk) ->
   arrow
      (Events (NoteBoundary (NoteId, Maybe MIDIEv.Program)))
      (Zip.T
         NoteOffList
         (Events (PIO.T noteOffListCtrl chunk)))
applyInstrumentCore makeGate bank = arr $
   uncurry Zip.Cons .
   mapFst NoteOffList .
   EventListTT.unzip .
   fmap (ListHT.unzipEithers . fmap (\ev ->
      case ev of
--         MIDIEv.AllNotesOff -> Left MIDIEv.AllNotesOff
         NoteBoundary pitch vel (noteId, mpgm) ->
            case mpgm of
               Nothing -> Left $ NoteBoundary pitch vel noteId
               Just pgm ->
                  Right $
                     bank pgm vel pitch
                     <<^
                     makeGate (pitch, noteId)))

applyInstrument ::
   (Arrow arrow) =>
   (MIDIEv.Program -> MIDIEv.Velocity -> MIDIEv.Pitch ->
    PIO.T GateChunk chunk) ->
   arrow
      (Events (NoteBoundary (NoteId, Maybe MIDIEv.Program)))
      (Zip.T
         NoteOffList
         (Events (PIO.T NoteOffList chunk)))
applyInstrument bank =
   applyInstrumentCore gateFromNoteOffs bank


type ModulatedBank y ctrl chunk =
        MIDIEv.Program -> y -> y ->
        PIO.T (Zip.T GateChunk ctrl) chunk

applyModulatedInstrument ::
   (Arrow arrow, CutG.Read ctrl) =>
   (MIDIEv.Program -> MIDIEv.Velocity -> MIDIEv.Pitch ->
    PIO.T (Zip.T GateChunk ctrl) chunk) ->
   arrow
      (Zip.T
         (Events (NoteBoundary (NoteId, Maybe MIDIEv.Program)))
         ctrl)
      (Zip.T
         (Zip.T NoteOffList ctrl)
         (Events (PIO.T (Zip.T NoteOffList ctrl) chunk)))
applyModulatedInstrument bank =
   (\(Zip.Cons (Zip.Cons noteOffs events) ctrl) ->
      Zip.Cons (Zip.Cons noteOffs ctrl) events)
   ^<<
   Zip.arrowFirst
      (applyInstrumentCore
         (Zip.arrowFirst . gateFromNoteOffs) bank)


{- |
Turn an event list with bundles of elements
into an event list with single events.
ToDo: Move to event-list package?
-}
flatten ::
   (NonNeg.C time) =>
   a ->
   EventListTT.T time [a] ->
   EventListTT.T time a
flatten empty =
   EventListTT.foldr
      EventListMT.consTime
      (\bt xs ->
         uncurry EventListMT.consBody $
         case bt of
            [] -> (empty, xs)
            b:bs -> (b, foldr (\c rest -> EventListTT.cons NonNeg.zero c rest) xs bs))
      EventListBT.empty


flattenControlSchedule ::
   (Monoid chunk, Arrow arrow) =>
   arrow
      (Zip.T ctrl
         (EventListTT.T StrictTime [PIO.T ctrl chunk]))
      (Zip.T ctrl
         (EventListTT.T StrictTime (PIO.T ctrl chunk)))
flattenControlSchedule = arr $
   \(Zip.Cons ctrl evs) ->
      -- Zip.consChecked "flattenControlSchedule" ctrl $
      Zip.Cons ctrl $
      flatten (arr (const mempty)) evs



data CausalState a b =
   forall state.
   CausalState
      (a -> state -> IO (b, state))
      (state -> IO ())
      state

_applyChunkSimple :: CausalState a b -> a -> IO (b, CausalState a b)
_applyChunkSimple (CausalState next delete state0) input = do
   (output, state1) <- next input state0
   return (output, CausalState next delete state1)

applyChunk ::
   (CutG.Read a, CutG.Read b) =>
   CausalState a b -> a -> IO (b, Maybe (CausalState a b))
applyChunk (CausalState next delete state0) input = do
   (output, state1) <- next input state0
   cs <-
      if CutG.length output < CutG.length input
        then do
           delete state1
           return Nothing
        else return $ Just $ CausalState next delete state1
   return (output, cs)

-- could be moved to synthesizer-core
applyModulation ::
   (CutG.Transform ctrl, CutG.NormalForm ctrl,
    CutG.Read chunk,
    Monoid time, ToInteger.C time) =>
   PIO.T
      (Zip.T ctrl (EventListTT.T time (PIO.T ctrl chunk)))
      (EventListTT.T time chunk)
applyModulation = PIO.Cons
   (\(Zip.Cons ctrl evs) acc0 -> do
      acc1 <- mapM (flip applyChunk ctrl) acc0
      let (accChunks, acc2) = unzip acc1

      (newChunks, newAcc) <-
         MW.runWriterT $
         flip MS.evalStateT ctrl $
         EventListTT.mapM
            (\time -> do
               ctrl_ <- MS.gets (CutG.drop (fromIntegral time))
               MS.put ctrl_
               return (case CutG.evaluateHead ctrl_ of () -> time))
            (\(PIO.Cons next create delete) -> do
               state0 <- liftIO create
               (chunk, state1) <-
                  liftIO . applyChunk (CausalState next delete state0)
                  =<< MS.get
               MT.lift $ MW.tell $ maybeToList state1
               return chunk)
            evs

      return
         (EventListTM.prependBodyEnd
             (EventList.fromPairList $
              map ((,) mempty) accChunks)
             newChunks,
          Maybe.catMaybes acc2 ++ newAcc))

   (return [])
   (mapM_ (\(CausalState _ close state) -> close state))

-- move synthesizer-core:CausalIO
arrangeStorable ::
   (Arrow arrow, Storable a, Additive.C a) =>
   arrow
      (EventListTT.T StrictTime (SV.Vector a))
      (SV.Vector a)
arrangeStorable =
   arr $ \evs ->
   SVST.runSTVector (do
      v <- SVST.new (fromIntegral $ EventListTT.duration evs) zero
      mapM_ (uncurry $ CutSt.addChunkToBuffer v) $
         AbsEventList.toPairList $
         AbsEventList.mapTime fromIntegral $
         EventList.toAbsoluteEventList 0 $
         EventListTM.switchTimeR const evs
      return v)



sequenceCore ::
   (Check.C event, Monoid chunk, CutG.Read chunk, Trans.C y) =>
   MIDIEv.Channel ->
   Bank y chunk ->
   PIO.T (Events event) (EventListTT.T StrictTime chunk)
sequenceCore channel bank =
   applyModulation
   .
   flattenControlSchedule
   .
   applyInstrument (velFreqBank bank)
   .
   assignNoteIds
   .
   embedPrograms (VoiceMsg.toProgram 0)
   .
   noteEvents channel


sequenceModulated ::
   (Check.C event, Monoid chunk, CutG.Read chunk,
    CutG.Transform ctrl, CutG.NormalForm ctrl, Trans.C y) =>
   MIDIEv.Channel ->
   ModulatedBank y ctrl chunk ->
   PIO.T (Zip.T (Events event) ctrl) (EventListTT.T StrictTime chunk)
sequenceModulated channel bank =
   applyModulation
   .
   flattenControlSchedule
   .
   applyModulatedInstrument (velFreqBank bank)
   .
   Zip.arrowFirst
      (assignNoteIds
       .
       embedPrograms (VoiceMsg.toProgram 0)
       .
       noteEvents channel)


sequenceModulatedMultiProgram ::
   (Check.C event, Monoid chunk, CutG.Read chunk,
    CutG.Transform ctrl, CutG.NormalForm ctrl, Trans.C y) =>
   MIDIEv.Channel ->
   MIDIEv.Program ->
   ModulatedBank y ctrl chunk ->
   PIO.T (Zip.T (Events event) ctrl) (EventListTT.T StrictTime chunk)
sequenceModulatedMultiProgram channel initPgm bank =
   applyModulation
   .
   flattenControlSchedule
   .
   applyModulatedInstrument (velFreqBank bank)
   .
   Zip.arrowFirst
      (assignNoteIds
       .
       embedPrograms initPgm
       .
       noteEvents channel)


-- | may replace the other functions
sequenceModulatedMultiProgramVelocityPitch ::
   (Check.C event, Monoid chunk, CutG.Read chunk,
    CutG.Transform ctrl, CutG.NormalForm ctrl) =>
   MIDIEv.Channel ->
   MIDIEv.Program ->
   (MIDIEv.Program -> MIDIEv.Velocity -> MIDIEv.Pitch ->
    PIO.T (Zip.T GateChunk ctrl) chunk) ->
   PIO.T (Zip.T (Events event) ctrl) (EventListTT.T StrictTime chunk)
sequenceModulatedMultiProgramVelocityPitch channel initPgm bank =
   applyModulation
   .
   flattenControlSchedule
   .
   applyModulatedInstrument bank
   .
   Zip.arrowFirst
      (assignNoteIds
       .
       embedPrograms initPgm
       .
       noteEvents channel)


sequenceStorable ::
   (Check.C event, Storable a, Additive.C a, Trans.C y) =>
   MIDIEv.Channel ->
   Bank y (SV.Vector a) ->
   PIO.T (Events event) (SV.Vector a)
sequenceStorable channel bank =
   arrangeStorable
   .
   sequenceCore channel bank