Mercurial > hg > audiostuff
comparison spandsp-0.0.3/spandsp-0.0.3/tests/time_scale_tests.c @ 5:f762bf195c4b
import spandsp-0.0.3
author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
---|---|
date | Fri, 25 Jun 2010 16:00:21 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
4:26cd8f1ef0b1 | 5:f762bf195c4b |
---|---|
1 /* | |
2 * SpanDSP - a series of DSP components for telephony | |
3 * | |
4 * time_scale_tests.c | |
5 * | |
6 * Written by Steve Underwood <steveu@coppice.org> | |
7 * | |
8 * Copyright (C) 2004 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: time_scale_tests.c,v 1.14 2006/11/19 14:07:27 steveu Exp $ | |
26 */ | |
27 | |
28 /*! \page time_scale_tests_page Time scaling tests | |
29 \section time_scale_tests_page_sec_1 What does it do? | |
30 These tests run a speech file through the time scaling routines. | |
31 | |
32 \section time_scale_tests_page_sec_2 How are the tests run? | |
33 These tests process a speech file called pre_time_scale.wav. This file should contain | |
34 8000 sample/second 16 bits/sample linear audio. The tests read this file, change the | |
35 time scale of its contents, and write the resulting audio to post_time_scale.wav. | |
36 This file also contains 8000 sample/second 16 bits/sample linear audio. | |
37 */ | |
38 | |
39 #ifdef HAVE_CONFIG_H | |
40 #include "config.h" | |
41 #endif | |
42 | |
43 #include <stdio.h> | |
44 #include <inttypes.h> | |
45 #include <stdlib.h> | |
46 #include <string.h> | |
47 #if defined(HAVE_TGMATH_H) | |
48 #include <tgmath.h> | |
49 #endif | |
50 #if defined(HAVE_MATH_H) | |
51 #include <math.h> | |
52 #endif | |
53 #include <audiofile.h> | |
54 #include <tiffio.h> | |
55 | |
56 #include "spandsp.h" | |
57 | |
58 #define BLOCK_LEN 160 | |
59 | |
60 #define IN_FILE_NAME "../localtests/short_nb_voice.wav" | |
61 #define OUT_FILE_NAME "post_time_scaling.wav" | |
62 | |
63 int main(int argc, char *argv[]) | |
64 { | |
65 AFfilehandle inhandle; | |
66 AFfilehandle outhandle; | |
67 AFfilesetup filesetup; | |
68 int frames; | |
69 int new_frames; | |
70 int out_frames; | |
71 int count; | |
72 time_scale_t state; | |
73 float x; | |
74 float rate; | |
75 int16_t in[BLOCK_LEN]; | |
76 int16_t out[5*BLOCK_LEN]; | |
77 | |
78 if ((inhandle = afOpenFile(IN_FILE_NAME, "r", 0)) == AF_NULL_FILEHANDLE) | |
79 { | |
80 printf(" Cannot open wave file '%s'\n", IN_FILE_NAME); | |
81 exit(2); | |
82 } | |
83 if ((x = afGetFrameSize(inhandle, AF_DEFAULT_TRACK, 1)) != 2.0) | |
84 { | |
85 printf(" Unexpected frame size in wave file '%s'\n", IN_FILE_NAME); | |
86 exit(2); | |
87 } | |
88 if ((x = afGetRate(inhandle, AF_DEFAULT_TRACK)) != (float) SAMPLE_RATE) | |
89 { | |
90 printf(" Unexpected sample rate in wave file '%s'\n", IN_FILE_NAME); | |
91 exit(2); | |
92 } | |
93 if ((x = afGetChannels(inhandle, AF_DEFAULT_TRACK)) != 1.0) | |
94 { | |
95 printf(" Unexpected number of channels in wave file '%s'\n", IN_FILE_NAME); | |
96 exit(2); | |
97 } | |
98 | |
99 if ((filesetup = afNewFileSetup()) == AF_NULL_FILESETUP) | |
100 { | |
101 fprintf(stderr, " Failed to create file setup\n"); | |
102 exit(2); | |
103 } | |
104 afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16); | |
105 afInitRate(filesetup, AF_DEFAULT_TRACK, (float) SAMPLE_RATE); | |
106 afInitFileFormat(filesetup, AF_FILE_WAVE); | |
107 afInitChannels(filesetup, AF_DEFAULT_TRACK, 1); | |
108 if ((outhandle = afOpenFile(OUT_FILE_NAME, "w", filesetup)) == AF_NULL_FILEHANDLE) | |
109 { | |
110 fprintf(stderr, " Cannot create wave file '%s'\n", OUT_FILE_NAME); | |
111 exit(2); | |
112 } | |
113 | |
114 rate = 1.8; | |
115 | |
116 time_scale_init(&state, rate); | |
117 count = 0; | |
118 while ((frames = afReadFrames(inhandle, AF_DEFAULT_TRACK, in, BLOCK_LEN))) | |
119 { | |
120 new_frames = time_scale(&state, out, in, frames); | |
121 out_frames = afWriteFrames(outhandle, AF_DEFAULT_TRACK, out, new_frames); | |
122 if (out_frames != new_frames) | |
123 { | |
124 fprintf(stderr, " Error writing wave file\n"); | |
125 exit(2); | |
126 } | |
127 if (++count > 100) | |
128 { | |
129 if (rate > 0.5) | |
130 { | |
131 rate -= 0.1; | |
132 if (rate >= 0.99 && rate <= 1.01) | |
133 rate -= 0.1; | |
134 printf("Rate is %f\n", rate); | |
135 time_scale_init(&state, rate); | |
136 } | |
137 count = 0; | |
138 } | |
139 } | |
140 if (afCloseFile(inhandle) != 0) | |
141 { | |
142 printf(" Cannot close wave file '%s'\n", IN_FILE_NAME); | |
143 exit(2); | |
144 } | |
145 if (afCloseFile(outhandle) != 0) | |
146 { | |
147 printf(" Cannot close wave file '%s'\n", OUT_FILE_NAME); | |
148 exit(2); | |
149 } | |
150 afFreeFileSetup(filesetup); | |
151 return 0; | |
152 } | |
153 /*- End of function --------------------------------------------------------*/ | |
154 /*- End of file ------------------------------------------------------------*/ |