1 /* libSoX file format: Psion wve   (c) 2008 robs@users.sourceforge.net
2  *
3  * See http://filext.com/file-extension/WVE
4  *
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation; either version 2.1 of the License, or (at
8  * your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 #include "sox_i.h"
21 #include <string.h>
22 
23 static char const ID1[18] = "ALawSoundFile**\0\017\020";
24 static char const ID2[] = {0,0,0,1,0,0,0,0,0,0}; /* pad & repeat info: ignore */
25 
start_read(sox_format_t * ft)26 static int start_read(sox_format_t * ft)
27 {
28   char buf[sizeof(ID1)];
29   uint32_t num_samples;
30 
31   if (lsx_readchars(ft, buf, sizeof(buf)) || lsx_readdw(ft, &num_samples) ||
32       lsx_skipbytes(ft, sizeof(ID2)))
33     return SOX_EOF;
34   if (memcmp(ID1, buf, sizeof(buf))) {
35     lsx_fail_errno(ft, SOX_EHDR, "wve: can't find Psion identifier");
36     return SOX_EOF;
37   }
38   return lsx_check_read_params(ft, 1, 8000., SOX_ENCODING_ALAW, 8, (uint64_t)num_samples, sox_true);
39 }
40 
write_header(sox_format_t * ft)41 static int write_header(sox_format_t * ft)
42 {
43   uint64_t size64 = ft->olength? ft->olength:ft->signal.length;
44   unsigned size = size64 > UINT_MAX ? 0 : (unsigned)size64;
45   return lsx_writechars(ft, ID1, sizeof(ID1))
46       || lsx_writedw(ft, size)
47       || lsx_writechars(ft, ID2, sizeof(ID2))? SOX_EOF:SOX_SUCCESS;
48 }
49 
LSX_FORMAT_HANDLER(wve)50 LSX_FORMAT_HANDLER(wve)
51 {
52   static char const * const names[] = {"wve", NULL};
53   static sox_rate_t   const write_rates[] = {8000, 0};
54   static unsigned     const write_encodings[] = {SOX_ENCODING_ALAW, 8, 0, 0};
55   static sox_format_handler_t const handler = {SOX_LIB_VERSION_CODE,
56     "Psion 3 audio format",
57     names, SOX_FILE_BIG_END | SOX_FILE_MONO | SOX_FILE_REWIND,
58     start_read, lsx_rawread, NULL,
59     write_header, lsx_rawwrite, NULL,
60     lsx_rawseek, write_encodings, write_rates, 0
61   };
62   return &handler;
63 }
64