summaryrefslogtreecommitdiff
path: root/tutorials/sndkit/tests/synctest.c
blob: d2ed77bf688aa1bad1018cc241b141b338593e77 (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
/*
 * Purpose: A program that demonstrates use of syncronization groups.
 * Copyright (C) 4Front Technologies, 2002-2004. Released under GPLv2/CDDL.
 *
 * Description:
 * This program opens three audio devices (hard coded in the program)
 * and creates a syncronization group using {!nlink SNDCTL_DSP_SYNCGROUP}.
 * 
 * Next it starts all the devices joined in the group simultaneously
 * by calling {!nlink SNDCTL_DSP_SYNCSTART}. Finally it will keep copying
 * audio input from the 3rd device to the other two ones.
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <soundcard.h>

#define MAX_DEV		10

int
main (int argc, char *argv[])
{
  int i, id, fd[MAX_DEV], ndevs = 0;
  char buf[32768] = { 0 };

  oss_syncgroup group;

  group.id = 0;
  group.mode = PCM_ENABLE_OUTPUT;

/*
 * Open the devices listed on command line
 */
  if (argc < 2)
    exit (-1);

  for (i = 1; i < argc; i++)
    {
      if ((fd[ndevs] = open (argv[i], O_WRONLY, 0)) == -1)
	{
	  perror (argv[i]);
	  exit (-1);
	}

      if (ioctl (fd[ndevs], SNDCTL_DSP_SYNCGROUP, &group) == -1)
	{
	  perror ("SNDCTL_DSP_SYNCGROUP");
	  exit (-1);
	}
/*
 * Note! It is very important to write some data to all output devices
 * between calling SNDCTL_DSP_SYNCGROUP and SNDCTL_DSP_SYNCSTART. Otherwise
 * playback will not start properly. However do not write more data than
 * there is room in device's DMA buffer. Recommended amount of prteload data
 * is one full fragment.
 *
 * In applications that record audio, process it and then play back it's
 * necessary to write two fragments of silence to the output device(s) before
 * starting the group. Otherwise output device(s) will run out of data before
 * the first read from the input device returns.
 */

      if (write (fd[ndevs], buf, sizeof (buf)) != sizeof (buf))
	{
	  perror ("write");
	  exit (-1);
	}

      ndevs++;
    }

  printf ("Sync group %x created with %d devices\n", group.id, ndevs);


  id = group.id;

  if (ioctl (fd[0], SNDCTL_DSP_SYNCSTART, &id) == -1)
    {
      perror ("SNDCTL_DSP_SYNCSTART");
      exit (-1);
    }

  while (1)
    {
      for (i = 0; i < ndevs; i++)
	if (write (fd[i], buf, sizeof (buf)) != sizeof (buf))
	  {
	    perror ("write2");
	    exit (-1);
	  }
    }

  exit (0);
}