File: sound2sun.c

package info (click to toggle)
sox 12.16-6
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,180 kB
  • ctags: 1,466
  • sloc: ansic: 16,658; sh: 2,071; makefile: 126
file content (201 lines) | stat: -rw-r--r-- 6,252 bytes parent folder | download | duplicates (3)
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
/************************************************************************/
/*      Copyright 1989 by Rich Gopstein and Harris Corporation          */
/*                                                                      */
/*      Permission to use, copy, modify, and distribute this software   */
/*      and its documentation for any purpose and without fee is        */
/*      hereby granted, provided that the above copyright notice        */
/*      appears in all copies and that both that copyright notice and   */
/*      this permission notice appear in supporting documentation, and  */
/*      that the name of Rich Gopstein and Harris Corporation not be    */
/*      used in advertising or publicity pertaining to distribution     */
/*      of the software without specific, written prior permission.     */
/*      Rich Gopstein and Harris Corporation make no representations    */
/*      about the suitability of this software for any purpose.  It     */
/*      provided "as is" without express or implied warranty.           */
/************************************************************************/

/************************************************************************/
/* sound2sun.c - Convert sampled audio files into uLAW format for the   */
/*               Sparcstation 1.                                        */
/*               Send comments to ..!rutgers!soleil!gopstein            */
/************************************************************************/
/*									*/
/*  Modified November 27, 1989 to convert to 8000 samples/sec           */
/*   (contrary to man page)                                             */
/*  Modified December 13, 1992 to write standard Sun .au header with	*/
/*   unspecified length.  Also made miscellaneous changes for 		*/
/*   VMS port.  (K. S. Kubo, ken@hmcvax.claremont.edu)			*/
/*  Fixed Bug with converting slow sample speeds			*/
/*									*/
/************************************************************************/


#include <stdio.h>

#define DEFAULT_FREQUENCY 11000

#ifdef VAXC
#define	READ_OPEN	"r", "mbf=16", "shr=get"
#define WR_OPEN		"w", "mbf=16"
#else
#define READ_OPEN	"r"
#define WR_OPEN		"w"
#endif

FILE *infile, *outfile;

/* convert two's complement ch into uLAW format */

unsigned int cvt(ch)
int ch;
{

  int mask;

  if (ch < 0) {
    ch = -ch;
    mask = 0x7f;
  } else {
    mask = 0xff;
  }

  if (ch < 32) {
    ch = 0xF0 | 15 - (ch / 2);
  } else if (ch < 96) {
    ch = 0xE0 | 15 - (ch - 32) / 4;
  } else if (ch < 224) {
    ch = 0xD0 | 15 - (ch - 96) / 8;
  } else if (ch < 480) {
    ch = 0xC0 | 15 - (ch - 224) / 16;
  } else if (ch < 992) {
    ch = 0xB0 | 15 - (ch - 480) / 32;
  } else if (ch < 2016) {
    ch = 0xA0 | 15 - (ch - 992) / 64;
  } else if (ch < 4064) {
    ch = 0x90 | 15 - (ch - 2016) / 128;
  } else if (ch < 8160) {
    ch = 0x80 | 15 - (ch - 4064) /  256;
  } else {
    ch = 0x80;
  }
return (mask & ch);
}

/* write a "standard" sun header with an unspecified length */
#define wrulong(fp, ul) putc((ul >> 24) & 0xff, fp); \
    putc((ul >> 16) & 0xff, fp); putc((ul >> 8) & 0xff, fp); \
    putc(ul & 0xff, fp);

static void
wr_header(optr)
FILE *optr;
{
    wrulong(optr, 0x2e736e64);	/* Sun magic */
    wrulong(optr, 24);		/* header size in bytes */
    wrulong(optr, ((unsigned)(~0)));	/* unspecified data size */
    wrulong(optr, 1);		/* Sun uLaw format */
    wrulong(optr, 8000);	/* sample rate by definition :-) */
    wrulong(optr, 1);		/* single channel */
}

/*******************************************************
/*                                                     */
/* Usage is "sound2sun [-f frequency] infile outfile"  */
/*                                                     */
/* "frequency" is the samples per second of the infile */
/* the outfile is always 8000 samples per second.      */
/*                                                     */
/*******************************************************/

/***********************************************************************/
/*                                                                     */
/* The input file is expected to be a stream of one-byte excess-128    */
/* samples.  Each sample is converted to 2's complement by subtracting */
/* 128, then converted to uLAW and output.  We calculate the proper    */
/* number of input bytes to skip in order to make the sample frequency */
/* convert to 8000/sec properly.  Interpolation could be added, but it */
/* doesn't appear to be necessary.                                     */
/*                                                                     */
/***********************************************************************/


main(argc, argv)
int argc;
char *argv[];
{

  float sum = 0;
  float frequency, increment;

  unsigned char ch;
  unsigned char ulaw;

  int chr;

  if ((argc != 3) && (argc != 5)) {
    fprintf(stderr,"Usage: sound2sun [-f frequency] infile outfile\n");
    exit(1);
  }

  if (argc == 5) {
    if (strcmp(argv[1], "-f") != 0) {
      fprintf(stderr, "Usage: sound2sun [-f frequency] infile outfile\n");
      exit(1);
    } else {
      frequency = atoi(argv[2]);
    }
  } else {
    frequency = DEFAULT_FREQUENCY;
  }

  if ((infile = fopen(argv[argc-2], READ_OPEN)) == NULL) {
    perror("Error opening infile");
    exit(0);
  }

  if ((outfile = fopen(argv[argc-1], WR_OPEN)) == NULL) {
    perror("Error opening outfile");
    exit(0);
  }

  wr_header(outfile);

  /* increment is the number of bytes to read each time */

  increment = frequency / 8000;

  ch = fgetc(infile);

  while (!feof(infile)) {

    /* convert the excess 128 to two's complement */

    chr = 0x80 - ch;

    /* increase the volume */
    /* convert to uLAW */

    ulaw = cvt(chr * 16);

    /* output it */

    fputc((char) ulaw, outfile);

    /* skip enough input bytes to compensate for sampling frequency diff */

    sum += increment;

    while(sum > 0) {
      if (!feof(infile)) ch = fgetc(infile);
      sum--;
    }

  }

  fclose(infile);
  fclose(outfile);
}

/*  DEC/CMS REPLACEMENT HISTORY, Element SOUND2SUN.C */
/*  *1    14-DEC-1992 17:46:37 CENYDD "main program" */
/*  DEC/CMS REPLACEMENT HISTORY, Element SOUND2SUN.C */