-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadManager.odin
More file actions
184 lines (161 loc) · 4.53 KB
/
Copy paththreadManager.odin
File metadata and controls
184 lines (161 loc) · 4.53 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
package main
import "base:runtime"
import "core:fmt"
import "core:math"
import "core:os"
import "core:strings"
import "core:thread"
import "core:time"
import "file_dialog"
import raylib "vendor:raylib/v55"
// Thread User Indices
LOAD_FROM_CONFIG :: 0
LOAD_FROM_DIR :: 1
LOAD_FROM_JSON :: 2
SAVE_TO_JSON :: 3
AUDIO_PROCESS :: 4
// Thread Pool
threads: [dynamic]^thread.Thread
thread_cleaner_thread: ^thread.Thread
thread_cleaner_running: bool
load_from_dir :: proc() {
t := thread.create(
proc(t: ^thread.Thread) {
PlayListLoading = true
playListLoaded = false
// TODO: Add a way to remember the last folder/playlist
folder := file_dialog.open_file_dialog("*.mp3", directory = true)
defer if folder != "" do delete(folder)
assert(os.exists(folder), fmt.tprintf("Folder does not exist: %v", folder))
handle, handleerror := os.open(folder)
assert(handleerror == nil, fmt.tprintf("Error opening directory: %v", handleerror))
defer os.close(handle)
fileinfo, fileinfoerror := os.read_all_directory(handle, context.temp_allocator)
assert(fileinfoerror == nil, fmt.tprintf("Error reading directory: %v", fileinfoerror))
totalProgress: f16 = f16(len(fileinfo) - 1)
progress: f16 = 0
for file in fileinfo {
// Check if file extension is mp3, wav, or flac supported by miniaudio
if strings.ends_with(file.name, ".mp3") ||
strings.ends_with(file.name, ".wav") ||
strings.ends_with(file.name, ".flac") {
progress = progress + 1
Texts["current song"].text = fmt.caprintf(
"Loading: %v%%",
math.round(progress / totalProgress * 100),
)
AddSong(file.fullpath)
}
}
ShufflePlaylist()
time.sleep(1 * time.Millisecond)
playListLoaded = true
media_play_state = .Stopped
UpdatePlaylistList()
},
)
if t == nil {
fmt.eprintln("Error creating task_load_from_config")
}
t.user_index = LOAD_FROM_DIR
append(&threads, t)
thread.start(t)
}
load_from_json :: proc() {
t := thread.create(proc(t: ^thread.Thread) {
PlayListLoading = true
playListLoaded = false
LoadPlaylist(clear = true)
time.sleep(1 * time.Millisecond)
playListLoaded = true
media_play_state = .Stopped
UpdatePlaylistList()
})
if t == nil {
fmt.eprintln("Error creating task_load_from_config")
}
t.user_index = LOAD_FROM_JSON
append(&threads, t)
thread.start(t)
}
save_to_json :: proc() {
t := thread.create(proc(t: ^thread.Thread) {
SavePlaylist()
})
if t == nil {
fmt.eprintln("Error creating task_load_from_config")
}
t.user_index = SAVE_TO_JSON
append(&threads, t)
thread.start(t)
}
@(init)
create_thread_pool :: proc "contextless" () {
// Thanks to VOU-folks for this code: https://github.com/VOU-folks/odin-tcp-server-example/blob/main/main.odin
context = runtime.default_context()
threads = make([dynamic]^thread.Thread, 0)
thread_cleaner_running = true
thread_cleaner()
}
destroy_thread_pool :: proc() {
thread_cleaner_running = false
if thread_cleaner_thread != nil {
for !thread.is_done(thread_cleaner_thread) {
time.sleep(10 * time.Millisecond)
}
thread.destroy(thread_cleaner_thread)
thread_cleaner_thread = nil
}
for i := 0; i < len(threads); {
if worker := threads[i]; thread.is_done(worker) {
thread.destroy(worker)
ordered_remove(&threads, i)
} else {
time.sleep(10 * time.Millisecond)
}
}
delete(threads)
}
thread_cleaner :: proc() {
t := thread.create(proc(t: ^thread.Thread) {
for thread_cleaner_running {
time.sleep(1 * time.Second)
if len(threads) == 0 {
continue
}
for i := 0; i < len(threads); {
if PlayListLoading {
if thread.is_done(t) &&
(threads[i].user_index == LOAD_FROM_CONFIG ||
threads[i].user_index == LOAD_FROM_JSON ||
threads[i].user_index == LOAD_FROM_DIR) {
Texts["current song"].text = fmt.caprintf("Playlist loaded!")
currentSongIndex = 0
currentSongPath = playList[currentSongIndex].path
current_song_tags = playList[currentSongIndex].tags
currentStream = raylib.LoadMusicStream(
strings.clone_to_cstring(
playList[currentSongIndex].path,
context.temp_allocator,
),
)
raylib.AttachAudioStreamProcessor(currentStream, AudioProcessFFT)
PlayListLoading = false
}
}
if threads_cleaner := threads[i]; thread.is_done(threads_cleaner) {
when ODIN_DEBUG {
fmt.printf("Thread %d is done\n", threads_cleaner.user_index)
}
threads_cleaner.data = nil
thread.destroy(threads_cleaner)
ordered_remove(&threads, i)
} else {
i += 1
}
}
}
})
thread_cleaner_thread = t
thread.start(t)
}