summaryrefslogtreecommitdiff
path: root/examples/audio_stream_effects/main.lua
blob: 575a5ce7ffe9f16bd58bb6628fb18692800a017f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
-- Based on raylib audio_stream_effects example

package.path = package.path..";"..RL.GetBasePath().."../resources/lib/?.lua"
package.cpath = package.cpath..";"..RL.GetBasePath().."../resources/clib/?.so"

Vector2 = require( "vector2" )
Rect = require( "rectangle" )

-- NOTE! You have to compile the C lib containing the effects in resources/clib/audioProcess.c
-- Implementing audio processors in Lua would probably be too slow.
AudioProcess = require( "audioProcess" )

local music = nil
local rect = Rect:new()
local rect2 = Rect:new()
local effectLPF = nil
local effectDelay = nil
local enableEffectLPF = false
local enableEffectDelay = false

function RL.init()
	RL.SetWindowTitle( "Audio stream effects" )
	RL.SetWindowState( RL.FLAG_VSYNC_HINT )
	RL.InitAudioDevice()

	music = RL.LoadMusicStream( RL.GetBasePath().."../resources/music/Juhani Junkala [Retro Game Music Pack] Title Screen.ogg" )

	RL.PlayMusicStream( music )

	AudioProcess.init()
	effectLPF = AudioProcess.AudioProcessEffectLPF()
	effectDelay = AudioProcess.AudioProcessEffectDelay()

	local winSize = Vector2:newT( RL.GetScreenSize() )
	local barWidth = 256

	rect:set(
		winSize.x / 2 - barWidth / 2,
		winSize.y / 2 - 5,
		barWidth,
		10
	)
	rect2:setR( rect )
end

function RL.update( delta )
	RL.UpdateMusicStream( music )

	-- Add/Remove effect: lowpass filter
	if RL.IsKeyPressed( RL.KEY_F ) then
		local stream = RL.GetMusicStream( music )
		enableEffectLPF = not enableEffectLPF

		if enableEffectLPF then
			RL.AttachAudioStreamProcessor( stream, effectLPF )
			-- RL.AttachAudioMixedProcessor( effectLPF )
		else
			RL.DetachAudioStreamProcessor( stream, effectLPF )
			-- RL.DetachAudioMixedProcessor( effectLPF )
		end
	end

	-- Add/Remove effect: delay
	if RL.IsKeyPressed( RL.KEY_D ) then
		local stream = RL.GetMusicStream( music )
		enableEffectDelay = not enableEffectDelay

		if enableEffectDelay then
			RL.AttachAudioStreamProcessor( stream, effectDelay )
			-- RL.AttachAudioMixedProcessor( effectDelay )
		else
			RL.DetachAudioStreamProcessor( stream, effectDelay )
			-- RL.DetachAudioMixedProcessor( effectDelay )
		end
	end
end

function RL.draw()
	local musicLen = RL.GetMusicTimeLength( music )
	local musicPos = RL.GetMusicTimePlayed( music )

	RL.ClearBackground( RL.RAYWHITE )

	RL.DrawText(
		"PRESS F TO TOGGLE LPF EFFECT "..( enableEffectLPF and "ON" or "OFF" ),
		Vector2:temp( rect.x - 80, rect.y - 80 ), 20, RL.GRAY
	)
	RL.DrawText(
		"PRESS D TO TOGGLE DELAY EFFECT "..( enableEffectDelay and "ON" or "OFF" ),
		Vector2:temp( rect.x - 90, rect.y - 40 ), 20, RL.GRAY
	)

	RL.DrawRectangleLines( rect + Rect:temp( 0, -1, 1, 1 ), RL.BLACK )
	rect2.width = musicPos / musicLen * rect.width
	RL.DrawRectangle( rect2, RL.RED )
end

function RL.exit()
	AudioProcess.free()
end