📄 adin_file.c
字号:
/** * Close the input file * * @return TRUE on success, FALSE on failure. */static booleanadin_file_close(){ FILE *fp; fp = gfp; if (fclose_readfile(fp) != 0) { perror("adin_file_close: fclose_readfile"); return FALSE; } return TRUE; }/* get 1 line input from stdin with prompt *//* return value: newly allocated buffer *//* repeat if no input, and *//* returns NULL on EOF *//** * Get one file name from stdin with a prompt . Blank line is omitted. * * @param prompt [in] prompt string * * @return newly allocated buffer filled with the input. */char *get_line(char *prompt){ char *buf = NULL; char *p; buf = (char *)mymalloc(500); do { j_printerr("%s",prompt); if (fgets(buf, 500, stdin) == NULL) { free(buf); return(NULL); } } while (!buf[0]); /* loop till some input */ /* chop last newline */ p = buf + strlen(buf) - 1; if (p >= buf && *p == '\n') { *(p --) = '\0'; } if (p >= buf && *p == '\r') { *(p --) = '\0'; } /* chop last space */ while(p >= buf && *p == ' ') { *(p --) = '\0'; } return(buf);}static boolean from_file; ///< TRUE if list file is used to read input filenamestatic FILE *fp_list; ///< File pointer used for the listfile/** * Initialization: if listfile is specified, open it here. * * @param freq [in] required sampling frequency. * @param arg [in] file name of listfile, or NULL if not use * * @return TRUE on success, FALSE on failure. */booleanadin_file_standby(int freq, void *arg){ char *fname = arg; if (fname != NULL) { /* read input filename from file */ if ((fp_list = fopen(fname, "r")) == NULL) { j_printerr("failed to open %s\n", fname); return(FALSE); } from_file = TRUE; } else { /* read filename from stdin */ from_file = FALSE; } /* store sampling frequency */ sfreq = freq; return(TRUE);}/** * @brief Begin reading audio data from a file. * * If listfile was specified in adin_file_standby(), the next filename * will be read from the listfile. Otherwise, the * filename will be obtained from stdin. Then the file will be opened here. * * @return TRUE on success, FALSE on failure. */booleanadin_file_begin(){ char *speechfilename; boolean readp; /* ready to read next input */ readp = FALSE; while(readp == FALSE) { if (from_file) { /* read file name from listfile */ speechfilename = (char *)mymalloc(500); do { if (getl_fp(speechfilename, 500, fp_list) == NULL) { /* end of input */ free(speechfilename); fclose(fp_list); return(FALSE); /* end of input */ } } while (speechfilename[0] == '#'); /* skip comment */ } else { /* read file name from stdin */ speechfilename = get_line("enter filename->"); if (speechfilename == NULL) return (FALSE); /* end of input */ } /* open input file */ if (adin_file_open(speechfilename) == FALSE) { j_printerr("Error in reading speech data: \"%s\"\n",speechfilename); } else { j_printf("\ninput speechfile: %s\n",speechfilename); readp = TRUE; } free(speechfilename); } return TRUE;}/** * Try to read @a sampnum samples and returns actual sample num recorded. * * @param buf [out] samples obtained in this function * @param sampnum [in] wanted number of samples to be read * * @return actural number of read samples, -1 if EOF, -2 if error. */intadin_file_read(SP16 *buf, int sampnum){ FILE *fp; int cnt; fp = gfp; if (wav_p) { cnt = myfread(buf, sizeof(SP16), sampnum, fp); if (nowlen + cnt > maxlen) { cnt = maxlen - nowlen; } nowlen += cnt; } else { if (has_pre) { buf[0] = pre_data[0]; buf[1] = pre_data[1]; has_pre = FALSE; cnt = myfread(&(buf[2]), sizeof(SP16), sampnum - 2, fp); if (cnt > 0) cnt += 2; } else { cnt = myfread(buf, sizeof(SP16), sampnum, fp); } } if (cnt == 0) { /* error or EOF */ if (myfeof(fp) == 1) { /* EOF */ return -1; } perror("adin_file_read"); adin_file_close(); return -2; /* error */ } /* all .wav data are in little endian */ /* assume .raw data are in big endian */#ifdef WORDS_BIGENDIAN if (wav_p) swap_sample_bytes(buf, cnt);#else if (!wav_p) swap_sample_bytes(buf, cnt);#endif return cnt;}/** * End recording. * * @return TRUE on success, FALSE on failure. */booleanadin_file_end(){ /* nothing needed */ adin_file_close(); return TRUE;}/** * Initialization for speech input via stdin. * * @param freq [in] required sampling frequency. * @param arg dummy, ignored * * @return TRUE on success, FALSE on failure. */booleanadin_stdin_standby(int freq, void *arg){ /* store sampling frequency */ sfreq = freq; return(TRUE);}/** * @brief Begin reading audio data from stdin * * @return TRUE on success, FALSE on failure. */booleanadin_stdin_begin(){ if (feof(stdin)) { /* already reached the end of input stream */ return FALSE; /* terminate search here */ } else { /* open input stream */ if (adin_file_open(NULL) == FALSE) { j_printerr("Error in reading speech data from stdin\n"); } j_printf("Reading wavedata from stdin...\n"); } return TRUE;}/** * Try to read @a sampnum samples and returns actual sample num recorded. * * @param buf [out] samples obtained in this function * @param sampnum [in] wanted number of samples to be read * * @return actural number of read samples, -1 if EOF, -2 if error. */intadin_stdin_read(SP16 *buf, int sampnum){ int cnt; if (wav_p) { cnt = myfread(buf, sizeof(SP16), sampnum, stdin); } else { if (has_pre) { buf[0] = pre_data[0]; buf[1] = pre_data[1]; has_pre = FALSE; cnt = fread(&(buf[2]), sizeof(SP16), sampnum - 2, stdin); if (cnt > 0) cnt += 2; } else { cnt = fread(buf, sizeof(SP16), sampnum, stdin); } } if (cnt == 0) { if (ferror(stdin)) { /* error */ perror("adin_stdin_read"); return -2; /* error */ } return -1; /* EOF */ } /* all .wav data are in little endian */ /* assume .raw data are in big endian */#ifdef WORDS_BIGENDIAN if (wav_p) swap_sample_bytes(buf, cnt);#else if (!wav_p) swap_sample_bytes(buf, cnt);#endif return cnt;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -