summaryrefslogtreecommitdiff
path: root/audio_hpux.c
blob: 8d666daac357f5b65e1a1f4a982b850ceff43a7c (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
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
/*
 * Taken mainly from xmp, Cc( 1996-98 Claudio Matsuoka and Hipolito Carraro Jr
 *
 * Who knows if it works, not I, not I...
 *  -- Isaac
 */

#include <sys/audio.h>

#define ARCH_esd_audio_open
int esd_audio_open()
{
    const char *device = "/dev/audio";

    int afd = -1;
    int mode = O_WRONLY;
    int gain = AUDIO_MAX_GAIN;
    int bsize;
    int port = AUDIO_OUT_SPEAKER;
    int flags, test;
    struct audio_gain again;
    struct audio_describe adescribe;
    struct audio_limits alimit;

    if ((esd_audio_format & ESD_MASK_FUNC) == ESD_RECORD) {
	fprintf(stderr, "No idea how to record audio on hp-ux boxen, FIXME\n");
	esd_audio_fd = -1;
	return -1;
    }
    
    if ((afd = open(device, mode)) == -1) {
	perror(device);
	esd_audio_fd = -1;
	return -1;
    }
    
    flags = fcntl(afd, F_GETFL, 0);
    if (flags < 0) {
	perror("F_GETFL");    
	esd_audio_fd = -1;
	return -1;
    }
    flags |= O_NDELAY;
    fcntl(afd, F_SETFL, flags);
    if (flags < 0) {
	perror("F_SETFL");    
	esd_audio_fd = -1;
	return -1;
    }
    
    if ((esd_audio_format & ESD_MASK_BITS) == ESD_BITS16)
        flags = AUDIO_FORMAT_LINEAR16BIT;
    else
	flags = AUDIO_FORMAT_ULAW; /* FIXME: is this right? */
    /* TODO: probably need a linear2ulaw before sending the mix to the audio device */
    if (ioctl(afd, AUDIO_SET_DATA_FORMAT, flags) == -1) {
	perror("AUDIO_SET_DATA_FORMAT");
	close(afd);
	esd_audio_fd = -1;
	return -1;
    }
    
    if ((esd_audio_format & ESD_MASK_CHAN) == ESD_STEREO)
	flags = 2;
    else
	flags = 1;
    if (ioctl(afd, AUDIO_SET_CHANNELS, flags) == -1) {
	perror("AUDIO_SET_CHANNELS");
	close(afd);
	esd_audio_fd = -1;
	return -1;
    }
    
    if (ioctl(afd, AUDIO_SET_OUTPUT, port) == -1) {
	perror("AUDIO_SET_PORT");
	close(afd);
	esd_audio_fd = -1;
	return -1;
    }
    
    test = esd_audio_rate;
    if (ioctl(afd, AUDIO_SET_SAMPLE_RATE, test) == -1) {
	perror("AUDIO_SET_SAMPLE_RATE");
	close(afd);
	esd_audio_fd = -1;
	return -1;
    }
    if (fabs(test - esd_audio_rate) > esd_audio_rate * 0.05) {
	fprintf(stderr,"unsupported playback rate: %d\n", esd_audio_rate);
	close(afd);
	esd_audio_fd = -1;
	return -1;
    }
    
    if (ioctl(afd, AUDIO_DESCRIBE, &adescribe) == -1) {
	perror("AUDIO_DESCRIBE");
        close(afd);
        esd_audio_fd = -1;
        return -1;
    }

    if (ioctl(afd, AUDIO_GET_GAINS, &again) == -1) {
	perror("AUDIO_GET_GAINS");
	close(afd);
	esd_audio_fd = -1;
	return -1;
    }
    
    /* Don't modify transmit_gain unless it's out of bounds. */
    if (again.cgain[0].transmit_gain < adescribe.min_transmit_gain ||
	again.cgain[1].transmit_gain < adescribe.min_transmit_gain ||
	again.cgain[0].transmit_gain > adescribe.max_transmit_gain ||
	again.cgain[1].transmit_gain > adescribe.max_transmit_gain) {
        fprintf(stderr, "one or more of your gain values was out of range - fixed.\n");
        if (again.cgain[0].transmit_gain < adescribe.min_transmit_gain)
            again.cgain[0].transmit_gain = adescribe.min_transmit_gain;
        if (again.cgain[1].transmit_gain < adescribe.min_transmit_gain)
            again.cgain[1].transmit_gain = adescribe.min_transmit_gain;
        if (again.cgain[0].transmit_gain > adescribe.max_transmit_gain)
            again.cgain[0].transmit_gain = adescribe.max_transmit_gain;
        if (again.cgain[1].transmit_gain > adescribe.max_transmit_gain)
            again.cgain[1].transmit_gain = adescribe.max_transmit_gain;
    }

    if (ioctl(afd, AUDIO_SET_GAINS, &again) == -1) {
	perror("AUDIO_SET_GAINS");
	close(afd);
	esd_audio_fd = -1;
	return -1;
    }

    if (!ioctl(afd, AUDIO_GET_LIMITS, &alimit)) {
	bsize = (0x0100 << 16);
    } else {
	bsize = alimit.max_transmit_buffer_size;
    }
    
    while (bsize) {
	if (ioctl(afd, AUDIO_SET_TXBUFSIZE, bsize) != -1) {
	    break;
	}
	bsize >>= 1;
    }
    if (!bsize) {
	close(afd);
	esd_audio_fd = -1;
	return -1;
    }

    if (ioctl(afd, AUDIO_SET_TXBUFSIZE, bsize) == -1) {
	perror("AUDIO_SET_TXBUFSIZE");
	close(afd);
	esd_audio_fd = -1;
	return -1;
    }
    
    esd_audio_fd = afd;
    return afd;
}