[0] | 1 | //loop.c: |
---|
| 2 | |
---|
[668] | 3 | /* |
---|
[5823] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2013 |
---|
[668] | 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 |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[668] | 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
[0] | 26 | #include "roard.h" |
---|
| 27 | |
---|
[4811] | 28 | int main_loop (struct roar_audio_info * sa, int sysclocksync) { |
---|
[0] | 29 | void ** streams_input = NULL; |
---|
[547] | 30 | int term = 0; |
---|
| 31 | int streams; |
---|
[1488] | 32 | #ifdef ROAR_HAVE_GETTIMEOFDAY |
---|
[905] | 33 | long int loopc = 0; |
---|
[9] | 34 | struct timeval try, ans; |
---|
[905] | 35 | float freq; |
---|
[1488] | 36 | #endif |
---|
[2530] | 37 | #ifdef ROAR_SUPPORT_LISTEN |
---|
| 38 | int i; |
---|
| 39 | int have_listen; |
---|
| 40 | #endif |
---|
[905] | 41 | #ifdef MONITOR_LATENCY |
---|
[9] | 42 | long int ans_1last = 0, ans_2last = 0, ans_3last = 0; |
---|
| 43 | |
---|
| 44 | printf("\n\e[s"); |
---|
| 45 | fflush(stdout); |
---|
| 46 | #endif |
---|
[0] | 47 | |
---|
| 48 | ROAR_DBG("main_loop(*) = ?"); |
---|
[982] | 49 | // alive = 1; |
---|
[258] | 50 | g_pos = 0; |
---|
[0] | 51 | |
---|
[1488] | 52 | #ifdef ROAR_HAVE_GETTIMEOFDAY |
---|
[905] | 53 | if ( sysclocksync ) { |
---|
| 54 | gettimeofday(&try, NULL); |
---|
| 55 | } |
---|
[1488] | 56 | #endif |
---|
[905] | 57 | |
---|
[0] | 58 | while (alive) { |
---|
[1488] | 59 | #if defined(MONITOR_LATENCY) && defined(ROAR_HAVE_GETTIMEOFDAY) |
---|
[9] | 60 | gettimeofday(&try, NULL); |
---|
| 61 | #endif |
---|
| 62 | |
---|
[0] | 63 | ROAR_DBG("main_loop(*): looping..."); |
---|
| 64 | |
---|
[5619] | 65 | roar_watchdog_trigger(); |
---|
| 66 | |
---|
[1494] | 67 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[2530] | 68 | have_listen = 0; |
---|
| 69 | for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) { |
---|
[3802] | 70 | if ( g_listen[i].used ) { |
---|
[2530] | 71 | have_listen = 1; |
---|
| 72 | break; |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | if ( have_listen ) { |
---|
[68] | 77 | ROAR_DBG("main_loop(*): check for new clients..."); |
---|
| 78 | net_check_listen(); |
---|
| 79 | } |
---|
[1494] | 80 | #endif |
---|
[0] | 81 | |
---|
| 82 | ROAR_DBG("main_loop(*): check for new data..."); |
---|
[1494] | 83 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[2530] | 84 | if ( clients_check_all() == 0 && g_terminate && !have_listen ) { |
---|
[1494] | 85 | #else |
---|
| 86 | if ( clients_check_all() == 0 && g_terminate ) { |
---|
| 87 | #endif |
---|
[547] | 88 | term = 1; |
---|
| 89 | } |
---|
[0] | 90 | |
---|
[2500] | 91 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
[1903] | 92 | ROAR_DBG("main_loop(*): updating midi subsystem..."); |
---|
[1855] | 93 | midi_update(); |
---|
[2500] | 94 | #endif |
---|
[1855] | 95 | |
---|
[4820] | 96 | if ( waveform_update_inputs() == -1 ) |
---|
| 97 | alive = 0; |
---|
| 98 | |
---|
[0] | 99 | ROAR_DBG("main_loop(*): mixing clients..."); |
---|
| 100 | if ( g_standby ) { |
---|
| 101 | // while in standby we still neet to get the buffers to free input buffer space. |
---|
[547] | 102 | streams = streams_get_mixbuffers(&streams_input, sa, g_pos); |
---|
[0] | 103 | } else { |
---|
[547] | 104 | if ( ( streams = streams_get_mixbuffers(&streams_input, sa, g_pos)) != -1 ) { |
---|
[2932] | 105 | roar_mix_pcm(g_output_buffer, sa->bits, streams_input, ROAR_OUTPUT_BUFFER_SAMPLES * sa->channels); |
---|
[0] | 106 | } |
---|
| 107 | } |
---|
| 108 | |
---|
[547] | 109 | if ( term && streams < 1 ) |
---|
| 110 | alive = 0; |
---|
| 111 | |
---|
[2947] | 112 | if ( waveform_update_mixer() == -1 ) |
---|
| 113 | alive = 0; |
---|
| 114 | |
---|
[0] | 115 | /* |
---|
| 116 | // while in standby we still need to write out our buffer to not run in an endless loop without |
---|
| 117 | // a break |
---|
| 118 | */ |
---|
| 119 | |
---|
[3362] | 120 | if ( plugins_update() == -1 ) |
---|
| 121 | alive = 0; |
---|
[1903] | 122 | |
---|
| 123 | ROAR_DBG("main_loop(*): sending output data..."); |
---|
| 124 | |
---|
[1750] | 125 | #ifdef ROAR_HAVE_USLEEP |
---|
[920] | 126 | if ( g_standby || (streams < 1 && g_autostandby) ) { |
---|
[4896] | 127 | roar_usleep(((uint_least32_t)1000000 * ROAR_OUTPUT_BUFFER_SAMPLES) / sa->rate); |
---|
[729] | 128 | ROAR_DBG("usleep(%u) = ?\n", (1000000 * ROAR_OUTPUT_BUFFER_SAMPLES) / sa->rate); |
---|
[0] | 129 | } else { |
---|
[1750] | 130 | #endif |
---|
[5586] | 131 | clients_send_filter(sa); |
---|
[5022] | 132 | // streams_send_filter_all(); // this currently results in deadlocks. |
---|
[5586] | 133 | clients_send_mon(sa); |
---|
[1750] | 134 | #ifdef ROAR_HAVE_USLEEP |
---|
[0] | 135 | } |
---|
[1750] | 136 | #endif |
---|
[257] | 137 | |
---|
[2500] | 138 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
[1855] | 139 | midi_reinit(); |
---|
[2500] | 140 | #endif |
---|
[2489] | 141 | #ifndef ROAR_WITHOUT_DCOMP_SSYNTH |
---|
[2467] | 142 | ssynth_update(); |
---|
[2489] | 143 | #endif |
---|
[2495] | 144 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
[1966] | 145 | light_reinit(); |
---|
[2495] | 146 | #endif |
---|
[1855] | 147 | |
---|
[0] | 148 | // output_buffer_reinit(); |
---|
| 149 | |
---|
[982] | 150 | g_pos = ROAR_MATH_OVERFLOW_ADD(g_pos, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels); |
---|
[259] | 151 | ROAR_DBG("main_loop(*): current pos: %u", g_pos); |
---|
[1488] | 152 | #if defined(MONITOR_LATENCY) && defined(ROAR_HAVE_GETTIMEOFDAY) |
---|
[9] | 153 | gettimeofday(&ans, NULL); |
---|
| 154 | |
---|
| 155 | while (ans.tv_sec > try.tv_sec) { |
---|
| 156 | ans.tv_sec--; |
---|
| 157 | ans.tv_usec += 1000000; |
---|
| 158 | } |
---|
| 159 | ans.tv_usec -= try.tv_usec; |
---|
| 160 | |
---|
[36] | 161 | if ( loopc % 128 ) { |
---|
[9] | 162 | printf("\e[ucurrent latency: %.3fms average: %.3fms ", ans.tv_usec / (double)1000, |
---|
| 163 | (ans.tv_usec+ans_3last+ans_2last+ans_1last)/ (double)4000); |
---|
| 164 | fflush(stdout); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | ans_3last = ans_2last; |
---|
| 168 | ans_2last = ans_1last; |
---|
| 169 | ans_1last = ans.tv_usec; |
---|
| 170 | #endif |
---|
[905] | 171 | |
---|
[1488] | 172 | #ifdef ROAR_HAVE_GETTIMEOFDAY |
---|
[906] | 173 | if ( sysclocksync && !(loopc % sysclocksync) ) { |
---|
[905] | 174 | gettimeofday(&ans, NULL); |
---|
| 175 | |
---|
| 176 | while (ans.tv_sec > try.tv_sec) { |
---|
| 177 | ans.tv_sec--; |
---|
| 178 | ans.tv_usec += 1000000; |
---|
| 179 | } |
---|
| 180 | ans.tv_usec -= try.tv_usec; |
---|
| 181 | |
---|
| 182 | |
---|
| 183 | freq = (sysclocksync * ROAR_OUTPUT_BUFFER_SAMPLES) / (ans.tv_usec / 1e6); |
---|
[2410] | 184 | printf("SYNC: f_conf=%iHz, f_real=%.2fHz diff=%+.3f%%\n", sa->rate, freq, 100*(freq/sa->rate - 1)); |
---|
[905] | 185 | |
---|
| 186 | // memcpy(&try, &ans, sizeof(try)); |
---|
| 187 | gettimeofday(&try, NULL); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | if ( sysclocksync ) |
---|
| 191 | loopc++; |
---|
[1488] | 192 | #endif |
---|
[0] | 193 | } |
---|
| 194 | |
---|
| 195 | return -1; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | //ll |
---|