aboutsummaryrefslogtreecommitdiff
path: root/sound/mods/module.cpp
blob: 291f95fe3fdf711898ab6b39515b0e46dbedbbd1 (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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2006 The ScummVM project
 * Based on code by madmoose
 *
 * 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/stdafx.h"

#include "sound/mods/module.h"

#include "common/util.h"

namespace Modules {

bool Module::load(Common::ReadStream &st) {
	st.read(songname, 20);
	songname[20] = '\0';

	for (int i = 0; i < 31; ++i) {
		st.read(sample[i].name, 22);
		sample[i].name[22] = '\0';
		sample[i].len = 2 * st.readUint16BE();

		sample[i].finetune = st.readByte();
		assert(sample[i].finetune < 0x10);

		sample[i].vol = st.readByte();
		sample[i].repeat = 2 * st.readUint16BE();
		sample[i].replen = 2 * st.readUint16BE();
	}

	songlen = 2 * st.readByte();
	undef = 2 * st.readByte();

	st.read(songpos, 128);
	st.read(sig, 4);
	if (memcmp(sig, "M.K.", 4)) {
		warning("Expected 'M.K.' in protracker module");
		return false;
	}

	int maxpattern = 0;
	for (int i = 0; i < 128; ++i)
		if (maxpattern < songpos[i])
			maxpattern = songpos[i];

	pattern = new pattern_t[maxpattern + 1];

	for (int i = 0; i <= maxpattern; ++i) {
		for (int j = 0; j < 64; ++j) {
			for (int k = 0; k < 4; ++k) {
				uint32 note = st.readUint32BE();
				pattern[i][j][k].sample = (note & 0xf0000000) >> 24 | (note & 0x0000f000) >> 12;
				pattern[i][j][k].period = (note >> 16) & 0xfff;
				pattern[i][j][k].effect = note & 0xfff;
			}
		}
	}

	for (int i = 0; i < 31; ++i) {
		if (!sample[i].len)
			sample[i].data = 0;
		else {
			sample[i].data = new int8[sample[i].len];
			st.read((byte *)sample[i].data, sample[i].len);
		}
	}

	if (!st.eos())
		warning("Expected EOS on module stream");

	return true;
}

Module::Module() {
	pattern = 0;
	for (int i = 0; i < 31; ++i) {
		sample[i].data = 0;
	}
}

Module::~Module() {
	delete[] pattern;
	for (int i = 0; i < 31; ++i) {
		delete[] sample[i].data;
	}
}

} // End of namespace Modules