Skip to content

Commit

Permalink
refactor: Input module seek[034685]
Browse files Browse the repository at this point in the history
  • Loading branch information
Greedysky committed Jul 27, 2021
1 parent 4750508 commit fcdbec0
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 74 deletions.
66 changes: 47 additions & 19 deletions libv2m/v2mconv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,45 @@ extern const char * const v2mconv_errors[] =
"V2M file was made with unknown synth version!",
};

static ssbase readfile(const unsigned char* inptr, const int inlen)
static struct _ssbase
{
const uint8_t* patchmap;
const uint8_t* globals;
uint32_t timediv;
uint32_t timediv2;
uint32_t maxtime;
const uint8_t* gptr;
uint32_t gdnum;
struct _basech
{
uint32_t notenum;
const uint8_t* noteptr;
uint32_t pcnum;
const uint8_t* pcptr;
uint32_t pbnum;
const uint8_t* pbptr;
struct _bcctl
{
uint32_t ccnum;
const uint8_t* ccptr;
} ctl[7];
} chan[16];

const uint8_t* mididata;
int midisize;
int patchsize;
int globsize;
int maxp;
const uint8_t* newpatchmap;

const uint8_t* speechdata;
int spsize;
} base;

static void readfile(const unsigned char* inptr, const int inlen)
{
const uint8_t* d = inptr;
ssbase base;
memset(&base, 0, sizeof(base));
memset(&base, 0, sizeof(_ssbase));

base.timediv = (*((uint32_t *)(d)));
base.timediv2 = 10000*base.timediv;
Expand All @@ -29,7 +63,7 @@ static ssbase readfile(const unsigned char* inptr, const int inlen)
d += 10*base.gdnum;
for (int ch = 0; ch < 16; ch++)
{
ssbase::_basech& c = base.chan[ch];
_ssbase::_basech& c = base.chan[ch];
c.notenum = *((uint32_t *)d);
d += 4;
if (c.notenum)
Expand All @@ -46,7 +80,7 @@ static ssbase readfile(const unsigned char* inptr, const int inlen)
d += 5*c.pbnum;
for (int cn = 0; cn < 7; cn++)
{
ssbase::_basech::_bcctl& cc = c.ctl[cn];
_ssbase::_basech::_bcctl& cc = c.ctl[cn];
cc.ccnum = *((uint32_t *)d);
d += 4;
cc.ccptr = d;
Expand All @@ -57,13 +91,13 @@ static ssbase readfile(const unsigned char* inptr, const int inlen)
base.midisize = d - inptr;
base.globsize = *((uint32_t *)d);
if (base.globsize < 0 || base.globsize > 131072)
return base;
return;
d += 4;
base.globals = d;
d += base.globsize;
base.patchsize = *((uint32_t *)d);
if (base.patchsize < 0 || base.patchsize > 1048576)
return base;
return;
d += 4;
base.patchmap = d;
d += base.patchsize;
Expand All @@ -73,18 +107,14 @@ static ssbase readfile(const unsigned char* inptr, const int inlen)
base.spsize = *((uint32_t *)d);
d += 4;
base.speechdata = d;
d += base.spsize;

// small sanity check
if (base.spsize < 0 || base.spsize > 8192)
if (base.spsize < 0 || base.spsize > 8192 || (d - inptr) > inlen)
{
base.spsize = 0;
base.speechdata = 0;
}
else if (base.spsize > inlen - (d - inptr))
{
base.spsize = inlen - (d - inptr);
}
d += base.spsize;
}
else
{
Expand All @@ -94,15 +124,14 @@ static ssbase readfile(const unsigned char* inptr, const int inlen)

printf2("after read: est %d, is %d\n", inlen, d - inptr);
printf2("midisize: %d, globs: %d, patches: %d\n", base.midisize, base.globsize, base.patchsize);
return base;
}

// gives version deltas
int CheckV2MVersion(const unsigned char* inptr, const int inlen, ssbase& base)
int CheckV2MVersion(const unsigned char* inptr, const int inlen)
{
int i;
int patchesused[128];
base = readfile(inptr, inlen);
readfile(inptr, inlen);

if (!base.patchsize)
return -1;
Expand Down Expand Up @@ -227,10 +256,9 @@ int CheckV2MVersion(const unsigned char* inptr, const int inlen, ssbase& base)

void ConvertV2M(const unsigned char* inptr, const int inlen, unsigned char* * outptr, int* outlen)
{
int i, p;
ssbase base;
int i, p;
// check version
int vdelta = CheckV2MVersion(inptr, inlen, base);
int vdelta = CheckV2MVersion(inptr, inlen);
if (!vdelta) // if same, simply clone
{
*outptr = new uint8_t[inlen + 4];
Expand Down
39 changes: 1 addition & 38 deletions libv2m/v2mconv.h
Original file line number Diff line number Diff line change
@@ -1,44 +1,7 @@
#pragma once

#include <stdint.h>

struct ssbase
{
const uint8_t* patchmap;
const uint8_t* globals;
uint32_t timediv;
uint32_t timediv2;
uint32_t maxtime;
const uint8_t* gptr;
uint32_t gdnum;
struct _basech
{
uint32_t notenum;
const uint8_t* noteptr;
uint32_t pcnum;
const uint8_t* pcptr;
uint32_t pbnum;
const uint8_t* pbptr;
struct _bcctl
{
uint32_t ccnum;
const uint8_t* ccptr;
} ctl[7];
} chan[16];

const uint8_t* mididata;
int midisize;
int patchsize;
int globsize;
int maxp;
const uint8_t* newpatchmap;

const uint8_t* speechdata;
int spsize;
};

// gives version deltas
int CheckV2MVersion(const unsigned char* inptr, const int inlen, ssbase& base);
int CheckV2MVersion(const unsigned char* inptr, const int inlen);

// converts V2M to newest version
void ConvertV2M(const unsigned char* inptr, const int inlen, unsigned char* * outptr, int* outlen);
Expand Down
16 changes: 10 additions & 6 deletions libv2m/v2mplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ void UpdateSampleDelta(uint32_t nexttime, uint32_t time, uint32_t usecs, uint32_
}
}

void V2MPlayer::Init(uint32_t a_tickspersec)
{
m_tpc = a_tickspersec;
memset(&m_base, 0, sizeof(V2MBase));
}

bool V2MPlayer::InitBase(const void* a_v2m)
{
const uint8_t* d = (const uint8_t *)a_v2m;
Expand Down Expand Up @@ -149,6 +155,7 @@ void V2MPlayer::Reset()
m_state.beat = 0;
m_state.tick = 0;
m_state.smplrem = 0;
m_state.cursmpl = 0;

if (m_samplerate)
{
Expand Down Expand Up @@ -312,6 +319,7 @@ void V2MPlayer::Play(uint32_t a_time)
m_state.smpldelta = -1;
}

m_state.cursmpl = cursmpl;
m_state.smpldelta -= (destsmpl - cursmpl);
m_fadeval = 1.0f;
m_fadedelta = 0.0f;
Expand Down Expand Up @@ -351,6 +359,7 @@ void V2MPlayer::Render(float* a_buffer, uint32_t a_len, bool a_add)
m_state.smpldelta -= torender;
m_state.cursmpl += torender;
}

if (!m_state.smpldelta)
{
Tick();
Expand Down Expand Up @@ -389,19 +398,14 @@ void V2MPlayer::Render(float* a_buffer, uint32_t a_len, bool a_add)
}
}

bool V2MPlayer::NoEnd()
{
return ((m_base.maxtime * m_base.timediv) > m_state.cursmpl);
}

uint32_t V2MPlayer::Length()
{
return ((m_base.maxtime * m_base.timediv) / m_samplerate + 1);
}

bool V2MPlayer::IsPlaying()
{
return m_base.valid && m_state.state == PlayerState::PLAYING;
return (m_base.valid && m_state.state == PlayerState::PLAYING) && ((m_base.maxtime * m_base.timediv) > m_state.cursmpl);
}


Expand Down
10 changes: 1 addition & 9 deletions libv2m/v2mplayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ class V2MPlayer
public:
// init
// call this instead of a constructor
void Init(uint32_t a_tickspersec = 1000)
{
m_tpc = a_tickspersec; /* m_base.valid=0; */ memset(&m_base, 0, sizeof(V2MBase));
}
void Init(uint32_t a_tickspersec = 1000);

// opens a v2m file for playing
//
Expand Down Expand Up @@ -78,9 +75,6 @@ class V2MPlayer
reinterpret_cast<V2MPlayer *>(a_this)->Render(a_buffer, a_len);
}

// returns if song is currently end
bool NoEnd();

// returns if song length
uint32_t Length();

Expand Down Expand Up @@ -115,9 +109,7 @@ class V2MPlayer
// ------------------------------------------------------------------------------------------------------

private:

// struct defs

// General info from V2M file
struct V2MBase
{
Expand Down
3 changes: 1 addition & 2 deletions v2mhelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ int load_and_convert(unsigned char *module, qint64 size, uint8_t **conv, int *co
v2m_initialized = true;
}

ssbase base;
const int ver = CheckV2MVersion(module, size, base);
const int ver = CheckV2MVersion(module, size);
if(ver < 0)
{
return -1;
Expand Down

0 comments on commit fcdbec0

Please sign in to comment.