From: robs <ro...@us...> - 2007-09-29 14:20:37
|
Update of /cvsroot/sox/sox/src In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv1524/src Modified Files: sndrtool.c tests.sh Log Message: Fix sndt read bug Index: sndrtool.c =================================================================== RCS file: /cvsroot/sox/sox/src/sndrtool.c,v retrieving revision 1.47 retrieving revision 1.48 diff -u -d -r1.47 -r1.48 --- sndrtool.c 2 Jun 2007 19:01:26 -0000 1.47 +++ sndrtool.c 29 Sep 2007 14:20:30 -0000 1.48 @@ -16,166 +16,162 @@ /* Private data used by writer */ typedef struct sndpriv { - sox_size_t nsamples; - sox_size_t dataStart; + sox_size_t nsamples; + sox_size_t dataStart; } *snd_t; static void sndtwriteheader(sox_format_t * ft, sox_size_t nsamples) { - char name_buf[97]; + char name_buf[97]; - /* sndtool header */ - sox_writes(ft, "SOUND"); /* magic */ - sox_writeb(ft, 0x1a); - sox_writew (ft,0); /* hGSound */ - sox_writedw (ft,nsamples); - sox_writedw (ft,0); - sox_writedw (ft,nsamples); - sox_writew (ft, ft->signal.rate); - sox_writew (ft,0); - sox_writew (ft,10); - sox_writew (ft,4); - memset (name_buf, 0, 96); - sprintf (name_buf,"%.62s - File created by SoX",ft->filename); - sox_writebuf(ft, name_buf, 96); + /* sndtool header */ + sox_writes(ft, "SOUND"); /* magic */ + sox_writeb(ft, 0x1a); + sox_writew(ft, 0); /* hGSound */ + sox_writedw(ft, nsamples); + sox_writedw(ft, 0); + sox_writedw(ft, nsamples); + sox_writew(ft, ft->signal.rate); + sox_writew(ft, 0); + sox_writew(ft, 10); + sox_writew(ft, 4); + memset(name_buf, 0, 96); + sprintf(name_buf, "%.62s - File created by SoX", ft->filename); + sox_writebuf(ft, name_buf, 96); } -static int sox_sndseek(sox_format_t * ft, sox_size_t offset) +static int sox_sndseek(sox_format_t * ft, sox_size_t offset) { - sox_size_t new_offset, channel_block, alignment; - snd_t snd = (snd_t ) ft->priv; + sox_size_t new_offset, channel_block, alignment; + snd_t snd = (snd_t) ft->priv; - new_offset = offset * ft->signal.size; - /* Make sure request aligns to a channel block (ie left+right) */ - channel_block = ft->signal.channels * ft->signal.size; - alignment = new_offset % channel_block; - /* Most common mistaken is to compute something like - * "skip everthing upto and including this sample" so - * advance to next sample block in this case. - */ - if (alignment != 0) - new_offset += (channel_block - alignment); - new_offset += snd->dataStart; + new_offset = offset * ft->signal.size; + /* Make sure request aligns to a channel block (ie left+right) */ + channel_block = ft->signal.channels * ft->signal.size; + alignment = new_offset % channel_block; + /* Most common mistaken is to compute something like + * "skip everthing upto and including this sample" so + * advance to next sample block in this case. + */ + if (alignment != 0) + new_offset += (channel_block - alignment); + new_offset += snd->dataStart; - return sox_seeki(ft, (sox_ssize_t)new_offset, SEEK_SET); + return sox_seeki(ft, (sox_ssize_t) new_offset, SEEK_SET); } static int sox_sndtstartread(sox_format_t * ft) { - snd_t snd = (snd_t ) ft->priv; + snd_t snd = (snd_t) ft->priv; - char buf[97]; + char buf[96]; - unsigned short rate; - int rc; + unsigned short rate; + int rc; - /* Needed for rawread() */ - rc = sox_rawstartread(ft); - if (rc) - return rc; + /* Needed for rawread() */ + rc = sox_rawstartread(ft); + if (rc) + return rc; - rate = 0; + rate = 0; - /* determine file type */ - /* if first 5 bytes == SOUND then this is probably a sndtool sound */ - /* if first word (16 bits) == 0 - and second word is between 4000 & 25000 then this is sounder sound */ - /* otherwise, its probably raw, not handled here */ + /* determine file type */ + /* if first 5 bytes == SOUND then this is probably a sndtool sound */ + /* if first word (16 bits) == 0 + * and second word is between 4000 & 25000 then this is sounder sound */ + /* otherwise, its probably raw, not handled here */ - if (sox_readbuf(ft, buf, 2) != 2) - { - sox_fail_errno(ft,errno,"SND: unexpected EOF"); - return(SOX_EOF); - } - if (strncmp(buf,"\0\0",2) == 0) - { - /* sounder */ - sox_readw(ft, &rate); - if (rate < 4000 || rate > 25000 ) - { - sox_fail_errno(ft,SOX_EFMT,"SND: sample rate out of range"); - return(SOX_EOF); - } - sox_seeki(ft, 4, SEEK_CUR); - } - else - { - /* sndtool ? */ - sox_readbuf(ft, &buf[2], 6); - if (strncmp(buf,"SOUND",5)) - { - sox_fail_errno(ft,SOX_EFMT,"SND: unrecognized SND format"); - return(SOX_EOF); - } - sox_seeki(ft, 12, SEEK_CUR); - sox_readw(ft, &rate); - sox_seeki(ft, 6, SEEK_CUR); - if (sox_reads(ft, buf, 96) == SOX_EOF) - { - sox_fail_errno(ft,SOX_EHDR,"SND: unexpected EOF in SND header"); - return(SOX_EOF); - } - sox_debug("%s",buf); - } + if (sox_readbuf(ft, buf, 2) != 2) { + sox_fail_errno(ft, errno, "SND: unexpected EOF"); + return (SOX_EOF); + } + if (strncmp(buf, "\0\0", 2) == 0) { + /* sounder */ + sox_readw(ft, &rate); + if (rate < 4000 || rate > 25000) { + sox_fail_errno(ft, SOX_EFMT, "SND: sample rate out of range"); + return (SOX_EOF); + } + sox_seeki(ft, 4, SEEK_CUR); + } else { + /* sndtool ? */ + sox_readbuf(ft, &buf[2], 6); + if (strncmp(buf, "SOUND", 5)) { + sox_fail_errno(ft, SOX_EFMT, "SND: unrecognized SND format"); + return (SOX_EOF); + } + sox_seeki(ft, 12, SEEK_CUR); + sox_readw(ft, &rate); + sox_seeki(ft, 6, SEEK_CUR); + if (sox_readbuf(ft, buf, 96) != 96) { + sox_fail_errno(ft, SOX_EHDR, "SND: unexpected EOF in SND header"); + return (SOX_EOF); + } + sox_debug("comments: %.96s", buf); + } - ft->signal.channels = 1; - ft->signal.rate = rate; - ft->signal.encoding = SOX_ENCODING_UNSIGNED; - ft->signal.size = SOX_SIZE_BYTE; + ft->signal.channels = 1; + ft->signal.rate = rate; + ft->signal.encoding = SOX_ENCODING_UNSIGNED; + ft->signal.size = SOX_SIZE_BYTE; - snd->dataStart = sox_tell(ft); - ft->length = sox_filelength(ft) - snd->dataStart; + snd->dataStart = sox_tell(ft); + ft->length = sox_filelength(ft) - snd->dataStart; - return (SOX_SUCCESS); + return (SOX_SUCCESS); } static int sox_sndtstartwrite(sox_format_t * ft) { - snd_t p = (snd_t ) ft->priv; - int rc; + snd_t p = (snd_t) ft->priv; + int rc; - /* Needed for rawwrite() */ - rc = sox_rawstartwrite(ft); - if (rc) - return rc; + /* Needed for rawwrite() */ + rc = sox_rawstartwrite(ft); + if (rc) + return rc; - /* write header */ - ft->signal.channels = 1; - ft->signal.encoding = SOX_ENCODING_UNSIGNED; - ft->signal.size = SOX_SIZE_BYTE; - p->nsamples = 0; - sndtwriteheader(ft, 0); + /* write header */ + ft->signal.channels = 1; + ft->signal.encoding = SOX_ENCODING_UNSIGNED; + ft->signal.size = SOX_SIZE_BYTE; + p->nsamples = 0; + sndtwriteheader(ft, 0); - return(SOX_SUCCESS); + return (SOX_SUCCESS); } -static sox_size_t sox_sndtwrite(sox_format_t * ft, const sox_ssample_t *buf, sox_size_t len) +static sox_size_t sox_sndtwrite(sox_format_t * ft, const sox_ssample_t * buf, + sox_size_t len) { - snd_t p = (snd_t ) ft->priv; - p->nsamples += len; - return sox_rawwrite(ft, buf, len); + snd_t p = (snd_t) ft->priv; + + p->nsamples += len; + return sox_rawwrite(ft, buf, len); } static int sox_sndtstopwrite(sox_format_t * ft) { - snd_t p = (snd_t ) ft->priv; - int rc; + snd_t p = (snd_t) ft->priv; + int rc; - /* Flush remaining buffer out */ - rc = sox_rawstopwrite(ft); - if (rc) - return rc; + /* Flush remaining buffer out */ + rc = sox_rawstopwrite(ft); + if (rc) + return rc; - /* fixup file sizes in header */ - if (sox_seeki(ft, 0, 0) != 0){ - sox_fail_errno(ft,errno,"can't rewind output file to rewrite SND header"); - return SOX_EOF; - } - - sndtwriteheader(ft, p->nsamples); - + /* fixup file sizes in header */ + if (sox_seeki(ft, 0, 0) != 0) { + sox_fail_errno(ft, errno, + "can't rewind output file to rewrite SND header"); + return SOX_EOF; + } - return(SOX_SUCCESS); + sndtwriteheader(ft, p->nsamples); + + + return (SOX_SUCCESS); } /* Sndtool Sound File */ @@ -200,5 +196,5 @@ const sox_format_handler_t *sox_sndrtool_format_fn(void) { - return &sox_snd_format; + return &sox_snd_format; } Index: tests.sh =================================================================== RCS file: /cvsroot/sox/sox/src/tests.sh,v retrieving revision 1.48 retrieving revision 1.49 diff -u -d -r1.48 -r1.49 --- tests.sh 15 Jul 2007 17:36:17 -0000 1.48 +++ tests.sh 29 Sep 2007 14:20:30 -0000 1.49 @@ -1,8 +1,6 @@ #!/bin/sh # # SoX Regression Test script: Lossless file conversion -# -# FIXME: Test sndt # Options: #verbose=-V @@ -109,7 +107,7 @@ convertToAndFrom ima s2 u2 s3 u3 s4 u4 raw Raw dat au aiff aifc flac caf # FIXME: vox wav format1=Wav - convertToAndFrom smp s1 s1X s1N s1XN + convertToAndFrom smp s1 s1X s1N s1XN sndt (rate=5512; convertToAndFrom hcom) || exit 1 # Fixed rate format1=wve |