File: macsnd.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 (107 lines) | stat: -rw-r--r-- 2,401 bytes parent folder | download | duplicates (22)
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
/*	SCCS Id: @(#)macsnd.c	3.1	92/11/28	*/
/* 	Copyright (c) 1992 by Jon Watte */
/* NetHack may be freely redistributed.  See license for details. */

/*
 * This file contains music playing code.
 *
 * If we were REALLY determinated, we would make the sound play
 * asynchronously, but I'll save that one for a rainy day...
 *
 * This may break A/UX, since it defines MAC but we need to
 * check that the toolbox is booted. I'll defer that one too.
 *
 * - h+ 921128
 */

#include "hack.h"
#include "mactty.h"
#include "macwin.h"
#if !TARGET_API_MAC_CARBON
# include <Sound.h>
# include <Resources.h>
#else
# define freqDurationCmd 40
#endif

#define SND_BUFFER(s) (&(*s)[20])
#define SND_LEN(s) (GetHandleSize(s)-42)


void
mac_speaker (struct obj *instr, char *melody) {
	SndChannelPtr theChannel = (SndChannelPtr) 0;
	SndCommand theCmd;
	Handle theSound;
	unsigned char theName [32];
	char *n = (char *) &theName [1];
	int typ = instr->otyp;
	const char *actualn = OBJ_NAME (objects [typ]);

	/*
	 * First: are we in the library ?
	 */
	if (flags.silent) {
		return;
	}

	/*
	 * Is this a known instrument ?
	 */
	strcpy (n, actualn);
	theName [0] = strlen (n);
	theSound = GetNamedResource ('snd ', theName);
	if (! theSound) {
		return;
	}
	HLock (theSound);

	/*
	 * Set up the synth
	 */
	if (SndNewChannel(&theChannel, sampledSynth, initMono +
		initNoInterp, (void *) 0) == noErr) {
		char midi_note [] = {57, 59, 60, 62, 64, 65, 67};

		short err;
		short snd_len = SND_LEN (theSound) / 18;

		theCmd.cmd = soundCmd;
		theCmd.param1 = 0;
		theCmd.param2 = (long) SND_BUFFER (theSound);
		err = SndDoCommand (theChannel, &theCmd, false);

	/*
	 * We rack 'em up all in a row
	 * The mac will play them correctly and then end, since
	 * we do a sync close below.
	 *
	 */
		while (*melody && ! err) {
			while (*melody > 'G') {
				*melody -= 8;
			}
			while (*melody < 'A') {
				*melody += 8;
			}
			theCmd.cmd = freqDurationCmd;
			theCmd.param1 = snd_len;
			theCmd.param2 = midi_note [*melody - 'A'];
			err = SndDoCommand (theChannel, &theCmd, false);
			melody ++;
		}
		SndDisposeChannel (theChannel, false);	/* Sync wait for completion */
		ReleaseResource (theSound);
	}
}

void tty_nhbell (void) {
	Handle h = GetNamedResource ('snd ', "\pNetHack Bell");

	if (h) {
		HLock (h);
		SndPlay ((SndChannelPtr) 0, (SndListHandle) h, 0);
		ReleaseResource (h);
	} else
		SysBeep (30);
}