
scope Music initializer i
	import Muscii
	import Cinematic
	import SoundControl

	constant string HI_HAT_HIT_CLOSED = "musRad-khatClosed.mp3"
	constant string TOM_1_HIT         = "musRad-tom1.mp3"
	constant string TOM_4_HIT         = "musRad-tom4.mp3"

	Muscii m
	Muscii v

	public function playGameMusic takes nothing returns nothing
		m.playCounted(8).callback(() -> playGameMusic())
	end

	function pauseForGameMusic takes nothing returns nothing
		DestroyTimer(GetExpiredTimer())
		playGameMusic()
	end

	public function playVoteMusic takes nothing returns nothing
		if not Cinematic.over then
			v.playCounted(1).callback(() -> playVoteMusic())
		else
			TimerStart(CreateTimer(), 0., false, function pauseForGameMusic)
		end
	end

	function initializeVoteMusic takes nothing returns nothing
		// o x o oxo x ------
		// t4, t1

		Note tm4 = Note.create(TOM_4_HIT, 80, 1.)
		Note tm1 = Note.create(TOM_1_HIT, 80, 1.)
		Note nul = Note.create(HI_HAT_HIT_CLOSED,   0, 1.)

		Chord t4 = Chord.create(tm4)
		Chord t1 = Chord.create(tm1)
		Chord z  = Chord.create(nul)

		v = Muscii.create(t4, 120.)
		v.addChord(z)
		v.addChord(t1)
		v.addChord(z)
		v.addChord(t4)
		v.addChord(z)
		v.addChord(t4)
		v.addChord(t1)
		v.addChord(t4)
		v.addChord(z)
		v.addChord(t1)
		v.addChord(z)
		v.addChord(z)
		v.addChord(z)
		v.addChord(z)
		v.addChord(z)
		v.addChord(z)
		v.addChord(z)
	end

	function initializeGameMusic takes nothing returns nothing
		// x x - o o - x x - o o xxx ooo x x - o o -
		// t1, hh, t4

		Note hat = Note.create(HI_HAT_HIT_CLOSED,  90, 1.)
		Note nul = Note.create(HI_HAT_HIT_CLOSED,   0, 1.)
		Note tm1 = Note.create(TOM_1_HIT,         110, 1.)
		Note tm4 = Note.create(TOM_4_HIT,         110, 1.)

		Chord z
		Chord h
		Chord t1
		Chord t4

		t1 = Chord.create(tm1)
		t4 = Chord.create(tm4)
		z  = Chord.create(nul)
		h  = Chord.create(hat)

		m = Muscii.create(t1, 240.)
		m.addChord(z)
		m.addChord(t1)
		m.addChord(z)
		m.addChord(h)
		m.addChord(z)
		m.addChord(t4)
		m.addChord(z)
		m.addChord(t4)
		m.addChord(z)
		m.addChord(h)
		m.addChord(z)
		m.addChord(t1)
		m.addChord(z)
		m.addChord(t1)
		m.addChord(z)
		m.addChord(h)
		m.addChord(z)
		m.addChord(t4)
		m.addChord(z)
		m.addChord(t4)
		m.addChord(z)
		m.addChord(t1)
		m.addChord(t1)
		m.addChord(t1)
		m.addChord(z)
		m.addChord(t4)
		m.addChord(t4)
		m.addChord(t4)
		m.addChord(z)
		m.addChord(t1)
		m.addChord(z)
		m.addChord(t1)
		m.addChord(z)
		m.addChord(h)
		m.addChord(z)
		m.addChord(t4)
		m.addChord(z)
		m.addChord(t4)
		m.addChord(z)
		m.addChord(h)
		m.addChord(z)
	end

	function i takes nothing returns nothing
		initializeVoteMusic()
		initializeGameMusic()

		playVoteMusic()

		SoundControl.preload(HI_HAT_HIT_CLOSED)
		SoundControl.preload(TOM_1_HIT)
		SoundControl.preload(TOM_4_HIT)
	end
endscope
