Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
pikechu committed Oct 28, 2020
1 parent 17a52d2 commit 60455e4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 24 deletions.
68 changes: 52 additions & 16 deletions src/MidiReader.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,47 @@
#include "MidiReader.h"
#include "error_def.h"

void bzero(void* ptr, size_t sz)
{
memset(ptr, 0, sz);
}

// ´óС¶Ëת»»
void EndianSwap(char *pData, int startIndex, int length)
{
int i, start;
start = startIndex;
uint8_t tmp;
for (i = 0; i < length; i += 2)
{
tmp = pData[start + i];
pData[start + i] = pData[start + i + 1];
pData[start + i + 1] = tmp;
}
}

void MidiReader::buff_clean()
{
bzero(static_cast<void*>(buff.get()), file_size);
}

void MidiReader::init()
{
fs.seekg(0, fs.end);
if (file_size < fs.tellg())
{
file_size = fs.tellg();
buff.reset(new char[file_size]);
}
fs.seekg(0);
}

MidiReader::MidiReader()
{
file_size = 0;
current_pos = 0;
fs.clear();
buff.clear();
buff_clean();
};

MidiReader::MidiReader(std::string file_path)
Expand All @@ -17,11 +52,7 @@ MidiReader::MidiReader(std::string file_path)
{
PRINT_ERROR(ERROR_READ_FILE)
}
fs.seekg(0, fs.end);
file_size = fs.tellg();
fs.seekg(0);
buff.reserve(file_size);
buff.clear();
init();
};

MidiReader::~MidiReader()
Expand All @@ -37,11 +68,7 @@ bool MidiReader::open_file(std::string file_path)
PRINT_ERROR(ERROR_READ_FILE)
return false;
}
fs.seekg(0, fs.end);
file_size = fs.tellg();
fs.seekg(0);
buff.reserve(file_size);
buff.clear();
init();
return true;
}

Expand All @@ -52,23 +79,32 @@ bool MidiReader::read(int byte_num)
PRINT_ERROR(ERROR_OVER_RANGE)
return false;
}
buff.clear();
fs.read(buff.data(), byte_num);
buff_clean();
fs.read(buff.get(), byte_num);
current_pos += byte_num;
return true;
}

bool MidiReader::read_header()
{
int header_size = MidiHeader::get_struct_size();

std::cout << sizeof(MidiHeader) << " " << MidiHeader::get_header_size() << std::endl;
int header_size = MidiHeader::get_header_size();
if (!read(header_size))
{
PRINT_ERROR(ERROR_READ_HEADER)
return false;
}
MidiHeader* header = static_cast<MidiHeader*>(static_cast<void*>(buff.data()));
MidiHeader midi_header;
memcpy_s(&midi_header, header_size, buff.get(), header_size);
EndianSwap((char *)&(midi_header.m_magic) + 4, 0, header_size - sizeof(midi_header.m_magic));
// test
std::cout << header->m_magic << std::endl;
std::cout << midi_header.m_magic << "add=" << &midi_header.m_magic << std::endl;
std::cout << midi_header.m_seclen << "add=" << &midi_header.m_seclen << std::endl;
std::cout << midi_header.m_format << "add=" << &midi_header.m_format << std::endl;
std::cout << midi_header.m_ntracks << "add=" << &midi_header.m_ntracks << std::endl;
std::cout << midi_header.m_tickdiv << "add=" << &midi_header.m_tickdiv << std::endl;

return true;
}

22 changes: 14 additions & 8 deletions src/MidiReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
#include <fstream>
#include <string>
#include <iostream>
#include <memory>

enum Format : uint16_t

enum Format : int16_t
{
MIDI_SINGLE = 0,
MIDI_MULTIPLE = 1,
Expand Down Expand Up @@ -40,16 +42,17 @@ enum Type : char
struct MidiHeader
{
char m_magic[4];
uint32_t m_seclen;
int32_t m_seclen;
Format m_format;
short m_ntracks;
short m_tickdiv;
int16_t m_ntracks;
int16_t m_tickdiv;

// 防止内存对齐导致读取错误
static int get_struct_size(){ return sizeof(char) * 4 + sizeof(uint32_t) + sizeof(Format) + sizeof(short) + sizeof(short); }
static int get_header_size(){ return sizeof(char) * 4 + sizeof(uint32_t) + sizeof(Format) + sizeof(short) + sizeof(short); }
};

#if 0
#define TEST
#ifndef TEST
struct DeltaTime
{
uint32_t total = 0;
Expand Down Expand Up @@ -279,11 +282,14 @@ class MidiReader
// 当前读到的位置
int current_pos;
std::fstream fs;
std::vector<char> buff;

std::shared_ptr<char> buff;
MidiHeader midi_header;

bool is_file_opened(){ return fs.is_open(); }
bool read(int byte_num);

void buff_clean();
void init();
};


Expand Down
1 change: 1 addition & 0 deletions src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ void test_read()
reader.read_header();
}


int main()
{
test_read();
Expand Down

0 comments on commit 60455e4

Please sign in to comment.