source: roaraudio/roard/loop.c @ 4820:2cff11e64bae

Last change on this file since 4820:2cff11e64bae was 4820:2cff11e64bae, checked in by phi, 13 years ago

Added Record Bridge to roard.

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