Skip to content

Commit

Permalink
fix: oversight in SoftwareMixer loop (#694)
Browse files Browse the repository at this point in the history
Signed-off-by: Maximilien Noal <noal.maximilien@gmail.com>
  • Loading branch information
maximilien-noal authored May 31, 2024
1 parent 2c1109a commit f3d8354
Showing 1 changed file with 3 additions and 25 deletions.
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);
}
}
}

0 comments on commit f3d8354

Please sign in to comment.