File: obuffer_sparc.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 (651 lines) | stat: -rw-r--r-- 16,974 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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/*
 *  @(#) obuffer_sparc.cc 1.6, last edit: 02 Mar 1995 18:38:54
 *  @(#) Copyright (C) 1993, 1994 Tobias Bading (bading@cs.tu-berlin.de)
 *  @(#) Berlin University of Technology
 *
 *  Idea and first implementation for u-law output with fast downsampling by
 *  Jim Boucher (jboucher@flash.bu.edu)
 *
 *  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.
 */

/*
 *  Changes from version 1.2 to 1.3:
 *    - SparcObuffer uses asynchronous audio output now.
 *	Calculated PCM samples are stored in a ringbuffer and written to
 *	the audio device by a signal handler. The handler works with either
 *	SIGIO or SIGALRM signals.
 *    - should work with SPARC 5 machines, if the audio patch is installed
 *	correctly. But don't ask me what's correct in this matter,
 *	please ask Sun.
 *
 *  Changes from version 1.1 to 1.2:
 *    - If ULAW is defined:
 *      SparcObuffer feeds mono u-law output to an amd device (u-law format).
 *      The required downsampling to 8 kHz in done in the synthesis filter.
 *    - O_NDELAY flag is cleared now after a successful open syscall
 *      on /dev/audio (caused problems under Solaris)
 *    - options -us, -uh and -ul should work now
 */


#include <stropts.h>
extern "C" {
int ioctl (int, int ...);
#ifdef USE_TIMER
int setitimer (int, itimerval *, itimerval *);
#endif
}


// statics of class SparcObuffer:
uint32		SparcObuffer::ringbuffersize;
#ifdef ULAW
ulawsample     *SparcObuffer::buffer;
char	       *SparcObuffer::head;
ulawsample     *SparcObuffer::tail;
#else
int16	       *SparcObuffer::buffer;
char	       *SparcObuffer::head;
int16	       *SparcObuffer::tail[2];
uint32		SparcObuffer::channels;
#endif
int		SparcObuffer::audio_fd = -1;
boil		SparcObuffer::handler_activated;
boil		SparcObuffer::buffer_empty;
boil		SparcObuffer::drain_buffer;
#ifdef USE_TIMER
itimerval	SparcObuffer::timerval;
#endif


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

  // configure the audio device:
  audio_info info;
  AUDIO_INITINFO (&info);
#ifdef SunOS4_1_1
  if (use_speaker || use_headphone)
#else
  info.output_muted = False;
  if (use_speaker || use_headphone || use_line_out)
#endif
  {
    info.play.port = 0;
    if (use_speaker)
      info.play.port |= AUDIO_SPEAKER;
    if (use_headphone)
      info.play.port |= AUDIO_HEADPHONE;
#ifndef SunOS4_1_1
    if (use_line_out)
      info.play.port |= AUDIO_LINE_OUT;
#endif
  }
  if (volume >= 0.0 && volume <= 1.0)
    info.play.gain = (unsigned int)(volume * 255.0 + 0.5);

#ifdef ULAW
  // configure the amd device:
  info.play.encoding = AUDIO_ENCODING_ULAW;
  info.play.precision = 8;
  info.play.channels = 1;
  info.play.sample_rate = 8000;
#else
  // configure the dbri or audiocs device:
  info.play.encoding = AUDIO_ENCODING_LINEAR;
  info.play.precision = 16;
  info.play.channels = number_of_channels;
  info.play.sample_rate = header->frequency ();
#endif
  if (ioctl (audio_fd, AUDIO_SETINFO, &info) < 0)
  {
    perror ("ioctl AUDIO_SETINFO on /dev/audio");
    exit (1);
  }

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

  // prepare to receive signals:
#ifdef USE_TIMER
  // prepare for SIGALRM timeouts:
  timerval.it_interval.tv_sec = 0;
  timerval.it_value.tv_sec = 0;
#ifdef ULAW
  // call signal_handler() three times a second:
  timerval.it_interval.tv_usec = 1000000 / 3;
  timerval.it_value.tv_usec =    1000000 / 3;
#else
  if (number_of_channels == 1)
  {
    // call signal_handler() six times a second:
    timerval.it_interval.tv_usec = 1000000 / 6;
    timerval.it_value.tv_usec =    1000000 / 6;
  }
  else
  {
    // call signal_handler() twelve times a second:
    timerval.it_interval.tv_usec = 1000000 / 12;
    timerval.it_value.tv_usec =    1000000 / 12;
  }
#endif
#ifdef Solaris
  if ((int)sigset (SIGALRM, (void (*)(int))signal_handler) == -1)
#else
  if ((int)signal (SIGALRM, (void (*)(int))signal_handler) == -1)
#endif
  {
    perror ("signal (SIGARLM, handler)");
    exit (1);
  }
#else
  // prepare for SIGIO signals from /dev/audio stream:
#ifdef Solaris
  if ((int)sigset (SIGIO, (void (*)(int))signal_handler) == -1)
#else
  if ((int)signal (SIGIO, (void (*)(int))signal_handler) == -1)
#endif
  {
    perror ("signal (SIGIO, SIG_IGN)");
    exit (1);
  }
#endif
  handler_activated = False;
}


SparcObuffer::~SparcObuffer (void)
{
#ifdef USE_TIMER
  // disable timer:
  static itimerval no_timer = { { 0, 0 }, { 0, 0 } };
  setitimer (ITIMER_REAL, &no_timer, NULL);
#else
  // disable SIGIO signals:
  if ((int)signal (SIGIO, SIG_IGN) == -1)
  {
    perror ("signal (SIGIO, SIG_IGN)");
    exit (1);
  }
#endif

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

  ioctl (audio_fd, AUDIO_DRAIN, NULL);
  close (audio_fd);
  free (buffer);
}


void SparcObuffer::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 ULAW
  // convert 16-bit PCM sample to 8-bit ulaw:
  *tail = linear2ulaw[value >> 3];
  if ((char *)(tail + 1) >= (char *)buffer + ringbuffersize)
    tail = buffer;
  else
    ++tail;
#else
#ifdef DEBUG
  if (channel >= channels)
  {
    cerr << "illegal channelnumber in SparcObuffer::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;
#endif
  buffer_empty = False;
#ifdef ULAW
  if ((char *)tail == head)
#else
  if ((char *)*tail == head)
#endif
  {
    // tail bites into head (buffer is full)
    if (!handler_activated)
    {
      // call handler by hand to (re)start playback:
      do
	signal_handler ();
#ifdef ULAW
      while (!buffer_empty && (char *)tail == head);
#else
      while (!buffer_empty && (char *)*tail == head);
#endif

#ifdef USE_TIMER
      // activate timer:
      if (setitimer (ITIMER_REAL, &timerval, NULL))
      {
	perror ("setitimer");
	exit (1);
      }
#else
      // give me SIGIO (== SIGPOLL) signals each time
      // there's empty room in the output queue:
      if (ioctl (audio_fd, I_SETSIG, S_OUTPUT) < 0)
      {
	perror ("ioctl I_SETSIG on /dev/audio");
	exit (1);
      }
#endif
      handler_activated = True;
    }
    else
    {
      // wait for a SIGALRM or SIGIO signal:
      sigset_t sigset;
      sigfillset (&sigset);
#ifdef USE_TIMER
      sigdelset (&sigset, SIGALRM);
#else
      sigdelset (&sigset, SIGIO);
#endif
      sigdelset (&sigset, SIGINT);
      sigdelset (&sigset, SIGQUIT);
      sigdelset (&sigset, SIGSTOP);
      do
	if (sigsuspend (&sigset) < 0 && errno != EINTR)
	{
	  perror ("sigsuspend");
	  exit (1);
	}
#ifdef ULAW
      while (!buffer_empty && (char *)tail == head);
#else
      while (!buffer_empty && (char *)*tail == head);
#endif
    }
  }
}


void SparcObuffer::signal_handler (void)
{
  extern boil verbose_mode;
  int length, length2, written_bytes;
  char *write_end;

  if (buffer_empty)
  {
pause:
#ifdef USE_TIMER
    // disable timer:
    static itimerval no_timer = { { 0, 0 }, { 0, 0 } };
    setitimer (ITIMER_REAL, &no_timer, NULL);
#else
    // no more SIGIO signals please:
    if (ioctl (audio_fd, I_SETSIG, 0) < 0)
    {
      perror ("ioctl I_SETSIG on /dev/audio");
      exit (1);
    }
#endif
    handler_activated = False;
    if (verbose_mode)
      write (2, "pausing playback...\n", 20);
    return;
  }

#ifdef ULAW
  write_end = (char *)tail;
#else
  write_end = (char *)(tail[channels - 1] - channels + 1);
#endif
  if (write_end == (char *)buffer)
    write_end = (char *)buffer + ringbuffersize;

  if (write_end > head)
  {
    length = write_end - head;
#ifdef ULAW
    if (!drain_buffer && length < 4 * 1024)
#else
    if (!drain_buffer && length < 32 * 1024)
#endif
      goto pause;
    if ((written_bytes = write (audio_fd, head, length)) < 0)
    {
      if (errno == EWOULDBLOCK)
	return;
      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;
#ifdef ULAW
    if (!drain_buffer && length + length2 < 4 * 1024)
#else
    if (!drain_buffer && length + length2 < 32 * 1024)
#endif
      goto pause;
    if ((written_bytes = write (audio_fd, head, length)) < 0)
    {
      if (errno == EWOULDBLOCK)
	return;
      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)
      {
	if (errno == EWOULDBLOCK)
	  written_bytes = 0;
	else
	{
	  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 SparcObuffer::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";
	if ((fd = open ("/dev/audio", O_WRONLY, 0)) < 0)
	{
	  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;
}


#ifdef Solaris
void SparcObuffer::get_device_type (int fd, audio_device *devtype)
{
  if (ioctl (fd, AUDIO_GETDEV, devtype))
  {
    perror ("ioctl AUDIO_GETDEV on /dev/audio");
    exit (1);
  }
}

#else
// !Solaris

int SparcObuffer::get_device_type (int fd)
{
#ifdef AUDIO_GETDEV
  int devtype;
  if (ioctl (fd, AUDIO_GETDEV, &devtype))
  {
    perror ("ioctl AUDIO_GETDEV on /dev/audio");
    exit (1);
  }
  return devtype;
#else
  cerr << "SparcObuffer::get_device_type(): AUDIO_GETDEV ioctl not available!\n";
  return -1;
#endif
}
#endif	// !Solaris


#ifdef ULAW
boil SparcObuffer::class_suitable (uint32 number_of_channels, boil force_amd)
#else
boil SparcObuffer::class_suitable (void)
#endif
{
#ifdef ULAW
  if (number_of_channels > 1)
  {
    cerr << "Your audio hardware cannot handle more than one audio channel.\n"
	    "Please use the option -l or -r for stereo streams.\n";
    return False;
  }
#endif

  audio_fd = open_audio_device ();

#ifdef ULAW
  if (force_amd)
    return True;
#endif

#ifdef Solaris
  audio_device devtype;
  get_device_type (audio_fd, &devtype);
# ifdef ULAW
    if (!strcmp (devtype.name, "SUNW,am79c30"))
      return True;
    else if (!strcmp (devtype.name, "SUNW,dbri") ||
	     !strcmp (devtype.name, "SUNW,CS4231"))	// Sparc 5 audiocs device
    {
      cerr << "Your machine can produce CD-quality audio output,\n"
	      "but this binary was compiled for 8 kHz u-law ouput. (telephone quality)\n"
	      "Please recompile it without the ULAW define in COMPILERFLAGS.\n"
	      "(or use the -amd option to use this binary with low-quality output)\n";
      close (audio_fd);
      return False;
    }
# else
    if (!strcmp (devtype.name, "SUNW,dbri"))
      return True;
    else if (!strcmp (devtype.name, "SUNW,CS4231"))	// Sparc 5 audiocs device
      return True;
    else if (!strcmp (devtype.name, "SUNW,am79c30"))
    {
      cerr << "Your machine can produce 8 kHz u-law audio output only,\n"
	      "but this binary was compiled for CD-quality output.\n"
	      "Please recompile it with ULAW defined in COMPILERFLAGS\n"
	      "or use it in stdout mode as an decoder only.\n";
      close (audio_fd);
      return False;
    }
# endif
#else
  // !Solaris
# ifdef SunOS4_1_1
    // no AUDIO_GETDEV under SunOS 4.1.1, so we have to assume that there is
    // an amd device attached to /dev/audio
#   ifdef ULAW
      return True;
#   else
      cerr << "Your machine can produce 8 kHz u-law audio output only,\n"
	      "but this binary was compiled for CD-quality output.\n"
	      "Please recompile it with ULAW defined in COMPILERFLAGS\n"
	      "or use it in stdout mode as an decoder only.\n";
      close (audio_fd);
      return False;
#   endif	// !ULAW
# else
    // SunOS 4.1.3
    int device_type = get_device_type (audio_fd);
#   ifdef ULAW
      if (device_type == AUDIO_DEV_AMD)
	return True;
      else if (device_type == AUDIO_DEV_SPEAKERBOX || device_type == AUDIO_DEV_CODEC)
      {
	cerr << "Your machine can produce CD-quality audio output,\n"
		"but this binary was compiled for 8 kHz u-law ouput. (telephone quality)\n"
		"Please recompile it without the ULAW define in COMPILERFLAGS.\n"
		"(or use the -amd option to use this binary with low-quality output)\n";
	close (audio_fd);
	return False;
      }
#   else
      if (device_type == AUDIO_DEV_SPEAKERBOX || device_type == AUDIO_DEV_CODEC)
	return True;
      else if (device_type == AUDIO_DEV_AMD)
      {
	cerr << "Your machine can produce 8 kHz u-law audio output only,\n"
		"but this binary was compiled for CD-quality output.\n"
		"Please recompile it with ULAW defined in COMPILERFLAGS\n"
		"or use it in stdout mode as an decoder only.\n";
	close (audio_fd);
	return False;
      }
#   endif	// !ULAW
# endif	// !SunOS4_1_1
#endif	// !Solaris

#ifndef SunOS4_1_1
  close (audio_fd);
  cerr << "Sorry, I don't recognize your audio device.\n"
# ifdef ULAW
	  "Please try the -amd option or use the stdout mode.\n";
# else
	  "Please use the stdout mode.\n";
# endif
  return False;
#endif
}