File: cdr.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 (159 lines) | stat: -rw-r--r-- 3,178 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
/*
 * CD-R format handler
 *
 * David Elliott, Sony Microsystems -  July 5, 1991
 *
 * Copyright 1991 David Elliott And Sundry Contributors
 * This source code is freely redistributable and may be used for
 * any purpose.  This copyright notice must be maintained. 
 * Lance Norskog And Sundry Contributors are not responsible for 
 * the consequences of using this software.
 *
 * This code automatically handles endianness differences
 *
 * cbagwell (cbagwell@sprynet.com) - 20 April 1998
 *
 *   Changed endianness handling.  Seemed to be reversed (since format
 *   is in big endian) and made it so that user could always override
 *   swapping no matter what endian machine they are one.
 *
 *   Fixed bug were trash could be appended to end of file for certain
 *   endian machines.
 *
 */

#include "st.h"

#define SECTORSIZE	(2352 / 2)

/* Private data for SKEL file */
typedef struct cdrstuff {
	LONG	samples;	/* number of samples written */
} *cdr_t;

LONG rawread(P3(ft_t, LONG *, LONG));
void rawwrite(P3(ft_t, LONG *, LONG));

/*
 * Do anything required before you start reading samples.
 * Read file header. 
 *	Find out sampling rate, 
 *	size and style of samples, 
 *	mono/stereo/quad.
 */

void cdrstartread(ft) 
ft_t ft;
{

	int     littlendian = 1;
	char    *endptr;

	/* Needed because of rawread() */
	rawstartread(ft);

	endptr = (char *) &littlendian;
	/* CDR is in Big Endian format.  Swap whats read in on */
        /* Little Endian machines.                             */
	if (*endptr)
	{ 
	    ft->swap = ft->swap ? 0 : 1;
	}

	ft->info.rate = 44100L;
	ft->info.size = WORD;
	ft->info.style = SIGN2;
	ft->info.channels = 2;
	ft->comment = NULL;
}

/*
 * Read up to len samples from file.
 * Convert to signed longs.
 * Place in buf[].
 * Return number of samples read.
 */

LONG cdrread(ft, buf, len) 
ft_t ft;
LONG *buf, len;
{

	return rawread(ft, buf, len);
}

/*
 * Do anything required when you stop reading samples.  
 * Don't close input file! 
 */
void cdrstopread(ft) 
ft_t ft;
{
	/* Needed because of rawread() */
	rawstopread(ft);
}

void cdrstartwrite(ft) 
ft_t ft;
{
	cdr_t cdr = (cdr_t) ft->priv;

	int     littlendian = 1;
	char    *endptr;

	endptr = (char *) &littlendian;
	/* CDR is in Big Endian format.  Swap whats written out on */
	/* Little Endian Machines.                                 */
	if (*endptr) 
	{
	    ft->swap = ft->swap ? 0 : 1;
	}

	/* Needed because of rawwrite() */
	rawstartwrite(ft);

	cdr->samples = 0;

	ft->info.rate = 44100L;
	ft->info.size = WORD;
	ft->info.style = SIGN2;
	ft->info.channels = 2;
}

void cdrwrite(ft, buf, len) 
ft_t ft;
LONG *buf, len;
{
	cdr_t cdr = (cdr_t) ft->priv;

	cdr->samples += len;

	rawwrite(ft, buf, len);
}

/*
 * A CD-R file needs to be padded to SECTORSIZE, which is in terms of
 * samples.  We write -32768 for each sample to pad it out.
 */

void cdrstopwrite(ft) 
ft_t ft;
{
	cdr_t cdr = (cdr_t) ft->priv;
	int padsamps = SECTORSIZE - (cdr->samples % SECTORSIZE);
	short zero;

	/* Flush buffer before writing anything else */
	rawstopwrite(ft);

	zero = 0;

	if (padsamps != SECTORSIZE) 
	{
		while (padsamps > 0) {
			wshort(ft, zero);
			padsamps--;
		}
	}
}