From f9eeaa25cc8653be50d0718e004aae80299587ea Mon Sep 17 00:00:00 2001 From: Leandro Nini Date: Sat, 25 Jul 2026 10:46:00 +0200 Subject: [PATCH 1/6] Refactor --- CMakeLists.txt | 2 ++ src/channels.cpp | 56 ++++++++++++++++++++++++++++++++++++++ src/channels.h | 62 +++++++++++++++++++++++++++++++++++++++++++ src/display.cpp | 1 + src/loadtrk.cpp | 1 + src/play.cpp | 45 +++++++------------------------ src/play.h | 33 ----------------------- src/song.cpp | 1 + src/tools/ltreloc.cpp | 1 + 9 files changed, 133 insertions(+), 69 deletions(-) create mode 100644 src/channels.cpp create mode 100644 src/channels.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 8babc19..4673f1d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,6 +49,7 @@ set(LOADTRK_SOURCES src/sound.cpp src/console.cpp src/colors.cpp + src/channels.cpp src/sid.cpp src/ltlog.cpp src/ltmidi.cpp @@ -100,6 +101,7 @@ set(LTRELOC_SOURCES src/reloc.cpp src/console.cpp src/colors.cpp + src/channels.cpp src/ltlog.cpp src/bme/bme_win.cpp src/bme/bme_io.cpp diff --git a/src/channels.cpp b/src/channels.cpp new file mode 100644 index 0000000..234e2bb --- /dev/null +++ b/src/channels.cpp @@ -0,0 +1,56 @@ +/* + * LoadTracker + * + * 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 + */ + +#include "channels.h" + +#include "configfile.h" + +#include + +Chn chn[MAX_CHN]; +unsigned char funktable[2]; + +void initchannels() +{ + int maxChns = getMaxChannels(); + Chn *cptr = &chn[0]; + + std::memset(chn, 0, sizeof chn); + + for (int c = 0; c < maxChns; c++) + { + chn[c].trans = 0; + chn[c].instr = 1; + if (multiplier) + cptr->tempo = 6*multiplier-1; + else + cptr->tempo = 6-1; + cptr++; + } + + if (multiplier) + { + funktable[0] = 9*multiplier-1; + funktable[1] = 6*multiplier-1; + } + else + { + funktable[0] = 9-1; + funktable[1] = 6-1; + } +} diff --git a/src/channels.h b/src/channels.h new file mode 100644 index 0000000..f0dbdb8 --- /dev/null +++ b/src/channels.h @@ -0,0 +1,62 @@ +/* + * LoadTracker + * + * 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 + */ + +#ifndef CHANNELS_H +#define CHANNELS_H + +#include "common.h" + +struct Chn +{ + unsigned char trans; + unsigned char instr; + unsigned char note; + unsigned char lastnote; + unsigned char newnote; + int pattptr; + unsigned char pattnum; + unsigned char songptr; + unsigned char repeat; + unsigned short freq; + unsigned char gate; + unsigned char wave; + unsigned short pulse; + unsigned char ptr[2]; + unsigned char pulsetime; + unsigned char wavetime; + unsigned char vibtime; + unsigned char vibdelay; + unsigned char command; + unsigned char cmddata; + unsigned char newcommand; + unsigned char newcmddata; + unsigned char tick; + unsigned char tempo; + unsigned char mute; + unsigned char advance; + unsigned char gatetimer; +}; + +#ifndef CHANNELS_C +extern Chn chn[MAX_CHN]; +extern unsigned char funktable[2]; +#endif + +void initchannels(); + +#endif diff --git a/src/display.cpp b/src/display.cpp index 606ec69..cc559bd 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -25,6 +25,7 @@ #include "display.h" #include "common.h" +#include "channels.h" #include "colors.h" #include "configfile.h" #include "console.h" diff --git a/src/loadtrk.cpp b/src/loadtrk.cpp index 9019bab..c35ae47 100644 --- a/src/loadtrk.cpp +++ b/src/loadtrk.cpp @@ -22,6 +22,7 @@ #include "config.h" +#include "channels.h" #include "colors.h" #include "configfile.h" #include "console.h" diff --git a/src/play.cpp b/src/play.cpp index 1dd0f27..52c80cd 100644 --- a/src/play.cpp +++ b/src/play.cpp @@ -25,6 +25,7 @@ #include "play.h" #include "configfile.h" +#include "channels.h" #include "display.h" #include "order.h" #include "pattern.h" @@ -41,7 +42,8 @@ #include #include -unsigned char freqtbllo[] = { +unsigned char freqtbllo[] = +{ 0x17,0x27,0x39,0x4b,0x5f,0x74,0x8a,0xa1,0xba,0xd4,0xf0,0x0e, 0x2d,0x4e,0x71,0x96,0xbe,0xe8,0x14,0x43,0x74,0xa9,0xe1,0x1c, 0x5a,0x9c,0xe2,0x2d,0x7c,0xcf,0x28,0x85,0xe8,0x52,0xc1,0x37, @@ -53,9 +55,11 @@ unsigned char freqtbllo[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 +}; -unsigned char freqtblhi[] = { +unsigned char freqtblhi[] = +{ 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x02, 0x02,0x02,0x02,0x02,0x02,0x02,0x03,0x03,0x03,0x03,0x03,0x04, 0x04,0x04,0x04,0x05,0x05,0x05,0x06,0x06,0x06,0x07,0x07,0x08, @@ -67,9 +71,9 @@ unsigned char freqtblhi[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 +}; -Chn chn[MAX_CHN]; unsigned char filterctrl = 0; unsigned char filtertype = 0; unsigned char filtercutoff = 0; @@ -80,7 +84,6 @@ unsigned char filter2type = 0; unsigned char filter2cutoff = 0; unsigned char filter2time = 0; unsigned char filter2ptr = 0; -unsigned char funktable[2]; unsigned char masterfader = 0x0f; int psnum = 0; int songinit = PLAY_STOPPED; @@ -98,36 +101,6 @@ void sequencer_stereo(int c, Chn *cptr); void resettime(); void incrementtime(); -void initchannels() -{ - int maxChns = getMaxChannels(); - Chn *cptr = &chn[0]; - - std::memset(chn, 0, sizeof chn); - - for (int c = 0; c < maxChns; c++) - { - chn[c].trans = 0; - chn[c].instr = 1; - if (multiplier) - cptr->tempo = 6*multiplier-1; - else - cptr->tempo = 6-1; - cptr++; - } - - if (multiplier) - { - funktable[0] = 9*multiplier-1; - funktable[1] = 6*multiplier-1; - } - else - { - funktable[0] = 9-1; - funktable[1] = 6-1; - } -} - void initsong(int num, int mode) { initsongpos(num, mode, 0); diff --git a/src/play.h b/src/play.h index af486fc..cec7a20 100644 --- a/src/play.h +++ b/src/play.h @@ -31,46 +31,13 @@ enum PLAY_STOPPED = 0x80 }; -struct Chn -{ - unsigned char trans; - unsigned char instr; - unsigned char note; - unsigned char lastnote; - unsigned char newnote; - int pattptr; - unsigned char pattnum; - unsigned char songptr; - unsigned char repeat; - unsigned short freq; - unsigned char gate; - unsigned char wave; - unsigned short pulse; - unsigned char ptr[2]; - unsigned char pulsetime; - unsigned char wavetime; - unsigned char vibtime; - unsigned char vibdelay; - unsigned char command; - unsigned char cmddata; - unsigned char newcommand; - unsigned char newcmddata; - unsigned char tick; - unsigned char tempo; - unsigned char mute; - unsigned char advance; - unsigned char gatetimer; -}; - #ifndef PLAY_C -extern Chn chn[MAX_CHN]; extern unsigned char masterfader; extern unsigned char freqtbllo[]; extern unsigned char freqtblhi[]; extern int lastsonginit; #endif -void initchannels(); void initsong(int num, int playmode); void initsongpos(int num, int playmode, int pattpos); void stopsong(); diff --git a/src/song.cpp b/src/song.cpp index ff31e64..4c39ca6 100644 --- a/src/song.cpp +++ b/src/song.cpp @@ -25,6 +25,7 @@ #include "song.h" #include "common.h" +#include "channels.h" #include "configfile.h" #include "file.h" #include "instr.h" diff --git a/src/tools/ltreloc.cpp b/src/tools/ltreloc.cpp index f811ddd..78c91f7 100644 --- a/src/tools/ltreloc.cpp +++ b/src/tools/ltreloc.cpp @@ -26,6 +26,7 @@ #include "config.h" +#include "channels.h" #include "colors.h" #include "console.h" #include "pattern.h" From 537d90d32ff191a1e5ee60232aa134f68e3af4de Mon Sep 17 00:00:00 2001 From: Leandro Nini Date: Sat, 25 Jul 2026 10:56:45 +0200 Subject: [PATCH 2/6] Refactor --- CMakeLists.txt | 2 ++ src/freqtbl.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++ src/freqtbl.h | 27 ++++++++++++++++++++++++ src/loadtrk.cpp | 9 ++++---- src/pattern.cpp | 1 + src/play.cpp | 33 +---------------------------- src/play.h | 2 -- src/reloc.cpp | 1 + src/tools/ltreloc.cpp | 3 ++- src/tools/ltreloc.h | 2 -- 10 files changed, 88 insertions(+), 41 deletions(-) create mode 100644 src/freqtbl.cpp create mode 100644 src/freqtbl.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 4673f1d..e852690 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,6 +50,7 @@ set(LOADTRK_SOURCES src/console.cpp src/colors.cpp src/channels.cpp + src/freqtbl.cpp src/sid.cpp src/ltlog.cpp src/ltmidi.cpp @@ -102,6 +103,7 @@ set(LTRELOC_SOURCES src/console.cpp src/colors.cpp src/channels.cpp + src/freqtbl.cpp src/ltlog.cpp src/bme/bme_win.cpp src/bme/bme_io.cpp diff --git a/src/freqtbl.cpp b/src/freqtbl.cpp new file mode 100644 index 0000000..f52f1e0 --- /dev/null +++ b/src/freqtbl.cpp @@ -0,0 +1,49 @@ +/* + * LoadTracker + * + * 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 + */ + +unsigned char freqtbllo[] = +{ + 0x17,0x27,0x39,0x4b,0x5f,0x74,0x8a,0xa1,0xba,0xd4,0xf0,0x0e, + 0x2d,0x4e,0x71,0x96,0xbe,0xe8,0x14,0x43,0x74,0xa9,0xe1,0x1c, + 0x5a,0x9c,0xe2,0x2d,0x7c,0xcf,0x28,0x85,0xe8,0x52,0xc1,0x37, + 0xb4,0x39,0xc5,0x5a,0xf7,0x9e,0x4f,0x0a,0xd1,0xa3,0x82,0x6e, + 0x68,0x71,0x8a,0xb3,0xee,0x3c,0x9e,0x15,0xa2,0x46,0x04,0xdc, + 0xd0,0xe2,0x14,0x67,0xdd,0x79,0x3c,0x29,0x44,0x8d,0x08,0xb8, + 0xa1,0xc5,0x28,0xcd,0xba,0xf1,0x78,0x53,0x87,0x1a,0x10,0x71, + 0x42,0x89,0x4f,0x9b,0x74,0xe2,0xf0,0xa6,0x0e,0x33,0x20,0xff, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 +}; + +unsigned char freqtblhi[] = +{ + 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x02, + 0x02,0x02,0x02,0x02,0x02,0x02,0x03,0x03,0x03,0x03,0x03,0x04, + 0x04,0x04,0x04,0x05,0x05,0x05,0x06,0x06,0x06,0x07,0x07,0x08, + 0x08,0x09,0x09,0x0a,0x0a,0x0b,0x0c,0x0d,0x0d,0x0e,0x0f,0x10, + 0x11,0x12,0x13,0x14,0x15,0x17,0x18,0x1a,0x1b,0x1d,0x1f,0x20, + 0x22,0x24,0x27,0x29,0x2b,0x2e,0x31,0x34,0x37,0x3a,0x3e,0x41, + 0x45,0x49,0x4e,0x52,0x57,0x5c,0x62,0x68,0x6e,0x75,0x7c,0x83, + 0x8b,0x93,0x9c,0xa5,0xaf,0xb9,0xc4,0xd0,0xdd,0xea,0xf8,0xff, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 +}; diff --git a/src/freqtbl.h b/src/freqtbl.h new file mode 100644 index 0000000..5ab537e --- /dev/null +++ b/src/freqtbl.h @@ -0,0 +1,27 @@ +/* + * LoadTracker + * + * 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 + */ + +#ifndef FREQTBL_H +#define FREQTBL_H + +#ifndef FREQTBL_C +extern unsigned char freqtbllo[]; +extern unsigned char freqtblhi[]; +#endif + +#endif diff --git a/src/loadtrk.cpp b/src/loadtrk.cpp index c35ae47..3841180 100644 --- a/src/loadtrk.cpp +++ b/src/loadtrk.cpp @@ -27,6 +27,7 @@ #include "configfile.h" #include "console.h" #include "display.h" +#include "freqtbl.h" #include "instr.h" #include "order.h" #include "pattern.h" @@ -1627,11 +1628,11 @@ void nextmultiplier() void calculatefreqtable() { double basefreq = (double)basepitch * (16777216.0 / 985248.0) * std::pow(2.0, 0.25) / 32.0; - double cyclebasefreq = basefreq; - double freq = basefreq; if (tuningcount) { + double cyclebasefreq = basefreq; + double freq = basefreq; int c = 0; while (c < 96) { @@ -1639,7 +1640,7 @@ void calculatefreqtable() { if (c < 96) { - int intfreq = freq + 0.5; + int intfreq = static_cast(freq + 0.5); if (intfreq > 0xffff) intfreq = 0xffff; freqtbllo[c] = intfreq & 0xff; @@ -1657,7 +1658,7 @@ void calculatefreqtable() { double note = c; double freq = basefreq * std::pow(2.0, note/(double)equaldivisionsperoctave); - int intfreq = freq + 0.5; + int intfreq = static_cast(freq + 0.5); if (intfreq > 0xffff) intfreq = 0xffff; freqtbllo[c] = intfreq & 0xff; diff --git a/src/pattern.cpp b/src/pattern.cpp index 51b85be..6188faf 100644 --- a/src/pattern.cpp +++ b/src/pattern.cpp @@ -24,6 +24,7 @@ #include "configfile.h" #include "console.h" +#include "freqtbl.h" #include "instr.h" #include "order.h" #include "pattern.h" diff --git a/src/play.cpp b/src/play.cpp index 52c80cd..6aafac2 100644 --- a/src/play.cpp +++ b/src/play.cpp @@ -27,6 +27,7 @@ #include "configfile.h" #include "channels.h" #include "display.h" +#include "freqtbl.h" #include "order.h" #include "pattern.h" #include "sid.h" @@ -42,38 +43,6 @@ #include #include -unsigned char freqtbllo[] = -{ - 0x17,0x27,0x39,0x4b,0x5f,0x74,0x8a,0xa1,0xba,0xd4,0xf0,0x0e, - 0x2d,0x4e,0x71,0x96,0xbe,0xe8,0x14,0x43,0x74,0xa9,0xe1,0x1c, - 0x5a,0x9c,0xe2,0x2d,0x7c,0xcf,0x28,0x85,0xe8,0x52,0xc1,0x37, - 0xb4,0x39,0xc5,0x5a,0xf7,0x9e,0x4f,0x0a,0xd1,0xa3,0x82,0x6e, - 0x68,0x71,0x8a,0xb3,0xee,0x3c,0x9e,0x15,0xa2,0x46,0x04,0xdc, - 0xd0,0xe2,0x14,0x67,0xdd,0x79,0x3c,0x29,0x44,0x8d,0x08,0xb8, - 0xa1,0xc5,0x28,0xcd,0xba,0xf1,0x78,0x53,0x87,0x1a,0x10,0x71, - 0x42,0x89,0x4f,0x9b,0x74,0xe2,0xf0,0xa6,0x0e,0x33,0x20,0xff, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 -}; - -unsigned char freqtblhi[] = -{ - 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x02, - 0x02,0x02,0x02,0x02,0x02,0x02,0x03,0x03,0x03,0x03,0x03,0x04, - 0x04,0x04,0x04,0x05,0x05,0x05,0x06,0x06,0x06,0x07,0x07,0x08, - 0x08,0x09,0x09,0x0a,0x0a,0x0b,0x0c,0x0d,0x0d,0x0e,0x0f,0x10, - 0x11,0x12,0x13,0x14,0x15,0x17,0x18,0x1a,0x1b,0x1d,0x1f,0x20, - 0x22,0x24,0x27,0x29,0x2b,0x2e,0x31,0x34,0x37,0x3a,0x3e,0x41, - 0x45,0x49,0x4e,0x52,0x57,0x5c,0x62,0x68,0x6e,0x75,0x7c,0x83, - 0x8b,0x93,0x9c,0xa5,0xaf,0xb9,0xc4,0xd0,0xdd,0xea,0xf8,0xff, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 -}; - unsigned char filterctrl = 0; unsigned char filtertype = 0; unsigned char filtercutoff = 0; diff --git a/src/play.h b/src/play.h index cec7a20..f5a0606 100644 --- a/src/play.h +++ b/src/play.h @@ -33,8 +33,6 @@ enum #ifndef PLAY_C extern unsigned char masterfader; -extern unsigned char freqtbllo[]; -extern unsigned char freqtblhi[]; extern int lastsonginit; #endif diff --git a/src/reloc.cpp b/src/reloc.cpp index 1c87088..1ef57d0 100644 --- a/src/reloc.cpp +++ b/src/reloc.cpp @@ -28,6 +28,7 @@ #include "configfile.h" #include "console.h" #include "display.h" +#include "freqtbl.h" #include "play.h" #include "song.h" #include "table.h" diff --git a/src/tools/ltreloc.cpp b/src/tools/ltreloc.cpp index 78c91f7..45e2944 100644 --- a/src/tools/ltreloc.cpp +++ b/src/tools/ltreloc.cpp @@ -29,9 +29,9 @@ #include "channels.h" #include "colors.h" #include "console.h" +#include "freqtbl.h" #include "pattern.h" #include "reloc.h" -#include "play.h" #include "song.h" #include "bme_io.h" @@ -397,6 +397,7 @@ void calculatefreqtable() } } +// FIXME remove int getMaxChannels() { return (numsids == 1) ? MAX_CHN_MONO : MAX_CHN; diff --git a/src/tools/ltreloc.h b/src/tools/ltreloc.h index 8427654..b90fec8 100644 --- a/src/tools/ltreloc.h +++ b/src/tools/ltreloc.h @@ -48,7 +48,5 @@ extern char instrfilename[MAX_FILENAME]; void waitkeymousenoupdate(); void waitkeynoupdate(); -int getMaxChannels(); -int getVisibleOrderlist(); #endif From e921f28ba241d4c69e58a677d37ad84f06df49e1 Mon Sep 17 00:00:00 2001 From: Leandro Nini Date: Sat, 25 Jul 2026 12:07:53 +0200 Subject: [PATCH 3/6] Refactor --- src/freqtbl.cpp | 18 ++++++++++++++++++ src/freqtbl.h | 2 ++ src/loadtrk.cpp | 14 ++------------ src/tools/ltreloc.cpp | 21 +-------------------- 4 files changed, 23 insertions(+), 32 deletions(-) diff --git a/src/freqtbl.cpp b/src/freqtbl.cpp index f52f1e0..40698c3 100644 --- a/src/freqtbl.cpp +++ b/src/freqtbl.cpp @@ -16,6 +16,8 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#include + unsigned char freqtbllo[] = { 0x17,0x27,0x39,0x4b,0x5f,0x74,0x8a,0xa1,0xba,0xd4,0xf0,0x0e, @@ -47,3 +49,19 @@ unsigned char freqtblhi[] = 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }; + +void calculatefreqtable(double basepitch, double equaldivisionsperoctave) +{ + double basefreq = basepitch * (16777216.0 / 985248.0) * std::pow(2.0, 0.25) / 32.0; + + for (int c = 0; c < 8*12 ; c++) + { + double note = c; + double freq = basefreq * std::pow(2.0, note/equaldivisionsperoctave); + int intfreq = freq + 0.5; + if (intfreq > 0xffff) + intfreq = 0xffff; + freqtbllo[c] = intfreq & 0xff; + freqtblhi[c] = intfreq >> 8; + } +} diff --git a/src/freqtbl.h b/src/freqtbl.h index 5ab537e..576a4e8 100644 --- a/src/freqtbl.h +++ b/src/freqtbl.h @@ -24,4 +24,6 @@ extern unsigned char freqtbllo[]; extern unsigned char freqtblhi[]; #endif +void calculatefreqtable(double basepitch, double equaldivisionsperoctave); + #endif diff --git a/src/loadtrk.cpp b/src/loadtrk.cpp index 3841180..529cfed 100644 --- a/src/loadtrk.cpp +++ b/src/loadtrk.cpp @@ -1627,10 +1627,9 @@ void nextmultiplier() void calculatefreqtable() { - double basefreq = (double)basepitch * (16777216.0 / 985248.0) * std::pow(2.0, 0.25) / 32.0; - if (tuningcount) { + double basefreq = (double)basepitch * (16777216.0 / 985248.0) * std::pow(2.0, 0.25) / 32.0; double cyclebasefreq = basefreq; double freq = basefreq; int c = 0; @@ -1654,16 +1653,7 @@ void calculatefreqtable() } else { - for (int c = 0; c < 8*12 ; c++) - { - double note = c; - double freq = basefreq * std::pow(2.0, note/(double)equaldivisionsperoctave); - int intfreq = static_cast(freq + 0.5); - if (intfreq > 0xffff) - intfreq = 0xffff; - freqtbllo[c] = intfreq & 0xff; - freqtblhi[c] = intfreq >> 8; - } + calculatefreqtable(basepitch, equaldivisionsperoctave); } } diff --git a/src/tools/ltreloc.cpp b/src/tools/ltreloc.cpp index 45e2944..ab9ace8 100644 --- a/src/tools/ltreloc.cpp +++ b/src/tools/ltreloc.cpp @@ -42,7 +42,6 @@ #endif #include -#include #include #include #include @@ -96,8 +95,6 @@ const char *programname = "LTReloc v" PACKAGE_VERSION; extern unsigned char datafile[]; -void calculatefreqtable(); - #ifdef _WIN32 FILE *STDOUT, *STDERR; #else @@ -363,7 +360,7 @@ int main(int argc, char **argv) if (basepitch < 0.0f) basepitch = 0.0f; if (basepitch > 0.0f) - calculatefreqtable(); + calculatefreqtable(basepitch, 12.); // perform relocation relocator(packedsongname); @@ -381,22 +378,6 @@ void waitkeynoupdate() { } -void calculatefreqtable() -{ - double basefreq = (double)basepitch * (16777216.0 / 985248.0) * std::pow(2.0, 0.25) / 32.0; - - for (int c = 0; c < 8*12 ; c++) - { - double note = c; - double freq = basefreq * std::pow(2.0, note/12.0); - int intfreq = freq + 0.5; - if (intfreq > 0xffff) - intfreq = 0xffff; - freqtbllo[c] = intfreq & 0xff; - freqtblhi[c] = intfreq >> 8; - } -} - // FIXME remove int getMaxChannels() { From d4a495ed786c67fb4766e66f73eeaa28d5f8f2a5 Mon Sep 17 00:00:00 2001 From: Leandro Nini Date: Sat, 25 Jul 2026 12:18:11 +0200 Subject: [PATCH 4/6] Update --- src/tools/sngspli2.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/tools/sngspli2.cpp b/src/tools/sngspli2.cpp index 46c6de0..4dcfb0a 100644 --- a/src/tools/sngspli2.cpp +++ b/src/tools/sngspli2.cpp @@ -41,10 +41,10 @@ int wavelen = 0, pulselen = 0, filtlen = 0; int loadsong(char *name); int savesong(char *name); -int processsong(void); -void countpatternlengths(void); -void countdestpatternlengths(void); -void clearsong(void); +int processsong(); +void countpatternlengths(); +void countdestpatternlengths(); +void clearsong(); int gettablelen(int num); int main(int argc, char **argv) @@ -424,7 +424,7 @@ int savesong(char *name) return 0; } -void countpatternlengths(void) +void countpatternlengths() { highestusedpattern = 0; for (int c = 0; c < MAX_PATT; c++) @@ -455,7 +455,7 @@ void countpatternlengths(void) } } -void countdestpatternlengths(void) +void countdestpatternlengths() { for (int c = 0; c < MAX_PATT; c++) { @@ -480,7 +480,7 @@ void countdestpatternlengths(void) } } -void clearsong(void) +void clearsong() { for (int c = 0; c < MAX_CHN; c++) { From b77c934033a4a9973d01f680b62393b6b2e859f8 Mon Sep 17 00:00:00 2001 From: Leandro Nini Date: Sat, 25 Jul 2026 13:25:11 +0200 Subject: [PATCH 5/6] Remove duplicate code --- src/play.cpp | 153 +++++++++++++++++---------------------------------- 1 file changed, 50 insertions(+), 103 deletions(-) diff --git a/src/play.cpp b/src/play.cpp index 6aafac2..db71b7a 100644 --- a/src/play.cpp +++ b/src/play.cpp @@ -66,7 +66,6 @@ int timesec = 0; unsigned timeframe = 0; void sequencer(int c, Chn *cptr); -void sequencer_stereo(int c, Chn *cptr); void resettime(); void incrementtime(); @@ -941,58 +940,6 @@ void playroutine() if (songinit != PLAY_STOPPED) incrementtime(); } -void sequencer(int c, Chn *cptr) -{ - if ((songinit != PLAY_STOPPED) && (cptr->pattptr == 0x7fffffff)) - { - cptr->pattptr = startpattpos * 4; - if (!cptr->advance) goto SEQDONE; - // Song loop - if (song.order[psnum][c][cptr->songptr] == LOOPSONG) - { - cptr->songptr = song.order[psnum][c][cptr->songptr+1]; - if (cptr->songptr >= song.len[psnum][c]) - { - stopsong(); - cptr->songptr = 0; - goto SEQDONE; - } - } - // Transpose - if ((song.order[psnum][c][cptr->songptr] >= TRANSDOWN) && (song.order[psnum][c][cptr->songptr] < LOOPSONG)) - { - cptr->trans = song.order[psnum][c][cptr->songptr]-TRANSUP; - cptr->songptr++; - } - // Repeat - if ((song.order[psnum][c][cptr->songptr] >= REPEAT) && (song.order[psnum][c][cptr->songptr] < TRANSDOWN)) - { - cptr->repeat = song.order[psnum][c][cptr->songptr]-REPEAT; - cptr->songptr++; - } - // Pattern number - cptr->pattnum = song.order[psnum][c][cptr->songptr]; - if (cptr->repeat) - cptr->repeat--; - else - cptr->songptr++; - - // Check for illegal pattern now - if (cptr->pattnum >= MAX_PATT) - { - stopsong(); - cptr->pattnum = 0; - } - if (cptr->pattptr >= (getPattlen(cptr->pattnum) * 4)) - cptr->pattptr = 0; - - // Check for playback endpos - if ((lastsonginit != PLAY_BEGINNING) && (esend[c] > 0) && (esend[c] > espos[c]) && (cptr->songptr > esend[c]) && (espos[c] < song.len[psnum][c])) - cptr->songptr = espos[c]; - } -SEQDONE: {} -} - /* ========================================================================== */ void playroutine_stereo() @@ -1061,7 +1008,7 @@ void playroutine_stereo() cptr->tempo = song.instr[MAX_INSTR-1].ad - 1; cptr->trans = 0; cptr->instr = 1; - sequencer_stereo(c, cptr); + sequencer(c, cptr); break; case PLAY_PATTERN: @@ -1074,7 +1021,7 @@ void playroutine_stereo() case PLAY_POS: cptr->songptr = espos[c]; - sequencer_stereo(c, cptr); + sequencer(c, cptr); break; } cptr++; @@ -1229,7 +1176,7 @@ void playroutine_stereo() // Tick 0 TICK0_S: // Advance in sequencer - sequencer_stereo(c, cptr); + sequencer(c, cptr); // Get gatetimer compare-value cptr->gatetimer = iptr->gatetimer & 0x3f; @@ -1951,57 +1898,57 @@ void playroutine_stereo() if (songinit != PLAY_STOPPED) incrementtime(); } -void sequencer_stereo(int c, Chn *cptr) +void sequencer(int c, Chn *cptr) { - if ((songinit != PLAY_STOPPED) && (cptr->pattptr == 0x7fffffff)) + if ((songinit != PLAY_STOPPED) && (cptr->pattptr == 0x7fffffff)) + { + cptr->pattptr = startpattpos * 4; + if (!cptr->advance) goto SEQDONE; + // Song loop + if (song.order[psnum][c][cptr->songptr] == LOOPSONG) { - cptr->pattptr = startpattpos * 4; - if (!cptr->advance) goto SEQDONE_S; - // Song loop - if (song.order[psnum][c][cptr->songptr] == LOOPSONG) - { - cptr->songptr = song.order[psnum][c][cptr->songptr+1]; - if (cptr->songptr >= song.len[psnum][c]) - { - stopsong(); - cptr->songptr = 0; - goto SEQDONE_S; - } - } - // Transpose - if ((song.order[psnum][c][cptr->songptr] >= TRANSDOWN) && (song.order[psnum][c][cptr->songptr] < LOOPSONG)) - { - cptr->trans = song.order[psnum][c][cptr->songptr]-TRANSUP; - cptr->songptr++; - } - // Repeat - if ((song.order[psnum][c][cptr->songptr] >= REPEAT) && (song.order[psnum][c][cptr->songptr] < TRANSDOWN)) - { - cptr->repeat = song.order[psnum][c][cptr->songptr]-REPEAT; - cptr->songptr++; - } - // Pattern number - cptr->pattnum = song.order[psnum][c][cptr->songptr]; - if (cptr->repeat) - cptr->repeat--; - else - cptr->songptr++; - - // Check for illegal pattern now - if (cptr->pattnum >= MAX_PATT) - { - stopsong(); - cptr->pattnum = 0; - } - if (cptr->pattptr >= (getPattlen(cptr->pattnum) * 4)) - cptr->pattptr = 0; + cptr->songptr = song.order[psnum][c][cptr->songptr+1]; + if (cptr->songptr >= song.len[psnum][c]) + { + stopsong(); + cptr->songptr = 0; + goto SEQDONE; + } + } + // Transpose + if ((song.order[psnum][c][cptr->songptr] >= TRANSDOWN) && (song.order[psnum][c][cptr->songptr] < LOOPSONG)) + { + cptr->trans = song.order[psnum][c][cptr->songptr]-TRANSUP; + cptr->songptr++; + } + // Repeat + if ((song.order[psnum][c][cptr->songptr] >= REPEAT) && (song.order[psnum][c][cptr->songptr] < TRANSDOWN)) + { + cptr->repeat = song.order[psnum][c][cptr->songptr]-REPEAT; + cptr->songptr++; + } + // Pattern number + cptr->pattnum = song.order[psnum][c][cptr->songptr]; + if (cptr->repeat) + cptr->repeat--; + else + cptr->songptr++; - // Check for playback endpos - if ((lastsonginit != PLAY_BEGINNING) && (esend[c] > 0) && (esend[c] > espos[c]) && (cptr->songptr > esend[c]) && (espos[c] < song.len[psnum][c])) - cptr->songptr = espos[c]; + // Check for illegal pattern now + if (cptr->pattnum >= MAX_PATT) + { + stopsong(); + cptr->pattnum = 0; } -SEQDONE_S: - {} + if (cptr->pattptr >= (getPattlen(cptr->pattnum) * 4)) + cptr->pattptr = 0; + + // Check for playback endpos + if ((lastsonginit != PLAY_BEGINNING) && (esend[c] > 0) && (esend[c] > espos[c]) && (cptr->songptr > esend[c]) && (espos[c] < song.len[psnum][c])) + cptr->songptr = espos[c]; + } +SEQDONE: + {} } void resettime() From 3ee72d30f5b6fc6285500bb2e98029655d9310d6 Mon Sep 17 00:00:00 2001 From: Leandro Nini Date: Sat, 25 Jul 2026 13:47:27 +0200 Subject: [PATCH 6/6] Refactor --- CMakeLists.txt | 2 ++ src/display.cpp | 3 ++- src/loadtrk.cpp | 7 ++++++ src/play.cpp | 58 ++++-------------------------------------- src/timer.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ src/timer.h | 43 +++++++++++++++++++++++++++++++ 6 files changed, 126 insertions(+), 54 deletions(-) create mode 100644 src/timer.cpp create mode 100644 src/timer.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e852690..a5c8b81 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,6 +51,7 @@ set(LOADTRK_SOURCES src/colors.cpp src/channels.cpp src/freqtbl.cpp + src/timer.cpp src/sid.cpp src/ltlog.cpp src/ltmidi.cpp @@ -104,6 +105,7 @@ set(LTRELOC_SOURCES src/colors.cpp src/channels.cpp src/freqtbl.cpp + src/timer.cpp src/ltlog.cpp src/bme/bme_win.cpp src/bme/bme_io.cpp diff --git a/src/display.cpp b/src/display.cpp index cc559bd..2a253ce 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -35,6 +35,7 @@ #include "play.h" #include "song.h" #include "table.h" +#include "timer.h" #include "bme_main.h" #include "bme_snd.h" @@ -532,7 +533,7 @@ void printstatus() const char *playmode = isplaying() ? "PLAYING" : "STOPPED"; printtext(dpos.octaveX+10, dpos.octaveY, colors.CTITLE, playmode); - gettime(textbuffer); + timer.get(textbuffer); color = isplaying() ? colors.CEDIT : colors.CMUTE; printtext(dpos.octaveX+10, dpos.octaveY+1, color, textbuffer); diff --git a/src/loadtrk.cpp b/src/loadtrk.cpp index 529cfed..ac651e9 100644 --- a/src/loadtrk.cpp +++ b/src/loadtrk.cpp @@ -36,6 +36,7 @@ #include "song.h" #include "sound.h" #include "table.h" +#include "timer.h" #include "bme_main.h" #include "bme_win.h" @@ -340,6 +341,9 @@ int main(int argc, char **argv) initchannels(); clearsong(true, true, true, true, true); + timer.setfreq(ntsc); + timer.setmult(multiplier); + // Init sound if (!sound_init(mr, writer, sidmodel, ntsc, multiplier, interpolate, customclockrate, exsid, filterbias, combwaves)) { @@ -960,6 +964,7 @@ void mousecommands() if ((mousex >= dpos.statusTopFvX+9) && (mousex <= dpos.statusTopFvX+12)) { ntsc ^= 1; + timer.setfreq(ntsc); sound_init(mr, writer, sidmodel, ntsc, multiplier, interpolate, customclockrate, exsid, filterbias, combwaves); } if ((mousex >= dpos.statusTopFvX+14) && (mousex <= dpos.statusTopFvX+17)) @@ -1612,6 +1617,7 @@ void prevmultiplier() if (multiplier > 0) { multiplier--; + timer.setmult(multiplier); sound_init(mr, writer, sidmodel, ntsc, multiplier, interpolate, customclockrate, exsid, filterbias, combwaves); } } @@ -1621,6 +1627,7 @@ void nextmultiplier() if (multiplier < 16) { multiplier++; + timer.setmult(multiplier); sound_init(mr, writer, sidmodel, ntsc, multiplier, interpolate, customclockrate, exsid, filterbias, combwaves); } } diff --git a/src/play.cpp b/src/play.cpp index db71b7a..d753aa6 100644 --- a/src/play.cpp +++ b/src/play.cpp @@ -33,6 +33,7 @@ #include "sid.h" #include "song.h" #include "sound.h" +#include "timer.h" #ifdef LTRELOC # include "tools/ltreloc.h" @@ -59,15 +60,7 @@ int songinit = PLAY_STOPPED; int lastsonginit = 0; int startpattpos = 0; -char timechar[] = {':', ' '}; - -int timemin = 0; -int timesec = 0; -unsigned timeframe = 0; - void sequencer(int c, Chn *cptr); -void resettime(); -void incrementtime(); void initsong(int num, int mode) { @@ -160,7 +153,7 @@ void playroutine() filterctrl = 0; filterptr = 0; - resettime(); + timer.reset(); if ((songinit == PLAY_POS) || (songinit == PLAY_PATTERN)) { @@ -937,7 +930,7 @@ void playroutine() cptr++; } } - if (songinit != PLAY_STOPPED) incrementtime(); + if (songinit != PLAY_STOPPED) timer.increment(); } /* ========================================================================== */ @@ -958,7 +951,7 @@ void playroutine_stereo() filter2ctrl = 0; filter2ptr = 0; - resettime(); + timer.reset(); if ((songinit == PLAY_POS) || (songinit == PLAY_PATTERN)) { @@ -1895,7 +1888,7 @@ void playroutine_stereo() cptr++; } } - if (songinit != PLAY_STOPPED) incrementtime(); + if (songinit != PLAY_STOPPED) timer.increment(); } void sequencer(int c, Chn *cptr) @@ -1950,44 +1943,3 @@ void sequencer(int c, Chn *cptr) SEQDONE: {} } - -void resettime() -{ - timemin = 0; - timesec = 0; - timeframe = 0; -} - -void incrementtime() -{ - timeframe++; - unsigned framerate = ntsc ? NTSCFRAMERATE : PALFRAMERATE; - if (((multiplier) && (timeframe >= framerate*multiplier)) - || ((!multiplier) && (timeframe >= framerate/2))) - { - timeframe = 0; - timesec++; - } - if (timesec == 60) - { - timesec = 0; - timemin++; - timemin %= 60; - } -} - -void gettime(char *buf) -{ - int idx; - if (multiplier) - { - idx = (ntsc ? 30 : 25) * multiplier; - } - else - { - idx = ntsc ? 15 : 13; - } - - std::sprintf(buf, " %02d%c%02d ", timemin, timechar[(timeframe/idx) & 1], timesec); -} - diff --git a/src/timer.cpp b/src/timer.cpp new file mode 100644 index 0000000..ad4db2c --- /dev/null +++ b/src/timer.cpp @@ -0,0 +1,67 @@ +/* + * LoadTracker + * + * 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 + */ + +#include "timer.h" + +#include "sound.h" + +#include + +Timer timer; + +char timechar[2] = {':', ' '}; + +void Timer::reset() +{ + timemin = 0; + timesec = 0; + timeframe = 0; +} + +void Timer::increment() +{ + timeframe++; + unsigned framerate = m_ntsc ? NTSCFRAMERATE : PALFRAMERATE; + if (((m_multiplier) && (timeframe >= framerate*m_multiplier)) + || ((!m_multiplier) && (timeframe >= framerate/2))) + { + timeframe = 0; + timesec++; + } + if (timesec == 60) + { + timesec = 0; + timemin++; + timemin %= 60; + } +} + +void Timer::get(char *buf) +{ + int idx; + if (m_multiplier) + { + idx = (m_ntsc ? 30 : 25) * m_multiplier; + } + else + { + idx = m_ntsc ? 15 : 13; + } + + std::sprintf(buf, " %02d%c%02d ", timemin, timechar[(timeframe/idx) & 1], timesec); +} diff --git a/src/timer.h b/src/timer.h new file mode 100644 index 0000000..09fc383 --- /dev/null +++ b/src/timer.h @@ -0,0 +1,43 @@ +/* + * LoadTracker + * + * 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 + */ + +#ifndef TIMER_H +#define TIMER_H + +class Timer +{ +private: + int timemin = 0; + int timesec = 0; + unsigned timeframe = 0; + + unsigned m_ntsc; + unsigned m_multiplier; + +public: + void get(char *buf); + void reset(); + void increment(); + + void setfreq(unsigned ntsc) { m_ntsc = ntsc; } + void setmult(unsigned multiplier) { m_multiplier = multiplier; } +}; + +extern Timer timer; + +#endif