source: roaraudio/roard/loop.c @ 5980:334fe3fbca15

Last change on this file since 5980:334fe3fbca15 was 5980:334fe3fbca15, checked in by phi, 10 years ago

done some updates to the light control subsystem. This will help allowing a more modern infrstructure for light control filters

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