Mercurial > hg > audiostuff
comparison spandsp-0.0.6pre17/tests/oki_adpcm_tests.c @ 4:26cd8f1ef0b1
import spandsp-0.0.6pre17
| author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
|---|---|
| date | Fri, 25 Jun 2010 15:50:58 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 3:c6c5a16ce2f2 | 4:26cd8f1ef0b1 |
|---|---|
| 1 /* | |
| 2 * SpanDSP - a series of DSP components for telephony | |
| 3 * | |
| 4 * oki_adpcm_tests.c - Test the Oki (Dialogic) ADPCM encode and decode | |
| 5 * software at 24kbps and 32kbps. | |
| 6 * | |
| 7 * Written by Steve Underwood <steveu@coppice.org> | |
| 8 * | |
| 9 * Copyright (C) 2004 Steve Underwood | |
| 10 * | |
| 11 * All rights reserved. | |
| 12 * | |
| 13 * This program is free software; you can redistribute it and/or modify | |
| 14 * it under the terms of the GNU General Public License version 2, as | |
| 15 * published by the Free Software Foundation. | |
| 16 * | |
| 17 * This program is distributed in the hope that it will be useful, | |
| 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 20 * GNU General Public License for more details. | |
| 21 * | |
| 22 * You should have received a copy of the GNU General Public License | |
| 23 * along with this program; if not, write to the Free Software | |
| 24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
| 25 * | |
| 26 * $Id: oki_adpcm_tests.c,v 1.37 2009/05/30 15:23:14 steveu Exp $ | |
| 27 */ | |
| 28 | |
| 29 /*! \file */ | |
| 30 | |
| 31 /*! \page oki_adpcm_tests_page OKI (Dialogic) ADPCM tests | |
| 32 \section oki_adpcm_tests_page_sec_1 What does it do? | |
| 33 To perform a general audio quality test, oki_adpcm_tests should be run. The test file | |
| 34 ../test-data/local/short_nb_voice.wav will be compressed to the specified bit rate, | |
| 35 decompressed, and the resulting audio stored in post_oki_adpcm.wav. A simple SNR test | |
| 36 is automatically performed. Listening tests may be used for a more detailed evaluation | |
| 37 of the degradation in quality caused by the compression. Both 32k bps and 24k bps | |
| 38 compression may be tested. | |
| 39 | |
| 40 \section oki_adpcm_tests_page_sec_2 How is it used? | |
| 41 */ | |
| 42 | |
| 43 #if defined(HAVE_CONFIG_H) | |
| 44 #include "config.h" | |
| 45 #endif | |
| 46 | |
| 47 #include <stdlib.h> | |
| 48 #include <stdio.h> | |
| 49 #include <fcntl.h> | |
| 50 #include <unistd.h> | |
| 51 #include <string.h> | |
| 52 #include <time.h> | |
| 53 #include <sndfile.h> | |
| 54 | |
| 55 //#if defined(WITH_SPANDSP_INTERNALS) | |
| 56 #define SPANDSP_EXPOSE_INTERNAL_STRUCTURES | |
| 57 //#endif | |
| 58 | |
| 59 #include "spandsp.h" | |
| 60 #include "spandsp-sim.h" | |
| 61 | |
| 62 #define IN_FILE_NAME "../test-data/local/short_nb_voice.wav" | |
| 63 #define OUT_FILE_NAME "post_oki_adpcm.wav" | |
| 64 | |
| 65 #define HIST_LEN 1000 | |
| 66 | |
| 67 int main(int argc, char *argv[]) | |
| 68 { | |
| 69 int i; | |
| 70 SNDFILE *inhandle; | |
| 71 SNDFILE *outhandle; | |
| 72 int frames; | |
| 73 int dec_frames; | |
| 74 int outframes; | |
| 75 int oki_bytes; | |
| 76 int bit_rate; | |
| 77 double pre_energy; | |
| 78 double post_energy; | |
| 79 double diff_energy; | |
| 80 int16_t pre_amp[HIST_LEN]; | |
| 81 int16_t post_amp[HIST_LEN]; | |
| 82 uint8_t oki_data[HIST_LEN]; | |
| 83 int16_t history[HIST_LEN]; | |
| 84 int hist_in; | |
| 85 int hist_out; | |
| 86 oki_adpcm_state_t *oki_enc_state; | |
| 87 oki_adpcm_state_t *oki_dec_state; | |
| 88 int xx; | |
| 89 int total_pre_samples; | |
| 90 int total_compressed_bytes; | |
| 91 int total_post_samples; | |
| 92 const char *in_file_name; | |
| 93 int log_encoded_data; | |
| 94 int opt; | |
| 95 | |
| 96 bit_rate = 32000; | |
| 97 in_file_name = IN_FILE_NAME; | |
| 98 log_encoded_data = FALSE; | |
| 99 while ((opt = getopt(argc, argv, "2i:l")) != -1) | |
| 100 { | |
| 101 switch (opt) | |
| 102 { | |
| 103 case '2': | |
| 104 bit_rate = 24000; | |
| 105 break; | |
| 106 case 'i': | |
| 107 in_file_name = optarg; | |
| 108 break; | |
| 109 case 'l': | |
| 110 log_encoded_data = TRUE; | |
| 111 break; | |
| 112 default: | |
| 113 //usage(); | |
| 114 exit(2); | |
| 115 break; | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 if ((inhandle = sf_open_telephony_read(in_file_name, 1)) == NULL) | |
| 120 { | |
| 121 fprintf(stderr, " Cannot open audio file '%s'\n", in_file_name); | |
| 122 exit(2); | |
| 123 } | |
| 124 if ((outhandle = sf_open_telephony_write(OUT_FILE_NAME, 1)) == NULL) | |
| 125 { | |
| 126 fprintf(stderr, " Cannot create audio file '%s'\n", OUT_FILE_NAME); | |
| 127 exit(2); | |
| 128 } | |
| 129 | |
| 130 if ((oki_enc_state = oki_adpcm_init(NULL, bit_rate)) == NULL) | |
| 131 { | |
| 132 fprintf(stderr, " Cannot create encoder\n"); | |
| 133 exit(2); | |
| 134 } | |
| 135 | |
| 136 if ((oki_dec_state = oki_adpcm_init(NULL, bit_rate)) == NULL) | |
| 137 { | |
| 138 fprintf(stderr, " Cannot create decoder\n"); | |
| 139 exit(2); | |
| 140 } | |
| 141 | |
| 142 hist_in = 0; | |
| 143 if (bit_rate == 32000) | |
| 144 hist_out = 0; | |
| 145 else | |
| 146 hist_out = HIST_LEN - 27; | |
| 147 memset(history, 0, sizeof(history)); | |
| 148 pre_energy = 0.0; | |
| 149 post_energy = 0.0; | |
| 150 diff_energy = 0.0; | |
| 151 total_pre_samples = 0; | |
| 152 total_compressed_bytes = 0; | |
| 153 total_post_samples = 0; | |
| 154 while ((frames = sf_readf_short(inhandle, pre_amp, 159))) | |
| 155 { | |
| 156 total_pre_samples += frames; | |
| 157 oki_bytes = oki_adpcm_encode(oki_enc_state, oki_data, pre_amp, frames); | |
| 158 if (log_encoded_data) | |
| 159 write(1, oki_data, oki_bytes); | |
| 160 total_compressed_bytes += oki_bytes; | |
| 161 dec_frames = oki_adpcm_decode(oki_dec_state, post_amp, oki_data, oki_bytes); | |
| 162 total_post_samples += dec_frames; | |
| 163 for (i = 0; i < frames; i++) | |
| 164 { | |
| 165 history[hist_in++] = pre_amp[i]; | |
| 166 if (hist_in >= HIST_LEN) | |
| 167 hist_in = 0; | |
| 168 pre_energy += (double) pre_amp[i] * (double) pre_amp[i]; | |
| 169 } | |
| 170 for (i = 0; i < dec_frames; i++) | |
| 171 { | |
| 172 post_energy += (double) post_amp[i] * (double) post_amp[i]; | |
| 173 xx = post_amp[i] - history[hist_out++]; | |
| 174 if (hist_out >= HIST_LEN) | |
| 175 hist_out = 0; | |
| 176 diff_energy += (double) xx * (double) xx; | |
| 177 //post_amp[i] = xx; | |
| 178 } | |
| 179 outframes = sf_writef_short(outhandle, post_amp, dec_frames); | |
| 180 } | |
| 181 if (sf_close(inhandle) != 0) | |
| 182 { | |
| 183 fprintf(stderr, " Cannot close audio file '%s'\n", in_file_name); | |
| 184 exit(2); | |
| 185 } | |
| 186 if (sf_close(outhandle) != 0) | |
| 187 { | |
| 188 fprintf(stderr, " Cannot close audio file '%s'\n", OUT_FILE_NAME); | |
| 189 exit(2); | |
| 190 } | |
| 191 oki_adpcm_release(oki_enc_state); | |
| 192 oki_adpcm_release(oki_dec_state); | |
| 193 | |
| 194 printf("Pre samples: %d\n", total_pre_samples); | |
| 195 printf("Compressed bytes: %d\n", total_compressed_bytes); | |
| 196 printf("Post samples: %d\n", total_post_samples); | |
| 197 | |
| 198 printf("Output energy is %f%% of input energy.\n", 100.0*post_energy/pre_energy); | |
| 199 printf("Residual energy is %f%% of the total.\n", 100.0*diff_energy/post_energy); | |
| 200 if (bit_rate == 32000) | |
| 201 { | |
| 202 if (fabs(1.0 - post_energy/pre_energy) > 0.05 | |
| 203 || | |
| 204 fabs(diff_energy/post_energy) > 0.03) | |
| 205 { | |
| 206 printf("Tests failed.\n"); | |
| 207 exit(2); | |
| 208 } | |
| 209 } | |
| 210 else | |
| 211 { | |
| 212 if (fabs(1.0 - post_energy/pre_energy) > 0.20 | |
| 213 || | |
| 214 fabs(diff_energy/post_energy) > 0.10) | |
| 215 { | |
| 216 printf("Tests failed.\n"); | |
| 217 exit(2); | |
| 218 } | |
| 219 } | |
| 220 | |
| 221 printf("Tests passed.\n"); | |
| 222 return 0; | |
| 223 } | |
| 224 /*- End of function --------------------------------------------------------*/ | |
| 225 /*- End of file ------------------------------------------------------------*/ |
