[asterisk-commits] qwell: branch 1.8 r284701 - /branches/1.8/formats/format_wav.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Sep 2 11:43:14 CDT 2010


Author: qwell
Date: Thu Sep  2 11:43:09 2010
New Revision: 284701

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=284701
Log:
Add slin16 support for format_wav (new wav16 file extension)

(closes issue #15029)
Reported by: andrew
Patches: 
      wav16.patch uploaded by andrew (license 240)
Tested by: qwell, andrew

Modified:
    branches/1.8/formats/format_wav.c

Modified: branches/1.8/formats/format_wav.c
URL: http://svnview.digium.com/svn/asterisk/branches/1.8/formats/format_wav.c?view=diff&rev=284701&r1=284700&r2=284701
==============================================================================
--- branches/1.8/formats/format_wav.c (original)
+++ branches/1.8/formats/format_wav.c Thu Sep  2 11:43:09 2010
@@ -39,6 +39,7 @@
 #define	WAV_BUF_SIZE	320
 
 struct wav_desc {	/* format-specific parameters */
+	int hz;
 	int bytes;
 	int lasttimeout;
 	int maxlen;
@@ -70,7 +71,7 @@
 #endif
 
 
-static int check_header(FILE *f)
+static int check_header(FILE *f, int hz)
 {
 	int type, size, formtype;
 	int fmt, hsize;
@@ -135,8 +136,10 @@
 		ast_log(LOG_WARNING, "Read failed (freq)\n");
 		return -1;
 	}
-	if (ltohl(freq) != DEFAULT_SAMPLE_RATE) {
-		ast_log(LOG_WARNING, "Unexpected frequency %d\n", ltohl(freq));
+	if (((ltohl(freq) != 8000) && (ltohl(freq) != 16000)) ||
+	    ((ltohl(freq) == 8000) && (hz != 8000)) ||
+	    ((ltohl(freq) == 16000) && (hz != 16000))) {
+		ast_log(LOG_WARNING, "Unexpected frequency mismatch %d (expecting %d)\n", ltohl(freq),hz);
 		return -1;
 	}
 	/* Ignore the byte frequency */
@@ -239,16 +242,24 @@
 	return 0;
 }
 
-static int write_header(FILE *f)
-{
-	unsigned int hz=htoll(8000);
-	unsigned int bhz = htoll(16000);
+static int write_header(FILE *f, int writehz)
+{
+	unsigned int hz;
+	unsigned int bhz;
 	unsigned int hs = htoll(16);
 	unsigned short fmt = htols(1);
 	unsigned short chans = htols(1);
 	unsigned short bysam = htols(2);
 	unsigned short bisam = htols(16);
 	unsigned int size = htoll(0);
+
+	if (writehz == 16000) {
+		hz = htoll(16000);
+		bhz = htoll(32000);
+	} else {
+		hz = htoll(8000);
+		bhz = htoll(16000);
+	}
 	/* Write a wav header, ignoring sizes which will be filled in later */
 	fseek(f,0,SEEK_SET);
 	if (fwrite("RIFF", 1, 4, f) != 4) {
@@ -308,7 +319,7 @@
 	   if we did, it would go here.  We also might want to check
 	   and be sure it's a valid file.  */
 	struct wav_desc *tmp = (struct wav_desc *)s->_private;
-	if ((tmp->maxlen = check_header(s->f)) < 0)
+	if ((tmp->maxlen = check_header(s->f, (s->fmt->format == AST_FORMAT_SLINEAR16 ? 16000 : 8000))) < 0)
 		return -1;
 	return 0;
 }
@@ -319,7 +330,9 @@
 	   if we did, it would go here.  We also might want to check
 	   and be sure it's a valid file.  */
 
-	if (write_header(s->f))
+	struct wav_desc *tmp = (struct wav_desc *)s->_private;
+	tmp->hz = (s->fmt->format == AST_FORMAT_SLINEAR16 ? 16000 : 8000);
+	if (write_header(s->f,tmp->hz))
 		return -1;
 	return 0;
 }
@@ -349,10 +362,12 @@
 	int x;
 #endif
 	short *tmp;
-	int bytes = WAV_BUF_SIZE;	/* in bytes */
+	int bytes;
 	off_t here;
 	/* Send a frame from the file to the appropriate channel */
 	struct wav_desc *fs = (struct wav_desc *)s->_private;
+
+	bytes = (fs->hz == 16000 ? (WAV_BUF_SIZE * 2) : WAV_BUF_SIZE);
 
 	here = ftello(s->f);
 	if (fs->maxlen - here < bytes)		/* truncate if necessary */
@@ -361,7 +376,7 @@
 		bytes = 0;
 /* 	ast_debug(1, "here: %d, maxlen: %d, bytes: %d\n", here, s->maxlen, bytes); */
 	s->fr.frametype = AST_FRAME_VOICE;
-	s->fr.subclass.codec = AST_FORMAT_SLINEAR;
+	s->fr.subclass.codec = (fs->hz == 16000 ? AST_FORMAT_SLINEAR16 : AST_FORMAT_SLINEAR);
 	s->fr.mallocd = 0;
 	AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, bytes);
 	
@@ -388,7 +403,7 @@
 {
 #if __BYTE_ORDER == __BIG_ENDIAN
 	int x;
-	short tmp[8000], *tmpi;
+	short tmp[16000], *tmpi;
 #endif
 	struct wav_desc *s = (struct wav_desc *)fs->_private;
 	int res;
@@ -397,8 +412,12 @@
 		ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
 		return -1;
 	}
-	if (f->subclass.codec != AST_FORMAT_SLINEAR) {
-		ast_log(LOG_WARNING, "Asked to write non-SLINEAR frame (%s)!\n", ast_getformatname(f->subclass.codec));
+	if ((f->subclass.codec != AST_FORMAT_SLINEAR) && (f->subclass.codec != AST_FORMAT_SLINEAR16)) {
+		ast_log(LOG_WARNING, "Asked to write non-SLINEAR%s frame (%s)!\n", s->hz == 16000 ? "16" : "", ast_getformatname(f->subclass.codec));
+		return -1;
+	}
+	if (f->subclass.codec != fs->fmt->format) {
+		ast_log(LOG_WARNING, "Can't change SLINEAR frequency during write\n");
 		return -1;
 	}
 	if (!f->datalen)
@@ -466,6 +485,22 @@
 	/* subtract header size to get samples, then divide by 2 for 16 bit samples */
 	return (offset - 44)/2;
 }
+
+static const struct ast_format wav16_f = {
+	.name = "wav16",
+	.exts = "wav16",
+	.format = AST_FORMAT_SLINEAR16,
+	.open =	wav_open,
+	.rewrite = wav_rewrite,
+	.write = wav_write,
+	.seek = wav_seek,
+	.trunc = wav_trunc,
+	.tell =	wav_tell,
+	.read = wav_read,
+	.close = wav_close,
+	.buf_size = (WAV_BUF_SIZE * 2) + AST_FRIENDLY_OFFSET,
+	.desc_size = sizeof(struct wav_desc),
+};
 
 static const struct ast_format wav_f = {
 	.name = "wav",
@@ -485,17 +520,19 @@
 
 static int load_module(void)
 {
-	if (ast_format_register(&wav_f))
+	if (ast_format_register(&wav_f)
+		|| ast_format_register(&wav16_f))
 		return AST_MODULE_LOAD_FAILURE;
 	return AST_MODULE_LOAD_SUCCESS;
 }
 
 static int unload_module(void)
 {
-	return ast_format_unregister(wav_f.name);
-}
-
-AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Microsoft WAV format (8000Hz Signed Linear)",
+	return ast_format_unregister(wav_f.name)
+		|| ast_format_unregister(wav16_f.name);
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Microsoft WAV/WAV16 format (8kHz/16kHz Signed Linear)",
 	.load = load_module,
 	.unload = unload_module,
 	.load_pri = AST_MODPRI_APP_DEPEND




More information about the asterisk-commits mailing list