Skip to content

Commit

Permalink
libmodplug: Reworked most of the C++ code to straight C.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Jul 19, 2018
1 parent 648210b commit c8b7e6e
Show file tree
Hide file tree
Showing 33 changed files with 1,849 additions and 2,649 deletions.
2 changes: 2 additions & 0 deletions src/SDL_sound_modplug.c
Expand Up @@ -59,6 +59,8 @@ static const char *extensions_modplug[] =
"UMX",
"XM", /* FastTracker II */
"ABC",
"MID",
"MIDI",
NULL
};

Expand Down
228 changes: 106 additions & 122 deletions src/libmodplug/fastmix.cpp

Large diffs are not rendered by default.

487 changes: 141 additions & 346 deletions src/libmodplug/libmodplug.h

Large diffs are not rendered by default.

72 changes: 33 additions & 39 deletions src/libmodplug/load_669.cpp
Expand Up @@ -44,7 +44,7 @@ DWORD lengthArrayToDWORD(const BYTE length[4]) {
}


BOOL CSoundFile::Read669(const BYTE *lpStream, DWORD dwMemLength)
BOOL CSoundFile_Read669(CSoundFile *_this, const BYTE *lpStream, DWORD dwMemLength)
//---------------------------------------------------------------
{
BOOL b669Ext;
Expand All @@ -57,25 +57,24 @@ BOOL CSoundFile::Read669(const BYTE *lpStream, DWORD dwMemLength)
b669Ext = (bswapLE16(pfh->sig) == 0x4E4A) ? TRUE : FALSE;
if ((!pfh->samples) || (pfh->samples > 64) || (pfh->restartpos >= 128)
|| (!pfh->patterns) || (pfh->patterns > 128)) return FALSE;
DWORD dontfuckwithme = 0x1F1 + pfh->samples * sizeof(SAMPLE669) + pfh->patterns * 0x600;
if (dontfuckwithme > dwMemLength) return FALSE;
DWORD donttouchme = 0x1F1 + pfh->samples * sizeof(SAMPLE669) + pfh->patterns * 0x600;
if (donttouchme > dwMemLength) return FALSE;
for (UINT ichk=0; ichk<pfh->samples; ichk++)
{
DWORD len = lengthArrayToDWORD(psmp[ichk].length);
dontfuckwithme += len;
donttouchme += len;
}
if (dontfuckwithme > dwMemLength) return FALSE;
if (donttouchme > dwMemLength) return FALSE;
// That should be enough checking: this must be a 669 module.
m_nType = MOD_TYPE_669;
m_dwSongFlags |= SONG_LINEARSLIDES;
m_nMinPeriod = 28 << 2;
m_nMaxPeriod = 1712 << 3;
m_nDefaultTempo = 125;
m_nDefaultSpeed = 6;
m_nChannels = 8;
SDL_memcpy(m_szNames[0], pfh->songmessage, 16);
m_nSamples = pfh->samples;
for (UINT nins=1; nins<=m_nSamples; nins++, psmp++)
_this->m_nType = MOD_TYPE_669;
_this->m_dwSongFlags |= SONG_LINEARSLIDES;
_this->m_nMinPeriod = 28 << 2;
_this->m_nMaxPeriod = 1712 << 3;
_this->m_nDefaultTempo = 125;
_this->m_nDefaultSpeed = 6;
_this->m_nChannels = 8;
_this->m_nSamples = pfh->samples;
for (UINT nins=1; nins<=_this->m_nSamples; nins++, psmp++)
{
DWORD len = lengthArrayToDWORD(psmp->length);
DWORD loopstart = lengthArrayToDWORD(psmp->loopstart);
Expand All @@ -84,37 +83,32 @@ BOOL CSoundFile::Read669(const BYTE *lpStream, DWORD dwMemLength)
if ((loopend > len) && (!loopstart)) loopend = 0;
if (loopend > len) loopend = len;
if (loopstart + 4 >= loopend) loopstart = loopend = 0;
Ins[nins].nLength = len;
Ins[nins].nLoopStart = loopstart;
Ins[nins].nLoopEnd = loopend;
if (loopend) Ins[nins].uFlags |= CHN_LOOP;
SDL_memcpy(m_szNames[nins], psmp->filename, 13);
Ins[nins].nVolume = 256;
Ins[nins].nGlobalVol = 64;
Ins[nins].nPan = 128;
_this->Ins[nins].nLength = len;
_this->Ins[nins].nLoopStart = loopstart;
_this->Ins[nins].nLoopEnd = loopend;
if (loopend) _this->Ins[nins].uFlags |= CHN_LOOP;
_this->Ins[nins].nVolume = 256;
_this->Ins[nins].nGlobalVol = 64;
_this->Ins[nins].nPan = 128;
}
// Song Message
m_lpszSongComments = new char[109];
SDL_memcpy(m_lpszSongComments, pfh->songmessage, 108);
m_lpszSongComments[108] = 0;
// Reading Orders
SDL_memcpy(Order, pfh->orders, 128);
m_nRestartPos = pfh->restartpos;
if (Order[m_nRestartPos] >= pfh->patterns) m_nRestartPos = 0;
SDL_memcpy(_this->Order, pfh->orders, 128);
_this->m_nRestartPos = pfh->restartpos;
if (_this->Order[_this->m_nRestartPos] >= pfh->patterns) _this->m_nRestartPos = 0;
// Reading Pattern Break Locations
for (UINT npan=0; npan<8; npan++)
{
ChnSettings[npan].nPan = (npan & 1) ? 0x30 : 0xD0;
ChnSettings[npan].nVolume = 64;
_this->ChnSettings[npan].nPan = (npan & 1) ? 0x30 : 0xD0;
_this->ChnSettings[npan].nVolume = 64;
}
// Reading Patterns
dwMemPos = 0x1F1 + pfh->samples * 25;
for (UINT npat=0; npat<pfh->patterns; npat++)
{
Patterns[npat] = AllocatePattern(64, m_nChannels);
if (!Patterns[npat]) break;
PatternSize[npat] = 64;
MODCOMMAND *m = Patterns[npat];
_this->Patterns[npat] = CSoundFile_AllocatePattern(64, _this->m_nChannels);
if (!_this->Patterns[npat]) break;
_this->PatternSize[npat] = 64;
MODCOMMAND *m = _this->Patterns[npat];
const BYTE *p = lpStream + dwMemPos;
for (UINT row=0; row<64; row++)
{
Expand Down Expand Up @@ -181,11 +175,11 @@ BOOL CSoundFile::Read669(const BYTE *lpStream, DWORD dwMemLength)
dwMemPos += 0x600;
}
// Reading Samples
for (UINT n=1; n<=m_nSamples; n++)
for (UINT n=1; n<=_this->m_nSamples; n++)
{
UINT len = Ins[n].nLength;
UINT len = _this->Ins[n].nLength;
if (dwMemPos >= dwMemLength) break;
if (len > 4) ReadSample(&Ins[n], RS_PCM8U, (LPSTR)(lpStream+dwMemPos), dwMemLength - dwMemPos);
if (len > 4) CSoundFile_ReadSample(_this, &_this->Ins[n], RS_PCM8U, (LPSTR)(lpStream+dwMemPos), dwMemLength - dwMemPos);
dwMemPos += len;
}
return TRUE;
Expand Down
75 changes: 26 additions & 49 deletions src/libmodplug/load_abc.cpp
Expand Up @@ -2185,7 +2185,7 @@ static int abc_parse_decorations(ABCHANDLE *h, ABCTRACK *tp, const char *p)
}

// =====================================================================================
BOOL CSoundFile::TestABC(const BYTE *lpStream, DWORD dwMemLength)
BOOL CSoundFile_TestABC(const BYTE *lpStream, DWORD dwMemLength)
// =====================================================================================
{
char id[128];
Expand Down Expand Up @@ -2356,7 +2356,7 @@ static int ABC_ReadPatterns(MODCOMMAND *pattern[], WORD psize[], ABCHANDLE *h, i
for( t = h->track; t; t = t->next ) t->capostart = t->head;
trillbits = 0; // trill effect admininstration: one bit per channel, max 32 channnels
for( pat = 0; pat < numpat; pat++ ) {
pattern[pat] = CSoundFile::AllocatePattern(64, channels);
pattern[pat] = CSoundFile_AllocatePattern(64, channels);
if( !pattern[pat] ) return 0;
psize[pat] = 64;
for( row = 0; row < 64; row++ ) {
Expand Down Expand Up @@ -3430,7 +3430,7 @@ static char *abc_continuated(ABCHANDLE *h, MMFILE *mmf, char *p) {
}

// =====================================================================================
BOOL CSoundFile::ReadABC(const uint8_t *lpStream, DWORD dwMemLength)
BOOL CSoundFile_ReadABC(CSoundFile *_this, const uint8_t *lpStream, DWORD dwMemLength)
{
static int avoid_reentry = 0;
ABCHANDLE *h;
Expand Down Expand Up @@ -3458,7 +3458,7 @@ BOOL CSoundFile::ReadABC(const uint8_t *lpStream, DWORD dwMemLength)
ABCMACRO *mp;
int mmsp;
MMFILE *mmstack[MAXABCINCLUDES];
if( !TestABC(lpStream, dwMemLength) ) return FALSE;
if( !CSoundFile_TestABC(lpStream, dwMemLength) ) return FALSE;
h = ABC_Init();
if( !h ) return FALSE;
mmfile = &mm;
Expand All @@ -3469,7 +3469,7 @@ BOOL CSoundFile::ReadABC(const uint8_t *lpStream, DWORD dwMemLength)
avoid_reentry = 1;
pat_resetsmp();
pat_init_patnames();
m_nDefaultTempo = 0;
_this->m_nDefaultTempo = 0;
global_voiceno = 0;
abckey = 0;
h->tracktime = 0;
Expand Down Expand Up @@ -3566,7 +3566,6 @@ BOOL CSoundFile::ReadABC(const uint8_t *lpStream, DWORD dwMemLength)
case INBETWEEN:
if( !SDL_strncmp(p,"X:",2) ) {
abcstate = INHEAD;
SDL_memset(m_szNames[0], 0, 32);
for( p+=2; SDL_isspace(*p); p++ ) ;
abcxnumber = SDL_atoi(p);
abchornpipe = 0;
Expand Down Expand Up @@ -3630,21 +3629,10 @@ BOOL CSoundFile::ReadABC(const uint8_t *lpStream, DWORD dwMemLength)
abc_add_partbreak(h, h->track, h->tracktime);
abc_add_tempo_event(h, h->track, h->tracktime, abctempo);
}
if( m_nDefaultTempo == 0 ) m_nDefaultTempo = abctempo;
if( _this->m_nDefaultTempo == 0 ) _this->m_nDefaultTempo = abctempo;
break;
}
if( !SDL_strncmp(p,"T:",2) ) {
char buf[200];
if( SDL_strchr(p,'%') ) *SDL_strchr(p,'%') = '\0';
for( t=SDL_strlen(p)-1; SDL_isspace(p[t]); t-- )
p[t]='\0';
for( t=2; SDL_isspace(p[t]); t++ ) ;
SDL_strlcpy(buf,m_szNames[0], sizeof (buf));
if( SDL_strlen(buf) + SDL_strlen(p+t) > 199 ) p[t+199-SDL_strlen(buf)] = '\0'; // chop it off
if( buf[0] ) SDL_strlcat(buf," ", sizeof (buf)); // add a space
SDL_strlcat(buf, p+t, sizeof (buf));
if( SDL_strlen(buf) > 31 ) buf[31] = '\0'; // chop it of
SDL_strlcpy(m_szNames[0], buf, 32);
break;
}
if( !SDL_strncmp(p,"R:",2) ) {
Expand Down Expand Up @@ -3699,7 +3687,7 @@ BOOL CSoundFile::ReadABC(const uint8_t *lpStream, DWORD dwMemLength)
abc_add_partbreak(h, h->track, h->tracktime);
abc_add_tempo_event(h, h->track, h->tracktime, abcrate);
}
if( m_nDefaultTempo == 0 ) m_nDefaultTempo = abcrate;
if( _this->m_nDefaultTempo == 0 ) _this->m_nDefaultTempo = abcrate;
}
abc_init_partpat(partpat);
partpat[26][0] = abc_patno(h, h->tracktime);
Expand Down Expand Up @@ -3781,17 +3769,6 @@ BOOL CSoundFile::ReadABC(const uint8_t *lpStream, DWORD dwMemLength)
*p = '%'; // make me skip the rest of the line....
}
if( !SDL_strncmp(p,"T:",2) ) {
char buf[200];
if( SDL_strchr(p,'%') ) *SDL_strchr(p,'%') = '\0';
for( t=SDL_strlen(p)-1; SDL_isspace(p[t]); t-- )
p[t]='\0';
for( t=2; SDL_isspace(p[t]); t++ ) ;
SDL_strlcpy(buf,m_szNames[0],sizeof (buf));
if( SDL_strlen(buf) + SDL_strlen(p+t) > 198 ) p[t+198-SDL_strlen(buf)] = '\0'; // chop it of
if( SDL_strlen(buf) ) SDL_strlcat(buf," ",sizeof (buf)); // add a space
SDL_strlcat(buf, p+t, sizeof (buf));
if( SDL_strlen(buf) > 31 ) buf[31] = '\0'; // chop it of
SDL_strlcpy(m_szNames[0], buf, 32);
*p = '%'; // make me skip the rest of the line....
}
break;
Expand Down Expand Up @@ -4694,48 +4671,48 @@ BOOL CSoundFile::ReadABC(const uint8_t *lpStream, DWORD dwMemLength)

// set module variables
if( abctempo == 0 ) abctempo = abcrate;
if( m_nDefaultTempo == 0 ) m_nDefaultTempo = abctempo;
m_nType = MOD_TYPE_ABC;
if( _this->m_nDefaultTempo == 0 ) _this->m_nDefaultTempo = abctempo;
_this->m_nType = MOD_TYPE_ABC;
numpat = 1+(modticks(h->tracktime) / h->speed / 64);
if( numpat > MAX_PATTERNS )
numpat = MAX_PATTERNS;
m_nDefaultSpeed = h->speed;
m_nChannels = abc_numtracks(h);
m_dwSongFlags = SONG_LINEARSLIDES;
m_nMinPeriod = 28 << 2;
m_nMaxPeriod = 1712 << 3;
_this->m_nDefaultSpeed = h->speed;
_this->m_nChannels = abc_numtracks(h);
_this->m_dwSongFlags = SONG_LINEARSLIDES;
_this->m_nMinPeriod = 28 << 2;
_this->m_nMaxPeriod = 1712 << 3;
// orderlist
for(t=0; t < (uint32_t)orderlen; t++){
if( t >= MAX_ORDERS )
break;
Order[t] = orderlist[t];
_this->Order[t] = orderlist[t];
}
SDL_free(orderlist); // get rid of orderlist memory
// ==============================
// Load the pattern info now!
if( ABC_ReadPatterns(Patterns, PatternSize, h, numpat, m_nChannels) ) {
if( ABC_ReadPatterns(_this->Patterns, _this->PatternSize, h, numpat, _this->m_nChannels) ) {
// :^( need one more channel to handle the global events ;^b
m_nChannels++;
_this->m_nChannels++;
h->tp = abc_locate_track(h, "", 99);
abc_add_sync(h, h->tp, h->tracktime);
for( t=0; t<numpat; t++ ) {
FreePattern(Patterns[t]);
Patterns[t] = NULL;
CSoundFile_FreePattern(_this->Patterns[t]);
_this->Patterns[t] = NULL;
}
ABC_ReadPatterns(Patterns, PatternSize, h, numpat, m_nChannels);
ABC_ReadPatterns(_this->Patterns, _this->PatternSize, h, numpat, _this->m_nChannels);
}
// load instruments after building the patterns (chan == 10 track handling)
if( !PAT_Load_Instruments(this) ) {
if( !PAT_Load_Instruments(_this) ) {
avoid_reentry = 0;
return FALSE;
}
// ============================================================
// set panning positions
if( m_nChannels > MAX_BASECHANNELS )
m_nChannels = MAX_BASECHANNELS;
for(t=0; t<m_nChannels; t++) {
ChnSettings[t].nPan = 0x30+((t+2)%5)*((0xD0 - 0x30)/5); // 0x30 = std s3m val
ChnSettings[t].nVolume = 64;
if( _this->m_nChannels > MAX_BASECHANNELS )
_this->m_nChannels = MAX_BASECHANNELS;
for(t=0; t<_this->m_nChannels; t++) {
_this->ChnSettings[t].nPan = 0x30+((t+2)%5)*((0xD0 - 0x30)/5); // 0x30 = std s3m val
_this->ChnSettings[t].nVolume = 64;
}
avoid_reentry = 0; // it is safe now, I'm finished
abc_set_parts(&abcparts, 0); // free the parts array
Expand Down

0 comments on commit c8b7e6e

Please sign in to comment.