1 | //loop.c: |
---|
2 | |
---|
3 | #include "roard.h" |
---|
4 | |
---|
5 | int main_loop (int driver, DRIVER_USERDATA_T driver_inst, struct roar_audio_info * sa) { |
---|
6 | void ** streams_input = NULL; |
---|
7 | int term = 0; |
---|
8 | int streams; |
---|
9 | #ifdef MONITOR_LATENCY |
---|
10 | struct timeval try, ans; |
---|
11 | long int ans_1last = 0, ans_2last = 0, ans_3last = 0; |
---|
12 | long int loopc = 0; |
---|
13 | |
---|
14 | printf("\n\e[s"); |
---|
15 | fflush(stdout); |
---|
16 | #endif |
---|
17 | |
---|
18 | ROAR_DBG("main_loop(*) = ?"); |
---|
19 | alive = 1; |
---|
20 | g_pos = 0; |
---|
21 | |
---|
22 | while (alive) { |
---|
23 | #ifdef MONITOR_LATENCY |
---|
24 | gettimeofday(&try, NULL); |
---|
25 | #endif |
---|
26 | |
---|
27 | ROAR_DBG("main_loop(*): looping..."); |
---|
28 | |
---|
29 | if ( g_listen_socket != -1 ) { |
---|
30 | ROAR_DBG("main_loop(*): check for new clients..."); |
---|
31 | net_check_listen(); |
---|
32 | } |
---|
33 | |
---|
34 | ROAR_DBG("main_loop(*): check for new data..."); |
---|
35 | if ( clients_check_all() == 0 && g_terminate && g_listen_socket == -1 ) { |
---|
36 | term = 1; |
---|
37 | } |
---|
38 | |
---|
39 | ROAR_DBG("main_loop(*): mixing clients..."); |
---|
40 | if ( g_standby ) { |
---|
41 | // while in standby we still neet to get the buffers to free input buffer space. |
---|
42 | streams = streams_get_mixbuffers(&streams_input, sa, g_pos); |
---|
43 | } else { |
---|
44 | if ( ( streams = streams_get_mixbuffers(&streams_input, sa, g_pos)) != -1 ) { |
---|
45 | mix_clients(g_output_buffer, sa->bits, streams_input, ROAR_OUTPUT_BUFFER_SAMPLES * sa->channels); |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | if ( term && streams < 1 ) |
---|
50 | alive = 0; |
---|
51 | |
---|
52 | /* |
---|
53 | // while in standby we still need to write out our buffer to not run in an endless loop without |
---|
54 | // a break |
---|
55 | */ |
---|
56 | |
---|
57 | if ( g_standby ) { |
---|
58 | usleep((1000000 * ROAR_OUTPUT_BUFFER_SAMPLES) / sa->rate); |
---|
59 | printf("usleep(%u) = ?\n", (1000000 * ROAR_OUTPUT_BUFFER_SAMPLES) / sa->rate); |
---|
60 | } else { |
---|
61 | clients_send_filter(sa, g_pos); |
---|
62 | output_buffer_flush(driver_inst, driver); |
---|
63 | clients_send_mon(sa, g_pos); |
---|
64 | } |
---|
65 | |
---|
66 | midi_cb_update(); |
---|
67 | // output_buffer_reinit(); |
---|
68 | |
---|
69 | g_pos = ROAR_MATH_OVERFLOW_ADD(g_pos, ROAR_OUTPUT_BUFFER_SAMPLES); |
---|
70 | ROAR_DBG("main_loop(*): current pos: %u", g_pos); |
---|
71 | #ifdef MONITOR_LATENCY |
---|
72 | gettimeofday(&ans, NULL); |
---|
73 | |
---|
74 | while (ans.tv_sec > try.tv_sec) { |
---|
75 | ans.tv_sec--; |
---|
76 | ans.tv_usec += 1000000; |
---|
77 | } |
---|
78 | ans.tv_usec -= try.tv_usec; |
---|
79 | |
---|
80 | if ( loopc % 128 ) { |
---|
81 | printf("\e[ucurrent latency: %.3fms average: %.3fms ", ans.tv_usec / (double)1000, |
---|
82 | (ans.tv_usec+ans_3last+ans_2last+ans_1last)/ (double)4000); |
---|
83 | fflush(stdout); |
---|
84 | } |
---|
85 | |
---|
86 | ans_3last = ans_2last; |
---|
87 | ans_2last = ans_1last; |
---|
88 | ans_1last = ans.tv_usec; |
---|
89 | loopc++; |
---|
90 | #endif |
---|
91 | } |
---|
92 | |
---|
93 | return -1; |
---|
94 | } |
---|
95 | |
---|
96 | //ll |
---|