-
Notifications
You must be signed in to change notification settings - Fork 6
/
ui.h
150 lines (131 loc) · 5.27 KB
/
ui.h
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
//brstm_rt user interface
//Copyright (C) 2022 I.C.
//UI state
struct uoutput_state_t {
//Quiet output (no player UI)
bool quietOutput = 0;
//Classic player UI
bool classic_noflush = 0;
//UI refresh timer
uint16_t ui_counter = 0;
uint16_t ui_counter_l = 0;
//Second counters and strings
unsigned long playback_seconds = 0;
char playback_seconds_string[10];
unsigned long total_seconds = 0;
char total_seconds_string[10];
//Pointer to playback state
player_state_t* playback_state;
};
//Data for input thread
struct uinput_state_t {
bool exit = 0;
//Pointer to playback state
player_state_t* playback_state;
//Pointer to output UI state
uoutput_state_t* output_state;
};
//User input thread function
void* uinput_thread(void* arg) {
uinput_state_t* state = (uinput_state_t*)arg;
char input;
player_state_t* pstate = state->playback_state;
while(state->exit == 0) {
input = getch();
//Unlock player state before changing it's data
while(pstate->lock) usleep(100);
pstate->lock = 1;
if(input == '\033') {
getch();
input=getch();
switch(input) {
case 'A': /*UP - Ffwd. ten times*/ {
pstate->playback_current_sample += pstate->brstm->sample_rate * 10;
if(pstate->playback_current_sample > pstate->brstm->total_samples) pstate->playback_current_sample = pstate->brstm->total_samples - 1;
break;
}
case 'B': /*DOWN - Rwd. ten times*/ {
if(pstate->playback_current_sample >= pstate->brstm->sample_rate * 10) pstate->playback_current_sample -= pstate->brstm->sample_rate * 10;
else pstate->playback_current_sample = 0;
break;
}
case 'C': /*RIGHT - Fast Forward*/ {
pstate->playback_current_sample += pstate->brstm->sample_rate;
if(pstate->playback_current_sample > pstate->brstm->total_samples) pstate->playback_current_sample = pstate->brstm->total_samples - 1;
break;
}
case 'D': /*LEFT - Rewind*/ {
if(pstate->playback_current_sample >= pstate->brstm->sample_rate) pstate->playback_current_sample -= pstate->brstm->sample_rate;
else pstate->playback_current_sample = 0;
break;
}
}
} else switch(input) {
//reserved for more features in the future?
/*case 'w': case 'W': break;
case 's': case 'S': break;
case 'a': case 'A': break;
case 'd': case 'D': break;*/
//Pause
case ' ': pstate->paused = !pstate->paused; break;
//Quit program
case 'q': case 'Q': {
pstate->stop_playing = 1;
state->exit = 1;
break;
}
default: {
//Track toggles
if(input >= '0' && input <= '9' && pstate->track_mixing_enabled) {
uint8_t input_num = input - '0';
if(input_num > 0 && input_num <= pstate->brstm->num_tracks) {
pstate->tracks_enabled[input_num-1] = !pstate->tracks_enabled[input_num-1];
}
}
break;
}
}
//Force display refresh to be more responsive to user input.
state->output_state->ui_counter = -1;
pstate->lock = 0;
}
return 0;
}
//Player UI
void drawPlayerUI(uoutput_state_t* state) {
player_state_t* pstate = state->playback_state;
state->playback_seconds = pstate->playback_current_sample / pstate->brstm->sample_rate;
if(!state->quietOutput) {
if(state->classic_noflush || (state->ui_counter_l <= state->ui_counter) ) {
state->ui_counter = 0;
std::cout << '\r'
// Pause
<< (pstate->paused ? "Paused " : "")
// Time
<< "(" << secondsToMString(state->playback_seconds_string, 10, state->playback_seconds) << " / " << state->total_seconds_string;
// Tracks
if(!pstate->track_mixing_enabled) {
// Removed - track mixing always enabled
//std::cout << " Track: " << pstate->current_track+1;
} else {
std::cout << " | Tracks:";
for(unsigned int t=0; t<pstate->brstm->num_tracks; t++) {
std::cout << ' ' << t+1 << '[' << (pstate->tracks_enabled[t] ? 'X' : ' ') << ']';
}
}
// Controls guide
std::cout << ") (< >:Seek ";
if(pstate->track_mixing_enabled) {
std::cout << "1-" << pstate->brstm->num_tracks << ":Toggle tracks";
} else {
std::cout << "/\\ \\/:Seek x10";
}
std::cout << ")\033[0m "
// End
<< (!pstate->paused ? " " : "") << "\r";
//Flush
if(!state->classic_noflush) fflush(stdout);
}
state->ui_counter++;
}
}