File: ux_audio_oss.c

package info (click to toggle)
frotz 2.40-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 468 kB
  • ctags: 891
  • sloc: ansic: 6,774; makefile: 143; sh: 63
file content (363 lines) | stat: -rw-r--r-- 8,135 bytes parent folder | download
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
 * ux_audio_oss.c - Sound support using the OSS drivers
 *
 * This code is mostly verbatim from the file x_sample.c in Daniel
 * Schepler's xfrotz-2.32.1.
 *
 * This file is part of Frotz.
 *
 * Frotz 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.
 *
 * Frotz 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
 */

#define __UNIX_PORT_FILE

#include <signal.h>
/* #include <bits/sigaction.h> */


#ifdef USE_NCURSES_H
#include <ncurses.h>
#else
#include <curses.h>
#endif

#include "frotz.h"
#include "ux_frotz.h"

#ifdef OSS_SOUND	/* don't compile this if not using OSS */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include "soundcard.h"
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/wait.h>


extern void end_of_sound(void);

/* Buffer used to store sample data */
static char *sound_buffer = NULL;
static int sound_length;
static int sample_rate;
static int current_num;

/* Implementation of the separate process which plays the sounds.
   The signals used to communicate with the process are:
     SIGINT - complete current repeat of the sound, then quit
     SIGTERM - stop sound immediately
*/

static pid_t child_pid;

/* Number of repeats left */
static int num_repeats;

/* File handles for mixer and PCM devices */
static int mixer_fd, dsp_fd;

static int old_volume;

static void sigterm_handler(int signal) {
  ioctl(dsp_fd, SNDCTL_DSP_RESET, 0);
  if (mixer_fd >= 0)
    ioctl(mixer_fd, SOUND_MIXER_WRITE_VOLUME, &old_volume);
  _exit(0);
}

static void sigint_handler(int signal) {
  num_repeats = 1;
}

static void play_sound(int volume, int repeats) {
  struct sigaction sa;

  dsp_fd = open("/dev/dsp", O_WRONLY);
  if (dsp_fd < 0) {
    perror("/dev/dsp");
    _exit(1);
  }
  ioctl(dsp_fd, SNDCTL_DSP_SPEED, &sample_rate);

  if (volume != 255) {
    mixer_fd = open("/dev/mixer", O_RDWR);
    if (mixer_fd < 0)
      perror("/dev/mixer");
    else {
      int new_vol;
      ioctl(mixer_fd, SOUND_MIXER_READ_VOLUME, &old_volume);
      new_vol = volume * 100 / 8;
      new_vol = (new_vol << 8) | new_vol;
      ioctl(mixer_fd, SOUND_MIXER_WRITE_VOLUME, &new_vol);
    }
  }
  else
    mixer_fd = -1;

  sa.sa_handler = sigterm_handler;
  sigemptyset(&sa.sa_mask);
  sigaddset(&sa.sa_mask, SIGINT);
  sigaddset(&sa.sa_mask, SIGTERM);
  sa.sa_flags = 0;
  sigaction(SIGTERM, &sa, NULL);
  sa.sa_handler = sigint_handler;
  sigaction(SIGINT, &sa, NULL);

  for (num_repeats = repeats; num_repeats > 0;
       num_repeats < 255 ? num_repeats-- : 0) {
    char *curr_pos = sound_buffer;
    int len_left = sound_length;
    int write_result;

    while (len_left > 0) {
      write_result = write(dsp_fd, curr_pos, len_left);
      if (write_result <= 0) {
        perror("write on /dev/dsp");
        goto finish;
      }
      curr_pos += write_result;
      len_left -= write_result;
    }
  }

 finish:
  ioctl(dsp_fd, SNDCTL_DSP_SYNC, 0);
  if (mixer_fd >= 0)
    ioctl(mixer_fd, SOUND_MIXER_WRITE_VOLUME, &old_volume);
  _exit(0);
}


/*
 * os_beep
 *
 * Play a beep sound. Ideally, the sound should be high- (number == 1)
 * or low-pitched (number == 2).
 *
 */

void os_beep (int number)
{

    /* This should later be expanded to support high and low beeps as well
     * as checking to see if Frotz was started in quiet mode.
     */

    beep();

}/* os_beep */



/*
 * os_prepare_sample
 *
 * Load the sample from the disk.
 *
 */

void os_prepare_sample (int number)
{
  FILE *samples;
  char *filename;
  const char *basename, *dotpos;
  int namelen;

  if (sound_buffer != NULL && current_num == number)
    return;

  free(sound_buffer);
  sound_buffer = NULL;

  filename = malloc(strlen(story_name) + 10);

  if (! filename)
    return;

  basename = strrchr(story_name, '/');
  if (basename) basename++; else basename = story_name;
  dotpos = strrchr(basename, '.');
  namelen = (dotpos ? dotpos - basename : strlen(basename));
  if (namelen > 6) namelen = 6;
  sprintf(filename, "%.*ssound/%.*s%02d.snd",
          basename - story_name, story_name, 
          namelen, basename, number);



  samples = fopen(filename, "r");

  if (samples == NULL) {
    perror(filename);
    return;
  }

  fgetc(samples); fgetc(samples); fgetc(samples); fgetc(samples);
  sample_rate = fgetc(samples) << 8;
  sample_rate |= fgetc(samples);
  fgetc(samples); fgetc(samples);
  sound_length = fgetc(samples) << 8;
  sound_length |= fgetc(samples);

  sound_buffer = malloc(sound_length);
  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 {
      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;
  struct sigaction sa;

  waitpid(child_pid, &status, WNOHANG);
  child_pid = 0;
  sa.sa_handler = SIG_IGN;
  sigemptyset(&sa.sa_mask);
  sa.sa_flags = 0;
  sigaction(SIGCHLD, &sa, NULL);
  end_of_sound();
}

/*
 * os_start_sample
 *
 * Play the given sample at the given volume (ranging from 1 to 8 and
 * 255 meaning a default volume). The sound is played once or several
 * times in the background (255 meaning forever). In Z-code 3 the
 * repeats value is always 0 and the number of repeats is taken from
 * the sound file itself. The end_of_sound function is called as soon
 * as the sound finishes.
 *
 */

void os_start_sample (int number, int volume, int repeats)
{
  sigset_t sigchld_mask;
  struct sigaction sa;

  os_prepare_sample(number);
  if (! sound_buffer)
    return;
  os_stop_sample();

  sigemptyset(&sigchld_mask);
  sigaddset(&sigchld_mask, SIGCHLD);
  sigprocmask(SIG_BLOCK, &sigchld_mask, NULL);

  child_pid = fork();

  if (child_pid < 0) {          /* error in fork */
    perror("fork");
    return;
  }
  else if (child_pid == 0) {    /* child */
    sigprocmask(SIG_UNBLOCK, &sigchld_mask, NULL);
    play_sound(volume, repeats);
  }
  else {                        /* parent */
    sa.sa_handler = sigchld_handler;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    sigaction(SIGCHLD, &sa, NULL);

    sigprocmask(SIG_UNBLOCK, &sigchld_mask, NULL);
  }
}/* os_start_sample */

/* Send the specified signal to the player program, then wait for
   it to exit. */

static void stop_player(int signal) {
  sigset_t sigchld_mask;
  struct sigaction sa;
  int status;

  sigemptyset(&sigchld_mask);
  sigaddset(&sigchld_mask, SIGCHLD);
  sigprocmask(SIG_BLOCK, &sigchld_mask, NULL);

  if (child_pid == 0) {
    sigprocmask(SIG_UNBLOCK, &sigchld_mask, NULL);
    return;
  }
  kill(child_pid, signal);
  waitpid(child_pid, &status, 0);
  child_pid = 0;

  sa.sa_handler = SIG_IGN;
  sigemptyset(&sa.sa_mask);
  sa.sa_flags = 0;
  sigaction(SIGCHLD, &sa, NULL);

  sigprocmask(SIG_UNBLOCK, &sigchld_mask, NULL);
}

/*
 * os_stop_sample
 *
 * Turn off the current sample.
 *
 */

void os_stop_sample (void)
{
  stop_player(SIGTERM);
}/* os_stop_sample */

/*
 * os_finish_with_sample
 *
 * Remove the current sample from memory (if any).
 *
 */

void os_finish_with_sample (void)
{
  free(sound_buffer);
  sound_buffer = NULL;
}/* os_finish_with_sample */

/*
 * os_wait_sample
 *
 * Stop repeating the current sample and wait until it finishes.
 *
 */

void os_wait_sample (void)
{
  stop_player(SIGINT);
}/* os_wait_sample */

#endif /* OSS_SOUND */