File: bseglobals.c

package info (click to toggle)
beast 0.7.4-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 33,500 kB
  • sloc: ansic: 197,348; cpp: 59,114; sh: 11,030; perl: 2,523; makefile: 2,474; xml: 1,026; lisp: 549; awk: 270; sed: 8
file content (228 lines) | stat: -rw-r--r-- 6,869 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* BSE - Better Sound Engine
 * Copyright (C) 1997-2004 Tim Janik
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * A copy of the GNU Lesser General Public License should ship along
 * with this library; if not, see http://www.gnu.org/copyleft/.
 */
#include	"bseglobals.h"
#include	"bsemain.h"


/* --- functions --- */
void
bse_globals_init (void) { /* FIXME: remove */ }

gdouble
bse_db_to_factor (gdouble dB)
{
  gdouble factor = dB / 20; /* Bell */
  return pow (10, factor);
}

gdouble
bse_db_from_factor (gdouble factor,
		    gdouble min_dB)
{
  if (factor > 0)
    {
      gdouble dB = log10 (factor); /* Bell */
      dB *= 20;
      return dB;
    }
  else
    return min_dB;
}

glong
bse_time_range_to_ms (BseTimeRangeType time_range)
{
  g_return_val_if_fail (time_range >= BSE_TIME_RANGE_SHORT, 0);
  g_return_val_if_fail (time_range <= BSE_TIME_RANGE_LONG, 0);
  
  switch (time_range)
    {
    case BSE_TIME_RANGE_SHORT:		return BSE_TIME_RANGE_SHORT_ms;
    case BSE_TIME_RANGE_MEDIUM:		return BSE_TIME_RANGE_MEDIUM_ms;
    case BSE_TIME_RANGE_LONG:		return BSE_TIME_RANGE_LONG_ms;
    }
  return 0;	/* can't be triggered */
}


/* --- idle handlers --- */
/* important ordering constrains:
 * BSE_PRIORITY_NOW             = -G_MAXINT / 2
 * BSE_PRIORITY_HIGH		= G_PRIORITY_HIGH - 10
 * BSE_PRIORITY_NEXT		= G_PRIORITY_HIGH - 5
 * G_PRIORITY_HIGH		(-100)
 * BSE_PRIORITY_NOTIFY		= G_PRIORITY_DEFAULT - 1
 * G_PRIORITY_DEFAULT		(0)
 * GDK_PRIORITY_EVENTS		= G_PRIORITY_DEFAULT
 * BSE_PRIORITY_PROG_IFACE	= G_PRIORITY_DEFAULT
 * G_PRIORITY_HIGH_IDLE		(100)
 * BSE_PRIORITY_UPDATE		= G_PRIORITY_HIGH_IDLE + 5
 * GTK_PRIORITY_RESIZE		= G_PRIORITY_HIGH_IDLE + 10
 * GDK_PRIORITY_REDRAW		= G_PRIORITY_HIGH_IDLE + 20
 * G_PRIORITY_DEFAULT_IDLE	(200)
 * G_PRIORITY_LOW		(300)
 * BSE_PRIORITY_BACKGROUND	= G_PRIORITY_LOW + 500
 */

/**
 * @param function	user function
 * @param data	        user data
 * @return		idle handler id, suitable for bse_idle_remove()
 * Execute @a function (@a data) inside the main BSE thread as soon as possible.
 * Usually this function should not be used but bse_idle_next() should be used instead.
 * Only callbacks that have hard dependencies on immediate asyncronous execution,
 * preceeding even realtime synthesis job handling should be executed this way.
 * This function is MT-safe and may be called from any thread.
 */
guint
bse_idle_now (GSourceFunc function,
	      gpointer    data)
{
  GSource *source = g_idle_source_new ();
  guint id;
  g_source_set_priority (source, BSE_PRIORITY_NOW);
  g_source_set_callback (source, function, data, NULL);
  id = g_source_attach (source, bse_main_context);
  g_source_unref (source);
  return id;
}

/**
 * @param function	user function
 * @param data	        user data
 * @return		idle handler id, suitable for bse_idle_remove()
 * Execute @a function (@a data) inside the main BSE thread as soon as resonably possible.
 * This function is intended to be used by code which needs to execute some portions
 * asyncronously as soon as the BSE core isn't occupied by realtime job handling.
 * This function is MT-safe and may be called from any thread.
 */
guint
bse_idle_next (GSourceFunc function,
	      gpointer    data)
{
  GSource *source = g_idle_source_new ();
  guint id;
  g_source_set_priority (source, BSE_PRIORITY_NEXT);
  g_source_set_callback (source, function, data, NULL);
  id = g_source_attach (source, bse_main_context);
  g_source_unref (source);
  return id;
}

/**
 * @param function	user function
 * @param data	user data
 * @return		idle handler id, suitable for bse_idle_remove()
 * Queue @a function (@a data) for execution inside the main BSE thread,
 * similar to bse_idle_now(), albeit with a lower priority.
 * This function is intended to be used by code which emits
 * asyncronous notifications.
 * This function is MT-safe and may be called from any thread.
 */
guint
bse_idle_notify (GSourceFunc function,
		 gpointer    data)
{
  GSource *source = g_idle_source_new ();
  guint id;
  g_source_set_priority (source, BSE_PRIORITY_NOTIFY);
  g_source_set_callback (source, function, data, NULL);
  id = g_source_attach (source, bse_main_context);
  g_source_unref (source);
  return id;
}

guint
bse_idle_normal (GSourceFunc function,
		 gpointer    data)
{
  GSource *source = g_idle_source_new ();
  guint id;
  g_source_set_priority (source, BSE_PRIORITY_NORMAL);
  g_source_set_callback (source, function, data, NULL);
  id = g_source_attach (source, bse_main_context);
  g_source_unref (source);
  return id;
}

guint
bse_idle_update (GSourceFunc function,
		 gpointer    data)
{
  GSource *source = g_idle_source_new ();
  guint id;
  g_source_set_priority (source, BSE_PRIORITY_UPDATE);
  g_source_set_callback (source, function, data, NULL);
  id = g_source_attach (source, bse_main_context);
  g_source_unref (source);
  return id;
}

guint
bse_idle_background (GSourceFunc function,
		     gpointer    data)
{
  GSource *source = g_idle_source_new ();
  guint id;
  g_source_set_priority (source, BSE_PRIORITY_BACKGROUND);
  g_source_set_callback (source, function, data, NULL);
  id = g_source_attach (source, bse_main_context);
  g_source_unref (source);
  return id;
}

/**
 * @param usec_delay	microsecond delay
 * @param function	user function
 * @param data	user data
 * @return		idle handler id, suitable for bse_idle_remove()
 * Execute @a function (@a data) with the main BSE thread, similar to
 * bse_idle_now(), after a delay period of @a usec_delay has passed.
 * This function is MT-safe and may be called from any thread.
 */
guint
bse_idle_timed (guint64     usec_delay,
		GSourceFunc function,
		gpointer    data)
{
  GSource *source = g_timeout_source_new (CLAMP (usec_delay / 1000, 0, G_MAXUINT));
  guint id;
  g_source_set_priority (source, BSE_PRIORITY_NEXT);
  g_source_set_callback (source, function, data, NULL);
  id = g_source_attach (source, bse_main_context);
  g_source_unref (source);
  return id;
}

/**
 * @param id	idle handler id
 * Remove or unqueue an idle handler queued by bse_idle_now()
 * or one of its variants.
 * This function is MT-safe and may be called from any thread.
 */
gboolean
bse_idle_remove (guint id)
{
  GSource *source;

  g_return_val_if_fail (id > 0, FALSE);

  source = g_main_context_find_source_by_id (bse_main_context, id);
  if (source)
    g_source_destroy (source);
  return source != NULL;
}