Skip to content

Commit

Permalink
PC Speaker: fix it
Browse files Browse the repository at this point in the history
  • Loading branch information
tildearrow committed Feb 26, 2025
1 parent d935d3c commit e94d99d
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions src/engine/platform/pcspkr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,31 +194,47 @@ const char** DivPlatformPCSpeaker::getRegisterSheet() {

void DivPlatformPCSpeaker::acquire_unfilt(blip_buffer_t** bb, size_t off, size_t len) {
int out=0;
int freq1=freq+1;
int timeToNextToggle=0;
if (on) {
// just in case
if (pos>freq1) {
pos=freq1;
}
if (pos>(freq>>1)) {
posToggle=true;
timeToNextToggle=pos-(freq>>1);
} else {
posToggle=false;
timeToNextToggle=pos;
}
out=(posToggle && !isMuted[0])?32767:0;
blip_add_delta(bb[0],off,out-oldOut);
oldOut=out;
if (freq>=1) {
if (pos>freq) pos=freq;
size_t boff=off;
size_t i=len;
while (true) {
if ((int)i<pos) {
if ((int)i<timeToNextToggle) {
pos-=i;
break;
}
i-=pos;
boff+=pos;
posToggle=!posToggle;
i-=timeToNextToggle;
boff+=timeToNextToggle;
pos-=timeToNextToggle;
if (pos<=0) {
pos=freq1;
}
if (pos>(freq>>1)) {
posToggle=true;
timeToNextToggle=pos-(freq>>1);
} else {
posToggle=false;
timeToNextToggle=pos;
}
out=(posToggle && !isMuted[0])?32767:0;
blip_add_delta(bb[0],boff,out-oldOut);
oldOut=out;
if (freq&1) {
pos=(freq>>1)+(posToggle?1:0);
} else {
pos=freq>>1;
}
}
}
} else {
Expand Down Expand Up @@ -396,7 +412,6 @@ void DivPlatformPCSpeaker::tick(bool sysTick) {
}
if (freq!=chan[i].freq && resetPhase) {
pos=0;
posToggle=false;
}
freq=chan[i].freq;
if (chan[i].keyOn) chan[i].keyOn=false;
Expand Down

0 comments on commit e94d99d

Please sign in to comment.