00001
00061
00062
00063
00064
00065
00066
00067
00068
00069 #include <sent/stddefs.h>
00070 #include <sent/speech.h>
00071 #include <sent/adin.h>
00072
00073 static FILE *gfp;
00074 static boolean wav_p;
00075 static int maxlen;
00076 static int nowlen;
00077
00082 static SP16 pre_data[2];
00083 static boolean has_pre;
00084
00085 static unsigned int sfreq;
00086
00087
00088
00089
00101 static boolean
00102 myread(void *buf, size_t unitbyte, int unitnum, FILE *fp)
00103 {
00104 int tmp;
00105 if ((tmp = myfread(buf, unitbyte, unitnum, fp)) < unitnum) {
00106 return(FALSE);
00107 }
00108 #ifdef WORDS_BIGENDIAN
00109 swap_bytes(buf, unitbyte, unitnum);
00110 #endif
00111 return(TRUE);
00112 }
00114 #define MYREAD(A,B,C,D) if (!myread(A, B, C, D)) {j_printerr("adin_file: file is corrupted\n"); return -1;}
00115
00129 static boolean
00130 setup_wav(FILE *fp)
00131 {
00132 char dummy[9];
00133 unsigned int i, len;
00134 unsigned short s;
00135
00136
00137
00138 MYREAD(dummy, 1, 4, fp);
00139
00140
00141 MYREAD(dummy, 1, 4, fp);
00142 if (dummy[0] != 'W' ||
00143 dummy[1] != 'A' ||
00144 dummy[2] != 'V' ||
00145 dummy[3] != 'E') {
00146 j_printerr("adin_file: WAVE header not found, file corrupted?\n");
00147 fclose_readfile(fp); return FALSE;
00148 }
00149
00150 MYREAD(dummy, 1, 4, fp);
00151 if (dummy[0] != 'f' ||
00152 dummy[1] != 'm' ||
00153 dummy[2] != 't' ||
00154 dummy[3] != ' ') {
00155 j_printerr("adin_file: fmt chunk not found, file corrupted?\n");
00156 fclose_readfile(fp); return FALSE;
00157 }
00158
00159 MYREAD(&len, 4, 1, fp);
00160
00161
00162 MYREAD(&s, 2, 1, fp);
00163 if (s != 1) {
00164 j_printerr("adin_file: data format != PCM (id=%d)\n", s);
00165 fclose_readfile(fp); return FALSE;
00166 }
00167
00168 MYREAD(&s, 2, 1, fp);
00169 if (s >= 2) {
00170 j_printerr("adin_file: channel num != 1 (%d)\n", s);
00171 fclose_readfile(fp); return FALSE;
00172 }
00173
00174 MYREAD(&i, 4, 1, fp);
00175 if (i != sfreq) {
00176 j_printerr("adin_file: sampling rate != %d (%d)\n", sfreq, i);
00177 fclose_readfile(fp); return FALSE;
00178 }
00179
00180 MYREAD(&i, 4, 1, fp);
00181 if (i != sfreq * sizeof(SP16)) {
00182 j_printerr("adin_file: bytes per second != %d (%d)\n", sfreq * sizeof(SP16), i);
00183 fclose_readfile(fp); return FALSE;
00184 }
00185
00186 MYREAD(&s, 2, 1, fp);
00187 if (s != 2) {
00188 j_printerr("adin_file: (bytes per sample) x channel != 2 (%d)\n", s);
00189 fclose_readfile(fp); return FALSE;
00190 }
00191
00192 MYREAD(&s, 2, 1, fp);
00193 if (s != 16) {
00194 j_printerr("adin_file: bits per sample != 16 (%d)\n", s);
00195 fclose_readfile(fp); return FALSE;
00196 }
00197
00198 if (len > 16) {
00199 len -= 16;
00200 while (len > 0) {
00201 if (len > 8) {
00202 MYREAD(dummy, 1, 8, fp);
00203 len -= 8;
00204 } else {
00205 MYREAD(dummy, 1, len, fp);
00206 len = 0;
00207 }
00208 }
00209 }
00210
00211
00212
00213 while (myread(dummy, 1, 4, fp)) {
00214 MYREAD(&len, 4, 1, fp);
00215 if (dummy[0] == 'd' &&
00216 dummy[1] == 'a' &&
00217 dummy[2] == 't' &&
00218 dummy[3] == 'a') {
00219 break;
00220 }
00221 for (i=0;i<len;i++) myread(dummy, 1, 1, fp);
00222 }
00223
00224 maxlen = len / sizeof(SP16);
00225 nowlen = 0;
00226 return TRUE;
00227 }
00228
00238 static boolean
00239 adin_file_open(char *filename)
00240 {
00241 FILE *fp;
00242 char dummy[4];
00243
00244 if (filename != NULL) {
00245 if ((fp = fopen_readfile(filename)) == NULL) {
00246 j_printerr("failed to open %s\n",filename);
00247 return(FALSE);
00248 }
00249 } else {
00250 fp = stdin;
00251 }
00252
00253 if (myfread(dummy, 1, 4, fp) < 4) {
00254 j_printerr("Error: size less than 4 bytes?\n",filename);
00255 fclose_readfile(fp);
00256 return(FALSE);
00257 }
00258 if (dummy[0] == 'R' &&
00259 dummy[1] == 'I' &&
00260 dummy[2] == 'F' &&
00261 dummy[3] == 'F') {
00262
00263 wav_p = TRUE;
00264 has_pre = FALSE;
00265 if (setup_wav(fp) == FALSE) {
00266 j_printerr("Error: failed to read %s as a wav file\n",filename);
00267 fclose_readfile(fp);
00268 return(FALSE);
00269 }
00270 } else {
00271
00272 wav_p = FALSE;
00273 memcpy(pre_data, dummy, 4);
00274 has_pre = TRUE;
00275 }
00276
00277 gfp = fp;
00278
00279 return(TRUE);
00280 }
00281
00287 static boolean
00288 adin_file_close()
00289 {
00290 FILE *fp;
00291
00292 fp = gfp;
00293 if (fclose_readfile(fp) != 0) {
00294 perror("adin_file_close: fclose_readfile");
00295 return FALSE;
00296 }
00297 return TRUE;
00298 }
00299
00300
00301
00302
00303
00304
00305
00313 char *
00314 get_line(char *prompt)
00315 {
00316 char *buf = NULL;
00317 char *p;
00318
00319 buf = (char *)mymalloc(500);
00320 do {
00321 j_printerr("%s",prompt);
00322 if (fgets(buf, 500, stdin) == NULL) {
00323 free(buf);
00324 return(NULL);
00325 }
00326 } while (!buf[0]);
00327
00328 p = buf + strlen(buf) - 1;
00329 if (p >= buf && *p == '\n') {
00330 *(p --) = '\0';
00331 }
00332 if (p >= buf && *p == '\r') {
00333 *(p --) = '\0';
00334 }
00335
00336 while(p >= buf && *p == ' ') {
00337 *(p --) = '\0';
00338 }
00339
00340 return(buf);
00341 }
00342
00343 static boolean from_file;
00344 static FILE *fp_list;
00345
00354 boolean
00355 adin_file_standby(int freq, void *arg)
00356 {
00357 char *fname = arg;
00358 if (fname != NULL) {
00359
00360 if ((fp_list = fopen(fname, "r")) == NULL) {
00361 j_printerr("failed to open %s\n", fname);
00362 return(FALSE);
00363 }
00364 from_file = TRUE;
00365 } else {
00366
00367 from_file = FALSE;
00368 }
00369
00370 sfreq = freq;
00371
00372 return(TRUE);
00373 }
00374
00384 boolean
00385 adin_file_begin()
00386 {
00387 char *speechfilename;
00388 boolean readp;
00389
00390
00391 readp = FALSE;
00392 while(readp == FALSE) {
00393 if (from_file) {
00394
00395 speechfilename = (char *)mymalloc(500);
00396 do {
00397 if (getl_fp(speechfilename, 500, fp_list) == NULL) {
00398 free(speechfilename);
00399 fclose(fp_list);
00400 return(FALSE);
00401 }
00402 } while (speechfilename[0] == '#');
00403 } else {
00404
00405 speechfilename = get_line("enter filename->");
00406 if (speechfilename == NULL) return (FALSE);
00407 }
00408
00409 if (adin_file_open(speechfilename) == FALSE) {
00410 j_printerr("Error in reading speech data: \"%s\"\n",speechfilename);
00411 } else {
00412 j_printf("\ninput speechfile: %s\n",speechfilename);
00413 readp = TRUE;
00414 }
00415 free(speechfilename);
00416 }
00417 return TRUE;
00418 }
00419
00428 int
00429 adin_file_read(SP16 *buf, int sampnum)
00430 {
00431 FILE *fp;
00432 int cnt;
00433
00434 fp = gfp;
00435
00436 if (wav_p) {
00437 cnt = myfread(buf, sizeof(SP16), sampnum, fp);
00438 if (nowlen + cnt > maxlen) {
00439 cnt = maxlen - nowlen;
00440 }
00441 nowlen += cnt;
00442 } else {
00443 if (has_pre) {
00444 buf[0] = pre_data[0]; buf[1] = pre_data[1];
00445 has_pre = FALSE;
00446 cnt = myfread(&(buf[2]), sizeof(SP16), sampnum - 2, fp);
00447 if (cnt > 0) cnt += 2;
00448 } else {
00449 cnt = myfread(buf, sizeof(SP16), sampnum, fp);
00450 }
00451 }
00452 if (cnt == 0) {
00453 if (myfeof(fp) == 1) {
00454 return -1;
00455 }
00456 perror("adin_file_read");
00457 adin_file_close();
00458 return -2;
00459 }
00460
00461
00462 #ifdef WORDS_BIGENDIAN
00463 if (wav_p) swap_sample_bytes(buf, cnt);
00464 #else
00465 if (!wav_p) swap_sample_bytes(buf, cnt);
00466 #endif
00467 return cnt;
00468 }
00469
00475 boolean
00476 adin_file_end()
00477 {
00478
00479 adin_file_close();
00480 return TRUE;
00481 }
00482
00491 boolean
00492 adin_stdin_standby(int freq, void *arg)
00493 {
00494
00495 sfreq = freq;
00496 return(TRUE);
00497 }
00498
00504 boolean
00505 adin_stdin_begin()
00506 {
00507 if (feof(stdin)) {
00508 return FALSE;
00509 } else {
00510
00511 if (adin_file_open(NULL) == FALSE) {
00512 j_printerr("Error in reading speech data from stdin\n");
00513 }
00514 j_printf("Reading wavedata from stdin...\n");
00515 }
00516 return TRUE;
00517 }
00518
00527 int
00528 adin_stdin_read(SP16 *buf, int sampnum)
00529 {
00530 int cnt;
00531
00532 if (wav_p) {
00533 cnt = myfread(buf, sizeof(SP16), sampnum, stdin);
00534 } else {
00535 if (has_pre) {
00536 buf[0] = pre_data[0]; buf[1] = pre_data[1];
00537 has_pre = FALSE;
00538 cnt = fread(&(buf[2]), sizeof(SP16), sampnum - 2, stdin);
00539 if (cnt > 0) cnt += 2;
00540 } else {
00541 cnt = fread(buf, sizeof(SP16), sampnum, stdin);
00542 }
00543 }
00544 if (cnt == 0) {
00545 if (ferror(stdin)) {
00546 perror("adin_stdin_read");
00547 return -2;
00548 }
00549 return -1;
00550 }
00551
00552
00553 #ifdef WORDS_BIGENDIAN
00554 if (wav_p) swap_sample_bytes(buf, cnt);
00555 #else
00556 if (!wav_p) swap_sample_bytes(buf, cnt);
00557 #endif
00558 return cnt;
00559 }