2
** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
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.
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.
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.
30
/*------------------------------------------------------------------------------
31
** Macros to handle big/little endian issues.
34
#define PVF1_MARKER (MAKE_MARKER ('P', 'V', 'F', '1'))
36
/*------------------------------------------------------------------------------
37
** Private static functions.
40
static int pvf_close (SF_PRIVATE *psf) ;
42
static int pvf_write_header (SF_PRIVATE *psf, int calc_length) ;
43
static int pvf_read_header (SF_PRIVATE *psf) ;
45
/*------------------------------------------------------------------------------
50
pvf_open (SF_PRIVATE *psf)
54
if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
55
{ if ((error = pvf_read_header (psf)))
59
subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
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 ;
65
psf->endian = SF_ENDIAN_BIG ;
67
if (pvf_write_header (psf, SF_FALSE))
70
psf->write_header = pvf_write_header ;
73
psf->container_close = pvf_close ;
75
psf->blockwidth = psf->bytewidth * psf->sf.channels ;
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) ;
90
/*------------------------------------------------------------------------------
94
pvf_close (SF_PRIVATE *psf)
102
pvf_write_header (SF_PRIVATE *psf, int calc_length)
103
{ sf_count_t current ;
105
if (psf->pipeoffset > 0)
108
calc_length = calc_length ; /* Avoid a compiler warning. */
110
current = psf_ftell (psf) ;
112
/* Reset the current header length to zero. */
113
psf->header [0] = 0 ;
116
if (psf->is_pipe == SF_FALSE)
117
psf_fseek (psf, 0, SEEK_SET) ;
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) ;
122
psf->headindex = strlen ((char*) psf->header) ;
124
/* Header construction complete so write it out. */
125
psf_fwrite (psf->header, psf->headindex, 1, psf) ;
130
psf->dataoffset = psf->headindex ;
133
psf_fseek (psf, current, SEEK_SET) ;
136
} /* pvf_write_header */
139
pvf_read_header (SF_PRIVATE *psf)
141
int marker, channels, samplerate, bitwidth ;
143
psf_binheader_readf (psf, "pmj", 0, &marker, 1) ;
144
psf_log_printf (psf, "%M\n", marker) ;
146
if (marker != PVF1_MARKER)
147
return SFE_PVF_NO_PVF1 ;
149
/* Grab characters up until a newline which is replaced by an EOS. */
150
psf_binheader_readf (psf, "G", buffer, sizeof (buffer)) ;
152
if (sscanf (buffer, "%d %d %d", &channels, &samplerate, &bitwidth) != 3)
153
return SFE_PVF_BAD_HEADER ;
155
psf_log_printf (psf, " Channels : %d\n Sample rate : %d\n Bit width : %d\n",
156
channels, samplerate, bitwidth) ;
158
psf->sf.channels = channels ;
159
psf->sf.samplerate = samplerate ;
163
psf->sf.format = SF_FORMAT_PVF | SF_FORMAT_PCM_S8 ;
168
psf->sf.format = SF_FORMAT_PVF | SF_FORMAT_PCM_16 ;
172
psf->sf.format = SF_FORMAT_PVF | SF_FORMAT_PCM_32 ;
177
return SFE_PVF_BAD_BITWIDTH ;
180
psf->dataoffset = psf_ftell (psf) ;
181
psf_log_printf (psf, " Data Offset : %D\n", psf->dataoffset) ;
183
psf->endian = SF_ENDIAN_BIG ;
185
psf->datalength = psf->filelength - psf->dataoffset ;
186
psf->blockwidth = psf->sf.channels * psf->bytewidth ;
188
if (! psf->sf.frames && psf->blockwidth)
189
psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
192
} /* pvf_read_header */
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.
198
** arch-tag: 20a26761-8bc1-41d7-b1f3-9793bf3d9864