[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; |
---|
[1488] | 31 | #ifdef ROAR_HAVE_GETTIMEOFDAY |
---|
[905] | 32 | long int loopc = 0; |
---|
[9] | 33 | struct timeval try, ans; |
---|
[905] | 34 | float freq; |
---|
[1488] | 35 | #endif |
---|
[905] | 36 | #ifdef MONITOR_LATENCY |
---|
[9] | 37 | long int ans_1last = 0, ans_2last = 0, ans_3last = 0; |
---|
| 38 | |
---|
| 39 | printf("\n\e[s"); |
---|
| 40 | fflush(stdout); |
---|
| 41 | #endif |
---|
[0] | 42 | |
---|
| 43 | ROAR_DBG("main_loop(*) = ?"); |
---|
[982] | 44 | // alive = 1; |
---|
[258] | 45 | g_pos = 0; |
---|
[0] | 46 | |
---|
[1488] | 47 | #ifdef ROAR_HAVE_GETTIMEOFDAY |
---|
[905] | 48 | if ( sysclocksync ) { |
---|
| 49 | gettimeofday(&try, NULL); |
---|
| 50 | } |
---|
[1488] | 51 | #endif |
---|
[905] | 52 | |
---|
[0] | 53 | while (alive) { |
---|
[1488] | 54 | #if defined(MONITOR_LATENCY) && defined(ROAR_HAVE_GETTIMEOFDAY) |
---|
[9] | 55 | gettimeofday(&try, NULL); |
---|
| 56 | #endif |
---|
| 57 | |
---|
[0] | 58 | ROAR_DBG("main_loop(*): looping..."); |
---|
| 59 | |
---|
[1494] | 60 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[68] | 61 | if ( g_listen_socket != -1 ) { |
---|
| 62 | ROAR_DBG("main_loop(*): check for new clients..."); |
---|
| 63 | net_check_listen(); |
---|
| 64 | } |
---|
[1494] | 65 | #endif |
---|
[0] | 66 | |
---|
| 67 | ROAR_DBG("main_loop(*): check for new data..."); |
---|
[1494] | 68 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[547] | 69 | if ( clients_check_all() == 0 && g_terminate && g_listen_socket == -1 ) { |
---|
[1494] | 70 | #else |
---|
| 71 | if ( clients_check_all() == 0 && g_terminate ) { |
---|
| 72 | #endif |
---|
[547] | 73 | term = 1; |
---|
| 74 | } |
---|
[0] | 75 | |
---|
[1903] | 76 | ROAR_DBG("main_loop(*): updating midi subsystem..."); |
---|
[1855] | 77 | midi_update(); |
---|
| 78 | |
---|
[0] | 79 | ROAR_DBG("main_loop(*): mixing clients..."); |
---|
| 80 | if ( g_standby ) { |
---|
| 81 | // while in standby we still neet to get the buffers to free input buffer space. |
---|
[547] | 82 | streams = streams_get_mixbuffers(&streams_input, sa, g_pos); |
---|
[0] | 83 | } else { |
---|
[547] | 84 | if ( ( streams = streams_get_mixbuffers(&streams_input, sa, g_pos)) != -1 ) { |
---|
[0] | 85 | mix_clients(g_output_buffer, sa->bits, streams_input, ROAR_OUTPUT_BUFFER_SAMPLES * sa->channels); |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | |
---|
[547] | 89 | if ( term && streams < 1 ) |
---|
| 90 | alive = 0; |
---|
| 91 | |
---|
[0] | 92 | /* |
---|
| 93 | // while in standby we still need to write out our buffer to not run in an endless loop without |
---|
| 94 | // a break |
---|
| 95 | */ |
---|
| 96 | |
---|
[1903] | 97 | |
---|
| 98 | ROAR_DBG("main_loop(*): sending output data..."); |
---|
| 99 | |
---|
[1750] | 100 | #ifdef ROAR_HAVE_USLEEP |
---|
[920] | 101 | if ( g_standby || (streams < 1 && g_autostandby) ) { |
---|
[0] | 102 | usleep((1000000 * ROAR_OUTPUT_BUFFER_SAMPLES) / sa->rate); |
---|
[729] | 103 | ROAR_DBG("usleep(%u) = ?\n", (1000000 * ROAR_OUTPUT_BUFFER_SAMPLES) / sa->rate); |
---|
[0] | 104 | } else { |
---|
[1750] | 105 | #endif |
---|
[258] | 106 | clients_send_filter(sa, g_pos); |
---|
[0] | 107 | output_buffer_flush(driver_inst, driver); |
---|
[258] | 108 | clients_send_mon(sa, g_pos); |
---|
[1750] | 109 | #ifdef ROAR_HAVE_USLEEP |
---|
[0] | 110 | } |
---|
[1750] | 111 | #endif |
---|
[257] | 112 | |
---|
[1855] | 113 | midi_reinit(); |
---|
[1966] | 114 | light_reinit(); |
---|
[1855] | 115 | |
---|
[0] | 116 | // output_buffer_reinit(); |
---|
| 117 | |
---|
[982] | 118 | g_pos = ROAR_MATH_OVERFLOW_ADD(g_pos, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels); |
---|
[259] | 119 | ROAR_DBG("main_loop(*): current pos: %u", g_pos); |
---|
[1488] | 120 | #if defined(MONITOR_LATENCY) && defined(ROAR_HAVE_GETTIMEOFDAY) |
---|
[9] | 121 | gettimeofday(&ans, NULL); |
---|
| 122 | |
---|
| 123 | while (ans.tv_sec > try.tv_sec) { |
---|
| 124 | ans.tv_sec--; |
---|
| 125 | ans.tv_usec += 1000000; |
---|
| 126 | } |
---|
| 127 | ans.tv_usec -= try.tv_usec; |
---|
| 128 | |
---|
[36] | 129 | if ( loopc % 128 ) { |
---|
[9] | 130 | printf("\e[ucurrent latency: %.3fms average: %.3fms ", ans.tv_usec / (double)1000, |
---|
| 131 | (ans.tv_usec+ans_3last+ans_2last+ans_1last)/ (double)4000); |
---|
| 132 | fflush(stdout); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | ans_3last = ans_2last; |
---|
| 136 | ans_2last = ans_1last; |
---|
| 137 | ans_1last = ans.tv_usec; |
---|
| 138 | #endif |
---|
[905] | 139 | |
---|
[1488] | 140 | #ifdef ROAR_HAVE_GETTIMEOFDAY |
---|
[906] | 141 | if ( sysclocksync && !(loopc % sysclocksync) ) { |
---|
[905] | 142 | gettimeofday(&ans, NULL); |
---|
| 143 | |
---|
| 144 | while (ans.tv_sec > try.tv_sec) { |
---|
| 145 | ans.tv_sec--; |
---|
| 146 | ans.tv_usec += 1000000; |
---|
| 147 | } |
---|
| 148 | ans.tv_usec -= try.tv_usec; |
---|
| 149 | |
---|
| 150 | |
---|
| 151 | freq = (sysclocksync * ROAR_OUTPUT_BUFFER_SAMPLES) / (ans.tv_usec / 1e6); |
---|
[2410] | 152 | printf("SYNC: f_conf=%iHz, f_real=%.2fHz diff=%+.3f%%\n", sa->rate, freq, 100*(freq/sa->rate - 1)); |
---|
[905] | 153 | |
---|
| 154 | // memcpy(&try, &ans, sizeof(try)); |
---|
| 155 | gettimeofday(&try, NULL); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | if ( sysclocksync ) |
---|
| 159 | loopc++; |
---|
[1488] | 160 | #endif |
---|
[0] | 161 | } |
---|
| 162 | |
---|
| 163 | return -1; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | //ll |
---|