-- region Lesson 1 musical notes
-- If you 'run' a pattern by itself, without a 'd1' or so, then Tidal
-- will do its best at telling you what's in the first cycle. For
-- example:

note "3"

-- gives:

-- (0>1)|note: 3.0f

-- 0>1 tells you it's an event that starts at position 0 (the start of
-- the first cycle) and lasts up to 1 (the start of the next cycle).
-- note is the name of the 'control' or 'effect' 3.0f is the value
-- ('f' tells you that it's a floating point, decimal number).

note "3 ~ 5"

-- the above gives two events:

-- (0>⅓)|note: 3.0f
-- (⅔>1)|note: 5.0f

-- We can listen to them:

d1 $ note "3 ~ 5" # s "superpiano"

-- Great notes!

-- (.. if you don't hear any, you probably need to install "sc3plugins".)

-- Tidal can also understand note names, and turn them into numbers
-- for you.

-- For example 'c' is the same as '0'

note "c"

-- This:

note "a b c d e f g"

-- is the same as:

note "9 11 0 2 4 5 7"

-- What happened to 1, 3, 6, 8, and 10?
-- You can get to them by adding 's' for 'sharp', to add 1 to a note:

note "cs ds fs gs as"

-- or by using 'f' for 'flat' to subtract 1:

note "df ef gf af bf"

-- In theory, you can get to them all via really sharp 'c'
-- notes. These two notes are identical:
d1 $ note "csssssss g" # s "superpiano"

-- In practice, that surely doesn't make a lot of sense.

-- Normally, there are twelve notes in an octave. The default octave
-- is 5, you can pick notes from other octaves by adding a different
-- number:
note "c5 c6 c4 c6"

-- Lets have a listen
d1 $ note "c5 c6 c4 c6" # s "superpiano"

-- Lets think about the difference between 'note', 'n', synths and
-- samples.

-- There is no folder of samples called 'superpiano', the sounds you
-- hear are being synthesised on-the-fly.

-- With synths, you can use either 'note' or 'n' to specify notes,
-- they mean the same thing.

d1 $ n "c a f e" # s "superpiano"

d1 $ note "c a f e" # s "superpiano"

-- For samples, they mean something different. 'n' chooses a sample,
-- 'note' plays it at a different speed, corresponding to a note.

-- Different sounds:
d1 $ n "0 1" # sound "dsynth"

-- Different notes:
d1 $ note "0 1" # sound "dsynth"

-- If you pick a high note, then you'll notice the sound is a lot
-- shorter, because it's making it higher by playing it faster.
d1 $ note "0 24" # sound "dsynth"

-- You might feel that's not good, because it doesn't sound as natural
-- as a synthesiser
-- You might feel that's great, because nature is a myth and this is
-- how old school 'tracker' music from early rave music and the
-- demoscene works
-- You might change your mind on different days

-- You can still use note names in mininotation:
d1 $ note "c a f e" # sound "dsynth"

-- (Actually you can use do this in any control/effect pattern that
-- expects a number.. Tidal just treats them as numbers)

-- This dsynth sample is in 'c'. If it wasn't, the notes would
-- probably sound out of tune with another synth or samplebank.

-- The 'dbass' sample has three bass sounds, again in 'c', of
-- different lengths.  So it makes sense to use *both* 'note' and 'n'
-- together, to pattern both the pitch and the sample that's used:
d1 $ note "c a f e" # sound "dbass" # n "<0 1 2>"

-- The 'rash' samplebank is organised differently.. There's a load of
-- samples, one for each note of 6 octaves. There's 12 notes in an
-- octave, so that's 72 samples. (actually there's 73, there's an
-- extra one note-084.wav which you could delete..) I sampled these
-- from my lovely Roland JV1080 synth.

-- So you can play notes as numbers using the 'n' instead of the
-- 'note' pattern. This sounds a bit more 'natural' than pitching them
-- up with 'note'.
d1 $ n "20 50" # sound "rash"

-- You can still use note names, but whereas for synths '0' is *middle*
-- c, with these samples that's right at the *bottom* of the scale.
d1 $ n "c a f e" # sound "rash"

-- So in this case you'll want to pick a higher octave
d1 $ n "c7 a7 f8 e7" # sound "rash"

-- I tend to add a few octaves like this:
d1 $ n "c a f e" # sound "rash"
  |+ n 24

-- Adding notes together is fun :
d1 $ n "c a f e" # sound "rash"
  |+ n 24
  |+ n "<0 2 7 12>"

-- You can also do it this way, adding together number patterns
-- 'inside' a single control pattern
d1 $ n ("c a f e" |+ 24 |+ "<0 2 7 12>")
  # sound "rash"

-- There's also an 'octave' control to jump up/down in twelves:
d1 $ note "c a f e" # sound "superpiano"
  # octave "<4 6 3>"

-- endregion

-- region Lesson 2 chords / arpeggios / AlgoKaraoke

-- Ok chords! We can play a 'major' chord like this:

d1 $ n "'maj" # sound "supermandolin"
  # legato 2 # gain 1.4

-- The default is c major, you can choose others like this, e.g. to
-- play c then e major:
d1 $ n "c'maj e'maj" # sound "supermandolin"
  # legato 2 # gain 1.4

-- Karaoke (algoraoke) time
-- Lets take the chord from a well known song:
-- https://ukutabs.com/r/radiohead/creep/

d1 $ n "<g'maj b'maj c'maj c'min>" # s "supermandolin"
  # room 0.6 # sz 0.9

-- and strum it a bit with struct:
d1
  $ qtrigger 1
  $ jux ((|- n "12") . rev)
  $ struct "t(5,8,<0 4>)"
  $ n "<g'maj b'maj c'maj c'min>" # s "supermandolin"
  # room 0.6 # sz 0.9

-- You can get a list of all the chords like this:
import Sound.Tidal.Chords

chordList

-- Try some out:
d1 $ n "c'sevenFlat9 a'm9sharp5" # sound "supermandolin"

-- Here's the raw data:
chordTable

-- Again, this all ends up being turned into plain note numbers. These
-- two patterns are the same:
d1 $ n "c'sevenFlat9 a'm9sharp5" # sound "supermandolin"

d1 $ n "[0,4,7,10,13] [9,10,23]" # sound "supermandolin"

-- You can say how many notes you want in a chord, with another ' and
-- the number of notes you want.

-- If you ask for more notes than exist in the basic chord, it will go
-- up the octaves to find more notes, sounding more and more impressive:
d1 $ n "c'maj'4" # s "superpiano"
d1 $ n "c'maj'8" # s "superpiano"
d1 $ n "c'maj'4" # s "superpiano"

-- This is clearer when we start doing.. ARPEGGIOS

-- These are 'broken' chords, where instead of playing the notes at
-- once, they're played one after another:
d1 $ arpeggiate $ n "c'maj" # s "superpiano"

-- The arpeggio happens within the 'step' that the chord occupies:
d1 $ arpeggiate $ n "c'maj e'min7" # s "superpiano"

-- Above, you can hear major chords have three notes, and minor 7
-- chords have four. You can modify that with ' so they have the same
-- number, if you want:
d1 $ arpeggiate $ n "c'maj'4 e'min7'4" # s "superpiano"

-- "arpeggiate" has a shorter, but more flexible cousin "arp", that
-- allows you to specify a different way of breaking up the chord:
d1 $ arp "updown thumbup" $ n "<c'maj'4 e'min7'4>" # s "superpiano"

-- Here's the list of currently available arp styles to explore:
-- up, down, updown, downup, converge, diverge, disconverge, pinkyup,
-- pinkyupdown, thumbup thumbupdown

-- Lots of fun
d1 $ jux rev $ arp "<updown thumbup pinkyupdown converge diverge>"
  $ n "<c4'maj'6 e4'min7'8 g5'maj'5 a5'maj'4>" # s "superpiano"
  # room 0.3 # sz 0.7
-- endregion

-- region Lesson
-- endregion

-- region Lesson
-- endregion

-- region Lesson
-- endregion

-- region Exercises
do
  d1 $ "jazz*4" # gain 0.6
  d2 $ fast "<2 [4 8]>" $ s "drum:8"
  d3 $ s "~ clap:3" # hpf (slow 40 $ range 1000 7500 sine)
  d4 $ n ("<c4'maj!4 c4'min!4>") # s "rash" # gain 0.8
-- endregion