fix scroll end bug

This commit is contained in:
Boaz Sender 2025-04-22 07:20:32 -07:00
parent 6a74ca9910
commit 0eaa809a4f
3 changed files with 39 additions and 166 deletions

View file

@ -26,7 +26,7 @@ let register = "high";
let count = 0;
setInterval(() => {
const absSample = Math.abs(audioData.channelData[0][count]);
if (count > totalSamples) {
if (count == totalSamples - 1) {
count = 0;
}
else {
@ -60,26 +60,19 @@ setInterval(() => {
}
}, TICK);
// Play top line
const playNewStanza = async () => {
let stanza = await getStanza(register);
let stanzaWaitTimeout;
let stanzaCharacterLocation = 0;
const scrollStanza = async () => {
let stanza = await getStanza(register);
while (true) {
if (stanzaCharacterLocation == stanza.length) {
stanzaCharacterLocation = 0;
stanza = await getStanza(register);
}
lcd.printLineSync(0, " ");
stanzaWaitTimeout = setTimeout(() => {
setTimeout(() => {
lcd.printLineSync(0, `${16 - stanzaCharacterLocation > 0
? Array(16 - stanzaCharacterLocation).join(" ")
: ""}${stanza}`.substring(stanzaCharacterLocation, stanza.length));
}, WAIT);
await timer(TICK);
stanzaCharacterLocation++;
if (stanzaCharacterLocation === stanza.length) {
playNewStanza();
}
else {
scrollStanza();
}
};
scrollStanza();
};
playNewStanza();

View file

@ -1,111 +0,0 @@
import fs from "node:fs";
import exec from "node:child_process";
import _ from "underscore";
import PCMPlayer from 'pcm-player'
import pcm from "pcm";
var min = 1.0;
var max = -1.0;
const pcmData = []
await pcm.getPcmData('/home/boazsender/Downloads/BlackPortal recording April 6 2025.wav', { stereo: true, sampleRate: 44100 },
function(sample, channel) {
// Sample is from [-1.0...1.0], channel is 0 for left and 1 for right
pcmData.push(sample)
min = Math.min(min, sample);
max = Math.max(max, sample);
},
function(err, output) {
if (err)
throw new Error(err);
console.log('min=' + min + ', max=' + max);
}
);
console.log(pcmData)
/**
* [findPeaks Naive algo to identify peaks in the audio data, and wave]
* @param {[type]} pcmdata [description]
* @param {[type]} samplerate [description]
* @return {[type]} [description]
*/
function findPeaks(pcmdata, samplerate) {
const interval = 0.05 * 1000;
index = 0;
const step = Math.round(samplerate * (interval / 1000));
const max = 0;
const prevmax = 0;
const prevdiffthreshold = 0.3;
//loop through song in time with sample rate
const samplesound = setInterval(
function () {
if (index >= pcmdata.length) {
clearInterval(samplesound);
console.log("finished sampling sound");
return;
}
for (const i = index; i < index + step; i++) {
max = pcmdata[i] > max ? pcmdata[i].toFixed(1) : max;
}
// Spot a significant increase? Potential peak
bars = getbars(max);
if (max - prevmax >= prevdiffthreshold) {
bars = bars + " == peak == ";
}
// Print out mini equalizer on commandline
console.log(bars, max);
prevmax = max;
max = 0;
index += step;
},
interval,
pcmdata
);
}
/**
* TBD
* @return {[type]} [description]
*/
function detectBeats() {}
/**
* [getbars Visualize image sound using bars, from average pcmdata within a sample interval]
* @param {[Number]} val [the pcmdata point to be visualized ]
* @return {[string]} [a set of bars as string]
*/
function getbars(val) {
bars = "";
for (const i = 0; i < val * 50 + 2; i++) {
bars = bars + "|";
}
return bars;
}
/**
* [Plays a sound file]
* @param {[string]} soundfile [file to be played]
* @return {[type]} [void]
*/
function playsound(soundfile) {
// linux or raspi
// const create_audio = exec('aplay'+soundfile, {maxBuffer: 1024 * 500}, function (error, stdout, stderr) {
const create_audio = exec(
"mplayer -loop 0 " + soundfile,
{ maxBuffer: 1024 * 500 },
function (error, stdout, stderr) {
if (error !== null) {
console.log("exec error: " + error);
} else {
//console.log(" finshed ");
//micInstance.resume();
}
}
);
}

View file

@ -35,7 +35,7 @@ let count = 0;
setInterval(() => {
const absSample = Math.abs(audioData.channelData[0][count]);
if (count > totalSamples) {
if (count == totalSamples - 1) {
count = 0;
} else {
count++;
@ -79,16 +79,16 @@ setInterval(() => {
}, TICK);
// Play top line
const playNewStanza = async () => {
let stanza = await getStanza(register);
let stanzaWaitTimeout;
let stanzaCharacterLocation = 0;
let stanza = await getStanza(register);
while (true) {
if (stanzaCharacterLocation == stanza.length) {
stanzaCharacterLocation = 0;
stanza = await getStanza(register);
}
const scrollStanza = async () => {
lcd.printLineSync(0, " ");
stanzaWaitTimeout = setTimeout(() => {
setTimeout(() => {
lcd.printLineSync(
0,
`${
@ -100,13 +100,4 @@ const playNewStanza = async () => {
}, WAIT);
await timer(TICK);
stanzaCharacterLocation++;
if (stanzaCharacterLocation === stanza.length) {
playNewStanza();
} else {
scrollStanza();
}
};
scrollStanza();
};
playNewStanza();