Tidy up RawSamplePlayer a bit

This commit is contained in:
Alex Iadicicco 2022-12-10 20:30:39 -08:00 committed by Alexis King
parent 5bc16604bb
commit 158e745975
4 changed files with 779 additions and 830 deletions

View File

@ -9,7 +9,7 @@ import java.util.Iterator;
import java.util.Map;
// "play time" has units of tick-microseconds per quarter note, i.e. it has an extra factor of ticks per
// quarter note in it, which is constant for any given song
// quarter note in it, which is constant for any given song. "play time" is abbreviated to "pt" in many places
public final class MidiPlayer extends AudioSource {
private static final double PHASE_512_TO_RADIANS = 0.01227184630308513D;
@ -41,9 +41,9 @@ public final class MidiPlayer extends AudioSource {
private int microsecondsPerSecond = 1000000;
private final int[] chExpression = new int[16];
private int trackWithSoonestEvent;
private long lastBufferPlayTime;
private long outputPt;
private int nextEventTicks;
private long currentPlayTime;
private long playerPt;
private boolean looped;
private SongData songData;
@ -172,20 +172,20 @@ public final class MidiPlayer extends AudioSource {
@Override
public synchronized void processAndWrite(final int[] dataS16P8, int offset, int len) {
if (this.midiReader.isLoaded()) {
final int var4 = this.midiReader.ticksPerQuarterNote * this.microsecondsPerSecond / SampledAudioChannelS16.SAMPLE_RATE;
final long ptPerSample = this.midiReader.ticksPerQuarterNote * this.microsecondsPerSecond / SampledAudioChannelS16.SAMPLE_RATE;
do {
final long var5 = this.lastBufferPlayTime + (long) len * (long) var4;
if (this.currentPlayTime - var5 >= 0L) {
this.lastBufferPlayTime = var5;
final long bufferPt = this.outputPt + len * ptPerSample;
if (this.playerPt - bufferPt >= 0L) {
this.outputPt = bufferPt;
break;
}
final int var7 = (int) ((-this.lastBufferPlayTime + (this.currentPlayTime - (-((long) var4) + 1L))) / (long) var4);
this.lastBufferPlayTime += (long) var7 * (long) var4;
this.noteSet.processAndWrite(dataS16P8, offset, var7);
offset += var7;
len -= var7;
final long samples = (this.playerPt - this.outputPt + ptPerSample - 1L) / ptPerSample;
this.outputPt += samples * ptPerSample;
this.noteSet.processAndWrite(dataS16P8, offset, (int) samples);
offset += samples;
len -= samples;
this.pumpEvents();
} while (this.midiReader.isLoaded());
}
@ -196,19 +196,19 @@ public final class MidiPlayer extends AudioSource {
@Override
public synchronized void processAndDiscard(int len) {
if (this.midiReader.isLoaded()) {
final int samplesToPlayTime = this.midiReader.ticksPerQuarterNote * this.microsecondsPerSecond / SampledAudioChannelS16.SAMPLE_RATE;
final long ptPerSample = this.midiReader.ticksPerQuarterNote * this.microsecondsPerSecond / SampledAudioChannelS16.SAMPLE_RATE;
do {
final long bufferPlayTime = (long) len * (long) samplesToPlayTime + this.lastBufferPlayTime;
if (this.currentPlayTime - bufferPlayTime >= 0L) {
this.lastBufferPlayTime = bufferPlayTime;
final long bufferPt = len * ptPerSample + this.outputPt;
if (this.playerPt - bufferPt >= 0L) {
this.outputPt = bufferPt;
break;
}
final int var5 = (int) ((-1L - this.lastBufferPlayTime + this.currentPlayTime + (long) samplesToPlayTime) / (long) samplesToPlayTime);
this.lastBufferPlayTime += (long) var5 * (long) samplesToPlayTime;
this.noteSet.processAndDiscard(var5);
len -= var5;
final long samples = (this.playerPt - this.outputPt + ptPerSample - 1) / ptPerSample;
this.outputPt += samples * ptPerSample;
this.noteSet.processAndDiscard((int) samples);
len -= samples;
this.pumpEvents();
} while (this.midiReader.isLoaded());
}
@ -231,8 +231,8 @@ public final class MidiPlayer extends AudioSource {
for (final MidiPlayerNoteState_idk note : this.noteSet.notes) {
if (channel < 0 || channel == note.channel) {
if (note.playback != null) {
note.playback.g150(SampledAudioChannelS16.SAMPLE_RATE / 100);
if (note.playback.volDeltaNonZero()) {
note.playback.setVolZeroRamped(SampledAudioChannelS16.SAMPLE_RATE / 100);
if (note.playback.isRampTimeNonzero()) {
this.noteSet.sum.addFirst(note.playback);
}
@ -498,7 +498,7 @@ public final class MidiPlayer extends AudioSource {
this.reset(doSoundOff);
this.midiReader.load(songData.midiData);
this.looped = looped;
this.lastBufferPlayTime = 0L;
this.outputPt = 0L;
final int numTracks = this.midiReader.numTracks();
for (int track = 0; track < numTracks; ++track) {
@ -509,7 +509,7 @@ public final class MidiPlayer extends AudioSource {
this.trackWithSoonestEvent = this.midiReader.trackWithSoonestNextTick();
this.nextEventTicks = this.midiReader.trackNextTick[this.trackWithSoonestEvent];
this.currentPlayTime = this.midiReader.getPlayTime(this.nextEventTicks);
this.playerPt = this.midiReader.getPlayTime(this.nextEventTicks);
}
private void handleGeneral6Off(final int var2) {
@ -590,7 +590,7 @@ public final class MidiPlayer extends AudioSource {
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public boolean a543(final int var1, final int[] var2, final MidiPlayerNoteState_idk var4, final int var5) {
var4._p = SampledAudioChannelS16.SAMPLE_RATE / 100;
var4.lenRemaining_idk = SampledAudioChannelS16.SAMPLE_RATE / 100;
if (var4.notePlaying_idfk < 0 || var4.playback != null && !var4.playback.isPlayheadOutOfBounds()) {
int var6 = var4._pitch_fac_2;
if (var6 > 0) {
@ -602,7 +602,7 @@ public final class MidiPlayer extends AudioSource {
var4._pitch_fac_2 = var6;
}
var4.playback.setPitchX(this.calcPitchX_idk(var4));
var4.playback.setAbsSpeed_p8(this.calcPitchX_idk(var4));
final KeyParams_idk var7 = var4.keyParams_idk;
var4.vibratoPhase_idk += var7.vibratoPhaseSpeed_idk;
++var4._C;
@ -653,14 +653,14 @@ public final class MidiPlayer extends AudioSource {
}
if (var8) {
var4.playback.g150(var4._p);
var4.playback.setVolZeroRamped(var4.lenRemaining_idk);
if (var2 == null) {
var4.playback.processAndDiscard(var5);
} else {
var4.playback.processAndWrite(var2, var1, var5);
}
if (var4.playback.volDeltaNonZero()) {
if (var4.playback.isRampTimeNonzero()) {
this.noteSet.sum.addFirst(var4.playback);
}
@ -675,7 +675,7 @@ public final class MidiPlayer extends AudioSource {
return true;
} else {
var4.playback.a326(var4._p, this.calcVolumeX_idk(var4), this.calcPanX_idk(var4));
var4.playback.setVolAndPanRamped_p14(var4.lenRemaining_idk, this.calcVolumeX_idk(var4), this.calcPanX_idk(var4));
return false;
}
} else {
@ -708,7 +708,7 @@ public final class MidiPlayer extends AudioSource {
private void pumpEvents() {
int track = this.trackWithSoonestEvent;
int ticks = this.nextEventTicks;
long playTime = this.currentPlayTime;
long playTime = this.playerPt;
if (this.songData != null && ticks == 0) {
this.changeSong(this.looped, this.songData, false);
@ -755,11 +755,11 @@ public final class MidiPlayer extends AudioSource {
this.trackWithSoonestEvent = track;
this.nextEventTicks = ticks;
this.currentPlayTime = playTime;
this.playerPt = playTime;
if (this.songData != null && ticks > 0) {
this.trackWithSoonestEvent = -1;
this.nextEventTicks = 0;
this.currentPlayTime = this.midiReader.getPlayTime(this.nextEventTicks);
this.playerPt = this.midiReader.getPlayTime(this.nextEventTicks);
}
}
@ -774,7 +774,7 @@ public final class MidiPlayer extends AudioSource {
playhead = (int) ((long) this.chGeneral1[note.channel] * (long) var6 >> 6);
sampleLength <<= 8;
if (playhead >= sampleLength) {
note.playback.setPitchXNegAbs_idk();
note.playback.setBackwards();
playhead = -playhead + sampleLength + sampleLength - 1;
}
} else {

View File

@ -34,14 +34,14 @@ public final class MidiPlayerNoteSet_idk extends AudioSource {
int var2 = len;
do {
if (note._p >= var2) {
if (note.lenRemaining_idk >= var2) {
this.a222(note, var2);
note._p -= var2;
note.lenRemaining_idk -= var2;
break;
}
this.a222(note, note._p);
var2 -= note._p;
this.a222(note, note.lenRemaining_idk);
var2 -= note.lenRemaining_idk;
} while (!this.midiPlayer.a543(0, null, note, var2));
}
}
@ -49,26 +49,28 @@ public final class MidiPlayerNoteSet_idk extends AudioSource {
}
@Override
public void processAndWrite(final int[] dataS16P8, final int offset, final int len) {
this.sum.processAndWrite(dataS16P8, offset, len);
public void processAndWrite(final int[] dataS16P8, final int totalOffset, final int totalLen) {
this.sum.processAndWrite(dataS16P8, totalOffset, totalLen);
for (final MidiPlayerNoteState_idk note : this.notes) {
if (!this.midiPlayer.a258(note)) {
int var4 = offset;
int var5 = len;
do {
if (note._p >= var5) {
this.a829(dataS16P8, var4, note, var5, var4 + var5);
note._p -= var5;
break;
}
this.a829(dataS16P8, var4, note, note._p, var4 + var5);
var5 -= note._p;
var4 += note._p;
} while (!this.midiPlayer.a543(var4, dataS16P8, note, var5));
if (this.midiPlayer.a258(note)) {
continue;
}
int offset = totalOffset;
int len = totalLen;
do {
if (note.lenRemaining_idk >= len) {
this.a829(dataS16P8, offset, note, len, offset + len);
note.lenRemaining_idk -= len;
break;
}
this.a829(dataS16P8, offset, note, note.lenRemaining_idk, offset + len);
len -= note.lenRemaining_idk;
offset += note.lenRemaining_idk;
} while (!this.midiPlayer.a543(offset, dataS16P8, note, len));
}
}
@ -80,9 +82,9 @@ public final class MidiPlayerNoteSet_idk extends AudioSource {
note._j = 1048575 & note._j + totalLen * var4;
if (var5 <= totalLen) {
if (this.midiPlayer.chGeneral1[note.channel] == 0) {
note.playback = RawSamplePlayer.start(note.sampleData, note.playback.getPitchX(), note.playback.getVol_p14(), note.playback.getPan_p14());
note.playback = RawSamplePlayer.start(note.sampleData, note.playback.getSpeed_p8(), note.playback.getVol_p14(), note.playback.getPan_p14());
} else {
note.playback = RawSamplePlayer.start(note.sampleData, note.playback.getPitchX(), 0, note.playback.getPan_p14());
note.playback = RawSamplePlayer.start(note.sampleData, note.playback.getSpeed_p8(), 0, note.playback.getPan_p14());
this.midiPlayer.a559(note, note.instrument.noteTuning_idk[note.note] < 0);
}
@ -99,21 +101,21 @@ public final class MidiPlayerNoteSet_idk extends AudioSource {
note.playback.processAndDiscard(totalLen);
}
private void a829(final int[] dest, int offset, final MidiPlayerNoteState_idk note, int totalLen, final int var6) {
private void a829(final int[] dest, int offset, final MidiPlayerNoteState_idk note, int lenToWrite, final int totalLen) {
if ((MidiPlayer.FLAG_GENERAL_6 & this.midiPlayer.chFlags[note.channel]) != 0 && note.notePlaying_idfk < 0) {
final int var7 = this.midiPlayer._u[note.channel] / SampledAudioChannelS16.SAMPLE_RATE;
while (true) {
final int len = (-note._j + 0xfffff + var7) / var7;
if (len > totalLen) {
note._j += var7 * totalLen;
if (len > lenToWrite) {
note._j += var7 * lenToWrite;
break;
}
note.playback.processAndWrite(dest, offset, len);
note._j += len * var7 - 0x100000;
offset += len;
totalLen -= len;
lenToWrite -= len;
int var9 = SampledAudioChannelS16.SAMPLE_RATE / 100;
final int var10 = 0x40000 / var7;
if (var10 < var9) {
@ -122,11 +124,11 @@ public final class MidiPlayerNoteSet_idk extends AudioSource {
final RawSamplePlayer playback = note.playback;
if (this.midiPlayer.chGeneral1[note.channel] == 0) {
note.playback = RawSamplePlayer.start(note.sampleData, playback.getPitchX(), playback.getVol_p14(), playback.getPan_p14());
note.playback = RawSamplePlayer.start(note.sampleData, playback.getSpeed_p8(), playback.getVol_p14(), playback.getPan_p14());
} else {
note.playback = RawSamplePlayer.start(note.sampleData, playback.getPitchX(), 0, playback.getPan_p14());
note.playback = RawSamplePlayer.start(note.sampleData, playback.getSpeed_p8(), 0, playback.getPan_p14());
this.midiPlayer.a559(note, note.instrument.noteTuning_idk[note.note] < 0);
note.playback.a093(var9, playback.getVol_p14());
note.playback.setVolRamped_p14(var9, playback.getVol_p14());
}
if (note.instrument.noteTuning_idk[note.note] < 0) {
@ -134,14 +136,14 @@ public final class MidiPlayerNoteSet_idk extends AudioSource {
note.playback.f150();
}
playback.g150(var9);
playback.processAndWrite(dest, offset, var6 - offset);
if (playback.volDeltaNonZero()) {
playback.setVolZeroRamped(var9);
playback.processAndWrite(dest, offset, totalLen - offset);
if (playback.isRampTimeNonzero()) {
this.sum.addFirst(playback);
}
}
}
note.playback.processAndWrite(dest, offset, totalLen);
note.playback.processAndWrite(dest, offset, lenToWrite);
}
}

View File

@ -22,7 +22,7 @@ public final class MidiPlayerNoteState_idk extends NodeList.Node {
public int _h;
public int noteOffNote_idk;
public int volume_idk;
public int _p;
public int lenRemaining_idk;
public int _B;
public int vibratoPhase_idk;

File diff suppressed because it is too large Load Diff