forked from aburch/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simsound.cc
322 lines (255 loc) · 5.31 KB
/
simsound.cc
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
/*
* Copyright (c) 1997 - 2001 Hansjörg Malthaner
*
* This file is part of the Simutrans project under the artistic license.
* (see license.txt)
*/
/*
* High-Level soundschnittstelle
* von Hj. Maltahner, 1998, 2000
*/
#include <stdio.h>
#include <string.h>
#include "macros.h"
#include "music/music.h"
#include "besch/sound_besch.h"
#include "sound/sound.h"
#include "simsound.h"
#include "simsys.h"
#include "simio.h"
#include "simdebug.h"
#include "dataobj/environment.h"
#include "utils/plainstring.h"
#include "utils/simrandom.h"
#include "utils/simstring.h"
/**
* max sound index
* @author hj. Malthaner
*/
static bool new_midi = false;
static plainstring midi_title[MAX_MIDI];
/**
* Gesamtlautstärke
* @author hj. Malthaner
*/
static int max_midi = -1; // number of MIDI files
static int current_midi = -1; // Hajo: init with error condition,
// reset during loading
/**
* setzt lautstärke für alle effekte
* @author Hj. Malthaner
*/
void sound_set_global_volume(int volume)
{
env_t::global_volume = volume;
}
/**
* ermittelt lautstaärke für alle effekte
* @author Hj. Malthaner
*/
int sound_get_global_volume()
{
return env_t::global_volume;
}
void sound_set_mute(bool on)
{
env_t::mute_sound = on;
}
bool sound_get_mute()
{
return ( env_t::mute_sound || SFX_CASH == NO_SOUND );
}
void sound_play(uint16 const idx, uint8 const volume)
{
if( idx != (uint16)NO_SOUND && !env_t::mute_sound ) {
dr_play_sample(idx, volume * env_t::global_volume >> 8);
}
}
bool sound_get_shuffle_midi()
{
return env_t::shuffle_midi;
}
void sound_set_shuffle_midi( bool shuffle )
{
env_t::shuffle_midi = shuffle;
}
/**
* setzt Lautstärke für MIDI playback
* @param volume volume in range 0..255
* @author Hj. Malthaner
*/
void sound_set_midi_volume(int volume)
{
if( !env_t::mute_midi && max_midi > -1 ) {
dr_set_midi_volume(volume);
}
env_t::midi_volume = volume;
}
/**
* ermittelt Lautstärke für MIDI playback
* @return volume in range 0..255
* @author Hj. Malthaner
*/
int sound_get_midi_volume()
{
return env_t::midi_volume;
}
/**
* gets midi title
* @author Hj. Malthaner
*/
const char *sound_get_midi_title(int index)
{
if ( index >= 0 && index <= max_midi ) {
return midi_title[index];
}
else {
return "Invalid MIDI index!";
}
}
/**
* gets curent midi number
* @author Hj. Malthaner
*/
int get_current_midi()
{
return current_midi;
}
/**
* Load MIDI files
* By Owen Rudge
*/
int midi_init(const char *directory)
{
// read a list of soundfiles
char full_path[1024];
sprintf( full_path, "%smusic/music.tab", directory );
if( FILE* const file = fopen(full_path, "rb") ) {
while(!feof(file)) {
char buf[256];
char title[256];
size_t len;
read_line(buf, sizeof(buf), file);
read_line(title, sizeof(title), file);
if( !feof(file) ) {
len = strlen(buf);
while( len>0 && buf[--len] <= 32 ) {
buf[len] = 0;
}
if( len > 1 ) {
sprintf( full_path, "%s%s", directory, buf );
printf(" Reading MIDI file '%s' - %s", full_path, title);
max_midi = dr_load_midi(full_path);
if( max_midi >= 0 ) {
len = strlen(title);
while( len > 0 && title[--len] <= 32 ) {
title[len] = 0;
}
midi_title[max_midi] = title;
}
}
}
}
fclose(file);
}
else {
dbg->warning("midi_init()","can't open file '%s' for reading.", full_path);
}
if( max_midi >= 0 ) {
current_midi = 0;
}
// success?
return ( max_midi >= 0 );
}
void midi_play(const int no)
{
if( no > max_midi ) {
dbg->warning("midi_play()", "MIDI index %d too high (total loaded: %d)", no, max_midi);
}
else if( !midi_get_mute() ) {
dr_play_midi(no);
}
}
void midi_stop()
{
if( !midi_get_mute() ) {
dr_stop_midi();
}
}
void midi_set_mute(bool on)
{
on |= ( max_midi == -1 );
if( on ) {
if( !env_t::mute_midi ) {
dr_stop_midi();
}
env_t::mute_midi = true;
}
else {
if( env_t::mute_midi ) {
env_t::mute_midi = false;
midi_play(current_midi);
}
dr_set_midi_volume(env_t::midi_volume);
}
}
bool midi_get_mute()
{
return ( env_t::mute_midi || max_midi == -1 );
}
/*
* Check if need to play new MIDI
* Max Kielland:
* Made it possible to get next song
* even if we are muted.
*/
void check_midi()
{
// Check for next sound
if (new_midi || (!midi_get_mute() && dr_midi_pos() < 0)) {
if( env_t::shuffle_midi && max_midi > 1 ) {
// shuffle songs (must not use simrand()!)
int new_song = sim_async_rand(max_midi);
if( new_song >= current_midi ) {
new_song ++;
}
current_midi = new_song;
}
else {
current_midi++;
if( current_midi > max_midi ) {
current_midi = 0;
}
}
// Are we in playing mode?
if( false == midi_get_mute() ) {
midi_play(current_midi);
DBG_MESSAGE("check_midi()", "Playing MIDI %d", current_midi);
}
}
new_midi = false;
}
/**
* shuts down midi playing
* @author Owen Rudge
*/
void close_midi()
{
if( max_midi > -1 ) {
dr_destroy_midi();
}
}
void midi_next_track()
{
new_midi = true;
}
void midi_last_track()
{
if ( current_midi == 0 ) {
current_midi = max_midi - 1;
}
else {
current_midi = current_midi - 2;
}
new_midi = true;
}