1 /* libSoX SoundTool format handler          (c) 2008 robs@users.sourceforge.net
2  * See description in sndtl26.zip on the net.
3  *
4  * This library is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 2.1 of the License, or (at
7  * your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 
19 #include "sox_i.h"
20 #include <string.h>
21 
22 static char const ID1[6] = "SOUND\x1a";
23 #define text_field_len (size_t)96  /* Includes null-terminator */
24 
start_read(sox_format_t * ft)25 static int start_read(sox_format_t * ft)
26 {
27   char id1[sizeof(ID1)], comments[text_field_len + 1];
28   uint32_t nsamples;
29   uint16_t rate;
30 
31   if (lsx_readchars(ft, id1, sizeof(ID1)) ||
32       lsx_skipbytes(ft, (size_t) 10) || lsx_readdw(ft, &nsamples) ||
33       lsx_readw(ft, &rate) || lsx_skipbytes(ft, (size_t) 6) ||
34       lsx_readchars(ft, comments, text_field_len))
35     return SOX_EOF;
36   if (memcmp(ID1, id1, sizeof(id1))) {
37     lsx_fail_errno(ft, SOX_EHDR, "soundtool: can't find SoundTool identifier");
38     return SOX_EOF;
39   }
40   comments[text_field_len] = '\0'; /* Be defensive against incorrect files */
41   sox_append_comments(&ft->oob.comments, comments);
42   return lsx_check_read_params(ft, 1, (sox_rate_t)rate, SOX_ENCODING_UNSIGNED, 8, (uint64_t)nsamples, sox_true);
43 }
44 
write_header(sox_format_t * ft)45 static int write_header(sox_format_t * ft)
46 {
47   char * comment = lsx_cat_comments(ft->oob.comments);
48   char text_buf[text_field_len];
49   uint64_t length = ft->olength? ft->olength:ft->signal.length;
50 
51   memset(text_buf, 0, sizeof(text_buf));
52   strncpy(text_buf, comment, text_field_len - 1);
53   free(comment);
54   return lsx_writechars(ft, ID1, sizeof(ID1))
55       || lsx_writew  (ft, 0)      /* GSound: not used */
56       || lsx_writedw (ft, (unsigned) length) /* length of complete sample */
57       || lsx_writedw (ft, 0)      /* first byte to play from sample */
58       || lsx_writedw (ft, (unsigned) length) /* first byte NOT to play from sample */
59       || lsx_writew  (ft, min(65535, (unsigned)(ft->signal.rate + .5)))
60       || lsx_writew  (ft, 0)      /* sample size/type */
61       || lsx_writew  (ft, 10)     /* speaker driver volume */
62       || lsx_writew  (ft, 4)      /* speaker driver DC shift */
63       || lsx_writechars(ft, text_buf, sizeof(text_buf))?  SOX_EOF:SOX_SUCCESS;
64 }
65 
LSX_FORMAT_HANDLER(soundtool)66 LSX_FORMAT_HANDLER(soundtool)
67 {
68   static char const * const names[] = {"sndt", NULL};
69   static unsigned const write_encodings[] = {SOX_ENCODING_UNSIGNED, 8, 0, 0};
70   static sox_format_handler_t const handler = {SOX_LIB_VERSION_CODE,
71     "8-bit linear audio as used by Martin Hepperle's `SoundTool' of 1991/2",
72     names, SOX_FILE_LIT_END | SOX_FILE_MONO | SOX_FILE_REWIND,
73     start_read, lsx_rawread, NULL,
74     write_header, lsx_rawwrite, NULL,
75     lsx_rawseek, write_encodings, NULL, 0
76   };
77   return &handler;
78 }
79