-
Notifications
You must be signed in to change notification settings - Fork 0
/
uniform.c
262 lines (220 loc) · 5.61 KB
/
uniform.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
/*
* uniform.c
*
* A truly stupid way to generate and pipe values
* into glslViewer.
*
* Current features:
*
* - u_beat from tapping inputs
* - ENTER 4 or more times to set tempo
* - [ or h to halve
* - ] or l to double
* - = or k to increase
* - - or j to decrease
* - p to saw down (classic!)
* - o to saw up
* - i to sine
* - u to triangle
*
* - u_amp from audio amplitude (crappy but working)
*
* - optional u_midi from midi driver
*
*/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stddef.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
#include <sys/time.h>
#include <pthread.h>
#include <termios.h>
#include <assert.h>
#include <stdbool.h>
#include <argp.h>
#include "arguments.h"
#include "audio.h"
#include "beat.h"
#include "midi.h"
#include "dir_rotator.h"
#define MAIN_DELAY 100000
#define DEBUG true
#define ENABLE_AUDIO true
#define ENABLE_BEAT true
//#define ENABLE_MIDI
const char *argp_program_version = "uniform.c 0.1";
static char doc[] = "Uniform.c - a program to wrap glslViewer with various weird features";
static char args_doc[] = "";
static struct argp_option options[] = {
{"verbose", 'v', 0, 0, "Produce verbose output" },
{"quiet", 'q', 0, 0, "Don't produce any output" },
{"silent", 's', 0, OPTION_ALIAS },
{"dir", 'd', "DIR", 0, "Rotate through a directory" },
{"rotation-speed", 'r', "S", 0, "Rotation speed in seconds" },
{"bpm", 'b', "BPM", 0, "Set a starting bpm value" },
{ 0 }
};
static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
/* Get the input argument from argp_parse, which we
know is a pointer to our arguments structure. */
struct arguments *arguments = state->input;
switch (key)
{
case 'q': case 's':
arguments->silent = 1;
break;
case 'v':
arguments->verbose = 1;
break;
case 'd':
arguments->dir = arg;
break;
case 'r':
arguments->rotation_speed = strtol(arg, NULL, 10);
break;
case 'b':
arguments->bpm = strtof(arg, NULL);
break;
case ARGP_KEY_ARG:
if (state->arg_num >= 1)
/* Too many arguments. */
argp_usage (state);
//arguments->args[state->arg_num] = arg;
break;
case ARGP_KEY_END:
if (state->arg_num < 0)
/* Not enough arguments. */
argp_usage (state);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = { options, parse_opt, args_doc, doc };
// Stupid terminal hacking to be able to get raw input
struct termios org_opts, new_opts;
void terminal_init(void) {
int res=0;
//----- store old settings -----------
res=tcgetattr(STDIN_FILENO, &org_opts);
assert(res==0);
//---- set new terminal parms --------
memcpy(&new_opts, &org_opts, sizeof(new_opts));
new_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL);
tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
}
void terminal_shutdown(void) {
int res=0;
res=tcsetattr(STDIN_FILENO, TCSANOW, &org_opts);
assert(res==0);
}
int main(int argc, char **argv) {
setbuf(stdout, NULL);
terminal_init();
pulseaudio_begin("nothing");
struct arguments arguments;
/* Default values. */
arguments.silent = 0;
arguments.verbose = 0;
arguments.bpm = 120.0;
arguments.dir = "";
arguments.rotation_speed = 30;
argp_parse(&argp, argc, argv, 0, 0, &arguments);
set_bpm(arguments.bpm);
struct timeval t1, t2, w1, w2, dt;
int64_t microseconds;
char input;
// TODO: Allow defining multiple LFO threads from args?
#ifdef ENABLE_BEAT
pthread_t beat_thread;
pthread_create(&beat_thread, NULL, beat, NULL);
#endif
#ifdef ENABLE_AUDIO
pthread_t audio_thread;
pthread_create(&audio_thread, NULL, amplitude, NULL);
#endif
#ifdef ENABLE_MIDI
pthread_t midi_thread;
pthread_create(&midi_thread, NULL, midi, NULL);
#endif
if (strlen(arguments.dir) > 0) {
pthread_t dir_thread;
pthread_create(&dir_thread, NULL, dir_rotator, (void*) &arguments);
}
while(1) {
input = getchar();
switch (input) {
// p sets sawtooth down
case 112:
set_beat_type(1);
break;
// o sets sawtooth up
case 111:
set_beat_type(2);
break;
// i sets sine wave
case 105:
set_beat_type(3);
break;
// u sets triangle
case 117:
set_beat_type(4);
break;
// Left bracket or h halves tempo
case 91:
case 104:
bpm_double();
break;
// Right bracket or l doubles tempo
case 93:
case 108:
bpm_halve();
break;
// Minus or j decreases tempo
case 61:
case 106:
bpm_decrease();
break;
// Equals or k increases tempo
case 45:
case 107:
bpm_increase();
break;
// Backslash resets tempo to default
case 92:
bpm_default();
break;
// Tap tempo with enter
case 10:
gettimeofday(&t1, NULL);
bool too_slow = 0;
for(int i=0; i<3; i++) {
gettimeofday(&w1, NULL);
getchar();
gettimeofday(&w2, NULL);
timersub(&w2, &w1, &dt);
if (dt.tv_sec > 2.0) {
too_slow = true;
break;
}
}
if (!too_slow) {
gettimeofday(&t2, NULL);
timersub(&t2, &t1, &dt);
microseconds = dt.tv_sec * 1000000 + dt.tv_usec;
// we take 4 taps
set_uspb(microseconds / 3.0);
}
}
usleep(MAIN_DELAY);
}
terminal_shutdown();
return 0;
}