forked from aburch/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathsimsound.cc
292 lines (226 loc) · 4.86 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
/*
* This file is part of the Simutrans-Extended project under the Artistic License.
* (see LICENSE.txt)
*/
#include <stdio.h>
#include <string.h>
#include "macros.h"
#include "music/music.h"
#include "descriptor/sound_desc.h"
#include "sound/sound.h"
#include "simsound.h"
#include "sys/simsys.h"
#include "simio.h"
#include "simdebug.h"
#include "dataobj/environment.h"
#include "utils/plainstring.h"
#include "utils/simrandom.h"
#include "utils/simstring.h"
static bool new_midi = false;
static plainstring midi_title[MAX_MIDI];
static int max_midi = -1; // number of MIDI files
static int current_midi = -1; // init with error condition, reset during loading
void sound_set_global_volume(int volume)
{
env_t::global_volume = volume;
}
void sound_set_specific_volume( int volume, sound_type_t t)
{
env_t::specific_volume[t] = volume;
}
int sound_get_global_volume()
{
return env_t::global_volume;
}
int sound_get_specific_volume( sound_type_t t )
{
return env_t::specific_volume[t];
}
void sound_set_mute(bool f)
{
env_t::global_mute_sound = f;
}
bool sound_get_mute()
{
return ( env_t::global_mute_sound );
}
void sound_play(uint16 const idx, uint8 const v, sound_type_t t)
{
uint32 volume = v;
if( idx != (uint16)NO_SOUND && !env_t::global_mute_sound ) {
dr_play_sample(idx, ( (volume * env_t::global_volume * env_t::specific_volume[t] ) >> 16) );
}
}
bool sound_get_shuffle_midi()
{
return env_t::shuffle_midi;
}
void sound_set_shuffle_midi( bool shuffle )
{
env_t::shuffle_midi = shuffle;
}
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;
}
int sound_get_midi_volume()
{
return env_t::midi_volume;
}
/**
* gets midi title
*/
const char *sound_get_midi_title(int index)
{
if ( index >= 0 && index <= max_midi ) {
return midi_title[index];
}
else {
return "Invalid MIDI index!";
}
}
/**
* gets current midi number
*/
int get_current_midi()
{
return current_midi;
}
/**
* Load MIDI files
*/
int midi_init(const char *directory)
{
// read a list of soundfiles
std::string full_path = std::string(directory) + "music" + PATH_SEPARATOR + "music.tab";
if( FILE* const file = dr_fopen(full_path.c_str(), "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 ) {
full_path = std::string(directory) + buf;
dbg->message("midi_init()", " Reading MIDI file '%s' - %s", full_path.c_str(), title);
max_midi = dr_load_midi(full_path.c_str());
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.c_str() );
}
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<0) ? sim_async_rand(max_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
*/
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;
}