source: roaraudio/roard/loop.c @ 2932:09d25b79ce75

Last change on this file since 2932:09d25b79ce75 was 2932:09d25b79ce75, checked in by phi, 15 years ago

removed now non-needed mix_clients*()

File size: 4.7 KB
Line 
1//loop.c:
2
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
25#include "roard.h"
26
27int main_loop (int driver, DRIVER_USERDATA_T driver_inst, struct roar_audio_info * sa, int sysclocksync) {
28 void ** streams_input = NULL;
29 int     term = 0;
30 int     streams;
31#ifdef ROAR_HAVE_GETTIMEOFDAY
32 long int loopc = 0;
33 struct timeval         try, ans;
34 float  freq;
35#endif
36#ifdef ROAR_SUPPORT_LISTEN
37 int i;
38 int have_listen;
39#endif
40#ifdef MONITOR_LATENCY
41 long int ans_1last = 0, ans_2last = 0, ans_3last = 0;
42
43 printf("\n\e[s");
44 fflush(stdout);
45#endif
46
47 ROAR_DBG("main_loop(*) = ?");
48// alive = 1;
49 g_pos = 0;
50
51#ifdef ROAR_HAVE_GETTIMEOFDAY
52 if ( sysclocksync ) {
53  gettimeofday(&try, NULL);
54 }
55#endif
56
57 while (alive) {
58#if defined(MONITOR_LATENCY) && defined(ROAR_HAVE_GETTIMEOFDAY)
59 gettimeofday(&try, NULL);
60#endif
61
62  ROAR_DBG("main_loop(*): looping...");
63
64#ifdef ROAR_SUPPORT_LISTEN
65  have_listen = 0;
66  for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
67   if ( g_listen_socket[i] != -1 ) {
68    have_listen = 1;
69    break;
70   }
71  }
72
73  if ( have_listen ) {
74   ROAR_DBG("main_loop(*): check for new clients...");
75   net_check_listen();
76  }
77#endif
78
79  ROAR_DBG("main_loop(*): check for new data...");
80#ifdef ROAR_SUPPORT_LISTEN
81  if ( clients_check_all() == 0 && g_terminate && !have_listen ) {
82#else
83  if ( clients_check_all() == 0 && g_terminate ) {
84#endif
85   term  = 1;
86  }
87
88#ifndef ROAR_WITHOUT_DCOMP_MIDI
89  ROAR_DBG("main_loop(*): updating midi subsystem...");
90  midi_update();
91#endif
92
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.
96   streams = streams_get_mixbuffers(&streams_input, sa, g_pos);
97  } else {
98   if ( ( streams = streams_get_mixbuffers(&streams_input, sa, g_pos)) != -1 ) {
99    roar_mix_pcm(g_output_buffer, sa->bits, streams_input, ROAR_OUTPUT_BUFFER_SAMPLES * sa->channels);
100   }
101  }
102
103  if ( term && streams < 1 )
104   alive = 0;
105
106/*
107  // while in standby we still need to write out our buffer to not run in an endless loop without
108  // a break
109*/
110
111
112  ROAR_DBG("main_loop(*): sending output data...");
113
114#ifdef ROAR_HAVE_USLEEP
115  if ( g_standby || (streams < 1 && g_autostandby) ) {
116   usleep((1000000 * ROAR_OUTPUT_BUFFER_SAMPLES) / sa->rate);
117   ROAR_DBG("usleep(%u) = ?\n", (1000000 * ROAR_OUTPUT_BUFFER_SAMPLES) / sa->rate);
118  } else {
119#endif
120   clients_send_filter(sa, g_pos);
121   output_buffer_flush(driver_inst, driver);
122   clients_send_mon(sa, g_pos);
123#ifdef ROAR_HAVE_USLEEP
124  }
125#endif
126
127#ifndef ROAR_WITHOUT_DCOMP_MIDI
128  midi_reinit();
129#endif
130#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
131  ssynth_update();
132#endif
133#ifndef ROAR_WITHOUT_DCOMP_LIGHT
134  light_reinit();
135#endif
136
137//  output_buffer_reinit();
138
139  g_pos = ROAR_MATH_OVERFLOW_ADD(g_pos, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels);
140  ROAR_DBG("main_loop(*): current pos: %u", g_pos);
141#if defined(MONITOR_LATENCY) && defined(ROAR_HAVE_GETTIMEOFDAY)
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 if ( loopc % 128 ) {
151  printf("\e[ucurrent latency: %.3fms  average: %.3fms   ",  ans.tv_usec                               / (double)1000,
152                                                            (ans.tv_usec+ans_3last+ans_2last+ans_1last)/ (double)4000);
153  fflush(stdout);
154 }
155
156 ans_3last = ans_2last;
157 ans_2last = ans_1last;
158 ans_1last = ans.tv_usec;
159#endif
160
161#ifdef ROAR_HAVE_GETTIMEOFDAY
162  if ( sysclocksync && !(loopc % sysclocksync) ) {
163   gettimeofday(&ans, NULL);
164
165   while (ans.tv_sec > try.tv_sec) {
166    ans.tv_sec--;
167    ans.tv_usec += 1000000;
168   }
169   ans.tv_usec -= try.tv_usec;
170
171
172   freq = (sysclocksync * ROAR_OUTPUT_BUFFER_SAMPLES) / (ans.tv_usec / 1e6);
173   printf("SYNC: f_conf=%iHz, f_real=%.2fHz diff=%+.3f%%\n", sa->rate, freq, 100*(freq/sa->rate - 1));
174
175//   memcpy(&try, &ans, sizeof(try));
176   gettimeofday(&try, NULL);
177  }
178
179  if ( sysclocksync )
180   loopc++;
181#endif
182 }
183
184 return -1;
185}
186
187//ll
Note: See TracBrowser for help on using the repository browser.