File: utility.c

package info (click to toggle)
quisk 4.1.77-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,608 kB
  • sloc: python: 19,111; ansic: 18,311; makefile: 46; sh: 2
file content (322 lines) | stat: -rwxr-xr-x 8,482 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <Python.h>
#ifdef MS_WINDOWS
#include <windows.h>
#else
#include <stdlib.h>
#include <sys/time.h>
#endif
#include <complex.h>
#include "quisk.h"

// Access to config file attributes.
// NOTE:  These must be called only from the main (GUI) thread,
//        not from the sound thread.

int QuiskGetConfigInt(const char * name, int deflt)
{  // return deflt for failure.  Accept int or float.
  int res;
  PyObject * attr;  
  if (!quisk_pyConfig || PyErr_Occurred()) {
    return deflt;
  }
  attr = PyObject_GetAttrString(quisk_pyConfig, name);
  if (attr) {
    res = (int)PyInt_AsUnsignedLongMask(attr);  // This works for floats too!
    Py_DECREF(attr);
    return res;		// success
  }
  else {
    PyErr_Clear();
  }
  return deflt;		// failure
}

int QuiskGetConfigBoolean(const char * name, int deflt)		// UNTESTED
{  //  Return 1 for True, 0 for False.  Return deflt for failure.
  int res;
  PyObject * attr;  
  if (!quisk_pyConfig || PyErr_Occurred()) {
    return deflt;
  }
  attr = PyObject_GetAttrString(quisk_pyConfig, name);
  if (attr) {
    res = PyObject_IsTrue(attr);
    Py_DECREF(attr);
    return res;		// success
  }
  else {
    PyErr_Clear();
  }
  return deflt;		// failure
}

double QuiskGetConfigDouble(const char * name, double deflt)
{  // return deflt for failure.  Accept int or float.
  double res;
  PyObject * attr;  

  if (!quisk_pyConfig || PyErr_Occurred())
    return deflt;
  attr = PyObject_GetAttrString(quisk_pyConfig, name);
  if (attr) {
    res = PyFloat_AsDouble(attr);
    Py_DECREF(attr);
    return res;		// success
  }
  else {
    PyErr_Clear();
  }
  return deflt;		// failure
}

char * QuiskGetConfigString(const char * name, char * deflt)
{  // Return the UTF-8 configuration string. Return deflt for failure.
  char * res;
  PyObject * attr;
#if PY_MAJOR_VERSION < 3
  static char retbuf[QUISK_SC_SIZE];
#endif

  if (!quisk_pyConfig || PyErr_Occurred())
    return deflt;
  attr = PyObject_GetAttrString(quisk_pyConfig, name);
  if (attr) {
#if PY_MAJOR_VERSION >= 3
    res = (char *)PyUnicode_AsUTF8(attr);
#else
    if (PyUnicode_Check(attr)) {
      PyObject * pystr = PyUnicode_AsUTF8String(attr);
      strMcpy(retbuf, PyString_AsString(pystr), QUISK_SC_SIZE);
      retbuf[QUISK_SC_SIZE - 1] = 0;
      res = retbuf;
      Py_DECREF(pystr);
    }
    else {
      res = PyString_AsString(attr);
    }
#endif
    Py_DECREF(attr);
    if (res)
      return res;		// success
    else
      PyErr_Clear();
  }
  else {
    PyErr_Clear();
  }
  return deflt;		// failure
}

double QuiskTimeSec(void)
{  // return time in seconds as a double
#ifdef MS_WINDOWS
	FILETIME ft;
	ULARGE_INTEGER ll;

	GetSystemTimeAsFileTime(&ft);
	ll.LowPart  = ft.dwLowDateTime;
	ll.HighPart = ft.dwHighDateTime;
	return (double)ll.QuadPart * 1.e-7;
#else
	struct timeval tv;

	gettimeofday(&tv, NULL);
	return (double)tv.tv_sec + tv.tv_usec * 1e-6;
#endif
}

int QuiskDeltaMsec(int timer)
{  // return the number of milliseconds since the last call for the timer.
   // There are two timers. The "timer" is either 0 or 1. Call first and throw away the result.
	static long long time0[2] = {0, 0};
	long long now;  // in msec
	int delta;
#ifdef MS_WINDOWS
	// Code contributed by Ben Cahill
	static long long timer_rate = 0;
	LARGE_INTEGER L;
	if ( ! timer_rate) {
		if (QueryPerformanceFrequency(&L))
			timer_rate = L.QuadPart;
		else
			timer_rate = 1;
	}
	if (QueryPerformanceCounter(&L))
		now = (double)L.QuadPart * 1000 / timer_rate;
	else
		now = 0;
#else
	struct timespec ts;
#ifdef CLOCK_MONOTONIC_RAW
	if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts) != 0)
#else
	if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
#endif
		return 0;
	now = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
#endif
	if (timer < 0 || timer >= 2)
		return 0;
	if (now < time0[timer])
		now = time0[timer] = 0;
	delta = (int)(now - time0[timer]);
	time0[timer] = now;
	return delta;
}

void QuiskPrintTime(const char * str, int index)
{  // print the time and a message and the delta time for index 0 to 9
	double tm;
	int i;
	static double time0 = 0;
	static double start_time[10];
#ifdef MS_WINDOWS
	static long long timer_rate = 0;
	LARGE_INTEGER L;
	if ( ! timer_rate) {
		if (QueryPerformanceFrequency(&L))
			timer_rate = L.QuadPart;
		else
			timer_rate = 1;
	}
	if (QueryPerformanceCounter(&L))
		tm = (double)L.QuadPart / timer_rate;
	else
		tm = 0;
#else
	struct timeval tv;
	gettimeofday(&tv, NULL);
	tm = (double)tv.tv_sec + tv.tv_usec * 1e-6;
#endif
	if (index < -9 || index > 9)	// error
		return;
	if (index < 0) {
		start_time[ - index] = tm;
		return;
	}
	if ( ! str) {		// initialize
		time0 = tm;
		for (i = 0; i < 10; i++)
			start_time[i] = tm;
		return;
	}
	// print the time since startup, and the time since the last call
	if (index > 0) {
		if (str[0])	// print message and a newline
			QuiskPrintf ("%12.6lf  %9.3lf  %9.3lf  %s\n",
				tm - time0, (tm - start_time[0])*1e3, (tm - start_time[index])*1e3, str);
		else		// no message; omit newline
			QuiskPrintf ("%12.6lf  %9.3lf  %9.3lf  ",
				tm - time0, (tm - start_time[0])*1e3, (tm - start_time[index])*1e3);
	}
	else {
		if (str[0])	// print message and a newline
			QuiskPrintf ("%12.6lf  %9.3lf  %s\n",
				tm - time0, (tm - start_time[0])*1e3, str);
		else		// no message; omit newline
			QuiskPrintf ("%12.6lf  %9.3lf  ",
				tm - time0, (tm - start_time[0])*1e3);
	}
	start_time[0] = tm;
}

void QuiskSleepMicrosec(int usec)
{
#ifdef MS_WINDOWS
	int msec = (usec + 500) / 1000;		// convert to milliseconds
	if (msec < 1)
		msec = 1;
	Sleep(msec);
#else
	struct timespec tspec;
	tspec.tv_sec = usec / 1000000;
	tspec.tv_nsec = (usec - tspec.tv_sec * 1000000) * 1000;
	nanosleep(&tspec, NULL);
#endif
}

void QuiskMeasureRate(const char * msg, int count, int index, int reset)
{  //measure the sample rate for index 0 to 9. If reset, reset the count and time at each print.
	double tm;
	static unsigned long total[10] = {0,0,0,0,0,0,0,0,0,0};
	static double time0[10] = {0,0,0,0,0,0,0,0,0,0};
	static double time_pr[10] = {0,0,0,0,0,0,0,0,0,0};

	if ( ! msg) {	// init
		time0[index] = 0;
                total[index] = 0;
		return;
	}
	if (count && time0[index] == 0) {		// init
		time0[index] = time_pr[index] = QuiskTimeSec();
		total[index] = 0;
		return;
	}
	if (time0[index] == 0)
		return;
	total[index] += count;
	tm = QuiskTimeSec();
	if (tm > time_pr[index] + 10.0) {	// time to print
		time_pr[index] = tm;
		QuiskPrintf("%s count %ld, time %.3lf, rate %.3lf\n", msg, total[index], tm - time0[index], total[index] / (tm - time0[index]));
                if (reset) {
                        total[index] = 0;
                        time0[index] = tm;
                }
	}
}

char * strMcpy(char * pDest, const char * pSrc, size_t sizeDest)
{  // replacement for strncpy()
	size_t sizeCopy;

	sizeCopy = strnlen(pSrc, sizeDest - 1);
	memcpy(pDest, pSrc, sizeCopy);
	pDest[sizeCopy] = 0;
	return pDest;
}

#ifdef MS_WINDOWS
#define QP_BUF_DELTA   256
PyObject * QuiskPrintf(char * format, ...)
{
        int length;
        PyObject * py_string;
        static int buf_size;            // size of buffer
        static int buf_strlen;          // number of chars in buffer
        static char * buffer = NULL;
        va_list args;

        EnterCriticalSection(&QuiskCriticalSection);
        if (buffer == NULL) {   // initialize
                buf_strlen = 0;
                buf_size = QP_BUF_DELTA * 2;
                buffer = malloc(buf_size);
        }
        if (format == NULL) {           // return the string
                py_string = PyUnicode_DecodeUTF8(buffer, buf_strlen, "replace");
                buf_strlen = 0;
                LeaveCriticalSection(&QuiskCriticalSection);
                return py_string;
        }
        if (buf_size - buf_strlen < QP_BUF_DELTA * 2) {     // max addition is QP_BUF_DELTA
                buf_size += QP_BUF_DELTA * 2;
                buffer = realloc(buffer, buf_size);
        }
        va_start(args, format);
        length = vsnprintf(buffer + buf_strlen, QP_BUF_DELTA, format, args);
        if (length < 0) {
                strcpy(buffer + buf_strlen, "Encoding error\n");
                buf_strlen = strlen(buffer);
        }
        else if (length > QP_BUF_DELTA - 1) {
                buf_strlen = strlen(buffer);
        }
        else {
                buf_strlen += length;
        }
        va_end(args);
        LeaveCriticalSection(&QuiskCriticalSection);
        return NULL;
}
#endif