Mercurial > hg > audiostuff
comparison spandsp-0.0.6pre17/tests/time_scale_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 * 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.24 2009/05/30 15:23:14 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 #if defined(HAVE_CONFIG_H) | |
| 40 #include "config.h" | |
| 41 #endif | |
| 42 | |
| 43 #include <stdlib.h> | |
| 44 #include <stdio.h> | |
| 45 #include <unistd.h> | |
| 46 #include <string.h> | |
| 47 #include <sndfile.h> | |
| 48 | |
| 49 #include "spandsp.h" | |
| 50 | |
| 51 #include "spandsp/private/time_scale.h" | |
| 52 | |
| 53 #define BLOCK_LEN 160 | |
| 54 | |
| 55 #define IN_FILE_NAME "../test-data/local/short_nb_voice.wav" | |
| 56 #define OUT_FILE_NAME "time_scale_result.wav" | |
| 57 | |
| 58 int main(int argc, char *argv[]) | |
| 59 { | |
| 60 SNDFILE *inhandle; | |
| 61 SNDFILE *outhandle; | |
| 62 SF_INFO info; | |
| 63 int16_t in[BLOCK_LEN]; | |
| 64 int16_t out[5*(BLOCK_LEN + TIME_SCALE_MAX_SAMPLE_RATE/TIME_SCALE_MIN_PITCH)]; | |
| 65 int frames; | |
| 66 int new_frames; | |
| 67 int out_frames; | |
| 68 int count; | |
| 69 int max; | |
| 70 time_scale_state_t state; | |
| 71 float rate; | |
| 72 float sample_rate; | |
| 73 const char *in_file_name; | |
| 74 int sweep_rate; | |
| 75 int opt; | |
| 76 | |
| 77 rate = 1.8f; | |
| 78 sweep_rate = FALSE; | |
| 79 in_file_name = IN_FILE_NAME; | |
| 80 while ((opt = getopt(argc, argv, "i:r:s")) != -1) | |
| 81 { | |
| 82 switch (opt) | |
| 83 { | |
| 84 case 'i': | |
| 85 in_file_name = optarg; | |
| 86 break; | |
| 87 case 'r': | |
| 88 rate = atof(optarg); | |
| 89 break; | |
| 90 case 's': | |
| 91 sweep_rate = TRUE; | |
| 92 break; | |
| 93 default: | |
| 94 //usage(); | |
| 95 exit(2); | |
| 96 break; | |
| 97 } | |
| 98 } | |
| 99 if ((inhandle = sf_open(in_file_name, SFM_READ, &info)) == NULL) | |
| 100 { | |
| 101 printf(" Cannot open audio file '%s'\n", in_file_name); | |
| 102 exit(2); | |
| 103 } | |
| 104 if (info.channels != 1) | |
| 105 { | |
| 106 printf(" Unexpected number of channels in audio file '%s'\n", in_file_name); | |
| 107 exit(2); | |
| 108 } | |
| 109 sample_rate = info.samplerate; | |
| 110 | |
| 111 memset(&info, 0, sizeof(info)); | |
| 112 info.frames = 0; | |
| 113 info.samplerate = sample_rate; | |
| 114 info.channels = 1; | |
| 115 info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16; | |
| 116 info.sections = 1; | |
| 117 info.seekable = 1; | |
| 118 | |
| 119 if ((outhandle = sf_open(OUT_FILE_NAME, SFM_WRITE, &info)) == NULL) | |
| 120 { | |
| 121 fprintf(stderr, " Cannot create audio file '%s'\n", OUT_FILE_NAME); | |
| 122 exit(2); | |
| 123 } | |
| 124 | |
| 125 if ((time_scale_init(&state, (int) sample_rate, rate)) == NULL) | |
| 126 { | |
| 127 fprintf(stderr, " Cannot start the time scaler\n"); | |
| 128 exit(2); | |
| 129 } | |
| 130 max = time_scale_max_output_len(&state, BLOCK_LEN); | |
| 131 printf("Rate is %f, longest output block is %d\n", rate, max); | |
| 132 count = 0; | |
| 133 while ((frames = sf_readf_short(inhandle, in, BLOCK_LEN))) | |
| 134 { | |
| 135 new_frames = time_scale(&state, out, in, frames); | |
| 136 out_frames = sf_writef_short(outhandle, out, new_frames); | |
| 137 if (out_frames != new_frames) | |
| 138 { | |
| 139 fprintf(stderr, " Error writing audio file\n"); | |
| 140 exit(2); | |
| 141 } | |
| 142 if (sweep_rate && ++count > 100) | |
| 143 { | |
| 144 if (rate > 0.5f) | |
| 145 { | |
| 146 rate -= 0.1f; | |
| 147 if (rate >= 0.99f && rate <= 1.01f) | |
| 148 rate -= 0.1f; | |
| 149 time_scale_init(&state, SAMPLE_RATE, rate); | |
| 150 max = time_scale_max_output_len(&state, BLOCK_LEN); | |
| 151 printf("Rate is %f, longest output block is %d\n", rate, max); | |
| 152 } | |
| 153 count = 0; | |
| 154 } | |
| 155 } | |
| 156 if (sf_close(inhandle) != 0) | |
| 157 { | |
| 158 printf(" Cannot close audio file '%s'\n", in_file_name); | |
| 159 exit(2); | |
| 160 } | |
| 161 if (sf_close(outhandle) != 0) | |
| 162 { | |
| 163 printf(" Cannot close audio file '%s'\n", OUT_FILE_NAME); | |
| 164 exit(2); | |
| 165 } | |
| 166 return 0; | |
| 167 } | |
| 168 /*- End of function --------------------------------------------------------*/ | |
| 169 /*- End of file ------------------------------------------------------------*/ |
