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
|
$NetBSD: patch-ac,v 1.1 2002/06/29 01:48:53 kristerw Exp $
--- ux_audio_oss.c.orig Wed Nov 22 07:31:17 2000
+++ ux_audio_oss.c Sat Jun 29 03:25:37 2002
@@ -86,12 +86,37 @@
static void play_sound(int volume, int repeats) {
struct sigaction sa;
+ int format = AFMT_U8;
+ int channels = 1;
- dsp_fd = open("/dev/dsp", O_WRONLY);
+ dsp_fd = open(SOUND_DEV, O_WRONLY);
if (dsp_fd < 0) {
- perror("/dev/dsp");
+ perror(SOUND_DEV);
_exit(1);
}
+
+ /* This section of code fixes the nasty problem of samples
+ * being played with pops and scratches when used with a
+ * non-Linux system implementing OSS sound.
+ */
+ if (ioctl(dsp_fd, SNDCTL_DSP_SETFMT, &format) == -1) {
+ perror(SOUND_DEV);
+ exit(1);
+ }
+ if (format != AFMT_U8) {
+ fprintf(stderr, "bad sound format\n");
+ exit(1);
+ }
+ if (ioctl(dsp_fd, SNDCTL_DSP_CHANNELS, &channels) == -1) {
+ perror(SOUND_DEV);
+ exit(1);
+ }
+ if (channels != 1) {
+ fprintf(stderr, "bad channels\n");
+ exit(1);
+ }
+ /* End sound fix from Torbjorn Andersson */
+
ioctl(dsp_fd, SNDCTL_DSP_SPEED, &sample_rate);
if (volume != 255) {
@@ -177,6 +202,7 @@
char *filename;
const char *basename, *dotpos;
int namelen;
+ int read_length;
if (sound_buffer != NULL && current_num == number)
return;
@@ -211,27 +237,38 @@
fgetc(samples); fgetc(samples);
sound_length = fgetc(samples) << 8;
sound_length |= fgetc(samples);
+ sound_buffer = NULL;
+ if (sound_length > 0) {
sound_buffer = malloc(sound_length);
- if (! sound_buffer) {
+ if (!sound_buffer) {
perror("malloc");
return;
}
-
- if (sound_length < 0 ||
- fread(sound_buffer, 1, sound_length, samples) < sound_length) {
- if (feof(samples))
- fprintf(stderr, "%s: premature EOF\n", filename);
- else {
+ read_length = fread(sound_buffer, 1, sound_length, samples);
+ if (read_length < sound_length) {
+ if (feof(samples)) {
+ /*
+ * One of the Sherlock samples trigger this for me, so let's make it
+ * a non-fatal error.
+ */
+ sound_buffer = realloc(sound_buffer, read_length);
+ if (! sound_buffer) {
+ perror("realloc");
+ return;
+ }
+ sound_length = read_length;
+ } else {
errno = ferror(samples);
perror(filename);
- }
free(sound_buffer);
sound_buffer = NULL;
}
-
+ }
+ }
current_num = number;
}/* os_prepare_sample */
+
static void sigchld_handler(int signal) {
int status;
|