📄 rossps.c
字号:
/* This material contains proprietary software of Entropic Processing, Inc. Any reproduction, distribution, or publication without the the prior written permission of Entropic Processing, Inc. is strictly prohibited. Any public distribution of copies of this work authorized in writing by Entropic Processing, Inc. must bear the notice "Copyright 1986 Entropic Processing, Inc." This program converts a Rosetta speech file (transfered by kermit through the console port into a SPS sampled data file. Original program by Badari Narayana. Some modifications by Alan Parker. rossps [-r sampled freq] [-v] ifile ofile*/#ifdef SCCSstatic char *sccs_id = "@(#)rossps.c 1.2 5/21/87 EPI";#define VERSION "1.2"#define DATE "@(#)rossps.c 1.2"#endif#ifndef VERSION#define VERSION "debug"#endif#ifndef DATE#define DATE "none"#endif#include <stdio.h>#include <sps/sps.h>#include <sps/sd.h>#define TRUE -1#define FALSE 0#define MYQUOTE '#' /* Quote character I will use */#define CR 13#define AT_BIT7 1#define AT_BIT5 2#define AT_BIT3 3#define AT_BIT1 4#define DEFAULT_SF 8000#define MAX_BUFF 8192int bit_pos; /* bit postion where the next 6 bits */ /* are to be packed or extracted */char image; /* image (8 bit mode) transmission */char quote; /* Flag to determine to quote the next char or not */FILE *tstrm; /* temp file */long total=0; /* total samples converted *//* * tochar: converts a control character to a printable one by * adding a space. * * unchar: undoes tochar. * * ctl: converts between control characters and printable characters by * toggling the control bit (ie. ^A becomes A and A becomes ^A). */#define tochar(ch) ((ch) + ' ')#define unchar(ch) ((ch) - ' ')#define ctl(ch) ((ch) ^ 64 )#define SYNTAX USAGE ("rossps [-r sample_freq] [-c] ifile ofile")main (argc,argv) int argc;char **argv;{ struct header *h; int c; short *sbuf; int correction_flag = 0, nleft, nget, i; extern optind; extern char *optarg; char *mktemp(), *get_cmd_line(); double sample_rate=DEFAULT_SF, atof(); FILE *istrm, *ostrm; char *ifile, *ofile, *template ="/usr/tmp/rctXXXXXX"; while ((c = getopt (argc,argv,"r:c")) != EOF) { switch (c) { case 'r': sample_rate = atoi (optarg); break; case 'c': correction_flag = 1; break; default: SYNTAX; } } if (argc - optind < 2) SYNTAX; /* two files must be given */ ifile = argv[optind]; if (strcmp(ifile,"-") == 0) { /* use standard in */ ifile = "<stdin>"; istrm = stdin; } else TRYOPEN(argv[0],ifile,"r",istrm); ofile = argv[optind+1]; if (strcmp(ofile,"-") == 0) { /* use standard out */ ofile = "<stdout>"; ostrm = stdout; } else TRYOPEN(argv[0],ofile,"w+",ostrm); image = FALSE; quote = FALSE; bit_pos = AT_BIT7; h = new_header (FT_SD); h -> hd.sd -> sf = sample_rate;/* Note that the correction scheme only works on short data. */ (void) set_sd_type(h,SHORT); (void) strcpy (h -> common.prog, "rossps"); (void) strcpy (h -> common.progdate, DATE); (void) strcpy (h -> common.vers, VERSION); (void) add_comment(h,get_cmd_line(argc,argv)); if (correction_flag) (void) add_comment(h,"Arithmetic correction made to negative numbers.\n"); (void) add_source_file(h,ifile,NULL);/* No need for temp file if output is a file and no correction needed. */ if (ostrm != stdout && correction_flag == 0) { tstrm = ostrm; /* using temp file */ (void) write_header(h,tstrm); } else /* output is stdout, so use temp */ if ((tstrm = fopen(mktemp(template),"w+")) == NULL) CANTOPEN (argv[0],template); /* open temp file */ while ((c = getc(istrm)) != EOF) convert (c); (void) rewind(tstrm); /* reposition output file */ h -> common.ndrec = total/2; /* compute number of records */ (void) fprintf(stderr,"rossps: converting %d samples.\n",total/2); (void) write_header(h,ostrm); /* write out a complete header */ if(tstrm != ostrm) /* if temp used, copy it to ostrm */ { sbuf = (short*) calloc (MAX_BUFF, sizeof(short)); nleft = h->common.ndrec; while (nleft > 0) { if (nleft < MAX_BUFF) nget = nleft; else nget = MAX_BUFF; get_sd_recs (sbuf, nget, h, tstrm); if (correction_flag) { for (i=0; i<nget; i++) if (sbuf[i] < 0) sbuf[i]++; } put_sd_recs (sbuf, nget, h, ostrm); nleft -= nget; } } (void) fclose(tstrm); (void) unlink(template); exit(0);}/* * c o n v e r t * * Unquote ASCII data byte and translate it into 6 bit char, * and write it into tstrm */convert (t)int t; /* ASCII char */{ t &= 0177; /* strip 8th bit */ if (t == MYQUOTE) { /* Control quote? */ quote = TRUE; /* save this info to quote next char */ return; } if (quote) { if ((t & 0177) != MYQUOTE)/* Low order bits match quote char? */ t = ctl (t); /* No, uncontrollify it */ quote = FALSE; /* reset quote flag */ } if (t == CR && !image) /* Don't pass CR if in image mode */ return; else memputc (t);}/* * m e m p u t c * * Memputc: store next 6 bit unit of data in the memory * * "bit_pos" is initialized to AT_BIT7 to start with. * Successive 6 bit units are stored at the bit position * indicated by the "bit_pos" in the byte pointed to * by the "memptr". The 6 bit units are stored starting from * the most significant bit position in the byte and any spill * over is stored on to the next byte pointed to by * memptr+1. The following diagram illustrates the packing * mechanism. * * * memory adr ----------------------------------> * * memptr memptr+1 memptr+2 * |7 0|7 0|7 0| * +-----------+---+-------+-------+---+-----------+ * | | | | * +-----------+---+-------+-------+---+-----------+ * | 6bit | 6bit | 6bit | 6bit | * AT_BIT7 AT_BIT1 AT_BIT3 AT_BIT5 * * order of storing 6 bit units --------------> */memputc (c)int c;{ static int part_byte6; /* space for storing part of 6 bit byte */ c -= 64; /* decode 6 bit data value */ switch (bit_pos) { case AT_BIT7: /* store byte6 at msb pos */ bit_pos = AT_BIT1; /* next byte6 pos */ part_byte6 = (c << 2); break; case AT_BIT1: /* store byte6 in the 2 lsb pos */ bit_pos = AT_BIT3; /* next byte6 storage pos */ putc (part_byte6 + (c >> 4),tstrm); total++; /* stor next 2 bits into lsb pos */ part_byte6 = (c << 4) & 0xf0; /* save 4 lsbs for next byte */ break; case AT_BIT3: /* store byte6 in the 4 lsb pos */ bit_pos = AT_BIT5; /* next byte6 storage pos */ putc (part_byte6 + (c >> 2),tstrm); total++; /* stor next 4 bits into lsb pos */ part_byte6 = c << 6;/* save 2 lsbs for next byte */ break; case AT_BIT5: /* store byte6 in the lsb pos */ bit_pos = AT_BIT7; /* next byte6 storage pos */ putc (part_byte6 + c,tstrm); total++; /* store next 6 bits into lsb pos */ break; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -