File: plugout_devdsp.c

package info (click to toggle)
gbsplay 0.0.102-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 704 kB
  • sloc: ansic: 8,341; sh: 1,319; makefile: 543; xml: 13
file content (97 lines) | stat: -rw-r--r-- 2,669 bytes parent folder | download
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
/*
 * gbsplay is a Gameboy sound player
 *
 * header file for /dev/dsp sound output plugin
 *
 * 2003-2020 (C) by Christian Garbs <mitch@cgarbs.de>
 *                  Tobias Diedrich <ranma+gbsplay@tdiedrich.de>
 *
 * Licensed under GNU GPL v1 or, at your option, any later version.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

#include <sys/ioctl.h>
#include <sys/soundcard.h>

#include "common.h"
#include "plugout.h"

static int fd;

static long devdsp_open(enum plugout_endian *endian, long rate, long *buffer_bytes, const struct plugout_metadata metadata)
{
	int c;
	int flags;

	UNUSED(metadata);
	
	if ((fd = open("/dev/dsp", O_WRONLY|O_NONBLOCK)) == -1) {
		fprintf(stderr, _("Could not open /dev/dsp: %s\n"), strerror(errno));
		return -1;
	}
	if ((flags = fcntl(fd, F_GETFL)) == -1) {
		fprintf(stderr, _("fcntl(F_GETFL) failed: %s\n"), strerror(errno));
	} else if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) == -1) {
		fprintf(stderr, _("fcntl(F_SETFL, flags&~O_NONBLOCK) failed: %s\n"), strerror(errno));
	}

	switch (*endian) {
	case PLUGOUT_ENDIAN_BIG:        c = AFMT_S16_BE; break;
	case PLUGOUT_ENDIAN_LITTLE:     c = AFMT_S16_LE; break;
	case PLUGOUT_ENDIAN_AUTOSELECT: c = AFMT_S16_NE; break;
	}
	if ((ioctl(fd, SNDCTL_DSP_SETFMT, &c)) == -1) {
		fprintf(stderr, _("ioctl(fd, SNDCTL_DSP_SETFMT, %d) failed: %s\n"), c, strerror(errno));
		return -1;
	}
	c = rate;
	if ((ioctl(fd, SNDCTL_DSP_SPEED, &c)) == -1) {
		fprintf(stderr, _("ioctl(fd, SNDCTL_DSP_SPEED, %ld) failed: %s\n"), rate, strerror(errno));
		return -1;
	}
	if (c != rate) {
		fprintf(stderr, _("Requested rate %ldHz, got %dHz.\n"), rate, c);
		rate = c;
	}
	c=1;
	if ((ioctl(fd, SNDCTL_DSP_STEREO, &c)) == -1) {
		fprintf(stderr, _("ioctl(fd, SNDCTL_DSP_STEREO, %d) failed: %s\n"), c, strerror(errno));
		return -1;
	}
	c=(4 /* max fragments */ << 16) + 11 /* selector for 2048 bytes fragment size */;
	if ((ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &c)) == -1)
		fprintf(stderr, _("ioctl(fd, SNDCTL_DSP_SETFRAGMENT, %08x) failed: %s\n"), c, strerror(errno));

	/* get the actual fragment size we got */
	if ((ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &c)) == -1) {
		fprintf(stderr, _("ioctl(fd, SNDCTL_DSP_GETBLKSIZE) failed: %s\n"), strerror(errno));
		return -1;
	}
	*buffer_bytes = c;

	return 0;
}

static ssize_t devdsp_write(const void *buf, size_t count)
{
	return write(fd, buf, count);
}

static void devdsp_close(void)
{
	(void)close(fd);
}

const struct output_plugin plugout_devdsp = {
	.name = "oss",
	.description = "OSS sound driver",
	.open = devdsp_open,
	.write = devdsp_write,
	.close = devdsp_close,
};