Improve audio and midi support.

This commit is contained in:
jmsgrogan 2021-05-23 21:02:38 +01:00
parent 9bcc0ae88e
commit 8b5f485d1e
47 changed files with 1446 additions and 634 deletions

View file

@ -1,5 +1,6 @@
#pragma once
#include <cstring>
#include <iostream>
class ByteUtils
{
@ -7,6 +8,11 @@ public:
using Word = int;
using DWord = int;
static bool MSBIsOne(char c)
{
return c & (1 << 7);
}
static int GetWordFirstBit(const Word word)
{
return word & ByteUtils::WORD_FIRST_BIT;
@ -17,22 +23,29 @@ public:
return word & ByteUtils::WORD_LAST_BYTE;
}
static void ReverseBuffer(char* buffer, char* reverse, unsigned size)
static void ReverseBuffer(char* buffer, char* reverse, unsigned size, unsigned targetSize)
{
for(unsigned idx=0; idx<size; idx++)
for(unsigned idx=0; idx<targetSize; idx++)
{
reverse[idx] = buffer[size - 1 -idx];
if (idx < size)
{
reverse[idx] = buffer[size - 1 -idx];
}
else
{
reverse[idx] = 0;
}
}
}
static int ToInt(char* buffer, const unsigned size, bool reverse = true)
{
int result;
int result {0};
if(reverse)
{
std::string reversed;
ReverseBuffer(buffer, reversed.data(), size);
std::memcpy(&result, reversed.data(), sizeof(int));
char reversed[sizeof(int)];
ReverseBuffer(buffer, reversed, size, sizeof(int));
std::memcpy(&result, reversed, sizeof(int));
}
else
{

View file

@ -1,20 +1,20 @@
#include "BinaryFile.h"
#include "ByteUtils.h"
bool BinaryFile::GetNextByteAsInt(std::ifstream& file, int& target)
bool BinaryFile::GetNextByteAsInt(std::ifstream* file, int& target)
{
char c;
file.get(c);
file->get(c);
target = int(c);
return true;
}
bool BinaryFile::GetNextNBytes(std::ifstream& file, char* buffer, unsigned number)
bool BinaryFile::GetNextNBytes(std::ifstream* file, char* buffer, unsigned number)
{
char c;
for(unsigned idx=0; idx<number; idx++)
{
if(file.get(c))
if(file->get(c))
{
buffer[idx] = c;
}
@ -26,17 +26,17 @@ bool BinaryFile::GetNextNBytes(std::ifstream& file, char* buffer, unsigned numbe
return true;
}
bool BinaryFile::GetNextWord(std::ifstream& file, char* buffer)
bool BinaryFile::GetNextWord(std::ifstream* file, char* buffer)
{
return GetNextNBytes(file, buffer, ByteUtils::BYTES_PER_WORD);
}
bool BinaryFile::GetNextDWord(std::ifstream& file, char* buffer)
bool BinaryFile::GetNextDWord(std::ifstream* file, char* buffer)
{
return GetNextNBytes(file, buffer, ByteUtils::BYTES_PER_DWORD);
}
bool BinaryFile::GetNextWord(std::ifstream& file, int& target, bool reverse)
bool BinaryFile::GetNextWord(std::ifstream* file, int& target, bool reverse)
{
char buffer[ByteUtils::BYTES_PER_WORD];
if(!BinaryFile::GetNextWord(file, buffer))
@ -47,7 +47,7 @@ bool BinaryFile::GetNextWord(std::ifstream& file, int& target, bool reverse)
return true;
}
bool BinaryFile::GetNextDWord(std::ifstream& file, int& target)
bool BinaryFile::GetNextDWord(std::ifstream* file, int& target)
{
char buffer[ByteUtils::BYTES_PER_DWORD];
if(!BinaryFile::GetNextDWord(file, buffer))
@ -58,7 +58,7 @@ bool BinaryFile::GetNextDWord(std::ifstream& file, int& target)
return true;
}
bool BinaryFile::CheckNextDWord(std::ifstream& file, const char* target)
bool BinaryFile::CheckNextDWord(std::ifstream* file, const char* target)
{
char buffer[ByteUtils::BYTES_PER_DWORD];
if(!BinaryFile::GetNextDWord(file, buffer))
@ -73,12 +73,12 @@ bool BinaryFile::CheckNextDWord(std::ifstream& file, const char* target)
return true;
}
bool BinaryFile::GetNextString(std::ifstream& file, std::string& target, unsigned numBytes)
bool BinaryFile::GetNextString(std::ifstream* file, std::string& target, unsigned numBytes)
{
char c;
for(unsigned idx=0; idx<numBytes; idx++)
{
if(!file.get(c))
if(!file->get(c))
{
return false;
}

View file

@ -7,19 +7,26 @@ class BinaryFile
{
public:
static bool GetNextByteAsInt(std::ifstream& file, int& target);
template<typename T>
static bool Write(std::ofstream* file, T data)
{
file->write(reinterpret_cast<char*>(&data), sizeof(data));
return true;
}
static bool GetNextNBytes(std::ifstream& file, char* buffer, unsigned numBytes);
static bool GetNextByteAsInt(std::ifstream* file, int& target);
static bool GetNextWord(std::ifstream& file, char* buffer);
static bool GetNextNBytes(std::ifstream* file, char* buffer, unsigned numBytes);
static bool GetNextDWord(std::ifstream& file, char* buffer);
static bool GetNextWord(std::ifstream* file, char* buffer);
static bool GetNextWord(std::ifstream& file, int& target, bool reverse = true);
static bool GetNextDWord(std::ifstream* file, char* buffer);
static bool GetNextDWord(std::ifstream& file, int& target);
static bool GetNextWord(std::ifstream* file, int& target, bool reverse = true);
static bool CheckNextDWord(std::ifstream& file, const char* target);
static bool GetNextDWord(std::ifstream* file, int& target);
static bool GetNextString(std::ifstream& file, std::string& target, unsigned numBytes);
static bool CheckNextDWord(std::ifstream* file, const char* target);
static bool GetNextString(std::ifstream* file, std::string& target, unsigned numBytes);
};

View file

@ -31,17 +31,27 @@ std::ofstream* File::GetOutHandle() const
return mOutHandle.get();
}
void File::Open()
void File::Open(bool asBinary)
{
if(mAccessMode == AccessMode::Read)
{
auto flags = std::ifstream::in;
if (asBinary)
{
flags |= std::ifstream::binary;
}
mInHandle = std::make_unique<std::ifstream>();
mInHandle->open(mFullPath, std::ifstream::in);
mInHandle->open(mFullPath, flags);
}
else
{
auto flags = std::ofstream::out;
if (asBinary)
{
flags |= std::ofstream::binary;
}
mOutHandle = std::make_unique<std::ofstream>();
mOutHandle->open(mFullPath, std::ofstream::out);
mOutHandle->open(mFullPath, flags);
}
}

View file

@ -40,7 +40,7 @@ public:
void SetAccessMode(AccessMode mode);
void Open();
void Open(bool asBinary = false);
void Close();

View file

@ -5,5 +5,6 @@ FileFormat::ExtensionMap FileFormat::mExtensions = []
ExtensionMap ret;
ret[Format::Markdown] = ".md";
ret[Format::Html] = ".html";
ret[Format::Wav] = ".wav";
return ret;
}();