Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change: Allow embedding any file into a .cat file. #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
FileReader::FileReader(string filename, bool binary)
{
this->file = fopen(filename.c_str(), binary ? "rb" : "r");
fseek(this->file, 0, SEEK_END);
this->filesize = ftell(this->file);
fseek(this->file, 0, SEEK_SET);
this->filename = filename;

if (this->file == NULL) {
Expand Down
2 changes: 2 additions & 0 deletions src/io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ typedef std::string string;
*/
class FileReader {
FILE *file; ///< The file to be read by this instance
size_t filesize; ///< The size of the file
string filename; ///< The filename of the file

public:
Expand All @@ -46,6 +47,7 @@ class FileReader {
*/
~FileReader();

size_t GetSize() const { return this->filesize; }

/**
* Read a single byte from the stream.
Expand Down
60 changes: 4 additions & 56 deletions src/sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Sample::Sample(string filename, string name) :
sample_data(NULL)
{
FileReader sample_reader(filename);
this->size = sample_reader.GetSize();
this->ReadSample(sample_reader, false);
}

Expand All @@ -105,46 +106,8 @@ void Sample::ReadSample(FileReader &reader, bool check_size)
{
assert(this->sample_data == NULL);

if (reader.ReadDword() != 'FFIR') throw "Unexpected chunk; expected \"RIFF\" in " + reader.GetFilename();

if (check_size) {
if (reader.ReadDword() + 8 != size ) throw "Unexpected RIFF chunk size in " + reader.GetFilename();
} else {
this->size = reader.ReadDword() + 8;
}
if (reader.ReadDword() != 'EVAW') throw "Unexpected format; expected \"WAVE\" in " + reader.GetFilename();
if (reader.ReadDword() != ' tmf') throw "Unexpected format; expected \"fmt \" in " + reader.GetFilename();
if (reader.ReadDword() != 16 ) throw "Unexpected fmt chunk size in " + reader.GetFilename();
if (reader.ReadWord() != 1 ) throw "Unexpected audio format; expected \"PCM\" in " + reader.GetFilename();

this->num_channels = reader.ReadWord();
if (this->num_channels != 1) throw "Unexpected number of audio channels; expected 1 in " + reader.GetFilename();

this->sample_rate = reader.ReadDword();
if (this->sample_rate != 11025 && this->sample_rate != 22050 && this->sample_rate != 44100) throw "Unexpected same rate; expected 11025, 22050 or 44100 in " + reader.GetFilename();

/* Read these and validate them later on.
* Saving them is unnecesary as they can be easily calucated. */
uint32_t byte_rate = reader.ReadDword();
uint16_t block_align = reader.ReadWord();

this->bits_per_sample = reader.ReadWord();
if (this->bits_per_sample != 8 && this->bits_per_sample != 16) throw "Unexpected number of bits per channel; expected 8 or 16 in " + reader.GetFilename();

if (byte_rate != this->sample_rate * this->num_channels * this->bits_per_sample / 8) throw "Unexpected byte rate in " + reader.GetFilename();
if (block_align != this->num_channels * this->bits_per_sample / 8) throw "Unexpected block align in " + reader.GetFilename();

if (reader.ReadDword() != 'atad') throw "Unexpected chunk; expected \"data\" in " + reader.GetFilename();

/* Sometimes the files are padded, which causes them to start at the
* wrong offset further on, so just read whatever amount of data was
* specified in the top RIFF as long as sample size is within those
* boundaries, i.e. within the RIFF. */
this->sample_size = reader.ReadDword();
if (this->sample_size + RIFF_HEADER_SIZE > size) throw "Unexpected data chunk size in " + reader.GetFilename();

this->sample_data = (uint8_t *)malloc(this->size - RIFF_HEADER_SIZE);
reader.ReadRaw(this->sample_data, this->size - RIFF_HEADER_SIZE);
this->sample_data = (uint8_t *)malloc(this->size);
reader.ReadRaw(this->sample_data, this->size);
}

void Sample::ReadCatEntry(FileReader &reader, bool new_format)
Expand Down Expand Up @@ -184,22 +147,7 @@ void Sample::WriteSample(FileWriter &writer) const
{
assert(this->sample_data != NULL);

writer.WriteDword('FFIR');
writer.WriteDword(this->size - 8);
writer.WriteDword('EVAW');

writer.WriteDword(' tmf');
writer.WriteDword(16);
writer.WriteWord(1);
writer.WriteWord(this->num_channels);
writer.WriteDword(this->sample_rate);
writer.WriteDword(this->sample_rate * this->num_channels * this->bits_per_sample / 8);
writer.WriteWord(this->num_channels * this->bits_per_sample / 8);
writer.WriteWord(this->bits_per_sample);

writer.WriteDword('atad');
writer.WriteDword(this->sample_size);
writer.WriteRaw(this->sample_data, this->size - RIFF_HEADER_SIZE);
writer.WriteRaw(this->sample_data, this->size);
Comment on lines -187 to +150
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks the format fix at line 170-172 (original).

Given these variables are not used anymore, they should be removed from sample.hpp.

}

void Sample::WriteCatEntry(FileWriter &writer) const
Expand Down
Loading