

AudioExample = {}

function AudioExample.start()
	UILib.init()

	AudioExample.lastTime = getTime()

	local fieldWidth = 140.0
	local fieldHeight = 12.0
	local labelHeight = 10.0
	local rowSpacing = 2.0
	local rowHeight = labelHeight + fieldHeight + rowSpacing
	local col1X = 8.0
	local col2X = 164.0
	local startY = 8.0

	local labelColor = Config.paletteNames.greyDark
	local labelTextColor = Config.paletteNames.white
	local fieldColor = Config.paletteNames.blueDark
	local fieldColorHover = Config.paletteNames.blue
	local fieldTextColor = Config.paletteNames.greyLight

	local sliderOpts = {
		fieldOffset = vec2(0.0, labelHeight),
		labelColor = labelColor,
		labelTextColor = labelTextColor,
		fieldColor = fieldColor,
		fieldColorHover = fieldColorHover,
		fieldTextColor = fieldTextColor,
	}

	local labelSize = vec2(fieldWidth, labelHeight)
	local fieldSize = vec2(fieldWidth, fieldHeight)

	local function col1Y(row) return startY + row * rowHeight end
	local function col2Y(row) return startY + row * rowHeight end

	sliderOpts.sensitivity = 0.01
	sliderOpts.defaultValue = 1.0
	AudioExample.sineOsc = UILib.slider("sine", vec3(col1X, col1Y(0), 0.0), labelSize, fieldSize, sliderOpts)

	sliderOpts.sensitivity = 1.0
	sliderOpts.defaultValue = 100.0
	AudioExample.sineOscFreq = UILib.slider("sine freq", vec3(col1X, col1Y(1), 0.0), labelSize, fieldSize, sliderOpts)

	sliderOpts.sensitivity = 0.01
	sliderOpts.defaultValue = 0.0
	AudioExample.squareOsc = UILib.slider("square", vec3(col1X, col1Y(2), 0.0), labelSize, fieldSize, sliderOpts)

	sliderOpts.sensitivity = 1.0
	sliderOpts.defaultValue = 40.0
	AudioExample.squareOscFreq = UILib.slider("square freq", vec3(col1X, col1Y(3), 0.0), labelSize, fieldSize, sliderOpts)

	sliderOpts.sensitivity = 0.01
	sliderOpts.defaultValue = 0.0
	AudioExample.sawOsc = UILib.slider("saw", vec3(col1X, col1Y(4), 0.0), labelSize, fieldSize, sliderOpts)

	sliderOpts.sensitivity = 1.0
	sliderOpts.defaultValue = 40.0
	AudioExample.sawOscFreq = UILib.slider("saw freq", vec3(col1X, col1Y(5), 0.0), labelSize, fieldSize, sliderOpts)

	sliderOpts.sensitivity = 0.01
	sliderOpts.defaultValue = 0.0
	AudioExample.noise = UILib.slider("noise", vec3(col1X, col1Y(6), 0.0), labelSize, fieldSize, sliderOpts)

	sliderOpts.sensitivity = 10.0
	sliderOpts.defaultValue = 0.0
	AudioExample.filterCutoff = UILib.slider("cutoff", vec3(col2X, col2Y(0), 0.0), labelSize, fieldSize, sliderOpts)

	sliderOpts.sensitivity = 0.01
	sliderOpts.defaultValue = 0.0
	AudioExample.filterResonance = UILib.slider("resonance", vec3(col2X, col2Y(1), 0.0), labelSize, fieldSize, sliderOpts)

	sliderOpts.sensitivity = 0.01
	sliderOpts.defaultValue = 0.0
	AudioExample.wavefold = UILib.slider("wavefold", vec3(col2X, col2Y(2), 0.0), labelSize, fieldSize, sliderOpts)

	sliderOpts.sensitivity = 0.01
	sliderOpts.defaultValue = 0.0
	AudioExample.reverb = UILib.slider("reverb", vec3(col2X, col2Y(3), 0.0), labelSize, fieldSize, sliderOpts)

	sliderOpts.sensitivity = 0.01
	sliderOpts.defaultValue = 0.0
	AudioExample.wet = UILib.slider("wet", vec3(col2X, col2Y(4), 0.0), labelSize, fieldSize, sliderOpts)

	sliderOpts.sensitivity = 0.01
	sliderOpts.defaultValue = 1.0
	AudioExample.level = UILib.slider("level", vec3(col2X, col2Y(5), 0.0), labelSize, fieldSize, sliderOpts)

	AudioExample.playButton = UILib.button("play wav", vec3(col2X, col2Y(6) + 4.0, 0.0), vec2(fieldWidth, 14.0), Config.paletteNames.greyDark, function()
		local voiceId = 2
		local pageId = 1
		playWav("sound.wav", pageId, voiceId)
	end)

	sliderOpts.sensitivity = 0.01
	sliderOpts.defaultValue = 1.0
	AudioExample.wavPitch = UILib.slider("wav pitch", vec3(col2X, col2Y(7) - 4.0, 0.0), labelSize, fieldSize, sliderOpts)
end

function AudioExample.update()
	local currentTime = getTime()
	local dt = currentTime - AudioExample.lastTime
	AudioExample.lastTime = currentTime

	local mousePos = getMousePosition()

	UILib.update()

	local voice1 = {
		sineOsc = AudioExample.sineOsc.value,
		sineOscFreq = AudioExample.sineOscFreq.value,
		squareOsc = AudioExample.squareOsc.value,
		squareOscFreq = AudioExample.squareOscFreq.value,
		sawOsc = AudioExample.sawOsc.value,
		sawOscFreq = AudioExample.sawOscFreq.value,
		noise  = AudioExample.noise.value,
		filterCutoff = AudioExample.filterCutoff.value,
		filterResonance = AudioExample.filterResonance.value,
		wavefold = AudioExample.wavefold.value,
		reverb = AudioExample.reverb.value,
		wet = AudioExample.wet.value,
		level = AudioExample.level.value,
		active = true,
	}

	local voiceId1 = 1
	setSynth(voice1, voiceId1)

	local voice2 = {
		pitch = AudioExample.wavPitch.value, 
		active = true
	}

	local voiceId2 = 2
	setSynth(voice2, voiceId2)

	-- ortho camera looking down Z axis
	local cameraMat = mat4LookAt(vec3(0.0, 0.0, 1.0), vec3(0.0, 0.0, 1.0), vec3(0.0, 1.0, 0.0))
	setCameraOrtho(cameraMat, 1.0)

	-- draw oscilloscope
	local signalSamples = getSynthSignal(voiceId1)
	if #signalSamples > 1 then
		AudioExample.cachedSignal = signalSamples
	end

	local numSamples = AudioExample.cachedSignal and #AudioExample.cachedSignal or 0

	if numSamples > 1 then
		signalSamples = AudioExample.cachedSignal
		local scopeWidth = 1.0
		local scopeHeight = 0.2
		local scopeX = -scopeWidth * 0.5
		local scopeY = -0.7

		local waveColor = vec4(80.0, 220.0, 120.0, 255.0)
		local centerColor = vec4(60.0, 60.0, 60.0, 255.0)

		-- center line
		drawLine(vec3(scopeX, scopeY, 0.0), vec3(scopeX + scopeWidth, scopeY, 0.0), centerColor)

		local step = numSamples / 512
		if step < 1 then step = 1 end

		for i = 0, 511 do
			local sampleIndex1 = math.floor(i * step) + 1
			local sampleIndex2 = math.floor((i + 1) * step) + 1
			if sampleIndex2 > numSamples then sampleIndex2 = numSamples end

			local x1 = scopeX + (i / 512) * scopeWidth
			local x2 = scopeX + ((i + 1) / 512) * scopeWidth
			local y1 = scopeY + signalSamples[sampleIndex1] * scopeHeight
			local y2 = scopeY + signalSamples[sampleIndex2] * scopeHeight

			drawLine(vec3(x1, y1, 0.0), vec3(x2, y2, 0.0), waveColor)
		end
	end

	UILib.drawCursor(mousePos)
end
