[0] | 1 | //loop.c: |
---|
| 2 | |
---|
[668] | 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
| 5 | * |
---|
| 6 | * This file is part of roard a part of RoarAudio, |
---|
| 7 | * a cross-platform sound system for both, home and professional use. |
---|
| 8 | * See README for details. |
---|
| 9 | * |
---|
| 10 | * This file is free software; you can redistribute it and/or modify |
---|
| 11 | * it under the terms of the GNU General Public License version 3 |
---|
| 12 | * as published by the Free Software Foundation. |
---|
| 13 | * |
---|
| 14 | * RoarAudio is distributed in the hope that it will be useful, |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | * GNU General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with this software; see the file COPYING. If not, write to |
---|
| 21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 22 | * |
---|
| 23 | */ |
---|
| 24 | |
---|
[0] | 25 | #include "roard.h" |
---|
| 26 | |
---|
[905] | 27 | int main_loop (int driver, DRIVER_USERDATA_T driver_inst, struct roar_audio_info * sa, int sysclocksync) { |
---|
[0] | 28 | void ** streams_input = NULL; |
---|
[547] | 29 | int term = 0; |
---|
| 30 | int streams; |
---|
[905] | 31 | long int loopc = 0; |
---|
[9] | 32 | struct timeval try, ans; |
---|
[905] | 33 | float freq; |
---|
| 34 | #ifdef MONITOR_LATENCY |
---|
[9] | 35 | long int ans_1last = 0, ans_2last = 0, ans_3last = 0; |
---|
| 36 | |
---|
| 37 | printf("\n\e[s"); |
---|
| 38 | fflush(stdout); |
---|
| 39 | #endif |
---|
[0] | 40 | |
---|
| 41 | ROAR_DBG("main_loop(*) = ?"); |
---|
| 42 | alive = 1; |
---|
[258] | 43 | g_pos = 0; |
---|
[0] | 44 | |
---|
[905] | 45 | if ( sysclocksync ) { |
---|
| 46 | gettimeofday(&try, NULL); |
---|
| 47 | } |
---|
| 48 | |
---|
[0] | 49 | while (alive) { |
---|
[9] | 50 | #ifdef MONITOR_LATENCY |
---|
| 51 | gettimeofday(&try, NULL); |
---|
| 52 | #endif |
---|
| 53 | |
---|
[0] | 54 | ROAR_DBG("main_loop(*): looping..."); |
---|
| 55 | |
---|
[68] | 56 | if ( g_listen_socket != -1 ) { |
---|
| 57 | ROAR_DBG("main_loop(*): check for new clients..."); |
---|
| 58 | net_check_listen(); |
---|
| 59 | } |
---|
[0] | 60 | |
---|
| 61 | ROAR_DBG("main_loop(*): check for new data..."); |
---|
[547] | 62 | if ( clients_check_all() == 0 && g_terminate && g_listen_socket == -1 ) { |
---|
| 63 | term = 1; |
---|
| 64 | } |
---|
[0] | 65 | |
---|
| 66 | ROAR_DBG("main_loop(*): mixing clients..."); |
---|
| 67 | if ( g_standby ) { |
---|
| 68 | // while in standby we still neet to get the buffers to free input buffer space. |
---|
[547] | 69 | streams = streams_get_mixbuffers(&streams_input, sa, g_pos); |
---|
[0] | 70 | } else { |
---|
[547] | 71 | if ( ( streams = streams_get_mixbuffers(&streams_input, sa, g_pos)) != -1 ) { |
---|
[0] | 72 | mix_clients(g_output_buffer, sa->bits, streams_input, ROAR_OUTPUT_BUFFER_SAMPLES * sa->channels); |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | |
---|
[547] | 76 | if ( term && streams < 1 ) |
---|
| 77 | alive = 0; |
---|
| 78 | |
---|
[0] | 79 | /* |
---|
| 80 | // while in standby we still need to write out our buffer to not run in an endless loop without |
---|
| 81 | // a break |
---|
| 82 | */ |
---|
| 83 | |
---|
| 84 | if ( g_standby ) { |
---|
| 85 | usleep((1000000 * ROAR_OUTPUT_BUFFER_SAMPLES) / sa->rate); |
---|
[729] | 86 | ROAR_DBG("usleep(%u) = ?\n", (1000000 * ROAR_OUTPUT_BUFFER_SAMPLES) / sa->rate); |
---|
[0] | 87 | } else { |
---|
[258] | 88 | clients_send_filter(sa, g_pos); |
---|
[0] | 89 | output_buffer_flush(driver_inst, driver); |
---|
[258] | 90 | clients_send_mon(sa, g_pos); |
---|
[0] | 91 | } |
---|
[257] | 92 | |
---|
[258] | 93 | midi_cb_update(); |
---|
[0] | 94 | // output_buffer_reinit(); |
---|
| 95 | |
---|
[258] | 96 | g_pos = ROAR_MATH_OVERFLOW_ADD(g_pos, ROAR_OUTPUT_BUFFER_SAMPLES); |
---|
[259] | 97 | ROAR_DBG("main_loop(*): current pos: %u", g_pos); |
---|
[9] | 98 | #ifdef MONITOR_LATENCY |
---|
| 99 | gettimeofday(&ans, NULL); |
---|
| 100 | |
---|
| 101 | while (ans.tv_sec > try.tv_sec) { |
---|
| 102 | ans.tv_sec--; |
---|
| 103 | ans.tv_usec += 1000000; |
---|
| 104 | } |
---|
| 105 | ans.tv_usec -= try.tv_usec; |
---|
| 106 | |
---|
[36] | 107 | if ( loopc % 128 ) { |
---|
[9] | 108 | printf("\e[ucurrent latency: %.3fms average: %.3fms ", ans.tv_usec / (double)1000, |
---|
| 109 | (ans.tv_usec+ans_3last+ans_2last+ans_1last)/ (double)4000); |
---|
| 110 | fflush(stdout); |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | ans_3last = ans_2last; |
---|
| 114 | ans_2last = ans_1last; |
---|
| 115 | ans_1last = ans.tv_usec; |
---|
| 116 | #endif |
---|
[905] | 117 | |
---|
[906] | 118 | if ( sysclocksync && !(loopc % sysclocksync) ) { |
---|
[905] | 119 | gettimeofday(&ans, NULL); |
---|
| 120 | |
---|
| 121 | while (ans.tv_sec > try.tv_sec) { |
---|
| 122 | ans.tv_sec--; |
---|
| 123 | ans.tv_usec += 1000000; |
---|
| 124 | } |
---|
| 125 | ans.tv_usec -= try.tv_usec; |
---|
| 126 | |
---|
| 127 | |
---|
| 128 | freq = (sysclocksync * ROAR_OUTPUT_BUFFER_SAMPLES) / (ans.tv_usec / 1e6); |
---|
| 129 | printf("SYNC: f_conf=%iHz, f_real=%.2fHz\n", sa->rate, freq); |
---|
| 130 | |
---|
| 131 | // memcpy(&try, &ans, sizeof(try)); |
---|
| 132 | gettimeofday(&try, NULL); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | if ( sysclocksync ) |
---|
| 136 | loopc++; |
---|
[0] | 137 | } |
---|
| 138 | |
---|
| 139 | return -1; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | //ll |
---|