Update src/main.cpp

This commit is contained in:
2025-10-23 20:02:12 -04:00
parent 87826ac9c8
commit c4f56bfd20

View File

@@ -12,17 +12,16 @@
#include "trakker.h"
#include "trakker_version.h"
#define SAMPLERATE 48000
#define BUFFER_SIZE 250000
static std::string note_name[] = { "C ", "C#", "D ", "D#", "E ", "F ", "F#", "G ", "G#", "A ", "A#", "B " };
static std::string pages[] = { "1. Info", "2. Pattern", "3. Scope", "4. Piano Roll", "5. Config", "6. Help" };
static const char* device = "default";
snd_pcm_hw_params_t *hwparams;
char* file;
bool colorMode = 0; // 0 - Auto, 1 - Monochrome, 2 - 8-bit, 3 - Full
int mtype = XMP_MODE_AUTO;
int smix = 70;
int srate = 48000;
int display = 0;
int mode = 0;
int vol;
@@ -57,6 +56,7 @@ int main(int argc, char *argv[]) {
printf("-d <num> Start on the specified panel.\n");
printf("-o <num> Toggle player options\n");
printf("-s <num> Stereo Seperation\n");
printf("-r <num> Sample rate (default 48000)\n");
exit(0);
} else if (strcmp(argv[a], "-d") == 0) {
int newdisplay = atoi(argv[a+1])-1;
@@ -119,6 +119,15 @@ int main(int argc, char *argv[]) {
mtype = XMP_MODE_ITSMP;
}
a++;
} else if (strcmp(argv[a], "-r") == 0) {
int newrate = atoi(argv[a+1]);
if (newrate > 192000 || newrate < 48000)
fprintf(stderr, "Sample rate argument is invalid.\n");
else {
printf("Setting sample rate to %dkHz\n", newrate/1000);
srate = newrate;
}
a++;
} else if (!file) {
file = argv[a];
} else {
@@ -128,13 +137,42 @@ int main(int argc, char *argv[]) {
}
int err;
if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
fprintf(stderr, "Playback open error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
unsigned int chan, rate;
unsigned int btime = 250000; /* 250ms */
unsigned int ptime = 50000; /* 50ms */
char *card_name = "default";
if ((err = snd_pcm_open(&handle, card_name,
SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
fprintf(stderr, "Unable to initialize ALSA pcm device: %s\n",
snd_strerror(err));
return -1;
}
if ((err = snd_pcm_set_params(handle, SND_PCM_FORMAT_S16, SND_PCM_ACCESS_RW_INTERLEAVED, 2, SAMPLERATE, 1, BUFFER_SIZE)) < 0) {
fprintf(stderr, "Playback open error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
chan = 2;
rate = 48000;
snd_pcm_hw_params_alloca(&hwparams);
snd_pcm_hw_params_any(handle, hwparams);
snd_pcm_hw_params_set_access(handle, hwparams,
SND_PCM_ACCESS_RW_INTERLEAVED);
snd_pcm_hw_params_set_format(handle, hwparams, SND_PCM_FORMAT_S16);
snd_pcm_hw_params_set_rate_near(handle, hwparams, &rate, 0);
snd_pcm_hw_params_set_channels_near(handle, hwparams, &chan);
snd_pcm_hw_params_set_buffer_time_near(handle, hwparams, &btime, 0);
snd_pcm_hw_params_set_period_time_near(handle, hwparams, &ptime, 0);
snd_pcm_nonblock(handle, 0);
if ((err = snd_pcm_hw_params(handle, hwparams)) < 0) {
fprintf(stderr, "Unable to set ALSA output parameters: %s\n",
snd_strerror(err));
return -1;
}
if ((err = snd_pcm_prepare(handle)) < 0) {
fprintf(stderr, "Unable to prepare ALSA: %s\n",
snd_strerror(err));
return -1;
}
printf("%s initialized.\n", "ALSA");
@@ -250,17 +288,21 @@ int main(int argc, char *argv[]) {
row = pos = -1;
generateEffectsTable(xmi.mod->type);
xmp_set_player(xc, XMP_PLAYER_MODE, mtype);
xmp_start_player(xc, SAMPLERATE, 0);
if ((err = xmp_start_player(xc, 48000, 0)) != 0) {
if ((err = xmp_start_player(xc, 44100, 0)) != 0) {
fprintf(stderr, "Failed to start player: %d\n", err);
exit(EXIT_FAILURE);
}
}
xmp_set_player(xc, XMP_PLAYER_MIX, smix);
int key;
bool displayChanged;
displayChanged = true;
while (true) {
while (xmp_play_frame(xc) == 0) {
xmp_get_frame_info(xc, &xfi);
if (xmp_play_frame(xc) != 0 && !stopped) break;
if (xfi.loop_count > looped && !loop) break;
else looped = xfi.loop_count;
if (xfi.loop_count > looped && !loop) break;
else looped = xfi.loop_count;
keys:
timeout(stopped?-1:0);
@@ -631,37 +673,46 @@ int lerp (int a, int b, float f) {
}
#include <vector>
std::vector<int> bufbuf;
std::vector<int> bufbufmin;
std::vector<int> bufbufmax;
void displayScope(xmp_module_info *mi, xmp_frame_info *fi) {
const char* xbuf = (char*)fi->buffer;
int percol = (fi->buffer_size/(COLS-2));
bufbuf.resize(COLS-2);
bufbufmin.resize(COLS-2);
bufbufmax.resize(COLS-2);
for (int s = 0; s < COLS-2; s++) {
int colsum = 0;
int min = 0;
int max = 0;
for (int v = 0; v < percol; v++) {
colsum += xbuf[(s*percol)+v];
int val = xbuf[(s*percol)+v];
if (val > max || max == 0) max = val;
if (val < min || min == 0) min = val;
}
int l = (colsum / percol)*2;
if (l >= 0x80) l = 0x80;
else if (l <= -0x80) l = -0x80;
bufbuf[s] = l;
// if (ln >= 0xFF) ln = 0xFF;
// else if (ln <= -0xFF) ln = -0xFF;
bufbufmin[s] = (lerp(min, bufbufmin[s-1], 0.2f) / percol)*2;
bufbufmax[s] = (lerp(max, bufbufmax[s-1], 0.2f) / percol)*2;
}
wmove(dis, 0, 0);
for (int t = 0; t < COLS-2; t++) {
//int r = (1.0*(bufbuf[t]+0x80)/0xFF)*(LINES-4);
int r = (1.0*(lerp(bufbuf[t-1], bufbuf[t], 0.5f)+0x80)/0xFF)*(LINES-4);
int n = bufbufmin[t];
int x = bufbufmax[t];
//(1.0*(bufbufmax[t]+0xFF)/512)*(LINES-4)
wmove(dis, r, t);
chtype pixpair = COLOR_PAIR(10);
if (std::abs(bufbuf[t-1] - bufbuf[t]) > 64)
pixpair = COLOR_PAIR(12);
else if (std::abs(bufbuf[t-1] - bufbuf[t]) > 32)
pixpair = COLOR_PAIR(11);
wattron(dis, pixpair);
waddch(dis, ' ');
wattroff(dis, pixpair);
for (int h = n; h <= x; h++) {
if (h >= (LINES-4) || h <= -(LINES-4)) continue;
wmove(dis, ((LINES-4)/2)+(1.0*h), t);
chtype pixpair = COLOR_PAIR(11);
if (h == n)
pixpair = COLOR_PAIR(12);
else if (h == x)
pixpair = COLOR_PAIR(10);
wattron(dis, pixpair);
waddch(dis, ' ');
wattroff(dis, pixpair);
}
}
}
@@ -1001,4 +1052,4 @@ bool isPartOf(char* w1, const char* w2) {
i++;
}
return false;
}
}