-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
342 lines (296 loc) · 8.7 KB
/
main.c
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
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <ncurses.h>
#include <menu.h>
#include <linux/limits.h>
#include <dirent.h>
#define MINIAUDIO_IMPLEMENTATION
#include "./miniaudio.h"
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4
#define MAX_DECODERS 100
#define MAX_AUDIO_FILES 100
#define SUCCESS 0
#define FAILURE 1
typedef struct {
char name[PATH_MAX + 1];
char path[PATH_MAX + 1];
} AudioFile;
// global variables
static int current_song_index = -1;
static ma_uint64 current_song_frame_index = 0;
static ma_uint64 frames_to_skip = 5 * 48000;
static bool is_audio_pause = false;
AudioFile* files;
ma_device device;
ma_decoder* decoders;
// module functions
bool is_mp3_file(char* filename);
int fetch_audio_files(char* path);
int init_device_and_decoder(int total_files);
void data_callback(
ma_device* pDevice,
void* pOutput,
const void* pInput,
ma_uint32 frameCount
);
void print_current_audio(int starty, int startx, char* audio_name);
void print_in_middle(
WINDOW* win,
int starty,
int startx,
int width,
char* str,
chtype color
);
int main(int argc, char* argv[]) {
// ncurses vars
ITEM** my_items;
MENU* my_menu;
WINDOW* my_menu_win;
int i, c;
int width, height, starty, startx;
decoders = (ma_decoder*) malloc(sizeof(ma_decoder) * MAX_DECODERS);
files = (AudioFile*) malloc(sizeof(AudioFile) * MAX_AUDIO_FILES);
if(argc < 2) {
fprintf(stderr, "directory argument missing\n");
exit(1);
}
// fetch audio files
int total_audio_files = fetch_audio_files(argv[1]);
if(total_audio_files < 1) {
fprintf(stderr, "Not able to fetch audio files from %s directory\n", argv[1]);
exit(1);
}
// initialize miniaudio decoder and device array
if(init_device_and_decoder(total_audio_files) != SUCCESS) {
fprintf(stderr, "Not able to initialize device & decoder\n");
exit(1);
}
// initialize curses
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_CYAN, COLOR_BLACK);
curs_set(0);
width = COLS - 5;
height = LINES / 2;
startx = (COLS - width) / 2;
starty = 1;
// create items
my_items = (ITEM**) calloc(total_audio_files + 1, sizeof(ITEM*));
for(i = 0; i < total_audio_files; i++) {
my_items[i] = new_item(" ", files[i].name);
}
// terminate my_items array with null
my_items[i] = (ITEM*) NULL;
// create menu
my_menu = new_menu((ITEM**) my_items);
// create the window to be associated with the menu
my_menu_win = newwin(height, width, starty, startx);
nodelay(my_menu_win, TRUE);
keypad(my_menu_win, TRUE);
// set main window and sub window
set_menu_win(my_menu, my_menu_win);
set_menu_sub(my_menu, derwin(my_menu_win, height - 4, width - 2, 3, 1));
set_menu_format(my_menu, height - 4, 1);
// set menu mark to the string " *"
set_menu_mark(my_menu, " *");
// print a border around the main window and print a title
box(my_menu_win, 0, 0);
print_in_middle(my_menu_win, 1, 0, COLS - 5, "Termzic", COLOR_PAIR(1));
mvwaddch(my_menu_win, 2, 0, ACS_LTEE);
mvwhline(my_menu_win, 2, 1, ACS_HLINE, width - 2);
mvwaddch(my_menu_win, 2, width - 1, ACS_RTEE);
// post the menu
post_menu(my_menu);
wrefresh(my_menu_win);
refresh();
while((c = wgetch(my_menu_win)) != KEY_F(1)) {
if(current_song_index > -1) {
print_current_audio(
LINES - 2,
startx,
files[current_song_index].name
);
}
switch(c) {
case KEY_DOWN:
case 'j':
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
case 'k':
menu_driver(my_menu, REQ_UP_ITEM);
break;
case KEY_NPAGE:
menu_driver(my_menu, REQ_SCR_DPAGE);
break;
case KEY_PPAGE:
menu_driver(my_menu, REQ_SCR_UPAGE);
break;
case KEY_RIGHT: // skip 5 seconds
current_song_frame_index += frames_to_skip;
ma_decoder_seek_to_pcm_frame(
&decoders[current_song_index],
current_song_frame_index
);
break;
case KEY_LEFT: // revert 5 seconds
current_song_frame_index -= frames_to_skip;
ma_decoder_seek_to_pcm_frame(
&decoders[current_song_index],
current_song_frame_index
);
break;
case 'r': // resume song
is_audio_pause = false;
break;
case 'p': // pause song
is_audio_pause = true;
break;
case 10: // enter
ITEM* curr_item = current_item(my_menu);
pos_menu_cursor(my_menu);
// change song
current_song_index = item_index(curr_item);
current_song_frame_index = 0;
ma_decoder_seek_to_pcm_frame(&decoders[current_song_index], 0);
break;
}
wrefresh(my_menu_win);
}
// unpost and free all the memory taken up
unpost_menu(my_menu);
free_menu(my_menu);
ma_device_stop(&device);
ma_device_uninit(&device);
for(i = 0; i < total_audio_files; ++i) {
free_item(my_items[i]);
ma_decoder_uninit(&decoders[i]);
}
free(my_items);
free(files);
endwin();
}
bool is_mp3_file(char* filename) {
char* dot = strrchr(filename, '.');
return dot && strcmp(dot, ".mp3") == 0;
}
// fetch_audio_files: fetch all files from dir and put in files array
int fetch_audio_files(char* path) {
DIR *d;
struct dirent *dir;
int i = 0;
char buf[PATH_MAX + 1];
d = opendir(path);
if(d) {
while((dir = readdir(d)) != NULL) {
if(i >= MAX_AUDIO_FILES) return i;
if (
strcmp(dir->d_name, ".") != 0 &&
strcmp(dir->d_name, "..") != 0 &&
is_mp3_file(dir->d_name)
) {
// create full path of a audio file
snprintf(buf, sizeof(buf), "%s%s", path, dir->d_name);
// add in array
strcpy(files[i].name, dir->d_name);
strcpy(files[i].path, buf);
i++;
}
}
closedir(d);
return i;
}
return -1;
}
// init miniaudio device & decoder array from files
int init_device_and_decoder(int total_audio_files) {
ma_result result;
ma_device_config device_config;
ma_decoder_config decoder_config = ma_decoder_config_init(ma_format_f32, 2, 48000);
for(int i = 0; i < total_audio_files; i++) {
result = ma_decoder_init_file(files[i].path, &decoder_config, &decoders[i]);
if (result != MA_SUCCESS) {
fprintf(stderr, "Could not load file: %s\n", files[i].path);
return -2;
}
}
device_config = ma_device_config_init(ma_device_type_playback);
device_config.playback.format = ma_format_f32;
device_config.playback.channels = 2;
device_config.sampleRate = 48000;
device_config.dataCallback = data_callback;
device_config.pUserData = NULL;
if (ma_device_init(NULL, &device_config, &device) != MA_SUCCESS) {
fprintf(stderr, "Failed to open playback device.\n");
ma_device_uninit(&device);
return -3;
}
if (ma_device_start(&device) != MA_SUCCESS) {
fprintf(stderr, "Failed to play device\n");
ma_device_uninit(&device);
return -3;
}
return SUCCESS;
}
void data_callback(
ma_device* pDevice,
void* pOutput,
const void* pInput,
ma_uint32 frameCount
) {
(void) pDevice;
if(current_song_index > -1 && !is_audio_pause) {
ma_uint64 framesRead = ma_decoder_read_pcm_frames(
&decoders[current_song_index],
pOutput,
frameCount,
NULL
);
current_song_frame_index += framesRead;
if(framesRead > frameCount) {
current_song_index++;
ma_decoder_seek_to_pcm_frame(&decoders[current_song_index], 0);
}
}
(void)pInput;
}
void print_current_audio(int starty, int startx, char* audio_name) {
move(starty, startx);
clrtoeol();
mvprintw(starty, startx, "Playing: %s", audio_name);
refresh();
}
void print_in_middle(
WINDOW* win,
int starty,
int startx,
int width,
char* str,
chtype color
) {
int length, x, y;
float temp;
if(win == NULL)
win = stdscr;
getyx(win, y, x);
if(startx != 0)
x = startx;
if(starty != 0)
y = starty;
if(width == 0)
width = 80;
length = strlen(str);
temp = (width - length)/ 2;
x = startx + (int) temp;
wattron(win, color);
mvwprintw(win, y, x, "%s", str);
wattroff(win, color);
refresh();
}