Skip to content
Permalink
Browse files

[Audio] Encode little bit longer to compensate for encoding delay (fixes

  • Loading branch information
xen2 committed Apr 9, 2019
1 parent 41f9389 commit 83e0c19f62b164f23d24866f1b0e587686850a6b
Showing with 12 additions and 4 deletions.
  1. +12 −4 sources/engine/Xenko.Assets/Media/SoundAssetCompiler.cs
@@ -34,7 +34,7 @@ protected class DecodeSoundFileCommand : AssetCommand<SoundAsset>
public DecodeSoundFileCommand(string url, SoundAsset parameters, IAssetFinder assetFinder)
: base(url, parameters, assetFinder)
{
Version = 2;
Version = 3;
}

/// <inheritdoc />
@@ -81,6 +81,8 @@ protected override async Task<ResultStatus> DoCommandOverride(ICommandContext co
//make sure we don't compress celt data
commandContext.AddTag(new ObjectUrl(UrlType.Content, dataUrl), Builder.DoNotCompressTag);

var delay = encoder.GetDecoderSampleDelay();

var frameSize = CompressedSoundSource.SamplesPerFrame * channels;
using (var reader = new BinaryReader(new FileStream(tempFile, FileMode.Open, FileAccess.Read)))
using (var outputStream = MicrothreadLocalDatabases.DatabaseFileProvider.OpenStream(dataUrl, VirtualFileMode.Create, VirtualFileAccess.Write, VirtualFileShare.Read, StreamFlags.Seekable))
@@ -90,8 +92,9 @@ protected override async Task<ResultStatus> DoCommandOverride(ICommandContext co
var outputBuffer = new byte[target];
var buffer = new float[frameSize];
var count = 0;
var padding = sizeof(float) * channels * delay;
var length = reader.BaseStream.Length; // Cache the length, because this getter is expensive to use
for (var position = 0; position < length; position += sizeof(float))
for (var position = 0; position < length + padding; position += sizeof(float))
{
if (count == frameSize) //flush
{
@@ -107,8 +110,10 @@ protected override async Task<ResultStatus> DoCommandOverride(ICommandContext co
Array.Clear(buffer, 0, frameSize);
}

buffer[count] = reader.ReadSingle();
count++;
// Pad with 0 once we reach end of stream (this is needed because of encoding delay)
buffer[count++] = (position < length)
? reader.ReadSingle()
: 0.0f;
}

if (count > 0) //flush
@@ -123,6 +128,9 @@ protected override async Task<ResultStatus> DoCommandOverride(ICommandContext co
}
}

// Samples is the real sound sample count, remove the delay at the end
newSound.Samples -= delay;

var assetManager = new ContentManager(MicrothreadLocalDatabases.ProviderService);
assetManager.Save(Url, newSound);

0 comments on commit 83e0c19

Please sign in to comment.
You can’t perform that action at this time.