IT import: compressed samples!

using code from Schism Tracker (GPLv2)
This commit is contained in:
tildearrow 2024-06-26 20:28:02 -05:00
parent 39e0158afc
commit cac079a1b8
7 changed files with 359 additions and 69 deletions

View file

@ -476,6 +476,8 @@ src/baseutils.cpp
src/fileutils.cpp src/fileutils.cpp
src/utfutils.cpp src/utfutils.cpp
extern/itcompress/compression.c
extern/SAASound/src/SAAAmp.cpp extern/SAASound/src/SAAAmp.cpp
extern/SAASound/src/SAADevice.cpp extern/SAASound/src/SAADevice.cpp
extern/SAASound/src/SAAEnv.cpp extern/SAASound/src/SAAEnv.cpp

244
extern/itcompress/compression.c vendored Normal file
View file

@ -0,0 +1,244 @@
/*
* Schism Tracker - a cross-platform Impulse Tracker clone
* copyright (c) 2003-2005 Storlek <storlek@rigelseven.com>
* copyright (c) 2005-2008 Mrs. Brisby <mrs.brisby@nimh.org>
* copyright (c) 2009 Storlek & Mrs. Brisby
* copyright (c) 2010-2012 Storlek
* URL: http://schismtracker.org/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// slightly modified for Furnace by tildearrow
#include "compression.h"
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
#define CLAMP(x,xMin,xMax) (MIN(MAX((x),(xMin)),(xMax)))
// ------------------------------------------------------------------------------------------------------------
// IT decompression code from itsex.c (Cubic Player) and load_it.cpp (Modplug)
// (I suppose this could be considered a merge between the two.)
static uint32_t it_readbits(int8_t n, uint32_t *bitbuf, uint32_t *bitnum, const uint8_t **ibuf)
{
uint32_t value = 0;
uint32_t i = n;
// this could be better
while (i--) {
if (!*bitnum) {
*bitbuf = *(*ibuf)++;
*bitnum = 8;
}
value >>= 1;
value |= (*bitbuf) << 31;
(*bitbuf) >>= 1;
(*bitnum)--;
}
return value >> (32 - n);
}
uint32_t it_decompress8(void *dest, uint32_t len, const void *file, uint32_t filelen, int it215, int channels)
{
const uint8_t *filebuf; // source buffer containing compressed sample data
const uint8_t *srcbuf; // current position in source buffer
int8_t *destpos; // position in destination buffer which will be returned
uint16_t blklen; // length of compressed data block in samples
uint16_t blkpos; // position in block
uint8_t width; // actual "bit width"
uint16_t value; // value read from file to be processed
int8_t d1, d2; // integrator buffers (d2 for it2.15)
int8_t v; // sample value
uint32_t bitbuf, bitnum; // state for it_readbits
filebuf = srcbuf = (const uint8_t *) file;
destpos = (int8_t *) dest;
// now unpack data till the dest buffer is full
while (len) {
// read a new block of compressed data and reset variables
// block layout: word size, <size> bytes data
if (srcbuf + 2 > filebuf + filelen
|| srcbuf + 2 + (srcbuf[0] | (srcbuf[1] << 8)) > filebuf + filelen) {
// truncated!
return srcbuf - filebuf;
}
srcbuf += 2;
bitbuf = bitnum = 0;
blklen = MIN(0x8000, len);
blkpos = 0;
width = 9; // start with width of 9 bits
d1 = d2 = 0; // reset integrator buffers
// now uncompress the data block
while (blkpos < blklen) {
if (width > 9) {
// illegal width, abort
printf("Illegal bit width %d for 8-bit sample\n", width);
return srcbuf - filebuf;
}
value = it_readbits(width, &bitbuf, &bitnum, &srcbuf);
if (width < 7) {
// method 1 (1-6 bits)
// check for "100..."
if (value == 1 << (width - 1)) {
// yes!
value = it_readbits(3, &bitbuf, &bitnum, &srcbuf) + 1; // read new width
width = (value < width) ? value : value + 1; // and expand it
continue; // ... next value
}
} else if (width < 9) {
// method 2 (7-8 bits)
uint8_t border = (0xFF >> (9 - width)) - 4; // lower border for width chg
if (value > border && value <= (border + 8)) {
value -= border; // convert width to 1-8
width = (value < width) ? value : value + 1; // and expand it
continue; // ... next value
}
} else {
// method 3 (9 bits)
// bit 8 set?
if (value & 0x100) {
width = (value + 1) & 0xff; // new width...
continue; // ... and next value
}
}
// now expand value to signed byte
if (width < 8) {
uint8_t shift = 8 - width;
v = (value << shift);
v >>= shift;
} else {
v = (int8_t) value;
}
// integrate upon the sample values
d1 += v;
d2 += d1;
// .. and store it into the buffer
*destpos = it215 ? d2 : d1;
destpos += channels;
blkpos++;
}
// now subtract block length from total length and go on
len -= blklen;
}
return srcbuf - filebuf;
}
// Mostly the same as above.
uint32_t it_decompress16(void *dest, uint32_t len, const void *file, uint32_t filelen, int it215, int channels)
{
const uint8_t *filebuf; // source buffer containing compressed sample data
const uint8_t *srcbuf; // current position in source buffer
int16_t *destpos; // position in destination buffer which will be returned
uint16_t blklen; // length of compressed data block in samples
uint16_t blkpos; // position in block
uint8_t width; // actual "bit width"
uint32_t value; // value read from file to be processed
int16_t d1, d2; // integrator buffers (d2 for it2.15)
int16_t v; // sample value
uint32_t bitbuf, bitnum; // state for it_readbits
filebuf = srcbuf = (const uint8_t *) file;
destpos = (int16_t *) dest;
// now unpack data till the dest buffer is full
while (len) {
// read a new block of compressed data and reset variables
// block layout: word size, <size> bytes data
if (srcbuf + 2 > filebuf + filelen
|| srcbuf + 2 + (srcbuf[0] | (srcbuf[1] << 8)) > filebuf + filelen) {
// truncated!
return srcbuf - filebuf;
}
srcbuf += 2;
bitbuf = bitnum = 0;
blklen = MIN(0x4000, len); // 0x4000 samples => 0x8000 bytes again
blkpos = 0;
width = 17; // start with width of 17 bits
d1 = d2 = 0; // reset integrator buffers
// now uncompress the data block
while (blkpos < blklen) {
if (width > 17) {
// illegal width, abort
printf("Illegal bit width %d for 16-bit sample\n", width);
return srcbuf - filebuf;
}
value = it_readbits(width, &bitbuf, &bitnum, &srcbuf);
if (width < 7) {
// method 1 (1-6 bits)
// check for "100..."
if (value == (uint32_t) 1 << (width - 1)) {
// yes!
value = it_readbits(4, &bitbuf, &bitnum, &srcbuf) + 1; // read new width
width = (value < width) ? value : value + 1; // and expand it
continue; // ... next value
}
} else if (width < 17) {
// method 2 (7-16 bits)
uint16_t border = (0xFFFF >> (17 - width)) - 8; // lower border for width chg
if (value > border && value <= (uint32_t) (border + 16)) {
value -= border; // convert width to 1-8
width = (value < width) ? value : value + 1; // and expand it
continue; // ... next value
}
} else {
// method 3 (17 bits)
// bit 16 set?
if (value & 0x10000) {
width = (value + 1) & 0xff; // new width...
continue; // ... and next value
}
}
// now expand value to signed word
if (width < 16) {
uint8_t shift = 16 - width;
v = (value << shift);
v >>= shift;
} else {
v = (int16_t) value;
}
// integrate upon the sample values
d1 += v;
d2 += d1;
// .. and store it into the buffer
*destpos = it215 ? d2 : d1;
destpos += channels;
blkpos++;
}
// now subtract block length from total length and go on
len -= blklen;
}
return srcbuf - filebuf;
}

6
extern/itcompress/compression.h vendored Normal file
View file

@ -0,0 +1,6 @@
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
uint32_t it_decompress8(void *dest, uint32_t len, const void *file, uint32_t filelen, int it215, int channels);
uint32_t it_decompress16(void *dest, uint32_t len, const void *file, uint32_t filelen, int it215, int channels);

View file

@ -19,6 +19,10 @@
#include "fileOpsCommon.h" #include "fileOpsCommon.h"
extern "C" {
#include "../../../extern/itcompress/compression.h"
}
static const unsigned char volPortaSlide[10]={ static const unsigned char volPortaSlide[10]={
0, 1, 4, 8, 16, 32, 64, 96, 128, 255 0, 1, 4, 8, 16, 32, 64, 96, 128, 255
}; };
@ -543,14 +547,6 @@ bool DivEngine::loadIT(unsigned char* file, size_t len) {
s->depth=DIV_SAMPLE_DEPTH_8BIT; s->depth=DIV_SAMPLE_DEPTH_8BIT;
} }
if (flags&8) {
logE("sample decompression not implemented!");
lastError="sample decompression not implemented";
delete s;
delete[] file;
return false;
}
s->init((unsigned int)reader.readI()); s->init((unsigned int)reader.readI());
s->loopStart=reader.readI(); s->loopStart=reader.readI();
s->loopEnd=reader.readI(); s->loopEnd=reader.readI();
@ -599,78 +595,89 @@ bool DivEngine::loadIT(unsigned char* file, size_t len) {
logD("seek not needed..."); logD("seek not needed...");
} }
if (s->depth==DIV_SAMPLE_DEPTH_16BIT) { if (flags&8) { // compressed sample
if (flags&4) { // downmix stereo unsigned int ret=0;
for (unsigned int i=0; i<s->samples; i++) { logV("decompression begin...");
short l; if (s->depth==DIV_SAMPLE_DEPTH_16BIT) {
if (convert&2) { ret=it_decompress16(s->data16,s->length16,&file[reader.tell()],len-reader.tell(),(convert&4)?1:0,(flags&4)?2:1);
l=reader.readS_BE();
} else {
l=reader.readS();
}
if (!(convert&1)) {
l^=0x8000;
}
s->data16[i]=l;
}
for (unsigned int i=0; i<s->samples; i++) {
short r;
if (convert&2) {
r=reader.readS_BE();
} else {
r=reader.readS();
}
if (!(convert&1)) {
r^=0x8000;
}
s->data16[i]=(s->data16[i]+r)>>1;
}
} else { } else {
for (unsigned int i=0; i<s->samples; i++) { ret=it_decompress8(s->data8,s->length8,&file[reader.tell()],len-reader.tell(),(convert&4)?1:0,(flags&4)?2:1);
if (convert&2) {
s->data16[i]=reader.readS_BE()^((convert&1)?0:0x8000);
} else {
s->data16[i]=reader.readS()^((convert&1)?0:0x8000);
}
}
} }
logV("got: %d",ret);
} else { } else {
if (flags&4) { // downmix stereo if (s->depth==DIV_SAMPLE_DEPTH_16BIT) {
for (unsigned int i=0; i<s->samples; i++) { if (flags&4) { // downmix stereo
signed char l=reader.readC(); for (unsigned int i=0; i<s->samples; i++) {
if (!(convert&1)) { short l;
l^=0x80; if (convert&2) {
l=reader.readS_BE();
} else {
l=reader.readS();
}
if (!(convert&1)) {
l^=0x8000;
}
s->data16[i]=l;
} }
s->data8[i]=l; for (unsigned int i=0; i<s->samples; i++) {
} short r;
for (unsigned int i=0; i<s->samples; i++) { if (convert&2) {
signed char r=reader.readC(); r=reader.readS_BE();
if (!(convert&1)) { } else {
r^=0x80; r=reader.readS();
}
if (!(convert&1)) {
r^=0x8000;
}
s->data16[i]=(s->data16[i]+r)>>1;
}
} else {
for (unsigned int i=0; i<s->samples; i++) {
if (convert&2) {
s->data16[i]=reader.readS_BE()^((convert&1)?0:0x8000);
} else {
s->data16[i]=reader.readS()^((convert&1)?0:0x8000);
}
} }
s->data8[i]=(s->data8[i]+r)>>1;
} }
} else { } else {
for (unsigned int i=0; i<s->samples; i++) { if (flags&4) { // downmix stereo
s->data8[i]=reader.readC()^((convert&1)?0:0x80); for (unsigned int i=0; i<s->samples; i++) {
signed char l=reader.readC();
if (!(convert&1)) {
l^=0x80;
}
s->data8[i]=l;
}
for (unsigned int i=0; i<s->samples; i++) {
signed char r=reader.readC();
if (!(convert&1)) {
r^=0x80;
}
s->data8[i]=(s->data8[i]+r)>>1;
}
} else {
for (unsigned int i=0; i<s->samples; i++) {
s->data8[i]=reader.readC()^((convert&1)?0:0x80);
}
} }
} }
} }
// scale sample if necessary // scale sample if necessary
if (sampleVol>64) sampleVol=64; if (s->samples>0) {
if (sampleVol<64) { if (sampleVol>64) sampleVol=64;
// convert to 16-bit if (sampleVol<64) {
/* // convert to 16-bit
if (s->depth==DIV_SAMPLE_DEPTH_8BIT) { if (s->depth==DIV_SAMPLE_DEPTH_8BIT) {
s->convert(DIV_SAMPLE_DEPTH_16BIT,0); s->convert(DIV_SAMPLE_DEPTH_16BIT,0);
} }
// then scale // then scale
for (unsigned int i=0; i<s->samples; i++) { for (unsigned int i=0; i<s->samples; i++) {
s->data16[i]=(s->data16[i]*sampleVol)>>6; s->data16[i]=(s->data16[i]*sampleVol)>>6;
}
} }
*/
} }
// does the song not use instruments? // does the song not use instruments?
@ -1162,6 +1169,31 @@ bool DivEngine::loadIT(unsigned char* file, size_t len) {
// find subsongs // find subsongs
ds.findSubSongs(maxChan); ds.findSubSongs(maxChan);
// populate subsongs with default panning values
for (size_t i=0; i<ds.subsong.size(); i++) {
for (int j=0; j<maxChan; j++) {
DivPattern* p=ds.subsong[i]->pat[j].getPattern(ds.subsong[i]->orders.ord[j][0],true);
for (int k=0; k<DIV_MAX_EFFECTS; k++) {
if (p->data[0][4+(k<<1)]==0x80) {
// give up if there's a panning effect already
break;
}
if (p->data[0][4+(k<<1)]==-1) {
if ((chanPan[j]&127)==100) {
// should be surround...
p->data[0][4+(k<<1)]=0x80;
p->data[0][5+(k<<1)]=0x80;
} else {
p->data[0][4+(k<<1)]=0x80;
p->data[0][5+(k<<1)]=CLAMP((chanPan[j]&127)<<2,0,255);
}
if (ds.subsong[i]->pat[j].effectCols<=k) ds.subsong[i]->pat[j].effectCols=k+1;
break;
}
}
}
}
if (active) quitDispatch(); if (active) quitDispatch();
BUSY_BEGIN_SOFT; BUSY_BEGIN_SOFT;
saveLock.lock(); saveLock.lock();

View file

@ -1017,12 +1017,19 @@ bool DivEngine::loadS3M(unsigned char* file, size_t len) {
memcpy(ds.subsong[i]->chanShowChanOsc,ds.subsong[0]->chanShowChanOsc,DIV_MAX_CHANS*sizeof(bool)); memcpy(ds.subsong[i]->chanShowChanOsc,ds.subsong[0]->chanShowChanOsc,DIV_MAX_CHANS*sizeof(bool));
} }
// find subsongs
ds.findSubSongs(DIV_MAX_CHANS);
// populate subsongs with default panning values // populate subsongs with default panning values
if (masterVol&128) { // only in stereo mode if (masterVol&128) { // only in stereo mode
for (size_t i=0; i<ds.subsong.size(); i++) { for (size_t i=0; i<ds.subsong.size(); i++) {
for (int j=0; j<16; j++) { for (int j=0; j<16; j++) {
DivPattern* p=ds.subsong[i]->pat[chanMap[j]].getPattern(ds.subsong[i]->orders.ord[j][0],true); DivPattern* p=ds.subsong[i]->pat[chanMap[j]].getPattern(ds.subsong[i]->orders.ord[j][0],true);
for (int k=0; k<DIV_MAX_EFFECTS; k++) { for (int k=0; k<DIV_MAX_EFFECTS; k++) {
if (p->data[0][4+(k<<1)]==0x80) {
// give up if there's a panning effect already
break;
}
if (p->data[0][4+(k<<1)]==-1) { if (p->data[0][4+(k<<1)]==-1) {
p->data[0][4+(k<<1)]=0x80; p->data[0][4+(k<<1)]=0x80;
if (chanPan[j]&16) { if (chanPan[j]&16) {
@ -1038,9 +1045,6 @@ bool DivEngine::loadS3M(unsigned char* file, size_t len) {
} }
} }
// find subsongs
ds.findSubSongs(DIV_MAX_CHANS);
if (active) quitDispatch(); if (active) quitDispatch();
BUSY_BEGIN_SOFT; BUSY_BEGIN_SOFT;
saveLock.lock(); saveLock.lock();

View file

@ -286,6 +286,7 @@ const char* aboutLine[]={
_N("ported by laoo to C++"), _N("ported by laoo to C++"),
_N("vgsound_emu (second version, modified version) by cam900"), _N("vgsound_emu (second version, modified version) by cam900"),
_N("Impulse Tracker GUS volume table by Jeffrey Lim"), _N("Impulse Tracker GUS volume table by Jeffrey Lim"),
_N("Schism Tracker IT sample decompression"),
_N("SM8521 emulator (modified version) by cam900"), _N("SM8521 emulator (modified version) by cam900"),
_N("D65010G031 emulator (modified version) by cam900"), _N("D65010G031 emulator (modified version) by cam900"),
_N("Namco C140/C219 emulator (modified version) by cam900"), _N("Namco C140/C219 emulator (modified version) by cam900"),

View file

@ -322,6 +322,7 @@ TAParamResult pVersion(String) {
printf("- Stella by Stella Team (GPLv2)\n"); printf("- Stella by Stella Team (GPLv2)\n");
printf("- vgsound_emu (second version, modified version) by cam900 (zlib license)\n"); printf("- vgsound_emu (second version, modified version) by cam900 (zlib license)\n");
printf("- Impulse Tracker GUS volume table by Jeffrey Lim (BSD 3-clause)\n"); printf("- Impulse Tracker GUS volume table by Jeffrey Lim (BSD 3-clause)\n");
printf("- Schism Tracker IT sample decompression (GPLv2)\n");
printf("- MAME GA20 core by Acho A. Tang, R. Belmont, Valley Bell (BSD 3-clause)\n"); printf("- MAME GA20 core by Acho A. Tang, R. Belmont, Valley Bell (BSD 3-clause)\n");
printf("- Atari800 mzpokeysnd POKEY emulator by Michael Borisov (GPLv2)\n"); printf("- Atari800 mzpokeysnd POKEY emulator by Michael Borisov (GPLv2)\n");
printf("- ASAP POKEY emulator by Piotr Fusik ported to C++ by laoo (GPLv2)\n"); printf("- ASAP POKEY emulator by Piotr Fusik ported to C++ by laoo (GPLv2)\n");