Debian Codegraph @

3rdparty/iaxclient/lib/libspeex/misc.c

raw symbols SCIP
1 /* Copyright (C) 2002 Jean-Marc Valin
2 File: mics.c
3 Various utility routines for Speex
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15
16 - Neither the name of the Xiph.org Foundation nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include "misc.h"
38 #include <stdlib.h>
39 #include <string.h>
40 #include <stdio.h>
41
42 #ifndef RELEASE
43 void print_vec(float *vec, int len, char *name)
44 {
45 int i;
46 printf ("%s ", name);
47 for (i=0;i<len;i++)
48 printf (" %f", vec[i]);
49 printf ("\n");
50 }
51 #endif
52
53 #ifdef FIXED_DEBUG
54 long long spx_mips=0;
55 #endif
56
57
58 spx_uint32_t be_int(spx_uint32_t i)
59 {
60 spx_uint32_t ret=i;
61 #ifndef WORDS_BIGENDIAN
62 ret = i>>24;
63 ret += (i>>8)&0x0000ff00;
64 ret += (i<<8)&0x00ff0000;
65 ret += (i<<24);
66 #endif
67 return ret;
68 }
69
70 spx_uint32_t le_int(spx_uint32_t i)
71 {
72 spx_uint32_t ret=i;
73 #ifdef WORDS_BIGENDIAN
74 ret = i>>24;
75 ret += (i>>8)&0x0000ff00;
76 ret += (i<<8)&0x00ff0000;
77 ret += (i<<24);
78 #endif
79 return ret;
80 }
81
82 #if BYTES_PER_CHAR == 2
83 void speex_memcpy_bytes(char *dst, char *src, int nbytes)
84 {
85 int i;
86 int nchars = nbytes/BYTES_PER_CHAR;
87 for (i=0;i<nchars;i++)
88 dst[i]=src[i];
89 if (nbytes & 1) {
90 /* copy in the last byte */
91 int last_i = nchars;
92 char last_dst_char = dst[last_i];
93 char last_src_char = src[last_i];
94 last_dst_char &= 0xff00;
95 last_dst_char |= (last_src_char & 0x00ff);
96 dst[last_i] = last_dst_char;
97 }
98 }
99 void speex_memset_bytes(char *dst, char c, int nbytes)
100 {
101 int i;
102 spx_int16_t cc = ((c << 8) | c);
103 int nchars = nbytes/BYTES_PER_CHAR;
104 for (i=0;i<nchars;i++)
105 dst[i]=cc;
106 if (nbytes & 1) {
107 /* copy in the last byte */
108 int last_i = nchars;
109 char last_dst_char = dst[last_i];
110 last_dst_char &= 0xff00;
111 last_dst_char |= (c & 0x00ff);
112 dst[last_i] = last_dst_char;
113 }
114 }
115 #else
116 void speex_memcpy_bytes(char *dst, char *src, int nbytes)
117 {
118 memcpy(dst, src, nbytes);
119 }
120 void speex_memset_bytes(char *dst, char src, int nbytes)
121 {
122 memset(dst, src, nbytes);
123 }
124 #endif
125
126 void *speex_alloc (int size)
127 {
128 return calloc(size,1);
129 }
130
131 void *speex_realloc (void *ptr, int size)
132 {
133 return realloc(ptr, size);
134 }
135
136 void speex_free (void *ptr)
137 {
138 free(ptr);
139 }
140
141 void *speex_move (void *dest, void *src, int n)
142 {
143 return memmove(dest,src,n);
144 }
145
146 void speex_error(const char *str)
147 {
148 fprintf (stderr, "Fatal error: %s\n", str);
149 exit(1);
150 }
151
152 void speex_warning(const char *str)
153 {
154 fprintf (stderr, "warning: %s\n", str);
155 }
156
157 void speex_warning_int(const char *str, int val)
158 {
159 fprintf (stderr, "warning: %s %d\n", str, val);
160 }
161
162 void speex_rand_vec(float std, spx_sig_t *data, int len)
163 {
164 int i;
165 for (i=0;i<len;i++)
166 data[i]+=SIG_SCALING*3*std*((((float)rand())/RAND_MAX)-.5);
167 }
168
169 float speex_rand(float std)
170 {
171 return 3*std*((((float)rand())/RAND_MAX)-.5);
172 }
173
174 void _speex_putc(int ch, void *file)
175 {
176 FILE *f = (FILE *)file;
177 fprintf(f, "%c", ch);
178 }