Skip to content

Commit 0772db8

Browse files
committed
OPL4 WIP new Volume algorithm
1 parent a584316 commit 0772db8

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

src/main/msx/audio/OPL4Audio.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,11 @@ wmsx.OPL4Audio = function(pName, cart) {
8989

9090
var audioSocket, audioSignal;
9191

92-
var VOLUME = 0.65 * (7 / 24 / 4096); // X channels, samples -4096 .. +4096
92+
var VOLUME = 0.65 * (7 / 24 / 32768); // 24 channels, samples -32768 .. +32768
9393
var SAMPLE_RATE = 44100; // Main CPU clock / 81.2734693877551 = 44100 Hz
9494
var CLOCK = 49780; // Main CPU clock / 72 = 49780 Hz
9595

96+
9697
// Savestate -------------------------------------------
9798

9899
this.saveState = function() {

src/main/msx/audio/OPL4AudioWave.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ wmsx.OPL4AudioWave = function(opl4) {
1616
rateDecayDurTable = tabs.getRateDecayDurations();
1717
envAttackCurve = tabs.getEnvAttackCurve();
1818
panpotValues = tabs.getPanPotValues();
19+
volumeTable = tabs.getVolumeTable();
1920
}
2021

2122
this.connect = function(machine) {
@@ -61,17 +62,19 @@ wmsx.OPL4AudioWave = function(opl4) {
6162
wmsx.Util.arrayFill(dl, 0);
6263
wmsx.Util.arrayFill(d2r, 0);
6364
wmsx.Util.arrayFill(rr, 0);
64-
wmsx.Util.arrayFill(volume, 0x7f);
6565
wmsx.Util.arrayFill(am, 0);
6666
wmsx.Util.arrayFill(vib, 0);
6767
wmsx.Util.arrayFill(rc, 0);
68+
wmsx.Util.arrayFill(volume, 0x7f);
69+
wmsx.Util.arrayFill(panpotL, 0);
70+
wmsx.Util.arrayFill(panpotR, 0);
6871

6972
// Dynamic values
7073

7174
wmsx.Util.arrayFill(amAtt, 0);
72-
wmsx.Util.arrayFill(envAtt, 0x100 << 4);
73-
wmsx.Util.arrayFill(totalAttL, 0x100 << 4);
74-
wmsx.Util.arrayFill(totalAttR, 0x100 << 4);
75+
wmsx.Util.arrayFill(envAtt, 256);
76+
wmsx.Util.arrayFill(totalAttL, 1024);
77+
wmsx.Util.arrayFill(totalAttR, 1024);
7578
wmsx.Util.arrayFill(envStep, IDLE);
7679
wmsx.Util.arrayFill(envStepLevelDur, 0);
7780
wmsx.Util.arrayFill(envStepLevelIncClock, 0);
@@ -135,11 +138,11 @@ wmsx.OPL4AudioWave = function(opl4) {
135138

136139
if (delta > 0) {
137140
sample = advanceSample(cha, delta);
138-
sampleL += expTable[sample + totalAttL[cha]];
139-
sampleR += expTable[sample + totalAttR[cha]];
141+
sampleL += sample * volumeTable[totalAttL[cha]];
142+
sampleR += sample * volumeTable[totalAttR[cha]];
140143
} else {
141-
sampleL += expTable[sampleValue[cha] + totalAttL[cha]];
142-
sampleR += expTable[sampleValue[cha] + totalAttR[cha]];
144+
sampleL += sampleValue[cha] * volumeTable[totalAttL[cha]];
145+
sampleR += sampleValue[cha] * volumeTable[totalAttR[cha]];
143146
}
144147

145148
// if (delta > 0) sampleL += advanceSample(cha, delta); else sampleL += sampleValue[cha];
@@ -319,15 +322,15 @@ wmsx.OPL4AudioWave = function(opl4) {
319322
function startSample(cha) {
320323
samplePos[cha] = 0;
321324
phaseCounter[cha] = 0;
322-
return updateSampleValue12bitsLog(cha);
325+
return updateSampleValue16bits(cha);
323326
}
324327

325328
function advanceSample(cha, quant) {
326329
var newPos = samplePos[cha] + quant;
327330
samplePos[cha] = newPos > endPosition[cha]
328331
? loopPosition[cha] + (newPos - endPosition[cha]) - 1
329332
: newPos;
330-
return updateSampleValue12bitsLog(cha);
333+
return updateSampleValue16bits(cha);
331334
}
332335

333336
function updateSampleValue12bitsLog(cha) {
@@ -492,8 +495,8 @@ wmsx.OPL4AudioWave = function(opl4) {
492495
}
493496

494497
function updateTotalAttenuation(cha) {
495-
totalAttL[cha] = amAtt[cha] + (envAtt[cha] << 4) + (volume[cha] << 4) + (panpotL[cha] << 7);
496-
totalAttR[cha] = amAtt[cha] + (envAtt[cha] << 4) + (volume[cha] << 4) + (panpotR[cha] << 7);
498+
totalAttL[cha] = amAtt[cha] + (envAtt[cha] << 0) + (volume[cha] << 0) + (panpotL[cha] << 3);
499+
totalAttR[cha] = amAtt[cha] + (envAtt[cha] << 0) + (volume[cha] << 0) + (panpotR[cha] << 3);
497500
}
498501

499502

@@ -559,7 +562,7 @@ wmsx.OPL4AudioWave = function(opl4) {
559562

560563
// Pre calculated tables, factors, values
561564

562-
var linearTable, expTable, vibValues, rateAttackDurTable, rateDecayDurTable, envAttackCurve, panpotValues;
565+
var volumeTable, linearTable, expTable, vibValues, rateAttackDurTable, rateDecayDurTable, envAttackCurve, panpotValues;
563566
var sampleResult = [ 0, 0 ];
564567

565568

src/main/msx/audio/OPL4WaveTables.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ wmsx.OPL4WaveTables = function() {
6868
return this.PANPOT_VALUES;
6969
};
7070

71+
this.getVolumeTable = function() {
72+
var v = new Array(1025);
73+
for (var i = 0; i < 1024; ++i) v[i] = Math.pow(10, -0.375 * i / 20);
74+
v[1024] = 0;
75+
return v;
76+
};
77+
78+
7179
this.VIB_VALUES = [
7280
[ 0, 0, 0, 0, 0, 0, 0, 0 ], // According to fNum >> 6 (one line for each value)
7381
[ 0, 0, 1, 0, 0, 0, -1, 0 ], // Half these values must be added to fNum BEFORE multi
@@ -121,7 +129,6 @@ wmsx.OPL4WaveTables = function() {
121129
// -3 .. -18 dB. Min = -96 dB
122130
[ 0, 1, 2, 3, 4, 5, 6, 32, 32, 0, 0, 0, 0, 0, 0, 0 ], // L
123131
[ 0, 0, 0, 0, 0, 0, 0, 0, 32, 32, 6, 5, 4, 3, 2, 1 ] // R
124-
]
125-
132+
];
126133

127134
};

0 commit comments

Comments
 (0)