File: gsm.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 (162 lines) | stat: -rw-r--r-- 3,502 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
#if defined(HAVE_LIBGSM)
/*
 * Copyright 1991, 1992, 1993 Guido van Rossum And Sundry Contributors.
 * This source code is freely redistributable and may be used for
 * any purpose.  This copyright notice must be maintained. 
 * Guido van Rossum And Sundry Contributors are not responsible for 
 * the consequences of using this software.
 */

/*
 * GSM 06.10 courtesy Communications and Operating Systems Research Group,
 * Technische Universitaet Berlin
 *
 * More information on this format can be obtained from
 * http://www.cs.tu-berlin.de/~jutta/toast.html
 *
 * Source is available from ftp://ftp.cs.tu-berlin.de/pub/local/kbs/tubmik/gsm
 *
 * Written 26 Jan 1995 by Andrew Pam
 * Portions Copyright (c) 1995 Serious Cybernetics
 *
 * July 19, 1998 - Chris Bagwell (cbagwell@sprynet.com)
 *   Added GSM support to SOX from patches floating around with the help
 *   of Dima Barsky (ess2db@ee.surrey.ac.uk).
 */

#include "st.h"
#include "gsm.h"

/* Private data */
struct gsmpriv {
	gsm		handle;
	gsm_signal	sample[160];
	int		index;
};

void
gsmstartread(ft) 
ft_t ft;
{
	struct gsmpriv *p = (struct gsmpriv *) ft->priv;

	/* Sanity check */
	if (sizeof(struct gsmpriv) > PRIVSIZE)
		fail(
"struct gsmpriv is too big (%d); change PRIVSIZE in st.h and recompile sox",
		     sizeof(struct gsmpriv));

	ft->info.style = GSM;
	ft->info.size = BYTE;
	if (!ft->info.rate)
		ft->info.rate = 8000;
	p->handle = gsm_create();
	if (!p->handle)
		fail("unable to create GSM stream");
	p->index = 0;
}

void
gsmstartwrite(ft)
ft_t ft;
{
	struct gsmpriv *p = (struct gsmpriv *) ft->priv;

	ft->info.style = GSM;
	ft->info.size = BYTE;
	if (!ft->info.rate)
		ft->info.rate = 8000;
	p->handle = gsm_create();
	if (!p->handle)
		fail("unable to create GSM stream");
	p->index = 0;
}

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

LONG
gsmread(ft, buf, samp)
ft_t ft;
long *buf, samp;
{
	int bytes;
	int done = 0;
	gsm_frame	frame;
	struct gsmpriv *p = (struct gsmpriv *) ft->priv;

	while (p->index && (p->index < 160) && (done < samp))
		buf[done++] = LEFT(p->sample[p->index++], 16);

	while (done < samp)
	{
		p->index = 0;
		bytes = fread( frame, 1, sizeof(frame), ft->fp );
		if (bytes <= 0)
			return done;
		if (bytes < sizeof(frame))
			fail("invalid frame size: %d bytes", bytes);
		if (gsm_decode(p->handle, frame, p->sample) < 0)
			fail("error during GSM decode");
		while ((p->index < 160) && (done < samp))
			buf[done++] = LEFT(p->sample[p->index++], 16);
	}

	return done;
}

int
gsmwrite(ft, buf, samp)
ft_t ft;
long *buf, samp;
{
	int done = 0;
	gsm_frame	frame;
	struct gsmpriv *p = (struct gsmpriv *) ft->priv;

	while (done < samp)
	{
		while ((p->index < 160) && (done < samp))
			p->sample[p->index++] = RIGHT(buf[done++], 16);
		if (p->index < 160)
			return done;
		gsm_encode(p->handle, p->sample, frame);
		if (fwrite(frame, 1, sizeof(frame), ft->fp) != sizeof(frame))
			fail("write error");
		p->index = 0;
	}

	return done;
}

void
gsmstopread(ft)
ft_t ft;
{
	struct gsmpriv *p = (struct gsmpriv *) ft->priv;

	gsm_destroy(p->handle);
}

void
gsmstopwrite(ft)
ft_t ft;
{
	gsm_frame	frame;
	struct gsmpriv *p = (struct gsmpriv *) ft->priv;

	if (p->index)
	{
		while (p->index < 160)
			p->sample[p->index++] = 0;
		gsm_encode(p->handle, p->sample, frame);
		if (fwrite(frame, 1, sizeof(frame), ft->fp) != sizeof(frame))
			fail("write error");
	}
	gsm_destroy(p->handle);
}
#endif /* HAVE_LIBGSM */