-
Notifications
You must be signed in to change notification settings - Fork 6
/
midi_mci.cpp
executable file
·133 lines (103 loc) · 2.72 KB
/
midi_mci.cpp
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
#include <string>
#include <cstdlib>
#include "audiere.h"
#include "internal.h"
#include "mci_device.h"
namespace audiere {
/**
* Since MCI uses a global list of device aliases, this prevents name
* collisions. Maybe should be synchronized or volatile.
*/
static int g_song_count = 0;
class MCIMIDIStream : public RefImplementation<MIDIStream>, public MCIDevice {
public:
static MCIMIDIStream* create(const char* filename) {
// Synchronize on g_song_count?
char alias[80];
sprintf(alias, "adr_song_%d", g_song_count);
bool error;
std::string result = sendString(
std::string("open \"") + filename + "\" type sequencer alias " + alias,
&error);
if (error) {
return 0;
}
++g_song_count;
return new MCIMIDIStream(alias);
}
MCIMIDIStream(const std::string& device)
: MCIDevice(device)
, m_repeat(false)
{
sendCommand("set", "time format milliseconds", MCI_WAIT);
}
ADR_METHOD(void) play() {
if (isPlaying()) {
return;
}
if (getPosition() == getLength()) {
setPosition(0);
}
sendCommand("play", "", MCI_NOTIFY);
}
ADR_METHOD(void) stop() {
pause();
setPosition(0);
}
ADR_METHOD(void) pause() {
sendCommand("stop");
}
ADR_METHOD(bool) isPlaying() {
return sendCommand("status", "mode") == "playing";
}
ADR_METHOD(int) getLength() {
return atoi(sendCommand("status", "length").c_str());
}
ADR_METHOD(int) getPosition() {
int pos = atoi(sendCommand("status", "position").c_str());
int length = getLength();
return (length == 0 ? pos : pos % length);
}
ADR_METHOD(void) setPosition(int position) {
bool playing = isPlaying();
char buffer[80];
sprintf(buffer, "%d", position);
sendCommand("seek", std::string("to ") + buffer, MCI_WAIT);
if (playing) {
play();
}
}
ADR_METHOD(bool) getRepeat() {
return m_repeat;
}
ADR_METHOD(void) setRepeat(bool repeat) {
m_repeat = repeat;
}
protected:
void notify(WPARAM flags) {
if (flags == MCI_NOTIFY_SUCCESSFUL) {
stop();
if (m_repeat) {
play();
}
}
}
private:
std::string m_device;
bool m_repeat;
};
class MCIMIDIDevice : public RefImplementation<MIDIDevice> {
public:
ADR_METHOD(const char*) getName() {
return "MCI";
}
ADR_METHOD(MIDIStream*) openStream(const char* filename) {
return MCIMIDIStream::create(filename);
}
};
namespace hidden {
ADR_EXPORT(MIDIDevice*) AdrOpenMIDIDevice(const char* /*name*/) {
return new MCIMIDIDevice;
}
}
}