File: sound.c

package info (click to toggle)
glhack 1.2-8.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,744 kB
  • sloc: ansic: 208,571; cpp: 13,139; yacc: 2,005; makefile: 1,152; lex: 377; sh: 121; awk: 89; sed: 11
file content (332 lines) | stat: -rw-r--r-- 6,837 bytes parent folder | download | duplicates (23)
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*   SCCS Id: @(#)sound.c   3.4     1996/02/19                        */
/*   Copyright (c) NetHack PC Development Team 1993,1995            */
/*   NetHack may be freely redistributed.  See license for details. */
/*                                                                  */
/*
 * sound.c - Hardware sound support
 *
 *Edit History:
 *     Initial Creation                              93/10/01
 *     Added PC Speaker Support for BC compilers     95/06/14
 *     Completed various fixes			     96/02/19
 *
 */

#include "hack.h"
#include <stdio.h>
#include "portio.h"

#include <dos.h>
#include <ctype.h>

#ifndef TESTING

#define printf	pline

int
assign_soundcard(sopt)
char *sopt;
{

	iflags.hassound = 0;
#  ifdef PCMUSIC
	iflags.usepcspeaker = 0;
#  endif

	if (strncmpi(sopt,"def",3) == 0) {              /* default */
		/* do nothing - default */
	}
#  ifdef PCMUSIC
	else if (strncmpi(sopt,"speaker",7) == 0) {	/* pc speaker */
		iflags.usepcspeaker = 1;
	}
#  endif
	else if (strncmpi(sopt,"auto",4) == 0) {	/* autodetect */
	/*
	 * Auto-detect Priorities (arbitrary for now):
	 *	Just pcspeaker
	 */
		if (0) ;
#  ifdef PCMUSIC
		else iflags.usepcspeaker = 1;
#  endif
	} else {
		return 0;
	}
	return 1;

}
#endif

#ifdef PCMUSIC

/* 8254/3 Control Word Defines */

#define CTR0SEL (0<<6)
#define	CTR1SEL	(1<<6)
#define	CTR2SEL	(2<<6)
#define RDBACK	(3<<6)

#define LATCH	(0<<4)
#define	RW_LSB	(1<<4)
#define RW_MSB	(2<<4)	/* If both LSB and MSB are read, LSB is done first */

#define MODE0	(0<<1)	/* Interrupt on terminal count */
#define MODE1	(1<<1)	/* Hardware One-Shot */
#define MODE2	(2<<1)	/* Pulse Generator */
#define MODE3	(3<<1)	/* Square Wave Generator */
#define MODE4	(4<<1)	/* Software Triggered Strobe */
#define MODE5	(5<<1)	/* Hardware Triggered Strobe */

#define BINARY	(0<<0)	/* Binary counter (16 bits) */
#define BCD	(1<<0)	/* Binary Coded Decimal (BCD) Counter (4 Decades) */

/* Misc 8254/3 Defines */

#define TIMRFRQ (1193180UL)	/* Input frequency to the clock (Hz) */

/* Speaker Defines */

#define TIMER	(1<<0)	/* Timer 2 Output connected to Speaker */
#define SPKR_ON	(1<<1)	/* Turn on/off Speaker */

/* Port Definitions */

/* 8254/3 Ports */

#define CTR0	0x40
#define CTR1	0x41
#define CTR2	0x42
#define CTRL	0x43

/* Speaker Port */

#define SPEAKER	0x61

void
startsound (unsigned freq)
{
	/* To start a sound on the PC:
	 *
	 * First, set the second counter to have the correct frequency:
	 */

	unsigned count;

	if (freq == 0) freq = 523;

	count = TIMRFRQ / freq; /* Divide frequencies to get count. */

#ifdef TESTING
	printf ("freq = %u, count = %u\n", freq, count);
#endif

	outportb (CTRL, CTR2SEL|RW_LSB|RW_MSB|MODE3|BINARY);
	outportb (CTR2, count & 0x0FF);
	outportb (CTR2, count / 0x100);

	/* Next, turn on the speaker */

        outportb (SPEAKER, inportb(SPEAKER)|TIMER|SPKR_ON);
}

void
stopsound (void)
{
        outportb (SPEAKER, inportb(SPEAKER) & ~(TIMER|SPKR_ON));
}

static unsigned tempo, length, octave, mtype;

/* The important numbers here are 287700UL for the factors and 4050816000UL
 * which gives the 440 Hz for the A below middle C. "middle C" is assumed to
 * be the C at octave 3. The rest are computed by multiplication/division of
 * 2^(1/12) which came out to 1.05946 on my calculator.  It is assumed that
 * no one will request an octave beyond 6 or below 0.  (At octave 7, some
 * notes still come out ok, but by the end of the octave, the "notes" that
 * are produced are just ticks.

 * These numbers were chosen by a process based on the C64 tables (which
 * weren't standardized) and then were 'standardized' by giving them the
 * closest value.  That's why they don't seem to be based on any sensible
 * number.
 */

unsigned long notefactors[12] = { 483852, 456695, 431063, 406869, 384033,
	362479, 342135, 322932, 304808, 287700, 271553, 256312 };

void
note (long notenum)
{
	startsound ((unsigned) (4050816000UL / notefactors[notenum % 12]
			   >> (7 - notenum / 12)));
}

int notes[7] = { 9, 11, 0, 2, 4, 5, 7 };

char *
startnote (char *c)
{
	long n;

	n = notes[toupper(*c++) - 'A'] + octave * 12;
	if (*c == '#' || *c == '+') { n++; c++; }
	else if (*c == '-') { if (n) n--; c++; }

	note (n);

	return --c;
}

void
delaytime (unsigned time)
{
	/* time and twait are in units of milliseconds */

	unsigned twait;

	switch (toupper (mtype)) {
	   case 'S': twait = time / 4; break;
	   case 'L': twait = 0; break;
	   default: twait = time / 8; break;
	}

	msleep (time - twait);
	stopsound ();
	msleep (twait);
}

char *
delaynote (char *c)
{
	unsigned time = 0;

	while (isdigit(*c)) time = time * 10 + (*c++ - '0');

	if (!time) time = length;

	time = (unsigned)(240000 / time / tempo);

	while (*c == '.') { time = time * 3 / 2; c++; }

	delaytime (time);

	return c;
}

void
initspeaker (void)
{
	tempo = 120, length = 4, octave = 3, mtype = 'N';
}


void
play (char *tune)
{
	char *c, *n;
	unsigned num;

	for (c = tune; *c; ) {
	    sscanf (c + 1, "%u", &num);
	    for (n = c + 1; isdigit(*n); n++) /* do nothing */;
	    if (isspace(*c)) c++;
	    else switch (toupper(*c)) {
		case 'A':
		case 'B':
		case 'C':
		case 'D':
		case 'E':
		case 'F':
		case 'G':
		    c = startnote (c);
		case 'P':
		    c = delaynote (++c);
		    break;
#if 0
		case 'M': c++; mtype = *c; c++; break;
		case 'T':
		    if (num) tempo = num;
		    else printf ("Zero Tempo (%s)!\n", c);
		    c = n;
		    break;
		case 'L':
		    if (num) length = num;
		    else printf ("Zero Length (%s)!\n", c);
		    c = n;
		    break;
		case 'O':
		    if (num <= 7)
			octave = num;
		    c = n;
		    break;
		case 'N':
		    note (num);
		    delaytime ((240000/length/tempo));
		    c = n;
		    break;
		case '>': if (octave < 7) octave++; c++; break;
		case '<': if (octave) octave--; c++; break;
#endif
		case ' ': c++; break;
		default:
		    printf ("Unrecognized play value (%s)!\n", c);
                    return;
	    }
	}
}

#ifndef TESTING
void
pc_speaker (struct obj *instr, char *tune)
{
    if (!iflags.usepcspeaker) return;
    initspeaker ();
    switch (instr->otyp)
    {
	case WOODEN_FLUTE:
	case MAGIC_FLUTE:
	    octave = 5; /* up one octave */
	    break;
	case TOOLED_HORN:
	case FROST_HORN:
	case FIRE_HORN:
	    octave = 2; /* drop two octaves */
	    break;
	case BUGLE:
	    break;
	case WOODEN_HARP:
	case MAGIC_HARP:
	    length = 8;
	    mtype = 'L'; /* fast, legato */
	    break;
    }
    play (tune);
}

#else

main ()
{
	char s[80];
	int tool;

	initspeaker();
	printf ("1) flute\n2) horn\n3) harp\n4) other\n");
	fgets (s, 80, stdin);
	sscanf (s, "%d", &tool);
	switch (tool) {
	case 1: octave = 5; break;
	case 2: octave = 2; break;
	case 3: length = 8; mtype = 'L'; break;
	default: break;
	}
	printf ("Enter tune:");
	fgets(s, 80, stdin);
	play (s);
}
#endif

#endif /* PCMUSIC */

/* sound.c */