Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small fix for game load crashes on Windows #7

Open
IcyChills opened this issue Oct 28, 2024 · 0 comments
Open

Small fix for game load crashes on Windows #7

IcyChills opened this issue Oct 28, 2024 · 0 comments

Comments

@IcyChills
Copy link

A fix for crash caused when loading save games on Windows under Doom 3. Found in snd_emitter.cpp. The issue is also in the fhDoom source. Just wanted to share, feel free to close the issue.

/*
===================
idSoundChannel::GatherChannelSamples

Will always return 44kHz samples for the given range, even if it deeply looped or
out of the range of the unlooped samples. Handles looping between multiple different
samples and leadins
===================
*/
void idSoundChannel::GatherChannelSamples(int sampleOffset44k, int sampleCount44k, float *dest) const {
	float *dest_p = dest;
	int len;

	// Check for null decoder
	if (!decoder) {
		// Fill destination with zeroes if decoder is null
		memset(dest_p, 0, sampleCount44k * sizeof(dest_p[0]));
		return;
	}

	// Negative offset times will zero-fill
	if (sampleOffset44k < 0) {
		len = -sampleOffset44k;
		if (len > sampleCount44k) {
			len = sampleCount44k;
		}
		memset(dest_p, 0, len * sizeof(dest_p[0]));
		dest_p += len;
		sampleCount44k -= len;
		sampleOffset44k += len;
	}

	// Handle lead-in sample
	idSoundSample *leadin = leadinSample;
	if (!leadin || sampleOffset44k < 0 || sampleCount44k <= 0) {
		memset(dest_p, 0, sampleCount44k * sizeof(dest_p[0]));
		return;
	}

	if (sampleOffset44k < leadin->LengthIn44kHzSamples()) {
		len = leadin->LengthIn44kHzSamples() - sampleOffset44k;
		if (len > sampleCount44k) {
			len = sampleCount44k;
		}

		// Decode the sample if decoder is valid
		decoder->Decode(leadin, sampleOffset44k, len, dest_p);
		dest_p += len;
		sampleCount44k -= len;
		sampleOffset44k += len;
	}

	// Zero-fill if not looping
	if (!soundShader || !(parms.soundShaderFlags & SSF_LOOPING)) {
		memset(dest_p, 0, sampleCount44k * sizeof(dest_p[0]));
		return;
	}

	// Looping sample handling
	idSoundSample *loop = soundShader->entries[0];
	if (!loop) {
		memset(dest_p, 0, sampleCount44k * sizeof(dest_p[0]));
		return;
	}

	sampleOffset44k -= leadin->LengthIn44kHzSamples();

	while (sampleCount44k > 0) {
		int totalLen = loop->LengthIn44kHzSamples();
		sampleOffset44k %= totalLen;

		len = totalLen - sampleOffset44k;
		if (len > sampleCount44k) {
			len = sampleCount44k;
		}

		// Decode the loop sample
		decoder->Decode(loop, sampleOffset44k, len, dest_p);

		dest_p += len;
		sampleCount44k -= len;
		sampleOffset44k += len;
	}
}
@IcyChills IcyChills changed the title Small fix to for game load crashes on Windows Small fix for game load crashes on Windows Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant