aboutsummaryrefslogtreecommitdiff
path: root/engines/testbed/midi.cpp
blob: 0ec2678d478550d255caff091991043d2a4b950f (plain)
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 */

#include "common/archive.h"
#include "common/events.h"

#include "graphics/cursorman.h"

#include "sound/mididrv.h"

#include "testbed/midi.h"
#include "testbed/testbed.h"

namespace Testbed {

bool MidiTests::loadMusicInMemory(Common::MemoryWriteStreamDynamic *ws) {
	Common::SeekableReadStream *midiFile = SearchMan.createReadStreamForMember("music.mid");
	if (!midiFile) {
		Testsuite::logPrintf("Error! Can't open Midi music file, check game data directory for file music.mid\n");
		return false;
	}

	while (!midiFile->eos()) {
		byte data = midiFile->readByte();
		ws->writeByte(data);
	}
	return true;
}

void MidiTests::waitForMusicToPlay(MidiParser *parser) {
	Common::EventManager *eventMan = g_system->getEventManager();
	bool quitLoop = false;
	Common::Event event;

	CursorMan.showMouse(true);
	while (!quitLoop) {
		while (eventMan->pollEvent(event)) {
			// Quit if explicitly requested!
			if (Engine::shouldQuit()) {
				return;
			}

			if (event.type == Common::EVENT_LBUTTONDOWN || event.type == Common::EVENT_RBUTTONDOWN) {
				quitLoop = true;
			} else {
				Testsuite::writeOnScreen("Playing Midi Music, Click to end", Common::Point(0, 100));
				if (!parser->isPlaying()) {
					quitLoop = true;
				}
			}
		}
	}
	CursorMan.showMouse(false);
	return;
}

TestExitStatus MidiTests::playMidiMusic() {
	Testsuite::clearScreen();
	Common::String info = "Testing Midi Sound output.\n"
						  "Here, We generate some Music by using the Midi Driver selected in the GUI.\n"
						  "You should expect to hear that. The initialization may take some time.\n";

	if (Testsuite::handleInteractiveInput(info, "OK", "Skip", kOptionRight)) {
		Testsuite::logPrintf("Info! Skipping test : Play Midi Music\n");
		return kTestSkipped;
	}

	MidiDriver::DeviceHandle dev = MidiDriver::detectDevice(MDT_MIDI | MDT_ADLIB);
	// Create a driver instance
	MidiDriver *driver = MidiDriver::createMidi(dev);
	// Create a SMF parser
	MidiParser *smfParser = MidiParser::createParser_SMF();
	// Open the driver
	int errCode = driver->open();

	if (errCode) {
		Common::String errMsg = driver->getErrorName(errCode);
		Testsuite::writeOnScreen(errMsg, Common::Point(0, 100));
		Testsuite::logPrintf("Error! %s", errMsg.c_str());
		return kTestFailed;
	}

	Testsuite::logDetailedPrintf("Info! Midi: Succesfully opened the driver\n");

	Common::MemoryWriteStreamDynamic ws(DisposeAfterUse::YES);
	loadMusicInMemory(&ws);

	// start playing
	if (smfParser->loadMusic(ws.getData(), ws.size())) {
		smfParser->setTrack(0);
		smfParser->setMidiDriver(driver);
		smfParser->setTimerRate(driver->getBaseTempo());
		driver->setTimerCallback(smfParser, MidiParser::timerCallback);
		Testsuite::logDetailedPrintf("Info! Midi: Parser Successfully loaded Music data.\n");
		if (smfParser->isPlaying()) {
			Testsuite::writeOnScreen("Playing Midi Music, Click to end.", Common::Point(0, 100));
			Testsuite::logDetailedPrintf("Info! Midi: Playing music!\n");
		}
	}


	// Play until track ends or an exit is requested.
	waitForMusicToPlay(smfParser);

	// Done. Clean up.
	smfParser->unloadMusic();
	driver->setTimerCallback(NULL, NULL);
	driver->close();
	delete smfParser;
	delete driver;

	if (Testsuite::handleInteractiveInput("Were you able to hear the music as described?", "Yes", "No", kOptionRight)) {
		Testsuite::logDetailedPrintf("Error! Midi: Can't play Music\n");
		return kTestFailed;
	}
	return kTestPassed;
}

MidiTestSuite::MidiTestSuite() {
	addTest("MidiTests", &MidiTests::playMidiMusic);
	_isMidiDataFound = true;
	if (!SearchMan.hasFile("music.mid")) {
		// add some fallback test if filesystem loading failed
		Testsuite::logPrintf("Warning! Midi: Sound data file music.mid not found\n");
		_isMidiDataFound = false;
		enable(false);
	}
}

void MidiTestSuite::enable(bool flag) {
	Testsuite::enable(_isMidiDataFound & flag);
}

}