Debian Codegraph @

src/audio/auclient.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 (snd) headered waveform to named socket */
41 /* */
42 /* Client side */
43 /* */
44 /*************************************************************************/
45 #include <stdlib.h>
46 #include "cst_file.h"
47 #include "cst_socket.h"
48 #include "cst_string.h"
49 #include "cst_wave.h"
50 #include "cst_audio.h"
51
52 #ifndef CST_NO_SOCKETS
53
54 #include <unistd.h>
55
56 int play_wave_client(cst_wave *w,const char *servername,int port,
57 const char *encoding)
58 {
59 int audiofd,q,i,n,r;
60 int sample_width;
61 unsigned char bytes[CST_AUDIOBUFFSIZE];
62 short shorts[CST_AUDIOBUFFSIZE];
63 snd_header header;
64
65 if (!w)
66 return CST_ERROR_FORMAT;
67
68 if ((audiofd = cst_socket_open(servername,port)) == 0)
69 return CST_ERROR_FORMAT;
70
71 header.magic = (unsigned int)0x2e736e64;
72 header.hdr_size = sizeof(header);
73 if (cst_streq(encoding,"ulaw"))
74 {
75 sample_width = 1;
76 header.encoding = 1; /* ulaw */
77 }
78 else if (cst_streq(encoding,"uchar"))
79 {
80 sample_width = 1;
81 header.encoding = 2; /* unsigned char */
82 }
83 else
84 {
85 sample_width = 2;
86 header.encoding = 3; /* short */
87 }
88 header.data_size = sample_width * w->num_samples * w->num_channels;
89 header.sample_rate = w->sample_rate;
90 header.channels = w->num_channels;
91 if (CST_LITTLE_ENDIAN)
92 { /* If I'm intel etc swap things, so "network byte order" */
93 header.magic = SWAPINT(header.magic);
94 header.hdr_size = SWAPINT(header.hdr_size);
95 header.data_size = SWAPINT(header.data_size);
96 header.encoding = SWAPINT(header.encoding);
97 header.sample_rate = SWAPINT(header.sample_rate);
98 header.channels = SWAPINT(header.channels);
99 }
100
101 if (write(audiofd, &header, sizeof(header)) != sizeof(header))
102 {
103 cst_errmsg("auclinet: failed to write header to server\n");
104 return CST_ERROR_FORMAT;
105 }
106
107 for (i=0; i < w->num_samples; i += r)
108 {
109 if (w->num_samples > i+CST_AUDIOBUFFSIZE)
110 n = CST_AUDIOBUFFSIZE;
111 else
112 n = w->num_samples-i;
113 if (cst_streq(encoding,"ulaw"))
114 {
115 for (q=0; q<n; q++)
116 bytes[q] = cst_short_to_ulaw(w->samples[i+q]);
117 r = write(audiofd,bytes,n);
118 }
119 else
120 {
121 for (q=0; q<n; q++)
122 if (CST_LITTLE_ENDIAN)
123 shorts[q] = SWAPSHORT(w->samples[i+q]);
124 else
125 shorts[q] = w->samples[i+q];
126 r = write(audiofd,shorts,n*2);
127 r /= 2;
128 }
129 if (r <= 0)
130 cst_errmsg("failed to write %d samples\n",n);
131 }
132
133 cst_socket_close(audiofd);
134
135 return CST_OK_FORMAT;
136 }
137
138 #endif