From 4072d9b1170b772a86f5245972e09e6b4c07747d Mon Sep 17 00:00:00 2001 From: routersys Date: Sun, 5 Jul 2026 00:31:44 +0900 Subject: [PATCH] =?UTF-8?q?=E9=9F=B3=E5=A3=B0=E3=82=B5=E3=83=B3=E3=83=97?= =?UTF-8?q?=E3=83=AB=E5=8F=96=E5=BE=97=E3=81=AE=E3=83=9B=E3=82=B9=E3=83=88?= =?UTF-8?q?=E9=83=A8=E5=93=81=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LuaScript/Audio/AudioSampleProvider.cs | 185 +++++++++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 LuaScript/Audio/AudioSampleProvider.cs diff --git a/LuaScript/Audio/AudioSampleProvider.cs b/LuaScript/Audio/AudioSampleProvider.cs new file mode 100644 index 0000000..9d3eb1d --- /dev/null +++ b/LuaScript/Audio/AudioSampleProvider.cs @@ -0,0 +1,185 @@ +using System.Reflection; +using LuaScript.Compat; +using YukkuriMovieMaker.Player.Audio; +using YukkuriMovieMaker.Plugin; +using YukkuriMovieMaker.Plugin.FileSource; +using YukkuriMovieMaker.Project; +using YukkuriMovieMaker.Project.Items; +using YukkuriMovieMaker.Settings; + +namespace LuaScript +{ + internal sealed class AudioSampleProvider : IDisposable + { + private static readonly object s_ctorLock = new(); + private static ConstructorInfo? s_effectedSourceCtor; + private static bool s_ctorResolved; + + private readonly AviUtlAudioConverter _converter = new(); + private readonly double[] _data = new double[AviUtlAudioConverter.MaxSamples]; + private float[] _readBuffer = []; + + private IAudioFileSource? _fileSource; + private string _fileSourcePath = string.Empty; + + private AudioStreamBase? _itemSource; + private IAudioItem? _itemSourceItem; + private Scene? _itemSourceScene; + private string? _itemSourceFile; + + public double[] Data => _data; + + public (int Count, int Rate) ReadFile(string path, double time, string type, int size) + { + try + { + if (_fileSource is null || !string.Equals(path, _fileSourcePath, StringComparison.Ordinal)) + { + _fileSource?.Dispose(); + _fileSource = null; + _fileSource = AudioFileSourceFactory.Create(path, 0); + _fileSourcePath = path; + } + + var source = _fileSource; + if (source is null || source.Hz <= 0) + return (0, 0); + + int floats = EnsureReadBuffer(type, size); + if (floats <= 0) + return (0, source.Hz); + + source.Seek(TimeSpan.FromSeconds(Math.Max(0d, time))); + int total = 0; + while (total < floats) + { + int read = source.Read(_readBuffer, total, floats - total); + if (read <= 0) + break; + total += read; + } + + return (_converter.Convert(type, _readBuffer, total / 2, size, _data), source.Hz); + } + catch + { + return (0, 0); + } + } + + public (int Count, int Rate) ReadItem(IAudioItem? item, Scene? scene, double time, int fps, string type, int size) + { + try + { + if (item is null || scene is null) + return (0, 0); + + string? file = item switch + { + AudioItem audio => audio.FilePath, + VideoItem video => video.FilePath, + _ => null, + }; + if (_itemSource is null || + !ReferenceEquals(item, _itemSourceItem) || + !ReferenceEquals(scene, _itemSourceScene) || + !string.Equals(file, _itemSourceFile, StringComparison.Ordinal)) + { + _itemSource?.Dispose(); + _itemSource = null; + _itemSource = CreateEffectedSource(item, scene, fps); + _itemSourceItem = item; + _itemSourceScene = scene; + _itemSourceFile = file; + } + + var source = _itemSource; + if (source is null || source.Hz <= 0) + return (0, 0); + + int floats = EnsureReadBuffer(type, size); + if (floats <= 0) + return (0, source.Hz); + + source.Seek(TimeSpan.FromSeconds(Math.Max(0d, time))); + int total = 0; + while (total < floats) + { + int read = source.Read(_readBuffer, total, floats - total); + if (read <= 0) + break; + total += read; + } + + return (_converter.Convert(type, _readBuffer, total / 2, size, _data), source.Hz); + } + catch + { + return (0, 0); + } + } + + private int EnsureReadBuffer(string type, int size) + { + int frames = AviUtlAudioConverter.RequiredFrames(type, size); + if (frames <= 0) + return 0; + int floats = frames * 2; + if (_readBuffer.Length < floats) + _readBuffer = new float[floats]; + return floats; + } + + private static AudioStreamBase? CreateEffectedSource(IAudioItem item, Scene scene, int fps) + { + var ctor = ResolveEffectedSourceCtor(); + if (ctor is null) + return null; + try + { + return ctor.Invoke([item, scene, 0, fps, ResamplerMode.Linear, false, false, false]) as AudioStreamBase; + } + catch + { + return null; + } + } + + private static ConstructorInfo? ResolveEffectedSourceCtor() + { + if (s_ctorResolved) + return s_effectedSourceCtor; + lock (s_ctorLock) + { + if (!s_ctorResolved) + { + try + { + s_effectedSourceCtor = typeof(Scene).Assembly + .GetType("YukkuriMovieMaker.Player.Audio.EffectedItemSource")? + .GetConstructor([ + typeof(IAudioItem), typeof(Scene), typeof(int), typeof(int), + typeof(ResamplerMode), typeof(bool), typeof(bool), typeof(bool)]); + } + catch + { + s_effectedSourceCtor = null; + } + s_ctorResolved = true; + } + } + return s_effectedSourceCtor; + } + + public void Dispose() + { + _fileSource?.Dispose(); + _fileSource = null; + _itemSource?.Dispose(); + _itemSource = null; + _itemSourceItem = null; + _itemSourceScene = null; + _itemSourceFile = null; + } + } +}