File: signpr_wav.c

package info (click to toggle)
gramofile 1.6-7
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 840 kB
  • ctags: 538
  • sloc: ansic: 10,238; makefile: 55
file content (240 lines) | stat: -rw-r--r-- 5,480 bytes parent folder | download | duplicates (6)
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
/* Signal Processing - Wave File Functions

 * Copyright (C) 1998 J.A. Bezemer
 *
 * Licensed under the terms of the GNU General Public License.
 * ABSOLUTELY NO WARRANTY.
 * See the file `COPYING' in this directory.
 */

#include "signpr_wav.h"
#include "fmtheaders.h"
#include "errorwindow.h"
#include "signpr_general.h"
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include "endian.h"

/* ----- SOURCE & READING -------------------------------------------------- */

FILE *sourcefile;

int
openwavsource (char *filename)
/* returns 0: failure (error_window has been displayed), 1: success.
   More or less adapted from bplay.c, with stdio-patch by Axel Kohlmeyer
 */
{
  int count;
  char hd_buf[20];
  wavhead wavhd;

  if ((sourcefile = fopen (filename, "rb")) == NULL)
    {
      error_window ("The source file could not be opened.");
      return 0;
    }

  count = fread (hd_buf, 1, 20, sourcefile);
  if (count < 20)
    {
      fclose (sourcefile);
      error_window ("The source file could not be read, or is too short.");
      return 0;
    }

  if (strstr (hd_buf, "RIFF") == NULL)
    {
      fclose (sourcefile);
      error_window ("The source file is not a .wav file, and can not be \
processed.");
      return 0;
    }

  memcpy ((void *) &wavhd, (void *) hd_buf, 20);
  count = fread (((char *) &wavhd) + 20, 1, sizeof (wavhd) - 20, sourcefile);

  if (count < sizeof (wavhd) - 20)
    {
      fclose (sourcefile);
      error_window ("The source file is too short. Probably it is not \
a .wav sound file.");
      return 0;
    }

#ifdef SWAP_ENDIAN
  wavhd.format = SwapTwoBytes (wavhd.format);
  wavhd.sample_fq = SwapFourBytes (wavhd.sample_fq);
  wavhd.bit_p_spl = SwapTwoBytes (wavhd.bit_p_spl);
  wavhd.modus = SwapTwoBytes (wavhd.modus);
#endif

  if (wavhd.format != 1)
    {
      fclose (sourcefile);
      error_window ("The source file is a .wav file with unknown format, \
and can not be processed.");
      return 0;
    }

  if (wavhd.sample_fq != 44100)
    {
      fclose (sourcefile);
      error_window ("The source file is not sampled at 44100 Hz, and can \
not be processed.");
      return 0;
    }

  if (wavhd.bit_p_spl != 16)
    {
      fclose (sourcefile);
      error_window ("The source file does not have 16 bit samples, and \
can not be processed.");
      return 0;
    }

  if (wavhd.modus != 2)
    {
      fclose (sourcefile);
      error_window ("The source file is not stereo, and can not be \
processed.");
      return 0;
    }

  /* Well, everything seems to be OK */
  return 1;
}

void
closewavsource ()
{
  fclose (sourcefile);
}

int
seeksamplesource (long samplenumber)
/* returns 0: failure, 1: success */
{
  struct stat buf;

  if (fstat (fileno (sourcefile), &buf))
    return 0;

  if (sizeof (wavhead) + 2 * 2 * samplenumber >= buf.st_size)
    return 0;

  if (fseek (sourcefile, sizeof (wavhead) + 2 * 2 * samplenumber, SEEK_SET) > -1)
    return 1;
  else
    return 0;
}

sample_t
readsamplesource ()
{
  sample_t sample;

  if (fread (&sample, 1, 4, sourcefile) != 4)
    {
      /* reading after end of file - this just happens when using
         pre-read buffers! */
      sample.left = 0;
      sample.right = 0;
    }

#ifdef SWAP_ENDIAN
  sample = SwapSample (sample);
#endif

  return sample;
}


/* ----- DESTINATION & WRITING --------------------------------------------- */

FILE *destfile;
int destfileispipe;		/* remember open() - - -> close() */

int
openwavdest (char *filename, long bcount)
/* returns 0: failure (error_window has NOT been displayed), 1: success.
   bcount must be 4 * number_of_samples !
   Adapted from bplay.c
 */
{
  wavhead header;
  char *riff = "RIFF";
  char *wave = "WAVE";
  char *fmt = "fmt ";
  char *data = "data";

  if (*filename == '|')
    {
      destfileispipe = 1;	/* remember for closing */
      if ((destfile = popen (filename + 1, "w")) == NULL)
	return 0;
    }
  else
    {
      destfileispipe = 0;
      if ((destfile = fopen (filename, "wb")) == NULL)
	return 0;
    }


  memcpy (&(header.main_chunk), riff, 4);
  header.length = sizeof (wavhead) - 8 + bcount;
  memcpy (&(header.chunk_type), wave, 4);

  memcpy (&(header.sub_chunk), fmt, 4);
  header.sc_len = 16;
  header.format = 1;
  header.modus = /*stereo */ 1 + 1;
  header.sample_fq = /*speed */ 44100;
  header.byte_p_sec = /*((bits > 8)? 2:1)*(stereo+1)*speed */ 2 * 2 * 44100;
  header.byte_p_spl = /*(bits > 8)? 2:1 */ 4;	/* stereo & 16 bit..? */
  header.bit_p_spl = /*bits */ 16;	/* stereo doesn't count here? */

  memcpy (&(header.data_chunk), data, 4);
  header.data_length = bcount;

#ifdef SWAP_ENDIAN
  header.length = SwapFourBytes (header.length);
  header.sc_len = SwapFourBytes (header.sc_len);
  header.format = SwapTwoBytes (header.format);
  header.modus = SwapTwoBytes (header.modus);
  header.sample_fq = SwapFourBytes (header.sample_fq);
  header.byte_p_sec = SwapFourBytes (header.byte_p_sec);
  header.byte_p_spl = SwapTwoBytes (header.byte_p_spl);
  header.bit_p_spl = SwapTwoBytes (header.bit_p_spl);
  header.data_length = SwapFourBytes (header.data_length);
#endif

  fwrite (&header, sizeof (header), 1, destfile);

  return 1;
}

void
closewavdest ()
{
  if (destfileispipe)
    pclose (destfile);
  else
    fclose (destfile);
}

void
writesampledest (sample_t sample)
{

#ifdef SWAP_ENDIAN
  sample = SwapSample (sample);
#endif

  fwrite (&sample, 4, 1, destfile);
}