summaryrefslogtreecommitdiffstats
path: root/apps/plugins/frotz/sound.c
blob: ea0e3570e615b4b5f0927f7d74f57ba75c296f4a (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
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
/* sound.c - Sound effect function
 *	Copyright (c) 1995-1997 Stefan Jokisch
 *
 * This file is part of Frotz.
 *
 * Frotz is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Frotz is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include "frotz.h"

#ifdef DJGPP
#include "djfrotz.h"
#endif

#define EFFECT_PREPARE 1
#define EFFECT_PLAY 2
#define EFFECT_STOP 3
#define EFFECT_FINISH_WITH 4

extern int direct_call (zword);

static zword routine = 0;

static int next_sample = 0;
static int next_volume = 0;

static bool locked = FALSE;
static bool playing = FALSE;

/*
 * init_sound
 *
 * Initialize sound variables.
 *
 */

void init_sound (void)
{
    locked = FALSE;
    playing = FALSE;
} /* init_sound */


/*
 * start_sample
 *
 * Call the IO interface to play a sample.
 *
 */

static void start_sample (int number, int volume, int repeats, zword eos)
{

    static zbyte lh_repeats[] = {
	0x00, 0x00, 0x00, 0x01, 0xff,
	0x00, 0x01, 0x01, 0x01, 0x01,
	0xff, 0x01, 0x01, 0xff, 0x00,
	0xff, 0xff, 0xff, 0xff, 0xff
    };

    if (story_id == LURKING_HORROR)
	repeats = lh_repeats[number];

    os_start_sample (number, volume, repeats, eos);

    routine = eos;
    playing = TRUE;

}/* start_sample */

/*
 * start_next_sample
 *
 * Play a sample that has been delayed until the previous sound effect has
 * finished.  This is necessary for two samples in The Lurking Horror that
 * immediately follow other samples.
 *
 */

static void start_next_sample (void)
{

    if (next_sample != 0)
	start_sample (next_sample, next_volume, 0, 0);

    next_sample = 0;
    next_volume = 0;

}/* start_next_sample */

/*
 * end_of_sound
 *
 * Call the Z-code routine which was given as the last parameter of
 * a sound_effect call. This function may be called from a hardware
 * interrupt (which requires extremely careful programming).
 *
 */

void end_of_sound (void)
{

#if defined(DJGPP) && defined(SOUND_SUPPORT)
    end_of_sound_flag = 0;
#endif

    playing = FALSE;

    if (!locked) {

	if (story_id == LURKING_HORROR)
	    start_next_sample ();

	direct_call (routine);

    }

}/* end_of_sound */

/*
 * z_sound_effect, load / play / stop / discard a sound effect.
 *
 *   	zargs[0] = number of bleep (1 or 2) or sample
 *	zargs[1] = operation to perform (samples only)
 *	zargs[2] = repeats and volume (play sample only)
 *	zargs[3] = end-of-sound routine (play sample only, optional)
 *
 * Note: Volumes range from 1 to 8, volume 255 is the default volume.
 *	 Repeats are stored in the high byte, 255 is infinite loop.
 *
 */

void z_sound_effect (void)
{
    zword number = zargs[0];
    zword effect = zargs[1];
    zword volume = zargs[2];

    /* By default play sound 1 at volume 8 */
    if (zargc < 1)
	number = 1;
    if (zargc < 2)
	effect = EFFECT_PLAY;
    if (zargc < 3)
	volume = 8;

    if (number >= 3 || number == 0) {

	locked = TRUE;

	if (story_id == LURKING_HORROR && (number == 9 || number == 16)) {

	    if (effect == EFFECT_PLAY) {

		next_sample = number;
		next_volume = volume;

		locked = FALSE;

		if (!playing)
		    start_next_sample ();

	    } else locked = FALSE;

	    return;

	}

	playing = FALSE;

	switch (effect) {

	case EFFECT_PREPARE:
	    os_prepare_sample (number);
	    break;
	case EFFECT_PLAY:
	    start_sample (number, lo (volume), hi (volume), (zargc == 4) ? zargs[3] : 0);
	    break;
	case EFFECT_STOP:
	    os_stop_sample (number);
	    break;
	case EFFECT_FINISH_WITH:
	    os_finish_with_sample (number);
	    break;

	}

	locked = FALSE;

    } else os_beep (number);

}/* z_sound_effect */