openMSX
TsxImage.cc
Go to the documentation of this file.
1// Based on code written by:
2// (2017) NataliaPC aka @ishwin74
3// Under GPL License
4
5#include "TsxImage.hh"
6#include "TsxParser.hh"
7#include "File.hh"
8#include "FilePool.hh"
9#include "Filename.hh"
10#include "CliComm.hh"
11#include "Clock.hh"
12#include "MSXException.hh"
13#include "xrange.hh"
14
15namespace openmsx {
16
17TsxImage::TsxImage(const Filename& filename, FilePool& filePool, CliComm& cliComm)
18{
19 File file(filename);
20 try {
21 TsxParser parser(file.mmap());
22
23 // Move the parsed waveform here
24 output = std::move(parser.stealOutput());
25
26 // Translate the TsxReader-filetype to a CassetteImage-filetype
27 if (auto type = parser.getFirstFileType()) {
29 switch (*type) {
33 default: return CassetteImage::UNKNOWN;
34 }
35 }());
36 }
37
38 // Print embedded messages
39 for (const auto& msg : parser.getMessages()) {
40 cliComm.printInfo(msg);
41 }
42 } catch (const std::string& msg) {
43 throw MSXException(filename.getOriginal(), ": ", msg);
44 }
45
46 // Conversion successful, now calculate sha1sum
47 setSha1Sum(filePool.getSha1Sum(file));
48}
49
50int16_t TsxImage::getSampleAt(EmuTime::param time) const
51{
52 static const Clock<TsxParser::OUTPUT_FREQUENCY> zero(EmuTime::zero());
53 unsigned pos = zero.getTicksTill(time);
54 return pos < output.size() ? output[pos] * 256 : 0;
55}
56
57EmuTime TsxImage::getEndTime() const
58{
59 Clock<TsxParser::OUTPUT_FREQUENCY> clk(EmuTime::zero());
60 clk += unsigned(output.size());
61 return clk.getTime();
62}
63
64unsigned TsxImage::getFrequency() const
65{
67}
68
69void TsxImage::fillBuffer(unsigned pos, std::span<float*, 1> bufs, unsigned num) const
70{
71 size_t nbSamples = output.size();
72 if (pos < nbSamples) {
73 for (auto i : xrange(num)) {
74 bufs[0][i] = (pos < nbSamples)
75 ? output[pos]
76 : 0;
77 ++pos;
78 }
79 } else {
80 bufs[0] = nullptr;
81 }
82}
83
85{
86 return 1.0f / 128;
87}
88
89} // namespace openmsx
const std::vector< std::string > & getMessages() const
Definition TsxParser.hh:33
std::optional< FileType > getFirstFileType() const
Definition TsxParser.hh:32
std::vector< int8_t > && stealOutput()
Definition TsxParser.hh:31
static constexpr unsigned OUTPUT_FREQUENCY
Definition TsxParser.hh:21
void setSha1Sum(const Sha1Sum &sha1sum)
void setFirstFileType(FileType type)
void printInfo(std::string_view message)
Definition CliComm.cc:7
Represents a clock with a fixed frequency.
Definition Clock.hh:19
constexpr EmuTime::param getTime() const
Gets the time at which the last clock tick occurred.
Definition Clock.hh:46
constexpr unsigned getTicksTill(EmuTime::param e) const
Calculate the number of ticks for this clock until the given time.
Definition Clock.hh:58
Sha1Sum getSha1Sum(File &file)
Calculate sha1sum for the given File object.
Definition FilePool.cc:58
std::span< const uint8_t > mmap()
Map file in memory.
Definition File.cc:102
This class represents a filename.
Definition Filename.hh:20
const std::string & getOriginal() const
Definition Filename.hh:37
EmuTime getEndTime() const override
Definition TsxImage.cc:57
void fillBuffer(unsigned pos, std::span< float *, 1 > bufs, unsigned num) const override
Definition TsxImage.cc:69
float getAmplificationFactorImpl() const override
Definition TsxImage.cc:84
int16_t getSampleAt(EmuTime::param time) const override
Definition TsxImage.cc:50
TsxImage(const Filename &fileName, FilePool &filePool, CliComm &cliComm)
Definition TsxImage.cc:17
unsigned getFrequency() const override
Definition TsxImage.cc:64
This file implemented 3 utility functions:
Definition Autofire.cc:11
constexpr auto xrange(T e)
Definition xrange.hh:132