SAF
Loading...
Searching...
No Matches
test__hrir_module.c
Go to the documentation of this file.
1/*
2 * Copyright 2020-2021 Leo McCormack
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
13 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
15 */
16
25#include "saf_test.h"
26
28 float* hrirs_out, *hrirs_tmp;
29 int i, j, target_fs, hrirs_out_len, hrirs_tmp_len, max_ind;
30
31 /* The Speex resampler has generally quite a good compromise between quality and speed.
32 * This tolerance is quite high, but ultimately, it's how it sounds that matters.
33 * If SAF_USE_INTEL_IPP is defined, then the Intel IPP resampler is employed instead */
34 const float acceptedTolerance = 0.08f;
35
36 /* Test 1 - passing a unit impulse through, and :qasserting the peak is where it should be */
37 float* ir;
38 ir = calloc1d(NUM_EARS * 256, sizeof(float));
39 ir[10] = 1.0f;
40 ir[256+10] = 1.0f;
41 hrirs_out = NULL;
42 for(i=0; i<100; i++){
43 resampleHRIRs((float*)ir, 1, 256, 48000, 48000, 0, &hrirs_out, &hrirs_out_len); /* 1x samplerate */
44 utility_simaxv(hrirs_out, hrirs_out_len, &max_ind);
45 TEST_ASSERT_TRUE(max_ind==10);
46 utility_simaxv(hrirs_out+hrirs_out_len, hrirs_out_len, &max_ind);
47 TEST_ASSERT_TRUE(max_ind==10);
48 free(hrirs_out); hrirs_out = NULL;
49 resampleHRIRs((float*)ir, 1, 256, 48000, 96000, 0, &hrirs_out, &hrirs_out_len); /* 2x samplerate */
50 utility_simaxv(hrirs_out, hrirs_out_len, &max_ind);
51 TEST_ASSERT_TRUE(max_ind==20);
52 utility_simaxv(hrirs_out+hrirs_out_len, hrirs_out_len, &max_ind);
53 TEST_ASSERT_TRUE(max_ind==20);
54 free(hrirs_out); hrirs_out = NULL;
55 resampleHRIRs((float*)ir, 1, 256, 48000, 24000, 0, &hrirs_out, &hrirs_out_len); /* 0.5x samplerate */
56 utility_simaxv(hrirs_out, hrirs_out_len, &max_ind);
57 TEST_ASSERT_TRUE(max_ind==5);
58 utility_simaxv(hrirs_out+hrirs_out_len, hrirs_out_len, &max_ind);
59 TEST_ASSERT_TRUE(max_ind==5);
60 free(hrirs_out); hrirs_out = NULL;
61 }
62 free(ir);
63
64 /* Test 2 - converting 48e3 to 48e3 (i.e., no actual resampling, but still passing through the filter) */
65 target_fs = 48000;
66 hrirs_out = NULL;
68 target_fs, 0 /*do not zero pad*/, &hrirs_out, &hrirs_out_len);
69 for(i=0; i<__default_N_hrir_dirs*NUM_EARS; i++) /* Loop over IRs */
70 for(j=0; j<SAF_MIN(__default_hrir_len,hrirs_out_len); j++) /* Loop over Samples */
71 TEST_ASSERT_TRUE(fabsf(((float*)__default_hrirs)[i*__default_hrir_len+j] - hrirs_out[i*hrirs_out_len+j]) <= acceptedTolerance);
72 TEST_ASSERT_TRUE(__default_hrir_len==hrirs_out_len);
73 free(hrirs_out);
74
75 /* Test 2 - converting 48e3 to 96e3 and then back to 48e3 */
76 target_fs = 96000;
77 hrirs_tmp = NULL;
79 target_fs, 0 /*do not zero pad*/, &hrirs_tmp, &hrirs_tmp_len);
80 target_fs = 48000;
81 hrirs_out = NULL;
82 resampleHRIRs(hrirs_tmp, __default_N_hrir_dirs, hrirs_tmp_len, 96000,
83 target_fs, 0 /*do not zero pad*/, &hrirs_out, &hrirs_out_len);
84 for(i=0; i<__default_N_hrir_dirs*NUM_EARS; i++) /* Loop over IRs */
85 for(j=0; j<SAF_MIN(__default_hrir_len,hrirs_out_len); j++) /* Loop over Samples */
86 TEST_ASSERT_TRUE(fabsf(((float*)__default_hrirs)[i*__default_hrir_len+j] - hrirs_out[i*hrirs_out_len+j]) <= acceptedTolerance);
87 TEST_ASSERT_TRUE(__default_hrir_len==hrirs_out_len);
88 free(hrirs_tmp);
89 free(hrirs_out);
90}
const int __default_N_hrir_dirs
The number of directions/measurements in the default HRIR dataset.
const float __default_hrirs[836][2][256]
The default HRIR data for SAF.
const int __default_hrir_len
The length of the filters, in samples, for the default HRIR dataset.
const int __default_hrir_fs
The samplerate used to measure the default HRIR filters
void resampleHRIRs(float *hrirs_in, int hrirs_N_dirs, int hrirs_in_len, int hrirs_in_fs, int hrirs_out_fs, int padToNextPow2, float **hrirs_out, int *hrirs_out_len)
Resamples a set of HRIRs from its original samplerate to a new samplerate.
Definition saf_hrir.c:366
#define NUM_EARS
2 (true for most humans)
#define SAF_MIN(a, b)
Returns the minimum of the two values.
void utility_simaxv(const float *a, const int len, int *index)
Single-precision, index of maximum absolute value in a vector, i.e.
void * calloc1d(size_t dim1, size_t data_size)
1-D calloc (same as calloc, but with error checking)
Definition md_malloc.c:69
Unit test program for the Spatial_Audio_Framework.
void test__resampleHRIRs(void)
Testing that resampleHRIRs() is resampling adequately.