Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

record.c

Go to the documentation of this file.
00001 
00034 /*
00035  * Copyright (c) 1991-2006 Kawahara Lab., Kyoto University
00036  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
00037  * Copyright (c) 2005-2006 Julius project team, Nagoya Institute of Technology, Nagoya Institute of Technology
00038  * All rights reserved
00039  */
00040 
00041 #include <julius.h>
00042 #include <time.h>
00043 
00044 #define TSTRLEN 18              
00045 
00046 static char tstr[TSTRLEN];      
00047 static char recfilename[MAXLINELEN]; 
00048 static char finalfilename[MAXLINELEN]; 
00049 static FILE *fp = NULL;         
00050 static int totalnum;            
00051 
00066 static void
00067 timestring(char *t, int maxlen)
00068 {
00069   time_t timep;
00070   struct tm *lmtm;
00071 
00072   time(&timep);
00073   lmtm = localtime(&timep);
00074 
00075   snprintf ( t, maxlen,"%04d.%02d%02d.%02d%02d%02d", 1900+lmtm->tm_year, 1+lmtm->tm_mon, lmtm->tm_mday, lmtm->tm_hour, lmtm->tm_min, lmtm->tm_sec);
00076 }
00077 
00096 static void
00097 make_record_filename(char *buf, int buflen, char *filename)
00098 {
00099   if (record_dirname == NULL) {
00100     j_error("no record directory specified??\n");
00101   }
00102   snprintf(buf, buflen,
00103 #if defined(_WIN32) && !defined(__CYGWIN32__)
00104            "%s\\%s.wav"
00105 #else
00106            "%s/%s.wav"
00107 #endif
00108            , record_dirname, filename);
00109 }
00110 
00125 static void
00126 make_tmp_filename(char *buf, int buflen)
00127 {
00128 #if defined(_WIN32) && !defined(__CYGWIN32__)
00129   snprintf(buf, buflen, "%s\\tmprecord.000", record_dirname);
00130 #else
00131   snprintf(buf, buflen, "%s/tmprecord.%d", record_dirname, getpid());
00132 #endif
00133 }  
00134 
00145 void
00146 record_sample_open()
00147 {
00148   if (fp != NULL) {
00149     j_error("Error: record_sample_open: re-opened before closed!\n");
00150   }
00151   make_tmp_filename(recfilename, MAXLINELEN);
00152   if ((fp = wrwav_open(recfilename, smpFreq)) == NULL) {
00153     perror("Error: record_sample_open");
00154     j_error("failed to open \"%s\" (temporary record file)\n", recfilename);
00155   }
00156 
00157   totalnum = 0;
00158 }
00159 
00174 void
00175 record_sample_write(SP16 *speech, int samplenum)
00176 {
00177   if (fp == NULL) {
00178     j_error("Error: record_sample_write; file not opened yet, cannot write!\n");
00179   }
00180 
00181   if (wrwav_data(fp, speech, samplenum) == FALSE) {
00182     perror("Error: record_sample_write");
00183     j_error("failed to write samples to \"%s\"\n", recfilename);
00184   }
00185 
00186   /* make timestamp of system time when an input begins */
00187   /* the current temporal recording file will be renamed to this time-stamp filename */
00188   if (totalnum == 0) {          /* beginning of recording */
00189     timestring(tstr, TSTRLEN);
00190   }
00191   
00192   totalnum += samplenum;
00193 }
00194 
00206 void
00207 record_sample_close()
00208 {
00209   if (fp == NULL) {
00210     j_printerr("Warning: record_sample_close; file not opened yet!?\n");
00211     return;
00212   }
00213 
00214   if (wrwav_close(fp) == FALSE) {
00215     perror("Error: record_sample_close");
00216   }
00217   fp = NULL;
00218 
00219   if (totalnum == 0) {
00220     unlink(recfilename);
00221     if (verbose_flag) {
00222       j_printerr("No input, not recorded\n");
00223     }
00224     return;
00225   }
00226 
00227   /* now rename the temporary file to time-stamp filename */
00228   make_record_filename(finalfilename, MAXLINELEN, tstr);
00229   if (rename(recfilename, finalfilename) < 0) {
00230     perror("Error: record_sample_close");
00231     j_error("failed to move %s to %s\n", recfilename, finalfilename);
00232   }
00233   if (verbose_flag) {
00234     j_printerr("recorded to \"%s\" (%d bytes, %.2f sec.)\n", finalfilename, totalnum * sizeof(SP16), (float)totalnum / (float) smpFreq);
00235   }
00236 }

Generated on Tue Mar 28 16:01:38 2006 for Julius by  doxygen 1.4.2