-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
225 lines (189 loc) · 5.72 KB
/
main.lua
File metadata and controls
225 lines (189 loc) · 5.72 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import "CoreLibs/graphics"
import "CoreLibs/sprites"
import "CoreLibs/animation"
import "CoreLibs/animator"
import "CoreLibs/easing"
import "CoreLibs/timer"
import "tape"
import "tape_gfx"
local gfx <const> = playdate.graphics
local geom <const> = playdate.geometry
local snd <const> = playdate.sound
local curr_tape
local tape_gfxs = {
makeTapeGFX("images/tape_star"),
makeTapeGFX("images/tape_a"),
makeTapeGFX("images/tape_b"),
makeTapeGFX("images/tape_c"),
makeTapeGFX("images/tape_d")
}
local tapes = {
makeTape(),
makeTape(),
makeTape(),
makeTape(),
makeTape()
}
local modePlaybackSpeed <const> = 1
local modeStartOffset <const> = 2
local modeLength <const> = 3
local modeVolume <const> = 4
local edit_mode = modePlaybackSpeed
local tape_index = 1
local tape_animator = gfx.animator.new(500, 0, 0, playdate.easingFunctions.inOutCubic)
local tape_width = 420
function startup()
curr_tape = tapes[1]
for index, tape_gfx in ipairs(tape_gfxs) do
moveTapeGFXTo(tape_gfx, tape_width * index - tape_width, 0)
setTapeGFXRate(tape_gfx, 1, 4)
setTapeGFXStartOffset(tape_gfx, 0)
setTapeGFXTargetLength(tape_gfx, 1)
end
end
startup()
local max_rate <const> = 4
function playdate.BButtonDown()
if isPlaying(curr_tape) then
stopPlaying(curr_tape)
end
if curr_tape.queued then
curr_tape.queued = false
end
-- if its the first tape, start recording
if tape_index == 1 or not isPlaying(tapes[1]) then
startRecording(curr_tape)
else
-- otherwise, queue the recording
curr_tape.queuedRecording = true
end
end
function playdate.BButtonUp()
if curr_tape.queuedRecording then
curr_tape.queuedRecording = false
end
stopRecording(curr_tape)
end
function playdate.AButtonDown()
-- if not recording, toggle queue and stop playing if necessary
if not playdate.buttonIsPressed(playdate.kButtonB) then
curr_tape.queued = not curr_tape.queued
if not curr_tape.queued then
stopPlaying(curr_tape)
end
end
end
function playdate.upButtonDown()
edit_mode -= 1
if edit_mode < 1 then
edit_mode = modeVolume
end
setLabelIndex(edit_mode)
end
function playdate.downButtonDown()
edit_mode += 1
if edit_mode > modeVolume then
edit_mode = 1
end
setLabelIndex(edit_mode)
end
function playdate.leftButtonDown()
tape_index -= 1
if tape_index < 1 then
tape_index = #tapes
end
curr_tape = tapes[tape_index]
tape_animator = gfx.animator.new(500,
tape_animator:currentValue(),
tape_index * tape_width - tape_width,
playdate.easingFunctions.inOutCubic)
end
function playdate.rightButtonDown()
tape_index += 1
if tape_index > #tapes then
tape_index = 1
end
curr_tape = tapes[tape_index]
tape_animator = gfx.animator.new(500,
tape_animator:currentValue(),
tape_index * tape_width - tape_width,
playdate.easingFunctions.inOutCubic)
end
function playdate.update()
local curr_tape_gfx = tape_gfxs[tape_index]
local playbackTime = getLength(tapes[1])
if playdate.getElapsedTime() > playbackTime then
playdate.resetElapsedTime()
for i, tape in ipairs(tapes) do
-- only play queued tapes
if tape.queued then
-- always replay the first tape, not necessarily the other tracks
if i == 1 or not isPlaying(tape) then
startPlaying(tape)
end
end
end
if curr_tape.queuedRecording then
curr_tape.queuedRecording = false
startRecording(curr_tape)
end
end
local change = playdate.getCrankChange()
if edit_mode == modePlaybackSpeed then
curr_tape.rate += change / 1440
if curr_tape.rate < -max_rate then
curr_tape.rate = -max_rate
elseif curr_tape.rate > max_rate then
curr_tape.rate = max_rate
end
setTapeGFXRate(curr_tape_gfx, curr_tape.rate, max_rate)
elseif edit_mode == modeStartOffset then
if change ~= 0 then
curr_tape.offset += change / 1440
if curr_tape.offset > 1 then
curr_tape.offset = 1
elseif curr_tape.offset < 0 then
curr_tape.offset = 0
end
if tape_index == 1 then
playdate.resetElapsedTime()
startPlaying(curr_tape)
end
end
setTapeGFXStartOffset(curr_tape_gfx, curr_tape.offset)
elseif edit_mode == modeLength then
if change ~= 0 then
curr_tape.targetLength += change / 1440
if curr_tape.targetLength < 0 then
curr_tape.targetLength = 0
elseif curr_tape.targetLength > 1 then
curr_tape.targetLength = 1
end
if tape_index == 1 then
playdate.resetElapsedTime()
startPlaying(curr_tape)
end
end
setTapeGFXTargetLength(curr_tape_gfx, curr_tape.targetLength)
elseif edit_mode == modeVolume then
if change ~= 0 then
curr_tape.volume += change / 1440
if curr_tape.volume > 1 then
curr_tape.volume = 1
elseif curr_tape.volume < 0 then
curr_tape.volume = 0
end
end
setTapeGFXVolume(curr_tape_gfx, curr_tape.volume)
end
-- animation stuff
playdate.graphics.setDrawOffset(-tape_animator:currentValue(), 0)
if curr_tape.queued then
unpauseTapeGFX(curr_tape_gfx)
else
pauseTapeGFX(curr_tape_gfx)
end
updateTapeGFX(curr_tape_gfx)
gfx.sprite.update()
playdate.timer.updateTimers()
end