5
|
1 /*
|
|
2 * SpanDSP - a series of DSP components for telephony
|
|
3 *
|
|
4 * test_utils.c - Utility routines for module tests.
|
|
5 *
|
|
6 * Written by Steve Underwood <steveu@coppice.org>
|
|
7 *
|
|
8 * Copyright (C) 2006 Steve Underwood
|
|
9 *
|
|
10 * All rights reserved.
|
|
11 *
|
|
12 * This program is free software; you can redistribute it and/or modify
|
|
13 * it under the terms of the GNU General Public License version 2, as
|
|
14 * published by the Free Software Foundation.
|
|
15 *
|
|
16 * This program is distributed in the hope that it will be useful,
|
|
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
19 * GNU General Public License for more details.
|
|
20 *
|
|
21 * You should have received a copy of the GNU General Public License
|
|
22 * along with this program; if not, write to the Free Software
|
|
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
24 *
|
|
25 * $Id: test_utils.c,v 1.7 2006/11/19 14:07:27 steveu Exp $
|
|
26 */
|
|
27
|
|
28 /*! \file */
|
|
29
|
|
30 #ifdef HAVE_CONFIG_H
|
|
31 #include "config.h"
|
|
32 #endif
|
|
33
|
|
34 #include <stdlib.h>
|
|
35 #include <inttypes.h>
|
|
36 #include <string.h>
|
|
37 #include <stdio.h>
|
|
38 #if defined(HAVE_TGMATH_H)
|
|
39 #include <tgmath.h>
|
|
40 #endif
|
|
41 #if defined(HAVE_MATH_H)
|
|
42 #include <math.h>
|
|
43 #endif
|
|
44 #include <time.h>
|
|
45 #include <fcntl.h>
|
|
46 #include <audiofile.h>
|
|
47 #include <tiffio.h>
|
|
48
|
|
49 #include "spandsp.h"
|
|
50 #include "test_utils.h"
|
|
51
|
|
52 struct codec_munge_state_s
|
|
53 {
|
|
54 int munging_codec;
|
|
55 g726_state_t g726_enc_state;
|
|
56 g726_state_t g726_dec_state;
|
|
57 };
|
|
58
|
|
59 codec_munge_state_t *codec_munge_init(int codec)
|
|
60 {
|
|
61 codec_munge_state_t *s;
|
|
62
|
|
63 if ((s = (codec_munge_state_t *) malloc(sizeof(*s))))
|
|
64 {
|
|
65 switch (codec)
|
|
66 {
|
|
67 case MUNGE_CODEC_G726_40K:
|
|
68 g726_init(&s->g726_enc_state, 40000, G726_ENCODING_LINEAR, G726_PACKING_NONE);
|
|
69 g726_init(&s->g726_dec_state, 40000, G726_ENCODING_LINEAR, G726_PACKING_NONE);
|
|
70 s->munging_codec = MUNGE_CODEC_G726_32K;
|
|
71 break;
|
|
72 case MUNGE_CODEC_G726_32K:
|
|
73 g726_init(&s->g726_enc_state, 32000, G726_ENCODING_LINEAR, G726_PACKING_NONE);
|
|
74 g726_init(&s->g726_dec_state, 32000, G726_ENCODING_LINEAR, G726_PACKING_NONE);
|
|
75 s->munging_codec = MUNGE_CODEC_G726_32K;
|
|
76 break;
|
|
77 case MUNGE_CODEC_G726_24K:
|
|
78 g726_init(&s->g726_enc_state, 24000, G726_ENCODING_LINEAR, G726_PACKING_NONE);
|
|
79 g726_init(&s->g726_dec_state, 24000, G726_ENCODING_LINEAR, G726_PACKING_NONE);
|
|
80 s->munging_codec = MUNGE_CODEC_G726_32K;
|
|
81 break;
|
|
82 case MUNGE_CODEC_G726_16K:
|
|
83 g726_init(&s->g726_enc_state, 16000, G726_ENCODING_LINEAR, G726_PACKING_NONE);
|
|
84 g726_init(&s->g726_dec_state, 16000, G726_ENCODING_LINEAR, G726_PACKING_NONE);
|
|
85 s->munging_codec = MUNGE_CODEC_G726_32K;
|
|
86 break;
|
|
87 default:
|
|
88 s->munging_codec = codec;
|
|
89 break;
|
|
90 }
|
|
91 }
|
|
92 return s;
|
|
93 }
|
|
94 /*- End of function --------------------------------------------------------*/
|
|
95
|
|
96 void codec_munge(codec_munge_state_t *s, int16_t amp[], int len)
|
|
97 {
|
|
98 uint8_t law;
|
|
99 uint8_t adpcmdata[160];
|
|
100 int i;
|
|
101 int adpcm;
|
|
102 int x;
|
|
103
|
|
104 switch (s->munging_codec)
|
|
105 {
|
|
106 case MUNGE_CODEC_NONE:
|
|
107 /* Do nothing */
|
|
108 break;
|
|
109 case MUNGE_CODEC_ALAW:
|
|
110 for (i = 0; i < len; i++)
|
|
111 {
|
|
112 law = linear_to_alaw(amp[i]);
|
|
113 amp[i] = alaw_to_linear(law);
|
|
114 }
|
|
115 break;
|
|
116 case MUNGE_CODEC_ULAW:
|
|
117 for (i = 0; i < len; i++)
|
|
118 {
|
|
119 law = linear_to_ulaw(amp[i]);
|
|
120 amp[i] = ulaw_to_linear(law);
|
|
121 }
|
|
122 break;
|
|
123 case MUNGE_CODEC_G726_32K:
|
|
124 /* This could actually be any of the G.726 rates */
|
|
125 for (i = 0; i < len; i += x)
|
|
126 {
|
|
127 x = (len - i >= 160) ? 160 : (len - i);
|
|
128 adpcm = g726_encode(&s->g726_enc_state, adpcmdata, amp + i, x);
|
|
129 g726_decode(&s->g726_dec_state, amp + i, adpcmdata, adpcm);
|
|
130 }
|
|
131 break;
|
|
132 }
|
|
133 }
|
|
134 /*- End of function --------------------------------------------------------*/
|
|
135 /*- End of file ------------------------------------------------------------*/
|