File size: 8,917 Bytes
5610573 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* ====================================================================
* Copyright (c) 2008 Carnegie Mellon University. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* This work was supported in part by funding from the Defense Advanced
* Research Projects Agency and the National Science Foundation of the
* United States of America, and the CMU Sphinx Speech Consortium.
*
* THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
* ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
* NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ====================================================================
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <pocketsphinx.h>
#include "util/byteorder.h"
#include "util/ckd_alloc.h"
#define TRY_FREAD(ptr, size, nmemb, stream) \
if (fread(ptr, size, nmemb, stream) != (nmemb)) { \
E_ERROR_SYSTEM("Failed to read %d bytes", size * nmemb); \
rv = -1; \
goto error_out; \
}
int
ps_config_soundfile(ps_config_t *config, FILE *infh, const char *file)
{
char header[4];
int rv = 0;
if (file == NULL)
file = "(input filehandle)";
fseek(infh, 0, SEEK_SET);
TRY_FREAD(header, 1, 4, infh);
fseek(infh, 0, SEEK_SET);
if (0 == memcmp(header, "RIFF", 4)) {
E_INFO("%s appears to be a WAV file\n", file);
rv = ps_config_wavfile(config, infh, file);
}
else if (0 == memcmp(header, "NIST", 4)) {
E_INFO("%s appears to be a NIST SPHERE file\n", file);
rv = ps_config_nistfile(config, infh, file);
}
else if (0 == memcmp(header, "OggS", 4)) {
E_INFO("%s appears to be an UNSUPPORTED Ogg file\n", file);
rv = -1;
goto error_out;
}
else if (0 == memcmp(header, "fLaC", 4)) {
E_INFO("%s appears to be an UNSUPPORTED FLAC file\n", file);
rv = -1;
goto error_out;
}
else if (0 == memcmp(header, "\xff\xff\xff", 3)
|| 0 == memcmp(header, "\xff\xff\xfe", 3)) {
E_INFO("%s might be an MP3 file, but who knows really! "
"UNSUPPORTED!\n", file);
rv = -1;
goto error_out;
}
else {
E_INFO("%s appears to be raw data\n", file);
}
error_out:
return rv;
}
int
ps_config_wavfile(ps_config_t *config, FILE *infh, const char *file)
{
char id[4];
int32 intval, header_len;
int16 shortval;
int rv = 0;
if (file == NULL)
file = "(input filehandle)";
/* RIFF files are little-endian by definition. */
ps_config_set_str(config, "input_endian", "little");
/* Read in all the header chunks and etcetera. */
TRY_FREAD(id, 1, 4, infh);
/* Total file length (we don't care) */
TRY_FREAD(&intval, 4, 1, infh);
/* 'WAVE' */
TRY_FREAD(id, 1, 4, infh);
if (0 != memcmp(id, "WAVE", 4)) {
E_ERROR("%s is not a WAVE file\n", file);
rv = -1;
goto error_out;
}
/* 'fmt ' */
TRY_FREAD(id, 1, 4, infh);
if (0 != memcmp(id, "fmt ", 4)) {
E_ERROR("Format chunk missing\n");
rv = -1;
goto error_out;
}
/* Length of 'fmt ' chunk */
TRY_FREAD(&intval, 4, 1, infh);
SWAP_LE_32(&intval);
header_len = intval;
/* Data format. */
TRY_FREAD(&shortval, 2, 1, infh);
SWAP_LE_16(&shortval);
if (shortval != 1) { /* PCM */
E_ERROR("%s is not in PCM format\n", file);
rv = -1;
goto error_out;
}
/* Number of channels. */
TRY_FREAD(&shortval, 2, 1, infh);
SWAP_LE_16(&shortval);
if (shortval != 1) { /* PCM */
E_ERROR("%s is not single channel\n", file);
rv = -1;
goto error_out;
}
/* Sampling rate (finally!) */
TRY_FREAD(&intval, 4, 1, infh);
SWAP_LE_32(&intval);
if (ps_config_int(config, "samprate") == 0)
ps_config_set_int(config, "samprate", intval);
else if (ps_config_int(config, "samprate") != intval) {
E_WARN("WAVE file sampling rate %d != samprate %d\n",
intval, ps_config_int(config, "samprate"));
}
/* Average bytes per second (we don't care) */
TRY_FREAD(&intval, 4, 1, infh);
/* Block alignment (we don't care) */
TRY_FREAD(&shortval, 2, 1, infh);
/* Bits per sample (must be 16) */
TRY_FREAD(&shortval, 2, 1, infh);
SWAP_LE_16(&shortval);
if (shortval != 16) {
E_ERROR("%s is not 16-bit\n", file);
rv = -1;
goto error_out;
}
/* Any extra parameters. */
if (header_len > 16) {
/* Avoid seeking... */
char *spam = malloc(header_len - 16);
if (fread(spam, 1, header_len - 16, infh) != (size_t)(header_len - 16)) {
E_ERROR_SYSTEM("%s: Failed to read extra header", file);
rv = -1;
}
ckd_free(spam);
if (rv == -1)
goto error_out;
}
/* Now skip to the 'data' chunk. */
while (1) {
TRY_FREAD(id, 1, 4, infh);
if (0 == memcmp(id, "data", 4)) {
/* Total number of bytes of data (we don't care). */
TRY_FREAD(&intval, 4, 1, infh);
break;
}
else {
char *spam;
/* Some other stuff... */
/* Number of bytes of ... whatever */
TRY_FREAD(&intval, 4, 1, infh);
SWAP_LE_32(&intval);
/* Avoid seeking... */
spam = malloc(intval);
if (fread(spam, 1, intval, infh) != (size_t)intval) {
E_ERROR_SYSTEM("%s: Failed to read %s chunk", file, id);
rv = -1;
}
ckd_free(spam);
if (rv == -1)
goto error_out;
}
}
error_out:
return rv;
}
int
ps_config_nistfile(ps_config_t *config, FILE *infh, const char *file)
{
char hdr[1024];
char *line, *c;
int rv = 0;
if (file == NULL)
file = "(input filehandle)";
TRY_FREAD(hdr, 1, 1024, infh);
hdr[1023] = '\0';
/* Roughly parse it to find the sampling rate and byte order
* (don't bother with other stuff) */
if ((line = strstr(hdr, "sample_rate")) == NULL) {
E_ERROR("No sampling rate in NIST header!\n");
rv = -1;
goto error_out;
}
c = strchr(line, '\n');
if (c) *c = '\0';
c = strrchr(line, ' ');
if (c == NULL) {
E_ERROR("Could not find sampling rate!\n");
rv = -1;
goto error_out;
}
++c;
if (ps_config_int(config, "samprate") == 0)
ps_config_set_int(config, "samprate", atoi(c));
else if (ps_config_int(config, "samprate") != atoi(c)) {
E_WARN("NIST file sampling rate %d != samprate %d\n",
atoi(c), ps_config_int(config, "samprate"));
}
if (line + strlen(line) < hdr + 1023)
line[strlen(line)] = ' ';
if ((line = strstr(hdr, "sample_byte_format")) == NULL) {
E_ERROR("No sample byte format in NIST header!\n");
rv = -1;
goto error_out;
}
c = strchr(line, '\n');
if (c) *c = '\0';
c = strrchr(line, ' ');
if (c == NULL) {
E_ERROR("Could not find sample byte order!\n");
rv = -1;
goto error_out;
}
++c;
if (0 == memcmp(c, "01", 2)) {
ps_config_set_str(config, "input_endian", "little");
}
else if (0 == memcmp(c, "10", 2)) {
ps_config_set_str(config, "input_endian", "big");
}
else {
E_ERROR("Unknown byte order %s\n", c);
rv = -1;
goto error_out;
}
error_out:
return rv;
}
|