OXYGENHUB
◆ main openmsx / src / cassette / TsxImage.cc
2.2 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
e8555c89 Andy McCall 1
// Based on code written by:
2
//   (2017) NataliaPC aka @ishwin74
3
//   Under GPL License
4
5
#include "TsxImage.hh"
6
7
#include "TsxParser.hh"
8
9
#include "CliComm.hh"
10
#include "Clock.hh"
11
#include "File.hh"
12
#include "FilePool.hh"
13
#include "Filename.hh"
14
#include "MSXException.hh"
15
16
#include "xrange.hh"
17
18
namespace openmsx {
19
20
TsxImage::TsxImage(const Filename& filename, FilePool& filePool, CliComm& cliComm)
21
{
22
	File file(filename.getResolved());
23
	try {
24
		TsxParser parser(file.mmap<const uint8_t>());
25
26
		// Move the parsed waveform here
27
		output = std::move(parser.stealOutput());
28
29
		// Translate the TsxReader-filetype to a CassetteImage-filetype
30
		if (auto type = parser.getFirstFileType()) {
31
			setFirstFileType([&] {
32
				switch (*type) {
33
				case TsxParser::FileType::ASCII:  return CassetteImage::FileType::ASCII;
34
				case TsxParser::FileType::BINARY: return CassetteImage::FileType::BINARY;
35
				case TsxParser::FileType::BASIC:  return CassetteImage::FileType::BASIC;
36
				default:                          return CassetteImage::FileType::UNKNOWN;
37
				}
38
			}(), filename);
39
		}
40
41
		// Print embedded messages
42
		for (const auto& msg : parser.getMessages()) {
43
			cliComm.printInfo(msg);
44
		}
45
	} catch (const std::string& msg) {
46
		throw MSXException(filename.getOriginal(), ": ", msg);
47
	}
48
49
	// Conversion successful, now calculate sha1sum
50
	setSha1Sum(filePool.getSha1Sum(file, filename.getResolved()));
51
}
52
53
int16_t TsxImage::getSampleAt(EmuTime time) const
54
{
55
	static const Clock<TsxParser::OUTPUT_FREQUENCY> zero(EmuTime::zero());
56
	unsigned pos = zero.getTicksTill(time);
57
	return pos < output.size() ? output[pos] * 256 : 0;
58
}
59
60
EmuTime TsxImage::getEndTime() const
61
{
62
	Clock<TsxParser::OUTPUT_FREQUENCY> clk(EmuTime::zero());
63
	clk += unsigned(output.size());
64
	return clk.getTime();
65
}
66
67
unsigned TsxImage::getFrequency() const
68
{
69
	return TsxParser::OUTPUT_FREQUENCY;
70
}
71
72
void TsxImage::fillBuffer(unsigned pos, std::span<float*, 1> bufs, unsigned num) const
73
{
74
	size_t nbSamples = output.size();
75
	if (pos < nbSamples) {
76
		for (auto i : xrange(num)) {
77
			bufs[0][i] = (pos < nbSamples)
78
			           ? output[pos]
79
			           : 0;
80
			++pos;
81
		}
82
	} else {
83
		bufs[0] = nullptr;
84
	}
85
}
86
87
float TsxImage::getAmplificationFactorImpl() const
88
{
89
	return 1.0f / 128;
90
}
91
92
} // namespace openmsx