esig_edr.c

来自「speech signal process tools」· C语言 代码 · 共 2,690 行 · 第 1/4 页

C
2,690
字号
		ex += 1;	    }	    else if (hi < 0x10000000 /* 2^28 */		     && ex > -1021)	    {		hi = 0x10000000; /* 2^28 */		lo = 0;	    }	    if (ex > 1024)	/* overflow */	    {		ex = 0x7ff;		hi = 0x0000000;		lo = 0x000000;	    }	    else if (hi < 0x10000000) /* 2^28 *//* subnormal */		ex = 0x000;	    else		/* normalized */	    {		ex += 1022;		hi &= 0xfffffff; /* mask off hidden bit */	    }	    if (sn)		ex |= 0x800;	}	if (putc(ex >> 4, file) == EOF) /* sign, 7 bits of exponent */	    break;	if (putc((ex << 4) & 0xf0 | (hi >> 24),		 file) == EOF)	/* rest of exponent, 4 fraction bits */	    break;	if (putc((hi >> 16) & 0xff, file) == EOF)	    break;	if (putc((hi >> 8) & 0xff, file) == EOF)	    break;	if (putc(hi & 0xff, file) == EOF)	    break;	if (putc((lo >> 16) & 0xff, file) == EOF) /* start on lo */	    break;	if (putc((lo >> 8) & 0xff, file) == EOF) /* start on lo */	    break;	if (putc(lo & 0xff, file) == EOF) /* start on lo */	    break;    }    return n;}/* Write items of type FLOAT */static intEdrWriteFloat(float	*data,	      long	length,	      FILE	*file){    long	    n;		/* number of items written */    float	    item;	/* one data item for output */    int		    sn;		/* sign */    int		    ex;		/* exponent */    unsigned long   fr;		/* 23 fraction bits (and hidden bit) */    double	    frx;	/* fr as double */    for (n = 0; n < length; n++)    {	item = data[n];	if (!(item <= FLT_MAX))	    /* Don't change to if (item > FLT_MAX)! */	{	    if (item > DBL_MAX)	/* Inf */	    {		ex = 0x0ff;		fr = 0x000000;	    }	    else		/* NaN */	    {		ex = 0x0ff;		fr = 0x7fffff;	    }	}	else if (item < -FLT_MAX)	/* -Inf */	{	    ex = 0x1ff;	    fr = 0x000000;	}	else	{	    /*! Check for IEEE -0.0 */	    sn = (item < 0.0);	    frx = frexp((sn) ? -item : item, &ex);	    if (frx == 0)		ex = -125;	    else if (ex < -125)	    {		frx = ldexp(frx, 24 - (-125 - ex));		ex = -125;	    }	    else		frx = ldexp(frx, 24);	    fr = 0.5 + frx;	    if (fr >= 0x1000000) /* 2^24 */	    {		fr = 0x800000;	/* 2^23 */		ex += 1;	    }	    else if (fr < 0x800000 /* 2^23 */		     && ex > -125)		fr = 0x800000;	/* 2^23 */	    if (ex > 128)	/* overflow */	    {		ex = 0x0ff;		fr = 0x000000;	    }	    else if (fr < 0x800000) /* 2^23 *//* subnormal */		ex = 0x000;	    else		/* normalized */	    {		ex += 126;		fr &= 0x7fffff;	/* mask off hidden bit */	    }	    if (sn)		ex |= 0x100;	}	if (putc(ex >> 1, file) == EOF) /* sign, 7 bits of exponent */	    break;	if (putc((ex << 7) & 0x80 | (fr >> 16),		 file) == EOF)  /* rest of exponent, 7 fraction bits */	    break;	if (putc((fr >> 8) & 0xff, file) == EOF)	    break;	if (putc(fr & 0xff, file) == EOF)	    break;    }    return n;}/* Write items of type LONG */static intEdrWriteLong(long	*data,	     long	length,	     FILE	*file,	     int	longlong){    long	    n;		/* number of items written */    long	    item;	/* one data item for output */    /* This clips if native long values fall outside range     * representable with EDR.     */    switch (longlong)    {    case EDR1:	{	    unsigned long   u;	    for (n = 0; n < length; n++)	    {#if (LONG_MAX > 0x7fffffffL)	/* longs more than 32 bits */		item = data[n];		if (item > 0x7fffffffL)		{		    u = 0x7fffffffL;		    /* CLIPPING */		}		else if (item < -0x80000000L)		{		    u = -0x80000000L;		    /* CLIPPING */		}		else		    u = item;#else  /* 32-bit longs */		u = data[n];#endif		if (putc((u >> 24) & 0xff, file) == EOF)		    break;		if (putc((u >> 16) & 0xff, file) == EOF)		    break;		if (putc((u >> 8) & 0xff, file) == EOF)		    break;		if (putc(u & 0xff, file) == EOF)		    break;	    }	    return n;	}	break;    case EDR2:	{	    unsigned long   hi, lo;	    for (n = 0; n < length; n++)	    {		item = data[n];#if (LONG_MAX > 0x7fffffffL)	/* longs more than 32 bits */		lo = ((unsigned long) item) & 0xffffffffUL;		if (item >= 0)		    hi = item >> 32;		else		    hi = ~((unsigned long) (-1L - item) >> 32);#else  /* 32-bit longs */		if (item >= 0)		    hi = 0UL;		else		    hi = 0xffffffffUL;		lo = (unsigned long) item;#endif		if (putc((hi >> 24) & 0xff, file) == EOF)		    break;		if (putc((hi >> 16) & 0xff, file) == EOF)		    break;		if (putc((hi >> 8) & 0xff, file) == EOF)		    break;		if (putc(hi & 0xff, file) == EOF)		    break;		if (putc((lo >> 24) & 0xff, file) == EOF)		    break;		if (putc((lo >> 16) & 0xff, file) == EOF)		    break;		if (putc((lo >> 8) & 0xff, file) == EOF)		    break;		if (putc(lo & 0xff, file) == EOF)		    break;	    }	    return n;	}	break;    default:	return 0;    }}/* Write items of type ULONG */static intEdrWriteUlong(Ulong *data,	      long length,	      FILE *file,	      int longlong){    long	    n;		/* number of items written */    /* This clips if native unsigned long values fall outside range     * representable with EDR.     */    switch (longlong)    {    case EDR1:	{	    unsigned long   u;	    for (n = 0; n < length; n++)	    {		u = data[n];#if (ULONG_MAX > 0xffffffffUL)	/* unsigned longs more than 32 bits */		if (u > 0xffffffffL)		{		    u = 0xffffffffL;		    /* CLIPPING */		}#endif		if (putc((u >> 24) & 0xff, file) == EOF)		    break;		if (putc((u >> 16) & 0xff, file) == EOF)		    break;		if (putc((u >> 8) & 0xff, file) == EOF)		    break;		if (putc(u & 0xff, file) == EOF)		    break;	    }	    return n;	}	break;    case EDR2:	{	    unsigned long   hi, lo;	    for (n = 0; n < length; n++)	    {		lo = data[n];#if (ULONG_MAX > 0xffffffffUL)	/* unsigned longs more than 32 bits */		hi = lo >> 32;		lo &= 0xffffffffUL;#else  /* 32-bit unsigned longs */		hi = 0UL;#endif		if (putc((hi >> 24) & 0xff, file) == EOF)		    break;		if (putc((hi >> 16) & 0xff, file) == EOF)		    break;		if (putc((hi >> 8) & 0xff, file) == EOF)		    break;		if (putc(hi & 0xff, file) == EOF)		    break;		if (putc((lo >> 24) & 0xff, file) == EOF)		    break;		if (putc((lo >> 16) & 0xff, file) == EOF)		    break;		if (putc((lo >> 8) & 0xff, file) == EOF)		    break;		if (putc(lo & 0xff, file) == EOF)		    break;	    }	    return n;	}	break;    default:	return 0;    }}/* Write items of type SHORT */static intEdrWriteShort(short	*data,	      long	length,	      FILE	*file){    long	    n;		/* number of items written */    long	    item;	/* one data item for output */    unsigned int    u;		/* item as unsigned int */    /* This clips if native short values fall outside range     * representable with EDR.     */    for (n = 0; n < length; n++)    {	item = data[n];	if (item < - 0x8000L)	    u = 0x8000U;	else if (item > 0x7fffL)	    u = 0x7fffU;	else	    u = (unsigned int) item;	if (putc((u >> 8) & 0xff, file) == EOF)	    break;	if (putc(u & 0xff, file) == EOF)	    break;    }    return n;}/* Write items of type USHORT */static intEdrWriteUshort(Ushort *data,	       long length,	       FILE *file){    long	    n;		/* number of items written */    Ushort	    u;		/* one data item for output */    /* This clips if native short values fall outside range     * representable with EDR.     */    for (n = 0; n < length; n++)    {	u = data[n];	if (u > 0xffffU)	    u = 0xffffU;	if (putc((u >> 8) & 0xff, file) == EOF)	    break;	if (putc(u & 0xff, file) == EOF)	    break;    }    return n;}/* Write items of type SCHAR */static intEdrWriteSchar(Schar	*data,	      long	length,	      FILE	*file){    long    n;			/* number of items written */    for (n = 0; n < length; n++)    {	if (putc(data[n], file) == EOF)	    break;    }    return n;}/* Write items of type UCHAR */static intEdrWriteUchar(Uchar	*data,	      long	length,	      FILE	*file){    long    n;			/* number of items written */    for (n = 0; n < length; n++)    {	if (putc(data[n], file) == EOF)	    break;    }    return n;}/* Write items of type BOOL */static intEdrWriteBool(Bool   *data,	     long   length,	     FILE   *file){    long    n;			/* number of items written */    for (n = 0; n < length; n++)    {	if (putc(data[n] != 0, file) == EOF)	    break;    }    return n;}/* Write items of type DOUBLE_COMPLEX */static intEdrWriteDoubleComplex(DoubleComplex *data,		      long	    length,		      FILE	    *file){    long    n;			/* number of items written */    double  x[2];		/* real and imaginary parts */    for (n = 0; n < length; n++)    {	x[0] = data[n].real;	x[1] = data[n].imag;	if (EdrWriteDouble(x, 2, file) == EOF)	    break;    }    return n;}/* Write items of type FLOAT_COMPLEX */static intEdrWriteFloatComplex(FloatComplex   *data,		     long	    length,		     FILE	    *file){    long    n;			/* number of items written */    float  x[2];		/* real and imaginary parts */    for (n = 0; n < length; n++)    {	x[0] = data[n].real;	x[1] = data[n].imag;	if (EdrWriteFloat(x, 2, file) == EOF)	    break;    }    return n;}/* Write items of type LONG_COMPLEX */static intEdrWriteLongComplex(LongComplex	*data,		    long	length,		    FILE	*file,		    int		longlong){    long    n;			/* number of items written */    long    x[2];		/* real and imaginary parts */    for (n = 0; n < length; n++)    {	x[0] = data[n].real;	x[1] = data[n].imag;	if (EdrWriteLong(x, 2, file, longlong) == EOF)	    break;    }    return n;}/* Write items of type SHORT_COMPLEX */static intEdrWriteShortComplex(ShortComplex   *data,		     long	    length,		     FILE	    *file){    long    n;			/* number of items written */    short   x[2];		/* real and imaginary parts */    for (n = 0; n < length; n++)    {	x[0] = data[n].real;	x[1] = data[n].imag;	if (EdrWriteShort(x, 2, file) == EOF)	    break;    }    return n;}/* Write items of type SCHAR_COMPLEX */static intEdrWriteScharComplex(ScharComplex   *data,		     long	    length,		     FILE	    *file){    long    n;			/* number of items written */    Schar   x[2];		/* real and imaginary parts */    for (n = 0; n < length; n++)    {	x[0] = data[n].real;	x[1] = data[n].imag;	if (EdrWriteSchar(x, 2, file) == EOF)	    break;    }    return n;}/* Write items of type CHAR */static intEdrWriteChar(char *data,	     long length,	     FILE *file){    long    n;			/* number of items written */    for (n = 0; n < length; n++)    {	if (putc(data[n], file) == EOF)	    break;    }    return n;}/* Write items of type WCHAR */static intEdrWriteWchar(Wchar	*data,	      long	length,	      FILE	*file){    long	    n;		/* number of items written */    Wchar	    u;		/* one data item for output */    for (n = 0; n < length; n++)    {	u = data[n];	if (u > 0xffffU)	    u = 0xffffU;	if (putc((u >> 8) & 0xff, file) == EOF)	    break;	if (putc(u & 0xff, file) == EOF)	    break;    }    return n;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?