File: audio.c

package info (click to toggle)
coolmail 1.3-15
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 312 kB
  • sloc: ansic: 1,527; makefile: 69
file content (250 lines) | stat: -rw-r--r-- 6,890 bytes parent folder | download | duplicates (2)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
 * Copyright (c) 1994,1995  Dimitrios P. Bouras and William K. W. Cheung
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 * Except as contained in this notice, the name of the X Consortium shall not be
 * used in advertising or otherwise to promote the sale, use or other dealings
 * in this Software without prior written authorization from the X Consortium.
 * 
 * Derived from the MIT X11R5 xbiff, written by Jim Fulton, which is
 * copyrighted (c) 1988 X Consortium.
 *
 * Mailbox XPM additions-modifications:  Dimitrios P. Bouras
 * Audio support and XPM icon animation: William K. W. Cheung

 * Derived from xmailbox by Dimitrios P. Bouras and William K. W. Cheung
 * as well as volume by Sam Lantinga. The above copyright notice is
 * from Dimitros and William's file Mailbox.c in the xmailbox application.
 * modified and integrated into Coolmail: Randall K. Sharpe */

#include <sys/stat.h>			/* for stat() ** needs types.h ***/
#include <sys/signal.h>			/* for signal() */
#include <stdio.h>				/* for printing error messages */
#include <pwd.h>				/* for getting username */
#include <stdlib.h>				/* for getenv() */
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/file.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>

extern int cool_vol;
extern char *sndfile;

/* I am only doing sun audio -- RKS */

/* Adapted from <multimedia/audio_filehdr.h> */

typedef unsigned 	u_32;	/* we assume sizeof(unsigned) = 4 */

typedef struct {
	u_32		magic;		/* magic number */
	u_32		hdr_size;	/* size of this header */
	u_32		data_size;	/* length of data (optional) */
	u_32		encoding;	/* data encoding format */
	u_32		sample_rate;	/* samples per second */
	u_32		channels;	/* number of interleaved channels */
} Audio_filehdr;

#if defined(linux) || defined(__FreeBSD_kernel__)

#if defined(linux)
#include <linux/soundcard.h>
#else
#include <sys/soundcard.h>
#endif

#define DEV_MIXER		"/dev/mixer"
#define MAX_VOLUME		100
#define MIN_VOLUME		1
#define RIGHT			0x01
#define LEFT			0x02

typedef struct stereovolume
{
	unsigned char left;
	unsigned char right;
	unsigned char pad[2];
} StereoVolume;

void setvolume(int which, unsigned char setting, StereoVolume *volptr)
{
	if ( setting < MIN_VOLUME )
		setting=MIN_VOLUME;
	if ( setting > MAX_VOLUME )
		setting=MAX_VOLUME;

	if ( which&RIGHT )
		volptr->right=setting;
	if ( which&LEFT )
		volptr->left=setting;
}
#else
#include <sun/audioio.h>
#endif

#ifndef min
#define min(x,y)	((x) < (y)? (x): (y))
#endif

#define CLOSE_FD(afd)	{ if (afd > -1) close(afd); afd = -1; }

#if defined(linux) || defined(__FreeBSD_kernel__)
#define INIT_FD	{ audiofd = filefd = mixer_fd = -1; }
#define END_FD	{ CLOSE_FD(audiofd); CLOSE_FD(filefd); CLOSE_FD(mixer_fd); return; }
#else
#define INIT_FD	{ audiofd = filefd = 0; }
#define END_FD	{ CLOSE_FD(audiofd); CLOSE_FD(filefd); return; }
#endif


void audio_beep (void)
{
	int		audiofd, filefd;
	int		rn, wn, len;
	unsigned char	buf[256];
	Audio_filehdr	*au_hdr;

#if defined(linux) || defined(__FreeBSD_kernel__)
	StereoVolume origVol, volume;
	int				mixer_fd;
#else
	audio_info_t 	ais;
	int				origVol;	
#endif

	/* NOP if requested volume is zero. */
	if (cool_vol <= 0) return;

	INIT_FD;
	audiofd = open( "/dev/audio", O_WRONLY | O_NDELAY ); 
	if (audiofd < 0) {
	        perror("/dev/audio");
		fprintf(stderr, "%s: Problem opening /dev/audio.\n",
				"Coolmail");
		END_FD;
	}
#if defined(linux) || defined(__FreeBSD_kernel__)
	if ( (mixer_fd=open(DEV_MIXER, O_RDWR, 0)) < 0 ) {
		fprintf(stderr, "Can't open %s: ", DEV_MIXER);
		END_FD;
	}

	if ( ioctl(mixer_fd, MIXER_READ(SOUND_MIXER_VOLUME), &origVol) < 0 ) {
		perror("Can't obtain current volume settings");
		END_FD;
	}

	setvolume(LEFT|RIGHT, cool_vol, &volume);

	if ( ioctl(mixer_fd, MIXER_WRITE(SOUND_MIXER_VOLUME), &volume) < 0 ) {

		fprintf(stderr, "Can't set current volume settings");
		END_FD;
	}

#else
	if( ioctl( audiofd, AUDIO_GETINFO, &ais ) ) {
		fprintf(stderr, "%s: Problem retrieving /dev/audio info.\n",
				"Coolmail");
		END_FD;
	}
	origVol = ais.play.gain;
	ais.play.gain = cool_vol;
	if( ioctl( audiofd, AUDIO_SETINFO, &ais ) ) {
                fprintf(stderr, "Coolmail: Error %d returned from ioctl.\n",
                        errno);
		fprintf(stderr, "%s: Problem setting /dev/audio info.\n",
				"Coolmail");
		END_FD;
	}
#endif
	filefd = open(sndfile, O_RDONLY);
	if (filefd < 0) {
		fprintf(stderr, "%s: Couldn't play file \"%s\"\n", 
				"Coolmail", sndfile);
		END_FD;
	}

	/* Read in the audio header */
	rn = read(filefd, buf, sizeof(Audio_filehdr)); 

	if (rn > 0 && strncmp(buf, ".snd", 4) != 0) {
		fprintf(stderr, "%s: Invalid audio file format.\n",
				"Coolmail");
		END_FD;
	}

	/* Strip the header */
	au_hdr = (Audio_filehdr *)buf;
#if defined(linux) || defined(__FreeBSD_kernel__)
	rn = ntohl(au_hdr->hdr_size) - sizeof(Audio_filehdr); 	
#else
	rn = au_hdr->hdr_size - sizeof(Audio_filehdr);
#endif
	for( ; rn > 0; ) {
		len = min(rn, sizeof(buf));
		len = read(filefd, buf, len); 
		rn -= len;	
	}

	while(1) {
		rn = read(filefd, buf, sizeof(buf));
		if (rn < 0) {
			fprintf(stderr, "%s: Error reading from file \"%s\"\n", 
					"Coolmail", sndfile);
			END_FD;
		}
		if (rn == 0)
			break;
		while(1) {
			wn = write(audiofd, buf, rn);
			if ( wn < 0 ) {
				fprintf(stderr, "%s: Error writing to /dev/audio.\n", 
						"Coolmail");
				END_FD;
			}
			if ( wn != 0 )
				break;
			usleep(1000);
		}
	}
#if defined(linux) || defined(__FreeBSD_kernel__)
	CLOSE_FD(audiofd);

	if ( ioctl(mixer_fd, MIXER_WRITE(SOUND_MIXER_VOLUME), &origVol) < 0 ) {
		fprintf(stderr, "Can't reset volume settings");
	}

	CLOSE_FD(mixer_fd);

#else
	ais.play.gain = origVol;
	if( ioctl( audiofd, AUDIO_SETINFO, &ais ) ) {
		fprintf(stderr, "%s: Problem setting /dev/audio info.\n",
				"Coolmail");
	}
#endif
	END_FD;

    return;
}