📄 ima_adpcm.c
字号:
current = pima->samples [k - pima->channels] ; diff = step >> 3 ; if (bytecode & 1) diff += step >> 2 ; if (bytecode & 2) diff += step >> 1 ; if (bytecode & 4) diff += step ; if (bytecode & 8) diff = -diff ; current += diff ; if (current > 32767) current = 32767 ; else if (current < -32768) current = -32768 ; stepindx [chan] += ima_indx_adjust [bytecode] ; if (stepindx [chan] < 0) stepindx [chan] = 0 ; else if (stepindx [chan] > 88) stepindx [chan] = 88 ; pima->samples [k] = current ; } ; return 1 ;} /* wav_w64_ima_decode_block */static intwav_w64_ima_encode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima){ int chan, k, step, diff, vpdiff, blockindx, indx, indxstart ; short bytecode, mask ; /* Encode the block header. */ for (chan = 0 ; chan < pima->channels ; chan++) { pima->block [chan*4] = pima->samples [chan] & 0xFF ; pima->block [chan*4+1] = (pima->samples [chan] >> 8) & 0xFF ; pima->block [chan*4+2] = pima->stepindx [chan] ; pima->block [chan*4+3] = 0 ; pima->previous [chan] = pima->samples [chan] ; } ; /* Encode the samples as 4 bit. */ for (k = pima->channels ; k < (pima->samplesperblock * pima->channels) ; k ++) { chan = (pima->channels > 1) ? (k % 2) : 0 ; diff = pima->samples [k] - pima->previous [chan] ; bytecode = 0 ; step = ima_step_size [pima->stepindx [chan]] ; vpdiff = step >> 3 ; if (diff < 0) { bytecode = 8 ; diff = -diff ; } ; mask = 4 ; while (mask) { if (diff >= step) { bytecode |= mask ; diff -= step ; vpdiff += step ; } ; step >>= 1 ; mask >>= 1 ; } ; if (bytecode & 8) pima->previous [chan] -= vpdiff ; else pima->previous [chan] += vpdiff ; if (pima->previous [chan] > 32767) pima->previous [chan] = 32767 ; else if (pima->previous [chan] < -32768) pima->previous [chan] = -32768 ; pima->stepindx [chan] += ima_indx_adjust [bytecode] ; if (pima->stepindx [chan] < 0) pima->stepindx [chan] = 0 ; else if (pima->stepindx [chan] > 88) pima->stepindx [chan] = 88 ; pima->samples [k] = bytecode ; } ; /* Pack the 4 bit encoded samples. */ blockindx = 4 * pima->channels ; indxstart = pima->channels ; while (blockindx < pima->blocksize) { for (chan = 0 ; chan < pima->channels ; chan++) { indx = indxstart + chan ; for (k = 0 ; k < 4 ; k++) { pima->block [blockindx] = pima->samples [indx] & 0x0F ; indx += pima->channels ; pima->block [blockindx] |= (pima->samples [indx] << 4) & 0xF0 ; indx += pima->channels ; blockindx ++ ; } ; } ; indxstart += 8 * pima->channels ; } ; /* Write the block to disk. */ if ((k = psf_fwrite (pima->block, 1, pima->blocksize, psf)) != pima->blocksize) psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, pima->blocksize) ; memset (pima->samples, 0, pima->samplesperblock * sizeof (short)) ; pima->samplecount = 0 ; pima->blockcount ++ ; return 1 ;} /* wav_w64_ima_encode_block */static intima_read_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima, short *ptr, int len){ int count, total = 0, indx = 0 ; while (indx < len) { if (pima->blockcount >= pima->blocks && pima->samplecount >= pima->samplesperblock) { memset (&(ptr [indx]), 0, (size_t) ((len - indx) * sizeof (short))) ; return total ; } ; if (pima->samplecount >= pima->samplesperblock) pima->decode_block (psf, pima) ; count = (pima->samplesperblock - pima->samplecount) * pima->channels ; count = (len - indx > count) ? count : len - indx ; memcpy (&(ptr [indx]), &(pima->samples [pima->samplecount * pima->channels]), count * sizeof (short)) ; indx += count ; pima->samplecount += count / pima->channels ; total = indx ; } ; return total ;} /* ima_read_block */static sf_count_tima_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len){ IMA_ADPCM_PRIVATE *pima ; int readcount, count ; sf_count_t total = 0 ; if (! psf->fdata) return 0 ; pima = (IMA_ADPCM_PRIVATE*) psf->fdata ; while (len > 0) { readcount = (len > 0x10000000) ? 0x10000000 : (int) len ; count = ima_read_block (psf, pima, ptr, readcount) ; total += count ; len -= count ; if (count != readcount) break ; } ; return total ;} /* ima_read_s */static sf_count_tima_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len){ IMA_ADPCM_PRIVATE *pima ; short *sptr ; int k, bufferlen, readcount, count ; sf_count_t total = 0 ; if (! psf->fdata) return 0 ; pima = (IMA_ADPCM_PRIVATE*) psf->fdata ; sptr = psf->u.sbuf ; bufferlen = ARRAY_LEN (psf->u.sbuf) ; while (len > 0) { readcount = (len >= bufferlen) ? bufferlen : (int) len ; count = ima_read_block (psf, pima, sptr, readcount) ; for (k = 0 ; k < readcount ; k++) ptr [total + k] = ((int) sptr [k]) << 16 ; total += count ; len -= readcount ; if (count != readcount) break ; } ; return total ;} /* ima_read_i */static sf_count_tima_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len){ IMA_ADPCM_PRIVATE *pima ; short *sptr ; int k, bufferlen, readcount, count ; sf_count_t total = 0 ; float normfact ; if (! psf->fdata) return 0 ; pima = (IMA_ADPCM_PRIVATE*) psf->fdata ; normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ; sptr = psf->u.sbuf ; bufferlen = ARRAY_LEN (psf->u.sbuf) ; while (len > 0) { readcount = (len >= bufferlen) ? bufferlen : (int) len ; count = ima_read_block (psf, pima, sptr, readcount) ; for (k = 0 ; k < readcount ; k++) ptr [total + k] = normfact * (float) (sptr [k]) ; total += count ; len -= readcount ; if (count != readcount) break ; } ; return total ;} /* ima_read_f */static sf_count_tima_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len){ IMA_ADPCM_PRIVATE *pima ; short *sptr ; int k, bufferlen, readcount, count ; sf_count_t total = 0 ; double normfact ; if (! psf->fdata) return 0 ; pima = (IMA_ADPCM_PRIVATE*) psf->fdata ; normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ; sptr = psf->u.sbuf ; bufferlen = ARRAY_LEN (psf->u.sbuf) ; while (len > 0) { readcount = (len >= bufferlen) ? bufferlen : (int) len ; count = ima_read_block (psf, pima, sptr, readcount) ; for (k = 0 ; k < readcount ; k++) ptr [total + k] = normfact * (double) (sptr [k]) ; total += count ; len -= readcount ; if (count != readcount) break ; } ; return total ;} /* ima_read_d */static sf_count_tima_seek (SF_PRIVATE *psf, int mode, sf_count_t offset){ IMA_ADPCM_PRIVATE *pima ; int newblock, newsample ; if (! psf->fdata) return 0 ; pima = (IMA_ADPCM_PRIVATE*) psf->fdata ; if (psf->datalength < 0 || psf->dataoffset < 0) { psf->error = SFE_BAD_SEEK ; return ((sf_count_t) -1) ; } ; if (offset == 0) { psf_fseek (psf, psf->dataoffset, SEEK_SET) ; pima->blockcount = 0 ; pima->decode_block (psf, pima) ; pima->samplecount = 0 ; return 0 ; } ; if (offset < 0 || offset > pima->blocks * pima->samplesperblock) { psf->error = SFE_BAD_SEEK ; return ((sf_count_t) -1) ; } ; newblock = offset / pima->samplesperblock ; newsample = offset % pima->samplesperblock ; if (mode == SFM_READ) { psf_fseek (psf, psf->dataoffset + newblock * pima->blocksize, SEEK_SET) ; pima->blockcount = newblock ; pima->decode_block (psf, pima) ; pima->samplecount = newsample ; } else { /* What to do about write??? */ psf->error = SFE_BAD_SEEK ; return ((sf_count_t) -1) ; } ; return newblock * pima->samplesperblock + newsample ;} /* ima_seek *//*==========================================================================================** IMA ADPCM Write Functions.*/static intima_writer_init (SF_PRIVATE *psf, int blockalign){ IMA_ADPCM_PRIVATE *pima ; int samplesperblock ; unsigned int pimasize ; if (psf->mode != SFM_WRITE) return SFE_BAD_MODE_RW ; samplesperblock = 2 * (blockalign - 4 * psf->sf.channels) / psf->sf.channels + 1 ; pimasize = sizeof (IMA_ADPCM_PRIVATE) + blockalign + 3 * psf->sf.channels * samplesperblock ; if ((pima = calloc (1, pimasize)) == NULL) return SFE_MALLOC_FAILED ; psf->fdata = (void*) pima ; pima->channels = psf->sf.channels ; pima->blocksize = blockalign ; pima->samplesperblock = samplesperblock ; pima->block = (unsigned char*) pima->data ; pima->samples = (short*) (pima->data + blockalign) ; pima->samplecount = 0 ; switch (psf->sf.format & SF_FORMAT_TYPEMASK) { case SF_FORMAT_WAV : case SF_FORMAT_W64 : pima->encode_block = wav_w64_ima_encode_block ; break ; case SF_FORMAT_AIFF : pima->encode_block = aiff_ima_encode_block ; break ; default : psf_log_printf (psf, "ima_reader_init: bad psf->sf.format\n") ; return SFE_INTERNAL ; break ; } ; psf->write_short = ima_write_s ; psf->write_int = ima_write_i ; psf->write_float = ima_write_f ; psf->write_double = ima_write_d ; return 0 ;} /* ima_writer_init *//*==========================================================================================*/static intima_write_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima, short *ptr, int len){ int count, total = 0, indx = 0 ; while (indx < len) { count = (pima->samplesperblock - pima->samplecount) * pima->channels ; if (count > len - indx) count = len - indx ; memcpy (&(pima->samples [pima->samplecount * pima->channels]), &(ptr [total]), count * sizeof (short)) ; indx += count ; pima->samplecount += count / pima->channels ; total = indx ; if (pima->samplecount >= pima->samplesperblock) pima->encode_block (psf, pima) ; } ; return total ;} /* ima_write_block */static sf_count_tima_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len){ IMA_ADPCM_PRIVATE *pima ; int writecount, count ; sf_count_t total = 0 ; if (! psf->fdata) return 0 ; pima = (IMA_ADPCM_PRIVATE*) psf->fdata ; while (len) { writecount = (len > 0x10000000) ? 0x10000000 : (int) len ; count = ima_write_block (psf, pima, ptr, writecount) ; total += count ; len -= count ; if (count != writecount) break ; } ; return total ;} /* ima_write_s */static sf_count_tima_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len){ IMA_ADPCM_PRIVATE *pima ; short *sptr ; int k, bufferlen, writecount, count ; sf_count_t total = 0 ; if (! psf->fdata) return 0 ; pima = (IMA_ADPCM_PRIVATE*) psf->fdata ; sptr = psf->u.sbuf ; bufferlen = ARRAY_LEN (psf->u.sbuf) ; while (len > 0) { writecount = (len >= bufferlen) ? bufferlen : (int) len ; for (k = 0 ; k < writecount ; k++) sptr [k] = ptr [total + k] >> 16 ; count = ima_write_block (psf, pima, sptr, writecount) ; total += count ; len -= writecount ; if (count != writecount) break ; } ; return total ;} /* ima_write_i */static sf_count_tima_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len){ IMA_ADPCM_PRIVATE *pima ; short *sptr ; int k, bufferlen, writecount, count ; sf_count_t total = 0 ; float normfact ; if (! psf->fdata) return 0 ; pima = (IMA_ADPCM_PRIVATE*) psf->fdata ; normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ; sptr = psf->u.sbuf ; bufferlen = ARRAY_LEN (psf->u.sbuf) ; while (len > 0) { writecount = (len >= bufferlen) ? bufferlen : (int) len ; for (k = 0 ; k < writecount ; k++) sptr [k] = lrintf (normfact * ptr [total + k]) ; count = ima_write_block (psf, pima, sptr, writecount) ; total += count ; len -= writecount ; if (count != writecount) break ; } ; return total ;} /* ima_write_f */static sf_count_tima_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len){ IMA_ADPCM_PRIVATE *pima ; short *sptr ; int k, bufferlen, writecount, count ; sf_count_t total = 0 ; double normfact ; if (! psf->fdata) return 0 ; pima = (IMA_ADPCM_PRIVATE*) psf->fdata ; normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ; sptr = psf->u.sbuf ; bufferlen = ARRAY_LEN (psf->u.sbuf) ; while (len > 0) { writecount = (len >= bufferlen) ? bufferlen : (int) len ; for (k = 0 ; k < writecount ; k++) sptr [k] = lrint (normfact * ptr [total + k]) ; count = ima_write_block (psf, pima, sptr, writecount) ; total += count ; len -= writecount ; if (count != writecount) break ; } ; return total ;} /* ima_write_d *//*** Do not edit or modify anything in this comment block.** The arch-tag line is a file identity tag for the GNU Arch ** revision control system.**** arch-tag: 75a54b82-ad18-4758-9933-64e00a7f24e0*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -