relpipe-data/examples/jack-midi-1.sql
author František Kučera <franta-hg@frantovo.cz>
Thu, 22 Oct 2020 01:51:32 +0200
branchv_0
changeset 317 fce3d6290c40
permissions -rw-r--r--
Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
317
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
WITH
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
	-- Configuration parameters:
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
	duration AS (SELECT    16 AS value), -- in seconds
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
	bpm      AS (SELECT   120 AS value), -- beats per minute
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
	-- Numbered beats, the outline:
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
	beat AS (
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
		WITH RECURSIVE beat0 AS (
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
			SELECT 0 AS value
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
			UNION ALL
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
			SELECT beat0.value + 1 FROM beat0, duration, bpm
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
			WHERE beat0.value < (duration.value * bpm.value / 60 - 1)
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
		)
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
		SELECT * FROM beat0
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
	),
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
	-- Set instruments for particular channels:
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
	raw AS (
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
		SELECT 0 AS time, 'c0 0e' AS raw UNION ALL   -- channel 1 = Tubular Bells
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
		SELECT 0 AS time, 'c1 58' AS raw             -- channel 2 = Fantasia
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
	),
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
	-- Parts (quite random sounds):
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
	drums_1 AS (
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
		SELECT
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
			9 AS channel,
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
			beat.value AS beat, NULL AS custom_time,
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
			250 * 1000 AS duration,
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
			CASE WHEN beat.value % 4 IN (0,1,2) THEN 35 ELSE 40 END AS note_pitch,
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
			80 AS note_velocity
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
		FROM beat, bpm
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
	),
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
	bells_1 AS (
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
		SELECT
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
			0 AS channel,
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
			beat.value AS beat, NULL AS custom_time,
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
			500 * 1000 AS duration,
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
			CASE WHEN beat.value / 4 % 2 = 0 THEN 68 ELSE 55 END AS note_pitch,
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
			90 AS note_velocity
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
		FROM beat, bpm
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
		WHERE beat.value % 4 IN (1)
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	),
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	fantasia_1 AS (
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
		SELECT
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
			1 AS channel,
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
			beat.value AS beat, NULL AS custom_time,
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
			500 * 1000 AS duration,
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
			60 + beat.value % 4 AS note_pitch,
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
			90 AS note_velocity
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
		FROM beat, bpm
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
		WHERE beat.value % 4 IN (2,3,0)
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	),
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	-- Put all parts together:
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	notes AS (
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		SELECT
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
			part.*,
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
			CASE
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
				WHEN custom_time IS NULL THEN beat * 1000 * 1000 * 60 / bpm.value
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
				ELSE CAST(custom_time AS integer)
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
			END AS time
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
		FROM (
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
			SELECT * FROM drums_1        UNION ALL
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
			SELECT * FROM bells_1        UNION ALL
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
			SELECT * FROM fantasia_1
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
		) AS part, bpm
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
	)
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
-- We need to emit two MIDI events: one for key press and one for key release.
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
-- So we add the release events (note_on = false) derived from the time and duration values.
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
SELECT 'note'  AS event, time,                  channel, 1 AS note_on,      note_pitch,      note_velocity, '' AS raw FROM notes   UNION ALL
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
SELECT 'note'  AS event, time+duration AS time, channel, 0 AS note_on,      note_pitch,      note_velocity, '' AS raw FROM notes   UNION ALL
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
SELECT 'sysex' AS event, time,             0 AS channel, 0 AS note_on, 0 AS note_pitch, 0 AS note_velocity,       raw FROM raw
fce3d6290c40 Release v0.16 – JACK MIDI output, ports + rename Guile to Scheme + improve CLI interfaces
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
ORDER BY time ASC, event DESC;