Skip to content

Commit

Permalink
Correct block size handling for downmixed render targets
Browse files Browse the repository at this point in the history
  • Loading branch information
VoidXH committed May 4, 2024
1 parent 0bc3f19 commit 71a7840
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
6 changes: 5 additions & 1 deletion Cavern.Format/Output/AudioWriter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;

using Cavern.Format.Common;
using Cavern.Format.Consts;
using Cavern.Format.Container;

Expand Down Expand Up @@ -114,7 +115,10 @@ internal static Stream Open(string path) =>
/// <param name="channels">Channel count of the input array</param>
/// <param name="from">Start position in the input array (inclusive)</param>
/// <param name="to">End position in the input array (exclusive)</param>
/// <remarks>This function is destructive, <paramref name="samples"/> will be unusable after the operation.</remarks>
/// <remarks>This function is destructive, <paramref name="samples"/> will be unusable after the operation.
/// Also, make sure when this <see cref="AudioWriter"/> is writing into a <see cref="ContainerWriter"/>, its
/// <see cref="RenderTrack.timeStep"/> is set correctly - not to a multiple of <paramref name="channels"/>,
/// but <paramref name="channelLimit"/>.</remarks>
public virtual void WriteChannelLimitedBlock(float[] samples, int channelLimit, int channels, long from, long to) {
long targetSample = from;
for (long sourceSample = from; sourceSample < to; sourceSample += channels) {
Expand Down
4 changes: 2 additions & 2 deletions CavernSamples/CavernizeGUI/MainWindow.Exporting.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using System.Threading;

Expand Down Expand Up @@ -122,7 +121,8 @@ RenderStats WriteRender(Track target, AudioWriter writer, RenderTarget renderTar
int cachePosition = 0;
float[] writeCache = null;
bool flush = false;
writeCache = new float[blockSize];
// The size of writeCache makes sure ContainerWriters get correct sized blocks when written with WriteChannelLimitedBlock
writeCache = new float[blockSize / renderTarget.OutputChannels * Listener.Channels.Length];

bool wasError = false;
while (progressor.Rendered < target.Length) {
Expand Down
10 changes: 4 additions & 6 deletions CavernSamples/CavernizeGUI/MainWindow.Rendering.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Action Render(string path) {
Codec codec = ((ExportFormat)audio.SelectedItem).Codec;
BitDepth bits = codec == Codec.PCM_Float ? BitDepth.Float32 : force24Bit.IsChecked ? BitDepth.Int24 : BitDepth.Int16;
if (!codec.IsEnvironmental()) {
SetBlockSize();
SetBlockSize(activeRenderTarget);
string exportFormat = path[^4..].ToLower(CultureInfo.InvariantCulture);
bool mkvTarget = exportFormat.Equals(".mkv");
string exportName = mkvTarget ? path[..^4] + waveExtension : path;
Expand Down Expand Up @@ -195,7 +195,7 @@ Action GetRenderTask() {
}
}
} else {
SetBlockSize();
SetBlockSize((RenderTarget)renderTarget.SelectedItem);
try {
return () => RenderTask(target, null, false, false, null);
} catch (Exception e) {
Expand All @@ -209,17 +209,15 @@ Action GetRenderTask() {
/// <summary>
/// Setup write cache block size depending on active settings.
/// </summary>
void SetBlockSize() {
void SetBlockSize(RenderTarget target) {
blockSize = FiltersUsed ? roomCorrection[0].Length : defaultWriteCacheLength;
if (blockSize < listener.UpdateRate) {
blockSize = listener.UpdateRate;
} else if (blockSize % listener.UpdateRate != 0) {
// Cache handling is written to only handle when its size is divisible with the update rate - it's faster this way
blockSize += listener.UpdateRate - blockSize % listener.UpdateRate;
}

bool virtualizerFilterUsed = Listener.HeadphoneVirtualizer || speakerVirtualizer.IsChecked;
blockSize *= virtualizerFilterUsed ? VirtualizerFilter.VirtualChannels : Listener.Channels.Length;
blockSize *= target.OutputChannels;
}

/// <summary>
Expand Down

0 comments on commit 71a7840

Please sign in to comment.