```javascript
// @title fading @by ACorp
// @license 0BSD

setCpm(60 / 2)

const latch = (f) => {
  let start = undefined;
  return signal((t) => {
    start = start ?? t;
    return f(t - start);
  });
}

const ramp = (cycles) => latch((t) => Math.min(t / cycles, 1));
const iramp = (cycles) => latch((t) => Math.max(1 - t / cycles, 0));
register('fadein', function(cycles, gainstart, gainstop, pat) {
  return pat.gain(ramp(cycles).range(gainstart, gainstop));
})

const shush = [Math.pow(2,32)-1, silence];

const chords = chord("<CM9 Am7 Dm9 Gm7>").voicing()
const nchords = p => n(p).chord("<CM9 Am7 Dm9 Gm7>").voicing()
const i1 = chords.s("supersaw").lpf(800)
  .ph(3).phd(1).phc(4000).room(2)

const parts = [20, 32, 8, 80]
const sparts = (start, stop) => parts
  .slice(start, stop + 1)
  .reduce((sum, val) => sum + val, 0);


i1$: arrange(
  [sparts(0,1), i1.fadein(parts[0], 0.2,1)],
  [parts[2], silence],
  [parts[3], i1],
  shush
)

const i2 = nchords("[3,4] - - -").s("square,pulse")
  .decay(0.2).release(0.4)
  .lpf(1600).gain(0.6)

i2$: arrange(
  [parts[0],silence],
  [sparts(1,3), i2],
  shush
)

const i3 = nchords("1,2")
  .struct("1 0 1 1 0 1 1 0")
  .s("tri,pulse")
  .ply("2 0 1 2 1 2 2 0")

i3$: arrange(
  [parts[0],silence],
  [sparts(1,3), i3.fadein(parts[1], 0.2,1.2)],
  shush
)

const i4 = nchords(irand(4).seg(8).add(2))
  .mask("1 <[0 1] 1> 1 <0 [1 0] [1 0 1] 1> 1")
  .s("gm_piano").n("0,1")
  .lpf(2000).rarely(x=>x.ply("2"))

i4$: arrange(
  [sparts(0,2), silence],
  [parts[3], i4],
  shush
)

all(x=>x.postgain(0.8))
```