source: roaraudio/roard/loop.c @ 4708:c9d40761088a

Last change on this file since 4708:c9d40761088a was 4708:c9d40761088a, checked in by phi, 13 years ago

updated copyright statements

File size: 4.8 KB
RevLine 
[0]1//loop.c:
2
[668]3/*
[4708]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
[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
[905]28int main_loop (int driver, DRIVER_USERDATA_T driver_inst, 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
[1494]65#ifdef ROAR_SUPPORT_LISTEN
[2530]66  have_listen = 0;
67  for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
[3802]68   if ( g_listen[i].used ) {
[2530]69    have_listen = 1;
70    break;
71   }
72  }
73
74  if ( have_listen ) {
[68]75   ROAR_DBG("main_loop(*): check for new clients...");
76   net_check_listen();
77  }
[1494]78#endif
[0]79
80  ROAR_DBG("main_loop(*): check for new data...");
[1494]81#ifdef ROAR_SUPPORT_LISTEN
[2530]82  if ( clients_check_all() == 0 && g_terminate && !have_listen ) {
[1494]83#else
84  if ( clients_check_all() == 0 && g_terminate ) {
85#endif
[547]86   term  = 1;
87  }
[0]88
[2500]89#ifndef ROAR_WITHOUT_DCOMP_MIDI
[1903]90  ROAR_DBG("main_loop(*): updating midi subsystem...");
[1855]91  midi_update();
[2500]92#endif
[1855]93
[0]94  ROAR_DBG("main_loop(*): mixing clients...");
95  if ( g_standby ) {
96   // while in standby we still neet to get the buffers to free input buffer space.
[547]97   streams = streams_get_mixbuffers(&streams_input, sa, g_pos);
[0]98  } else {
[547]99   if ( ( streams = streams_get_mixbuffers(&streams_input, sa, g_pos)) != -1 ) {
[2932]100    roar_mix_pcm(g_output_buffer, sa->bits, streams_input, ROAR_OUTPUT_BUFFER_SAMPLES * sa->channels);
[0]101   }
102  }
103
[547]104  if ( term && streams < 1 )
105   alive = 0;
106
[2947]107  if ( waveform_update_mixer() == -1 )
108   alive = 0;
109
[0]110/*
111  // while in standby we still need to write out our buffer to not run in an endless loop without
112  // a break
113*/
114
[3362]115  if ( plugins_update() == -1 )
116   alive = 0;
[1903]117
118  ROAR_DBG("main_loop(*): sending output data...");
119
[1750]120#ifdef ROAR_HAVE_USLEEP
[920]121  if ( g_standby || (streams < 1 && g_autostandby) ) {
[0]122   usleep((1000000 * ROAR_OUTPUT_BUFFER_SAMPLES) / sa->rate);
[729]123   ROAR_DBG("usleep(%u) = ?\n", (1000000 * ROAR_OUTPUT_BUFFER_SAMPLES) / sa->rate);
[0]124  } else {
[1750]125#endif
[258]126   clients_send_filter(sa, g_pos);
[0]127   output_buffer_flush(driver_inst, driver);
[258]128   clients_send_mon(sa, g_pos);
[1750]129#ifdef ROAR_HAVE_USLEEP
[0]130  }
[1750]131#endif
[257]132
[2500]133#ifndef ROAR_WITHOUT_DCOMP_MIDI
[1855]134  midi_reinit();
[2500]135#endif
[2489]136#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2467]137  ssynth_update();
[2489]138#endif
[2495]139#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1966]140  light_reinit();
[2495]141#endif
[1855]142
[0]143//  output_buffer_reinit();
144
[982]145  g_pos = ROAR_MATH_OVERFLOW_ADD(g_pos, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels);
[259]146  ROAR_DBG("main_loop(*): current pos: %u", g_pos);
[1488]147#if defined(MONITOR_LATENCY) && defined(ROAR_HAVE_GETTIMEOFDAY)
[9]148 gettimeofday(&ans, NULL);
149
150 while (ans.tv_sec > try.tv_sec) {
151  ans.tv_sec--;
152  ans.tv_usec += 1000000;
153 }
154 ans.tv_usec -= try.tv_usec;
155
[36]156 if ( loopc % 128 ) {
[9]157  printf("\e[ucurrent latency: %.3fms  average: %.3fms   ",  ans.tv_usec                               / (double)1000,
158                                                            (ans.tv_usec+ans_3last+ans_2last+ans_1last)/ (double)4000);
159  fflush(stdout);
160 }
161
162 ans_3last = ans_2last;
163 ans_2last = ans_1last;
164 ans_1last = ans.tv_usec;
165#endif
[905]166
[1488]167#ifdef ROAR_HAVE_GETTIMEOFDAY
[906]168  if ( sysclocksync && !(loopc % sysclocksync) ) {
[905]169   gettimeofday(&ans, NULL);
170
171   while (ans.tv_sec > try.tv_sec) {
172    ans.tv_sec--;
173    ans.tv_usec += 1000000;
174   }
175   ans.tv_usec -= try.tv_usec;
176
177
178   freq = (sysclocksync * ROAR_OUTPUT_BUFFER_SAMPLES) / (ans.tv_usec / 1e6);
[2410]179   printf("SYNC: f_conf=%iHz, f_real=%.2fHz diff=%+.3f%%\n", sa->rate, freq, 100*(freq/sa->rate - 1));
[905]180
181//   memcpy(&try, &ans, sizeof(try));
182   gettimeofday(&try, NULL);
183  }
184
185  if ( sysclocksync )
186   loopc++;
[1488]187#endif
[0]188 }
189
190 return -1;
191}
192
193//ll
Note: See TracBrowser for help on using the repository browser.