-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathbasic_example.c
More file actions
153 lines (112 loc) · 4.09 KB
/
basic_example.c
File metadata and controls
153 lines (112 loc) · 4.09 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
#include <math.h>
#include <stdio.h>
#if defined(WIN32)
#include <windows.h>
#else
#include <unistd.h>
#endif
#include "../external/rocket/lib/sync.h"
static struct sync_device* device;
#if !defined(SYNC_PLAYER)
static struct sync_cb cb;
#endif
#define sizeof_array(array) (int)(sizeof(array) / sizeof(array[0]))
const float bpm = 180.0f;
const float rpb = 8.0f;
float rps = 24.0f; // bpm / 60.0f * rpb; <- msvc cant compute this compile time... sigh
int audio_is_playing = 1;
int curtime_ms = 0;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static int row_to_ms_round(int row, float rps) {
const float newtime = ((float)(row)) / rps;
return (int)(floor(newtime * 1000.0f + 0.5f));
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static float ms_to_row_f(int time_ms, float rps) {
const float row = rps * ((float)time_ms) * 1.0f / 1000.0f;
return row;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static int ms_to_row_round(int time_ms, float rps) {
const float r = ms_to_row_f(time_ms, rps);
return (int)(floor(r + 0.5f));
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if !defined(SYNC_PLAYER)
static void xpause(void* data, int flag) {
(void)data;
if (flag)
audio_is_playing = 0;
else
audio_is_playing = 1;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void xset_row(void* data, int row) {
int newtime_ms = row_to_ms_round(row, rps);
curtime_ms = newtime_ms;
(void)data;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static int xis_playing(void* data) {
(void)data;
return audio_is_playing;
}
#endif //! SYNC_PLAYER
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int rocket_init(const char* prefix) {
device = sync_create_device(prefix);
if (!device) {
printf("Unable to create rocketDevice\n");
return 0;
}
#if !defined(SYNC_PLAYER)
cb.is_playing = xis_playing;
cb.pause = xpause;
cb.set_row = xset_row;
if (sync_tcp_connect(device, "localhost", SYNC_DEFAULT_PORT)) {
printf("Rocket failed to connect\n");
return 0;
}
#endif
printf("Rocket connected.\n");
return 1;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static int rocket_update(void) {
int row = 0;
if (audio_is_playing)
curtime_ms += 16; //...60hz or gtfo
#if !defined(SYNC_PLAYER)
row = ms_to_row_round(curtime_ms, rps);
if (sync_update(device, row, &cb, 0))
sync_tcp_connect(device, "localhost", SYNC_DEFAULT_PORT);
#endif
return 1;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static const char* s_trackNames[] = {
"group0#track0", "group0#track1", "group0#track2", "group0#track3",
"group1#track0", "group1#track1", "group1#track2",
};
static const struct sync_track* s_tracks[sizeof_array(s_trackNames)];
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main(void) {
int i;
if (!rocket_init("data/sync"))
return -1;
for (i = 0; i < sizeof_array(s_trackNames); ++i)
s_tracks[i] = sync_get_track(device, s_trackNames[i]);
for (;;) {
float row_f;
rocket_update();
row_f = ms_to_row_f(curtime_ms, rps);
printf("current time %d\n", curtime_ms);
for (i = 0; i < sizeof_array(s_trackNames); ++i)
printf("%s %f\n", s_trackNames[i], sync_get_val(s_tracks[i], row_f));
#if defined(WIN32)
Sleep(16);
#else
usleep(16000);
#endif
}
}