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