[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; |
---|
[547] | 7 | int term = 0; |
---|
| 8 | int streams; |
---|
[9] | 9 | #ifdef MONITOR_LATENCY |
---|
| 10 | struct timeval try, ans; |
---|
| 11 | long int ans_1last = 0, ans_2last = 0, ans_3last = 0; |
---|
[36] | 12 | long int loopc = 0; |
---|
[9] | 13 | |
---|
| 14 | printf("\n\e[s"); |
---|
| 15 | fflush(stdout); |
---|
| 16 | #endif |
---|
[0] | 17 | |
---|
| 18 | ROAR_DBG("main_loop(*) = ?"); |
---|
| 19 | alive = 1; |
---|
[258] | 20 | g_pos = 0; |
---|
[0] | 21 | |
---|
| 22 | while (alive) { |
---|
[9] | 23 | #ifdef MONITOR_LATENCY |
---|
| 24 | gettimeofday(&try, NULL); |
---|
| 25 | #endif |
---|
| 26 | |
---|
[0] | 27 | ROAR_DBG("main_loop(*): looping..."); |
---|
| 28 | |
---|
[68] | 29 | if ( g_listen_socket != -1 ) { |
---|
| 30 | ROAR_DBG("main_loop(*): check for new clients..."); |
---|
| 31 | net_check_listen(); |
---|
| 32 | } |
---|
[0] | 33 | |
---|
| 34 | ROAR_DBG("main_loop(*): check for new data..."); |
---|
[547] | 35 | if ( clients_check_all() == 0 && g_terminate && g_listen_socket == -1 ) { |
---|
| 36 | term = 1; |
---|
| 37 | } |
---|
[0] | 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. |
---|
[547] | 42 | streams = streams_get_mixbuffers(&streams_input, sa, g_pos); |
---|
[0] | 43 | } else { |
---|
[547] | 44 | if ( ( streams = streams_get_mixbuffers(&streams_input, sa, g_pos)) != -1 ) { |
---|
[0] | 45 | mix_clients(g_output_buffer, sa->bits, streams_input, ROAR_OUTPUT_BUFFER_SAMPLES * sa->channels); |
---|
| 46 | } |
---|
| 47 | } |
---|
| 48 | |
---|
[547] | 49 | if ( term && streams < 1 ) |
---|
| 50 | alive = 0; |
---|
| 51 | |
---|
[0] | 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); |
---|
[52] | 59 | printf("usleep(%u) = ?\n", (1000000 * ROAR_OUTPUT_BUFFER_SAMPLES) / sa->rate); |
---|
[0] | 60 | } else { |
---|
[258] | 61 | clients_send_filter(sa, g_pos); |
---|
[0] | 62 | output_buffer_flush(driver_inst, driver); |
---|
[258] | 63 | clients_send_mon(sa, g_pos); |
---|
[0] | 64 | } |
---|
[257] | 65 | |
---|
[258] | 66 | midi_cb_update(); |
---|
[0] | 67 | // output_buffer_reinit(); |
---|
| 68 | |
---|
[258] | 69 | g_pos = ROAR_MATH_OVERFLOW_ADD(g_pos, ROAR_OUTPUT_BUFFER_SAMPLES); |
---|
[259] | 70 | ROAR_DBG("main_loop(*): current pos: %u", g_pos); |
---|
[9] | 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 | |
---|
[36] | 80 | if ( loopc % 128 ) { |
---|
[9] | 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; |
---|
[36] | 89 | loopc++; |
---|
[9] | 90 | #endif |
---|
[0] | 91 | } |
---|
| 92 | |
---|
| 93 | return -1; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | //ll |
---|