Merged with trunk revision 600
[ardour.git] / libs / libsndfile / src / pvf.c
1 /*
2 ** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
3 **
4 ** This program is free software; you can redistribute it and/or modify
5 ** it 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
7 ** (at your option) any later version.
8 **
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ** GNU Lesser 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 program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #include        "sfconfig.h"
20
21 #include        <stdio.h>
22 #include        <fcntl.h>
23 #include        <string.h>
24 #include        <ctype.h>
25
26 #include        "sndfile.h"
27 #include        "sfendian.h"
28 #include        "common.h"
29
30 /*------------------------------------------------------------------------------
31 ** Macros to handle big/little endian issues.
32 */
33
34 #define PVF1_MARKER             (MAKE_MARKER ('P', 'V', 'F', '1'))
35
36 /*------------------------------------------------------------------------------
37 ** Private static functions.
38 */
39
40 static  int             pvf_close               (SF_PRIVATE *psf) ;
41
42 static int              pvf_write_header (SF_PRIVATE *psf, int calc_length) ;
43 static int              pvf_read_header (SF_PRIVATE *psf) ;
44
45 /*------------------------------------------------------------------------------
46 ** Public function.
47 */
48
49 int
50 pvf_open        (SF_PRIVATE *psf)
51 {       int             subformat ;
52         int             error = 0 ;
53
54         if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
55         {       if ((error = pvf_read_header (psf)))
56                         return error ;
57                 } ;
58
59         subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
60
61         if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
62         {       if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PVF)
63                         return  SFE_BAD_OPEN_FORMAT ;
64
65                 psf->endian = SF_ENDIAN_BIG ;
66
67                 if (pvf_write_header (psf, SF_FALSE))
68                         return psf->error ;
69
70                 psf->write_header = pvf_write_header ;
71                 } ;
72
73         psf->container_close = pvf_close ;
74
75         psf->blockwidth = psf->bytewidth * psf->sf.channels ;
76
77         switch (subformat)
78         {       case SF_FORMAT_PCM_S8 : /* 8-bit linear PCM. */
79                 case SF_FORMAT_PCM_16 : /* 16-bit linear PCM. */
80                 case SF_FORMAT_PCM_32 : /* 32-bit linear PCM. */
81                                 error = pcm_init (psf) ;
82                                 break ;
83
84                 default :       break ;
85                 } ;
86
87         return error ;
88 } /* pvf_open */
89
90 /*------------------------------------------------------------------------------
91 */
92
93 static int
94 pvf_close       (SF_PRIVATE *psf)
95 {
96         psf = psf ;
97
98         return 0 ;
99 } /* pvf_close */
100
101 static int
102 pvf_write_header (SF_PRIVATE *psf, int calc_length)
103 {       sf_count_t      current ;
104
105         if (psf->pipeoffset > 0)
106                 return 0 ;
107
108         calc_length = calc_length ; /* Avoid a compiler warning. */
109
110         current = psf_ftell (psf) ;
111
112         /* Reset the current header length to zero. */
113         psf->header [0] = 0 ;
114         psf->headindex = 0 ;
115
116         if (psf->is_pipe == SF_FALSE)
117                 psf_fseek (psf, 0, SEEK_SET) ;
118
119         LSF_SNPRINTF ((char*) psf->header, sizeof (psf->header), "PVF1\n%d %d %d\n",
120                 psf->sf.channels, psf->sf.samplerate, psf->bytewidth * 8) ;
121
122         psf->headindex = strlen ((char*) psf->header) ;
123
124         /* Header construction complete so write it out. */
125         psf_fwrite (psf->header, psf->headindex, 1, psf) ;
126
127         if (psf->error)
128                 return psf->error ;
129
130         psf->dataoffset = psf->headindex ;
131
132         if (current > 0)
133                 psf_fseek (psf, current, SEEK_SET) ;
134
135         return psf->error ;
136 } /* pvf_write_header */
137
138 static int
139 pvf_read_header (SF_PRIVATE *psf)
140 {       char    buffer [32] ;
141         int             marker, channels, samplerate, bitwidth ;
142
143         psf_binheader_readf (psf, "pmj", 0, &marker, 1) ;
144         psf_log_printf (psf, "%M\n", marker) ;
145
146         if (marker != PVF1_MARKER)
147                 return SFE_PVF_NO_PVF1 ;
148
149         /* Grab characters up until a newline which is replaced by an EOS. */
150         psf_binheader_readf (psf, "G", buffer, sizeof (buffer)) ;
151
152         if (sscanf (buffer, "%d %d %d", &channels, &samplerate, &bitwidth) != 3)
153                 return SFE_PVF_BAD_HEADER ;
154
155         psf_log_printf (psf, " Channels    : %d\n Sample rate : %d\n Bit width   : %d\n",
156                                 channels, samplerate, bitwidth) ;
157
158         psf->sf.channels = channels ;
159         psf->sf.samplerate = samplerate ;
160
161         switch (bitwidth)
162         {       case 8 :
163                                 psf->sf.format = SF_FORMAT_PVF | SF_FORMAT_PCM_S8 ;
164                                 psf->bytewidth = 1 ;
165                                 break ;
166
167                 case 16 :
168                                 psf->sf.format = SF_FORMAT_PVF | SF_FORMAT_PCM_16 ;
169                                 psf->bytewidth = 2 ;
170                                 break ;
171                 case 32 :
172                                 psf->sf.format = SF_FORMAT_PVF | SF_FORMAT_PCM_32 ;
173                                 psf->bytewidth = 4 ;
174                                 break ;
175
176                 default :
177                                 return SFE_PVF_BAD_BITWIDTH ;
178                 } ;
179
180         psf->dataoffset = psf_ftell (psf) ;
181         psf_log_printf (psf, " Data Offset : %D\n", psf->dataoffset) ;
182
183         psf->endian = SF_ENDIAN_BIG ;
184
185         psf->datalength = psf->filelength - psf->dataoffset ;
186         psf->blockwidth = psf->sf.channels * psf->bytewidth ;
187
188         if (! psf->sf.frames && psf->blockwidth)
189                 psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
190
191         return 0 ;
192 } /* pvf_read_header */
193 /*
194 ** Do not edit or modify anything in this comment block.
195 ** The arch-tag line is a file identity tag for the GNU Arch 
196 ** revision control system.
197 **
198 ** arch-tag: 20a26761-8bc1-41d7-b1f3-9793bf3d9864
199 */