⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wav_gsm610.c

📁 SIP 1.5.0源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*** 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	<stdio.h>#include	<unistd.h>#include	<string.h>#include	<math.h>#include	"sndfile.h"#include	"config.h"#include	"sfendian.h"#include	"common.h"#include	"wav.h"#include	"GSM610/gsm.h"#if (CPU_IS_LITTLE_ENDIAN == 1)#	define	MAKE_MARKER(a,b,c,d)		((a)|((b)<<8)|((c)<<16)|((d)<<24))#elif (CPU_IS_BIG_ENDIAN == 1)#	define	MAKE_MARKER(a,b,c,d)		(((a)<<24)|((b)<<16)|((c)<<8)|(d))#else#	error "Cannot determine endian-ness of processor."#endif#define RIFF_MARKER	(MAKE_MARKER ('R', 'I', 'F', 'F')) #define WAVE_MARKER	(MAKE_MARKER ('W', 'A', 'V', 'E')) #define fmt_MARKER	(MAKE_MARKER ('f', 'm', 't', ' ')) #define fact_MARKER	(MAKE_MARKER ('f', 'a', 'c', 't')) #define data_MARKER	(MAKE_MARKER ('d', 'a', 't', 'a')) #define 	WAVE_FORMAT_GSM610	0x0031#define		GSM610_BLOCKSIZE	65#define		GSM610_SAMPLES		320typedef struct{	unsigned int	blocks ; 	int				blockcount, samplecount ;	unsigned char	block [GSM610_BLOCKSIZE] ;	short			samples [GSM610_SAMPLES] ;	gsm				gsm_data ;} GSM610_PRIVATE ;static	int	wav_gsm610_read_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610) ;static	int wav_gsm610_read (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610, short *ptr, int len) ;static	int	wav_gsm610_write_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610) ;static	int wav_gsm610_write (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610, short *ptr, int len) ;static off_t  wav_gsm610_seek   (SF_PRIVATE *psf, off_t offset, int whence) ;static int	wav_gsm610_read_s (SF_PRIVATE *psf, short *ptr, int len) ;static int	wav_gsm610_read_i (SF_PRIVATE *psf, int *ptr, int len) ;static int	wav_gsm610_read_f (SF_PRIVATE *psf, float *ptr, int len) ;static int	wav_gsm610_read_d (SF_PRIVATE *psf, double *ptr, int len, int normalize) ;static int	wav_gsm610_write_s (SF_PRIVATE *psf, short *ptr, int len) ;static int	wav_gsm610_write_i (SF_PRIVATE *psf, int *ptr, int len) ;static int	wav_gsm610_write_f (SF_PRIVATE *psf, float *ptr, int len) ;static int	wav_gsm610_write_d (SF_PRIVATE *psf, double *ptr, int len, int normalize) ;static int	wav_gsm610_close	(SF_PRIVATE  *psf) ;static int	wav_gsm610_write_header (SF_PRIVATE  *psf) ;/*============================================================================================** WAV GSM610 Reader initialisation function.*/int	wav_gsm610_reader_init (SF_PRIVATE *psf, WAV_FMT *fmt){	GSM610_PRIVATE	*pgsm610 ;	int  true = 1 ;		psf->sf.seekable = SF_FALSE ;	/*-psf->sf.seekable = SF_TRUE ;		-*/	psf->seek_func = (func_seek) wav_gsm610_seek ;	psf->read_short  = (func_short)  wav_gsm610_read_s ;	psf->read_int    = (func_int)    wav_gsm610_read_i ;	psf->read_float  = (func_float)  wav_gsm610_read_f ;	psf->read_double = (func_double) wav_gsm610_read_d ;	if (psf->mode != SF_MODE_READ)		return SFE_BAD_MODE_RW ;	if (! (pgsm610 = malloc (sizeof (GSM610_PRIVATE))))		return SFE_MALLOC_FAILED ;	psf->fdata = (void*) pgsm610 ;	memset (pgsm610, 0, sizeof (GSM610_PRIVATE)) ;	if (! (pgsm610->gsm_data = gsm_create ()))		return SFE_MALLOC_FAILED ;	gsm_option (pgsm610->gsm_data,  GSM_OPT_WAV49, &true) ;	if (psf->datalength % GSM610_BLOCKSIZE)	{	psf_log_printf (psf, "*** Warning : data chunk seems to be truncated.\n") ;		pgsm610->blocks = psf->datalength / GSM610_BLOCKSIZE + 1 ;		}	else		pgsm610->blocks = psf->datalength / GSM610_BLOCKSIZE ;		psf->sf.samples = GSM610_SAMPLES * pgsm610->blocks ;		wav_gsm610_read_block (psf, pgsm610) ;	/* Read first block. */		return 0 ;	} /* wav_gsm610_reader_init *//*============================================================================================** WAV GSM610 writer initialisation function.*/int	wav_gsm610_writer_init (SF_PRIVATE *psf){	GSM610_PRIVATE	*pgsm610 ;	int  true = 1, samplesperblock, bytespersec ;		if (psf->mode != SF_MODE_WRITE)		return SFE_BAD_MODE_RW ;	if (! (pgsm610 = malloc (sizeof (GSM610_PRIVATE))))		return SFE_MALLOC_FAILED ;			psf->fdata = (void*) pgsm610 ;	memset (pgsm610, 0, sizeof (GSM610_PRIVATE)) ;		if (! (pgsm610->gsm_data = gsm_create ()))		return SFE_MALLOC_FAILED ;	gsm_option (pgsm610->gsm_data,  GSM_OPT_WAV49, &true) ;	pgsm610->blockcount  = 0 ;	pgsm610->samplecount = 0 ;		samplesperblock = GSM610_SAMPLES ;	bytespersec     = psf->sf.samplerate * GSM610_BLOCKSIZE / GSM610_SAMPLES ;		if (bytespersec * GSM610_SAMPLES / GSM610_BLOCKSIZE < psf->sf.samplerate) 		bytespersec ++ ;	psf->datalength  = (psf->sf.samples / samplesperblock) * samplesperblock ;	if (psf->sf.samples % samplesperblock)		psf->datalength += samplesperblock ;	wav_gsm610_write_header (psf) ;	psf->write_short  = (func_short)   wav_gsm610_write_s ;	psf->write_int    = (func_int)     wav_gsm610_write_i ;	psf->write_float  = (func_float)   wav_gsm610_write_f ;	psf->write_double = (func_double)  wav_gsm610_write_d ;		psf->seek_func    = NULL ; /* Not seekable */	psf->close        = (func_close)   wav_gsm610_close ;	psf->write_header = (func_wr_hdr)  wav_gsm610_write_header ;						return 0 ;} /* wav_gsm610_writer_init *//*============================================================================================** GSM 6.10 Read Functions.*/static int		wav_gsm610_read_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610){	int	k ;		pgsm610->blockcount ++ ;	pgsm610->samplecount = 0 ;		if (pgsm610->blockcount > pgsm610->blocks)	{	memset (pgsm610->samples, 0, GSM610_SAMPLES * sizeof (short)) ;		return 1 ;		} ;	if ((k = fread (pgsm610->block, 1, GSM610_BLOCKSIZE, psf->file)) != GSM610_BLOCKSIZE)		psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, GSM610_BLOCKSIZE) ;	if (gsm_decode (pgsm610->gsm_data, pgsm610->block, pgsm610->samples) < 0)	{	psf_log_printf (psf, "Error from gsm_decode() on frame : %d\n", pgsm610->blockcount) ;		return 0 ;		} ;				if (gsm_decode (pgsm610->gsm_data, pgsm610->block+(GSM610_BLOCKSIZE+1)/2, pgsm610->samples+GSM610_SAMPLES/2) < 0)	{	psf_log_printf (psf, "Error from gsm_decode() on frame : %d.5\n", pgsm610->blockcount) ;		return 0 ;		} ;	return 1 ;} /* wav_gsm610_read_block */static int wav_gsm610_read (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610, short *ptr, int len){	int		count, total = 0, index = 0 ;	while (index < len)	{	if (pgsm610->blockcount >= pgsm610->blocks && pgsm610->samplecount >= GSM610_SAMPLES)		{	memset (&(ptr[index]), 0, (len - index) * sizeof (short)) ;			return total ;			} ;				if (pgsm610->samplecount >= GSM610_SAMPLES)			wav_gsm610_read_block (psf, pgsm610) ;				count = GSM610_SAMPLES - pgsm610->samplecount ;		count = (len - index > count) ? count : len - index ;				memcpy (&(ptr[index]), &(pgsm610->samples [pgsm610->samplecount]), count * sizeof (short)) ;		index += count ;		pgsm610->samplecount += count ;		total = index ;		} ;	return total ;		} /* wav_gsm610_read */static int		wav_gsm610_read_s (SF_PRIVATE *psf, short *ptr, int len){	GSM610_PRIVATE 	*pgsm610 ; 	int				total ;	if (! psf->fdata)		return 0 ;	pgsm610 = (GSM610_PRIVATE*) psf->fdata ;		total = wav_gsm610_read (psf, pgsm610, ptr, len) ;	return total ;} /* wav_gsm610_read_s */static int		wav_gsm610_read_i  (SF_PRIVATE *psf, int *ptr, int len){	GSM610_PRIVATE *pgsm610 ; 	short		*sptr ;	int			k, bufferlen, readcount = 0, count ;	int			index = 0, total = 0 ;	if (! psf->fdata)		return 0 ;	pgsm610 = (GSM610_PRIVATE*) psf->fdata ;		sptr = (short*) psf->buffer ;	bufferlen = ((SF_BUFFER_LEN / psf->blockwidth) * psf->blockwidth) / sizeof (short) ;	while (len > 0)	{	readcount = (len >= bufferlen) ? bufferlen : len ;		count = wav_gsm610_read (psf, pgsm610, sptr, readcount) ;		for (k = 0 ; k < readcount ; k++)			ptr [index+k] = (int) (sptr [k]) ;		index += readcount ;		total += count ;		len -= readcount ;		} ;	return total ;} /* wav_gsm610_read_i */static int		wav_gsm610_read_f  (SF_PRIVATE *psf, float *ptr, int len){	GSM610_PRIVATE *pgsm610 ; 	short		*sptr ;	int			k, bufferlen, readcount = 0, count ;	int			index = 0, total = 0 ;	if (! psf->fdata)		return 0 ;	pgsm610 = (GSM610_PRIVATE*) psf->fdata ;		sptr = (short*) psf->buffer ;	bufferlen = ((SF_BUFFER_LEN / psf->blockwidth) * psf->blockwidth) / sizeof (short) ;	while (len > 0)	{	readcount = (len >= bufferlen) ? bufferlen : len ;		count = wav_gsm610_read (psf, pgsm610, sptr, readcount) ;		for (k = 0 ; k < readcount ; k++)			ptr [index+k] = 1.0 * (float) (sptr [k]) ;		index += readcount ;		total += count ;		len -= readcount ;		} ;	return total ;} /* wav_gsm610_read_d */static int		wav_gsm610_read_d  (SF_PRIVATE *psf, double *ptr, int len, int normalize){	GSM610_PRIVATE *pgsm610 ; 	short		*sptr ;	int			k, bufferlen, readcount = 0, count ;	int			index = 0, total = 0 ;	double		normfact ;		normfact = (normalize ? 1.0 / ((double) 0x8000) : 1.0) ;	if (! psf->fdata)		return 0 ;

⌨️ 快捷键说明

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