File: callback_hang_test.c

package info (click to toggle)
libsamplerate 0.2.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,444 kB
  • sloc: ansic: 371,377; sh: 121; makefile: 111
file content (127 lines) | stat: -rw-r--r-- 2,496 bytes parent folder | download | duplicates (3)
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
/*
** Copyright (c) 2002-2016, Erik de Castro Lopo <erikd@mega-nerd.com>
** All rights reserved.
**
** This code is released under 2-clause BSD license. Please see the
** file at : https://github.com/libsndfile/libsamplerate/blob/master/COPYING
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <math.h>

#if HAVE_ALARM && HAVE_SIGNAL && HAVE_SIGALRM

#include <signal.h>

#include <samplerate.h>

#include "util.h"

#define	SHORT_BUFFER_LEN	512
#define	LONG_BUFFER_LEN		(1 << 14)

typedef struct
{	double ratio ;
	int count ;
} SRC_PAIR ;

static void callback_hang_test (int converter) ;

static void alarm_handler (int number) ;
static long input_callback (void *cb_data, float **data) ;


int
main (void)
{
	/* Set up SIGALRM handler. */
	signal (SIGALRM, alarm_handler) ;

	puts ("") ;
	callback_hang_test (SRC_ZERO_ORDER_HOLD) ;
	callback_hang_test (SRC_LINEAR) ;
#ifdef ENABLE_SINC_FAST_CONVERTER
	callback_hang_test (SRC_SINC_FASTEST) ;
#endif
	puts ("") ;

	return 0 ;
} /* main */


static void
callback_hang_test (int converter)
{	static float output [LONG_BUFFER_LEN] ;
	static SRC_PAIR pairs [] =
	{
		{ 1.2, 5 }, { 1.1, 1 }, { 1.0, 1 }, { 3.0, 1 }, { 2.0, 1 }, { 0.3, 1 },
		{ 1.2, 0 }, { 1.1, 10 }, { 1.0, 1 }
		} ;


	SRC_STATE	*src_state ;

	double src_ratio = 1.0 ;
	int k, error ;

	printf ("\tcallback_hang_test  (%-28s) ....... ", src_get_name (converter)) ;
	fflush (stdout) ;

	/* Perform sample rate conversion. */
	src_state = src_callback_new (input_callback, converter, 1, &error, NULL) ;
	if (src_state == NULL)
	{	printf ("\n\nLine %d : src_callback_new () failed : %s\n\n", __LINE__, src_strerror (error)) ;
		exit (1) ;
		} ;

	for (k = 0 ; k < ARRAY_LEN (pairs) ; k++)
	{	alarm (1) ;
		src_ratio = pairs [k].ratio ;
		src_callback_read (src_state, src_ratio, pairs [k].count, output) ;
		} ;

	src_state = src_delete (src_state) ;

	alarm (0) ;
	puts ("ok") ;

	return ;
} /* callback_hang_test */

static void
alarm_handler (int number)
{
	(void) number ;
	printf ("\n\n    Error : Hang inside src_callback_read() detected. Exiting!\n\n") ;
	exit (1) ;
} /* alarm_handler */

static long
input_callback (void *cb_data, float **data)
{
	static float buffer [20] ;

	(void) cb_data ;
	*data = buffer ;

	return ARRAY_LEN (buffer) ;
} /* input_callback */

#else

int
main (void)
{
	puts ("\tCan't run this test on this platform.") ;
	return 0 ;
} /* main */

#endif