-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.gd
347 lines (243 loc) · 8.25 KB
/
Main.gd
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
extends Control
onready var AudioPlayer = $AudioStreamPlayer
onready var TrackLength = $TrackLength
onready var CurrentPlayback = $CurrentPlayback
onready var VolumeSlider = $Volume/VolumeSlider
onready var PlayBackSlider = $PlayBackSlider
onready var Playlist = $Playlist
var stream
var lowpass
var lowshelf
var songlist = []
var currentsong = ""
var currentsongidx = 0
var songplay = ""
var currenttime
var currentmin = 0
var currentsec = 0
var StartingDB = -20
var loop = false
func _ready():
add_options()
$ColorPicker.color = Color("3F985D")
VolumeSlider.value = StartingDB
AudioPlayer.volume_db = StartingDB
func _unhandled_key_input(event):
if Input.is_key_pressed(KEY_DELETE):
songlist.remove(currentsongidx)
Playlist.remove_item(currentsongidx)
func _process(delta):
if AudioPlayer.is_playing():
currenttime = int(AudioPlayer.get_playback_position())
calc_time()
PlayBackSlider.value = currenttime
func calc_time():
if currenttime >= 60:
currentmin = int(currenttime/60)
currentsec = currenttime - (currentmin * 60)
else:
currentmin = 0
currentsec = currenttime
DisplayCurrentTime()
func DisplayCurrentTime():
if currentsec < 10:
CurrentPlayback.text = "%s:0%s" %[currentmin, currentsec]
else:
CurrentPlayback.text = "%s:%s" %[currentmin, currentsec]
func play_button():
if songplay != currentsong:
PlaySong()
elif AudioPlayer.is_playing() and currentsong == songplay:
PauseMusic()
func PlaySong():
yield(get_tree(),"idle_frame")
var file = File.new()
file.open(songplay, file.READ)
yield(get_tree(),"idle_frame")
var buffer = file.get_buffer(file.get_len())
if songplay.ends_with(".wav"):
stream = AudioStreamSample.new()
for i in range(0, 100):
var those4bytes = str(char(buffer[i])+char(buffer[i+1])+char(buffer[i+2])+char(buffer[i+3]))
if those4bytes == "RIFF":
print ("RIFF OK at bytes " + str(i) + "-" + str(i+3))
if those4bytes == "WAVE":
print ("WAVE OK at bytes " + str(i) + "-" + str(i+3))
if those4bytes == "fmt ":
print ("fmt OK at bytes " + str(i) + "-" + str(i+3))
var formatsubchunksize = buffer[i+4] + (buffer[i+5] << 8) + (buffer[i+6] << 16) + (buffer[i+7] << 24)
print ("Format subchunk size: " + str(formatsubchunksize))
var fsc0 = i+8
var format_code = buffer[fsc0] + (buffer[fsc0+1] << 8)
var format_name
if format_code == 0: format_name = "8_BITS"
elif format_code == 1: format_name = "16_BITS"
elif format_code == 2: format_name = "IMA_ADPCM"
print ("Format: " + str(format_code) + " " + format_name)
stream.format = format_code
var channel_num = buffer[fsc0+2] + (buffer[fsc0+3] << 8)
print ("Number of channels: " + str(channel_num))
if channel_num == 2:
stream.stereo = true
var sample_rate = buffer[fsc0+4] + (buffer[fsc0+5] << 8) + (buffer[fsc0+6] << 16) + (buffer[fsc0+7] << 24)
print ("Sample rate: " + str(sample_rate))
stream.mix_rate = sample_rate
var byte_rate = buffer[fsc0+8] + (buffer[fsc0+9] << 8) + (buffer[fsc0+10] << 16) + (buffer[fsc0+11] << 24)
print ("Byte rate: " + str(byte_rate))
var bits_sample_channel = buffer[fsc0+12] + (buffer[fsc0+13] << 8)
print ("BitsPerSample * Channel / 8: " + str(bits_sample_channel))
var bits_per_sample = buffer[fsc0+14] + (buffer[fsc0+15] << 8)
print ("Bits per sample: " + str(bits_per_sample))
if those4bytes == "data":
var audio_data_size = buffer[i+4] + (buffer[i+5] << 8) + (buffer[i+6] << 16) + (buffer[i+7] << 24)
print ("Audio data/stream size is " + str(audio_data_size) + " bytes")
var data_entry_point = (i+8)
print ("Audio data starts at byte " + str(data_entry_point))
stream.data = buffer.subarray(data_entry_point, data_entry_point+audio_data_size-1)
var samplenum = stream.data.size() / 4
for i in 80:
buffer.remove(buffer.size()-1)
buffer.remove(0)
stream.data = buffer
AudioPlayer.stream = stream
AudioPlayer.set_stream_paused(false)
AudioPlayer.play()
elif songplay.ends_with(".mp3"):
stream = AudioStreamMP3.new()
for i in 200:
buffer.remove(buffer.size()-1)
buffer.remove(0)
stream.data = buffer
AudioPlayer.stream = stream
AudioPlayer.set_stream_paused(false)
AudioPlayer.play()
elif songplay.ends_with(".ogg"):
print(songplay)
stream = AudioStreamOGGVorbis.new()
stream.data = buffer
AudioPlayer.stream = stream
AudioPlayer.set_stream_paused(false)
AudioPlayer.play()
print(60 * (stream.get_length()/60 - int(stream.get_length()/60)))
printt("Minutes",round(stream.get_length()/60),"Seconds",round(60 * (stream.get_length()/60 - int(stream.get_length()/60))))
var seconds = round(60 * (stream.get_length()/60 - int(stream.get_length()/60)))
var minutes = round(stream.get_length()/60)
if seconds <= 9:
TrackLength.text = "%s:0%s" %[minutes, seconds]
else:
TrackLength.text = "%s:%s" %[minutes, seconds]
PlayBackSlider.max_value = stream.get_length()
file.close()
currentsong = songplay
AudioPlayer.seek(0)
func PauseMusic():
if AudioPlayer.get_stream_paused() == false:
AudioPlayer.set_stream_paused(true)
elif AudioPlayer.get_stream_paused() == true:
AudioPlayer.set_stream_paused(false)
func _on_VolumeSlider_value_changed(value):
AudioPlayer.volume_db = value
func _on_PlayBackSlider_value_changed(value):
AudioPlayer.seek(value)
func _on_ItemList_item_activated(index):
songplay = songlist[index]
currentsongidx = index
PlaySong()
$PlayButton.pressed = true
func _on_ItemList_item_selected(index):
songplay = songlist[index]
currentsongidx = index
print(songplay)
print(currentsong)
func _on_FileDialog_files_selected(paths):
var temp_list = paths
for path in temp_list:
songlist.append(path)
for song in temp_list:
Playlist.add_item(song.get_file().replace(".%s" %song.get_extension(),""))
print(song.get_file())
if songplay == "":
Playlist.select(0)
songplay = songlist[0]
currentsongidx = 0
func _on_AddSongButton_pressed():
$FileDialog.popup_exclusive = true
print(OS.get_system_dir(OS.SYSTEM_DIR_MUSIC))
$FileDialog.current_dir = OS.get_system_dir(OS.SYSTEM_DIR_MUSIC) + "/Music"
$FileDialog.current_path = OS.get_system_dir(OS.SYSTEM_DIR_MUSIC) + "/Music"
$FileDialog.popup_centered()
func _on_VolumeButton_toggled(button_pressed):
if $Volume.visible == false:
$Volume.visible = true
VolumeSlider.grab_focus()
else:
$Volume.visible = false
func _on_StopButton_pressed():
AudioPlayer.set_stream_paused(true)
yield(get_tree().create_timer(0.3),"timeout")
currentsong = ""
CurrentPlayback.text = "0:00"
print("reset")
PlayBackSlider.value = 0
AudioPlayer.stop()
$PlayButton.pressed = false
func _on_PrevButton_pressed():
if currentsongidx > 0:
currentsongidx -= 1
songplay = songlist[currentsongidx]
PlaySong()
Playlist.select(currentsongidx)
else:
pass
func _on_NextButton_pressed():
if currentsongidx < songlist.size() - 1:
currentsongidx += 1
songplay = songlist[currentsongidx]
PlaySong()
Playlist.select(currentsongidx)
else:
pass
func _on_ColorPicker_color_changed(color):
$Visualizers/Visualizer1.color = color
$Visualizers/Visualizer2.color = color
$Visualizers/Visualizer3.color = color
func _on_LoopButton_pressed():
if loop:
loop = false
print(loop)
else:
loop = true
print(loop)
func _on_AudioStreamPlayer_finished():
if loop == true:
songplay = songlist[currentsongidx]
PlaySong()
elif loop == false:
if currentsongidx < songlist.size() - 1:
yield(get_tree(),"idle_frame")
currentsongidx += 1
yield(get_tree(),"idle_frame")
songplay = songlist[currentsongidx]
yield(get_tree(),"idle_frame")
Playlist.select(currentsongidx)
yield(get_tree(),"idle_frame")
print(songplay)
PlaySong()
elif currentsongidx == songlist.size() - 1:
yield(get_tree(),"idle_frame")
currentsongidx = 0
yield(get_tree(),"idle_frame")
songplay = songlist[currentsongidx]
yield(get_tree(),"idle_frame")
PlaySong()
Playlist.select(currentsongidx)
func add_options():
for i in $Visualizers.get_children():
$OptionButton.add_item(i.name)
$OptionButton.select(1)
func _on_OptionButton_item_selected(index):
var Vis_name = $OptionButton.get_item_text(index)
for i in $Visualizers.get_children():
print(i.name)
get_node("Visualizers/%s" %i.name).visible = false
get_node("Visualizers/%s" %Vis_name).visible = true