📄 ulaw.c
字号:
/*** Copyright (C) 1999-2000 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 void ulaw2s_array (unsigned char *buffer, unsigned int count, short *ptr, unsigned int index) ;static void ulaw2i_array (unsigned char *buffer, unsigned int count, int *ptr, unsigned int index) ;static void ulaw2d_array (unsigned char *buffer, unsigned int count, double *ptr, unsigned int index, double normfact) ;static void s2ulaw_array (short *buffer, unsigned int count, unsigned char *ptr, unsigned int index) ;static void i2ulaw_array (int *buffer, unsigned int count, unsigned char *ptr, unsigned int index) ;static void d2ulaw_array (double *buffer, unsigned int count, unsigned char *ptr, unsigned int index, double normfact) ;int ulaw_read_ulaw2s (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) ; ulaw2s_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 ;} /* ulaw_read_ulaw2s */int ulaw_read_ulaw2i (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) ; ulaw2i_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 ;} /* ulaw_read_ulaw2i */int ulaw_read_ulaw2d (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) ; ulaw2d_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 ;} /* ulaw_read_ulaw2d *//*=============================================================================================*/int ulaw_write_s2ulaw (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 ; s2ulaw_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 ;} /* ulaw_write_s2ulaw */int ulaw_write_i2ulaw (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 ; i2ulaw_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 ;} /* ulaw_write_i2ulaw */int ulaw_write_d2ulaw (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 ; d2ulaw_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 ;} /* ulaw_write_d2ulaw *//*============================================================================================= * Private static functions and data. */static short ulaw_decode [128] ={ -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956, -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764, -15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412, -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316, -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140, -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092, -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004, -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980, -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436, -1372, -1308, -1244, -1180, -1116, -1052, -988, -924, -876, -844, -812, -780, -748, -716, -684, -652, -620, -588, -556, -524, -492, -460, -428, -396, -372, -356, -340, -324, -308, -292, -276, -260, -244, -228, -212, -196, -180, -164, -148, -132, -120, -112, -104, -96, -88, -80, -72, -64, -56, -48, -40, -32, -24, -16, -8, 0, } ;static unsigned char ulaw_encode [8193] ={ 0xFF, 0xFE, 0xFE, 0xFD, 0xFD, 0xFC, 0xFC, 0xFB, 0xFB, 0xFA, 0xFA, 0xF9, 0xF9, 0xF8, 0xF8, 0xF7, 0xF7, 0xF6, 0xF6, 0xF5, 0xF5, 0xF4, 0xF4, 0xF3, 0xF3, 0xF2, 0xF2, 0xF1, 0xF1, 0xF0, 0xF0, 0xEF, 0xEF, 0xEF, 0xEF, 0xEE, 0xEE, 0xEE, 0xEE, 0xED, 0xED, 0xED, 0xED, 0xEC, 0xEC, 0xEC, 0xEC, 0xEB, 0xEB, 0xEB, 0xEB, 0xEA, 0xEA, 0xEA, 0xEA, 0xE9, 0xE9, 0xE9, 0xE9, 0xE8, 0xE8, 0xE8, 0xE8, 0xE7, 0xE7, 0xE7, 0xE7, 0xE6, 0xE6, 0xE6, 0xE6, 0xE5, 0xE5, 0xE5, 0xE5, 0xE4, 0xE4, 0xE4, 0xE4, 0xE3, 0xE3, 0xE3, 0xE3, 0xE2, 0xE2, 0xE2, 0xE2, 0xE1, 0xE1, 0xE1, 0xE1, 0xE0, 0xE0, 0xE0, 0xE0, 0xDF, 0xDF, 0xDF, 0xDF, 0xDF, 0xDF, 0xDF, 0xDF, 0xDE, 0xDE, 0xDE, 0xDE, 0x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