INSERT OR REPLACE INTO audio_tracks (id, sample_rate, format) VALUES ('supermariobros', 8000, 'u8') RETURNING id;

WITH RECURSIVE
  audio AS
    (SELECT *, 225 AS beat, 160 AS gain FROM audio_tracks WHERE id = 'supermariobros'),
  notes AS (SELECT
    440.0000 AS A4,
    466.1638 AS Bflat4,
    493.8833 AS B4,
    261.6256 AS C4,
    329.6276 AS E4,
    391.9954 AS G4,
    880.0000 AS A5,
    523.2511 AS C5,
    587.3295 AS D5,
    622.2540 AS Dsharp5,
    659.2250 AS E5,
    698.4565 AS F5,
    739.9888 AS Fsharp5,
    783.9909 AS G5,
    1760.000 AS A6,
    1046.502 AS C6,
    1318.510 AS E6,
    1396.913 AS F6,
    1567.982 AS G6),
  melody(m_id, mduration, mnote) AS
    (SELECT ROW_NUMBER() OVER () as m_id, * FROM (VALUES
      (5,  (SELECT E5 FROM notes))     , (1,  0),
      (5,  (SELECT E5 FROM notes))     , (6,  0),
      (5,  (SELECT E5 FROM notes))     , (6,  0),
      (5,  (SELECT C5 FROM notes))     , (1,  0),
      (5,  (SELECT E5 FROM notes))     , (6,  0),
      (5,  (SELECT G5 FROM notes))     , (17, 0),
      (5,  (SELECT G4 FROM notes))     , (17, 0),
      --
      (5,  (SELECT C5 FROM notes))     , (12, 0),
      (5,  (SELECT G4 FROM notes))     , (10, 0),
      (5,  (SELECT E4 FROM notes))     , (7,  0),
      (5,  (SELECT A4 FROM notes))     , (6,  0),
      (5,  (SELECT B4 FROM notes))     , (5,  0),
      (5,  (SELECT Bflat4 FROM notes)) , (1,  0),
      (5,  (SELECT A4 FROM notes))     , (6,  0),
      (5,  (SELECT G4 FROM notes))     , (2,  0),
      (5,  (SELECT E5 FROM notes))     , (2,  0),
      (5,  (SELECT G5 FROM notes))     , (2,  0),
      (5,  (SELECT A5 FROM notes))     , (6,  0),
      (5,  (SELECT F5 FROM notes))     , (1,  0),
      (5,  (SELECT G5 FROM notes))     , (5,  0),
      (5,  (SELECT E5 FROM notes))     , (8,  0),
      (5,  (SELECT C5 FROM notes))     , (1,  0),
      (5,  (SELECT D5 FROM notes))     , (1,  0),
      (5,  (SELECT B4 FROM notes))     , (12,  0),
      --
      (5,  (SELECT C5 FROM notes))     , (12, 0),
      (5,  (SELECT G4 FROM notes))     , (10, 0),
      (5,  (SELECT E4 FROM notes))     , (7,  0),
      (5,  (SELECT A4 FROM notes))     , (6,  0),
      (5,  (SELECT B4 FROM notes))     , (5,  0),
      (5,  (SELECT Bflat4 FROM notes)) , (1,  0),
      (5,  (SELECT A4 FROM notes))     , (6,  0),
      (5,  (SELECT G4 FROM notes))     , (2,  0),
      (5,  (SELECT E5 FROM notes))     , (2,  0),
      (5,  (SELECT G5 FROM notes))     , (2,  0),
      (5,  (SELECT A5 FROM notes))     , (6,  0),
      (5,  (SELECT F5 FROM notes))     , (1,  0),
      (5,  (SELECT G5 FROM notes))     , (5,  0),
      (5,  (SELECT E5 FROM notes))     , (8,  0),
      (5,  (SELECT C5 FROM notes))     , (1,  0),
      (5,  (SELECT D5 FROM notes))     , (1,  0),
      (5,  (SELECT B4 FROM notes))     , (24,  0),
      --
      (5,  (SELECT G5 FROM notes))     , (1, 0),
      (5,  (SELECT Fsharp5 FROM notes)), (1, 0),
      (5,  (SELECT F5 FROM notes))     , (1,  0),
      (5,  (SELECT D5 FROM notes))     , (8,  0),
      (5,  (SELECT E5 FROM notes))     , (6,  0),
      (5,  (SELECT G4 FROM notes))     , (1, 0),
      (5,  (SELECT A4 FROM notes))     , (1,  0),
      (5,  (SELECT C5 FROM notes))     , (6,  0),
      (5,  (SELECT A4 FROM notes))     , (1, 0),
      (5,  (SELECT C5 FROM notes))     , (1,  0),
      (5,  (SELECT D5 FROM notes))     , (12,  0),
      --
      (5,  (SELECT G5 FROM notes))     , (1, 0),
      (5,  (SELECT Fsharp5 FROM notes)), (1, 0),
      (5,  (SELECT F5 FROM notes))     , (1,  0),
      (5,  (SELECT D5 FROM notes))     , (8,  0),
      (5,  (SELECT E5 FROM notes))     , (6,  0),
      (5,  (SELECT C6 FROM notes))     , (4,  0),
      (5,  (SELECT C6 FROM notes))     , (1,  0),
      (5,  (SELECT C6 FROM notes))     , (36,  0),
      --
      (5,  (SELECT G5 FROM notes))     , (1, 0),
      (5,  (SELECT Fsharp5 FROM notes)), (1, 0),
      (5,  (SELECT F5 FROM notes))     , (1,  0),
      (5,  (SELECT D5 FROM notes))     , (8,  0),
      (5,  (SELECT E5 FROM notes))     , (6,  0),
      (5,  (SELECT G4 FROM notes))     , (1, 0),
      (5,  (SELECT A4 FROM notes))     , (1,  0),
      (5,  (SELECT C5 FROM notes))     , (6,  0),
      (5,  (SELECT A4 FROM notes))     , (1, 0),
      (5,  (SELECT C5 FROM notes))     , (1,  0),
      (5,  (SELECT D5 FROM notes))     , (12,  0),
      --
      (5,  (SELECT Dsharp5 FROM notes)), (12,  0),
      (5,  (SELECT D5 FROM notes))     , (12,  0),
      (5,  (SELECT C5 FROM notes))     , (36,  0),
      --
      (5,  (SELECT C5 FROM notes))     , (1,  0),
      (5,  (SELECT C5 FROM notes))     , (6,  0),
      (5,  (SELECT C5 FROM notes))     , (6,  0),
      (5,  (SELECT C5 FROM notes))     , (1,  0),
      (5,  (SELECT D5 FROM notes))     , (6,  0),
      (5,  (SELECT E5 FROM notes))     , (1,  0),
      (5,  (SELECT C5 FROM notes))     , (6,  0),
      (5,  (SELECT A4 FROM notes))     , (1,  0),
      (5,  (SELECT G4 FROM notes))     , (17,  0),
      --
      (5,  (SELECT C5 FROM notes))     , (1,  0),
      (5,  (SELECT C5 FROM notes))     , (6,  0),
      (5,  (SELECT C5 FROM notes))     , (6,  0),
      (5,  (SELECT C5 FROM notes))     , (1,  0),
      (5,  (SELECT D5 FROM notes))     , (1,  0),
      (5,  (SELECT E5 FROM notes))     , (40,  0),
      --
      (5,  (SELECT C5 FROM notes))     , (1,  0),
      (5,  (SELECT C5 FROM notes))     , (6,  0),
      (5,  (SELECT C5 FROM notes))     , (6,  0),
      (5,  (SELECT C5 FROM notes))     , (1,  0),
      (5,  (SELECT D5 FROM notes))     , (6,  0),
      (5,  (SELECT E5 FROM notes))     , (1,  0),
      (5,  (SELECT C5 FROM notes))     , (6,  0),
      (5,  (SELECT A4 FROM notes))     , (1,  0),
      (5,  (SELECT G4 FROM notes))     , (17,  0),
      --
      (5,  (SELECT E5 FROM notes))     , (1,  0),
      (5,  (SELECT E5 FROM notes))     , (6,  0),
      (5,  (SELECT E5 FROM notes))     , (6,  0),
      (5,  (SELECT C5 FROM notes))     , (1,  0),
      (5,  (SELECT E5 FROM notes))     , (6,  0),
      (5,  (SELECT G5 FROM notes))     , (17,  0),
      (5,  (SELECT G4 FROM notes))     , (17,  0),
      --
      (5,  (SELECT C5 FROM notes))     , (12, 0),
      (5,  (SELECT G4 FROM notes))     , (10, 0),
      (5,  (SELECT E4 FROM notes))     , (8,  0),
      (5,  (SELECT A4 FROM notes))     , (6,  0),
      (5,  (SELECT B4 FROM notes))     , (5,  0),
      (5,  (SELECT Bflat4 FROM notes)) , (1,  0),
      (5,  (SELECT A4 FROM notes))     , (6,  0),
      (5,  (SELECT G4 FROM notes))     , (2,  0),
      (5,  (SELECT E5 FROM notes))     , (2,  0),
      (5,  (SELECT G5 FROM notes))     , (2,  0),
      (5,  (SELECT A5 FROM notes))     , (6,  0),
      (5,  (SELECT F5 FROM notes))     , (1,  0),
      (5,  (SELECT G5 FROM notes))     , (5,  0),
      (5,  (SELECT E5 FROM notes))     , (8,  0),
      (5,  (SELECT C5 FROM notes))     , (1,  0),
      (5,  (SELECT D5 FROM notes))     , (1,  0),
      (5,  (SELECT B4 FROM notes))     , (12,  0),
      --
      (5,  (SELECT C5 FROM notes))     , (12, 0),
      (5,  (SELECT G4 FROM notes))     , (10, 0),
      (5,  (SELECT E4 FROM notes))     , (8,  0),
      (5,  (SELECT A4 FROM notes))     , (6,  0),
      (5,  (SELECT B4 FROM notes))     , (5,  0),
      (5,  (SELECT Bflat4 FROM notes)) , (1,  0),
      (5,  (SELECT A4 FROM notes))     , (6,  0),
      (5,  (SELECT G4 FROM notes))     , (2,  0),
      (5,  (SELECT E5 FROM notes))     , (2,  0),
      (5,  (SELECT G5 FROM notes))     , (2,  0),
      (5,  (SELECT A5 FROM notes))     , (6,  0),
      (5,  (SELECT F5 FROM notes))     , (1,  0),
      (5,  (SELECT G5 FROM notes))     , (5,  0),
      (5,  (SELECT E5 FROM notes))     , (8,  0),
      (5,  (SELECT C5 FROM notes))     , (1,  0),
      (5,  (SELECT D5 FROM notes))     , (1,  0),
      (5,  (SELECT B4 FROM notes))     , (17,  0),
      --
      (5,  (SELECT E5 FROM notes))     , (0,  0),
      (5,  (SELECT C5 FROM notes))     , (6,  0),
      (5,  (SELECT G4 FROM notes))     , (12,  0),
      (5,  (SELECT G4 FROM notes))     , (6,  0),
      (5,  (SELECT A4 FROM notes))     , (2,  0),
      (5,  (SELECT F5 FROM notes))     , (4,  0),
      (5,  (SELECT F5 FROM notes))     , (2,  0),
      (5,  (SELECT A4 FROM notes))     , (12,  0),
      --
      (5,  (SELECT B4 FROM notes))     , (1,  0),
      (5,  (SELECT A5 FROM notes))     , (3,  0),
      (5,  (SELECT A5 FROM notes))     , (1,  0),
      (5,  (SELECT A5 FROM notes))     , (4,  0),
      (5,  (SELECT G5 FROM notes))     , (1,  0),
      (5,  (SELECT F5 FROM notes))     , (1,  0),
      (5,  (SELECT E5 FROM notes))     , (2,  0),
      (5,  (SELECT C5 FROM notes))     , (6,  0),
      (5,  (SELECT A4 FROM notes))     , (1,  0),
      (5,  (SELECT G4 FROM notes))     , (17,  0),
      --
      (5,  (SELECT E5 FROM notes))     , (0,  0),
      (5,  (SELECT C5 FROM notes))     , (6,  0),
      (5,  (SELECT G4 FROM notes))     , (12,  0),
      (5,  (SELECT G4 FROM notes))     , (6,  0),
      (5,  (SELECT A4 FROM notes))     , (2,  0),
      (5,  (SELECT F5 FROM notes))     , (4,  0),
      (5,  (SELECT F5 FROM notes))     , (2,  0),
      (5,  (SELECT A4 FROM notes))     , (12,  0),
      --
      (5,  (SELECT B4 FROM notes))     , (1,  0),
      (5,  (SELECT F5 FROM notes))     , (3,  0),
      (5,  (SELECT F5 FROM notes))     , (1,  0),
      (5,  (SELECT F5 FROM notes))     , (4,  0),
      (5,  (SELECT E5 FROM notes))     , (1,  0),
      (5,  (SELECT D5 FROM notes))     , (1,  0),
      (5,  (SELECT C5 FROM notes))     , (2,  0),
      (5,  (SELECT G4 FROM notes))     , (6,  0),
      (5,  (SELECT E4 FROM notes))     , (1,  0),
      (5,  (SELECT C4 FROM notes))     , (17,  0),
      --
      (1,  0))),
  melody_with_offsets(mstart, mend, mnote) AS
    (SELECT
      SUM(mduration) OVER (ORDER BY m_id ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) AS mstart,
      SUM(mduration) OVER (ORDER BY m_id) AS mend,
      mnote
    FROM melody),
  note_positions(position) AS
    (SELECT 0 UNION ALL
      SELECT position + 1 FROM note_positions, audio
        WHERE position < (SELECT SUM(mduration) FROM melody) * beat),
  _samples(value, position) AS
    (SELECT
      IFNULL((SELECT mnote * position FROM melody_with_offsets
          WHERE position BETWEEN IFNULL(mstart, 0) * beat AND mend * beat
          LIMIT 1), 0) AS value,
      position
    FROM note_positions, notes, audio),
  samples(value, position) AS
    (SELECT MOD(FLOOR(value * 2.0 / sample_rate), 2) * gain, position
      FROM _samples, audio)
INSERT INTO audio_track_samples (audio_track_id, value, position) SELECT 'supermariobros', value, position FROM samples ORDER BY position;
