File: util.cc

package info (click to toggle)
cdrdao 1.09-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 604 kB
  • ctags: 786
  • sloc: cpp: 6,648; sh: 1,530; makefile: 245
file content (311 lines) | stat: -rw-r--r-- 6,307 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*  cdrdao - write audio CD-Rs in disc-at-once mode
 *
 *  Copyright (C) 1998  Andreas Mueller <mueller@daneb.ping.de>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
/*
 * $Log: util.cc,v $
 * Revision 1.5  1998/10/03 14:32:31  mueller
 * Applied patch from Bjoern Fischer <bfischer@Techfak.Uni-Bielefeld.DE>.
 *
 * Revision 1.4  1998/09/06 12:00:26  mueller
 * Used 'message' function for messages.
 *
 * Revision 1.3  1998/08/15 12:41:32  mueller
 * Ignore case when checking for '.wav' extension.
 * Suggested by Paul Martin <pm@nowster.zetnet.co.uk>.
 *
 */

static char rcsid[] = "$Id: util.cc,v 1.5 1998/10/03 14:32:31 mueller Exp $";

#include <config.h>

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "util.h"
#include "Sample.h"

char *strdupCC(const char *s)
{
  char *ret;
  long len;

  if (s == NULL) {
    return NULL;
  }

  len = strlen(s);

  ret = new char[len + 1];

  strcpy(ret, s);

  return ret;
}

long fullRead(int fd, void *buf, long count)
{
  long n = 0;
  long nread = 0;
  
  do {
    do {
      n = read(fd, (char *)buf + nread, count);
    } while (n < 0 && (errno == EAGAIN || errno == EINTR));

    if (n < 0) {
      return -1;
    }

    if (n == 0) {
      return nread;
    }
    
    count -= n;
    nread += n;
  } while (count > 0);

  return nread;
}
  
// Returns length in samples for given audio file.
// return: 1: file cannot be opened
//         2: 'fstat' failed
//         3: file header corruption
//         0: OK
int audioDataLength(const char *fname, unsigned long *length)
{
  int fd;
  struct stat buf;
  long headerLength = 0;

  *length = 0;

  if (access(fname, R_OK) != 0) {
    return 1;
  }

  if (audioFileType(fname) == 2) {
    if ((headerLength = waveHeaderLength(fname)) < 0) {
      return 3;
    }
  }

  if ((fd = open(fname, O_RDONLY)) > 0) {
    if (fstat(fd, &buf) == 0) {
      *length = (buf.st_size - headerLength) / sizeof(Sample);
    }
    else {
      close(fd);
      return 2;
    }
    close(fd);
  }
  else {
    return 1;
  }

  return 0;
}

long readLong(FILE *fp)
{
  unsigned char c1 = getc(fp);
  unsigned char c2 = getc(fp);
  unsigned char c3 = getc(fp);
  unsigned char c4 = getc(fp);

  return ((long)c4 << 24) | ((long)c3 << 16) | ((long)c2 << 8) | (long)c1;
}

short readShort(FILE *fp)
{
  unsigned char c1 = getc(fp);
  unsigned char c2 = getc(fp);

  return ((short)c2 << 8) | (short)c1;
}
  

long waveHeaderLength(const char *filename)
{
  FILE *fp;
  char    magic[4];
  long headerLen = 0;
  long len;
  short waveFormat;
  short waveChannels;
  long waveRate;
  short waveBits;

  if ((fp = fopen(filename, "r")) == NULL) {
    message(-2, "Cannot open audio file \"%s\" for reading: %s",
	    filename, strerror(errno));
    return -1;
  }

  if (fread(magic, sizeof(char), 4, fp) != 4 ||
      strncmp("RIFF", magic, 4) != 0) {
    message(-2, "%s: not a WAVE file.", filename);
    fclose(fp);
    return -1;
  }

  readLong(fp);

  if (fread(magic, sizeof(char), 4, fp) != 4 ||
      strncmp("WAVE", magic, 4) != 0) {
    message(-2, "%s: not a WAVE file.", filename);
    fclose(fp);
    return -1;
  }

  // search for format chunk
  for (;;) {
    if (fread(magic, sizeof(char), 4, fp) != 4) {
      message(-2, "%s: corrupted WAVE file.", filename);
      fclose(fp);
      return -1;
    }

    len = readLong(fp);

    if (strncmp("fmt ", magic, 4) == 0) {
      // format chunk found
      break;
    }

    // skip chunk data
    while (len > 0 && !feof(fp)) {
      getc(fp);
      len--;
    }
  }

  if (len < 16) {
    message(-2, "%s: corrupted WAVE file.", filename);
    fclose(fp);
    return -1;
  }

  waveFormat = readShort(fp);

  if (waveFormat != 1) {
    // not PCM format
    message(-2, "%s: not in PCM format.", filename);
    fclose(fp);
    return -1;
  }

  waveChannels = readShort(fp);
  if (waveChannels != 2) {
    message(-2, "%s: found %d channel(s), require 2 channels.",
	    filename, waveChannels);
    fclose(fp);
    return -1;
  }

  waveRate = readLong(fp);
  if (waveRate != 44100) {
     message(-2, "%s: found sampling rate %ld, require 44100.",
	    filename, waveRate);
     fclose(fp);
     return -1;
  }
  
  readLong(fp); // average bytes/second
  readShort(fp); // block align
  
  waveBits = readShort(fp);
  if (waveBits != 16) {
    message(-2, "%s: found %d bits per sample, require 16.",
	    filename, waveBits);
    fclose(fp);
    return -1;
  }
  
  len -=16;
  while (len > 0 && !feof(fp)) {
    getc(fp);
    len--;
  }

  // search wave data chunk
  for (;;) {
    if (fread(magic, sizeof(char), 4, fp) != 4) {
      message(-2, "%s: corrupted WAVE file.", filename);
      fclose(fp);
      return -1;
    }
    
    len = readLong(fp);
    if (strncmp("data", magic, 4) == 0) {
      // found data chunk
      break;
    }
     
    while (len > 0 && !feof(fp)) {
      getc(fp);
      len--;
    }
  }

  if ((headerLen = ftell(fp)) < 0) {
    message(-2, "%s: cannot determine file position: %s",
	    filename, strerror(errno));
    fclose(fp);
    return -1;
  }

  fclose(fp);

  //message(0, "%s: header length: %ld", filename, headerLen);

  return headerLen;
}


// determines type of audio file
// return: 1: raw samples
//         2: wav file
int audioFileType(const char *filename)
{
  char *p;

  if ((p = strrchr(filename, '.')) != NULL) {
    if (strcasecmp(p, ".wav") == 0) {
      return 2;
    }
  }

  return 1;
}

void swapSamples(Sample *buf, unsigned long len)
{
  unsigned long i;

  for (i = 0; i < len; i++) {
    buf[i].swap();
  }
}