forked from teamhimeh/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdl_midi.cc
128 lines (109 loc) · 2.16 KB
/
sdl_midi.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
/*
* This file is part of the Simutrans project under the Artistic License.
* (see LICENSE.txt)
*/
#include <SDL.h>
#include <SDL_mixer.h>
#include "../simdebug.h"
#include "../utils/plainstring.h"
#include "music.h"
// SDL_Mixer music routine interfaces
static int midi_number = -1;
static plainstring midi_filenames[MAX_MIDI];
//Mix_Music *music[MAX_MIDI];
Mix_Music *music = NULL;
/**
* sets midi playback volume
*/
void dr_set_midi_volume(int vol)
{
Mix_VolumeMusic((vol*MIX_MAX_VOLUME)/256);
}
/**
* Loads a MIDI file
*/
int dr_load_midi(const char * filename)
{
if (midi_number < MAX_MIDI - 1) {
const int i = midi_number + 1;
if(i >= 0 && i < MAX_MIDI) {
music = Mix_LoadMUS(filename);
if(music) {
midi_number = i;
midi_filenames[i] = filename;
}
else {
dbg->warning( "dr_load_midi()", "Failed to load MIDI %s because %s", filename, Mix_GetError() );
}
Mix_FreeMusic(music);
music = NULL;
}
}
return midi_number;
}
/**
* Plays a MIDI file
*/
void dr_play_midi(int key)
{
if(dr_midi_pos()!= -1) {
dr_stop_midi();
}
music = Mix_LoadMUS(midi_filenames[key]);
Mix_PlayMusic(music, 1);
}
/**
* Stops playing MIDI file
*/
void dr_stop_midi(void)
{
if(music) {
Mix_HaltMusic();
Mix_FreeMusic(music);
music = NULL;
}
}
/**
* Returns the midi_pos variable <- doesn't actually do this
* Simutrans only needs to know whether file has finished (so that it can start the next music)
* Returns -1 if current music has finished, else 0
*/
sint32 dr_midi_pos(void)
{
if(Mix_PlayingMusic()== 0) {
return -1;
}
else {
return 0;
}
}
/**
* Midi shutdown/cleanup
*/
void dr_destroy_midi(void)
{
dr_stop_midi();
midi_number = -1;
}
/**
* MIDI initialisation routines
*/
bool dr_init_midi(void)
{
// if audio not init
if(!SDL_WasInit(SDL_INIT_AUDIO)) {
// if audio subsys is ok
if(SDL_InitSubSystem(SDL_INIT_AUDIO) != -1) {
if(Mix_OpenAudio(22050, AUDIO_S16SYS, 2, 1024)==-1) {
//if OpenAudio returns error, dr_init_midi is false
return false;
}
}
else {
//if SDL_InitSubSystem returns error, dr_init_midi is false
return false;
}
}
//if all is fine, return true
return true;
}