summaryrefslogtreecommitdiff
path: root/tutorials/sndkit/samples/recsrc.c
blob: cc309230ecdd8eb2d2ee4c6b1d87beb9ec503047 (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
/*
 * Purpose: A sample program for recording source selection
 * Copyright (C) 4Front Technologies, 2002-2004. Released under GPLv2/CDDL.
 *
 * Description:
 * This program demonstrates the new ioctl call interface used to
 * control the recording source.
 *
 * The same program can be used for playback target selection simply by
 * changing O_RDONLY to O_WRONLY in the open call and by replacing all
 * "RECSRC" strings with "PLAYTGT" (see playtgt.c).
 *
 * The first command line argument is the audio device (/dev/dsp#). If there 
 * are no other arguments then the available choices will be printed. If the 
 * second argument is "-" then the current setting will be printed.
 * Finally the source can be changed by giving it's name as the
 * second argument.
 *
 * {!notice Please not that the change may stay in effect even after closing
 * the device. However equally well it's possible that the device returns back
 * to some default source. There is no way to predict how the device will
 * behave and the application must not expect any particular behaviour.}
 */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <soundcard.h>
#include <time.h>

int
main (int argc, char *argv[])
{
  int fd, i, src;
  oss_mixer_enuminfo ei;

  if (argc < 2)
    {
      fprintf (stderr, "Usage: %s dspdev\n", argv[0]);
      exit (-1);
    }

  if ((fd = open (argv[1], O_RDONLY, 0)) == -1)
    {
      perror (argv[1]);
      exit (-1);
    }

  if (ioctl (fd, SNDCTL_DSP_GET_RECSRC_NAMES, &ei) == -1)
    {
      perror ("SNDCTL_DSP_GET_RECSRC_NAMES");
      exit (-1);
    }

  if (argc == 2)
    {
      for (i = 0; i < ei.nvalues; i++)
	printf ("Rec source #%d = '%s'\n", i, ei.strings + ei.strindex[i]);
      exit (0);
    }

  if (strcmp (argv[2], "?") == 0 || strcmp (argv[2], "-") == 0)
    {
      if (ioctl (fd, SNDCTL_DSP_GET_RECSRC, &src) == -1)
	{
	  perror ("SNDCTL_DSP_GET_RECSRC");
	  exit (-1);
	}

      printf ("Current recording source is #%d\n", src);
      printf ("Current recording source is #%d (%s)\n",
	      src, ei.strings + ei.strindex[src]);
      exit (0);
    }

  src = 0;

  for (i = 0; i < ei.nvalues; i++)
    {
      if (strcmp (argv[2], ei.strings + ei.strindex[i]) == 0)
	{
	  if (ioctl (fd, SNDCTL_DSP_SET_RECSRC, &src) == -1)
	    {
	      perror ("SNDCTL_DSP_SET_RECSRC");
	      exit (-1);
	    }

	  exit (0);
	}

      src++;
    }

  fprintf (stderr, "What?\n");
  exit (-1);
}