source: roaraudio/roard/loop.c @ 3358:7f9d211148e0

Last change on this file since 3358:7f9d211148e0 was 3358:7f9d211148e0, checked in by phi, 14 years ago

updated (C) statements

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