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 <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <math.h> |
| 23 | |
| 24 | #include <samplerate.h> |
| 25 | |
| 26 | #include "util.h" |
| 27 | |
| 28 | #define BUFFER_LEN 2048 |
| 29 | |
| 30 | static void simple_test (int converter, double ratio) ; |
| 31 | |
| 32 | int |
| 33 | main (void) |
| 34 | { static double src_ratios [] = |
| 35 | { 1.0001, 0.099, 0.1, 0.33333333, 0.789, 1.9, 3.1, 9.9 |
| 36 | } ; |
| 37 | |
| 38 | int k ; |
| 39 | |
| 40 | puts ("") ; |
| 41 | |
| 42 | puts (" Zero Order Hold interpolator :") ; |
| 43 | for (k = 0 ; k < ARRAY_LEN (src_ratios) ; k++) |
| 44 | simple_test (SRC_ZERO_ORDER_HOLD, src_ratios [k]) ; |
| 45 | |
| 46 | puts (" Linear interpolator :") ; |
| 47 | for (k = 0 ; k < ARRAY_LEN (src_ratios) ; k++) |
| 48 | simple_test (SRC_LINEAR, src_ratios [k]) ; |
| 49 | |
| 50 | puts (" Sinc interpolator :") ; |
| 51 | for (k = 0 ; k < ARRAY_LEN (src_ratios) ; k++) |
| 52 | simple_test (SRC_SINC_FASTEST, src_ratios [k]) ; |
| 53 | |
| 54 | puts ("") ; |
| 55 | |
| 56 | return 0 ; |
| 57 | } /* main */ |
| 58 | |
| 59 | static void |
| 60 | simple_test (int converter, double src_ratio) |
| 61 | { static float input [BUFFER_LEN], output [BUFFER_LEN] ; |
| 62 | |
| 63 | SRC_DATA src_data ; |
| 64 | |
| 65 | int input_len, output_len, error, terminate ; |
| 66 | |
| 67 | printf ("\tsimple_test (SRC ratio = %6.4f) ........... ", src_ratio) ; |
| 68 | fflush (stdout) ; |
| 69 | |
| 70 | /* Calculate maximun input and output lengths. */ |
| 71 | if (src_ratio >= 1.0) |
| 72 | { output_len = BUFFER_LEN ; |
| 73 | input_len = (int) floor (BUFFER_LEN / src_ratio) ; |
| 74 | } |
| 75 | else |
| 76 | { input_len = BUFFER_LEN ; |
| 77 | output_len = (int) floor (BUFFER_LEN * src_ratio) ; |
| 78 | } ; |
| 79 | |
| 80 | /* Reduce input_len by 10 so output is longer than necessary. */ |
| 81 | input_len -= 10 ; |
| 82 | |
| 83 | if (output_len > BUFFER_LEN) |
| 84 | { printf ("\n\nLine %d : output_len > BUFFER_LEN\n\n", __LINE__) ; |
| 85 | exit (1) ; |
| 86 | } ; |
| 87 | |
| 88 | memset (&src_data, 0, sizeof (src_data)) ; |
| 89 | |
| 90 | src_data.data_in = input ; |
| 91 | src_data.input_frames = input_len ; |
| 92 | |
| 93 | src_data.src_ratio = src_ratio ; |
| 94 | |
| 95 | src_data.data_out = output ; |
| 96 | src_data.output_frames = BUFFER_LEN ; |
| 97 | |
| 98 | if ((error = src_simple (&src_data, converter, 1))) |
| 99 | { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; |
| 100 | exit (1) ; |
| 101 | } ; |
| 102 | |
| 103 | terminate = (int) ceil ((src_ratio >= 1.0) ? src_ratio : 1.0 / src_ratio) ; |
| 104 | |
| 105 | if (fabs (src_data.output_frames_gen - src_ratio * input_len) > 2 * terminate) |
| 106 | { printf ("\n\nLine %d : bad output data length %ld should be %d.\n", __LINE__, |
| 107 | src_data.output_frames_gen, (int) floor (src_ratio * input_len)) ; |
| 108 | printf ("\tsrc_ratio : %.4f\n", src_ratio) ; |
| 109 | printf ("\tinput_len : %d\n\toutput_len : %d\n\n", input_len, output_len) ; |
| 110 | exit (1) ; |
| 111 | } ; |
| 112 | |
| 113 | puts ("ok") ; |
| 114 | |
| 115 | return ; |
| 116 | } /* simple_test */ |
| 117 | |