-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.C
More file actions
279 lines (225 loc) · 6.15 KB
/
GUI.C
File metadata and controls
279 lines (225 loc) · 6.15 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
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
#include "gui.h"
#include "wav.h"
#include "list.h"
#include "player.h"
#include "utils.h"
#include "natsort.h"
#include <errno.h>
#include <conio.h>
#include <dir.h>
#include <stdio.h>
#define GUI_PLAY_CHAR '>'
#define GUI_PAUSE_CHAR '='
#define GUI_VOLUME_STEP 2
#define GUI_INITIAL_VOLUME 50
struct gui_track_t
{
char name[MEMBER_SIZE(struct ffblk, ff_name)];
uint32_t length;
};
static struct list_t *files;
static struct list_node_t *current;
static uint32_t last_elapsed_time;
static enum player_state_t last_player_state;
static char prev_cwd[128];
static int8_t volume;
static bool gui_compare_ascending(const void *val1, const void *val2)
{
const struct gui_track_t *track1 = val1;
const struct gui_track_t *track2 = val2;
return (strnatcmp(track1->name, track2->name) > 0);
}
static uint32_t gui_get_track_length(const char *path)
{
int err;
FILE *fd;
struct wav_header_t header;
fd = fopen(path, "rb");
if (fd == NULL) {
return 0;
}
/* Get WAV file metadata */
err = wav_parse_header(fd, &header);
fclose(fd);
if (err) {
return 0;
}
return (header.data_size / header.byte_rate);
}
static int gui_create_files_list(void)
{
uint32_t files_count = 0;
int done;
struct ffblk fileinfo;
struct gui_track_t track;
/* Create new files list */
files = list_create();
if (files == NULL) {
return -ENOMEM;
}
/* Append all WAV files to the list and sort it alphabetically */
done = findfirst("*.wav", &fileinfo, 0);
while (!done) {
memcpy(track.name, fileinfo.ff_name, sizeof(track.name));
track.length = gui_get_track_length(track.name);
list_add(files, &track, sizeof(track), LIST_APPEND);
++files_count;
done = findnext(&fileinfo);
}
list_sort(files, gui_compare_ascending);
if (files_count == 0) {
return -ENOENT;
}
return 0;
}
static void gui_show_files_list(void)
{
uint32_t length_total = 0;
uint32_t track_num = 1;
struct list_node_t *file = files->head;
struct gui_track_t *track;
printf("----------------------------\tControls:\n");
printf("---------- Tracks ----------\t[ESC] - leave to DOS\t[I] - previous track\n");
printf("----------------------------\t[Y] - volume down\t[O] - next track\n");
printf("| Track | Time |\t[U] - volume up\t\t[P] - pause/resume\n");
printf("|-----------------|--------|\n");
do {
track = file->data;
printf("| %02lu. %-12s| %02lu:%02lu |\n", track_num, track->name, track->length / 60, track->length % 60);
length_total += track->length;
++track_num;
file = file->next;
} while (file != NULL);
printf("----------------------------\n");
printf("| Total | %02lu:%02lu |\n\n", length_total / 60, length_total % 60);
}
int gui_init(const char *path)
{
int err;
struct gui_track_t *track;
/* Preserve current working directory and change it to the one
* provided by the user. If no directory has been provided, use
* the current one. */
if (path != NULL) {
getcwd(prev_cwd, sizeof(prev_cwd));
err = chdir(path);
if (err) {
gui_deinit();
return -ENOENT;
}
}
/* Create files list */
err = gui_create_files_list();
if (err) {
gui_deinit();
return err;
}
/* Show files list */
gui_show_files_list();
/* Set volume */
volume = GUI_INITIAL_VOLUME;
player_set_volume(volume);
/* Start playback of the first song */
current = files->head;
track = current->data;
err = player_start(track->name);
if (err) {
return err;
}
return 0;
}
void gui_deinit(void)
{
/* Restore previous working directory */
chdir(prev_cwd);
/* Cleanup */
list_destroy(files);
}
int gui_task(void)
{
char key;
char track_state;
struct gui_track_t *track;
uint32_t elapsed_time;
enum player_state_t player_state;
/* Update track info. This should refresh also if the volume
* has been changed, but on Xi8088 this causes audio buffer
* underruns if the volume is changed quickly. On faster
* machines this shouldn't be an issue. */
elapsed_time = player_get_seconds_played();
player_state = player_get_state();
if ((last_elapsed_time != elapsed_time) ||
(last_player_state != player_state)) {
track = current->data;
track_state = (player_state == PLAYER_PLAYING) ? GUI_PLAY_CHAR : GUI_PAUSE_CHAR;
printf("Now playing: [%c] %s (%02lu:%02lu/%02lu:%02lu) | Volume: %d%% \r", track_state, track->name, elapsed_time / 60, elapsed_time % 60, track->length / 60, track->length % 60, (int)volume);
last_elapsed_time = elapsed_time;
last_player_state = player_state;
}
/* Play next song if previous has ended */
if (player_get_state() == PLAYER_STOPPED) {
if (current->next == NULL) {
printf("\nDone!\n");
return GUI_KEY_EXIT; // We've played all the files, exit
}
current = current->next;
track = current->data;
player_start(track->name);
}
/* Handle user input */
if (!kbhit()) {
return 0;
}
key = toupper(getch());
switch (key) {
case GUI_KEY_NEXT:
/* Get next song if exists */
if (current->next == NULL) {
break;
}
current = current->next;
/* Start playback */
track = current->data;
player_start(track->name);
/* Force track info refresh */
last_player_state = PLAYER_STOPPED;
break;
case GUI_KEY_PREV:
/* Get previous song if exists */
if (current->prev == NULL) {
break;
}
current = current->prev;
/* Start playback */
track = current->data;
player_start(track->name);
/* Force track info refresh */
last_player_state = PLAYER_STOPPED;
break;
case GUI_KEY_PAUSE:
switch (player_get_state()) {
case PLAYER_PLAYING:
player_pause();
break;
case PLAYER_PAUSED:
player_resume();
break;
default:
break;
}
break;
case GUI_KEY_VOLUME_UP:
volume += GUI_VOLUME_STEP;
volume = CLAMP(volume, PERCENT_MIN, PERCENT_MAX);
player_set_volume(volume);
break;
case GUI_KEY_VOLUME_DOWN:
volume -= GUI_VOLUME_STEP;
volume = CLAMP(volume, PERCENT_MIN, PERCENT_MAX);
player_set_volume(volume);
break;
default:
break;
}
return key;
}