source: roaraudio/roard/loop.c @ 1855:60ee58430f49

Last change on this file since 1855:60ee58430f49 was 1855:60ee58430f49, checked in by phi, 15 years ago

there need to be a function for midi called before mixing and one after for cleanup.

File size: 4.2 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 MONITOR_LATENCY
37 long int ans_1last = 0, ans_2last = 0, ans_3last = 0;
38
39 printf("\n\e[s");
40 fflush(stdout);
41#endif
42
43 ROAR_DBG("main_loop(*) = ?");
44// alive = 1;
45 g_pos = 0;
46
47#ifdef ROAR_HAVE_GETTIMEOFDAY
48 if ( sysclocksync ) {
49  gettimeofday(&try, NULL);
50 }
51#endif
52
53 while (alive) {
54#if defined(MONITOR_LATENCY) && defined(ROAR_HAVE_GETTIMEOFDAY)
55 gettimeofday(&try, NULL);
56#endif
57
58  ROAR_DBG("main_loop(*): looping...");
59
60#ifdef ROAR_SUPPORT_LISTEN
61  if ( g_listen_socket != -1 ) {
62   ROAR_DBG("main_loop(*): check for new clients...");
63   net_check_listen();
64  }
65#endif
66
67  ROAR_DBG("main_loop(*): check for new data...");
68#ifdef ROAR_SUPPORT_LISTEN
69  if ( clients_check_all() == 0 && g_terminate && g_listen_socket == -1 ) {
70#else
71  if ( clients_check_all() == 0 && g_terminate ) {
72#endif
73   term  = 1;
74  }
75
76  midi_update();
77
78  ROAR_DBG("main_loop(*): mixing clients...");
79  if ( g_standby ) {
80   // while in standby we still neet to get the buffers to free input buffer space.
81   streams = streams_get_mixbuffers(&streams_input, sa, g_pos);
82  } else {
83   if ( ( streams = streams_get_mixbuffers(&streams_input, sa, g_pos)) != -1 ) {
84    mix_clients(g_output_buffer, sa->bits, streams_input, ROAR_OUTPUT_BUFFER_SAMPLES * sa->channels);
85   }
86  }
87
88  if ( term && streams < 1 )
89   alive = 0;
90
91/*
92  // while in standby we still need to write out our buffer to not run in an endless loop without
93  // a break
94*/
95
96#ifdef ROAR_HAVE_USLEEP
97  if ( g_standby || (streams < 1 && g_autostandby) ) {
98   usleep((1000000 * ROAR_OUTPUT_BUFFER_SAMPLES) / sa->rate);
99   ROAR_DBG("usleep(%u) = ?\n", (1000000 * ROAR_OUTPUT_BUFFER_SAMPLES) / sa->rate);
100  } else {
101#endif
102   clients_send_filter(sa, g_pos);
103   output_buffer_flush(driver_inst, driver);
104   clients_send_mon(sa, g_pos);
105#ifdef ROAR_HAVE_USLEEP
106  }
107#endif
108
109  midi_reinit();
110
111//  output_buffer_reinit();
112
113  g_pos = ROAR_MATH_OVERFLOW_ADD(g_pos, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels);
114  ROAR_DBG("main_loop(*): current pos: %u", g_pos);
115#if defined(MONITOR_LATENCY) && defined(ROAR_HAVE_GETTIMEOFDAY)
116 gettimeofday(&ans, NULL);
117
118 while (ans.tv_sec > try.tv_sec) {
119  ans.tv_sec--;
120  ans.tv_usec += 1000000;
121 }
122 ans.tv_usec -= try.tv_usec;
123
124 if ( loopc % 128 ) {
125  printf("\e[ucurrent latency: %.3fms  average: %.3fms   ",  ans.tv_usec                               / (double)1000,
126                                                            (ans.tv_usec+ans_3last+ans_2last+ans_1last)/ (double)4000);
127  fflush(stdout);
128 }
129
130 ans_3last = ans_2last;
131 ans_2last = ans_1last;
132 ans_1last = ans.tv_usec;
133#endif
134
135#ifdef ROAR_HAVE_GETTIMEOFDAY
136  if ( sysclocksync && !(loopc % sysclocksync) ) {
137   gettimeofday(&ans, NULL);
138
139   while (ans.tv_sec > try.tv_sec) {
140    ans.tv_sec--;
141    ans.tv_usec += 1000000;
142   }
143   ans.tv_usec -= try.tv_usec;
144
145
146   freq = (sysclocksync * ROAR_OUTPUT_BUFFER_SAMPLES) / (ans.tv_usec / 1e6);
147   printf("SYNC: f_conf=%iHz, f_real=%.2fHz\n", sa->rate, freq);
148
149//   memcpy(&try, &ans, sizeof(try));
150   gettimeofday(&try, NULL);
151  }
152
153  if ( sysclocksync )
154   loopc++;
155#endif
156 }
157
158 return -1;
159}
160
161//ll
Note: See TracBrowser for help on using the repository browser.