forked from teamhimeh/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simsound.cc
266 lines (208 loc) · 4.17 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
/*
* simsound.cc
*
* Copyright (c) 1997 - 2001 Hansjörg Malthaner
*
* This file is part of the Simutrans project and may not be used
* in other projects without written permission of the author.
*/
/*
* High-Level soundschnittstelle
* von Hj. Maltahner, 1998, 2000
*/
#include <stdio.h>
#include <string.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 "utils/simstring.h"
/**
* max sound index
* @author hj. Malthaner
*/
static int new_midi = 0;
#define MAX_MIDI_TITLE 30
static char midi_title[128][MAX_MIDI_TITLE];
/**
* Gesamtlautstärke
* @author hj. Malthaner
*/
static int global_volume = 128;
/**
* MIDI Lautstärke
* @author hj. Malthaner
*/
static int midi_volume = 128;
static int max_midi = -1; // number of MIDI files
static int current_midi = -1; // Hajo: init with error condition,
// reset during loading
/**
* setzt lautstaärke für alle effekte
* @author Hj. Malthaner
*/
void sound_set_global_volume(int volume)
{
// printf("Message: setting global volume to %d.\n", volume);
global_volume = volume;
}
/**
* ermittelt lautstaärke für alle effekte
* @author Hj. Malthaner
*/
int sound_get_global_volume()
{
return global_volume;
}
/**
* spielt sound ab
* @author Hj. Malthaner
*/
void
sound_play(const struct sound_info info)
{
if(info.index!=NO_SOUND) {
DBG_MESSAGE("karte_t::interactive_event(event_t &ev)", "play sound %i",info.index);
dr_play_sample(info.index, (info.volume*global_volume)>>8);
}
}
/**
* setzt Lautstärke für MIDI playback
* @param volume volume in range 0..255
* @author Hj. Malthaner
*/
void sound_set_midi_volume(int volume)
{
dr_set_midi_volume(volume);
midi_volume = volume;
}
/**
* Sets the MIDI volume variable - for internal use only
* @param volume Volume in range 0-255
* @author Owen Rudge
*/
extern "C" void sound_set_midi_volume_var(int volume)
{
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 midi_volume;
}
/**
* gets midi title
* @author Hj. Malthaner
*/
const char * sound_get_midi_title(int index)
{
if(index >= 0 && index <= max_midi && midi_title[index] != NULL) {
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
*/
void
midi_init()
{
// read a list of soundfiles
FILE * file = fopen("music.tab", "rb");
if(file) {
dr_init_midi();
while(!feof(file)) {
char buf[80];
char title[80];
int len;
read_line(buf, 80, file);
read_line(title, 80, file);
if(!feof(file)) {
len = strlen(buf);
while(buf[--len] < 32) {
buf[len] = 0;
}
if(len > 1) {
print(" Reading MIDI file '%s' - %s", buf, title);
max_midi = dr_load_midi(buf);
if(max_midi >= 0 && max_midi < MAX_MIDI_TITLE) {
tstrncpy(midi_title[max_midi], title, 128);
}
}
}
}
fclose(file);
} else {
dbg->warning("midi_init()",
"can't open file 'music.tab' for reading, turning music off.");
}
if(max_midi >= 0) {
current_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 {
dr_play_midi(no);
}
}
void midi_stop()
{
dr_stop_midi();
}
/*
* Check if need to play new MIDI
*/
void check_midi()
{
if((dr_midi_pos() < 0 || new_midi == 1) && max_midi > -1) {
current_midi++;
if (current_midi > max_midi)
current_midi = 0;
midi_play(current_midi);
DBG_MESSAGE("check_midi()",
"Playing MIDI %d", current_midi);
new_midi = 0;
}
}
/**
* shuts down midi playing
* @author Owen Rudge
*/
void close_midi()
{
dr_destroy_midi();
}
void midi_next_track()
{
new_midi = 1;
}
void midi_last_track()
{
if (current_midi == 0)
current_midi = max_midi - 1;
else
current_midi = current_midi - 2;
new_midi = 1;
}