Skip to content

Commit c3adfc4

Browse files
committed
integer player
1 parent ac23f02 commit c3adfc4

File tree

4 files changed

+42
-30
lines changed

4 files changed

+42
-30
lines changed

app_player_adapter.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,17 @@ using std::int16_t;
1515
using std::uint16_t;
1616

1717
namespace rqdq {
18-
namespace app {
18+
namespace {
19+
20+
int FP16toS16_fast(int x) {
21+
return x >> 1; }
1922

23+
int FP16toS16_slow(int x) {
24+
x = std::min(std::max(x, -0xffff), 0xffff); // clamp
25+
return x >> 1; }
26+
27+
}
28+
namespace app {
2029

2130
void PlayerAdapter::BlasterJmp(void* out, int fmt, int numChannels, int numSamples, void* self) {
2231
static_cast<PlayerAdapter*>(self)->BlasterProc(out, fmt, numChannels, numSamples); }
@@ -64,8 +73,8 @@ vga::SetRGB(0, 0x30, 0x20, 0x10);
6473
if (numSamples > 0) {
6574
player_.Render(pbuf_, pbuf_+4096, numSamples);
6675
for (int i=0; i<numSamples; i++) {
67-
int l = pbuf_[i] * 32767.0;
68-
int r = pbuf_[i+4096] * 32767.0;
76+
int l = FP16toS16_fast(pbuf_[i]);
77+
int r = FP16toS16_fast(pbuf_[i+4096]);
6978
PushBack(l, r); }}
7079
#ifdef SHOW_TIMING
7180
vga::SetRGB(0, 0, 0, 0);

app_player_adapter.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class PlayerAdapter {
4040
kb::ModPlayer& player_;
4141
alg::RingIndex<4096> rw_;
4242
std::int16_t buf_[4096*2];
43-
float pbuf_[4096*2]; };
43+
int pbuf_[4096*2]; };
4444

4545

4646
} // namespace app

kb_tinymod.cpp

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,52 +45,55 @@ Paula::Voice::Voice()
4545
volume_(0) {}
4646

4747

48-
void Paula::Voice::Render(float* buffer, int numSamples) {
48+
void Paula::Voice::Render(int* buffer, int numSamples) {
4949
if (samplePtr_ == NULL) {
5050
return; }
5151

52-
const float gain = (volume_ == 0) ? 0.0f : volume_ / 64.0;
52+
int speed = 3546895 / period_ / kOutputFreqInHz * 65536.0f;
5353

54-
float speed = (3546895 / period_ / kOutputFreqInHz);
5554
int8_t* smp = samplePtr_;
5655
for (int i=0; i<numSamples; i++) {
57-
int p1 = int(pos_);
58-
float frac = pos_ - p1;
56+
int p1 = pos_>>16;
57+
int frac = pos_&0xffff;
5958
pos_ += speed;
60-
if (pos_ >= sampleLen_) {
61-
pos_ -= loopLen_; }
62-
int p2 = int(pos_);
63-
float smp1 = int8ToFloat(smp[p1]);
64-
float smp2 = int8ToFloat(smp[p2]);
65-
float cur = smp1*(1.0-frac) + smp2*frac;
59+
if (pos_ >= sampleLen_<<16) {
60+
pos_ -= loopLen_<<16; }
61+
int p2 = pos_>>16;
62+
int smp1 = smp[p1];
63+
int smp2 = smp[p2];
6664

67-
buffer[i] += cur * gain; }}
65+
// int cur = smp1 * 65536; // cur is 16:16 [-128, 127]
66+
int s = smp1*(0x10000-frac) + smp2*frac;
67+
s = s*volume_ >> 6; // s is unchanged
68+
s = s >> 7; // s is 16:16 [-1, 1]
69+
buffer[i] += s; }}
6870

6971

7072
void Paula::Voice::Trigger(int8_t* samplePtr, int sampleLen, int loopLen, int offs) {
7173
samplePtr_ = samplePtr;
7274
sampleLen_ = sampleLen;
7375
loopLen_ = loopLen;
74-
pos_ = std::min(offs, sampleLen-1); }
76+
pos_ = std::min(offs, sampleLen-1) << 16; }
7577

7678

77-
Paula::Paula() :masterGain_(0.5f) {}
79+
Paula::Paula() :masterGain_(0.25f) {}
7880

7981

80-
void Paula::Render(float* lb, float* rb, int numSamples) {
82+
void Paula::Render(int* lb, int* rb, int numSamples) {
8183
// const float pan = 0.5f + 0.5f * masterSeparation_;
8284
// const float vm0 = masterGain_ * sqrt(pan);
8385
// const float vm1 = masterGain_ * sqrt(1-pan);
86+
const int mg = masterGain_ * 65536.0f;
8487

8588
for (int i=0; i<numSamples; i++) {
8689
out_[i] = 0;
8790
out_[i+4096] = 0; }
8891
for (int vi=0; vi<kNumVoices; vi++) {
89-
float* dst = (vi==1||vi==2) ? out_ + 4096 : out_;
92+
int* dst = (vi==1||vi==2) ? out_ + 4096 : out_;
9093
voice_[vi].Render(dst, numSamples); }
9194
for (int s=0; s<numSamples; s++) {
92-
*lb++ = out_[s] * masterGain_;
93-
*rb++ = out_[s+4096] * masterGain_; }}
95+
*lb++ = out_[s] *mg >> 16;
96+
*rb++ = out_[s+4096]*mg >> 16; }}
9497

9598

9699
void ModPlayer::Sample::Prepare() {
@@ -439,7 +442,7 @@ ModPlayer::ModPlayer(Paula* p, uint8_t* moddata) :paula_(p) {
439442
Reset(); }
440443

441444

442-
void ModPlayer::Render(float* lb, float* rb, int numSamples) {
445+
void ModPlayer::Render(int* lb, int* rb, int numSamples) {
443446
while (numSamples) {
444447
int todo = std::min(numSamples, trCounter_);
445448
if (todo) {
@@ -453,7 +456,7 @@ void ModPlayer::Render(float* lb, float* rb, int numSamples) {
453456
trCounter_ = tickRate_; }}}
454457

455458

456-
void ModPlayer::RenderJmp(void* param, float* lb, float *rb, int len) {
459+
void ModPlayer::RenderJmp(void* param, int* lb, int* rb, int len) {
457460
((ModPlayer*)param)->Render(lb, rb, len); }
458461

459462

kb_tinymod.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@ class Paula {
99

1010
class Voice {
1111
public:
12-
float pos_;
12+
int pos_; // 16:16 fixed
1313
std::int8_t* samplePtr_;
1414
int sampleLen_;
1515
int loopLen_;
1616
int period_; // 124 .. 65535
1717
int volume_; // 0 .. 64
1818

1919
Voice();
20-
void Render(float* buffer, int numSamples);
20+
void Render(int* buffer, int numSamples);
2121
void Trigger(std::int8_t* samplePtr, int sampleLen, int loopLen, int offs=0); };
2222

2323
Voice voice_[4];
24-
float out_[4096*2]; // left, right
24+
int out_[4096*2]; // left, right
2525
float masterGain_;
2626
// float masterSeparation_;
2727

2828
public:
2929
Paula();
30-
void Render(float* lb, float *rb, int numSamples); };
30+
void Render(int* lb, int* rb, int numSamples); };
3131

3232

3333
class ModPlayer {
@@ -113,12 +113,12 @@ class ModPlayer {
113113
char name_[21];
114114

115115
ModPlayer(Paula* paula, std::uint8_t* moddata);
116-
void Render(float* lb, float* rb, int numSamples);
116+
void Render(int* lb, int* rb, int numSamples);
117117
int GetCurrentPos() const {
118118
return curPos_; }
119119
int GetCurrentRow() const {
120120
return curRow_; }
121-
static void RenderJmp(void* param, float* lb, float* rb, int len); };
121+
static void RenderJmp(void* param, int* lb, int* rb, int len); };
122122

123123

124124
} // namespace kb

0 commit comments

Comments
 (0)