author | bjarni |
Wed, 28 Feb 2007 00:33:40 +0000 | |
branch | cpp_gui |
changeset 6268 | 4b5241e5dd10 |
parent 5835 | e0ff603ae0b7 |
child 6298 | c30fe89622df |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
2189
d240b9097139
(svn r2704) Remove . from include path again, too much trouble. Also add some #ifdefs and #includes for the Windows build
tron
parents:
2186
diff
changeset
|
3 |
#include "../stdafx.h" |
d240b9097139
(svn r2704) Remove . from include path again, too much trouble. Also add some #ifdefs and #includes for the Windows build
tron
parents:
2186
diff
changeset
|
4 |
#include "win32_m.h" |
0 | 5 |
#include <windows.h> |
2189
d240b9097139
(svn r2704) Remove . from include path again, too much trouble. Also add some #ifdefs and #includes for the Windows build
tron
parents:
2186
diff
changeset
|
6 |
#include <mmsystem.h> |
0 | 7 |
|
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
8 |
static struct { |
0 | 9 |
bool stop_song; |
10 |
bool terminate; |
|
11 |
bool playing; |
|
12 |
int new_vol; |
|
13 |
HANDLE wait_obj; |
|
2458
c95808cd2ac9
(svn r2984) Use adequate types, this should aid portability a bit
tron
parents:
2434
diff
changeset
|
14 |
UINT_PTR devid; |
0 | 15 |
char start_song[260]; |
16 |
} _midi; |
|
17 |
||
18 |
static void Win32MidiPlaySong(const char *filename) |
|
19 |
{ |
|
20 |
strcpy(_midi.start_song, filename); |
|
21 |
_midi.playing = true; |
|
22 |
_midi.stop_song = false; |
|
23 |
SetEvent(_midi.wait_obj); |
|
24 |
} |
|
25 |
||
1102
316643e34104
(svn r1603) -Fix: unused variable in FormatTinyDate
darkvater
parents:
1040
diff
changeset
|
26 |
static void Win32MidiStopSong(void) |
0 | 27 |
{ |
28 |
if (_midi.playing) { |
|
29 |
_midi.stop_song = true; |
|
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
30 |
_midi.start_song[0] = '\0'; |
0 | 31 |
SetEvent(_midi.wait_obj); |
32 |
} |
|
33 |
} |
|
34 |
||
1102
316643e34104
(svn r1603) -Fix: unused variable in FormatTinyDate
darkvater
parents:
1040
diff
changeset
|
35 |
static bool Win32MidiIsSongPlaying(void) |
0 | 36 |
{ |
37 |
return _midi.playing; |
|
38 |
} |
|
39 |
||
40 |
static void Win32MidiSetVolume(byte vol) |
|
41 |
{ |
|
42 |
_midi.new_vol = vol; |
|
43 |
SetEvent(_midi.wait_obj); |
|
44 |
} |
|
45 |
||
2548
97ada3bd2702
(svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents:
2458
diff
changeset
|
46 |
static MCIERROR CDECL MidiSendCommand(const char* cmd, ...) |
97ada3bd2702
(svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents:
2458
diff
changeset
|
47 |
{ |
0 | 48 |
va_list va; |
49 |
char buf[512]; |
|
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
50 |
|
0 | 51 |
va_start(va, cmd); |
6268
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
5835
diff
changeset
|
52 |
vsnprintf(buf, lengthof(buf), cmd, va); |
0 | 53 |
va_end(va); |
54 |
return mciSendStringA(buf, NULL, 0, 0); |
|
55 |
} |
|
56 |
||
57 |
static bool MidiIntPlaySong(const char *filename) |
|
58 |
{ |
|
59 |
MidiSendCommand("close all"); |
|
6268
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
5835
diff
changeset
|
60 |
if (MidiSendCommand("open \"%s\" type sequencer alias song", filename) != 0) return false; |
0 | 61 |
|
6268
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
5835
diff
changeset
|
62 |
return MidiSendCommand("play song from 0") == 0; |
0 | 63 |
} |
64 |
||
1102
316643e34104
(svn r1603) -Fix: unused variable in FormatTinyDate
darkvater
parents:
1040
diff
changeset
|
65 |
static void MidiIntStopSong(void) |
0 | 66 |
{ |
67 |
MidiSendCommand("close all"); |
|
68 |
} |
|
69 |
||
70 |
static void MidiIntSetVolume(int vol) |
|
71 |
{ |
|
2458
c95808cd2ac9
(svn r2984) Use adequate types, this should aid portability a bit
tron
parents:
2434
diff
changeset
|
72 |
DWORD v = (vol * 65535 / 127); |
2434
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
73 |
midiOutSetVolume((HMIDIOUT)_midi.devid, v + (v << 16)); |
0 | 74 |
} |
75 |
||
1102
316643e34104
(svn r1603) -Fix: unused variable in FormatTinyDate
darkvater
parents:
1040
diff
changeset
|
76 |
static bool MidiIntIsSongPlaying(void) |
0 | 77 |
{ |
78 |
char buf[16]; |
|
79 |
mciSendStringA("status song mode", buf, sizeof(buf), 0); |
|
80 |
return strcmp(buf, "playing") == 0 || strcmp(buf, "seeking") == 0; |
|
81 |
} |
|
82 |
||
83 |
static DWORD WINAPI MidiThread(LPVOID arg) |
|
84 |
{ |
|
85 |
_midi.wait_obj = CreateEvent(NULL, FALSE, FALSE, NULL); |
|
86 |
||
87 |
do { |
|
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
88 |
char *s; |
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
89 |
int vol; |
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
90 |
|
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
91 |
vol = _midi.new_vol; |
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
92 |
if (vol != -1) { |
0 | 93 |
_midi.new_vol = -1; |
94 |
MidiIntSetVolume(vol); |
|
95 |
} |
|
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
96 |
|
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
97 |
s = _midi.start_song; |
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
98 |
if (s[0] != '\0') { |
0 | 99 |
_midi.playing = MidiIntPlaySong(s); |
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
100 |
s[0] = '\0'; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
101 |
|
0 | 102 |
// Delay somewhat in case we don't manage to play. |
6268
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
5835
diff
changeset
|
103 |
if (!_midi.playing) Sleep(5000); |
0 | 104 |
} |
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
105 |
|
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
106 |
if (_midi.stop_song && _midi.playing) { |
0 | 107 |
_midi.stop_song = false; |
108 |
_midi.playing = false; |
|
109 |
MidiIntStopSong(); |
|
110 |
} |
|
111 |
||
6268
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
5835
diff
changeset
|
112 |
if (_midi.playing && !MidiIntIsSongPlaying()) _midi.playing = false; |
0 | 113 |
|
114 |
WaitForMultipleObjects(1, &_midi.wait_obj, FALSE, 1000); |
|
115 |
} while (!_midi.terminate); |
|
116 |
||
117 |
DeleteObject(_midi.wait_obj); |
|
118 |
return 0; |
|
119 |
} |
|
120 |
||
1301
313804601383
(svn r1805) Teach the driver layer a few things about const correctness
tron
parents:
1230
diff
changeset
|
121 |
static const char *Win32MidiStart(const char * const *parm) |
0 | 122 |
{ |
2434
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
123 |
MIDIOUTCAPS midicaps; |
0 | 124 |
DWORD threadId; |
2458
c95808cd2ac9
(svn r2984) Use adequate types, this should aid portability a bit
tron
parents:
2434
diff
changeset
|
125 |
UINT nbdev; |
c95808cd2ac9
(svn r2984) Use adequate types, this should aid portability a bit
tron
parents:
2434
diff
changeset
|
126 |
UINT_PTR dev; |
2212
7e53bdecc115
(svn r2730) -Fix: Fail if the sequencer can't play audio
tron
parents:
2189
diff
changeset
|
127 |
char buf[16]; |
7e53bdecc115
(svn r2730) -Fix: Fail if the sequencer can't play audio
tron
parents:
2189
diff
changeset
|
128 |
|
7e53bdecc115
(svn r2730) -Fix: Fail if the sequencer can't play audio
tron
parents:
2189
diff
changeset
|
129 |
mciSendStringA("capability sequencer has audio", buf, lengthof(buf), 0); |
7e53bdecc115
(svn r2730) -Fix: Fail if the sequencer can't play audio
tron
parents:
2189
diff
changeset
|
130 |
if (strcmp(buf, "true") != 0) return "MCI sequencer can't play audio"; |
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
131 |
|
0 | 132 |
memset(&_midi, 0, sizeof(_midi)); |
133 |
_midi.new_vol = -1; |
|
2212
7e53bdecc115
(svn r2730) -Fix: Fail if the sequencer can't play audio
tron
parents:
2189
diff
changeset
|
134 |
|
2434
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
135 |
/* Get midi device */ |
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
136 |
_midi.devid = MIDI_MAPPER; |
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
137 |
for (dev = 0, nbdev = midiOutGetNumDevs(); dev < nbdev; dev++) { |
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
138 |
if (midiOutGetDevCaps(dev, &midicaps, sizeof(midicaps)) == 0 && (midicaps.dwSupport & MIDICAPS_VOLUME)) { |
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
139 |
_midi.devid = dev; |
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
140 |
break; |
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
141 |
} |
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
142 |
} |
3428947c9c87
(svn r2960) - Fix: [ 1227779 ] Fix win32 midi volume level control which didn't work (glx)
Darkvater
parents:
2212
diff
changeset
|
143 |
|
6268
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
5835
diff
changeset
|
144 |
if (CreateThread(NULL, 8192, MidiThread, 0, 0, &threadId) == NULL) return "Failed to create thread"; |
2212
7e53bdecc115
(svn r2730) -Fix: Fail if the sequencer can't play audio
tron
parents:
2189
diff
changeset
|
145 |
|
7e53bdecc115
(svn r2730) -Fix: Fail if the sequencer can't play audio
tron
parents:
2189
diff
changeset
|
146 |
return NULL; |
0 | 147 |
} |
148 |
||
1102
316643e34104
(svn r1603) -Fix: unused variable in FormatTinyDate
darkvater
parents:
1040
diff
changeset
|
149 |
static void Win32MidiStop(void) |
0 | 150 |
{ |
151 |
_midi.terminate = true; |
|
152 |
SetEvent(_midi.wait_obj); |
|
153 |
} |
|
154 |
||
155 |
const HalMusicDriver _win32_music_driver = { |
|
156 |
Win32MidiStart, |
|
157 |
Win32MidiStop, |
|
158 |
Win32MidiPlaySong, |
|
159 |
Win32MidiStopSong, |
|
160 |
Win32MidiIsSongPlaying, |
|
161 |
Win32MidiSetVolume, |
|
162 |
}; |