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

fix: oversight in SoftwareMixer loop #694

Merged
merged 1 commit into from
May 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 3 additions & 25 deletions src/Spice86.Core/Emulator/Devices/Sound/SoftwareMixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,7 @@ internal int Render(Span<float> data, SoundChannel channel) {

Span<float> target = stackalloc float[data.Length];
for (int i = 0; i < data.Length; i++) {
// Apply volume and separation to left channel
target[i] = data[i] * finalVolumeFactor;
// Ensure we don't go out of range
if (i + 1 >= data.Length) {
break;
}
// Apply volume and separation to right channel
target[i + 1] = data[i] * finalVolumeFactor;

}
return _channels[channel].WriteData(target);
}
Expand All @@ -66,15 +58,8 @@ internal int Render(Span<short> data, SoundChannel channel) {
float finalVolumeFactor = volumeFactor * (1 + separation);

Span<float> target = stackalloc float[data.Length];
for (int i = 0; i < data.Length; i += 2) {
// Apply volume and separation to left channel
for (int i = 0; i < data.Length; i++) {
target[i] = (data[i] / 32768f) * finalVolumeFactor;
// Ensure we don't go out of range
if (i + 1 >= data.Length) {
break;
}
// Apply volume and separation to right channel
target[i + 1] = (data[i] / 32768f) * finalVolumeFactor;
}

return _channels[channel].WriteData(target);
Expand All @@ -90,15 +75,8 @@ internal int Render(Span<byte> data, SoundChannel channel) {
float finalVolumeFactor = volumeFactor * (1 + separation);

Span<float> target = stackalloc float[data.Length];
for (int i = 0; i < data.Length; i += 2) {
// Apply volume and separation to left channel
for (int i = 0; i < data.Length; i++) {
target[i] = ((data[i] - 127) / 128f) * finalVolumeFactor;
// Ensure we don't go out of range
if (i + 1 >= data.Length) {
break;
}
// Apply volume and separation to right channel
target[i + 1] = ((data[i + 1] - 127) / 128f) * finalVolumeFactor;
}
return _channels[channel].WriteData(target);
}
Expand All @@ -124,4 +102,4 @@ public void Dispose() {
public void Accept<T>(T emulatorDebugger) where T : IInternalDebugger {
emulatorDebugger.Visit(this);
}
}
}
Loading