Coverage Report

Created: 2025-11-11 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libsndfile/src/avr.c
Line
Count
Source
1
/*
2
** Copyright (C) 2004-2017 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 <string.h>
23
24
#include "sndfile.h"
25
#include "sfendian.h"
26
#include "common.h"
27
28
157
#define TWOBIT_MARKER (MAKE_MARKER ('2', 'B', 'I', 'T'))
29
97
#define AVR_HDR_SIZE  128
30
31
/*
32
** From: hyc@hanauma.Jpl.Nasa.Gov (Howard Chu)
33
**
34
** A lot of PD software exists to play Mac .snd files on the ST. One other
35
** format that seems pretty popular (used by a number of commercial packages)
36
** is the AVR format (from Audio Visual Research). This format has a 128 byte
37
** header that looks like this (its actually packed, but thats not portable):
38
*/
39
40
typedef struct
41
{ int   marker ;  /* 2BIT */
42
  char  name [8] ;  /* null-padded sample name */
43
  short mono ;    /* 0 = mono, 0xffff = stereo */
44
  short rez ;   /* 8 = 8 bit, 16 = 16 bit */
45
  short sign ;    /* 0 = unsigned, 0xffff = signed */
46
47
  short loop ;    /* 0 = no loop, 0xffff = looping sample */
48
  short midi ;    /* 0xffff = no MIDI note assigned,  */
49
            /*  0xffXX = single key note assignment */
50
            /*  0xLLHH = key split, low/hi note */
51
  int   srate ;   /* sample frequency in hertz */
52
  int   frames ;  /* sample length in bytes or words (see rez) */
53
  int   lbeg ;    /* offset to start of loop in bytes or words. */
54
            /* set to zero if unused */
55
  int   lend ;    /* offset to end of loop in bytes or words. */
56
            /* set to sample length if unused */
57
  short res1 ;    /* Reserved, MIDI keyboard split */
58
  short res2 ;    /* Reserved, sample compression */
59
  short res3 ;    /* Reserved */
60
  char  ext [20] ;  /* Additional filename space, used if (name[7] != 0) */
61
  char  user [64] ; /* User defined. Typically ASCII message */
62
} AVR_HEADER ;
63
64
/*------------------------------------------------------------------------------
65
** Private static functions.
66
*/
67
68
static int    avr_close (SF_PRIVATE *psf) ;
69
70
static int    avr_read_header (SF_PRIVATE *psf) ;
71
static int    avr_write_header (SF_PRIVATE *psf, int calc_length) ;
72
73
/*------------------------------------------------------------------------------
74
** Public function.
75
*/
76
77
int
78
avr_open  (SF_PRIVATE *psf)
79
157
{ int   error = 0 ;
80
81
157
  if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0))
82
157
  { if ((error = avr_read_header (psf)))
83
61
      return error ;
84
157
    } ;
85
86
96
  if ((SF_CONTAINER (psf->sf.format)) != SF_FORMAT_AVR)
87
0
    return  SFE_BAD_OPEN_FORMAT ;
88
89
96
  if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
90
0
  { psf->endian = SF_ENDIAN_BIG ;
91
92
0
    if (avr_write_header (psf, SF_FALSE))
93
0
      return psf->error ;
94
95
0
    psf->write_header = avr_write_header ;
96
96
    } ;
97
98
96
  psf->container_close = avr_close ;
99
100
96
  psf->blockwidth = psf->bytewidth * psf->sf.channels ;
101
102
96
  error = pcm_init (psf) ;
103
104
96
  return error ;
105
96
} /* avr_open */
106
107
static int
108
avr_read_header (SF_PRIVATE *psf)
109
157
{ AVR_HEADER  hdr ;
110
111
157
  memset (&hdr, 0, sizeof (hdr)) ;
112
113
157
  psf_binheader_readf (psf, "pmb", 0, &hdr.marker, &hdr.name, sizeof (hdr.name)) ;
114
157
  psf_log_printf (psf, "%M\n", hdr.marker) ;
115
116
157
  if (hdr.marker != TWOBIT_MARKER)
117
0
    return SFE_AVR_NOT_AVR ;
118
119
157
  psf_log_printf (psf, "  Name        : %s\n", hdr.name) ;
120
121
157
  psf_binheader_readf (psf, "E22222", &hdr.mono, &hdr.rez, &hdr.sign, &hdr.loop, &hdr.midi) ;
122
123
157
  psf->sf.channels = (hdr.mono & 1) + 1 ;
124
125
157
  psf_log_printf (psf, "  Channels    : %d\n  Bit width   : %d\n  Signed      : %s\n",
126
157
      (hdr.mono & 1) + 1, hdr.rez, hdr.sign ? "yes" : "no") ;
127
128
157
  switch (arith_shift_left (hdr.rez, 16) + (hdr.sign & 1))
129
157
  { case ((8 << 16) + 0) :
130
49
      psf->sf.format = SF_FORMAT_AVR | SF_FORMAT_PCM_U8 ;
131
49
      psf->bytewidth = 1 ;
132
49
      break ;
133
134
41
    case ((8 << 16) + 1) :
135
41
      psf->sf.format = SF_FORMAT_AVR | SF_FORMAT_PCM_S8 ;
136
41
      psf->bytewidth = 1 ;
137
41
      break ;
138
139
6
    case ((16 << 16) + 1) :
140
6
      psf->sf.format = SF_FORMAT_AVR | SF_FORMAT_PCM_16 ;
141
6
      psf->bytewidth = 2 ;
142
6
      break ;
143
144
61
    default :
145
61
      psf_log_printf (psf, "Error : bad rez/sign combination.\n") ;
146
61
      return SFE_AVR_BAD_REZ_SIGN ;
147
157
    } ;
148
149
96
  psf_binheader_readf (psf, "E4444", &hdr.srate, &hdr.frames, &hdr.lbeg, &hdr.lend) ;
150
151
96
  psf->sf.frames = hdr.frames ;
152
96
  psf->sf.samplerate = hdr.srate ;
153
154
96
  psf_log_printf (psf, "  Frames      : %D\n", psf->sf.frames) ;
155
96
  psf_log_printf (psf, "  Sample rate : %d\n", psf->sf.samplerate) ;
156
157
96
  psf_binheader_readf (psf, "E222", &hdr.res1, &hdr.res2, &hdr.res3) ;
158
96
  psf_binheader_readf (psf, "bb", hdr.ext, sizeof (hdr.ext), hdr.user, sizeof (hdr.user)) ;
159
160
96
  psf_log_printf (psf, "  Ext         : %s\n  User        : %s\n", hdr.ext, hdr.user) ;
161
162
96
  psf->endian = SF_ENDIAN_BIG ;
163
164
96
  psf->dataoffset = AVR_HDR_SIZE ;
165
96
  psf->datalength = (sf_count_t) hdr.frames * (hdr.rez / 8) ;
166
167
96
  if (psf->fileoffset > 0)
168
1
    psf->filelength = AVR_HDR_SIZE + psf->datalength ;
169
170
96
  if (psf_ftell (psf) != psf->dataoffset)
171
87
    psf_binheader_readf (psf, "j", psf->dataoffset - psf_ftell (psf)) ;
172
173
96
  psf->blockwidth = psf->sf.channels * psf->bytewidth ;
174
175
96
  if (psf->sf.frames == 0 && psf->blockwidth)
176
9
    psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
177
178
96
  return 0 ;
179
157
} /* avr_read_header */
180
181
static int
182
avr_write_header (SF_PRIVATE *psf, int calc_length)
183
0
{ sf_count_t  current ;
184
0
  int     sign ;
185
186
0
  if (psf->pipeoffset > 0)
187
0
    return 0 ;
188
189
0
  current = psf_ftell (psf) ;
190
191
0
  if (calc_length)
192
0
  { psf->filelength = psf_get_filelen (psf) ;
193
194
0
    psf->datalength = psf->filelength - psf->dataoffset ;
195
0
    if (psf->dataend)
196
0
      psf->datalength -= psf->filelength - psf->dataend ;
197
198
0
    psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
199
0
    } ;
200
201
  /* Reset the current header length to zero. */
202
0
  psf->header.ptr [0] = 0 ;
203
0
  psf->header.indx = 0 ;
204
205
  /*
206
  ** Only attempt to seek if we are not writng to a pipe. If we are
207
  ** writing to a pipe we shouldn't be here anyway.
208
  */
209
0
  if (psf->is_pipe == SF_FALSE)
210
0
    psf_fseek (psf, 0, SEEK_SET) ;
211
212
0
  psf_binheader_writef (psf, "Emz22", BHWm (TWOBIT_MARKER), BHWz (8),
213
0
      BHW2 (psf->sf.channels == 2 ? 0xFFFF : 0), BHW2 (psf->bytewidth * 8)) ;
214
215
0
  sign = ((SF_CODEC (psf->sf.format)) == SF_FORMAT_PCM_U8) ? 0 : 0xFFFF ;
216
217
0
  psf_binheader_writef (psf, "E222", BHW2 (sign), BHW2 (0), BHW2 (0xFFFF)) ;
218
0
  psf_binheader_writef (psf, "E4444", BHW4 (psf->sf.samplerate), BHW4 (psf->sf.frames), BHW4 (0), BHW4 (0)) ;
219
220
0
  psf_binheader_writef (psf, "E222zz", BHW2 (0), BHW2 (0), BHW2 (0), BHWz (20), BHWz (64)) ;
221
222
  /* Header construction complete so write it out. */
223
0
  psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
224
225
0
  if (psf->error)
226
0
    return psf->error ;
227
228
0
  psf->dataoffset = psf->header.indx ;
229
230
0
  if (current > 0)
231
0
    psf_fseek (psf, current, SEEK_SET) ;
232
233
0
  return psf->error ;
234
0
} /* avr_write_header */
235
236
static int
237
avr_close (SF_PRIVATE *psf)
238
96
{
239
96
  if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
240
0
    avr_write_header (psf, SF_TRUE) ;
241
242
96
  return 0 ;
243
96
} /* avr_close */
244