forked from catmirrors/xlvns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audioinfo.c
286 lines (246 loc) · 5.77 KB
/
audioinfo.c
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* LEAF Visual Novel System For X
* (c) Copyright 1999,2000 Go Watanabe mailto:[email protected]
* All rights reserverd.
*
* ORIGINAL LVNS (c) Copyright 1996-1999 LEAF/AQUAPLUS Inc.
*
* $Id: audioinfo.c,v 1.6 2000/08/09 16:52:04 go Exp $
*
*/
/*
* サウンド再生用のモジュール
* 各 OS 用に同様のものを書き起こせば良いようにしたつもり…
* _AUDIOInfo は必要に応じて変更して下さい
*
* 情報保持用構造体
* struct _AUDIOInfo
*
* 初期化用
* AUDIOInfo *audioinfo_new(const char *audiodevice);
*
* 情報破棄用
* void audioinfo_delete(AUDIOInfo *ainfo)
*
* データ (11KHz, 16bit, Linear, Stereo, Little Endian) のべた再生
* void audioinfo_play(AUDIOInfo *ainfo, const char *data, size_t size);
*
* プロセスは LvnsCoreSound のほうで分けてあるのでそのまま出力するだけで
* かまいません
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/param.h>
#include "audioinfo.h"
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#ifdef ESDAUDIO
#include <esd.h>
struct _AUDIOInfo {
int fd;
};
AUDIOInfo *
audioinfo_new(const char *audiodevice)
{
AUDIOInfo *ainfo;
int fd;
if ((ainfo = malloc(sizeof *ainfo)) == NULL) {
perror("audioinfo_new");
exit(1);
}
if ((fd = esd_play_stream_fallback(ESD_BITS16 | ESD_STEREO | ESD_STREAM | ESD_PLAY, 11025, audiodevice, "xlvns")) < 0) {
fprintf(stderr, "ESD: can't connect to %s.\n", audiodevice);
free(ainfo);
return NULL;
}
ainfo->fd = fd;
return ainfo;
}
void
audioinfo_delete(AUDIOInfo *ainfo)
{
if (ainfo && ainfo->fd >= 0) {
close(ainfo->fd);
free(ainfo);
}
}
void
audioinfo_play(AUDIOInfo *ainfo, const char *data, size_t size)
{
if (ainfo->fd >= 0) {
write(ainfo->fd, data, size);
sleep(1); /* XXX */
}
}
#else
/*
* OpenBSD/Solaris 用 (/dev/audio インターフェース仕様)
*/
#if defined(__OpenBSD__) || (defined(sun) && defined(SVR4))
#include <sys/audioio.h>
struct _AUDIOInfo {
int fd;
};
AUDIOInfo *
audioinfo_new(const char *audiodevice)
{
AUDIOInfo *ainfo;
struct audio_info info;
int fd;
if ((ainfo = malloc(sizeof *ainfo)) == NULL) {
perror("audioinfo_new");
exit(1);
}
if ((fd = open(audiodevice, O_WRONLY, 0)) < 0) {
perror(audiodevice);
return NULL;
}
AUDIO_INITINFO(&info);
#if defined (__OpenBSD__)
info.play.sample_rate = 11000;
info.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
#else /* Solaris */
info.play.sample_rate = 11025;
info.play.encoding = AUDIO_ENCODING_LINEAR;
#endif
info.play.precision = 16;
info.play.channels = 2;
if (ioctl(fd, AUDIO_SETINFO, &info) < 0) {
perror(audiodevice);
close(fd);
return NULL;
}
ainfo->fd = fd;
return ainfo;
}
void
audioinfo_delete(AUDIOInfo *ainfo)
{
if (ainfo && ainfo->fd >= 0) {
close(ainfo->fd);
free(ainfo);
}
}
#if !defined(__OpenBSD__)
#include <sys/byteorder.h>
#endif
void
audioinfo_play(AUDIOInfo *ainfo, const char *data, size_t size)
{
size_t ret;
#if !defined(__OpenBSD__) && defined(_BIG_ENDIAN)
/* エンディアン変換する */
int i;
u_char *data2;
if ((data2 = malloc(size)) == NULL)
return;
for (i=0;i<size;i++) {
data2[i] = data[i+1];
data2[i+1] = data[i];
}
do {
if ((ret = write(ainfo->fd, data2, size)) < 0) {
perror("audioinfo_play");
return;
}
size -= ret;
data += ret;
} while (size > 3);
free(data2);
#else
do {
if ((ret = write(ainfo->fd, data, size)) < 0) {
perror("audioinfo_play");
return;
}
size -= ret;
data += ret;
} while (size > 3);
#endif
/* 演奏完了待ち */
ioctl(ainfo->fd, AUDIO_DRAIN, NULL);
}
/*
* FreeBSD/NetBSD/Linux 用コード (OSS互換仕様)
* OpenBSD もこれでもいけるはず…
*/
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__linux__)
#if defined(__FreeBSD__)
#include <machine/soundcard.h>
#elif defined(__NetBSD__)
#include <soundcard.h>
#elif defined(__linux__)
#include <sys/soundcard.h>
#endif
struct _AUDIOInfo {
int fd;
};
AUDIOInfo *
audioinfo_new(const char *audiodevice)
{
AUDIOInfo *ainfo;
int fd, t;
if ((ainfo = malloc(sizeof *ainfo)) == NULL) {
perror("audioinfo_new");
exit(1);
}
if ((fd = open(audiodevice, O_WRONLY, 0)) < 0) {
perror(audiodevice);
return NULL;
}
t = 16;
if (ioctl(fd, SNDCTL_DSP_SAMPLESIZE, &t) < 0) {
perror(audiodevice);
close(fd);
return NULL;
}
t = 1;
if (ioctl(fd, SNDCTL_DSP_STEREO, &t) < 0) {
perror(audiodevice);
close(fd);
return NULL;
}
t = 11000;
if (ioctl(fd, SNDCTL_DSP_SPEED, &t) < 0) {
perror(audiodevice);
close(fd);
return NULL;
}
ainfo->fd = fd;
return ainfo;
}
void
audioinfo_delete(AUDIOInfo *ainfo)
{
if (ainfo && ainfo->fd >= 0) {
close(ainfo->fd);
free(ainfo);
}
}
void
audioinfo_play(AUDIOInfo *ainfo, const char *data, size_t size)
{
size_t ret;
do {
if ((ret = write(ainfo->fd, data, size)) < 0) {
perror("audioinfo_play");
return;
}
size -= ret;
data += ret;
} while (size > 3);
/* 演奏完了待ち */
ioctl(ainfo->fd, SNDCTL_DSP_SYNC, NULL);
}
#else
/*
* 何もしないダミー関数
*/
struct _AUDIOInfo {};
AUDIOInfo *audioinfo_new(const char *audiodevice) { return NULL; }
void audioinfo_play(AUDIOInfo *ainfo, const char *data, size_t size) {};
void audioinfo_delete(AUDIOInfo *ainfo) {};
#endif
#endif