| 1 |
/* |
| 2 |
* sample_list_test.c - Tests sample edit lists and virtual sample store |
| 3 |
* |
| 4 |
* - Creates double stereo format test waveform with triangle wave in left |
| 5 |
* channel and sine wave in right channel |
| 6 |
* - Re-constructs both channels using 2 sample lists using various list |
| 7 |
* operations |
| 8 |
* - Creates virtual sample store using double stereo format with left |
| 9 |
* and right sample lists |
| 10 |
* - Duplicates virtual sample store to a double stereo format store |
| 11 |
* - Compares final duplicated store to original waveform and makes sure its |
| 12 |
* the same |
| 13 |
* |
| 14 |
* libInstPatch |
| 15 |
* Copyright (C) 1999-2014 Element Green <[email protected]> |
| 16 |
* |
| 17 |
* This program is free software; you can redistribute it and/or |
| 18 |
* modify it under the terms of the GNU Lesser General Public License |
| 19 |
* as published by the Free Software Foundation; version 2.1 |
| 20 |
* of the License only. |
| 21 |
* |
| 22 |
* This program is distributed in the hope that it will be useful, |
| 23 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 |
* GNU Lesser General Public License for more details. |
| 26 |
* |
| 27 |
* You should have received a copy of the GNU Lesser General Public License |
| 28 |
* along with this program; if not, see <https://www.gnu.org/licenses/>. |
| 29 |
*/ |
| 30 |
#include <libinstpatch/libinstpatch.h> |
| 31 |
#include <math.h> |
| 32 |
|
| 33 |
#define DEFAULT_AUDIO_SIZE (32 * 1024) /* default test waveform size in samples */ |
| 34 |
#define WAVEFORM_PERIOD 1684 /* something to exercise LSB bytes too */ |
| 35 |
#define MAX_DIFF_ALLOWED 0.0 /* maximum difference allowed */ |
| 36 |
|
| 37 |
#define WAVEFORM_QUARTER (WAVEFORM_PERIOD / 4) |
| 38 |
|
| 39 |
|
| 40 |
static int test_size = DEFAULT_AUDIO_SIZE; |
| 41 |
static gboolean verbose = FALSE; |
| 42 |
|
| 43 |
static GOptionEntry entries[] = |
| 44 |
{ |
| 45 |
{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL }, |
| 46 |
{ NULL } |
| 47 |
}; |
| 48 |
|
| 49 |
int main(int argc, char *argv[]) |
| 50 |
{ |
| 51 |
IpatchSampleStore *store, *vstore, *finstore; |
| 52 |
IpatchSampleList *rlist, *llist; |
| 53 |
IpatchSampleData *data, *vdata; |
| 54 |
double *dwave, *findata; |
| 55 |
guint periodpos; |
| 56 |
double d, maxdiff; |
| 57 |
int i, maxindex; |
| 58 |
GError *error = NULL; |
| 59 |
GOptionContext *context; |
| 60 |
int test_size_q; |
| 61 |
|
| 62 |
context = g_option_context_new("- test libInstPatch sample edit list functions"); |
| 63 |
g_option_context_add_main_entries(context, entries, "sample_list_test"); |
| 64 |
|
| 65 |
if(!g_option_context_parse(context, &argc, &argv, &error)) |
| 66 |
{ |
| 67 |
printf("Failed to parse command line arguments: %s\n", |
| 68 |
ipatch_gerror_message(error)); |
| 69 |
return 1; |
| 70 |
} |
| 71 |
|
| 72 |
g_option_context_free(context); |
| 73 |
|
| 74 |
test_size_q = test_size / 4; /* test size quarter */ |
| 75 |
|
| 76 |
ipatch_init(); |
| 77 |
|
| 78 |
/* allocate audio buffer (double format stereo) */ |
| 79 |
dwave = g_new(double, test_size * 2); /* ++ alloc */ |
| 80 |
|
| 81 |
/* generate triangle waveform for left channel and sine wave for right */ |
| 82 |
for(i = 0; i < test_size * 2; i += 2) |
| 83 |
{ |
| 84 |
periodpos = i % WAVEFORM_PERIOD; |
| 85 |
|
| 86 |
if(periodpos <= WAVEFORM_QUARTER) |
| 87 |
{ |
| 88 |
dwave[i] = periodpos / (double)WAVEFORM_QUARTER; |
| 89 |
} |
| 90 |
else if(periodpos <= WAVEFORM_QUARTER * 3) |
| 91 |
{ |
| 92 |
dwave[i] = -((periodpos - WAVEFORM_QUARTER) / (double)WAVEFORM_QUARTER - 1.0); |
| 93 |
} |
| 94 |
else |
| 95 |
{ |
| 96 |
dwave[i] = (periodpos - WAVEFORM_QUARTER * 3) / (double)WAVEFORM_QUARTER - 1.0; |
| 97 |
} |
| 98 |
|
| 99 |
dwave[i + 1] = sin((periodpos / (double)WAVEFORM_PERIOD) * G_PI * 2.0); |
| 100 |
} |
| 101 |
|
| 102 |
data = ipatch_sample_data_new(test_size); /* ++ ref */ |
| 103 |
|
| 104 |
/* ++ ref - create sample store object for original waveform */ |
| 105 |
store = ipatch_sample_store_new(IPATCH_TYPE_SAMPLE_STORE_RAM, data, |
| 106 |
"format", IPATCH_SAMPLE_DOUBLE |
| 107 |
| IPATCH_SAMPLE_STEREO |
| 108 |
| IPATCH_SAMPLE_ENDIAN_HOST, |
| 109 |
"location", dwave, |
| 110 |
"active", TRUE, |
| 111 |
NULL); |
| 112 |
|
| 113 |
rlist = ipatch_sample_list_new(); |
| 114 |
|
| 115 |
/* re-construct right channel in a very inefficient way ;) */ |
| 116 |
ipatch_sample_list_prepend(rlist, store, test_size_q * 2, test_size_q, |
| 117 |
IPATCH_SAMPLE_LIST_CHAN_RIGHT); |
| 118 |
ipatch_sample_list_prepend(rlist, store, 0, test_size_q, |
| 119 |
IPATCH_SAMPLE_LIST_CHAN_RIGHT); |
| 120 |
ipatch_sample_list_insert_index(rlist, 1, store, test_size_q, test_size_q, |
| 121 |
IPATCH_SAMPLE_LIST_CHAN_RIGHT); |
| 122 |
ipatch_sample_list_append(rlist, store, test_size_q * 3, test_size_q, |
| 123 |
IPATCH_SAMPLE_LIST_CHAN_RIGHT); |
| 124 |
/* cut a segment out which overlaps segments and then re-insert the cut seg */ |
| 125 |
ipatch_sample_list_cut(rlist, test_size_q + test_size_q / 2, test_size_q); |
| 126 |
ipatch_sample_list_insert(rlist, test_size_q + test_size_q / 2, |
| 127 |
store, test_size_q + test_size_q / 2, |
| 128 |
test_size_q, IPATCH_SAMPLE_LIST_CHAN_RIGHT); |
| 129 |
|
| 130 |
llist = ipatch_sample_list_new(); |
| 131 |
|
| 132 |
/* have fun with left channel too */ |
| 133 |
ipatch_sample_list_append(llist, store, 0, test_size, |
| 134 |
IPATCH_SAMPLE_LIST_CHAN_LEFT); |
| 135 |
ipatch_sample_list_cut(llist, test_size_q, test_size_q); |
| 136 |
ipatch_sample_list_insert(llist, test_size_q, store, test_size_q, |
| 137 |
test_size_q, IPATCH_SAMPLE_LIST_CHAN_LEFT); |
| 138 |
|
| 139 |
vdata = ipatch_sample_data_new(test_size); /* ++ ref */ |
| 140 |
|
| 141 |
/* ++ ref - create virtual store from left and right sample lists */ |
| 142 |
vstore = ipatch_sample_store_new(IPATCH_TYPE_SAMPLE_STORE_VIRTUAL, vdata, |
| 143 |
"format", IPATCH_SAMPLE_DOUBLE |
| 144 |
| IPATCH_SAMPLE_STEREO |
| 145 |
| IPATCH_SAMPLE_ENDIAN_HOST, |
| 146 |
NULL); |
| 147 |
ipatch_sample_store_virtual_set_list(vstore, 0, llist); |
| 148 |
ipatch_sample_store_virtual_set_list(vstore, 1, rlist); |
| 149 |
ipatch_sample_store_activate(vstore); |
| 150 |
|
| 151 |
/* ++ ref - Duplicate store to render final waveform */ |
| 152 |
finstore = ipatch_sample_store_duplicate(vstore, IPATCH_TYPE_SAMPLE_STORE_RAM, |
| 153 |
IPATCH_SAMPLE_DOUBLE |
| 154 |
| IPATCH_SAMPLE_STEREO |
| 155 |
| IPATCH_SAMPLE_ENDIAN_HOST, &error); |
| 156 |
|
| 157 |
if(!finstore) |
| 158 |
{ |
| 159 |
printf("Failed to create new duplicate sample store: %s", |
| 160 |
ipatch_gerror_message(error)); |
| 161 |
return 1; |
| 162 |
} |
| 163 |
|
| 164 |
findata = ipatch_sample_store_RAM_get_location(finstore); |
| 165 |
|
| 166 |
/* compare final waveform against original */ |
| 167 |
for(i = 0, maxdiff = 0.0, maxindex = 0; i < test_size * 2; i++) |
| 168 |
{ |
| 169 |
d = dwave[i] - findata[i]; |
| 170 |
|
| 171 |
if(d < 0.0) |
| 172 |
{ |
| 173 |
d = -d; |
| 174 |
} |
| 175 |
|
| 176 |
if(d > maxdiff) |
| 177 |
{ |
| 178 |
maxdiff = d; |
| 179 |
maxindex = i; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
/* free up objects and data (for memcheck) */ |
| 184 |
g_object_unref(data); |
| 185 |
g_object_unref(vdata); |
| 186 |
g_object_unref(store); |
| 187 |
g_object_unref(vstore); |
| 188 |
g_object_unref(finstore); |
| 189 |
g_free(dwave); |
| 190 |
|
| 191 |
if(maxdiff > MAX_DIFF_ALLOWED) |
| 192 |
{ |
| 193 |
printf("Sample list test failed: maxdiff=%0.16f index=%d\n", |
| 194 |
maxdiff, maxindex); |
| 195 |
return 1; |
| 196 |
} |
| 197 |
|
| 198 |
if(verbose) |
| 199 |
printf("Sample list test passed: maxdiff=%0.16f index=%d\n", |
| 200 |
maxdiff, maxindex); |
| 201 |
else |
| 202 |
{ |
| 203 |
printf("Sample list and virtual sample store test passed\n"); |
| 204 |
} |
| 205 |
|
| 206 |
return 0; |
| 207 |
} |