Debian Codegraph @

src/audio/auserver.c

raw
1 /*************************************************************************/
2 /* */
3 /* Language Technologies Institute */
4 /* Carnegie Mellon University */
5 /* Copyright (c) 2000 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author: Alan W Black ([email protected]) */
34 /* Date: October 2000 */
35 /*************************************************************************/
36 /* */
37 /* A client/server audio play program */
38 /* */
39 /* Server listens to a socket and play anything coming in (streaming) */
40 /* Client just sends (riff) headered waveform to named socket */
41 /* */
42 /*************************************************************************/
43 #include "cst_file.h"
44 #include <stdlib.h>
45 #include "cst_socket.h"
46 #include "cst_string.h"
47 #include "cst_wave.h"
48 #include "cst_file.h"
49 #include "cst_audio.h"
50
51 #ifndef CST_NO_SOCKETS
52
53 #ifdef _MSC_VER
54 #include <io.h>
55 #define read(fd, buffer, count) _read(fd, buffer, count)
56 #else
57 #include <unistd.h>
58 #endif
59
60 static int play_wave_from_socket(snd_header *header,int audiostream)
61 {
62 /* Read audio from stream and play it to audio device, converting */
63 /* it to pcm if required */
64 int num_samples;
65 int sample_width;
66 cst_audiodev *audio_device;
67 int q,i,n,r;
68 unsigned char bytes[CST_AUDIOBUFFSIZE];
69 short shorts[CST_AUDIOBUFFSIZE];
70 #ifdef DEBUG
71 cst_file fff;
72
73 fff = cst_fopen("/tmp/awb.wav",CST_OPEN_WRITE|CST_OPEN_BINARY);
74 #endif
75
76 if ((audio_device = audio_open(header->sample_rate,1,
77 (header->encoding == CST_SND_SHORT) ?
78 CST_AUDIO_LINEAR16 : CST_AUDIO_LINEAR8)) == NULL)
79 {
80 cst_errmsg("play_wave_from_socket: can't open audio device\n");
81 return -1;
82 }
83
84 if (header->encoding == CST_SND_SHORT)
85 sample_width = 2;
86 else
87 sample_width = 1;
88
89 num_samples = header->data_size / sample_width;
90 /* we naively let the num_channels sort itself out */
91 for (i=0; i < num_samples; i += r/2)
92 {
93 if (num_samples > i+CST_AUDIOBUFFSIZE)
94 n = CST_AUDIOBUFFSIZE;
95 else
96 n = num_samples-i;
97 if (header->encoding == CST_SND_ULAW)
98 {
99 r = read(audiostream,bytes,n);
100 for (q=0; q<r; q++)
101 shorts[q] = cst_ulaw_to_short(bytes[q]);
102 r *= 2;
103 }
104 else /* if (header->encoding == CST_SND_SHORT) */
105 {
106 r = read(audiostream,shorts,n*2);
107 if (CST_LITTLE_ENDIAN)
108 for (q=0; q<r/2; q++)
109 shorts[q] = SWAPSHORT(shorts[q]);
110 }
111
112 if (r <= 0)
113 { /* I'm not getting any data from the server */
114 audio_close(audio_device);
115 return CST_ERROR_FORMAT;
116 }
117
118 for (q=r; q > 0; q-=n)
119 {
120 n = audio_write(audio_device,shorts,q);
121 #ifdef DEBUG
122 cst_fwrite(fff,shorts,2,q);
123 #endif
124 if (n <= 0)
125 {
126 audio_close(audio_device);
127 return CST_ERROR_FORMAT;
128 }
129 }
130 }
131 audio_close(audio_device);
132 #ifdef DEBUG
133 cst_fclose(fff);
134 #endif
135
136 return CST_OK_FORMAT;
137
138 }
139
140 static int auserver_process_client(int client_name, int fd)
141 {
142 /* Gets called for each client */
143 snd_header header;
144 int r;
145
146 printf("client %d connected, ",client_name);
147 fflush(stdout);
148 /* Get header */
149 r = read(fd,&header,sizeof(header));
150 if (r != sizeof(header))
151 {
152 cst_errmsg("socket: connection didn't give a header\n");
153 return -1;
154 }
155 if (CST_LITTLE_ENDIAN)
156 {
157 header.magic = SWAPINT(header.magic);
158 header.hdr_size = SWAPINT(header.hdr_size);
159 header.data_size = SWAPINT(header.data_size);
160 header.encoding = SWAPINT(header.encoding);
161 header.sample_rate = SWAPINT(header.sample_rate);
162 header.channels = SWAPINT(header.channels);
163 }
164
165 if (header.magic != CST_SND_MAGIC)
166 {
167 cst_errmsg("socket: client something other than snd waveform\n");
168 return -1;
169 }
170
171 printf("%d bytes at %d rate, ", header.data_size, header.sample_rate);
172 fflush(stdout);
173
174 if (play_wave_from_socket(&header,fd) == CST_OK_FORMAT)
175 printf("successful\n");
176 else
177 printf("unsuccessful\n");
178
179 return 0;
180 }
181
182 int auserver(int port)
183 {
184 return cst_socket_server("audio",port,auserver_process_client);
185 }
186
187 #endif