📄 alaw.c
字号:
/*** Copyright (C) 1999-2001 Erik de Castro Lopo <erikd@zip.com.au>** ** This program is free software; you can redistribute it and/or modify** it under the terms of the GNU Lesser General Public License as published by** the Free Software Foundation; either version 2.1 of the License, or** (at your option) any later version.** ** This program is distributed in the hope that it will be useful,** but WITHOUT ANY WARRANTY; without even the implied warranty of** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the** GNU Lesser General Public License for more details.** ** You should have received a copy of the GNU Lesser General Public License** along with this program; if not, write to the Free Software ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/#include <unistd.h>#include "sndfile.h"#include "common.h"static int alaw_read_alaw2s (SF_PRIVATE *psf, short *ptr, unsigned int len) ;static int alaw_read_alaw2i (SF_PRIVATE *psf, int *ptr, unsigned int len) ;static int alaw_read_alaw2f (SF_PRIVATE *psf, float *ptr, unsigned int len) ;static int alaw_read_alaw2d (SF_PRIVATE *psf, double *ptr, unsigned int len, int normalize) ;static int alaw_write_s2alaw (SF_PRIVATE *psf, short *ptr, unsigned int len) ;static int alaw_write_i2alaw (SF_PRIVATE *psf, int *ptr, unsigned int len) ;static int alaw_write_f2alaw (SF_PRIVATE *psf, float *ptr, unsigned int len) ;static int alaw_write_d2alaw (SF_PRIVATE *psf, double *ptr, unsigned int len, int normalize) ;static void alaw2s_array (unsigned char *buffer, unsigned int count, short *ptr, unsigned int index) ;static void alaw2i_array (unsigned char *buffer, unsigned int count, int *ptr, unsigned int index) ;static void alaw2f_array (unsigned char *buffer, unsigned int count, float *ptr, unsigned int index, float normfact) ;static void alaw2d_array (unsigned char *buffer, unsigned int count, double *ptr, unsigned int index, double normfact) ;static void s2alaw_array (short *buffer, unsigned int count, unsigned char *ptr, unsigned int index) ;static void i2alaw_array (int *buffer, unsigned int count, unsigned char *ptr, unsigned int index) ;static void f2alaw_array (float *buffer, unsigned int count, unsigned char *ptr, unsigned int index, float normfact) ;static void d2alaw_array (double *buffer, unsigned int count, unsigned char *ptr, unsigned int index, double normfact) ;intalaw_read_init (SF_PRIVATE *psf){ psf->read_short = (func_short) alaw_read_alaw2s ; psf->read_int = (func_int) alaw_read_alaw2i ; psf->read_float = (func_float) alaw_read_alaw2f ; psf->read_double = (func_double) alaw_read_alaw2d ; return 0 ;} /* alaw_read_init */intalaw_write_init (SF_PRIVATE *psf){ psf->write_short = (func_short) alaw_write_s2alaw ; psf->write_int = (func_int) alaw_write_i2alaw ; psf->write_float = (func_float) alaw_write_f2alaw ; psf->write_double = (func_double) alaw_write_d2alaw ; return 0 ;} /* alaw_read_init */static intalaw_read_alaw2s (SF_PRIVATE *psf, short *ptr, unsigned int len){ unsigned int readcount, thisread, index = 0 ; int bytecount, bufferlen ; int total = 0 ; bufferlen = (SF_BUFFER_LEN / psf->blockwidth) * psf->blockwidth ; bytecount = len * psf->bytewidth ; while (bytecount > 0) { readcount = (bytecount >= bufferlen) ? bufferlen : bytecount ; thisread = fread (psf->buffer, 1, readcount, psf->file) ; alaw2s_array ((unsigned char*) (psf->buffer), thisread / psf->bytewidth, ptr, index) ; total += thisread ; if (thisread < readcount) break ; index += thisread / psf->bytewidth ; bytecount -= thisread ; } ; total /= psf->bytewidth ; if (total < len) psf->error = SFE_SHORT_READ ; return total ;} /* alaw_read_alaw2s */static intalaw_read_alaw2i (SF_PRIVATE *psf, int *ptr, unsigned int len){ unsigned int readcount, thisread, index = 0 ; int bytecount, bufferlen ; int total = 0 ; bufferlen = (SF_BUFFER_LEN / psf->blockwidth) * psf->blockwidth ; bytecount = len * psf->bytewidth ; while (bytecount > 0) { readcount = (bytecount >= bufferlen) ? bufferlen : bytecount ; thisread = fread (psf->buffer, 1, readcount, psf->file) ; alaw2i_array ((unsigned char*) (psf->buffer), thisread / psf->bytewidth, ptr, index) ; total += thisread ; if (thisread < readcount) break ; index += thisread / psf->bytewidth ; bytecount -= thisread ; } ; total /= psf->bytewidth ; if (total < len) psf->error = SFE_SHORT_READ ; return total ;} /* alaw_read_alaw2i */static intalaw_read_alaw2f (SF_PRIVATE *psf, float *ptr, unsigned int len){ unsigned int readcount, thisread, index = 0 ; int bytecount, bufferlen ; int total = 0 ; bufferlen = (SF_BUFFER_LEN / psf->blockwidth) * psf->blockwidth ; bytecount = len * psf->bytewidth ; while (bytecount > 0) { readcount = (bytecount >= bufferlen) ? bufferlen : bytecount ; thisread = fread (psf->buffer, 1, readcount, psf->file) ; alaw2f_array ((unsigned char*) (psf->buffer), thisread / psf->bytewidth, ptr, index, 1.0) ; total += thisread ; if (thisread < readcount) break ; index += thisread / psf->bytewidth ; bytecount -= thisread ; } ; total /= psf->bytewidth ; if (total < len) psf->error = SFE_SHORT_READ ; return total ;} /* alaw_read_alaw2f */static intalaw_read_alaw2d (SF_PRIVATE *psf, double *ptr, unsigned int len, int normalize){ unsigned int readcount, thisread, index = 0 ; int bytecount, bufferlen ; int total = 0 ; double normfact ; normfact = (normalize ? 1.0 / ((double) 0x8000) : 1.0) ; bufferlen = (SF_BUFFER_LEN / psf->blockwidth) * psf->blockwidth ; bytecount = len * psf->bytewidth ; while (bytecount > 0) { readcount = (bytecount >= bufferlen) ? bufferlen : bytecount ; thisread = fread (psf->buffer, 1, readcount, psf->file) ; alaw2d_array ((unsigned char*) (psf->buffer), thisread / psf->bytewidth, ptr, index, normfact) ; total += thisread ; if (thisread < readcount) break ; index += thisread / psf->bytewidth ; bytecount -= thisread ; } ; total /= psf->bytewidth ; if (total < len) psf->error = SFE_SHORT_READ ; return total ;} /* alaw_read_alaw2d *//*=============================================================================================*/static intalaw_write_s2alaw (SF_PRIVATE *psf, short *ptr, unsigned int len){ unsigned int writecount, thiswrite, index = 0 ; int bytecount, bufferlen ; int total = 0 ; bufferlen = (SF_BUFFER_LEN / psf->blockwidth) * psf->blockwidth ; bytecount = len * psf->bytewidth ; while (bytecount > 0) { writecount = (bytecount >= bufferlen) ? bufferlen : bytecount ; s2alaw_array (ptr, index, (unsigned char*) (psf->buffer), writecount / psf->bytewidth) ; thiswrite = fwrite (psf->buffer, 1, writecount, psf->file) ; total += thiswrite ; if (thiswrite < writecount) break ; index += thiswrite / psf->bytewidth ; bytecount -= thiswrite ; } ; total /= psf->bytewidth ; if (total < len) psf->error = SFE_SHORT_READ ; return total ;} /* alaw_write_s2alaw */static intalaw_write_i2alaw (SF_PRIVATE *psf, int *ptr, unsigned int len){ unsigned int writecount, thiswrite, index = 0 ; int bytecount, bufferlen ; int total = 0 ; bufferlen = (SF_BUFFER_LEN / psf->blockwidth) * psf->blockwidth ; bytecount = len * psf->bytewidth ; while (bytecount > 0) { writecount = (bytecount >= bufferlen) ? bufferlen : bytecount ; i2alaw_array (ptr, index, (unsigned char*) (psf->buffer), writecount / psf->bytewidth) ; thiswrite = fwrite (psf->buffer, 1, writecount, psf->file) ; total += thiswrite ; if (thiswrite < writecount) break ; index += thiswrite / psf->bytewidth ; bytecount -= thiswrite ; } ; total /= psf->bytewidth ; if (total < len) psf->error = SFE_SHORT_READ ; return total ;} /* alaw_write_i2alaw */static intalaw_write_f2alaw (SF_PRIVATE *psf, float *ptr, unsigned int len){ unsigned int writecount, thiswrite, index = 0 ; int bytecount, bufferlen ; int total = 0 ; bufferlen = (SF_BUFFER_LEN / psf->blockwidth) * psf->blockwidth ; bytecount = len * psf->bytewidth ; while (bytecount > 0) { writecount = (bytecount >= bufferlen) ? bufferlen : bytecount ; f2alaw_array (ptr, index, (unsigned char*) (psf->buffer), writecount / psf->bytewidth, 1.0) ; thiswrite = fwrite (psf->buffer, 1, writecount, psf->file) ; total += thiswrite ; if (thiswrite < writecount) break ; index += thiswrite / psf->bytewidth ; bytecount -= thiswrite ; } ; total /= psf->bytewidth ; if (total < len) psf->error = SFE_SHORT_READ ; return total ;} /* alaw_write_f2alaw */static intalaw_write_d2alaw (SF_PRIVATE *psf, double *ptr, unsigned int len, int normalize){ unsigned int writecount, thiswrite, index = 0 ; int bytecount, bufferlen ; int total = 0 ; double normfact ; normfact = (normalize ? ((double) 0x8000) : 1.0) ; bufferlen = (SF_BUFFER_LEN / psf->blockwidth) * psf->blockwidth ; bytecount = len * psf->bytewidth ; while (bytecount > 0) { writecount = (bytecount >= bufferlen) ? bufferlen : bytecount ; d2alaw_array (ptr, index, (unsigned char*) (psf->buffer), writecount / psf->bytewidth, normfact) ; thiswrite = fwrite (psf->buffer, 1, writecount, psf->file) ; total += thiswrite ; if (thiswrite < writecount) break ; index += thiswrite / psf->bytewidth ; bytecount -= thiswrite ; } ; total /= psf->bytewidth ; if (total < len) psf->error = SFE_SHORT_READ ; return total ;} /* alaw_write_d2alaw *//*============================================================================================= * Private static functions and data. */static short alaw_decode [128] ={ -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736, -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784, -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -