wav_ima_adpcm.c
来自「LastWave」· C语言 代码 · 共 624 行 · 第 1/2 页
C
624 行
return ((off_t) -1) ; } ; newblock = offset / pima->samplesperblock ; newsample = offset % pima->samplesperblock ; break ; case SEEK_CUR : if (psf->current + offset < 0 || psf->current + offset > pima->blocks * pima->samplesperblock) { psf->error = SFE_BAD_SEEK ; return ((off_t) -1) ; } ; newblock = (psf->current + offset) / pima->samplesperblock ; newsample = (psf->current + offset) % pima->samplesperblock ; break ; case SEEK_END : if (offset > 0 || pima->samplesperblock * pima->blocks + offset < 0) { psf->error = SFE_BAD_SEEK ; return ((off_t) -1) ; } ; newblock = (pima->samplesperblock * pima->blocks + offset) / pima->samplesperblock ; newsample = (pima->samplesperblock * pima->blocks + offset) % pima->samplesperblock ; break ; default : psf->error = SFE_BAD_SEEK ; return ((off_t) -1) ; } ; if (psf->mode == SF_MODE_READ) { fseek (psf->file, (int) (psf->dataoffset + newblock * pima->blocksize), SEEK_SET) ; pima->blockcount = newblock ; __ima_read_block (psf, pima) ; pima->samplecount = newsample ; } else { /* What to do about write??? */ psf->error = SFE_BAD_SEEK ; return ((off_t) -1) ; } ; psf->current = newblock * pima->samplesperblock + newsample ; return psf->current ;} /* __ima_seek *//*==========================================================================================** IMA ADPCM Write Functions.*/int __ima_writer_init (SF_PRIVATE *psf, WAV_FMT *fmt){ IMA_PRIVATE *pima ; unsigned int pimasize ; if (fmt->format != 0x0011) __psf_sprintf (psf, "*** Warning : format tag != WAVE_FORMAT_IMA_ADPCM.\n") ; fmt->ima.blockalign = srate2blocksize (fmt->ima.samplerate) ; fmt->ima.bitwidth = 4 ; fmt->ima.extrabytes = 2 ; fmt->ima.samplesperblock = 2 * (fmt->ima.blockalign - 4 * fmt->ima.channels) / fmt->ima.channels + 1 ; fmt->ima.bytespersec = (fmt->ima.samplerate * fmt->ima.blockalign) / fmt->ima.samplesperblock ; pimasize = sizeof (IMA_PRIVATE) + fmt->ima.blockalign + 3 * fmt->ima.channels * fmt->ima.samplesperblock ; if (! (psf->fdata = malloc (pimasize))) return SFE_MALLOC_FAILED ; pima = (IMA_PRIVATE*) psf->fdata ; memset (pima, 0, pimasize) ; pima->channels = fmt->ima.channels ; pima->blocksize = fmt->ima.blockalign ; pima->samplesperblock = fmt->ima.samplesperblock ; pima->block = (unsigned char*) pima->data ; pima->samples = (short*) (pima->data + fmt->ima.blockalign) ; pima->samplecount = 0 ; return 0 ;} /* __ima_writer_init *//*==========================================================================================*/staticint __ima_write_block (SF_PRIVATE *psf, IMA_PRIVATE *pima){ int chan, k, step, diff, vpdiff, blockindex, index, indexstart ; short bytecode, mask ; /* Write 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->stepindex [chan] ; pima->block [chan*4+3] = 0 ; } ; pima->previous [0] = pima->samples [0] ; pima->previous [1] = pima->samples [1] ; /* 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->stepindex [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->stepindex [chan] += ima_index_adjust [bytecode] ; if (pima->stepindex [chan] < 0) pima->stepindex [chan] = 0 ; else if (pima->stepindex [chan] > 88) pima->stepindex [chan] = 88 ; pima->samples [k] = bytecode ; } ; /* Pack the 4 bit encoded samples. */ blockindex = 4 * pima->channels ; indexstart = pima->channels ; while (blockindex < pima->blocksize) { for (chan = 0 ; chan < pima->channels ; chan++) { index = indexstart + chan ; for (k = 0 ; k < 4 ; k++) { pima->block [blockindex] = pima->samples [index] & 0x0F ; index += pima->channels ; pima->block [blockindex] |= (pima->samples [index] << 4) & 0xF0 ; index += pima->channels ; blockindex ++ ; } ; } ; indexstart += 8 * pima->channels ; } ; /* Write the block to disk. */ if ((k = fwrite (pima->block, 1, pima->blocksize, psf->file)) != pima->blocksize) __psf_sprintf (psf, "*** Warning : short write (%d != %d).\n", k, pima->blocksize) ; memset (pima->samples, 0, pima->samplesperblock * sizeof (short)) ; pima->samplecount = 0 ; return 1 ;} /* __ima_write_block */staticint __ima_write (SF_PRIVATE *psf, IMA_PRIVATE *pima, short *ptr, int len){ int count, total = 0, index = 0 ; while (index < len) { count = (pima->samplesperblock - pima->samplecount) * pima->channels ; if (count > len - index) count = len - index ; memcpy (&(pima->samples [pima->samplecount * pima->channels]), &(ptr [index]), count * sizeof (short)) ; index += count ; pima->samplecount += count / pima->channels ; total = index ; if (pima->samplecount >= pima->samplesperblock) __ima_write_block (psf, pima) ; } ; return total ; } /* __ima_write */int __ima_write_s (SF_PRIVATE *psf, short *ptr, int len){ IMA_PRIVATE *pima ; int total ; if (! psf->fdata) return 0 ; pima = (IMA_PRIVATE*) psf->fdata ; total = __ima_write (psf, pima, ptr, len) ; return total ;} /* __ima_write_s */int __ima_write_i (SF_PRIVATE *psf, int *ptr, int len){ IMA_PRIVATE *pima ; short *sptr ; int k, bufferlen, writecount = 0, count ; int index = 0, total = 0 ; if (! psf->fdata) return 0 ; pima = (IMA_PRIVATE*) psf->fdata ; sptr = (short*) psf->buffer ; bufferlen = ((SF_BUFFER_LEN / psf->blockwidth) * psf->blockwidth) / sizeof (short) ; while (len > 0) { writecount = (len >= bufferlen) ? bufferlen : len ; for (k = 0 ; k < writecount ; k++) sptr [k] = (short) ptr [index+k] ; count = __ima_write (psf, pima, sptr, writecount) ; index += writecount ; total += count ; len -= writecount ; } ; return total ;} /* __ima_write_i */int __ima_write_d (SF_PRIVATE *psf, double *ptr, int len, int normalize){ IMA_PRIVATE *pima ; short *sptr ; int k, bufferlen, writecount = 0, count ; int index = 0, total = 0 ; if (! psf->fdata) return 0 ; pima = (IMA_PRIVATE*) psf->fdata ; sptr = (short*) psf->buffer ; bufferlen = ((SF_BUFFER_LEN / psf->blockwidth) * psf->blockwidth) / sizeof (short) ; while (len > 0) { writecount = (len >= bufferlen) ? bufferlen : len ; for (k = 0 ; k < writecount ; k++) sptr [k] = (short) ptr [index+k] ; count = __ima_write (psf, pima, sptr, writecount) ; index += writecount ; total += count ; len -= writecount ; } ; return total ;} /* __ima_write_d */int __ima_close (SF_PRIVATE *psf){ IMA_PRIVATE *pima ; unsigned int dword ; if (! psf->fdata) return __wav_close (psf) ; pima = (IMA_PRIVATE*) psf->fdata ; if (psf->mode == SF_MODE_WRITE) { /* If a block has been partially assembled, write it out ** as the final block. */ if (pima->samplecount && pima->samplecount < pima->samplesperblock) __ima_write_block (psf, pima) ; /* Now we know for certain the length of the file we can ** re-write correct values for the RIFF and data chunks. */ fseek (psf->file, 0, SEEK_END) ; psf->filelength = ftell (psf->file) ; /* Fix RIFF size. */ dword = H2LE_INT (psf->filelength - 2 * sizeof (dword)) ; fseek (psf->file, sizeof (dword), SEEK_SET) ; fwrite (&dword, sizeof (dword), 1, psf->file) ; psf->datalength = psf->filelength - psf->dataoffset ; fseek (psf->file, (int) (psf->dataoffset - sizeof (dword)), SEEK_SET) ; dword = H2LE_INT (psf->datalength) ; fwrite (&dword, sizeof (dword), 1, psf->file) ; } ; if (psf->fdata) free (psf->fdata) ; psf->fdata = NULL ; return 0 ;} /* __ima_close */static unsigned int srate2blocksize (unsigned int srate){ if (srate < 12000) return 256 ; if (srate < 23000) return 512 ; return 1024 ;} /* srate2blocksize */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?