File: Load_pt36.cpp

package info (click to toggle)
libopenmpt 0.8.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,844 kB
  • sloc: cpp: 129,441; sh: 4,695; ansic: 1,107; makefile: 712
file content (223 lines) | stat: -rw-r--r-- 5,534 bytes parent folder | download
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * Load_pt36.cpp
 * -------------
 * Purpose: ProTracker 3.6 wrapper format loader
 * Notes  : (currently none)
 * Authors: OpenMPT Devs
 * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
 */


#include "stdafx.h"
#include "Loaders.h"

OPENMPT_NAMESPACE_BEGIN

struct PT36IffChunk
{
	// IFF chunk names
	enum ChunkIdentifiers
	{
		idVERS = MagicBE("VERS"),
		idINFO = MagicBE("INFO"),
		idCMNT = MagicBE("CMNT"),
		idPTDT = MagicBE("PTDT"),
	};

	uint32be signature;  // IFF chunk name
	uint32be chunksize;  // chunk size without header
};

MPT_BINARY_STRUCT(PT36IffChunk, 8)

struct PT36InfoChunk
{
	char     name[32];
	uint16be numSamples;
	uint16be numOrders;
	uint16be numPatterns;
	uint16be volume;
	uint16be tempo;
	uint16be flags;
	uint16be dateDay;
	uint16be dateMonth;
	uint16be dateYear;
	uint16be dateHour;
	uint16be dateMinute;
	uint16be dateSecond;
	uint16be playtimeHour;
	uint16be playtimeMinute;
	uint16be playtimeSecond;
	uint16be playtimeMsecond;
};

MPT_BINARY_STRUCT(PT36InfoChunk, 64)


struct PT36Header
{
	char     magicFORM[4]; // "FORM"
	uint32be size;
	char     magicMODL[4]; // "MODL"

	bool IsValid() const
	{
		if(std::memcmp(magicFORM, "FORM", 4))
			return false;
		if(std::memcmp(magicMODL, "MODL", 4))
			return false;
		return true;
	}
};

MPT_BINARY_STRUCT(PT36Header, 12)


CSoundFile::ProbeResult CSoundFile::ProbeFileHeaderPT36(MemoryFileReader file, const uint64 *pfilesize)
{
	PT36Header fileHeader;
	if(!file.ReadStruct(fileHeader))
		return ProbeWantMoreData;
	if(!fileHeader.IsValid())
		return ProbeFailure;
	MPT_UNREFERENCED_PARAMETER(pfilesize);
	return ProbeSuccess;
}


// ProTracker 3.6 version of the MOD format
// Basically just a normal ProTracker mod with different magic, wrapped in an IFF file.
// The "PTDT" chunk is passed to the normal MOD loader.
bool CSoundFile::ReadPT36(FileReader &file, ModLoadingFlags loadFlags)
{
	file.Rewind();

	PT36Header fileHeader;
	if(!file.ReadStruct(fileHeader))
		return false;
	if(!fileHeader.IsValid())
		return false;

	bool ok = false, infoOk = false;
	FileReader commentChunk;
	mpt::ustring version;
	PT36InfoChunk info;
	MemsetZero(info);

	// Go through IFF chunks...
	PT36IffChunk iffHead;
	if(!file.ReadStruct(iffHead))
	{
		return false;
	}
	// First chunk includes "MODL" magic in size
	iffHead.chunksize -= 4;

	do
	{
		// All chunk sizes include chunk header
		iffHead.chunksize -= 8;
		if(loadFlags == onlyVerifyHeader && iffHead.signature == PT36IffChunk::idPTDT)
		{
			return true;
		}

		const auto startPos = file.GetPosition();
		FileReader chunk = file.ReadChunk(iffHead.chunksize);
		if(!chunk.IsValid())
		{
			break;
		}

		switch(iffHead.signature)
		{
		case PT36IffChunk::idVERS:
			chunk.Skip(4);
			if(chunk.ReadMagic("PT") && iffHead.chunksize > 6)
			{
				chunk.ReadString<mpt::String::maybeNullTerminated>(version, mpt::Charset::Amiga_no_C1, iffHead.chunksize - 6);
			}
			break;

		case PT36IffChunk::idINFO:
			infoOk = chunk.ReadStruct(info);
			break;

		case PT36IffChunk::idCMNT:
			commentChunk = chunk;
			break;

		case PT36IffChunk::idPTDT:
			// Some (all?) PT36 files with samples larger than 64k report incorrect chunk sizes (off by 65536 for each large sample).
			// In those broken files, the PTDT chunk is the last chunk in the file anway,
			// so we just pass the entire chunk plus the maximum possible extra size to the MOD loader.
			// Example: megadream5 by Thunder
			{
				FileReader moduleChunk = file.GetChunkAt(startPos, iffHead.chunksize + 65536 * 31);
				ok = ReadMOD(moduleChunk, loadFlags);
			}
			break;
		}
	} while(file.ReadStruct(iffHead));

	if(version.empty())
	{
		version = UL_("3.6");
	}

	// both an info chunk and a module are required
	if(ok && infoOk)
	{
		bool vblank = (info.flags & 0x100) == 0;
		m_playBehaviour.set(kMODVBlankTiming, vblank);
		if(info.volume != 0)
			m_nSamplePreAmp = std::min(uint16(64), static_cast<uint16>(info.volume));
		if(info.tempo != 0 && !vblank)
			Order().SetDefaultTempoInt(info.tempo);

		if(info.name[0])
			m_songName = mpt::String::ReadBuf(mpt::String::maybeNullTerminated, info.name);

		if(mpt::is_in_range(info.dateMonth, 1, 12) && mpt::is_in_range(info.dateDay, 1, 31) && mpt::is_in_range(info.dateHour, 0, 23)
		   && mpt::is_in_range(info.dateMinute, 0, 59) && mpt::is_in_range(info.dateSecond, 0, 59))
		{
#ifdef MODPLUG_TRACKER
			m_modFormat.timezone = mpt::Date::LogicalTimezone::Local;
#else
			m_modFormat.timezone = mpt::Date::LogicalTimezone::Unspecified;
#endif
			FileHistory mptHistory;
			mptHistory.loadDate.year = info.dateYear + 1900;
			mptHistory.loadDate.month = info.dateMonth;
			mptHistory.loadDate.day = info.dateDay;
			mptHistory.loadDate.hours = info.dateHour;
			mptHistory.loadDate.minutes = info.dateMinute;
			mptHistory.loadDate.seconds = info.dateSecond;
			m_FileHistory.push_back(mptHistory);
		}
	}
	if(ok)
	{
		if(commentChunk.IsValid())
		{
			std::string author;
			commentChunk.ReadString<mpt::String::maybeNullTerminated>(author, 32);
			if(author != "UNNAMED AUTHOR")
				m_songArtist = mpt::ToUnicode(mpt::Charset::Amiga_no_C1, author);
			if(!commentChunk.NoBytesLeft())
			{
				m_songMessage.ReadFixedLineLength(commentChunk, commentChunk.BytesLeft(), 40, 0);
			}
		}

		m_modFormat.madeWithTracker = UL_("ProTracker ") + version;
	}
	m_SongFlags.set(SONG_PT_MODE);
	m_playBehaviour.set(kMODIgnorePanning);
	m_playBehaviour.set(kMODOneShotLoops);
	m_playBehaviour.reset(kMODSampleSwap);

	return ok;
}

OPENMPT_NAMESPACE_END