Repository navigation
Expand file tree
/
Copy pathssfunc.cpp
More file actions
246 lines (233 loc) · 5.68 KB
/
Copy pathssfunc.cpp
File metadata and controls
246 lines (233 loc) · 5.68 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
** ssfunc.cpp --- Utility functions
**
** Copyright (C) 2000, 2001 Bilingual Emacspeak Project <inoue@argv.org>
**
** Last modified: $Date: 2002/01/15 11:21:42 $ by $Author: seiken $
** Keywords: Emacs, Emacspeak, speech, Linux
**
** This file is part of BEP (Bilingual Emacspeak Project)
** <http://www.argv.org/bep/>
** This file is originally written by Gary Bishop, and modified by BEP
** to make a bilingual, Japanese and English, DECTalk-like speech server
** running under Windows.
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/soundcard.h>
#include <pthread.h>
#include <string>
#include "ssdefs.h"
// #include "dic.h"
// #include "dt.h"
#include "spcsrv.h"
#if SS_SYN_USE_ESD==TRUE
#include <esd.h>
#endif
char *ss_read_input(FILE *fp)
{
static char *buf = NULL;
static int buf_blocks;
int pos;
int c;
if (buf == NULL) {
buf = (char *)malloc(SS_INPUT_BLK_SIZE * sizeof(char));
if (buf == NULL) {
return NULL;
}
buf_blocks = 1;
}
pos = 0;
while ((c = fgetc(fp)) != EOF) {
buf[pos++] = c;
if (pos == SS_INPUT_BLK_SIZE * buf_blocks) {
buf = (char *)realloc(buf,
SS_INPUT_BLK_SIZE * (++buf_blocks));
}
if (c == '\n') {
buf[pos-1] = '\0';
return buf;
}
}
free (buf);
return NULL;
}
#if SS_SYN_USE_ESD==TRUE
void
ss_make_prog_name(char *namep, int len)
{
char hostname[MAXHOSTNAMELEN];
char pidStr[10];
int pid;
int iRet;
iRet = gethostname(hostname, MAXHOSTNAMELEN);
pid = getpid();
sprintf(pidStr, "%d", pid);
if ((int)(strlen(hostname) + strlen(pidStr)) < len - 2) {
strcpy(namep, hostname);
strcat(namep, pidStr);
}
printf("get prog_name: %s\n", namep);
return;
}
int open_audio(char *host, char *prog_namep)
{
int sock = -1;
int rate = 16000;
int bits = ESD_BITS16, channels = ESD_MONO;
int mode = ESD_STREAM;
int func = ESD_PLAY;
esd_format_t format = 0;
char prog_name[SS_MAX_PROG_NAME_LEN];
if (host == NULL) {
host = getenv(SS_SYN_HOST_ENV);
}
strcpy(prog_name, SS_SYN_PROG_NAME);
ss_make_prog_name(prog_name, SS_MAX_PROG_NAME_LEN);
if (prog_namep != NULL) {
strcpy(prog_namep, prog_name);
}
format = bits | channels | mode | func;
sock = esd_play_stream_fallback( format, rate, host, prog_name );
return sock;
}
#else
int open_audio(char *audevice)
{
int fd;
/*int dsp_samplesize = 16;*/
int stereo=FALSE;
int fmts=AFMT_S16_LE;
int dsp_speed=16000;
fd=open(audevice, O_WRONLY);
if (fd==-1) {
return -1;
}
ioctl (fd, SNDCTL_DSP_RESET);
ioctl (fd, SNDCTL_DSP_SPEED, &dsp_speed);
ioctl (fd, SNDCTL_DSP_STEREO, &stereo);
ioctl (fd, SNDCTL_DSP_SETFMT, &fmts);
return fd;
}
#endif
#if SS_SYN_USE_ESD==TRUE
int ss_close(int fd)
{
return esd_close(fd);
}
#else
int ss_close(int fd)
{
return close(fd);
}
#endif
int sync_audio(int fd)
{
return ioctl (fd, SNDCTL_DSP_SYNC);
}
#if SS_SYN_USE_ESD == TRUE
void ss_reset_audio(char *prog_name, char *host)
{
int sock = -1;
esd_info_t *einfop;
esd_player_info_t *eplp;
if (host == NULL) {
host = getenv("SPEECH_SERVER_HOST");
}
sock = esd_open_sound( host );
if (sock == -1) {
printf("can't open control channel\n");
return;
}
einfop = esd_get_all_info(sock);
eplp = einfop->player_list;
while(eplp != NULL) {
printf("%s\n", eplp->name);
if (strcmp(eplp->name, prog_name) == 0) break;
eplp = eplp->next;
}
if (eplp != NULL) {
printf("id: %d\n", eplp->source_id);
fflush(stdout);
esd_set_stream_pan(sock, eplp->source_id, 0, 0);
printf("set volume: 0:0\n");
}
else {
printf("set volume failed, stream %d\n", eplp->source_id);
}
esd_free_all_info(einfop);
esd_close(sock);
return;
}
#else
int ss_reset_audio(int fd)
{
return ioctl (fd, SNDCTL_DSP_RESET);
}
#endif
/* PCM$B%G!<%?$rC;=L$9$k(B */
int ssWarpPcm(short *data, int len, double factor)
{
double ratio = ((double)1. / factor);
int i, j;
double w;
for (i = 0; i < len; ++i) {
w = (double) i * ratio;
j = (int) w;
w -= (double) j;
if ((j + 1) >= len)
{
break;
}
data [i] = /*nint*/ (short)((double)data [j] * (1. - w)
+ (double)data [j + 1] * w + 0.5);
}
return i;
}
/* skip a `frames' frame of every `unit' frames. */
int ssSkipPcm(short *data, int len, int unit, int frames)
{
int i;
int outframes;
int nframes;
short *dfrom, *dto;
if (unit - frames <= 0) {
/* illegal argument. */
return len;
}
dfrom = dto = data;
nframes = (len / SS_SKIP_FRAME_SIZE) - unit + frames;
outframes = 0;
for (i = 0; i < nframes; i += unit) {
dfrom = data + i * SS_SKIP_FRAME_SIZE;
if (dfrom != dto) {
memcpy((void *)dto, (void *)dfrom,
sizeof(short)*SS_SKIP_FRAME_SIZE * (unit - frames));
}
outframes += unit - frames;
dto += SS_SKIP_FRAME_SIZE * (unit - frames);
}
return outframes * SS_SKIP_FRAME_SIZE;
}