Debian Codegraph @

tests/callback_hang_test.c

raw
1 /*
2 ** Copyright (c) 2002-2016, Erik de Castro Lopo <[email protected]>
3 ** All rights reserved.
4 **
5 ** This code is released under 2-clause BSD license. Please see the
6 ** file at : https://github.com/libsndfile/libsamplerate/blob/master/COPYING
7 */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #ifdef HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
18 #include <math.h>
19
20 #if HAVE_ALARM && HAVE_SIGNAL && HAVE_SIGALRM
21
22 #include <signal.h>
23
24 #include <samplerate.h>
25
26 #include "util.h"
27
28 #define SHORT_BUFFER_LEN 512
29 #define LONG_BUFFER_LEN (1 << 14)
30
31 typedef struct
32 { double ratio ;
33 int count ;
34 } SRC_PAIR ;
35
36 static void callback_hang_test (int converter) ;
37
38 static void alarm_handler (int number) ;
39 static long input_callback (void *cb_data, float **data) ;
40
41
42 int
43 main (void)
44 {
45 /* Set up SIGALRM handler. */
46 signal (SIGALRM, alarm_handler) ;
47
48 puts ("") ;
49 callback_hang_test (SRC_ZERO_ORDER_HOLD) ;
50 callback_hang_test (SRC_LINEAR) ;
51 #ifdef ENABLE_SINC_FAST_CONVERTER
52 callback_hang_test (SRC_SINC_FASTEST) ;
53 #endif
54 puts ("") ;
55
56 return 0 ;
57 } /* main */
58
59
60 static void
61 callback_hang_test (int converter)
62 { static float output [LONG_BUFFER_LEN] ;
63 static SRC_PAIR pairs [] =
64 {
65 { 1.2, 5 }, { 1.1, 1 }, { 1.0, 1 }, { 3.0, 1 }, { 2.0, 1 }, { 0.3, 1 },
66 { 1.2, 0 }, { 1.1, 10 }, { 1.0, 1 }
67 } ;
68
69
70 SRC_STATE *src_state ;
71
72 double src_ratio = 1.0 ;
73 int k, error ;
74
75 printf ("\tcallback_hang_test (%-28s) ....... ", src_get_name (converter)) ;
76 fflush (stdout) ;
77
78 /* Perform sample rate conversion. */
79 src_state = src_callback_new (input_callback, converter, 1, &error, NULL) ;
80 if (src_state == NULL)
81 { printf ("\n\nLine %d : src_callback_new () failed : %s\n\n", __LINE__, src_strerror (error)) ;
82 exit (1) ;
83 } ;
84
85 for (k = 0 ; k < ARRAY_LEN (pairs) ; k++)
86 { alarm (1) ;
87 src_ratio = pairs [k].ratio ;
88 src_callback_read (src_state, src_ratio, pairs [k].count, output) ;
89 } ;
90
91 src_state = src_delete (src_state) ;
92
93 alarm (0) ;
94 puts ("ok") ;
95
96 return ;
97 } /* callback_hang_test */
98
99 static void
100 alarm_handler (int number)
101 {
102 (void) number ;
103 printf ("\n\n Error : Hang inside src_callback_read() detected. Exiting!\n\n") ;
104 exit (1) ;
105 } /* alarm_handler */
106
107 static long
108 input_callback (void *cb_data, float **data)
109 {
110 static float buffer [20] ;
111
112 (void) cb_data ;
113 *data = buffer ;
114
115 return ARRAY_LEN (buffer) ;
116 } /* input_callback */
117
118 #else
119
120 int
121 main (void)
122 {
123 puts ("\tCan't run this test on this platform.") ;
124 return 0 ;
125 } /* main */
126
127 #endif