From 71a7840a2d2b2d117b1567ae0f71cb1a60195fb2 Mon Sep 17 00:00:00 2001 From: VoidX Date: Sat, 4 May 2024 22:54:48 +0200 Subject: [PATCH] Correct block size handling for downmixed render targets --- Cavern.Format/Output/AudioWriter.cs | 6 +++++- CavernSamples/CavernizeGUI/MainWindow.Exporting.cs | 4 ++-- CavernSamples/CavernizeGUI/MainWindow.Rendering.cs | 10 ++++------ 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Cavern.Format/Output/AudioWriter.cs b/Cavern.Format/Output/AudioWriter.cs index f46e68c0..be48375e 100644 --- a/Cavern.Format/Output/AudioWriter.cs +++ b/Cavern.Format/Output/AudioWriter.cs @@ -1,6 +1,7 @@ using System; using System.IO; +using Cavern.Format.Common; using Cavern.Format.Consts; using Cavern.Format.Container; @@ -114,7 +115,10 @@ internal static Stream Open(string path) => /// Channel count of the input array /// Start position in the input array (inclusive) /// End position in the input array (exclusive) - /// This function is destructive, will be unusable after the operation. + /// This function is destructive, will be unusable after the operation. + /// Also, make sure when this is writing into a , its + /// is set correctly - not to a multiple of , + /// but . 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) { diff --git a/CavernSamples/CavernizeGUI/MainWindow.Exporting.cs b/CavernSamples/CavernizeGUI/MainWindow.Exporting.cs index fe292d79..5c9ec1eb 100644 --- a/CavernSamples/CavernizeGUI/MainWindow.Exporting.cs +++ b/CavernSamples/CavernizeGUI/MainWindow.Exporting.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.IO; using System.Numerics; using System.Threading; @@ -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) { diff --git a/CavernSamples/CavernizeGUI/MainWindow.Rendering.cs b/CavernSamples/CavernizeGUI/MainWindow.Rendering.cs index 85b19f09..ad25184f 100644 --- a/CavernSamples/CavernizeGUI/MainWindow.Rendering.cs +++ b/CavernSamples/CavernizeGUI/MainWindow.Rendering.cs @@ -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; @@ -195,7 +195,7 @@ Action GetRenderTask() { } } } else { - SetBlockSize(); + SetBlockSize((RenderTarget)renderTarget.SelectedItem); try { return () => RenderTask(target, null, false, false, null); } catch (Exception e) { @@ -209,7 +209,7 @@ Action GetRenderTask() { /// /// Setup write cache block size depending on active settings. /// - void SetBlockSize() { + void SetBlockSize(RenderTarget target) { blockSize = FiltersUsed ? roomCorrection[0].Length : defaultWriteCacheLength; if (blockSize < listener.UpdateRate) { blockSize = listener.UpdateRate; @@ -217,9 +217,7 @@ void SetBlockSize() { // 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; } ///