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 | |
---|
27 | int main_loop (int driver, DRIVER_USERDATA_T driver_inst, struct roar_audio_info * sa) { |
---|
28 | void ** streams_input = NULL; |
---|
29 | int term = 0; |
---|
30 | int streams; |
---|
31 | #ifdef MONITOR_LATENCY |
---|
32 | struct timeval try, ans; |
---|
33 | long int ans_1last = 0, ans_2last = 0, ans_3last = 0; |
---|
34 | long int loopc = 0; |
---|
35 | |
---|
36 | printf("\n\e[s"); |
---|
37 | fflush(stdout); |
---|
38 | #endif |
---|
39 | |
---|
40 | ROAR_DBG("main_loop(*) = ?"); |
---|
41 | alive = 1; |
---|
42 | g_pos = 0; |
---|
43 | |
---|
44 | while (alive) { |
---|
45 | #ifdef MONITOR_LATENCY |
---|
46 | gettimeofday(&try, NULL); |
---|
47 | #endif |
---|
48 | |
---|
49 | ROAR_DBG("main_loop(*): looping..."); |
---|
50 | |
---|
51 | if ( g_listen_socket != -1 ) { |
---|
52 | ROAR_DBG("main_loop(*): check for new clients..."); |
---|
53 | net_check_listen(); |
---|
54 | } |
---|
55 | |
---|
56 | ROAR_DBG("main_loop(*): check for new data..."); |
---|
57 | if ( clients_check_all() == 0 && g_terminate && g_listen_socket == -1 ) { |
---|
58 | term = 1; |
---|
59 | } |
---|
60 | |
---|
61 | ROAR_DBG("main_loop(*): mixing clients..."); |
---|
62 | if ( g_standby ) { |
---|
63 | // while in standby we still neet to get the buffers to free input buffer space. |
---|
64 | streams = streams_get_mixbuffers(&streams_input, sa, g_pos); |
---|
65 | } else { |
---|
66 | if ( ( streams = streams_get_mixbuffers(&streams_input, sa, g_pos)) != -1 ) { |
---|
67 | mix_clients(g_output_buffer, sa->bits, streams_input, ROAR_OUTPUT_BUFFER_SAMPLES * sa->channels); |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | if ( term && streams < 1 ) |
---|
72 | alive = 0; |
---|
73 | |
---|
74 | /* |
---|
75 | // while in standby we still need to write out our buffer to not run in an endless loop without |
---|
76 | // a break |
---|
77 | */ |
---|
78 | |
---|
79 | if ( g_standby ) { |
---|
80 | usleep((1000000 * ROAR_OUTPUT_BUFFER_SAMPLES) / sa->rate); |
---|
81 | ROAR_DBG("usleep(%u) = ?\n", (1000000 * ROAR_OUTPUT_BUFFER_SAMPLES) / sa->rate); |
---|
82 | } else { |
---|
83 | clients_send_filter(sa, g_pos); |
---|
84 | output_buffer_flush(driver_inst, driver); |
---|
85 | clients_send_mon(sa, g_pos); |
---|
86 | } |
---|
87 | |
---|
88 | midi_cb_update(); |
---|
89 | // output_buffer_reinit(); |
---|
90 | |
---|
91 | g_pos = ROAR_MATH_OVERFLOW_ADD(g_pos, ROAR_OUTPUT_BUFFER_SAMPLES); |
---|
92 | ROAR_DBG("main_loop(*): current pos: %u", g_pos); |
---|
93 | #ifdef MONITOR_LATENCY |
---|
94 | gettimeofday(&ans, NULL); |
---|
95 | |
---|
96 | while (ans.tv_sec > try.tv_sec) { |
---|
97 | ans.tv_sec--; |
---|
98 | ans.tv_usec += 1000000; |
---|
99 | } |
---|
100 | ans.tv_usec -= try.tv_usec; |
---|
101 | |
---|
102 | if ( loopc % 128 ) { |
---|
103 | printf("\e[ucurrent latency: %.3fms average: %.3fms ", ans.tv_usec / (double)1000, |
---|
104 | (ans.tv_usec+ans_3last+ans_2last+ans_1last)/ (double)4000); |
---|
105 | fflush(stdout); |
---|
106 | } |
---|
107 | |
---|
108 | ans_3last = ans_2last; |
---|
109 | ans_2last = ans_1last; |
---|
110 | ans_1last = ans.tv_usec; |
---|
111 | loopc++; |
---|
112 | #endif |
---|
113 | } |
---|
114 | |
---|
115 | return -1; |
---|
116 | } |
---|
117 | |
---|
118 | //ll |
---|