summaryrefslogtreecommitdiff
path: root/tutorials/sndkit/tests/multich32.c
blob: ab39297a1fc8d6d919fcb194692547a086f78d36 (plain)
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
/*
 * Multi channel audio test.
 *
 * This program is intended to test playback of 16 bit samples using 4 or more
 * channels at 48000 Hz. The program plays sine wave pulses sequentially on
 * channels 0 to N-1.
 *
 * Arguments:
 *
 * 1:	Number of channelts (default is 8).
 * 2:	Audio device (/dev/dsp by default).
 * 3-N: Options
 * 	  -b	Bypass virtual mixer
 * 	  -r	Raw mode (disables automatic sample rate/format conversions)
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <soundcard.h>

static int sinedata[48] = {

  0, 4276, 8480, 12539, 16383, 19947, 23169, 25995,
  28377, 30272, 31650, 32486, 32767, 32486, 31650, 30272,
  28377, 25995, 23169, 19947, 16383, 12539, 8480, 4276,
  0, -4276, -8480, -12539, -16383, -19947, -23169, -25995,
  -28377, -30272, -31650, -32486, -32767, -32486, -31650, -30272,
  -28377, -25995, -23169, -19947, -16383, -12539, -8480, -4276
};

int
main (int argc, char *argv[])
{
  char *dev = "/dev/dsp";
  int fd, l, i, n = 0, ch, p = 0, phase = 0, arg, channels, srate, thisch = 0;
  int tick = 0;
  int nch = 8;
  int bypass_vmix=0;
  int disable_format_conversions=0;

  int buf[32*1024];

  if (argc > 1)
    if (sscanf (argv[1], "%d", &nch) != 1)
      nch = 2;

  if (argc > 2)
    dev = argv[2];

  for (i=3;i<argc;i++)
  if (argv[i][0]=='-')
  switch (argv[i][1])
  {
  case 'b': /* Bypass virtual mixer */
	  bypass_vmix = O_EXCL;
	  break;

  case 'r': /* Use raw mode (disable automatic format conversions) */
	  disable_format_conversions=1;
	  break;
  }

  if ((fd = open (dev, O_WRONLY|bypass_vmix, 0)) == -1)
    {
      perror (dev);
      exit (-1);
    }

  if (disable_format_conversions)
     {
  	arg=0;
  	ioctl(fd, SNDCTL_DSP_COOKEDMODE, &arg);
     }

  arg = nch;
  if (ioctl (fd, SNDCTL_DSP_CHANNELS, &arg) == -1)
    perror ("SNDCTL_DSP_CHANNELS");
  channels = arg;
  fprintf (stderr, "Channels %d\n", arg);

  arg = AFMT_S32_NE;
  if (ioctl (fd, SNDCTL_DSP_SETFMT, &arg) == -1)
    perror ("SNDCTL_DSP_SETFMT");
  fprintf (stderr, "Format %x\n", arg);

  arg = 48000;
  if (ioctl (fd, SNDCTL_DSP_SPEED, &arg) == -1)
    perror ("SNDCTL_DSP_SPEED");
  printf ("Using sampling rate %d\n", arg);
  srate = arg;

  while (1)
    {
      for (ch = 0; ch < channels; ch++)
	{
	  if (ch == thisch)
	    {
	      buf[p] = sinedata[phase]<<16;
	      phase = (phase + 1 + (ch / 2)) % 48;
	      if (phase == 0 && tick > 10 * channels)
		{
		  thisch = (thisch + 1) % channels;
		  tick = 0;
		}
	    }
	  else
	    buf[p] = 0;

	  p++;

	  if (p >= sizeof (buf) / 4)
	    {
	      if (write (fd, buf, p * 4) != p * 4)
		perror ("write");
	      p = 0;
	      tick++;

	    }
	}
      n++;
    }

  exit (0);
}