File: obuffer_hp.cc

package info (click to toggle)
maplay 1.2b-5
  • links: PTS
  • area: main
  • in suites: slink
  • size: 432 kB
  • ctags: 441
  • sloc: cpp: 5,421; sh: 97; makefile: 86
file content (429 lines) | stat: -rw-r--r-- 11,217 bytes parent folder | download | duplicates (2)
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
 *  @(#) obuffer_hp.cc 1.4, last edit: 02 Mar 1995 18:37:33
 *  @(#) Copyright (C) 1993, 1994 Tobias Bading (bading@cs.tu-berlin.de)
 *  @(#) Berlin University of Technology
 *
 *  Many thanks to:
 *  -> John Brezak (brezak@apollo.hp.com)
 *     for his first HP implementation using the audio server
 *     (the current version does not use the audio server to keep
 *      ethernets on low traffic for playing DOOM :-)
 *
 *  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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 *  New in maplay 1.3
 */


// statics of class HpuxObuffer:
uint32		HpuxObuffer::ringbuffersize;
int16	       *HpuxObuffer::buffer;
char	       *HpuxObuffer::head;
int16	       *HpuxObuffer::tail[2];
uint32		HpuxObuffer::channels;
boil		HpuxObuffer::handler_activated;
boil		HpuxObuffer::buffer_empty;
itimerval	HpuxObuffer::timerval;
boil		HpuxObuffer::drain_buffer;
int		HpuxObuffer::audio_fd = -1;


HpuxObuffer::HpuxObuffer (uint32 number_of_channels, Header *header, boil use_speaker,
			  boil use_headphone, boil use_line_out, float volume)
{
#ifdef DEBUG
  if (!number_of_channels || number_of_channels > MAXCHANNELS)
  {
    cerr << "HpuxObuffer: 0 < number of channels < " << MAXCHANNELS << "!\n";
    exit (1);
  }
#endif
  if (audio_fd < 0)
  {
    cerr << "Internal error: HpuxObuffer::audio has to be initialized\n"
	    "by HpuxObuffer::class_suitable()!\n";
    exit (1);
  }

  if (!use_speaker && !use_headphone && !use_line_out)
    use_speaker = True;			// speaker is default

  // configure the audio device:
  if (ioctl (audio_fd, AUDIO_SET_DATA_FORMAT, AUDIO_FORMAT_LINEAR16BIT))
  {
    perror ("ioctl AUDIO_SET_DATA_FORMAT on /dev/audio");
    exit (1);
  }
  if (ioctl (audio_fd, AUDIO_SET_SAMPLE_RATE, header->frequency ()))
  {
    perror ("ioctl AUDIO_SET_SAMPLE_RATE on /dev/audio");
    exit (1);
  }
  if (ioctl (audio_fd, AUDIO_SET_CHANNELS, number_of_channels))
  {
    perror ("ioctl AUDIO_SET_CHANNELS on /dev/audio");
    exit (1);
  }
  int output_channel = 0;
  if (use_speaker)
    output_channel |= AUDIO_OUT_SPEAKER;
  if (use_headphone)
    output_channel |= AUDIO_OUT_HEADPHONE;
  if (use_line_out)
    output_channel |= AUDIO_OUT_LINE;
  if (ioctl (audio_fd, AUDIO_SET_OUTPUT, output_channel))
  {
    perror ("ioctl AUDIO_SET_OUTPUT on /dev/audio");
    exit (1);
  }

  if (volume >= 0.0 && volume <= 1.0)
  {
    // set volume:
    audio_describe description;
    audio_gains gains;
    if (ioctl (audio_fd, AUDIO_DESCRIBE, &description))
    {
      perror ("ioctl AUDIO_DESCRIBE on /dev/audio");
      exit (1);
    }
    if (ioctl (audio_fd, AUDIO_GET_GAINS, &gains))
    {
      perror ("ioctl AUDIO_GET_GAINS on /dev/audio");
      exit (1);
    }
    gains.transmit_gain = (int)((float)description.min_transmit_gain +
				(float)(description.max_transmit_gain
					- description.min_transmit_gain) * volume);
    if (ioctl (audio_fd, AUDIO_SET_GAINS, &gains))
    {
      perror ("ioctl AUDIO_GET_GAINS on /dev/audio");
      exit (1);
    }
  }

  // set device internal buffer size:
  if (ioctl (audio_fd, AUDIO_SET_TXBUFSIZE, TXBUFSIZE))
  {
    perror ("ioctl AUDIO_SET_TXBUFSIZE on /dev/audio");
    exit (1);
  }

  // initialize ringbuffer:
  channels = number_of_channels;
  if (number_of_channels > 1)
    ringbuffersize = 512 * 1024;	// 512 KB ringbuffer
  else
    ringbuffersize = 256 * 1024;	// 256 KB ringbuffer
  buffer = (int16 *)malloc (ringbuffersize);
  head = (char *)buffer;
  tail[0] = buffer;
  tail[1] = buffer + 1;
  buffer_empty = True;
  drain_buffer = False;

  // prepare for SIGALRM timeouts:
  timerval.it_interval.tv_sec = 0;
  timerval.it_value.tv_sec = 0;
  if (number_of_channels == 1)
  {
    // call signal_handler() three times a second:
    timerval.it_interval.tv_usec = 1000000 / 3;
    timerval.it_value.tv_usec =    1000000 / 3;
  }
  else
  {
    // call signal_handler() six times a second:
    timerval.it_interval.tv_usec = 1000000 / 6;
    timerval.it_value.tv_usec =    1000000 / 6;
  }
  sigvec my_sigvec;
  my_sigvec.sv_handler = (void (*)(int))signal_handler;
  my_sigvec.sv_mask = 0;
  my_sigvec.sv_flags = 0;
  if (sigvector (SIGALRM, &my_sigvec, NULL))
  {
    perror ("signal (SIGARLM, signal_handler)");
    exit (1);
  }
  handler_activated = False;
}


HpuxObuffer::~HpuxObuffer (void)
{
  // disable timer:
  static itimerval no_timer = { { 0, 0 }, { 0, 0 } };
  setitimer (ITIMER_REAL, &no_timer, NULL);

  // wait until ringbuffer is empty:
  drain_buffer = True;
  while (!buffer_empty)
    signal_handler ();

  if (ioctl (audio_fd, AUDIO_DRAIN, NULL))
  {
    perror ("ioctl AUDIO_DRAIN on /dev/audio");
    exit (1);
  }
  close (audio_fd);
  delete buffer;
}


void HpuxObuffer::append (uint32 channel, int16 value)
/* Remark:
   Because signal_handler() can interrupt the normal program execution at
   (nearly) any time in any statement and synchronization mechanisms like
   sigblock() create too much overload, some values are precomputed first
   and assigned to the target variable later. */
{
#ifdef DEBUG
  if (channel >= channels)
  {
    cerr << "illegal channelnumber in HpuxObuffer::append()!\n";
    exit (1);
  }
#endif
  *tail[channel] = value;
  if ((char *)(tail[channel] + channels) >= (char *)buffer + ringbuffersize)
  {
    register int16 *tmp = buffer + channel;		// fold into ringbuffer
    tail[channel] = tmp;
  }
  else
    tail[channel] += channels;
  buffer_empty = False;
  if ((char *)*tail == head)
  {
    // tail bites into head (buffer is full)
    if (!handler_activated)
    {
      // call handler by hand to (re)start playback:
      do
	signal_handler ();
      while (!buffer_empty && (char *)*tail == head);

      // activate timer:
      if (setitimer (ITIMER_REAL, &timerval, NULL))
      {
	perror ("setitimer");
	exit (1);
      }
      handler_activated = True;
    }
    else
    {
      // wait for a SIGALRM signal:
      sigset_t sigset;
      sigfillset (&sigset);
      sigdelset (&sigset, SIGALRM);
      sigdelset (&sigset, SIGINT);
      sigdelset (&sigset, SIGQUIT);
      sigdelset (&sigset, SIGSTOP);
      do
	if (sigsuspend (&sigset) < 0 && errno != EINTR)
	{
	  perror ("sigsuspend");
	  exit (1);
	}
      while (!buffer_empty && (char *)*tail == head);
    }
  }
}


void HpuxObuffer::signal_handler (void)
{
  extern boil verbose_mode;
  int length, length2, max_length, written_bytes;
  char *write_end;
  audio_status status;

  // determine size of empty room in device internal buffer:
  if (ioctl (audio_fd, AUDIO_GET_STATUS, &status))
  {
    perror ("ioctl AUDIO_GET_STATUS on /dev/audio");
    exit (1);
  }
  max_length = TXBUFSIZE - status.transmit_buffer_count;
  if (!max_length)
    return;

  if (buffer_empty)
  {
pause:
    // disable timer:
    static itimerval no_timer = { { 0, 0 }, { 0, 0 } };
    setitimer (ITIMER_REAL, &no_timer, NULL);
    handler_activated = False;
    if (verbose_mode)
      write (2, "pausing playback...\n", 20);
    return;
  }

  write_end = (char *)(tail[channels - 1] - (channels - 1));
  if (write_end == (char *)buffer)
    write_end = (char *)buffer + ringbuffersize;

  if (write_end > head)
  {
    length = write_end - head;
    if (!drain_buffer && length < 32 * 1024)
      goto pause;
    if (length > max_length)
      length = max_length;
    if ((written_bytes = write (audio_fd, head, length)) < 0)
    {
      perror ("write to /dev/audio");
      exit (1);
    }

#ifdef AUDIO_DEBUG
    timeval actual_time;
    gettimeofday (&actual_time, NULL);
    char string[100];
    sprintf (string, "Written bytes (1):  %6d at time %09d.%06d\n",
	     written_bytes, actual_time.tv_sec, actual_time.tv_usec);
    write (2, string, strlen (string));
#endif

    if ((head += written_bytes) == write_end)
      buffer_empty = True;
    if (head == (char *)buffer + ringbuffersize)
      head = (char *)buffer;
  }
  else
  {
    length  = (char *)buffer + ringbuffersize - head;
    length2 = write_end - (char *)buffer;
    if (!drain_buffer && length + length2 < 32 * 1024)
      goto pause;
    if (length + length2 > max_length)
      if (length > max_length)
      {
	length = max_length;
	length2 = 0;
      }
      else
	length2 = max_length - length;
    if ((written_bytes = write (audio_fd, head, length)) < 0)
    {
      perror ("write to /dev/audio");
      exit (1);
    }

#ifdef AUDIO_DEBUG
    timeval actual_time;
    gettimeofday (&actual_time, NULL);
    char string[100];
    sprintf (string, "Written bytes (2):  %6d at time %09d.%06d\n",
	     written_bytes, actual_time.tv_sec, actual_time.tv_usec);
    write (2, string, strlen (string));
#endif

    if (written_bytes == length && length2)
    {
      if ((written_bytes = write (audio_fd, (char *)buffer, length2)) < 0)
      {
	perror ("write to /dev/audio");
	exit (1);
      }

#ifdef AUDIO_DEBUG
      timeval actual_time;
      gettimeofday (&actual_time, NULL);
      char string[100];
      sprintf (string, "Written bytes (2+): %6d at time %09d.%06d\n",
	       written_bytes, actual_time.tv_sec, actual_time.tv_usec);
      write (2, string, strlen (string));
#endif

      head = (char *)buffer + written_bytes;
      if (head == write_end)
	buffer_empty = True;
    }
    else
      if ((head += written_bytes) == (char *)buffer + ringbuffersize)
      {
	if (head == write_end)
	  buffer_empty = True;
	head = (char *)buffer;
      }
  }
}


int HpuxObuffer::open_audio_device (void)
{
  int fd;

  if ((fd = open ("/dev/audio", O_WRONLY | O_NDELAY, 0)) < 0)
    if (errno == EBUSY)
    {
      extern boil verbose_mode, wait_if_busy;

      if (wait_if_busy)
      {
	if (verbose_mode)
	  cerr << "Audio device is busy, waiting...\n";
	while (errno = 0, (fd = open ("/dev/audio", O_WRONLY, 0)) < 0 &&
	       (errno == EBUSY || errno == EINTR))
	  sleep (3);
	if (errno && errno != EBUSY)
	{
	  perror ("Can't open /dev/audio for writing");
	  exit (1);
	}
	if (verbose_mode)
	  cerr << "Starting playback...\n";
      }
      else
      {
	cerr << "Sorry, the audio device is busy!\n";
	exit (1);
      }
    }
    else
    {
      perror ("Can't open /dev/audio for writing");
      exit (1);
    }

  // turn NDELAY mode on, because write() must never block maplay:
  int flags;
  if ((flags = fcntl (fd, F_GETFL, 0)) < 0)
  {
    perror ("fcntl F_GETFL on /dev/audio failed");
    exit (1);
  }
  flags |= O_NDELAY;
  if (fcntl (fd, F_SETFL, flags) < 0)
  {
    perror ("fcntl F_SETFL on /dev/audio failed");
    exit (1);
  }

  return fd;
}


boil HpuxObuffer::class_suitable (void)
{
  audio_fd = open_audio_device ();
  return True;
}