obj.getaudioを両エンジンへ配線しテストを追加
このコミットが含まれているのは:
6ファイル変更
+479
-2
検索条件に一致するファイルはありません
@@ -79,6 +79,7 @@ namespace LuaScript.Engine
|
||||
Action<string, int, bool, int, double[]> setAnchor,
|
||||
Func<string, SceneValue> sceneGetValue,
|
||||
Action<string, SceneValue> sceneSetValue,
|
||||
Func<string, string, int, (int Count, int Rate, double[] Data)> loadAudio,
|
||||
PixelShaderInvoke? runPixelShader,
|
||||
out bool pixelsDirty,
|
||||
out bool bufferReplaced,
|
||||
@@ -163,7 +164,7 @@ namespace LuaScript.Engine
|
||||
}
|
||||
else
|
||||
{
|
||||
DispatchCallback(view, resolveObject, loadFigure, loadText, loadImage, loadMovie, loadScene, loadBrush, addEffect, setAnchor, sceneGetValue, sceneSetValue, runPixelShader);
|
||||
DispatchCallback(view, resolveObject, loadFigure, loadText, loadImage, loadMovie, loadScene, loadBrush, addEffect, setAnchor, sceneGetValue, sceneSetValue, loadAudio, runPixelShader);
|
||||
}
|
||||
_workEvent.Set();
|
||||
}
|
||||
@@ -330,6 +331,7 @@ namespace LuaScript.Engine
|
||||
Action<string, int, bool, int, double[]> setAnchor,
|
||||
Func<string, SceneValue> sceneGetValue,
|
||||
Action<string, SceneValue> sceneSetValue,
|
||||
Func<string, string, int, (int Count, int Rate, double[] Data)> loadAudio,
|
||||
PixelShaderInvoke? runPixelShader)
|
||||
{
|
||||
int kind = view.ReadInt32(NativeProtocol.OffCallbackKind);
|
||||
@@ -371,6 +373,9 @@ namespace LuaScript.Engine
|
||||
case NativeProtocol.CbKindSceneSet:
|
||||
ResolveSceneSetCallback(view, sceneSetValue);
|
||||
break;
|
||||
case NativeProtocol.CbKindGetAudio:
|
||||
ResolveGetAudioCallback(view, loadAudio);
|
||||
break;
|
||||
case NativeProtocol.CbKindPixelShaderStage:
|
||||
ResolveShaderStageCallback(view);
|
||||
break;
|
||||
@@ -571,6 +576,40 @@ namespace LuaScript.Engine
|
||||
view.Write(NativeProtocol.OffCallbackFound, 1);
|
||||
}
|
||||
|
||||
private static readonly double[] s_noAudioData = [];
|
||||
|
||||
private void ResolveGetAudioCallback(
|
||||
MemoryMappedViewAccessor view,
|
||||
Func<string, string, int, (int Count, int Rate, double[] Data)> loadAudio)
|
||||
{
|
||||
int tagLen = Math.Clamp(view.ReadInt32(NativeProtocol.OffCallbackTagLen), 0, NativeProtocol.CallbackTagMax);
|
||||
view.ReadArray(NativeProtocol.CallbackTagOffset, _callbackTag, 0, tagLen);
|
||||
int separator = Array.IndexOf(_callbackTag, (byte)0, 0, tagLen);
|
||||
string file = Encoding.UTF8.GetString(_callbackTag, 0, separator >= 0 ? separator : tagLen);
|
||||
string type = separator >= 0
|
||||
? Encoding.UTF8.GetString(_callbackTag, separator + 1, tagLen - separator - 1)
|
||||
: "pcm";
|
||||
int size = (int)view.ReadDouble(NativeProtocol.CallbackResultOffset);
|
||||
|
||||
int count = 0;
|
||||
int rate = 0;
|
||||
double[] data = s_noAudioData;
|
||||
try { (count, rate, data) = loadAudio(file, type, size); }
|
||||
catch
|
||||
{
|
||||
count = 0;
|
||||
rate = 0;
|
||||
data = s_noAudioData;
|
||||
}
|
||||
|
||||
count = Math.Clamp(count, 0, Math.Min(data.Length, NativeProtocol.CallbackTagMax / 8));
|
||||
if (count > 0)
|
||||
view.WriteArray(NativeProtocol.CallbackTagOffset, data, 0, count);
|
||||
view.Write(NativeProtocol.CallbackResultOffset, (double)count);
|
||||
view.Write(NativeProtocol.CallbackResultOffset + 8, (double)rate);
|
||||
view.Write(NativeProtocol.OffCallbackFound, 1);
|
||||
}
|
||||
|
||||
private void ResolveSetAnchorCallback(MemoryMappedViewAccessor view, Action<string, int, bool, int, double[]> setAnchor)
|
||||
{
|
||||
int tagLen = Math.Clamp(view.ReadInt32(NativeProtocol.OffCallbackTagLen), 0, NativeProtocol.CallbackTagMax);
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
using LuaScript.Compat;
|
||||
|
||||
namespace LuaScript.Tests
|
||||
{
|
||||
public sealed class AviUtlAudioConverterTests
|
||||
{
|
||||
private static float[] Interleave(double[] left, double[] right)
|
||||
{
|
||||
var data = new float[left.Length * 2];
|
||||
for (int i = 0; i < left.Length; i++)
|
||||
{
|
||||
data[i * 2] = (float)left[i];
|
||||
data[i * 2 + 1] = (float)right[i];
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RequiredFrames_MatchesTypeWindows()
|
||||
{
|
||||
Assert.Equal(16, AviUtlAudioConverter.RequiredFrames("pcm", 16));
|
||||
Assert.Equal(AviUtlAudioConverter.MaxSamples, AviUtlAudioConverter.RequiredFrames("pcm", 0));
|
||||
Assert.Equal(AviUtlAudioConverter.MaxSamples, AviUtlAudioConverter.RequiredFrames("pcm", 99999));
|
||||
Assert.Equal(AviUtlAudioConverter.SpectrumWindow, AviUtlAudioConverter.RequiredFrames("spectrum", 32));
|
||||
Assert.Equal(AviUtlAudioConverter.FourierWindow, AviUtlAudioConverter.RequiredFrames("fourier", 0));
|
||||
Assert.Equal(AviUtlAudioConverter.FourierWindow, AviUtlAudioConverter.RequiredFrames("fourier.l", 0));
|
||||
Assert.Equal(0, AviUtlAudioConverter.RequiredFrames("unknown", 8));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Pcm_ScalesMonoMixTo16BitRange()
|
||||
{
|
||||
var converter = new AviUtlAudioConverter();
|
||||
var data = Interleave([0.5d, -1d, 0d], [0.5d, -1d, 1d]);
|
||||
var dest = new double[AviUtlAudioConverter.MaxSamples];
|
||||
|
||||
int count = converter.Convert("pcm", data, 3, 3, dest);
|
||||
|
||||
Assert.Equal(3, count);
|
||||
Assert.Equal(16384d, dest[0]);
|
||||
Assert.Equal(-32768d, dest[1]);
|
||||
Assert.Equal(16384d, dest[2]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Pcm_SelectsChannelBySuffix()
|
||||
{
|
||||
var converter = new AviUtlAudioConverter();
|
||||
var data = Interleave([0.25d, 0.25d], [-0.5d, -0.5d]);
|
||||
var dest = new double[AviUtlAudioConverter.MaxSamples];
|
||||
|
||||
converter.Convert("pcm.l", data, 2, 2, dest);
|
||||
Assert.Equal(8192d, dest[0]);
|
||||
|
||||
converter.Convert("pcm.r", data, 2, 2, dest);
|
||||
Assert.Equal(-16384d, dest[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Pcm_ReturnsFewerWhenSourceIsShort()
|
||||
{
|
||||
var converter = new AviUtlAudioConverter();
|
||||
var data = Interleave([0.1d, 0.2d], [0.1d, 0.2d]);
|
||||
var dest = new double[AviUtlAudioConverter.MaxSamples];
|
||||
|
||||
int count = converter.Convert("pcm", data, 2, 100, dest);
|
||||
|
||||
Assert.Equal(2, count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Spectrum_PeaksInBandContainingSineFrequency()
|
||||
{
|
||||
var converter = new AviUtlAudioConverter();
|
||||
const int frames = AviUtlAudioConverter.SpectrumWindow;
|
||||
const int cycle = 64;
|
||||
var mono = new double[frames];
|
||||
for (int i = 0; i < frames; i++)
|
||||
mono[i] = Math.Sin(2d * Math.PI * i * cycle / frames);
|
||||
var data = Interleave(mono, mono);
|
||||
var dest = new double[AviUtlAudioConverter.MaxSamples];
|
||||
|
||||
int count = converter.Convert("spectrum", data, frames, 32, dest);
|
||||
|
||||
Assert.Equal(32, count);
|
||||
int expectedBand = cycle * 32 / AviUtlAudioConverter.SpectrumBins;
|
||||
int peakBand = 0;
|
||||
for (int i = 1; i < count; i++)
|
||||
{
|
||||
if (dest[i] > dest[peakBand])
|
||||
peakBand = i;
|
||||
}
|
||||
Assert.Equal(expectedBand, peakBand);
|
||||
Assert.True(dest[peakBand] > 1000d);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Fourier_ReturnsNormalizedMagnitudes()
|
||||
{
|
||||
var converter = new AviUtlAudioConverter();
|
||||
const int frames = AviUtlAudioConverter.FourierWindow;
|
||||
const int cycle = 8;
|
||||
var mono = new double[frames];
|
||||
for (int i = 0; i < frames; i++)
|
||||
mono[i] = Math.Sin(2d * Math.PI * i * cycle / frames);
|
||||
var data = Interleave(mono, mono);
|
||||
var dest = new double[AviUtlAudioConverter.MaxSamples];
|
||||
|
||||
int count = converter.Convert("fourier", data, frames, 0, dest);
|
||||
|
||||
Assert.Equal(AviUtlAudioConverter.MaxSamples, count);
|
||||
int peak = 0;
|
||||
for (int i = 1; i < count; i++)
|
||||
{
|
||||
if (dest[i] > dest[peak])
|
||||
peak = i;
|
||||
}
|
||||
Assert.Equal(cycle - 1, peak);
|
||||
Assert.Equal(1d, dest[peak], 3);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UnknownTypeOrEmptySource_YieldsZero()
|
||||
{
|
||||
var converter = new AviUtlAudioConverter();
|
||||
var dest = new double[AviUtlAudioConverter.MaxSamples];
|
||||
|
||||
Assert.Equal(0, converter.Convert("waveform", new float[8], 4, 4, dest));
|
||||
Assert.Equal(0, converter.Convert("pcm", [], 0, 4, dest));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -48,7 +48,7 @@ namespace LuaScript.Tests
|
||||
"obj.getpixeldata", "obj.putpixeldata", "obj.rand", "obj.load",
|
||||
"obj.setfont", "obj.draw", "obj.drawpoly", "obj.copybuffer",
|
||||
"obj.pixelshader",
|
||||
"obj.getvalue", "obj.setoption", "obj.getoption", "obj.pixeloption",
|
||||
"obj.getvalue", "obj.getaudio", "obj.setoption", "obj.getoption", "obj.pixeloption",
|
||||
"obj.setanchor", "obj.effect", "obj.brush", "obj.getinfo"
|
||||
]),
|
||||
("math", [
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
using LuaScript;
|
||||
|
||||
namespace LuaScript.Tests
|
||||
{
|
||||
public sealed class LuaScriptEngineGetAudioTests
|
||||
{
|
||||
private sealed class StubMediaLoader : IMediaSourceLoader
|
||||
{
|
||||
public byte[] RenderText(string text, string fontFamily, double fontSize, bool bold, bool italic, int colorRgb, out int width, out int height)
|
||||
{
|
||||
width = 1;
|
||||
height = 1;
|
||||
return new byte[4];
|
||||
}
|
||||
|
||||
public byte[] DecodeImage(string path, out int width, out int height)
|
||||
{
|
||||
width = 1;
|
||||
height = 1;
|
||||
return new byte[4];
|
||||
}
|
||||
|
||||
public byte[] DecodeMovie(string path, double time, out int width, out int height)
|
||||
{
|
||||
width = 1;
|
||||
height = 1;
|
||||
return new byte[4];
|
||||
}
|
||||
|
||||
public void Dispose() { }
|
||||
}
|
||||
|
||||
private static LuaScriptEngine CreateEngine() => new(static () => new StubMediaLoader());
|
||||
|
||||
private static AviUtlScriptContext NewContext()
|
||||
{
|
||||
return new AviUtlScriptContext
|
||||
{
|
||||
ImageWidth = 2,
|
||||
ImageHeight = 2,
|
||||
Framerate = 30,
|
||||
};
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetAudio_FillsBufferTableFromLoader()
|
||||
{
|
||||
using var engine = CreateEngine();
|
||||
var ctx = NewContext();
|
||||
|
||||
string? capturedFile = null;
|
||||
string? capturedType = null;
|
||||
int capturedSize = -1;
|
||||
ctx.AudioLoader = (file, type, size) =>
|
||||
{
|
||||
capturedFile = file;
|
||||
capturedType = type;
|
||||
capturedSize = size;
|
||||
return (3, 44100, [1.5d, -2.5d, 3d]);
|
||||
};
|
||||
|
||||
engine.Execute(
|
||||
"local buf = {}\n" +
|
||||
"local n, rate = obj.getaudio(buf, 'audiobuffer', 'pcm', 3)\n" +
|
||||
"obj.x = n\n" +
|
||||
"obj.y = rate\n" +
|
||||
"obj.z = buf[1]\n" +
|
||||
"obj.ox = buf[2]\n" +
|
||||
"obj.oy = buf[3]",
|
||||
ctx);
|
||||
|
||||
Assert.Equal("audiobuffer", capturedFile);
|
||||
Assert.Equal("pcm", capturedType);
|
||||
Assert.Equal(3, capturedSize);
|
||||
Assert.Equal(3d, ctx.X);
|
||||
Assert.Equal(44100d, ctx.Y);
|
||||
Assert.Equal(1.5d, ctx.Z);
|
||||
Assert.Equal(-2.5d, ctx.Ox);
|
||||
Assert.Equal(3d, ctx.Oy);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetAudio_NilBuffer_ReturnsTableAsThirdValue()
|
||||
{
|
||||
using var engine = CreateEngine();
|
||||
var ctx = NewContext();
|
||||
|
||||
ctx.AudioLoader = (_, type, _) => (2, 48000, [7d, 8d]);
|
||||
|
||||
engine.Execute(
|
||||
"local n, rate, buf = obj.getaudio(nil, 'C:/test.wav', 'spectrum.r', 2)\n" +
|
||||
"obj.x = n\n" +
|
||||
"obj.y = rate\n" +
|
||||
"obj.z = buf[1]\n" +
|
||||
"obj.ox = buf[2]",
|
||||
ctx);
|
||||
|
||||
Assert.Equal(2d, ctx.X);
|
||||
Assert.Equal(48000d, ctx.Y);
|
||||
Assert.Equal(7d, ctx.Z);
|
||||
Assert.Equal(8d, ctx.Ox);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetAudio_DefaultsTypeToPcm()
|
||||
{
|
||||
using var engine = CreateEngine();
|
||||
var ctx = NewContext();
|
||||
|
||||
string? capturedType = null;
|
||||
int capturedSize = -1;
|
||||
ctx.AudioLoader = (_, type, size) =>
|
||||
{
|
||||
capturedType = type;
|
||||
capturedSize = size;
|
||||
return (0, 48000, []);
|
||||
};
|
||||
|
||||
engine.Execute("obj.x = obj.getaudio({}, 'audiobuffer')", ctx);
|
||||
|
||||
Assert.Equal("pcm", capturedType);
|
||||
Assert.Equal(0, capturedSize);
|
||||
Assert.Equal(0d, ctx.X);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetAudio_WithoutLoader_ReturnsZero()
|
||||
{
|
||||
using var engine = CreateEngine();
|
||||
var ctx = NewContext();
|
||||
|
||||
engine.Execute(
|
||||
"local n, rate, buf = obj.getaudio(nil, 'audiobuffer', 'pcm', 8)\n" +
|
||||
"obj.x = n\n" +
|
||||
"obj.y = rate\n" +
|
||||
"obj.z = #buf\n" +
|
||||
"obj.ox = obj.getaudio({}, 7)",
|
||||
ctx);
|
||||
|
||||
Assert.Equal(0d, ctx.X);
|
||||
Assert.Equal(0d, ctx.Y);
|
||||
Assert.Equal(0d, ctx.Z);
|
||||
Assert.Equal(0d, ctx.Ox);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetAudio_ClampsCountToDataLength()
|
||||
{
|
||||
using var engine = CreateEngine();
|
||||
var ctx = NewContext();
|
||||
|
||||
ctx.AudioLoader = (_, _, _) => (10, 48000, [5d]);
|
||||
|
||||
engine.Execute(
|
||||
"local n, _, buf = obj.getaudio(nil, 'audiobuffer', 'pcm', 10)\n" +
|
||||
"obj.x = n\n" +
|
||||
"obj.y = #buf",
|
||||
ctx);
|
||||
|
||||
Assert.Equal(1d, ctx.X);
|
||||
Assert.Equal(1d, ctx.Y);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,7 @@ namespace LuaScript.Tests
|
||||
private Func<string, double, double, System.Collections.Generic.IReadOnlyList<System.Collections.Generic.KeyValuePair<string, object>>, (byte[] buffer, int w, int h)> _loadBrush = (_, _, _, _) => ([], 0, 0);
|
||||
private Func<string, SceneValue> _sceneGet = _ => SceneValue.Nil;
|
||||
private Action<string, SceneValue> _sceneSet = (_, _) => { };
|
||||
private Func<string, string, int, (int Count, int Rate, double[] Data)> _loadAudio = (_, _, _) => (0, 0, []);
|
||||
private PixelShaderInvoke? _runPixelShader;
|
||||
|
||||
public void Dispose() => _worker.Dispose();
|
||||
@@ -74,6 +75,7 @@ namespace LuaScript.Tests
|
||||
width, height, timeoutMs,
|
||||
resolveObject, loadFigure, loadText, loadImage, loadMovie, _loadScene, _loadBrush, addEffect, addDraw, setAnchor,
|
||||
_sceneGet, _sceneSet,
|
||||
_loadAudio,
|
||||
_runPixelShader,
|
||||
out pixelsDirty, out bufferReplaced, out resultWidth, out resultHeight, out error);
|
||||
|
||||
@@ -110,6 +112,7 @@ namespace LuaScript.Tests
|
||||
},
|
||||
w, h, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoLoadMovie, _loadScene, _loadBrush, NoAddEffect, NoAddDraw, NoSetAnchor,
|
||||
_sceneGet, _sceneSet,
|
||||
_loadAudio,
|
||||
_runPixelShader,
|
||||
out _, out _, out _, out _, out string? error);
|
||||
|
||||
@@ -1267,6 +1270,79 @@ namespace LuaScript.Tests
|
||||
Assert.Equal(0d, fields[NativeProtocol.Z]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetAudio_RoundTripsThroughCallback()
|
||||
{
|
||||
Assert.True(LuaJitWorker.IsAvailable(NativeDir), "native/luajit.exe must be present");
|
||||
|
||||
string? capturedFile = null;
|
||||
string? capturedType = null;
|
||||
int capturedSize = -1;
|
||||
_loadAudio = (file, type, size) =>
|
||||
{
|
||||
capturedFile = file;
|
||||
capturedType = type;
|
||||
capturedSize = size;
|
||||
return (4, 48000, [10d, -20d, 30.5d, 40d]);
|
||||
};
|
||||
|
||||
var pixels = new byte[16];
|
||||
var fields = Fields(2, 2, 0d);
|
||||
const string script =
|
||||
"local buf = {}\n" +
|
||||
"local n, rate = obj.getaudio(buf, 'audiobuffer', 'pcm', 4)\n" +
|
||||
"obj.x = n\n" +
|
||||
"obj.y = rate\n" +
|
||||
"obj.z = buf[1]\n" +
|
||||
"obj.ox = buf[2]\n" +
|
||||
"obj.oy = buf[3]\n" +
|
||||
"obj.oz = buf[4]\n" +
|
||||
"local n2, rate2, buf2 = obj.getaudio(nil, 'C:/test.wav', 'spectrum.l', 2)\n" +
|
||||
"obj.sx = n2\n" +
|
||||
"obj.sy = buf2[1]";
|
||||
|
||||
bool ok = RunWorker(script, fields, NoStringParams, () => pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoLoadMovie, NoAddEffect, NoAddDraw, NoSetAnchor, out _, out _, out _, out _, out _, out string? error);
|
||||
|
||||
Assert.True(ok, error);
|
||||
Assert.Equal("C:/test.wav", capturedFile);
|
||||
Assert.Equal("spectrum.l", capturedType);
|
||||
Assert.Equal(2, capturedSize);
|
||||
Assert.Equal(4d, fields[NativeProtocol.X]);
|
||||
Assert.Equal(48000d, fields[NativeProtocol.Y]);
|
||||
Assert.Equal(10d, fields[NativeProtocol.Z]);
|
||||
Assert.Equal(-20d, fields[NativeProtocol.Ox]);
|
||||
Assert.Equal(30.5d, fields[NativeProtocol.Oy]);
|
||||
Assert.Equal(40d, fields[NativeProtocol.Oz]);
|
||||
Assert.Equal(4d, fields[NativeProtocol.Sx]);
|
||||
Assert.Equal(10d, fields[NativeProtocol.Sy]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetAudio_FailureYieldsZeroCount()
|
||||
{
|
||||
Assert.True(LuaJitWorker.IsAvailable(NativeDir), "native/luajit.exe must be present");
|
||||
|
||||
_loadAudio = (_, _, _) => throw new InvalidOperationException();
|
||||
|
||||
var pixels = new byte[16];
|
||||
var fields = Fields(2, 2, 0d);
|
||||
const string script =
|
||||
"local n, rate, buf = obj.getaudio(nil, 'audiobuffer', 'pcm', 8)\n" +
|
||||
"obj.x = n\n" +
|
||||
"obj.y = rate\n" +
|
||||
"obj.z = #buf\n" +
|
||||
"local n2 = obj.getaudio({}, 7)\n" +
|
||||
"obj.ox = n2";
|
||||
|
||||
bool ok = RunWorker(script, fields, NoStringParams, () => pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoLoadMovie, NoAddEffect, NoAddDraw, NoSetAnchor, out _, out _, out _, out _, out _, out string? error);
|
||||
|
||||
Assert.True(ok, error);
|
||||
Assert.Equal(0d, fields[NativeProtocol.X]);
|
||||
Assert.Equal(0d, fields[NativeProtocol.Y]);
|
||||
Assert.Equal(0d, fields[NativeProtocol.Z]);
|
||||
Assert.Equal(0d, fields[NativeProtocol.Ox]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CopyBuffer_TmpSaveRestore_RoundTripsPixels()
|
||||
{
|
||||
|
||||
@@ -143,6 +143,9 @@ namespace LuaScript
|
||||
private SceneImageRenderer? _sceneImageRenderer;
|
||||
private BrushImageRenderer? _brushImageRenderer;
|
||||
private bool _sceneImageLoaded;
|
||||
private Func<string, string, int, (int Count, int Rate, double[] Data)>? _loadAudio;
|
||||
private AudioSampleProvider? _audioProvider;
|
||||
private bool _audioLoaded;
|
||||
private Action<string, IReadOnlyList<KeyValuePair<string, object>>>? _nativeAddEffect;
|
||||
private Action<DrawCommand>? _nativeAddDraw;
|
||||
private Action<string, int, bool, int, double[]>? _nativeSetAnchor;
|
||||
@@ -189,6 +192,7 @@ namespace LuaScript
|
||||
_context.Compositor = _bufferCompositor;
|
||||
_context.SceneImageLoader = _loadSceneImage ??= LoadSceneImage;
|
||||
_context.BrushImageLoader = _loadBrushImage ??= LoadBrushImage;
|
||||
_context.AudioLoader = _loadAudio ??= LoadAudio;
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -202,6 +206,7 @@ namespace LuaScript
|
||||
context.Compositor = _bufferCompositor;
|
||||
context.SceneImageLoader = _loadSceneImage ??= LoadSceneImage;
|
||||
context.BrushImageLoader = _loadBrushImage ??= LoadBrushImage;
|
||||
context.AudioLoader = _loadAudio ??= LoadAudio;
|
||||
return context;
|
||||
}
|
||||
|
||||
@@ -483,6 +488,11 @@ namespace LuaScript
|
||||
_sceneImageLoaded = false;
|
||||
_isFirst = true;
|
||||
}
|
||||
if (_audioLoaded)
|
||||
{
|
||||
_audioLoaded = false;
|
||||
_isFirst = true;
|
||||
}
|
||||
_cachedKey = key;
|
||||
_cachedQueries = SnapshotQueries(ctx.ObjectQueries, _cachedQueries);
|
||||
_cachedSceneValueQueries = SnapshotQueries(ctx.SceneValueQueries, _cachedSceneValueQueries);
|
||||
@@ -1028,6 +1038,7 @@ namespace LuaScript
|
||||
_nativeLoadMovie ??= NativeLoadMovie;
|
||||
_loadSceneImage ??= LoadSceneImage;
|
||||
_loadBrushImage ??= LoadBrushImage;
|
||||
_loadAudio ??= LoadAudio;
|
||||
_nativeAddEffect ??= (name, args) => _context.AddEffect(new AviUtlEffectRequest(name, args));
|
||||
_nativeAddDraw ??= command => _context.AddDraw(command);
|
||||
_nativeSetAnchor ??= ResolveNativeAnchor;
|
||||
@@ -1052,6 +1063,7 @@ namespace LuaScript
|
||||
_nativeSetAnchor,
|
||||
_nativeSceneGet,
|
||||
_nativeSceneSet,
|
||||
_loadAudio,
|
||||
_nativeRunPixelShader,
|
||||
out bool dirty, out bool bufferReplaced,
|
||||
out int resultW, out int resultH, out string? error);
|
||||
@@ -1249,6 +1261,59 @@ namespace LuaScript
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly (int Count, int Rate, double[] Data) s_noAudio = (0, 0, []);
|
||||
|
||||
private (int Count, int Rate, double[] Data) LoadAudio(string file, string type, int size)
|
||||
{
|
||||
var desc = _frameDesc;
|
||||
if (desc is null)
|
||||
return s_noAudio;
|
||||
|
||||
_audioProvider ??= new AudioSampleProvider();
|
||||
(int Count, int Rate) result;
|
||||
if (string.Equals(file, "audiobuffer", StringComparison.Ordinal))
|
||||
{
|
||||
var (audioItem, scene) = ResolveOwnAudioItem(desc);
|
||||
result = _audioProvider.ReadItem(audioItem, scene, desc.ItemPosition.Time.TotalSeconds, desc.FPS, type, size);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = _audioProvider.ReadFile(file, desc.ItemPosition.Time.TotalSeconds, type, size);
|
||||
}
|
||||
|
||||
if (result.Count > 0)
|
||||
_audioLoaded = true;
|
||||
return (result.Count, result.Rate, _audioProvider.Data);
|
||||
}
|
||||
|
||||
private (IAudioItem? Item, Scene? Scene) ResolveOwnAudioItem(EffectDescription desc)
|
||||
{
|
||||
var scenes = desc.Scenes;
|
||||
if (scenes is null)
|
||||
return (null, null);
|
||||
|
||||
foreach (var info in scenes)
|
||||
{
|
||||
if (info is not Scene scene || scene.ID != desc.SceneId)
|
||||
continue;
|
||||
|
||||
var items = scene.Timeline.Items;
|
||||
for (int i = 0; i < items.Count; i++)
|
||||
{
|
||||
if (items[i] is not IVideoItem video)
|
||||
continue;
|
||||
var effects = video.VideoEffects;
|
||||
for (int k = 0; k < effects.Count; k++)
|
||||
{
|
||||
if (ReferenceEquals(effects[k], item))
|
||||
return (items[i] as IAudioItem, scene);
|
||||
}
|
||||
}
|
||||
return (null, scene);
|
||||
}
|
||||
return (null, null);
|
||||
}
|
||||
|
||||
private (byte[] buffer, int w, int h) NativeLoadMovie(string path, double time)
|
||||
{
|
||||
_nativeMovieDecoder ??= new MovieDecoder();
|
||||
@@ -1298,6 +1363,7 @@ namespace LuaScript
|
||||
_nativeImageDecoder?.Dispose();
|
||||
_sceneImageRenderer?.Dispose();
|
||||
_brushImageRenderer?.Dispose();
|
||||
_audioProvider?.Dispose();
|
||||
_pixelLoaderSemaphore.Dispose();
|
||||
_pixelManager?.Dispose();
|
||||
_pixelManager = null;
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする