Emeric Vigier | eebea67 | 2012-08-06 17:36:30 -0400 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright (C) 2002-2011 Erik de Castro Lopo <erikd@mega-nerd.com> |
| 3 | ** |
| 4 | ** This program is free software; you can redistribute it and/or modify |
| 5 | ** it under the terms of the GNU General Public License as published by |
| 6 | ** the Free Software Foundation; either version 2 of the License, or |
| 7 | ** (at your option) any later version. |
| 8 | ** |
| 9 | ** This program is distributed in the hope that it will be useful, |
| 10 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | ** GNU General Public License for more details. |
| 13 | ** |
| 14 | ** You should have received a copy of the GNU General Public License |
| 15 | ** along with this program; if not, write to the Free Software |
| 16 | ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #include "config.h" |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <unistd.h> |
| 24 | #include <math.h> |
| 25 | |
| 26 | #if HAVE_ALARM && HAVE_SIGNAL && HAVE_SIGALRM |
| 27 | |
| 28 | #include <signal.h> |
| 29 | |
| 30 | #include <samplerate.h> |
| 31 | |
| 32 | #include "util.h" |
| 33 | |
| 34 | #define SHORT_BUFFER_LEN 512 |
| 35 | #define LONG_BUFFER_LEN (1 << 14) |
| 36 | |
| 37 | typedef struct |
| 38 | { double ratio ; |
| 39 | int count ; |
| 40 | } SRC_PAIR ; |
| 41 | |
| 42 | static void callback_hang_test (int converter) ; |
| 43 | |
| 44 | static void alarm_handler (int number) ; |
| 45 | static long input_callback (void *cb_data, float **data) ; |
| 46 | |
| 47 | |
| 48 | int |
| 49 | main (void) |
| 50 | { |
| 51 | /* Set up SIGALRM handler. */ |
| 52 | signal (SIGALRM, alarm_handler) ; |
| 53 | |
| 54 | puts ("") ; |
| 55 | callback_hang_test (SRC_ZERO_ORDER_HOLD) ; |
| 56 | callback_hang_test (SRC_LINEAR) ; |
| 57 | callback_hang_test (SRC_SINC_FASTEST) ; |
| 58 | puts ("") ; |
| 59 | |
| 60 | return 0 ; |
| 61 | } /* main */ |
| 62 | |
| 63 | |
| 64 | static void |
| 65 | callback_hang_test (int converter) |
| 66 | { static float output [LONG_BUFFER_LEN] ; |
| 67 | static SRC_PAIR pairs [] = |
| 68 | { |
| 69 | { 1.2, 5 }, { 1.1, 1 }, { 1.0, 1 }, { 3.0, 1 }, { 2.0, 1 }, { 0.3, 1 }, |
| 70 | { 1.2, 0 }, { 1.1, 10 }, { 1.0, 1 } |
| 71 | } ; |
| 72 | |
| 73 | |
| 74 | SRC_STATE *src_state ; |
| 75 | |
| 76 | double src_ratio = 1.0 ; |
| 77 | int k, error ; |
| 78 | |
| 79 | printf ("\tcallback_hang_test (%-28s) ....... ", src_get_name (converter)) ; |
| 80 | fflush (stdout) ; |
| 81 | |
| 82 | /* Perform sample rate conversion. */ |
| 83 | src_state = src_callback_new (input_callback, converter, 1, &error, NULL) ; |
| 84 | if (src_state == NULL) |
| 85 | { printf ("\n\nLine %d : src_callback_new () failed : %s\n\n", __LINE__, src_strerror (error)) ; |
| 86 | exit (1) ; |
| 87 | } ; |
| 88 | |
| 89 | for (k = 0 ; k < ARRAY_LEN (pairs) ; k++) |
| 90 | { alarm (1) ; |
| 91 | src_ratio = pairs [k].ratio ; |
| 92 | src_callback_read (src_state, src_ratio, pairs [k].count, output) ; |
| 93 | } ; |
| 94 | |
| 95 | src_state = src_delete (src_state) ; |
| 96 | |
| 97 | alarm (0) ; |
| 98 | puts ("ok") ; |
| 99 | |
| 100 | return ; |
| 101 | } /* callback_hang_test */ |
| 102 | |
| 103 | static void |
| 104 | alarm_handler (int number) |
| 105 | { |
| 106 | (void) number ; |
| 107 | printf ("\n\n Error : Hang inside src_callback_read() detected. Exiting!\n\n") ; |
| 108 | exit (1) ; |
| 109 | } /* alarm_handler */ |
| 110 | |
| 111 | static long |
| 112 | input_callback (void *cb_data, float **data) |
| 113 | { |
| 114 | static float buffer [20] ; |
| 115 | |
| 116 | (void) cb_data ; |
| 117 | *data = buffer ; |
| 118 | |
| 119 | return ARRAY_LEN (buffer) ; |
| 120 | } /* input_callback */ |
| 121 | |
| 122 | #else |
| 123 | |
| 124 | int |
| 125 | main (void) |
| 126 | { |
| 127 | puts ("\tCan't run this test on this platform.") ; |
| 128 | return 0 ; |
| 129 | } /* main */ |
| 130 | |
| 131 | #endif |