diff --git a/.github/release-notes/custom-resampler.md b/.github/release-notes/custom-resampler.md new file mode 100644 index 00000000..3d042e98 --- /dev/null +++ b/.github/release-notes/custom-resampler.md @@ -0,0 +1,38 @@ +## Custom resamplers v0.4 + Auto pitch + Harmony generator + +> **Vibe-coded with AI.** The Custom resamplers, Auto pitch and Harmony generator features in this build were written by [Claude](https://claude.com/claude-code) (Anthropic's AI, Claude Sonnet 5.5), directed by sparky-commits1637. The code has not been hand-reviewed or tested on a device yet, so please treat it as experimental. + +Adds a **Custom resamplers** page to OpenUtau Mobile so you can use your own external resamplers. + +### What's new +- **Options → Custom resamplers**: a new entry between *Plugin manager* and *Help & Tutorials*, styled like the rest of Options. +- **Add** a resampler with the **+** button. Its matching `.yaml` manifest (same name) is copied along with it. +- **Replace** an existing one by adding a file with the same name, or **remove** one with the trash button. +- Added resamplers show up in **Track render settings → Resampler**. +- Translations: English, 简体中文, 日本語, Русский, Українська. + +### Good to know +- Resamplers are external programs that run on your device. Only add files you trust. The app asks you to confirm. +- Supported files on Android: `.sh` scripts or extensionless native binaries built for Android. `.exe` / `.bat` only load if a Wine path is set in Settings. The page shows what your device accepts. +- Being listed does not guarantee a program runs on your device. +- Close the open project before adding or removing a resampler. +- Names used by built-in resamplers (`worldline`, `hifisampler`) are not allowed. +- Not offered on iOS or in the browser. + +### What's new in v0.4 +- **Auto pitch** (Batch edit → Notes): generates a pitch curve from note data with an ONNX model, ported from [UtauV](https://github.com/emeraldsingers/UtauV). The model is **not included**. Download `autopitchcurve` (`.oudep`) from the [UtauV_Packages](https://github.com/emeraldsingers/UtauV_Packages) releases and install it via **Dependency manager → Install from file**. Select a whole phrase first. It runs on the CPU, so expect it to be slow on a phone. +- **Harmony generator** (Batch edit → Notes): adds harmony tracks for the current track (Pop style, automatic key, 1–8 voices), also ported from UtauV. +- **Signed with a fixed key**: this and later builds from this workflow are signed with the same key, so updates install over each other. **Uninstalling the previous copy is needed once**, because the key changed. +- Neither feature has been tested on a device. + +### What was new in v0.3 +- The app now **targets Android API 28**. On Android 10+, apps targeting API 29 or higher are blocked by SELinux from executing files in their own data folder, so starting a custom resampler failed with **"Permission denied"**. With API 28 that block no longer applies (Termux works the same way). This is the only code change since v0.2. +- **Not fixed / not confirmed:** some native resamplers still crash with **exit code 159 (SIGSYS)**, where Android's seccomp filter kills a process that uses a blocked system call. v0.3 does not address that, and a full render has not been confirmed to finish inside the app. +- Targeting API 28 changes some Android behaviors (for example storage and network defaults). Nothing has been tested on a device for those either. + +### About this build +- Installs as a **separate app** named *OpenUtau Mobile (Custom Resampler)* (package `pers.vocoder712.openutaumobile.canary.resampler`), so it can sit next to the official build. It has its own settings, singers and plugins. Projects saved in shared folders can be opened from either app. +- **Updating from v0.3 or earlier:** those builds used a random debug key, so Android will refuse to install this over them. Uninstall that copy first, which erases its app data (imported voicebanks, projects), so back them up before you do. From this version on, builds share one key and update in place. +- Android **ARM64** only, **signed with a fixed (self-generated) key**. +- Built from upstream `dev` plus the custom resampler, Auto pitch and Harmony generator features. It compiles, but has not been tested on a device yet. +- Please report problems with the log from **Options → Export Log**. diff --git a/.github/workflows/build-apk-artifact.yml b/.github/workflows/build-apk-artifact.yml new file mode 100644 index 00000000..a6a181d4 --- /dev/null +++ b/.github/workflows/build-apk-artifact.yml @@ -0,0 +1,117 @@ +name: Build APK Artifact + +on: + push: + branches: + - feature/custom-resampler-manager + +permissions: + contents: write + +jobs: + android-build: + name: Build Android ARM64 APK + env: + AVALONIA_TELEMETRY_OPTOUT: "1" + Configuration: Release + RuntimeIdentifier: android-arm64 + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + submodules: recursive + persist-credentials: false + + - name: Setup .NET + uses: actions/setup-dotnet@v4 + with: + global-json-file: global.json + + - name: Setup Java + uses: actions/setup-java@v4 + with: + distribution: temurin + java-version: 17 + + - name: Setup Android SDK command-line tools + uses: android-actions/setup-android@v3 + with: + packages: 'platform-tools platforms;android-36 build-tools;36.0.0' + log-accepted-android-sdk-licenses: false + + - name: Install Android workload + run: dotnet workload install android + + - name: Setup native Bazel build tools + uses: ./.github/actions/setup-worldline + + - name: Install Android native toolchain + shell: bash + run: | + set -euo pipefail + sdkmanager "ndk;$(tr -d '\r\n' < native/android-ndk-version.txt)" + + - name: Give the build its own app name (CI only, repo files stay unchanged) + run: | + sed -i 's|.*|OpenUtau Mobile (Custom Resampler)|' OpenUtauMobile.Android/Resources/values/strings.xml + grep -n app_name OpenUtauMobile.Android/Resources/values/strings.xml + + - name: Prepare signing key + id: signing + env: + KEYSTORE_B64: ${{ secrets.ANDROID_KEYSTORE_BASE64 }} + run: | + # With the repo secrets set, every build is signed with the same key, so new APKs + # install over older ones (no uninstall, no data loss). Without them the build falls + # back to a random debug key, like before. + if [ -n "$KEYSTORE_B64" ]; then + echo "$KEYSTORE_B64" | base64 -d > "$RUNNER_TEMP/release.keystore" + echo "enabled=true" >> "$GITHUB_OUTPUT" + echo "Using the fixed signing key." + else + echo "enabled=false" >> "$GITHUB_OUTPUT" + echo "::warning::ANDROID_KEYSTORE_BASE64 is not set - falling back to a random debug key (updates will need an uninstall)." + fi + + - name: Build Android ARM64 + env: + KS_PASS: ${{ secrets.ANDROID_KEYSTORE_PASSWORD }} + KS_FILE: ${{ runner.temp }}/release.keystore + run: | + SIGN=() + if [ "${{ steps.signing.outputs.enabled }}" = "true" ]; then + SIGN=(-p:AndroidKeyStore=true "-p:AndroidSigningKeyStore=$KS_FILE" -p:AndroidSigningKeyAlias=openutau + -p:AndroidSigningStorePass=env:KS_PASS -p:AndroidSigningKeyPass=env:KS_PASS) + fi + dotnet build OpenUtauMobile.Android/OpenUtauMobile.Android.csproj -p:ApplicationId=pers.vocoder712.openutaumobile.canary.resampler "${SIGN[@]}" + + - name: Remove signing key from the runner + if: always() + run: rm -f "$RUNNER_TEMP/release.keystore" + + - name: Upload APK + uses: actions/upload-artifact@v4 + with: + name: OpenUtauMobile-android-arm64-debug + path: OpenUtauMobile.Android/bin/Release/**/*.apk + if-no-files-found: error + + - name: Publish GitHub release + if: contains(github.event.head_commit.message, '[release]') + env: + GH_TOKEN: ${{ github.token }} + TAG: custom-resampler-v0.4 + run: | + set -euo pipefail + apk=$(find OpenUtauMobile.Android/bin/Release -name '*-Signed.apk' | head -n1) + if [ -z "$apk" ]; then apk=$(find OpenUtauMobile.Android/bin/Release -name '*.apk' | head -n1); fi + test -n "$apk" + cp "$apk" OpenUtauMobile-custom-resampler-v0.4-android-arm64.apk + gh release create "$TAG" OpenUtauMobile-custom-resampler-v0.4-android-arm64.apk \ + --repo "$GITHUB_REPOSITORY" \ + --target "$GITHUB_SHA" \ + --title "Custom resamplers v0.4 + Auto pitch + Harmony generator" \ + --notes-file .github/release-notes/custom-resampler.md \ + --prerelease diff --git a/OpenUtau.Core/Editing/AutoPitchEdit.cs b/OpenUtau.Core/Editing/AutoPitchEdit.cs new file mode 100644 index 00000000..cc8f70fc --- /dev/null +++ b/OpenUtau.Core/Editing/AutoPitchEdit.cs @@ -0,0 +1,1671 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Security.Cryptography; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading; +using Microsoft.ML.OnnxRuntime; +using Microsoft.ML.OnnxRuntime.Tensors; +using OpenUtau.Core; +using OpenUtau.Core.Ustx; +using OpenUtau.Core.Util; +using Serilog; + +namespace OpenUtau.Core.Editing { + public class AutoPitchEdit : BatchEdit { + public string Name => "Auto Pitch"; + public bool IsAsync => true; + + private const string DefaultModelName = "autopitch.onnx"; + private const string DefaultConfigName = "autopitch.onnx.config.json"; + private const string DefaultVocabName = "autopitch.onnx.vocab.json"; + + private static readonly object AssetLock = new object(); + private static AutoPitchAssets cachedAssets; + + public static void ClearCache() { + lock (AssetLock) { + if (cachedAssets != null) { + try { + cachedAssets.Session?.Dispose(); + } catch (Exception ex) { + Log.Warning(ex, "Failed to dispose AutoPitch session."); + } + cachedAssets = null; + } + } + } + + public void Run(UProject project, UVoicePart part, List selectedNotes, DocManager docManager) { + RunAsync(project, part, selectedNotes, docManager, (_, __) => { }, CancellationToken.None); + } + + public void RunAsync( + UProject project, UVoicePart part, List selectedNotes, DocManager docManager, + Action setProgressCallback, CancellationToken cancellationToken) { + var assets = LoadAssets(); + if (assets == null) { + return; + } + RefreshConfigAndVocab(assets); + var notesAll = part.notes.OrderBy(n => n.position).ToList(); + if (notesAll.Count == 0) { + return; + } + var selected = selectedNotes.Count > 0 ? selectedNotes.OrderBy(n => n.position).ToList() : null; + int? rangeStart = null; + int? rangeEnd = null; + if (selected != null && selected.Count > 0) { + rangeStart = selected.Min(n => n.position); + rangeEnd = selected.Max(n => n.position + n.duration); + } + var cfg = assets.Config ?? new AutoPitchConfig(); + string lyricConditioning = ResolveLyricConditioning(cfg); + bool useLyric = lyricConditioning != "none"; + if (lyricConditioning == "vocab" && (assets.Vocab == null || assets.Vocab.Count == 0)) { + useLyric = false; + lyricConditioning = "none"; + } + int channels = cfg.model?.in_channels ?? 10; + + int padLeft = cfg.infer.pad_ticks; + int padRight = cfg.infer.pad_ticks; + if (rangeStart.HasValue && rangeEnd.HasValue) { + padLeft = Math.Max(padLeft, cfg.selection_pad_left); + padRight = Math.Max(padRight, cfg.selection_pad_right); + } + + // Extract BPM from project + double projectBpm = 120.0; + if (project.tempos != null && project.tempos.Count > 0) { + projectBpm = project.tempos[0].bpm; + } + if (projectBpm <= 0) { + projectBpm = 120.0; + } + + setProgressCallback(0, 3); + var grid = BuildTickGrid(notesAll, cfg.data.dt, padLeft, padRight, rangeStart, rangeEnd); + int gridStart = grid.gridStart; + int[] ticks = grid.ticks; + if (ticks.Length == 0 || cancellationToken.IsCancellationRequested) { + return; + } + + int conditioningClassCount = GetConditioningClassCount(cfg, lyricConditioning); + var features = BuildFeatures( + notesAll, + gridStart, + ticks, + cfg.data, + channels, + lyricConditioning, + assets.Vocab, + conditioningClassCount, + (float)projectBpm); + if (features.mask.All(m => m <= 0.5f)) { + return; + } + if (cancellationToken.IsCancellationRequested) { + return; + } + + ApplyInferenceNoise(features.features, cfg); + setProgressCallback(1, 3); + + float styleId = 0.0f; + float residualStrength = 0.0f; + if (cfg.model != null && cfg.model.style_conditioning) { + bool manualStyle = string.Equals(cfg.style, "manual", StringComparison.OrdinalIgnoreCase); + styleId = manualStyle ? 1.0f : 0.0f; + residualStrength = manualStyle ? cfg.style_strength : 0.0f; + } + + float[] predNorm = PredictFull( + assets.Session, + features.features, + features.lyricIds, + channels, + ticks.Length, + cfg.infer.chunk_size, + cfg.infer.chunk_overlap, + lyricConditioning, + cfg.model != null && cfg.model.style_conditioning ? styleId : (float?)null); + if (predNorm.Length == 0 || cancellationToken.IsCancellationRequested) { + return; + } + setProgressCallback(2, 3); + + Log.Information("[AutoPitch] predNorm length={len} residualStrength={rs} pitd_clamp={clamp} smooth_window={sw} rdp_epsilon={rdp}", + predNorm.Length, residualStrength, cfg.data.pitd_clamp, cfg.infer.smooth_window, cfg.infer.rdp_epsilon); + Log.Information("[AutoPitch] predNorm base range=[{min:F5},{max:F5}]", + predNorm.Take(ticks.Length).Min(), predNorm.Take(ticks.Length).Max()); + + float[] predTotal = new float[ticks.Length]; + for (int t = 0; t < ticks.Length; t++) { + predTotal[t] = predNorm[t] + predNorm[ticks.Length + t] * residualStrength; + } + predTotal = MovingAverage(predTotal, cfg.infer.smooth_window); + for (int t = 0; t < predTotal.Length; t++) { + predTotal[t] = Math.Clamp(predTotal[t], -1.0f, 1.0f); + } + float[] predCents = new float[predTotal.Length]; + for (int t = 0; t < predTotal.Length; t++) { + float val = predTotal[t] * cfg.data.pitd_clamp; + predCents[t] = Math.Clamp(val, -cfg.infer.clamp_output, cfg.infer.clamp_output); + } + Log.Information("[AutoPitch] predCents range=[{min:F1},{max:F1}] non_zero={nz}/{total}", + predCents.Min(), predCents.Max(), + predCents.Count(c => Math.Abs(c) > 1.0f), predCents.Length); + + var forcedTicks = new HashSet(); + foreach (var note in notesAll) { + forcedTicks.Add(note.position); + forcedTicks.Add(note.position + note.duration); + } + if (rangeStart.HasValue && rangeEnd.HasValue) { + int minTick = rangeStart.Value - padLeft; + int maxTick = rangeEnd.Value + padRight; + forcedTicks.RemoveWhere(t => t < minTick || t > maxTick); + } + + var simplified = SimplifyCurve(ticks, predCents, features.mask, cfg.infer.rdp_epsilon, forcedTicks); + if (simplified.xs.Count == 0) { + return; + } + if (rangeStart.HasValue && rangeEnd.HasValue) { + int anchorStart = rangeStart.Value - padLeft; + int anchorEnd = rangeEnd.Value + padRight; + int minTick = ticks[0]; + int maxTick = ticks[^1]; + anchorStart = Math.Clamp(anchorStart, minTick, maxTick); + anchorEnd = Math.Clamp(anchorEnd, minTick, maxTick); + AddBoundaryAnchors(simplified.xs, simplified.ys, anchorStart, anchorEnd); + } + + int[] oldXs = null; + int[] oldYs = null; + var curve = part.curves.FirstOrDefault(c => c.abbr == Format.Ustx.PITD); + if (curve != null) { + oldXs = curve.xs.ToArray(); + oldYs = curve.ys.ToArray(); + } + + List xsFinal; + List ysFinal; + if (rangeStart.HasValue && rangeEnd.HasValue) { + int mergeStart = rangeStart.Value - padLeft; + int mergeEnd = rangeEnd.Value + padRight; + var merged = MergeCurves(oldXs, oldYs, simplified.xs, simplified.ys, mergeStart, mergeEnd, cfg.infer.clamp_output); + xsFinal = merged.xs; + ysFinal = merged.ys; + } else { + var validated = ValidateCurve(simplified.xs, simplified.ys, cfg.infer.clamp_output); + xsFinal = validated.xs; + ysFinal = validated.ys; + } + if (xsFinal.Count == 0 || cancellationToken.IsCancellationRequested) { + return; + } + + DocManager.Inst.PostOnUIThread(() => { + docManager.StartUndoGroup(null, true); + docManager.ExecuteCmd(new MergedSetCurveCommand( + project, part, Format.Ustx.PITD, + oldXs, oldYs, xsFinal.ToArray(), ysFinal.ToArray())); + docManager.EndUndoGroup(); + }); + setProgressCallback(3, 3); + } + + private static AutoPitchAssets LoadAssets() { + lock (AssetLock) { + if (cachedAssets != null) { + return cachedAssets; + } + string basePath = Path.Combine(PathManager.Inst.DependencyPath, "autopitchcurve"); + string modelPath = Path.Combine(basePath, DefaultModelName); + string configPath = Path.Combine(basePath, DefaultConfigName); + string vocabPath = Path.Combine(basePath, DefaultVocabName); + if (!File.Exists(modelPath)) { + var e = new MessageCustomizableException( + "AutoPitch model not found", + $"AutoPitch model not found at {modelPath}. Install model files via the Package Manager or place them manually.", + new FileNotFoundException(modelPath)); + DocManager.Inst.ExecuteCmd(new ErrorMessageNotification(e)); + return null; + } + + InferenceSession session = null; + try { + // Force CPU: DirectML produces NaN on every chunk due to + // structural incompatibility with the NoteAttentionLayer ops + // (scatter_add / CumSum in _note_idx_from_features). + // AutoPitch runs once per part, so CPU performance is fine. + session = Onnx.getInferenceSession(modelPath, OnnxRunnerChoice.CPU); + } catch (Exception ex) { + Log.Error(ex, "Failed to load AutoPitch ONNX session."); + var e = new MessageCustomizableException( + "AutoPitch model failed to load", + "Failed to load AutoPitch ONNX model.", + ex); + DocManager.Inst.ExecuteCmd(new ErrorMessageNotification(e)); + return null; + } + + AutoPitchConfig config = new AutoPitchConfig(); + if (File.Exists(configPath)) { + Log.Information("[AutoPitch] loading config from sidecar file: {path}", configPath); + try { + var json = File.ReadAllText(configPath, Encoding.UTF8); + config = Json.Deserialize(json) ?? new AutoPitchConfig(); + } catch (Exception ex) { + Log.Warning(ex, "Failed to parse AutoPitch config. Using defaults."); + } + } else { + Log.Information("[AutoPitch] no sidecar config, loading from ONNX metadata"); + try { + var meta = session.ModelMetadata.CustomMetadataMap; + if (meta != null && meta.TryGetValue("autopitch_config_json", out var metaJson)) { + config = Json.Deserialize(metaJson) ?? new AutoPitchConfig(); + } + } catch (Exception ex) { + Log.Warning(ex, "Failed to load AutoPitch config from model metadata."); + } + } + + Dictionary vocab = new Dictionary(); + if (File.Exists(vocabPath)) { + try { + var json = File.ReadAllText(vocabPath, Encoding.UTF8); + vocab = Json.Deserialize>(json) ?? new Dictionary(); + } catch (Exception ex) { + Log.Warning(ex, "Failed to parse AutoPitch vocab."); + } + } else { + try { + var meta = session.ModelMetadata.CustomMetadataMap; + if (meta != null && meta.TryGetValue("lyric_vocab_json", out var vocabJson)) { + vocab = Json.Deserialize>(vocabJson) ?? new Dictionary(); + } + } catch (Exception ex) { + Log.Warning(ex, "Failed to load AutoPitch vocab from model metadata."); + } + } + + Log.Information("[AutoPitch] loaded config: dt={dt} chunk_size={cs} chunk_overlap={co} rdp_eps={rdp} smooth={sm} pitd_clamp={pc} bpm_scale={bs} in_channels={ch}", + config.data.dt, config.infer.chunk_size, config.infer.chunk_overlap, + config.infer.rdp_epsilon, config.infer.smooth_window, config.data.pitd_clamp, + config.data.bpm_scale, config.model?.in_channels ?? -1); + Log.Information("[AutoPitch] vocab size={vs} OnnxRunner={runner}", + vocab.Count, Preferences.Default.OnnxRunner ?? "(default)"); + + cachedAssets = new AutoPitchAssets { + BasePath = basePath, + Session = session, + Config = config, + Vocab = vocab, + ConfigPath = configPath, + VocabPath = vocabPath, + }; + return cachedAssets; + } + } + + private static void RefreshConfigAndVocab(AutoPitchAssets assets) { + if (assets == null) { + return; + } + if (!string.IsNullOrEmpty(assets.ConfigPath) && File.Exists(assets.ConfigPath)) { + try { + var json = File.ReadAllText(assets.ConfigPath, Encoding.UTF8); + assets.Config = Json.Deserialize(json) ?? assets.Config; + } catch (Exception ex) { + Log.Warning(ex, "Failed to reload AutoPitch config."); + } + } + if (!string.IsNullOrEmpty(assets.VocabPath) && File.Exists(assets.VocabPath)) { + try { + var json = File.ReadAllText(assets.VocabPath, Encoding.UTF8); + assets.Vocab = Json.Deserialize>(json) ?? assets.Vocab; + } catch (Exception ex) { + Log.Warning(ex, "Failed to reload AutoPitch vocab."); + } + } + } + + private static (int gridStart, int[] ticks) BuildTickGrid( + List notes, int dt, int padLeft, int padRight, int? rangeStart, int? rangeEnd) { + if (notes.Count == 0) { + return (0, Array.Empty()); + } + int minPos = rangeStart ?? notes.Min(n => n.position); + int maxEnd = rangeEnd ?? notes.Max(n => n.position + n.duration); + minPos = Math.Max(0, minPos - padLeft); + maxEnd = Math.Max(minPos + dt, maxEnd + padRight); + int gridStart = (minPos / dt) * dt; + int gridEnd = (int)Math.Ceiling(maxEnd / (double)dt) * dt; + int length = Math.Max(0, (gridEnd - gridStart) / dt); + int[] ticks = new int[length]; + for (int i = 0; i < length; i++) { + ticks[i] = gridStart + i * dt; + } + return (gridStart, ticks); + } + + private static FeatureBundle BuildFeatures( + List notes, + int gridStart, + int[] ticks, + DataConfig cfg, + int channels, + string lyricConditioning, + Dictionary vocab, + int conditioningClassCount, + float bpm = 120.0f) { + int tLen = ticks.Length; + float[] features = new float[channels * tLen]; + float[] mask = new float[tLen]; + bool useLyric = lyricConditioning != "none"; + bool useClass = lyricConditioning == "class"; + bool useClassEdge = lyricConditioning == "class_edge"; + bool useEdge = lyricConditioning == "edge"; + bool useEdgeClass = lyricConditioning == "edge_class"; + // Rest frames must carry the conditioning mode's rest sentinel, not 0. + // For the class-like modes ID 0 is a real sound class ("vowel"), so a + // zero-filled array told the model a vowel was being sung during every + // silence. Rest frames are excluded from the loss during training, but + // the TCN's receptive field spans them, so this contaminates the + // context for the voiced frames on either side. For "vocab" mode 0 is + // "", which is already correct. + long[]? lyricIds = useLyric ? new long[tLen] : null; + if (lyricIds != null) { + long restId = ConditioningRestId(lyricConditioning); + if (restId != 0) { + for (int t = 0; t < tLen; t++) { + lyricIds[t] = restId; + } + } + } + float pitchScale = cfg.pitch_scale != 0.0f ? cfg.pitch_scale : 1.0f; + float intervalScale = cfg.interval_scale != 0.0f ? cfg.interval_scale : 1.0f; + float noteLenScale = cfg.note_len_scale != 0.0f ? cfg.note_len_scale : 1.0f; + float noteLenNorm = cfg.note_len_norm != 0.0f ? cfg.note_len_norm : 1.0f; + // BPM-derived constants (only used when channels >= 12) + float bpmClamped = Math.Max(1.0f, bpm); + float secondsPerTick = 60.0f / (bpmClamped * cfg.ticks_per_beat); + float bpmScale = cfg.bpm_scale > 0 ? cfg.bpm_scale : 120.0f; + float noteDurSecScale = cfg.note_dur_sec_scale > 0 ? cfg.note_dur_sec_scale : 1.0f; + float timeScale = Math.Clamp(bpmClamped / bpmScale, 0.0f, 4.0f); + if (notes.Count == 0 || tLen == 0) { + if (tLen > 0 && channels > 1) { + for (int t = 0; t < tLen; t++) { + features[tLen + t] = 1.0f; + } + } + // Fill ch11 (time_scale) even for empty notes + if (tLen > 0 && channels >= 12) { + for (int t = 0; t < tLen; t++) { + features[11 * tLen + t] = timeScale; + } + } + return new FeatureBundle(features, mask, lyricIds); + } + int[]? edgeIds = (useEdge || useClassEdge) ? BuildEdgeIdsForNotes(notes) : null; + int[]? edgeClassIds = useEdgeClass ? BuildEdgeClassIdsForNotes(notes) : null; + + for (int i = 0; i < notes.Count; i++) { + var note = notes[i]; + int pos = note.position; + int dur = note.duration; + if (dur <= 0) { + continue; + } + int tone = note.tone; + int startIdx = (int)Math.Ceiling((pos - gridStart) / (double)cfg.dt); + int endIdx = (int)Math.Ceiling((pos + dur - gridStart) / (double)cfg.dt); + startIdx = Math.Clamp(startIdx, 0, tLen); + endIdx = Math.Clamp(endIdx, 0, tLen); + if (endIdx <= startIdx) { + continue; + } + + var prev = i > 0 ? notes[i - 1] : null; + var next = i + 1 < notes.Count ? notes[i + 1] : null; + int intervalPrev = prev != null ? tone - prev.tone : 0; + int intervalNext = next != null ? next.tone - tone : 0; + intervalPrev = Math.Clamp(intervalPrev, -cfg.interval_clip, cfg.interval_clip); + intervalNext = Math.Clamp(intervalNext, -cfg.interval_clip, cfg.interval_clip); + float legatoPrev = prev != null && prev.position + prev.duration == pos ? 1.0f : 0.0f; + float legatoNext = next != null && pos + dur == next.position ? 1.0f : 0.0f; + + if (pos >= ticks[0]) { + features[2 * tLen + startIdx] = 1.0f; + } + int noteEnd = pos + dur; + if (noteEnd <= ticks[^1] + cfg.dt) { + features[3 * tLen + Math.Max(startIdx, endIdx - 1)] = 1.0f; + } + + float noteLen = dur / noteLenNorm; + noteLen = Math.Clamp(noteLen, 0.0f, cfg.note_len_clip) / noteLenScale; + float basePitch = (tone - cfg.pitch_center) / pitchScale; + float intervalPrevScaled = intervalPrev / intervalScale; + float intervalNextScaled = intervalNext / intervalScale; + + int tokenId = 0; + if (useLyric && lyricIds != null) { + string token = NormalizePhoneme(note.lyric); + if (useClass) { + tokenId = ClassifyPhoneme(token); + } else if (useClassEdge && edgeIds != null && i < edgeIds.Length) { + int classId = ClassifyPhoneme(token); + tokenId = classId * EDGE_CLASS_COUNT + edgeIds[i]; + } else if (useEdge && edgeIds != null && i < edgeIds.Length) { + tokenId = edgeIds[i]; + } else if (useEdgeClass && edgeClassIds != null && i < edgeClassIds.Length) { + tokenId = edgeClassIds[i]; + } else if (vocab != null && vocab.Count > 0) { + if (vocab.TryGetValue(token, out int id)) { + tokenId = id; + } + } + if ((useClass || useClassEdge || useEdge || useEdgeClass) && conditioningClassCount > 0) { + tokenId = Math.Clamp(tokenId, 0, conditioningClassCount - 1); + } + } + + // BPM features for this note (only when channels >= 12) + float noteDurSec = 0.0f; + float noteDurSecFeat = 0.0f; + if (channels >= 12) { + noteDurSec = dur * secondsPerTick; + noteDurSec = Math.Min(noteDurSec, cfg.note_dur_sec_clip); + noteDurSecFeat = noteDurSec / noteDurSecScale; + } + + for (int t = startIdx; t < endIdx; t++) { + mask[t] = 1.0f; + features[0 * tLen + t] = basePitch; + float posInNote = (ticks[t] - pos) / (float)dur; + features[4 * tLen + t] = Math.Clamp(posInNote, 0.0f, 1.0f); + features[5 * tLen + t] = noteLen; + features[6 * tLen + t] = intervalPrevScaled; + features[7 * tLen + t] = intervalNextScaled; + features[8 * tLen + t] = legatoPrev; + features[9 * tLen + t] = legatoNext; + if (channels >= 12) { + features[10 * tLen + t] = noteDurSecFeat; + features[11 * tLen + t] = timeScale; + } + if (useLyric && lyricIds != null) { + lyricIds[t] = tokenId; + } + } + + int tailTicks = Math.Max(0, cfg.post_note_tail_ticks); + int tailMaxGap = Math.Max(0, cfg.post_note_tail_max_gap_ticks); + if (tailTicks > 0) { + int maxTailTicks = tailTicks; + if (next != null) { + int gapTicks = Math.Max(0, next.position - noteEnd); + maxTailTicks = Math.Min(maxTailTicks, gapTicks); + } + if (tailMaxGap > 0) { + maxTailTicks = Math.Min(maxTailTicks, tailMaxGap); + } + if (maxTailTicks > 0) { + int tailStartIdx = endIdx; + int tailEndIdx = (int)Math.Ceiling((noteEnd + maxTailTicks - gridStart) / (double)cfg.dt); + tailStartIdx = Math.Clamp(tailStartIdx, 0, tLen); + tailEndIdx = Math.Clamp(tailEndIdx, 0, tLen); + for (int t = tailStartIdx; t < tailEndIdx; t++) { + mask[t] = 1.0f; + features[0 * tLen + t] = basePitch; + float posInTail = (ticks[t] - noteEnd) / (float)Math.Max(1, maxTailTicks); + features[4 * tLen + t] = 1.0f + Math.Clamp(posInTail, 0.0f, 1.0f); + features[5 * tLen + t] = noteLen; + features[6 * tLen + t] = intervalPrevScaled; + features[7 * tLen + t] = intervalNextScaled; + features[8 * tLen + t] = legatoPrev; + features[9 * tLen + t] = legatoNext; + if (channels >= 12) { + features[10 * tLen + t] = noteDurSecFeat; + features[11 * tLen + t] = timeScale; + } + if (useLyric && lyricIds != null) { + lyricIds[t] = tokenId; + } + } + } + } + } + + if (channels > 1) { + for (int t = 0; t < tLen; t++) { + features[tLen + t] = 1.0f - mask[t]; + } + } + return new FeatureBundle(features, mask, lyricIds); + } + + private static void ApplyInferenceNoise(float[] features, AutoPitchConfig cfg) { + float dropoutP = cfg.dropout_p ?? (cfg.model?.dropout ?? 0.0f); + bool useDropout = cfg.stochastic_dropout && dropoutP > 0.0f; + float noiseStd = cfg.feat_noise_std ?? 0.0f; + float tinyNoiseStd = cfg.feat_noise_std_tiny ?? 0.0f; + if (!useDropout && noiseStd <= 0.0f && tinyNoiseStd <= 0.0f) { + return; + } + int seed = cfg.always_random_seed ? RandomNumberGenerator.GetInt32(int.MaxValue) : 1337; + var rng = new Random(seed); + for (int i = 0; i < features.Length; i++) { + if (useDropout && rng.NextDouble() < dropoutP) { + features[i] = 0.0f; + continue; + } + } + if (noiseStd > 0.0f || tinyNoiseStd > 0.0f) { + int channels = cfg.model?.in_channels ?? 10; + int tLen = features.Length / channels; + int window = 50; + float scale = (float)Math.Sqrt(window); + + for (int c = 0; c < channels; c++) { + float[] channelNoise = new float[tLen]; + for (int t = 0; t < tLen; t++) { + float std = noiseStd; + if (tinyNoiseStd > 0.0f && (c == 0 || c == 6 || c == 7)) { + std = tinyNoiseStd; + } + channelNoise[t] = std > 0.0f ? NextGaussian(rng, std) : 0.0f; + } + float[] smoothedNoise = MovingAverage(channelNoise, window); + int offset = c * tLen; + for (int t = 0; t < tLen; t++) { + features[offset + t] += smoothedNoise[t] * scale; + } + } + } + if (tinyNoiseStd > 0.0f && cfg.model != null && cfg.model.in_channels > 0) { + int channels = cfg.model.in_channels; + int tLen = features.Length / channels; + // Apply tiny noise only to tone (0), interval_prev (6), interval_next (7). + int[] targetChannels = new int[] { 0, 6, 7 }; + foreach (int c in targetChannels) { + if (c < 0 || c >= channels) { + continue; + } + int baseIdx = c * tLen; + for (int t = 0; t < tLen; t++) { + features[baseIdx + t] += NextGaussian(rng, tinyNoiseStd); + } + } + } + } + + private static float NextGaussian(Random rng, float std) { + double u1 = 1.0 - rng.NextDouble(); + double u2 = 1.0 - rng.NextDouble(); + double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2); + return (float)(std * randStdNormal); + } + + private static float[] PredictFull( + InferenceSession session, + float[] features, + long[]? lyricIds, + int channels, + int tLen, + int chunkSize, + int chunkOverlap, + string lyricConditioning, + float? styleId) { + if (tLen == 0) { + return Array.Empty(); + } + bool useLyric = lyricConditioning != "none"; + // Only feed inputs the graph actually declares. Sending an undeclared + // input makes onnxruntime throw "Input name: X is not in the metadata", + // which is what happened with models exported before the conditioning + // inputs were named consistently: the sidecar config claimed a + // class-like mode while the graph had no phoneme_class_ids input. + var declaredInputs = new HashSet(session.InputMetadata.Keys, StringComparer.Ordinal); + Log.Information("[AutoPitch] tLen={tLen} chunkSize={chunkSize} chunkOverlap={chunkOverlap} channels={channels} lyricCond={lyric} declaredInputs=[{inputs}]", + tLen, chunkSize, chunkOverlap, channels, lyricConditioning, string.Join(",", declaredInputs)); + int step = Math.Max(1, chunkSize - chunkOverlap); + float[] output = new float[2 * tLen]; + float[] weight = new float[tLen]; + + for (int start = 0; start < tLen; start += step) { + int end = Math.Min(tLen, start + chunkSize); + int winLen = end - start; + int padLen = Math.Max(0, 2048 - winLen); + int modelLen = winLen + padLen; + + float[] featChunk = new float[channels * modelLen]; + for (int c = 0; c < channels; c++) { + Array.Copy(features, c * tLen + start, featChunk, c * modelLen, winLen); + } + // CRITICAL FIX: padding frames must have ch1=1.0 (is_rest), not 0.0 (voiced). + // With ch1=0, _note_idx_from_features treats padding as voiced frames, + // polluting note_emb with zeros → NaN in attention. + int padLen_actual = modelLen - winLen; + if (channels > 1 && padLen_actual > 0) { + for (int t = winLen; t < modelLen; t++) { + featChunk[1 * modelLen + t] = 1.0f; // ch1 = is_rest for padding + } + } + var inputs = new List { + NamedOnnxValue.CreateFromTensor("features", + new DenseTensor(featChunk, new int[] { 1, channels, modelLen })) + }; + if (useLyric && lyricIds != null) { + bool classLike = lyricConditioning == "class" || lyricConditioning == "edge" || lyricConditioning == "edge_class" || lyricConditioning == "class_edge"; + string preferred = classLike ? "phoneme_class_ids" : "lyric_ids"; + string fallback = classLike ? "lyric_ids" : "phoneme_class_ids"; + string? inputName = + declaredInputs.Contains(preferred) ? preferred : + declaredInputs.Contains(fallback) ? fallback : null; + if (inputName != null) { + long[] lyricChunk = new long[modelLen]; + Array.Copy(lyricIds, start, lyricChunk, 0, winLen); + inputs.Add(NamedOnnxValue.CreateFromTensor(inputName, + new DenseTensor(lyricChunk, new int[] { 1, modelLen }))); + } + } + // note_idx is optional: when the graph does not declare it, the model + // reconstructs note segmentation from features ch1/ch2 internally. + if (declaredInputs.Contains("note_idx")) { + long[] noteIdxChunk = BuildNoteIdxChunk(featChunk, channels, modelLen, winLen); + inputs.Add(NamedOnnxValue.CreateFromTensor("note_idx", + new DenseTensor(noteIdxChunk, new int[] { 1, modelLen }))); + } + if (styleId.HasValue && declaredInputs.Contains("style_id")) { + float[] styleArr = new float[] { styleId.Value }; + inputs.Add(NamedOnnxValue.CreateFromTensor("style_id", + new DenseTensor(styleArr, new int[] { 1 }))); + } + Log.Information("[AutoPitch] chunk start={start} winLen={winLen} padLen={padLen} modelLen={modelLen} inputs=[{inp}]", + start, winLen, padLen, modelLen, string.Join(",", inputs.Select(i => i.Name))); + // Log a sample of the feature tensor to verify data is non-zero + int voicedCount = 0; + for (int t = 0; t < winLen; t++) { if (featChunk[1 * modelLen + t] < 0.5f) voicedCount++; } + float featMin = featChunk.Take(channels * winLen).Min(); + float featMax = featChunk.Take(channels * winLen).Max(); + Log.Information("[AutoPitch] featChunk voiced_frames={voiced}/{total} feat_range=[{min:F4},{max:F4}]", + voicedCount, winLen, featMin, featMax); + using var results = session.Run(inputs); + var outputTensor = results.First(r => r.Name == "pitd_base_residual").AsTensor(); + float[] pred = outputTensor.ToArray(); + // Log raw model output stats + float predMin = pred.Take(winLen).Where(v => !float.IsNaN(v)).DefaultIfEmpty(0f).Min(); + float predMax = pred.Take(winLen).Where(v => !float.IsNaN(v)).DefaultIfEmpty(0f).Max(); + bool hasNaN = pred.Any(float.IsNaN); + bool hasInf = pred.Any(float.IsInfinity); + Log.Information("[AutoPitch] raw pred len={len} base_range=[{min:F5},{max:F5}] hasNaN={nan} hasInf={inf}", + pred.Length, predMin, predMax, hasNaN, hasInf); + if (hasNaN) { + Log.Warning("[AutoPitch] NaN in model output (chunk start={start}, winLen={winLen}) — replacing with 0. Likely DirectML numerical instability with long sequences or many notes.", + start, winLen); + for (int i = 0; i < pred.Length; i++) { + if (float.IsNaN(pred[i]) || float.IsInfinity(pred[i])) { + pred[i] = 0f; + } + } + } + for (int t = 0; t < winLen; t++) { + output[start + t] += pred[t]; + output[tLen + start + t] += pred[modelLen + t]; + weight[start + t] += 1.0f; + } + } + + for (int t = 0; t < tLen; t++) { + float w = Math.Max(weight[t], 1.0f); + output[t] /= w; + output[tLen + t] /= w; + } + return output; + } + + /// + /// Per-frame note indices derived from the feature channels, matching + /// PitchTCN._note_idx_from_features on the Python side. + /// + /// Channel 1 is is_rest (1 - voiced) and channel 2 is a one-hot note_onset, + /// so the note segmentation is already present in the features. Padding + /// frames past winLen stay at -1 so they are excluded from attention. + /// + private static long[] BuildNoteIdxChunk(float[] featChunk, int channels, int modelLen, int winLen) { + var noteIdx = new long[modelLen]; + long current = -1; + bool prevVoiced = false; + for (int t = 0; t < modelLen; t++) { + if (t >= winLen || channels < 3) { + noteIdx[t] = -1; + prevVoiced = false; + continue; + } + bool voiced = featChunk[1 * modelLen + t] < 0.5f; + bool onset = featChunk[2 * modelLen + t] > 0.5f; + if (voiced && (onset || !prevVoiced)) { + current++; + } + noteIdx[t] = voiced ? Math.Max(current, 0) : -1; + prevVoiced = voiced; + } + return noteIdx; + } + + private static float[] MovingAverage(float[] data, int window) { + if (window <= 1 || data.Length == 0) { + return data; + } + float[] outData = new float[data.Length]; + int radius = window / 2; + for (int i = 0; i < data.Length; i++) { + int start = Math.Max(0, i - radius); + int end = Math.Min(data.Length, i - radius + window); + float sum = 0.0f; + for (int j = start; j < end; j++) { + sum += data[j]; + } + outData[i] = sum / Math.Max(1, end - start); + } + return outData; + } + + private static SimplifiedCurve SimplifyCurve( + int[] ticks, + float[] pitd, + float[] mask, + float epsilon, + HashSet forcedTicks) { + var xsOut = new List(); + var ysOut = new List(); + foreach (var segment in MaskToSegments(mask)) { + int start = segment.start; + int end = segment.end; + if (end <= start) { + continue; + } + int len = end - start; + int[] xsSeg = new int[len]; + float[] ysSeg = new float[len]; + Array.Copy(ticks, start, xsSeg, 0, len); + Array.Copy(pitd, start, ysSeg, 0, len); + var simplified = RdpSimplify(xsSeg, ysSeg, epsilon); + xsOut.AddRange(simplified.xs); + ysOut.AddRange(simplified.ys); + } + + if (forcedTicks != null && forcedTicks.Count > 0) { + foreach (int tick in forcedTicks.OrderBy(t => t)) { + if (tick < ticks[0] || tick > ticks[^1]) { + continue; + } + float y = Interp(ticks, pitd, tick); + xsOut.Add(tick); + ysOut.Add(y); + } + } + + return new SimplifiedCurve(xsOut, ysOut); + } + + private static void AddBoundaryAnchors( + List xs, List ys, int startTick, int endTick) { + if (xs == null || ys == null || xs.Count == 0 || ys.Count == 0) { + return; + } + if (startTick > endTick) { + return; + } + RemoveTick(xs, ys, startTick); + RemoveTick(xs, ys, endTick); + xs.Add(startTick); + ys.Add(0.0f); + if (endTick != startTick) { + xs.Add(endTick); + ys.Add(0.0f); + } + } + + private static void RemoveTick(List xs, List ys, int tick) { + for (int i = xs.Count - 1; i >= 0; i--) { + if (xs[i] == tick) { + xs.RemoveAt(i); + ys.RemoveAt(i); + } + } + } + + private static IEnumerable<(int start, int end)> MaskToSegments(float[] mask) { + int start = -1; + for (int i = 0; i < mask.Length; i++) { + bool voiced = mask[i] > 0.5f; + if (voiced && start < 0) { + start = i; + } else if (!voiced && start >= 0) { + yield return (start, i); + start = -1; + } + } + if (start >= 0) { + yield return (start, mask.Length); + } + } + + private static SimplifiedCurve RdpSimplify(int[] xs, float[] ys, float epsilon) { + int n = xs.Length; + if (n <= 2) { + return new SimplifiedCurve(xs.ToList(), ys.ToList()); + } + bool[] keep = new bool[n]; + keep[0] = true; + keep[n - 1] = true; + var stack = new Stack<(int start, int end)>(); + stack.Push((0, n - 1)); + + while (stack.Count > 0) { + var (start, end) = stack.Pop(); + if (end - start <= 1) { + continue; + } + int x1 = xs[start]; + float y1 = ys[start]; + int x2 = xs[end]; + float y2 = ys[end]; + double dx = x2 - x1; + double dy = y2 - y1; + double maxDist = -1.0; + int maxIdx = -1; + for (int i = start + 1; i < end; i++) { + double dist; + if (dx == 0 && dy == 0) { + dist = Math.Abs(ys[i] - y1); + } else { + double numer = Math.Abs(dy * xs[i] - dx * ys[i] + x2 * y1 - y2 * x1); + double denom = Math.Sqrt(dx * dx + dy * dy); + dist = numer / (denom + 1e-8); + } + if (dist > maxDist) { + maxDist = dist; + maxIdx = i; + } + } + if (maxIdx >= 0 && maxDist > epsilon) { + keep[maxIdx] = true; + stack.Push((start, maxIdx)); + stack.Push((maxIdx, end)); + } + } + + var outXs = new List(); + var outYs = new List(); + for (int i = 0; i < n; i++) { + if (keep[i]) { + outXs.Add(xs[i]); + outYs.Add(ys[i]); + } + } + return new SimplifiedCurve(outXs, outYs); + } + + private static float Interp(int[] xs, float[] ys, int x) { + int idx = Array.BinarySearch(xs, x); + if (idx >= 0) { + return ys[idx]; + } + idx = ~idx; + if (idx <= 0) { + return ys[0]; + } + if (idx >= xs.Length) { + return ys[^1]; + } + int x0 = xs[idx - 1]; + int x1 = xs[idx]; + float y0 = ys[idx - 1]; + float y1 = ys[idx]; + if (x1 == x0) { + return y0; + } + return y0 + (x - x0) * (y1 - y0) / (x1 - x0); + } + + private static (List xs, List ys) ValidateCurve( + List xs, List ys, float clamp) { + if (xs.Count == 0 || ys.Count == 0) { + return (new List(), new List()); + } + var order = xs + .Select((x, i) => new { x, i }) + .OrderBy(p => p.x) + .Select(p => p.i) + .ToArray(); + var xsOut = new List(); + var ysOut = new List(); + int lastX = int.MinValue; + foreach (int idx in order) { + int x = xs[idx]; + if (x <= lastX) { + continue; + } + float y = Math.Clamp(ys[idx], -clamp, clamp); + xsOut.Add(x); + ysOut.Add((int)MathF.Round(y)); + lastX = x; + } + return (xsOut, ysOut); + } + + private static (List xs, List ys) MergeCurves( + int[] existingXs, int[] existingYs, + List newXs, List newYs, + int rangeStart, int rangeEnd, + float clamp) { + if (existingXs == null || existingYs == null || existingXs.Length == 0 || existingYs.Length == 0) { + return ValidateCurve(newXs, newYs, clamp); + } + var xs = new List(); + var ys = new List(); + for (int i = 0; i < existingXs.Length; i++) { + int x = existingXs[i]; + if (x < rangeStart || x > rangeEnd) { + xs.Add(x); + ys.Add(existingYs[i]); + } + } + xs.AddRange(newXs); + ys.AddRange(newYs); + return ValidateCurve(xs, ys, clamp); + } + + private static int[] BuildEdgeIdsForNotes(List notes) { + // Default EDGE_REST, not 0: ID 0 is "vowel_start_consonant_end". + var parsed = notes.Select(n => ParseSyllableMarker(n.lyric)).ToList(); + var groups = new List>(); + var current = new List(); + for (int i = 0; i < parsed.Count; i++) { + var marker = parsed[i].marker; + if (marker == "continue") { + if (current.Count > 0) { + current.Add(i); + } else if (groups.Count > 0) { + groups[^1].Add(i); + } else { + current.Add(i); + } + continue; + } + if (marker == "new") { + if (current.Count > 0) { + groups.Add(new List(current)); + } + current.Clear(); + current.Add(i); + continue; + } + if (current.Count > 0) { + groups.Add(new List(current)); + current.Clear(); + } + groups.Add(new List { i }); + } + if (current.Count > 0) { + groups.Add(current); + } + // Default to the rest edge class: ID 0 is vowel_start_consonant_end, a real + // sound, so notes yielding no usable token would look like sung vowels. + var edgeIds = new int[notes.Count]; + Array.Fill(edgeIds, EDGE_REST); + foreach (var group in groups) { + string? startToken = null; + string? endToken = null; + foreach (int idx in group) { + var entry = parsed[idx]; + string source = string.IsNullOrWhiteSpace(entry.core) + ? ExtractPrimaryLyricToken(notes[idx].lyric) + : entry.core; + string token = NormalizePhoneme(source); + if (string.IsNullOrWhiteSpace(token)) { + continue; + } + if (startToken == null) { + startToken = token; + } + endToken = token; + } + startToken ??= "R"; + endToken ??= startToken; + int edgeId = ClassifyNoteEdge(startToken, endToken); + foreach (int idx in group) { + edgeIds[idx] = edgeId; + } + } + return edgeIds; + } + + private static int[] BuildEdgeClassIdsForNotes(List notes) { + var parsed = notes.Select(n => ParseSyllableMarker(n.lyric)).ToList(); + var groups = new List>(); + var current = new List(); + for (int i = 0; i < parsed.Count; i++) { + var marker = parsed[i].marker; + if (marker == "continue") { + if (current.Count > 0) { + current.Add(i); + } else if (groups.Count > 0) { + groups[^1].Add(i); + } else { + current.Add(i); + } + continue; + } + if (marker == "new") { + if (current.Count > 0) { + groups.Add(new List(current)); + } + current.Clear(); + current.Add(i); + continue; + } + if (current.Count > 0) { + groups.Add(new List(current)); + current.Clear(); + } + groups.Add(new List { i }); + } + if (current.Count > 0) { + groups.Add(current); + } + // Default to rest/rest in the class-pair space rather than 0 (vowel/vowel). + var edgeIds = new int[notes.Count]; + Array.Fill(edgeIds, ClassifyNoteEdgeClass("R", "R")); + foreach (var group in groups) { + string? startToken = null; + string? endToken = null; + foreach (int idx in group) { + var entry = parsed[idx]; + string source = string.IsNullOrWhiteSpace(entry.core) + ? ExtractPrimaryLyricToken(notes[idx].lyric) + : entry.core; + string token = NormalizePhoneme(source); + if (string.IsNullOrWhiteSpace(token)) { + continue; + } + if (startToken == null) { + startToken = token; + } + endToken = token; + } + startToken ??= "R"; + endToken ??= startToken; + int edgeId = ClassifyNoteEdgeClass(startToken, endToken); + foreach (int idx in group) { + edgeIds[idx] = edgeId; + } + } + return edgeIds; + } + + private static (string marker, string core) ParseSyllableMarker(string lyric) { + string token = ExtractPrimaryLyricToken(lyric); + if (token.StartsWith("+~", StringComparison.Ordinal)) { + return ("continue", token.Substring(2).Trim()); + } + if (token.StartsWith("-", StringComparison.Ordinal)) { + return ("continue", token.Substring(1).Trim()); + } + if (token.StartsWith("+", StringComparison.Ordinal)) { + if (token.Length == 1) { + return ("new", "+"); + } + return ("new", token.Substring(1).Trim()); + } + return ("single", token); + } + + private static string ExtractPrimaryLyricToken(string lyric) { + if (string.IsNullOrWhiteSpace(lyric)) { + return string.Empty; + } + string token = lyric.Trim(); + if (token.Contains(' ')) { + var parts = token.Split(' ', StringSplitOptions.RemoveEmptyEntries); + if (parts.Length > 1) { + token = parts[1]; + } + } + return token.Trim(); + } + + private static string NormalizePhoneme(string lyric) { + if (string.IsNullOrWhiteSpace(lyric)) { + return "R"; + } + string token = ExtractPrimaryLyricToken(lyric); + if (token.StartsWith("+~", StringComparison.Ordinal)) { + token = token.Substring(2).Trim(); + } else if (token.StartsWith("-", StringComparison.Ordinal)) { + token = token.Substring(1).Trim(); + } else if (token.StartsWith("+", StringComparison.Ordinal)) { + if (token.Length > 1) { + token = token.Substring(1).Trim(); + } + } + if (string.IsNullOrWhiteSpace(token)) { + return "R"; + } + token = PhoneticHintPattern.Replace(token, string.Empty); + token = ParenPattern.Replace(token, string.Empty); + token = token.Trim(); + if (ProtectedTokens.Contains(token)) { + return token; + } + if (token == ".hh" || token == "hh") { + return "hh"; + } + if (ContainsKatakana(token)) { + token = KatakanaToHiragana(token); + } + if (TryRomajiToHiragana(token, out string kana)) { + return kana; + } + token = StripBracketsQuotesPattern.Replace(token, string.Empty); + token = AllowedCharsPattern.Replace(token, string.Empty); + token = token.Trim(); + if (TryRomajiToHiragana(token, out kana)) { + return kana; + } + return token; + } + + private static string ResolveLyricConditioning(AutoPitchConfig cfg) { + string mode = cfg.lyric_conditioning ?? cfg.model?.lyric_conditioning ?? string.Empty; + if (mode == "vocab" || mode == "class" || mode == "edge" || mode == "edge_class" || mode == "class_edge" || mode == "none") { + return mode; + } + bool useLyric = cfg.use_lyric || (cfg.model?.use_lyric ?? false); + return useLyric ? "vocab" : "none"; + } + + private static int GetConditioningClassCount(AutoPitchConfig cfg, string lyricConditioning) { + if (cfg?.phoneme_class_names != null && cfg.phoneme_class_names.Count > 0) { + return cfg.phoneme_class_names.Count; + } + return lyricConditioning switch { + "class" => CLASS_COUNT, + "edge" => 7, + "edge_class" => CLASS_COUNT * CLASS_COUNT, + "class_edge" => CLASS_COUNT * EDGE_CLASS_COUNT, + _ => 0, + }; + } + + /// + /// Prefix -> class, longest prefix first. Must mirror + /// phoneme_classes._PREFIX_LOOKUP on the Python side. + /// + /// The previous if-chain tested whole groups in order, so a short prefix in + /// an earlier group masked a longer prefix in a later one: "ts" and "ky" + /// both classified as stop (via "t"/"k") instead of affricate/glide, and + /// the multi-char kana in AffricatePrefixes lost to single-char stops. + /// Ordering by descending prefix length fixes that; the original group + /// order is preserved only as the tie-break between equal-length prefixes. + /// + // Lazily built: static field initializers run in declaration order, and the + // prefix arrays below are declared AFTER this field. Eager initialization + // therefore read them while they were still null and threw + // NullReferenceException from the static constructor. + private static (string prefix, int cls)[]? _prefixLookup; + + private static (string prefix, int cls)[] PrefixLookup => + _prefixLookup ??= BuildPrefixLookup(); + + private static (string prefix, int cls)[] BuildPrefixLookup() { + var groups = new (int cls, string[] prefixes)[] { + (1, NasalPrefixes), + (2, LiquidPrefixes), + (3, SibilantPrefixes), + (6, AffricatePrefixes), + (4, StopPrefixes), + (5, FricativePrefixes), + (7, GlidePrefixes), + }; + var seen = new HashSet(); + var entries = new List<(string prefix, int cls, int order)>(); + int order = 0; + foreach (var (cls, prefixes) in groups) { + foreach (var prefix in prefixes) { + if (string.IsNullOrEmpty(prefix) || !seen.Add(prefix)) { + continue; // first group wins an exact duplicate + } + entries.Add((prefix, cls, order++)); + } + } + return entries + .OrderByDescending(e => e.prefix.Length) + .ThenBy(e => e.order) + .Select(e => (e.prefix, e.cls)) + .ToArray(); + } + + private static int ClassifyPhoneme(string token) { + if (IsRestToken(token)) { + return 9; + } + if (IsNoiseToken(token)) { + return 8; + } + if (VowelTokens.Contains(token)) { + return 0; + } + if (token == "ん") { + return 1; + } + if (!string.IsNullOrEmpty(token)) { + foreach (var (prefix, cls) in PrefixLookup) { + if (token.StartsWith(prefix, StringComparison.Ordinal)) { + return cls; + } + } + } + return 10; + } + + /// + /// Class ID meaning "no note sounding here", per conditioning mode. + /// Must match phoneme_classes.conditioning_rest_id() on the Python side: + /// a mismatch feeds the model conditioning it was never trained on. + /// + private static long ConditioningRestId(string lyricConditioning) { + const int REST = 9; + switch (lyricConditioning) { + case "class": + return REST; + case "edge": + return EDGE_REST; + case "edge_class": + return REST * CLASS_COUNT + REST; + case "class_edge": + return REST * EDGE_CLASS_COUNT + EDGE_REST; + default: + return 0; // "vocab" -> , "none" -> unused + } + } + + private static int ClassifyNoteEdge(string startToken, string endToken) { + string s = startToken?.Trim() ?? string.Empty; + string e = endToken?.Trim() ?? s; + if (IsRestToken(s) && IsRestToken(e)) { + return EDGE_REST; + } + if (IsNoiseToken(s) || IsNoiseToken(e)) { + return EDGE_NOISE; + } + int startKind = GetEdgeStartKind(s); + int endKind = GetEdgeEndKind(e); + if (startKind == SymVowel && endKind == SymConsonant) { + return EDGE_VOWEL_START_CONSONANT_END; + } + if (startKind == SymVowel && endKind == SymVowel) { + return EDGE_VOWEL_START_VOWEL_END; + } + if (startKind == SymConsonant && endKind == SymConsonant) { + return EDGE_CONSONANT_START_CONSONANT_END; + } + if (startKind == SymConsonant && endKind == SymVowel) { + return EDGE_CONSONANT_START_VOWEL_END; + } + return EDGE_OTHER; + } + + private static int ClassifyNoteEdgeClass(string startToken, string endToken) { + int startClass = ClassifyPhoneme(startToken); + int endClass = ClassifyPhoneme(endToken); + return startClass * CLASS_COUNT + endClass; + } + + private static int GetEdgeStartKind(string token) { + if (TryArpabetKinds(token, out int first, out _)) { + return first; + } + if (TryKanaKinds(token, out first, out _)) { + return first; + } + var letters = token.Where(c => char.IsLetter(c) || IsKana(c) || c == '+').ToArray(); + if (letters.Length == 0) { + return SymUnknown; + } + return CharKind(letters[0]); + } + + private static int GetEdgeEndKind(string token) { + if (TryArpabetKinds(token, out _, out int last)) { + return last; + } + if (TryKanaKinds(token, out _, out last)) { + return last; + } + var letters = token.Where(c => char.IsLetter(c) || IsKana(c) || c == '+').ToArray(); + if (letters.Length == 0) { + return SymUnknown; + } + return CharKind(letters[^1]); + } + + private static bool TryArpabetKinds(string token, out int startKind, out int endKind) { + startKind = SymUnknown; + endKind = SymUnknown; + var parts = Regex.Split(token ?? string.Empty, @"[\s/_\-]+") + .Where(p => !string.IsNullOrWhiteSpace(p)) + .ToArray(); + if (parts.Length == 0) { + return false; + } + int[] kinds = new int[parts.Length]; + for (int i = 0; i < parts.Length; i++) { + string phone = ArpabetStressPattern.Replace(parts[i], string.Empty).ToUpperInvariant(); + if (ArpabetVowels.Contains(phone)) { + kinds[i] = SymVowel; + } else if (ArpabetConsonants.Contains(phone)) { + kinds[i] = SymConsonant; + } else { + return false; + } + } + startKind = kinds[0]; + endKind = kinds[^1]; + return true; + } + + private static bool TryKanaKinds(string token, out int startKind, out int endKind) { + startKind = SymUnknown; + endKind = SymUnknown; + var kana = (token ?? string.Empty).Where(IsKana).ToArray(); + if (kana.Length == 0) { + return false; + } + startKind = KanaVowels.Contains(kana[0]) ? SymVowel : SymConsonant; + endKind = KanaConsonantEnd.Contains(kana[^1]) ? SymConsonant : SymVowel; + return true; + } + + private static bool IsRestToken(string token) { + string t = token?.Trim() ?? string.Empty; + return t.Length == 0 || RestTokens.Contains(t) || RestTokensLower.Contains(t.ToLowerInvariant()); + } + + private static bool IsNoiseToken(string token) { + string t = token?.Trim() ?? string.Empty; + return t.Length > 0 && (NoiseTokens.Contains(t) || NoiseTokensLower.Contains(t.ToLowerInvariant())); + } + + private static int CharKind(char ch) { + char lower = char.ToLowerInvariant(ch); + if (EnVowels.Contains(lower) || RuVowels.Contains(lower) || KanaVowels.Contains(ch) || ch == '+') { + return SymVowel; + } + return (char.IsLetter(ch) || IsKana(ch)) ? SymConsonant : SymUnknown; + } + + private static bool IsKana(char ch) { + int code = ch; + return (code >= 0x3040 && code <= 0x309F) || (code >= 0x30A0 && code <= 0x30FF); + } + + private static bool StartsWithAny(string token, string[] prefixes) { + if (string.IsNullOrEmpty(token)) { + return false; + } + foreach (var prefix in prefixes) { + if (token.StartsWith(prefix, StringComparison.Ordinal)) { + return true; + } + } + return false; + } + + private static bool ContainsKatakana(string text) { + foreach (char ch in text) { + int code = ch; + if (code >= 0x30A1 && code <= 0x30F6) { + return true; + } + } + return false; + } + + private static string KatakanaToHiragana(string text) { + var chars = text.ToCharArray(); + for (int i = 0; i < chars.Length; i++) { + int code = chars[i]; + if (code >= 0x30A1 && code <= 0x30F6) { + chars[i] = (char)(code - 0x60); + } + } + return new string(chars); + } + + private static bool TryRomajiToHiragana(string token, out string converted) { + converted = string.Empty; + if (string.IsNullOrWhiteSpace(token)) { + return false; + } + if (token.Any(char.IsUpper)) { + return false; + } + string lower = token.ToLowerInvariant(); + if (!RomajiToHiragana.TryGetValue(lower, out converted!)) { + return false; + } + return true; + } + + private const int EDGE_VOWEL_START_CONSONANT_END = 0; + private const int EDGE_VOWEL_START_VOWEL_END = 1; + private const int EDGE_CONSONANT_START_CONSONANT_END = 2; + private const int EDGE_CONSONANT_START_VOWEL_END = 3; + private const int EDGE_REST = 4; + private const int EDGE_NOISE = 5; + private const int EDGE_OTHER = 6; + private const int EDGE_CLASS_COUNT = 7; + + private const int SymUnknown = 0; + private const int SymVowel = 1; + private const int SymConsonant = 2; + private const int CLASS_COUNT = 11; + + private static readonly Regex PhoneticHintPattern = new Regex(@"\[(.*?)\]", RegexOptions.Compiled); + private static readonly Regex ParenPattern = new Regex(@"\(.*?\)", RegexOptions.Compiled); + private static readonly Regex StripBracketsQuotesPattern = new Regex(@"[\[\]""'`]", RegexOptions.Compiled); + private static readonly Regex AllowedCharsPattern = new Regex(@"[^\u0400-\u04FF\u3040-\u309F\u30A0-\u30FF\u4E00-\u9FFF\u3000-\u303Fa-zA-Z]", RegexOptions.Compiled); + private static readonly Regex ArpabetStressPattern = new Regex(@"\d+$", RegexOptions.Compiled); + + private static readonly HashSet ProtectedTokens = new HashSet(StringComparer.Ordinal) { + "", "R", "br", "bre", "cl", "vf", "hh", "iR", "ahh", "ihh", "uhh", "ohh", "ehh", "+" + }; + private static readonly HashSet RestTokens = new HashSet(StringComparer.Ordinal) { + "", "R", "SP", "R2", "R吸", "R裏", "R・", ".hh", "hh", ".h", "・", + }; + private static readonly HashSet RestTokensLower = + new HashSet(RestTokens.Select(t => t.ToLowerInvariant()), StringComparer.Ordinal); + private static readonly HashSet NoiseTokens = new HashSet(StringComparer.Ordinal) { + "br", "bre", "cl", "vf", "iR", "ahh", "ihh", "uhh", "ohh", "ehh", "AP", "吸", "R囁", + }; + private static readonly HashSet NoiseTokensLower = + new HashSet(NoiseTokens.Select(t => t.ToLowerInvariant()), StringComparer.Ordinal); + private static readonly HashSet VowelTokens = new HashSet(StringComparer.Ordinal) { + "あ", "い", "う", "え", "お", "ぁ", "ぃ", "ぅ", "ぇ", "ぉ", "を", "うぉ", "うぃ", "うぇ", "いぇ", "+" + }; + private static readonly string[] NasalPrefixes = new string[] { + "ん", "な", "に", "ぬ", "ね", "の", "ま", "み", "む", "め", "も", "n", "m", + }; + private static readonly string[] LiquidPrefixes = new string[] { "ら", "り", "る", "れ", "ろ", "l", "r" }; + private static readonly string[] SibilantPrefixes = new string[] { + "さ", "し", "す", "せ", "そ", "ざ", "じ", "ず", "ぜ", "ぞ", + "しゃ", "しゅ", "しょ", "じゃ", "じゅ", "じょ", "すぃ", "ずぃ", + "s", "S", "sh", "SH", + }; + private static readonly string[] AffricatePrefixes = new string[] { + "ち", "つ", "ちゃ", "ちゅ", "ちょ", "ちぇ", "てぃ", "とぅ", "てゅ", + "つぁ", "つぃ", "つぇ", "つぉ", "ぢ", "づ", "ぢゃ", "ぢゅ", "ぢょ", "っ", "ts", "CH", + }; + private static readonly string[] StopPrefixes = new string[] { + "か", "き", "く", "け", "こ", "が", "ぎ", "ぐ", "げ", "ご", + "た", "て", "と", "だ", "で", "ど", "てぃ", "とぅ", "でぃ", "どぅ", + "ぱ", "ぴ", "ぷ", "ぺ", "ぽ", "ば", "び", "ぶ", "べ", "ぼ", + "t", "k", "p", "b", "d", "g", "T", "K", "P", "B", "D", "G", + }; + private static readonly string[] FricativePrefixes = new string[] { + "は", "ひ", "ふ", "へ", "ほ", "ふぁ", "ふぃ", "ふぇ", "ふぉ", "f", "h", "F", "H", "ゔ", "ゔぃ", + }; + private static readonly string[] GlidePrefixes = new string[] { + "や", "ゆ", "よ", "わ", "ゐ", "ゑ", "ゃ", "ゅ", "ょ", "ゎ", "ky", "KY", + }; + + private static readonly HashSet EnVowels = new HashSet("aeiouy"); + private static readonly HashSet RuVowels = new HashSet("аеёиоуыэюя"); + private static readonly HashSet KanaVowels = new HashSet("あいうえおぁぃぅぇぉをゐゑアイウエオ+"); + private static readonly HashSet KanaConsonantEnd = new HashSet("んっンッ"); + + private static readonly HashSet ArpabetVowels = new HashSet(StringComparer.Ordinal) { + "AA", "AE", "AH", "AO", "AW", "AX", "AXR", "AY", "EH", "ER", "EY", "IH", "IX", "IY", "OW", "OY", "UH", "UW", "UX", + }; + private static readonly HashSet ArpabetConsonants = new HashSet(StringComparer.Ordinal) { + "B", "CH", "D", "DH", "DX", "EL", "EM", "EN", "F", "G", "HH", "JH", "K", "L", "M", "N", "NG", "NX", "P", "Q", "R", "S", "SH", "T", "TH", "V", "W", "WH", "Y", "Z", "ZH", + }; + private static readonly Dictionary RomajiToHiragana = new Dictionary(StringComparer.Ordinal) { + {"a", "あ"}, {"i", "い"}, {"u", "う"}, {"e", "え"}, {"o", "お"}, + {"ka", "か"}, {"ki", "き"}, {"ku", "く"}, {"ke", "け"}, {"ko", "こ"}, + {"sa", "さ"}, {"shi", "し"}, {"su", "す"}, {"se", "せ"}, {"so", "そ"}, + {"ta", "た"}, {"chi", "ち"}, {"tsu", "つ"}, {"te", "て"}, {"to", "と"}, + {"na", "な"}, {"ni", "に"}, {"nu", "ぬ"}, {"ne", "ね"}, {"no", "の"}, + {"ha", "は"}, {"hi", "ひ"}, {"fu", "ふ"}, {"he", "へ"}, {"ho", "ほ"}, + {"ma", "ま"}, {"mi", "み"}, {"mu", "む"}, {"me", "め"}, {"mo", "も"}, + {"ya", "や"}, {"yu", "ゆ"}, {"yo", "よ"}, + {"ra", "ら"}, {"ri", "り"}, {"ru", "る"}, {"re", "れ"}, {"ro", "ろ"}, + {"wa", "わ"}, {"wo", "を"}, {"n", "ん"}, + {"ga", "が"}, {"gi", "ぎ"}, {"gu", "ぐ"}, {"ge", "げ"}, {"go", "ご"}, + {"za", "ざ"}, {"ji", "じ"}, {"zu", "ず"}, {"ze", "ぜ"}, {"zo", "ぞ"}, + {"da", "だ"}, {"de", "で"}, {"do", "ど"}, + {"ba", "ば"}, {"bi", "び"}, {"bu", "ぶ"}, {"be", "べ"}, {"bo", "ぼ"}, + {"pa", "ぱ"}, {"pi", "ぴ"}, {"pu", "ぷ"}, {"pe", "ぺ"}, {"po", "ぽ"}, + {"kya", "きゃ"}, {"kyu", "きゅ"}, {"kyo", "きょ"}, + {"gya", "ぎゃ"}, {"gyu", "ぎゅ"}, {"gyo", "ぎょ"}, + {"sha", "しゃ"}, {"shu", "しゅ"}, {"sho", "しょ"}, + {"cha", "ちゃ"}, {"chu", "ちゅ"}, {"cho", "ちょ"}, + {"ja", "じゃ"}, {"ju", "じゅ"}, {"jo", "じょ"}, + {"nya", "にゃ"}, {"nyu", "にゅ"}, {"nyo", "にょ"}, + {"rya", "りゃ"}, {"ryu", "りゅ"}, {"ryo", "りょ"}, + {"ye", "いぇ"}, {"tei", "てい"}, {"ti", "てぃ"}, {"tu", "とぅ"}, + {"fa", "ふぁ"}, {"fi", "ふぃ"}, {"fe", "ふぇ"}, {"fo", "ふぉ"}, + {"+", "+"}, + }; + private class AutoPitchAssets { + public string BasePath; + public InferenceSession Session; + public AutoPitchConfig Config; + public Dictionary Vocab; + public string ConfigPath; + public string VocabPath; + } + + private class FeatureBundle { + public float[] features; + public float[] mask; + public long[]? lyricIds; + + public FeatureBundle(float[] features, float[] mask, long[]? lyricIds) { + this.features = features; + this.mask = mask; + this.lyricIds = lyricIds; + } + } + + private class SimplifiedCurve { + public List xs; + public List ys; + + public SimplifiedCurve(List xs, List ys) { + this.xs = xs; + this.ys = ys; + } + } + + private class AutoPitchConfig { + public DataConfig data = new DataConfig(); + public ModelConfig model = new ModelConfig(); + public InferConfig infer = new InferConfig(); + public bool always_random_seed = true; + public bool stochastic_dropout = false; + public float? dropout_p = null; + public float? feat_noise_std = null; + public float? feat_noise_std_tiny = null; + public bool use_lyric = false; + public string lyric_conditioning = "vocab"; + public int selection_pad_left = 100; + public int selection_pad_right = 50; + public string style = "base"; + public float style_strength = 1.0f; + public List phoneme_class_names = new List(); + } + + private class DataConfig { + public int dt = 2; + public int window_steps = 4096; + public int window_stride = 512; + public float note_len_norm = 480.0f; + public float note_len_clip = 4.0f; + public float note_len_scale = 4.0f; + public float pitch_center = 60.0f; + public float pitch_scale = 24.0f; + public int interval_clip = 24; + public float interval_scale = 24.0f; + public float pitd_clamp = 1200.0f; + public int post_note_tail_ticks = 24; + public int post_note_tail_max_gap_ticks = 96; + // BPM-derived feature config (used when in_channels >= 12) + public float bpm_default = 120.0f; + public float bpm_scale = 120.0f; + public int ticks_per_beat = 480; + public float note_dur_sec_clip = 2.0f; + public float note_dur_sec_scale = 1.0f; + } + + private class ModelConfig { + public int in_channels = 10; + public int channels = 64; + public int num_blocks = 8; + public int kernel_size = 7; + public float dropout = 0.1f; + public bool use_lyric = false; + public string lyric_conditioning = "vocab"; + public int lyric_embed_dim = 16; + public bool style_conditioning = false; + } + + private class InferConfig { + public int smooth_window = 2; + public float rdp_epsilon = 12.0f; + public int chunk_size = 2048; + public int chunk_overlap = 128; + public int pad_ticks = 10; + public float clamp_output = 1200.0f; + } + } + + public class AutoPitchUnload : BatchEdit { + public string Name => "AutoPitch: unload model"; + + public void Run(UProject project, UVoicePart part, List selectedNotes, DocManager docManager) { + AutoPitchEdit.ClearCache(); + } + } +} + diff --git a/OpenUtau.Core/Editing/HarmGenCurrentTrackEdit.cs b/OpenUtau.Core/Editing/HarmGenCurrentTrackEdit.cs new file mode 100644 index 00000000..d04f30a5 --- /dev/null +++ b/OpenUtau.Core/Editing/HarmGenCurrentTrackEdit.cs @@ -0,0 +1,28 @@ +using System.Collections.Generic; +using OpenUtau.Core.Ustx; + +namespace OpenUtau.Core.Editing { + /// + /// Mobile entry point for the harmony generator: harmonizes the track of the part being edited + /// with the Pop style, automatic key detection and default voice ranges. Only the number of + /// voices is configurable. + /// + public class HarmGenCurrentTrackEdit : BatchEdit { + private readonly int numVoices; + + public HarmGenCurrentTrackEdit(int numVoices) { + this.numVoices = System.Math.Clamp(numVoices, 1, 8); + } + + public string Name => "BatchEdit.Action.HarmonyGenerator"; + + public void Run(UProject project, UVoicePart part, List selectedNotes, DocManager docManager) { + var config = new HarmGenConfig { + TrackIndexes = new List { part.trackNo }, + NumVoices = numVoices, + StyleName = "Pop", + }; + new HarmGenEdit(config).Run(project, part, selectedNotes, docManager); + } + } +} diff --git a/OpenUtau.Core/Editing/HarmGenEdit.cs b/OpenUtau.Core/Editing/HarmGenEdit.cs new file mode 100644 index 00000000..a11c3c84 --- /dev/null +++ b/OpenUtau.Core/Editing/HarmGenEdit.cs @@ -0,0 +1,1377 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using OpenUtau.Api; +using OpenUtau.Core; +using OpenUtau.Core.Format; +using OpenUtau.Core.Util; +using OpenUtau.Core.Ustx; + +namespace OpenUtau.Core.Editing { + public sealed class KeyEstimate { + public int KeyIndex { get; } + public string Mode { get; } + public double Score { get; } + public double Confidence { get; } + + public KeyEstimate(int keyIndex, string mode, double score, double confidence) { + KeyIndex = keyIndex; + Mode = mode; + Score = score; + Confidence = confidence; + } + } + + public static class HarmonyAnalysis { + private static readonly double[] MajorProfile = { + 6.35, 2.23, 3.48, 2.33, 4.38, 4.09, 2.52, 3.78, 2.14, 4.04, 2.0, 3.5, + }; + private static readonly double[] MinorProfile = { + 6.33, 2.68, 3.52, 5.38, 2.60, 3.53, 2.54, 2.53, 4.48, 2.42, 3.17, 2.35, + }; + + public static IReadOnlyList DetectKeys( + IEnumerable<(int Tone, int Duration)> notes, + int topN = 3) { + var histogram = new double[12]; + foreach (var note in notes) { + histogram[PositiveMod(note.Tone, 12)] += Math.Max(1, note.Duration); + } + if (histogram.Sum() <= 0) { + return Array.Empty(); + } + + var scores = new List<(int KeyIndex, string Mode, double Score)>(); + for (int keyIndex = 0; keyIndex < 12; keyIndex++) { + scores.Add((keyIndex, "major", Correlation(histogram, MajorProfile, keyIndex))); + scores.Add((keyIndex, "minor", Correlation(histogram, MinorProfile, keyIndex))); + } + scores = scores.OrderByDescending(item => item.Score).ToList(); + + // A softmax makes ambiguity visible instead of reporting the best candidate as 100% by definition. + const double temperature = 8.0; + double maxScore = scores[0].Score; + var weights = scores.Select(item => Math.Exp((item.Score - maxScore) * temperature)).ToArray(); + double totalWeight = weights.Sum(); + int count = Math.Clamp(topN, 1, scores.Count); + return scores.Take(count) + .Select((item, index) => new KeyEstimate( + item.KeyIndex, + item.Mode, + item.Score, + totalWeight > 0 ? weights[index] / totalWeight : 0)) + .ToList(); + } + + internal static int PositiveMod(int value, int divisor) { + int result = value % divisor; + return result < 0 ? result + divisor : result; + } + + private static double Correlation(double[] histogram, double[] profile, int keyIndex) { + double histogramMean = histogram.Average(); + double profileMean = profile.Average(); + double numerator = 0; + double histogramVariance = 0; + double profileVariance = 0; + for (int relativePc = 0; relativePc < 12; relativePc++) { + double histogramValue = histogram[(keyIndex + relativePc) % 12] - histogramMean; + double profileValue = profile[relativePc] - profileMean; + numerator += histogramValue * profileValue; + histogramVariance += histogramValue * histogramValue; + profileVariance += profileValue * profileValue; + } + double denominator = Math.Sqrt(histogramVariance * profileVariance); + return denominator > 1e-9 ? numerator / denominator : 0; + } + } + + public class HarmGenConfig { + public List TrackIndexes { get; set; } = new List(); + public int NumVoices { get; set; } = 2; + public string StyleName { get; set; } = "Pop"; + public bool SustainedChoir { get; set; } = false; + public int WindowSizeIndex { get; set; } = 1; + public bool UseManualKey { get; set; } = false; + public int ManualKeyIndex { get; set; } = 0; + public string ManualMode { get; set; } = "major"; + public bool BlankLyrics { get; set; } = false; + public string LyricReplacement { get; set; } = ""; + public bool AvoidCrossing { get; set; } = true; + public bool RangesRelative { get; set; } = true; + public bool UseDefaultRanges { get; set; } = true; + public List<(int Min, int Max)> VoiceRanges { get; set; } = new List<(int, int)>(); + public int DensityMinDuration { get; set; } = 0; + public int MelismaNoteDuration { get; set; } = 0; + public int MelismaRunLen { get; set; } = 4; + public int HumanizeTiming { get; set; } = 0; + public int HumanizeVelocity { get; set; } = 0; + public int Seed { get; set; } = 0; + public double HarmonyVolume { get; set; } = -5.0; + public bool AllowSus { get; set; } = true; + public bool AllowSevenths { get; set; } = true; + public double ChordConfidenceThreshold { get; set; } = 0.45; + public double StrongBeatWeight { get; set; } = 1.0; + public double WeakBeatWeight { get; set; } = 0.5; + public string? PhonemizerFullName { get; set; } = null; + } + + internal sealed class NoteEvent { + public int Tone { get; } + public int Position { get; } + public int Duration { get; } + public int AbsPosition { get; } + public string Lyric { get; } + public UVoicePart VoicePart { get; } + public UNote Note { get; } + public int NoteIndex { get; } + public int VoicePartIndex { get; } + + public NoteEvent(int tone, int position, int duration, int absPosition, string lyric, UVoicePart voicePart, + UNote note, int noteIndex, int voicePartIndex) { + Tone = tone; + Position = position; + Duration = duration; + AbsPosition = absPosition; + Lyric = lyric; + VoicePart = voicePart; + Note = note; + NoteIndex = noteIndex; + VoicePartIndex = voicePartIndex; + } + } + + internal sealed class ChordInfo { + public int Root { get; } + public string Quality { get; } + public List Tones { get; } + + public ChordInfo(int root, string quality, List tones) { + Root = root; + Quality = quality; + Tones = tones; + } + } + + internal sealed class ChordWindow { + public int Start { get; } + public int End { get; } + public ChordInfo Chord { get; } + public double Confidence { get; } + public bool LowConfidence { get; } + + public ChordWindow(int start, int end, ChordInfo chord, double confidence, bool lowConfidence) { + Start = start; + End = end; + Chord = chord; + Confidence = confidence; + LowConfidence = lowConfidence; + } + } + + internal sealed class ChordCandidate { + public ChordInfo Chord { get; } + public bool IsDiatonic { get; } + public double Prior { get; } + + public ChordCandidate(ChordInfo chord, bool isDiatonic, double prior) { + Chord = chord; + IsDiatonic = isDiatonic; + Prior = prior; + } + } + + internal sealed class StyleProfile { + public string Name { get; } + public List PreferredIntervals { get; } + public double MovementWeight { get; } + public double DissonanceWeight { get; } + public double ChordBonus { get; } + public double ScalePenalty { get; } + public double Jitter { get; } + public double WideIntervalPenalty { get; } + + public StyleProfile( + string name, + List preferredIntervals, + double movementWeight, + double dissonanceWeight, + double chordBonus, + double scalePenalty, + double jitter, + double wideIntervalPenalty) { + Name = name; + PreferredIntervals = preferredIntervals; + MovementWeight = movementWeight; + DissonanceWeight = dissonanceWeight; + ChordBonus = chordBonus; + ScalePenalty = scalePenalty; + Jitter = jitter; + WideIntervalPenalty = wideIntervalPenalty; + } + } + + internal static class StyleProfiles { + private static readonly Dictionary Profiles = new Dictionary { + ["Pop"] = new StyleProfile( + name: "Pop", + preferredIntervals: new List { 3, 4, 7, 8, 9 }, + movementWeight: 0.35, + dissonanceWeight: 2.2, + chordBonus: -1.2, + scalePenalty: 0.4, + jitter: 0.0, + wideIntervalPenalty: 0.6), + ["Choir"] = new StyleProfile( + name: "Choir", + preferredIntervals: new List { 3, 4, 5, 7, 8, 9, 12 }, + movementWeight: 0.25, + dissonanceWeight: 2.6, + chordBonus: -1.4, + scalePenalty: 0.6, + jitter: 0.0, + wideIntervalPenalty: 0.8), + ["Cinematic"] = new StyleProfile( + name: "Cinematic", + preferredIntervals: new List { 7, 12, 19 }, + movementWeight: 0.18, + dissonanceWeight: 1.8, + chordBonus: -1.0, + scalePenalty: 0.5, + jitter: 0.0, + wideIntervalPenalty: 0.4), + ["Randomized Human"] = new StyleProfile( + name: "Randomized Human", + preferredIntervals: new List { 3, 4, 7, 8, 9 }, + movementWeight: 0.35, + dissonanceWeight: 2.1, + chordBonus: -1.1, + scalePenalty: 0.4, + jitter: 0.6, + wideIntervalPenalty: 0.6), + }; + + public static StyleProfile? GetProfile(string name) { + if (Profiles.TryGetValue(name, out var profile)) { + return profile; + } + return null; + } + + public static string[] Names => Profiles.Keys.ToArray(); + } + + internal sealed class HarmonyConfig { + public int NumVoices { get; } + public StyleProfile Style { get; } + public int KeyIndex { get; } + public string Mode { get; } + public bool UseManualKey { get; } + public List ScalePcs { get; } + public int TicksPerBeat { get; } + public int BeatsPerBar { get; } + public int BeatUnit { get; } + public int WindowTicks { get; } + public bool AllowSus { get; } + public bool AllowSevenths { get; } + public double ChordConfidenceThreshold { get; } + public bool AvoidCrossing { get; } + public int DensityMinDuration { get; } + public int MelismaNoteDuration { get; } + public int MelismaRunLen { get; } + public bool BlankLyrics { get; } + public string LyricReplacement { get; } + public int HumanizeTiming { get; } + public int HumanizeVelocity { get; } + public int Seed { get; } + public bool RangesRelative { get; } + public List<(int Min, int Max)> VoiceRanges { get; } + public double StrongBeatWeight { get; } + public double WeakBeatWeight { get; } + + public HarmonyConfig( + int numVoices, + StyleProfile style, + int keyIndex, + string mode, + bool useManualKey, + List scalePcs, + int ticksPerBeat, + int beatsPerBar, + int beatUnit, + int windowTicks, + bool allowSus, + bool allowSevenths, + double chordConfidenceThreshold, + bool avoidCrossing, + int densityMinDuration, + int melismaNoteDuration, + int melismaRunLen, + bool blankLyrics, + string lyricReplacement, + int humanizeTiming, + int humanizeVelocity, + int seed, + bool rangesRelative, + List<(int Min, int Max)> voiceRanges, + double strongBeatWeight, + double weakBeatWeight) { + NumVoices = numVoices; + Style = style; + KeyIndex = keyIndex; + Mode = mode; + UseManualKey = useManualKey; + ScalePcs = scalePcs; + TicksPerBeat = ticksPerBeat; + BeatsPerBar = beatsPerBar; + BeatUnit = beatUnit; + WindowTicks = windowTicks; + AllowSus = allowSus; + AllowSevenths = allowSevenths; + ChordConfidenceThreshold = chordConfidenceThreshold; + AvoidCrossing = avoidCrossing; + DensityMinDuration = densityMinDuration; + MelismaNoteDuration = melismaNoteDuration; + MelismaRunLen = melismaRunLen; + BlankLyrics = blankLyrics; + LyricReplacement = lyricReplacement ?? string.Empty; + HumanizeTiming = humanizeTiming; + HumanizeVelocity = humanizeVelocity; + Seed = seed; + RangesRelative = rangesRelative; + VoiceRanges = voiceRanges; + StrongBeatWeight = strongBeatWeight; + WeakBeatWeight = weakBeatWeight; + } + } + + public class HarmGenEdit : BatchEdit { + public string Name => "BatchEdit.Action.HarmonyGenerator"; + + private readonly HarmGenConfig config; + + public HarmGenEdit(HarmGenConfig config) { + this.config = config; + } + + public void Run(UProject project, UVoicePart part, List selectedNotes, DocManager docManager) { + if (config.TrackIndexes == null || config.TrackIndexes.Count == 0) { + return; + } + + var style = StyleProfiles.GetProfile(config.StyleName) ?? StyleProfiles.GetProfile("Pop")!; + var selectedSet = selectedNotes.Count > 0 ? new HashSet(selectedNotes) : null; + int selectedTrackNo = part.trackNo; + + docManager.StartUndoGroup(Name, true); + try { + (int keyIndex, string mode)? detectedKey = null; + if (!config.UseManualKey) { + var analysisNotes = project.parts.OfType() + .Where(p => config.TrackIndexes.Contains(p.trackNo)) + .SelectMany(p => p.notes) + .Select(note => (note.tone, note.duration)); + var estimate = HarmonyAnalysis.DetectKeys(analysisNotes, 1).FirstOrDefault(); + if (estimate != null) { + detectedKey = (estimate.KeyIndex, estimate.Mode); + } + } + + foreach (var trackIndex in config.TrackIndexes.Distinct()) { + if (trackIndex < 0 || trackIndex >= project.tracks.Count) { + continue; + } + GenerateForTrack(project, trackIndex, style, docManager, selectedSet, selectedTrackNo, detectedKey); + } + } finally { + docManager.EndUndoGroup(); + } + } + + private void GenerateForTrack(UProject project, int trackIndex, StyleProfile style, DocManager docManager, + HashSet? selectedSet, int selectedTrackNo, (int keyIndex, string mode)? detectedKey) { + var allNotes = GetNotesForTrack(project, trackIndex); + if (allNotes.Count == 0) { + return; + } + var notes = allNotes; + if (selectedSet != null && trackIndex == selectedTrackNo) { + bool fullTrackSelected = allNotes.Count > 0 && allNotes.All(n => selectedSet.Contains(n.Note)); + if (!fullTrackSelected) { + notes = allNotes.Where(n => selectedSet.Contains(n.Note)).ToList(); + if (notes.Count == 0) { + return; + } + } + } + + int ticksPerBeat = project.resolution; + var (beatsPerBar, beatUnit) = GetTimeSignature(project); + int beatTicks = (int)(ticksPerBeat * (4.0 / beatUnit)); + int windowTicks = config.WindowSizeIndex switch { + 0 => beatTicks * beatsPerBar, + 1 => beatTicks * 2, + _ => beatTicks + }; + + int keyIndex; + string mode; + if (config.UseManualKey) { + keyIndex = Math.Clamp(config.ManualKeyIndex, 0, 11); + mode = config.ManualMode == "minor" ? "minor" : "major"; + } else if (detectedKey.HasValue) { + keyIndex = detectedKey.Value.keyIndex; + mode = detectedKey.Value.mode; + } else { + (keyIndex, mode) = DetectKey(allNotes); + } + + var scalePcs = GetScalePcs(keyIndex, mode); + var chords = EstimateChords( + allNotes, + keyIndex, + mode, + windowTicks, + config.AllowSus, + config.AllowSevenths, + config.ChordConfidenceThreshold, + strictDiatonic: config.UseManualKey, + ticksPerBeat, + beatsPerBar, + beatUnit, + config.StrongBeatWeight, + config.WeakBeatWeight); + + int voiceCount = Math.Clamp(config.NumVoices, 1, 8); + var genConfig = new HarmonyConfig( + numVoices: voiceCount, + style: style, + keyIndex: keyIndex, + mode: mode, + useManualKey: config.UseManualKey, + scalePcs: scalePcs, + ticksPerBeat: ticksPerBeat, + beatsPerBar: beatsPerBar, + beatUnit: beatUnit, + windowTicks: windowTicks, + allowSus: config.AllowSus, + allowSevenths: config.AllowSevenths, + chordConfidenceThreshold: config.ChordConfidenceThreshold, + avoidCrossing: config.AvoidCrossing, + densityMinDuration: config.DensityMinDuration, + melismaNoteDuration: config.MelismaNoteDuration, + melismaRunLen: config.MelismaRunLen, + blankLyrics: config.BlankLyrics, + lyricReplacement: config.LyricReplacement, + humanizeTiming: config.HumanizeTiming, + humanizeVelocity: config.HumanizeVelocity, + seed: config.Seed, + rangesRelative: config.RangesRelative, + voiceRanges: config.UseDefaultRanges + ? DefaultVoiceRanges(style.Name, voiceCount, config.SustainedChoir) + : NormalizeRanges(config.VoiceRanges, voiceCount, config.SustainedChoir), + strongBeatWeight: config.StrongBeatWeight, + weakBeatWeight: config.WeakBeatWeight + ); + + if (config.SustainedChoir) { + var sustainedNotes = GenerateSustainedHarmonies(notes, chords, genConfig, config.LyricReplacement); + AppendSustainedTracks(project, trackIndex, sustainedNotes, genConfig, config, docManager); + } else { + var harmonyNotes = GenerateHarmonies(notes, chords, genConfig); + AppendTracks(project, trackIndex, harmonyNotes, notes, genConfig, config, docManager); + } + } + + private static List<(int Min, int Max)> NormalizeRanges( + List<(int Min, int Max)> ranges, + int numVoices, + bool sustainedChoir) { + var result = new List<(int Min, int Max)>(); + var fallback = DefaultVoiceRanges("Choir", numVoices, sustainedChoir); + for (int i = 0; i < numVoices; i++) { + if (ranges != null && i < ranges.Count) { + result.Add(ranges[i]); + } else { + result.Add(i < fallback.Count ? fallback[i] : (-8, 8)); + } + } + return result; + } + + private static (int beatsPerBar, int beatUnit) GetTimeSignature(UProject project) { + if (project.timeSignatures != null && project.timeSignatures.Count > 0) { + var first = project.timeSignatures[0]; + return (first.beatPerBar, first.beatUnit); + } + return (4, 4); + } + + private static List GetNotesForTrack(UProject project, int trackNo) { + var notes = new List(); + var voiceParts = project.parts.OfType() + .Where(p => p.trackNo == trackNo) + .OrderBy(p => p.position) + .ToList(); + for (int vpIndex = 0; vpIndex < voiceParts.Count; vpIndex++) { + var voicePart = voiceParts[vpIndex]; + int vpPosition = voicePart.position; + int noteIndex = 0; + foreach (var note in voicePart.notes) { + notes.Add(new NoteEvent( + tone: note.tone, + position: note.position, + duration: note.duration, + absPosition: vpPosition + note.position, + lyric: note.lyric, + voicePart: voicePart, + note: note, + noteIndex: noteIndex, + voicePartIndex: vpIndex)); + noteIndex++; + } + } + notes.Sort((a, b) => { + var cmp = a.AbsPosition.CompareTo(b.AbsPosition); + if (cmp != 0) return cmp; + cmp = a.Duration.CompareTo(b.Duration); + if (cmp != 0) return cmp; + return a.NoteIndex.CompareTo(b.NoteIndex); + }); + return notes; + } + + private static (int keyIndex, string mode) DetectKey(List notes) { + var result = HarmonyAnalysis.DetectKeys(notes.Select(note => (note.Tone, note.Duration)), 1) + .FirstOrDefault(); + return result == null ? (0, "major") : (result.KeyIndex, result.Mode); + } + + private static List GetScalePcs(int keyIndex, string mode) { + int[] majorIntervals = { 0, 2, 4, 5, 7, 9, 11 }; + int[] minorIntervals = { 0, 2, 3, 5, 7, 8, 10 }; + var intervals = mode == "minor" ? minorIntervals : majorIntervals; + return intervals.Select(interval => (keyIndex + interval) % 12).ToList(); + } + + private static List GetTonicChordPcs(int keyIndex, string mode) { + var intervals = mode == "minor" ? new[] { 0, 3, 7 } : new[] { 0, 4, 7 }; + return intervals.Select(interval => HarmonyAnalysis.PositiveMod(keyIndex + interval, 12)).ToList(); + } + + internal static List EstimateChords( + List notes, + int keyIndex, + string mode, + int windowTicks, + bool allowSus, + bool allowSevenths, + double confidenceThreshold, + bool strictDiatonic, + int ticksPerBeat, + int beatsPerBar, + int beatUnit, + double strongBeatWeight, + double weakBeatWeight) { + var windows = new List(); + if (notes.Count == 0 || windowTicks <= 0) { + return windows; + } + int minStart = notes.Min(n => n.AbsPosition); + int maxEnd = notes.Max(n => n.AbsPosition + n.Duration); + var scalePcs = GetScalePcs(keyIndex, mode); + + var triads = new Dictionary { + ["maj"] = new[] { 0, 4, 7 }, + ["min"] = new[] { 0, 3, 7 }, + ["dim"] = new[] { 0, 3, 6 }, + }; + if (allowSus) { + triads["sus2"] = new[] { 0, 2, 7 }; + triads["sus4"] = new[] { 0, 5, 7 }; + } + var sevenths = new Dictionary { + ["maj7"] = new[] { 0, 4, 7, 11 }, + ["7"] = new[] { 0, 4, 7, 10 }, + ["min7"] = new[] { 0, 3, 7, 10 }, + ["m7b5"] = new[] { 0, 3, 6, 10 }, + }; + + var candidates = new List(); + for (int root = 0; root < 12; root++) { + foreach (var kv in triads) { + AddChordCandidate(candidates, root, kv.Key, kv.Value, scalePcs, keyIndex, mode, strictDiatonic); + } + if (allowSevenths) { + foreach (var kv in sevenths) { + AddChordCandidate(candidates, root, kv.Key, kv.Value, scalePcs, keyIndex, mode, strictDiatonic); + } + } + } + if (candidates.Count == 0) { + candidates.Add(new ChordCandidate(new ChordInfo(keyIndex, "scale", scalePcs), true, 0)); + } + + int firstWindow = (int)Math.Floor(minStart / (double)windowTicks) * windowTicks; + var observations = new List(); + for (int start = firstWindow; start < maxEnd; start += windowTicks) { + int end = start + windowTicks; + double[] hist = new double[12]; + double total = 0.0; + foreach (var note in notes) { + int noteStart = note.AbsPosition; + int noteEnd = note.AbsPosition + note.Duration; + int overlap = Math.Min(noteEnd, end) - Math.Max(noteStart, start); + if (overlap <= 0) { + continue; + } + double beatFactor = StrongBeatFactor(note.AbsPosition, ticksPerBeat, beatsPerBar, beatUnit); + double beatWeight = Math.Max(0.05, weakBeatWeight + beatFactor * (strongBeatWeight - weakBeatWeight)); + double weightedOverlap = overlap * beatWeight; + hist[HarmonyAnalysis.PositiveMod(note.Tone, 12)] += weightedOverlap; + total += weightedOverlap; + } + observations.Add(BuildChordObservation(candidates, hist, total, keyIndex, mode)); + } + + if (observations.Count == 0) { + return windows; + } + + var dp = new double[observations.Count, candidates.Count]; + var back = new int[observations.Count, candidates.Count]; + for (int candidateIndex = 0; candidateIndex < candidates.Count; candidateIndex++) { + dp[0, candidateIndex] = observations[0][candidateIndex] + InitialChordPrior(candidates[candidateIndex], keyIndex); + back[0, candidateIndex] = -1; + } + for (int windowIndex = 1; windowIndex < observations.Count; windowIndex++) { + for (int candidateIndex = 0; candidateIndex < candidates.Count; candidateIndex++) { + double best = double.NegativeInfinity; + int bestPrevious = 0; + for (int previousIndex = 0; previousIndex < candidates.Count; previousIndex++) { + double score = dp[windowIndex - 1, previousIndex] + + TransitionScore(candidates[previousIndex], candidates[candidateIndex], keyIndex); + if (score > best) { + best = score; + bestPrevious = previousIndex; + } + } + dp[windowIndex, candidateIndex] = observations[windowIndex][candidateIndex] + best; + back[windowIndex, candidateIndex] = bestPrevious; + } + } + + int selectedIndex = 0; + double finalScore = double.NegativeInfinity; + for (int candidateIndex = 0; candidateIndex < candidates.Count; candidateIndex++) { + if (dp[observations.Count - 1, candidateIndex] > finalScore) { + finalScore = dp[observations.Count - 1, candidateIndex]; + selectedIndex = candidateIndex; + } + } + var selected = new int[observations.Count]; + for (int windowIndex = observations.Count - 1; windowIndex >= 0; windowIndex--) { + selected[windowIndex] = selectedIndex; + selectedIndex = back[windowIndex, selectedIndex]; + if (selectedIndex < 0) { + break; + } + } + + for (int windowIndex = 0; windowIndex < observations.Count; windowIndex++) { + int start = firstWindow + windowIndex * windowTicks; + int end = start + windowTicks; + int candidateIndex = selected[windowIndex]; + double selectedObservation = observations[windowIndex][candidateIndex]; + double nextBest = observations[windowIndex] + .Where((_, index) => index != candidateIndex) + .DefaultIfEmpty(double.NegativeInfinity) + .Max(); + double margin = selectedObservation - nextBest; + double confidence = double.IsNegativeInfinity(margin) + ? 0 + : 1.0 - Math.Exp(-Math.Max(0, margin)); + windows.Add(new ChordWindow( + start, + end, + candidates[candidateIndex].Chord, + confidence, + confidence < confidenceThreshold)); + } + return windows; + } + + private static void AddChordCandidate( + List candidates, + int root, + string quality, + int[] intervals, + List scalePcs, + int keyIndex, + string mode, + bool strictDiatonic) { + var tones = intervals.Select(interval => HarmonyAnalysis.PositiveMod(root + interval, 12)).ToList(); + bool diatonic = tones.All(scalePcs.Contains) && scalePcs.Contains(root); + bool minorDominant = mode == "minor" + && root == HarmonyAnalysis.PositiveMod(keyIndex + 7, 12) + && (quality == "maj" || quality == "7"); + if (minorDominant) { + diatonic = true; + } + if (strictDiatonic && !diatonic) { + return; + } + double prior = diatonic ? 0.40 : -0.90; + if (root == keyIndex) prior += 0.15; + if (root == HarmonyAnalysis.PositiveMod(keyIndex + 7, 12)) prior += 0.08; + if (root == HarmonyAnalysis.PositiveMod(keyIndex + 5, 12)) prior += 0.06; + if (quality.StartsWith("sus", StringComparison.Ordinal)) prior -= 0.22; + if (quality == "dim" || quality == "m7b5") prior -= 0.18; + if (quality.Contains('7')) prior -= 0.04; + candidates.Add(new ChordCandidate(new ChordInfo(root, quality, tones), diatonic, prior)); + } + + private static double[] BuildChordObservation( + List candidates, + double[] hist, + double total, + int keyIndex, + string mode) { + var scores = new double[candidates.Count]; + if (total <= 0) { + for (int i = 0; i < candidates.Count; i++) { + scores[i] = candidates[i].Prior; + } + return scores; + } + for (int i = 0; i < candidates.Count; i++) { + var candidate = candidates[i]; + double chordWeight = candidate.Chord.Tones.Sum(pc => hist[pc]); + double nonChordWeight = Math.Max(0, total - chordWeight); + double coverage = chordWeight / total; + double score = coverage * 4.0 - (nonChordWeight / total) * 2.5 + candidate.Prior; + int root = candidate.Chord.Root; + int third = candidate.Chord.Tones.Count > 1 ? candidate.Chord.Tones[1] : root; + int fifth = candidate.Chord.Tones.Count > 2 ? candidate.Chord.Tones[2] : root; + score += hist[root] / total * 0.12; + score += hist[third] / total * 0.08; + score += hist[fifth] / total * 0.05; + scores[i] = score; + } + return scores; + } + + private static double InitialChordPrior(ChordCandidate candidate, int keyIndex) { + if (candidate.Chord.Root == keyIndex && candidate.Chord.Quality == "maj") return 0.30; + if (candidate.Chord.Root == keyIndex && candidate.Chord.Quality == "min") return 0.30; + if (candidate.Chord.Root == HarmonyAnalysis.PositiveMod(keyIndex + 5, 12)) return 0.12; + if (candidate.Chord.Root == HarmonyAnalysis.PositiveMod(keyIndex + 7, 12)) return 0.10; + return 0; + } + + private static double TransitionScore(ChordCandidate previous, ChordCandidate current, int keyIndex) { + if (previous.Chord.Root == current.Chord.Root && previous.Chord.Quality == current.Chord.Quality) { + return 0.42; + } + int commonTones = previous.Chord.Tones.Intersect(current.Chord.Tones).Count(); + int rootDistance = HarmonyAnalysis.PositiveMod(current.Chord.Root - previous.Chord.Root, 12); + double score = commonTones * 0.08 - 0.12; + if (rootDistance == 5 || rootDistance == 7) score += 0.22; + if (previous.Chord.Root == HarmonyAnalysis.PositiveMod(keyIndex + 7, 12) + && current.Chord.Root == keyIndex) { + score += 0.35; + } + if (previous.Chord.Root == HarmonyAnalysis.PositiveMod(keyIndex + 2, 12) + && current.Chord.Root == HarmonyAnalysis.PositiveMod(keyIndex + 7, 12)) { + score += 0.18; + } + return score; + } + + private static List> GenerateHarmonies( + List notes, + List chords, + HarmonyConfig config) { + var pitchRng = new Random(config.Seed); + var timingRng = new Random(unchecked(config.Seed ^ 0x4D3A2F11)); + var velocityRng = new Random(unchecked(config.Seed ^ 0x17C9A5E3)); + var harmonyNotes = new List>(); + for (int i = 0; i < config.NumVoices; i++) { + harmonyNotes.Add(Enumerable.Repeat(null, notes.Count).ToList()); + } + + var prevPitches = Enumerable.Repeat(null, config.NumVoices).ToList(); + var voiceOrder = ChooseVoiceOrder(config.VoiceRanges); + var orderMap = voiceOrder.Select((voiceIdx, order) => (voiceIdx, order)) + .ToDictionary(x => x.voiceIdx, x => x.order); + NoteEvent? previousNote = null; + List? stableChordTones = null; + + for (int idx = 0; idx < notes.Count; idx++) { + var note = notes[idx]; + if (ShouldSkipNote(note, notes, idx, config)) { + continue; + } + if (previousNote != null + && (previousNote.VoicePart != note.VoicePart + || note.AbsPosition - (previousNote.AbsPosition + previousNote.Duration) > config.TicksPerBeat * 2)) { + for (int voiceIdx = 0; voiceIdx < config.NumVoices; voiceIdx++) { + prevPitches[voiceIdx] = null; + } + } + var chordWindow = GetChordForPosition(chords, note.AbsPosition); + bool uncertainChord = chordWindow?.LowConfidence == true; + var chordTones = chordWindow == null + ? config.ScalePcs + : uncertainChord + ? (stableChordTones ?? GetTonicChordPcs(config.KeyIndex, config.Mode)) + : chordWindow.Chord.Tones; + if (!uncertainChord && chordWindow != null) { + stableChordTones = chordWindow.Chord.Tones; + } + var candidatePcs = chordTones.Concat(config.ScalePcs).Distinct().ToList(); + double chordStrength = uncertainChord ? 0.35 : 1.0; + + double beatFactor = StrongBeatFactor(note.AbsPosition, config.TicksPerBeat, config.BeatsPerBar, config.BeatUnit); + double dissonanceScale = config.Style.DissonanceWeight * (beatFactor >= 0.9 ? config.StrongBeatWeight : config.WeakBeatWeight); + + var chosenPitches = new Dictionary(); + foreach (var voiceIdx in voiceOrder) { + var range = config.VoiceRanges[voiceIdx]; + int minPitch = config.RangesRelative ? note.Tone + range.Min : range.Min; + int maxPitch = config.RangesRelative ? note.Tone + range.Max : range.Max; + var candidates = BuildCandidates(candidatePcs, minPitch, maxPitch); + bool crossingEnforced = false; + bool sideEnforced = false; + if (config.AvoidCrossing && chosenPitches.Count > 0) { + int lowerMax = chosenPitches.Values.Max(); + var nonCrossing = candidates.Where(candidate => candidate > lowerMax).ToList(); + if (nonCrossing.Count > 0) { + candidates = nonCrossing; + crossingEnforced = true; + } + } + if (config.AvoidCrossing && config.RangesRelative) { + var side = ExpectedSide(range.Min, range.Max); + var sideConstrained = side switch { + "below" => candidates.Where(candidate => candidate < note.Tone).ToList(), + "above" => candidates.Where(candidate => candidate > note.Tone).ToList(), + _ => candidates, + }; + if (sideConstrained.Count > 0) { + candidates = sideConstrained; + sideEnforced = true; + } + } + + double bestCost = double.PositiveInfinity; + int bestPitch = note.Tone; + foreach (var cand in candidates) { + double cost = 0.0; + if (chordTones.Contains(HarmonyAnalysis.PositiveMod(cand, 12))) { + cost += config.Style.ChordBonus * chordStrength; + } else { + cost += config.Style.ScalePenalty; + } + int interval = cand - note.Tone; + cost += IntervalCost(interval, config.Style); + + if (prevPitches[voiceIdx] != null) { + int move = Math.Abs(cand - prevPitches[voiceIdx]!.Value); + cost += move * config.Style.MovementWeight; + if (move > 12) { + cost += config.Style.WideIntervalPenalty * (move - 12); + } + } + int intervalClass = Math.Abs(interval) % 12; + if (intervalClass == 1 || intervalClass == 2 || intervalClass == 6 || intervalClass == 11) { + cost += dissonanceScale; + } + if (config.AvoidCrossing) { + if (!crossingEnforced) { + foreach (var other in chosenPitches) { + if (orderMap[voiceIdx] > orderMap[other.Key] && cand <= other.Value) { + cost += 4.0; + } + if (orderMap[voiceIdx] < orderMap[other.Key] && cand >= other.Value) { + cost += 4.0; + } + } + } + if (config.RangesRelative && !sideEnforced) { + var side = ExpectedSide(range.Min, range.Max); + if (side == "below" && cand >= note.Tone) { + cost += 4.5; + } + if (side == "above" && cand <= note.Tone) { + cost += 4.5; + } + } + } + if (chosenPitches.Values.Contains(cand)) { + cost += 1.25; + } else if (chosenPitches.Values.Any(other => HarmonyAnalysis.PositiveMod(other, 12) == HarmonyAnalysis.PositiveMod(cand, 12))) { + cost += 0.18; + } + if (config.Style.Jitter > 0.0) { + cost += (pitchRng.NextDouble() * 2.0 - 1.0) * config.Style.Jitter; + } + if (cost < bestCost) { + bestCost = cost; + bestPitch = cand; + } + } + chosenPitches[voiceIdx] = bestPitch; + } + + foreach (var kv in chosenPitches) { + var voiceIdx = kv.Key; + int pitch = kv.Value; + var newNote = notes[idx].Note.Clone() as UNote; + if (newNote == null) { + continue; + } + newNote.tone = pitch; + if (config.BlankLyrics) { + newNote.lyric = string.Empty; + } else if (!string.IsNullOrWhiteSpace(config.LyricReplacement)) { + newNote.lyric = config.LyricReplacement; + } + if (config.HumanizeTiming > 0) { + int offset = timingRng.Next(-config.HumanizeTiming, config.HumanizeTiming + 1); + int maxPosition = Math.Max(0, note.VoicePart.duration - newNote.duration); + newNote.position = Math.Clamp(newNote.position + offset, 0, maxPosition); + } + if (config.HumanizeVelocity > 0) { + ApplyVelocityHumanize(newNote, config.HumanizeVelocity, velocityRng); + } + harmonyNotes[voiceIdx][idx] = newNote; + } + + for (int voiceIdx = 0; voiceIdx < config.NumVoices; voiceIdx++) { + var h = harmonyNotes[voiceIdx][idx]; + if (h != null) { + prevPitches[voiceIdx] = h.tone; + } + } + previousNote = note; + } + + return harmonyNotes; + } + + internal static List> GenerateSustainedHarmonies( + List targetNotes, + List chords, + HarmonyConfig config, + string lyric) { + var result = Enumerable.Range(0, config.NumVoices) + .Select(_ => new List()) + .ToList(); + if (targetNotes.Count == 0 || chords.Count == 0) { + return result; + } + + int targetStart = targetNotes.Min(note => note.AbsPosition); + int targetEnd = targetNotes.Max(note => note.AbsPosition + note.Duration); + int fallbackAnchor = targetNotes.Select(note => note.Tone).OrderBy(tone => tone) + .ElementAt(targetNotes.Count / 2); + string padLyric = config.BlankLyrics + ? string.Empty + : (string.IsNullOrWhiteSpace(lyric) ? "a" : lyric.Trim()); + var previousPitches = Enumerable.Repeat(null, config.NumVoices).ToArray(); + var voiceOrder = ChooseVoiceOrder(config.VoiceRanges); + List? stableChordTones = null; + + foreach (var chordWindow in chords.Where(chord => chord.End > targetStart && chord.Start < targetEnd)) { + int start = Math.Max(targetStart, chordWindow.Start); + int end = Math.Min(targetEnd, chordWindow.End); + if (end - start < 10) { + continue; + } + // Keep the pad's register stable even when the lead melody moves between windows. + int anchor = fallbackAnchor; + var chordTones = chordWindow.LowConfidence + ? (stableChordTones ?? GetTonicChordPcs(config.KeyIndex, config.Mode)) + : chordWindow.Chord.Tones; + if (!chordWindow.LowConfidence) { + stableChordTones = chordWindow.Chord.Tones; + } + var chosen = new Dictionary(); + foreach (int voiceIndex in voiceOrder) { + var range = config.VoiceRanges[voiceIndex]; + int minPitch = config.RangesRelative ? anchor + range.Min : range.Min; + int maxPitch = config.RangesRelative ? anchor + range.Max : range.Max; + var candidates = BuildCandidates(chordTones, minPitch, maxPitch); + if (config.AvoidCrossing && chosen.Count > 0) { + int lowerPitch = chosen.Values.Max(); + var orderedCandidates = candidates.Where(candidate => candidate > lowerPitch).ToList(); + if (orderedCandidates.Count > 0) { + candidates = orderedCandidates; + } + } + + double center = (minPitch + maxPitch) / 2.0; + double bestCost = double.PositiveInfinity; + int bestPitch = candidates[0]; + foreach (int candidate in candidates) { + double cost = Math.Abs(candidate - center) * 0.08; + if (previousPitches[voiceIndex].HasValue) { + cost += Math.Abs(candidate - previousPitches[voiceIndex]!.Value) + * config.Style.MovementWeight; + } + cost += IntervalCost(candidate - anchor, config.Style) * 0.35; + if (chosen.Values.Contains(candidate)) { + cost += 8.0; + } + int pitchClass = HarmonyAnalysis.PositiveMod(candidate, 12); + int duplicatePitchClasses = chosen.Values.Count( + other => HarmonyAnalysis.PositiveMod(other, 12) == pitchClass); + cost += duplicatePitchClasses * 0.30; + if (cost < bestCost) { + bestCost = cost; + bestPitch = candidate; + } + } + chosen[voiceIndex] = bestPitch; + } + + foreach (var item in chosen) { + int voiceIndex = item.Key; + int pitch = item.Value; + var previous = result[voiceIndex].LastOrDefault(); + if (previous != null && previous.End == start && previous.tone == pitch) { + previous.duration += end - start; + } else { + var note = CreateDefaultHarmonyNote(); + note.position = start; + note.duration = end - start; + note.tone = pitch; + note.lyric = padLyric; + result[voiceIndex].Add(note); + } + previousPitches[voiceIndex] = pitch; + } + } + return result; + } + + private static UNote CreateDefaultHarmonyNote() { + var note = UNote.Create(); + int start = NotePresets.Default.DefaultPortamento.PortamentoStart; + int length = NotePresets.Default.DefaultPortamento.PortamentoLength; + var shape = NotePresets.Default.DefaultPitchShape; + note.pitch.AddPoint(new PitchPoint(start, 0, shape)); + note.pitch.AddPoint(new PitchPoint(start + length, 0, shape)); + return note; + } + + private static void ApplyVelocityHumanize(UNote note, int maxDelta, Random rng) { + if (note.phonemeExpressions == null || note.phonemeExpressions.Count == 0) { + return; + } + foreach (var exp in note.phonemeExpressions) { + if (exp.descriptor?.abbr != Format.Ustx.VEL) { + continue; + } + int delta = rng.Next(-maxDelta, maxDelta + 1); + exp.value = Math.Clamp(exp.value + delta, exp.descriptor.min, exp.descriptor.max); + } + } + + internal static List BuildCandidates(List candidatePcs, int minPitch, int maxPitch) { + if (minPitch > maxPitch) { + (minPitch, maxPitch) = (maxPitch, minPitch); + } + var candidates = new List(); + for (int pitch = minPitch; pitch <= maxPitch; pitch++) { + if (candidatePcs.Contains(HarmonyAnalysis.PositiveMod(pitch, 12))) { + candidates.Add(pitch); + } + } + if (candidates.Count == 0) { + for (int pitch = minPitch; pitch <= maxPitch; pitch++) { + candidates.Add(pitch); + } + } + return candidates; + } + + internal static double IntervalCost(int interval, StyleProfile style) { + int absoluteInterval = Math.Abs(interval); + if (style.PreferredIntervals.Contains(absoluteInterval)) { + return 0.0; + } + int intervalClass = absoluteInterval % 12; + if (absoluteInterval > 12 && style.PreferredIntervals.Contains(intervalClass)) { + return 0.2; + } + if (intervalClass == 0) { + return absoluteInterval == 0 ? 0.8 : 0.35; + } + if (intervalClass == 1 || intervalClass == 2 || intervalClass == 6 || intervalClass == 11) { + return 2.5; + } + return 0.8; + } + + private static List ChooseVoiceOrder(List<(int Min, int Max)> voiceRanges) { + var centers = new List<(double center, int idx)>(); + for (int i = 0; i < voiceRanges.Count; i++) { + var r = voiceRanges[i]; + centers.Add(((r.Min + r.Max) / 2.0, i)); + } + return centers.OrderBy(c => c.center).Select(c => c.idx).ToList(); + } + + private static string? ExpectedSide(int rangeMin, int rangeMax) { + if (rangeMax < 0) { + return "below"; + } + if (rangeMin > 0) { + return "above"; + } + return null; + } + + private static bool ShouldSkipNote(NoteEvent note, List notes, int index, HarmonyConfig config) { + if (config.DensityMinDuration > 0 && note.Duration < config.DensityMinDuration) { + return true; + } + if (config.MelismaNoteDuration <= 0 || config.MelismaRunLen <= 1) { + return false; + } + if (note.Duration > config.MelismaNoteDuration) { + return false; + } + int runLen = 1; + var current = note; + for (int i = index - 1; i >= 0; i--) { + var previous = notes[i]; + int gap = current.AbsPosition - (previous.AbsPosition + previous.Duration); + if (previous.VoicePart != note.VoicePart + || previous.Duration > config.MelismaNoteDuration + || gap > config.MelismaNoteDuration) { + break; + } + runLen += 1; + current = previous; + } + current = note; + for (int i = index + 1; i < notes.Count; i++) { + var next = notes[i]; + int gap = next.AbsPosition - (current.AbsPosition + current.Duration); + if (next.VoicePart != note.VoicePart + || next.Duration > config.MelismaNoteDuration + || gap > config.MelismaNoteDuration) { + break; + } + runLen += 1; + current = next; + } + return runLen >= config.MelismaRunLen; + } + + private static ChordWindow? GetChordForPosition(List chords, int position) { + if (chords == null || chords.Count == 0) { + return null; + } + foreach (var chord in chords) { + if (chord.Start <= position && position < chord.End) { + return chord; + } + } + return chords[^1]; + } + + private static double StrongBeatFactor(int position, int ticksPerBeat, int beatsPerBar, int beatUnit) { + int beatTicks = (int)(ticksPerBeat * (4.0 / beatUnit)); + int barTicks = beatTicks * beatsPerBar; + if (barTicks > 0 && HarmonyAnalysis.PositiveMod(position, barTicks) == 0) { + return 1.0; + } + if (beatTicks > 0 && HarmonyAnalysis.PositiveMod(position, beatTicks) == 0) { + return 0.7; + } + return 0.4; + } + + private static List<(int Min, int Max)> DefaultVoiceRanges( + string styleName, + int numVoices, + bool sustainedChoir) { + if (sustainedChoir) { + var choirPool = new List<(int Min, int Max)> { + (-24, -13), (-19, -10), (-14, -7), (-9, -3), + (3, 9), (7, 14), (10, 19), (15, 24), + }; + if (numVoices == 1) { + return new List<(int, int)> { choirPool[3] }; + } + return Enumerable.Range(0, numVoices) + .Select(index => choirPool[(int)Math.Round(index * (choirPool.Count - 1.0) / (numVoices - 1))]) + .ToList(); + } + List<(int Min, int Max)> presets = styleName switch { + "Cinematic" => new List<(int, int)> { (-12, -5), (7, 14), (12, 19), (-19, -12) }, + "Choir" => new List<(int, int)> { (-8, -3), (3, 8), (10, 16), (-16, -10) }, + "Randomized Human" => new List<(int, int)> { (-8, -3), (3, 9), (10, 17), (-16, -10) }, + _ => new List<(int, int)> { (-8, -3), (3, 8), (10, 16), (-16, -10) }, + }; + var result = presets.Take(numVoices).ToList(); + var wider = new List<(int Min, int Max)> { + (-21, -14), (17, 23), (-26, -22), (24, 28), + }; + result.AddRange(wider.Take(Math.Max(0, numVoices - result.Count))); + return result; + } + + private static Phonemizer ClonePhonemizer(Phonemizer source) { + if (source == null) { + return PhonemizerFactory.Get(typeof(DefaultPhonemizer))?.Create(); + } + var factory = PhonemizerFactory.Get(source.GetType()); + return factory?.Create() ?? source; + } + + private void AppendSustainedTracks( + UProject project, + int baseTrackIndex, + List> harmonyNotes, + HarmonyConfig config, + HarmGenConfig uiConfig, + DocManager docManager) { + var baseTrack = project.tracks[baseTrackIndex]; + var labels = BuildHarmonyLabels(config.Style.Name, config.NumVoices, sustainedChoir: true); + for (int voiceIndex = 0; voiceIndex < config.NumVoices; voiceIndex++) { + var newTrack = new UTrack(project) { + TrackNo = project.tracks.Count, + TrackName = $"{baseTrack.TrackName} - {labels[voiceIndex]}", + TrackColor = baseTrack.TrackColor, + Singer = baseTrack.Singer, + RendererSettings = baseTrack.RendererSettings.Clone(), + Volume = uiConfig.HarmonyVolume, + }; + if (!string.IsNullOrWhiteSpace(uiConfig.PhonemizerFullName)) { + var factory = PhonemizerFactory.Get(uiConfig.PhonemizerFullName); + newTrack.Phonemizer = factory?.Create() ?? ClonePhonemizer(baseTrack.Phonemizer); + } else { + newTrack.Phonemizer = ClonePhonemizer(baseTrack.Phonemizer); + } + docManager.ExecuteCmd(new AddTrackCommand(project, newTrack)); + + var sourceNotes = harmonyNotes[voiceIndex]; + if (sourceNotes.Count == 0) { + continue; + } + int partPosition = sourceNotes.Min(note => note.position); + var newNotes = new SortedSet(); + foreach (var sourceNote in sourceNotes) { + var newNote = sourceNote.Clone(); + newNote.position -= partPosition; + newNotes.Add(newNote); + } + var newPart = new UVoicePart { + name = $"{newTrack.TrackName} Part", + trackNo = newTrack.TrackNo, + position = partPosition, + notes = newNotes, + curves = new List(), + }; + newPart.Duration = Math.Max(10, newNotes.Max(note => note.position + note.duration)); + docManager.ExecuteCmd(new AddPartCommand(project, newPart)); + } + } + + private void AppendTracks( + UProject project, + int baseTrackIndex, + List> harmonyNotes, + List notes, + HarmonyConfig config, + HarmGenConfig uiConfig, + DocManager docManager) { + var baseTrack = project.tracks[baseTrackIndex]; + var labels = BuildHarmonyLabels(config.Style.Name, config.NumVoices, sustainedChoir: false); + var voiceParts = project.parts.OfType() + .Where(p => p.trackNo == baseTrackIndex) + .OrderBy(p => p.position) + .ToList(); + + for (int voiceIdx = 0; voiceIdx < config.NumVoices; voiceIdx++) { + var newTrack = new UTrack(project) { + TrackNo = project.tracks.Count, + TrackName = $"{baseTrack.TrackName} - {labels[voiceIdx]}", + TrackColor = baseTrack.TrackColor, + Singer = baseTrack.Singer, + RendererSettings = baseTrack.RendererSettings.Clone(), + Volume = uiConfig.HarmonyVolume, + }; + if (!string.IsNullOrWhiteSpace(uiConfig.PhonemizerFullName)) { + var factory = PhonemizerFactory.Get(uiConfig.PhonemizerFullName); + if (factory != null) { + newTrack.Phonemizer = factory.Create(); + } else { + newTrack.Phonemizer = ClonePhonemizer(baseTrack.Phonemizer); + } + } else { + newTrack.Phonemizer = ClonePhonemizer(baseTrack.Phonemizer); + } + docManager.ExecuteCmd(new AddTrackCommand(project, newTrack)); + + foreach (var voicePart in voiceParts) { + var newNotes = new SortedSet(); + for (int i = 0; i < notes.Count; i++) { + if (notes[i].VoicePart != voicePart) { + continue; + } + var hNote = harmonyNotes[voiceIdx][i]; + if (hNote != null) { + newNotes.Add(hNote); + } + } + if (newNotes.Count == 0) { + continue; + } + + var newPart = new UVoicePart { + name = $"{newTrack.TrackName} Part", + trackNo = newTrack.TrackNo, + position = voicePart.position, + notes = newNotes, + curves = new List(), + }; + newPart.Duration = ComputeVoicePartDuration(voicePart.duration, newNotes); + docManager.ExecuteCmd(new AddPartCommand(project, newPart)); + } + } + } + + private static int ComputeVoicePartDuration(int baseDuration, SortedSet notes) { + if (notes.Count == 0) { + return baseDuration; + } + int maxEnd = notes.Max(n => n.position + n.duration); + return Math.Max(baseDuration, maxEnd); + } + + private static List BuildHarmonyLabels(string styleName, int numVoices, bool sustainedChoir) { + if (sustainedChoir) { + var choirLabels = new List { + "Bass 2", "Bass", "Tenor", "Alto", "Mezzo", "Soprano", "High Soprano", "Air", + }; + return Enumerable.Range(0, numVoices) + .Select(index => index < choirLabels.Count ? choirLabels[index] : $"Voice {index + 1}") + .ToList(); + } + List labels = styleName switch { + "Cinematic" => new List { "Low Oct", "High Fifth", "High Oct", "Low Oct 2" }, + "Choir" => new List { "Alto", "Tenor", "Soprano", "Bass" }, + _ => new List { "Harmony 1", "Harmony 2", "Harmony 3", "Harmony 4" }, + }; + return Enumerable.Range(0, numVoices) + .Select(index => index < labels.Count ? labels[index] : $"Harmony {index + 1}") + .ToList(); + } + } +} diff --git a/OpenUtauMobile.Android/Properties/AndroidManifest.xml b/OpenUtauMobile.Android/Properties/AndroidManifest.xml index 1ded9638..1f846f30 100644 --- a/OpenUtauMobile.Android/Properties/AndroidManifest.xml +++ b/OpenUtauMobile.Android/Properties/AndroidManifest.xml @@ -1,5 +1,7 @@  + + diff --git a/OpenUtauMobile/Assets/Lang/Strings.en.resx b/OpenUtauMobile/Assets/Lang/Strings.en.resx index 01956d6d..eedef199 100644 --- a/OpenUtauMobile/Assets/Lang/Strings.en.resx +++ b/OpenUtauMobile/Assets/Lang/Strings.en.resx @@ -1639,6 +1639,15 @@ Load rendered pitch + + Auto pitch + + + Harmony generator + + + Voices + Bake pitch @@ -2275,6 +2284,99 @@ Failed to undo deletion + + Custom resamplers + + + Add and remove external resamplers + + + Custom resamplers + + + Add resampler + + + No custom resamplers yet. Tap + to add one. + + + Supported files: {0}. Added resamplers appear in track render settings. Being listed doesn't guarantee a program runs on this device. + + + no extension + + + Select a resampler + + + Failed to read resamplers: {0} + + + Available in track render settings + + + Not loaded on this device + + + This device can't load this file type. + + + This name is reserved by a built-in resampler. Rename the file and try again. + + + This file is already in the resampler folder. + + + Add resampler? + + + Add “{0}”? + + + Resamplers are external programs that run on your device. Only add files from sources you trust. + + + Add + + + Replace resampler? + + + “{0}” is already added. Replace it with the selected file? + + + Replace + + + Adding resampler… + + + Done + + + Failed to add the resampler + + + Resampler added + + + Remove resampler? + + + Remove “{0}” and its manifest? Projects that use it will fall back to the default resampler. + + + Failed to remove the resampler + + + Removing resampler… + + + Resampler removed + + + Close the open project before changing resamplers. + Workspace Arrangement Piano roll diff --git a/OpenUtauMobile/Assets/Lang/Strings.ja.resx b/OpenUtauMobile/Assets/Lang/Strings.ja.resx index 3fe53108..387015af 100644 --- a/OpenUtauMobile/Assets/Lang/Strings.ja.resx +++ b/OpenUtauMobile/Assets/Lang/Strings.ja.resx @@ -1639,6 +1639,15 @@ Load rendered pitch + + オートピッチ + + + ハーモニー生成 + + + 声部数 + Bake pitch @@ -2271,6 +2280,99 @@ 削除予定の取り消しに失敗しました + + カスタムリサンプラー + + + 外部リサンプラーの追加と削除 + + + カスタムリサンプラー + + + リサンプラーを追加 + + + カスタムリサンプラーはまだありません。+ をタップして追加します。 + + + 対応ファイル:{0}。追加したリサンプラーはトラックのレンダリング設定で選べます。一覧に表示されても、このデバイスで実行できるとは限りません。 + + + 拡張子なし + + + リサンプラーを選択 + + + リサンプラーを読み込めませんでした:{0} + + + トラックのレンダリング設定で選択できます + + + このデバイスでは読み込まれていません + + + このデバイスでは、この種類のファイルを読み込めません。 + + + この名前は内蔵リサンプラーが使用しています。ファイル名を変更してもう一度お試しください。 + + + このファイルはすでにリサンプラーのフォルダーにあります。 + + + リサンプラーを追加しますか? + + + 「{0}」を追加しますか? + + + リサンプラーはデバイス上で実行される外部プログラムです。信頼できる入手元のファイルだけを追加してください。 + + + 追加 + + + リサンプラーを置き換えますか? + + + 「{0}」はすでに追加されています。選択したファイルで置き換えますか? + + + 置き換え + + + リサンプラーを追加しています… + + + 完了 + + + リサンプラーを追加できませんでした + + + リサンプラーを追加しました + + + リサンプラーを削除しますか? + + + 「{0}」とそのマニフェストを削除しますか?使用しているプロジェクトは既定のリサンプラーに戻ります。 + + + リサンプラーを削除できませんでした + + + リサンプラーを削除しています… + + + リサンプラーを削除しました + + + リサンプラーを変更する前に、開いているプロジェクトを閉じてください。 + ワークスペース アレンジ ピアノロール diff --git a/OpenUtauMobile/Assets/Lang/Strings.ru.resx b/OpenUtauMobile/Assets/Lang/Strings.ru.resx index fc8e0567..f1536aad 100644 --- a/OpenUtauMobile/Assets/Lang/Strings.ru.resx +++ b/OpenUtauMobile/Assets/Lang/Strings.ru.resx @@ -1559,6 +1559,15 @@ Загрузить отрендеренный питч + + Авто-питч + + + Генератор гармонии + + + Голоса + Запечь питч @@ -2230,6 +2239,99 @@ Не удалось отменить удаление + + Пользовательские ресемплеры + + + Добавление и удаление внешних ресемплеров + + + Пользовательские ресемплеры + + + Добавить ресемплер + + + Пользовательских ресемплеров пока нет. Нажмите +, чтобы добавить. + + + Поддерживаемые файлы: {0}. Добавленные ресемплеры доступны в настройках рендеринга дорожки. Наличие в списке не гарантирует, что программа запустится на этом устройстве. + + + без расширения + + + Выберите ресемплер + + + Не удалось прочитать ресемплеры: {0} + + + Доступен в настройках рендеринга дорожки + + + Не загружен на этом устройстве + + + Это устройство не может загрузить файл такого типа. + + + Это имя занято встроенным ресемплером. Переименуйте файл и повторите попытку. + + + Этот файл уже находится в папке ресемплеров. + + + Добавить ресемплер? + + + Добавить «{0}»? + + + Ресемплеры — это внешние программы, которые запускаются на вашем устройстве. Добавляйте только файлы из надёжных источников. + + + Добавить + + + Заменить ресемплер? + + + «{0}» уже добавлен. Заменить его выбранным файлом? + + + Заменить + + + Добавление ресемплера… + + + Готово + + + Не удалось добавить ресемплер + + + Ресемплер добавлен + + + Удалить ресемплер? + + + Удалить «{0}» и его манифест? Проекты, которые его используют, перейдут на ресемплер по умолчанию. + + + Не удалось удалить ресемплер + + + Удаление ресемплера… + + + Ресемплер удалён + + + Закройте открытый проект, прежде чем менять ресемплеры. + Рабочая область Аранжировка Пианоролл diff --git a/OpenUtauMobile/Assets/Lang/Strings.uk.resx b/OpenUtauMobile/Assets/Lang/Strings.uk.resx index 63b584dc..01d75b4b 100644 --- a/OpenUtauMobile/Assets/Lang/Strings.uk.resx +++ b/OpenUtauMobile/Assets/Lang/Strings.uk.resx @@ -1559,6 +1559,15 @@ Завантажити відрендерений пітч + + Авто-пітч + + + Генератор гармонії + + + Голоси + Запекти пітч @@ -2230,6 +2239,99 @@ Не вдалося скасувати видалення + + Користувацькі ресемплери + + + Додавання та видалення зовнішніх ресемплерів + + + Користувацькі ресемплери + + + Додати ресемплер + + + Користувацьких ресемплерів ще немає. Натисніть +, щоб додати. + + + Підтримувані файли: {0}. Додані ресемплери доступні в налаштуваннях рендерингу доріжки. Наявність у списку не гарантує, що програма запуститься на цьому пристрої. + + + без розширення + + + Виберіть ресемплер + + + Не вдалося прочитати ресемплери: {0} + + + Доступний у налаштуваннях рендерингу доріжки + + + Не завантажено на цьому пристрої + + + Цей пристрій не може завантажити файл такого типу. + + + Цю назву займає вбудований ресемплер. Перейменуйте файл і спробуйте ще раз. + + + Цей файл уже є в теці ресемплерів. + + + Додати ресемплер? + + + Додати «{0}»? + + + Ресемплери — це зовнішні програми, що запускаються на вашому пристрої. Додавайте лише файли з надійних джерел. + + + Додати + + + Замінити ресемплер? + + + «{0}» уже додано. Замінити його вибраним файлом? + + + Замінити + + + Додавання ресемплера… + + + Готово + + + Не вдалося додати ресемплер + + + Ресемплер додано + + + Видалити ресемплер? + + + Видалити «{0}» та його маніфест? Проєкти, які його використовують, перейдуть на ресемплер за замовчуванням. + + + Не вдалося видалити ресемплер + + + Видалення ресемплера… + + + Ресемплер видалено + + + Закрийте відкритий проєкт, перш ніж змінювати ресемплери. + Робоча область Аранжування Піанорол diff --git a/OpenUtauMobile/Assets/Lang/Strings.zh-Hans.resx b/OpenUtauMobile/Assets/Lang/Strings.zh-Hans.resx index a9a003cb..edeb376d 100644 --- a/OpenUtauMobile/Assets/Lang/Strings.zh-Hans.resx +++ b/OpenUtauMobile/Assets/Lang/Strings.zh-Hans.resx @@ -1639,6 +1639,15 @@ 载入渲染音高 + + 自动音高 + + + 和声生成 + + + 声部数 + 烘焙音高 @@ -2275,6 +2284,99 @@ 取消删除标记失败 + + 自定义重采样器 + + + 添加和移除外部重采样器 + + + 自定义重采样器 + + + 添加重采样器 + + + 还没有自定义重采样器,点按 + 添加。 + + + 支持的文件:{0}。添加后可在轨道渲染设置中选用。列入此处不代表程序一定能在本设备上运行。 + + + 无扩展名 + + + 选择重采样器 + + + 读取重采样器失败:{0} + + + 可在轨道渲染设置中选用 + + + 本设备未能加载 + + + 本设备无法加载此类型的文件。 + + + 该名称已被内置重采样器占用,请重命名文件后重试。 + + + 该文件已位于重采样器目录中。 + + + 添加重采样器? + + + 要添加“{0}”吗? + + + 重采样器是会在你的设备上运行的外部程序,请仅添加来源可信的文件。 + + + 添加 + + + 替换重采样器? + + + “{0}”已存在,要用所选文件替换吗? + + + 替换 + + + 正在添加重采样器… + + + 完成 + + + 添加重采样器失败 + + + 已添加重采样器 + + + 移除重采样器? + + + 要移除“{0}”及其清单吗?使用它的工程将回退到默认重采样器。 + + + 移除重采样器失败 + + + 正在移除重采样器… + + + 已移除重采样器 + + + 请先关闭当前打开的工程,再修改重采样器。 + 工作区 编曲 钢琴卷帘 diff --git a/OpenUtauMobile/Services/ResamplerManagementService.cs b/OpenUtauMobile/Services/ResamplerManagementService.cs new file mode 100644 index 00000000..d5429122 --- /dev/null +++ b/OpenUtauMobile/Services/ResamplerManagementService.cs @@ -0,0 +1,228 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using OpenUtau.Classic; +using OpenUtau.Core; +using OpenUtau.Core.Util; +using OpenUtauMobile.Helpers; +using OpenUtauMobile.ViewModels; + +namespace OpenUtauMobile.Services; + +/// 重采样器目录中的一个文件,以及引擎当前是否已将其注册为可选重采样器。 +public sealed record ResamplerFileEntry(string RelativePath, long Size, bool IsAvailable) +{ + /// 不含目录的文件名。 + public string FileName => Path.GetFileName(RelativePath); + + /// 以 KB 表示的文件大小。 + public string SizeText => $"{Size / 1024d:N1} KB"; +} + +/// +/// 管理自定义重采样器文件。安装和移除后重新扫描引擎注册表; +/// “可用”只表示引擎已识别该文件,不代表程序能在当前设备上实际运行。 +/// +public static class ResamplerManagementService +{ + // 内置重采样器在 Core 中是 internal 类型,Mobile 层无法引用其常量,只能按名称保留。 + private static readonly string[] BuiltinNames = ["worldline", "hifisampler"]; + + private static StringComparer PathComparer => OperatingSystem.IsWindows() + ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal; + + private static StringComparison PathComparison => OperatingSystem.IsWindows() + ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; + + private static string Root => Path.GetFullPath(PathManager.Inst.ResamplersPath); + + /// 文件选择器的过滤器。内置选择器无法匹配无扩展名文件,非 Windows 平台因此不加过滤。 + public static string[] PickerFilters => OperatingSystem.IsWindows() ? ["*.exe", "*.bat"] : []; + + /// 当前设备可添加的文件类型,供界面提示使用。 + public static string DescribeSupportedTypes() + { + List types; + if (OperatingSystem.IsWindows()) + { + types = [".exe", ".bat"]; + } + else + { + types = [".sh", L.S("ResamplerManager.NoExtension")]; + if (!string.IsNullOrEmpty(Preferences.Default.WinePath)) types.AddRange([".exe", ".bat"]); + } + return string.Join(", ", types); + } + + /// 按引擎扫描重采样器时相同的规则,判断当前设备能否加载该文件类型。 + public static bool IsLoadableFile(string path) + { + string extension = Path.GetExtension(path).ToLowerInvariant(); + if (extension is ".exe" or ".bat") + return OperatingSystem.IsWindows() || !string.IsNullOrEmpty(Preferences.Default.WinePath); + return !OperatingSystem.IsWindows() && (extension == ".sh" || extension.Length == 0); + } + + /// 目录中可能是重采样器的文件;不含配置文件和安装中的临时文件。 + private static bool IsCandidate(string path) + { + string fileName = Path.GetFileName(path); + if (fileName.StartsWith('.')) return false; + return Path.GetExtension(fileName).ToLowerInvariant() is ".exe" or ".bat" or ".sh" or ""; + } + + /// 列出重采样器目录中的文件,并标出引擎已注册的项。 + public static IReadOnlyList ListFiles() + { + string root = Root; + Directory.CreateDirectory(root); + // 内置重采样器位于应用根目录,不会与此目录下的文件重合。 + HashSet registered = new( + ToolsManager.Inst.Resamplers + .Select(resampler => resampler.FilePath) + .Where(path => !string.IsNullOrEmpty(path)) + .Select(Path.GetFullPath), + PathComparer); + EnumerationOptions options = new() + { + RecurseSubdirectories = true, + IgnoreInaccessible = false, + AttributesToSkip = FileAttributes.ReparsePoint, + }; + return + [ + .. Directory.EnumerateFiles(root, "*", options) + .Where(IsCandidate) + .Select(path => new ResamplerFileEntry( + Path.GetRelativePath(root, path), + new FileInfo(path).Length, + registered.Contains(Path.GetFullPath(path)))) + .OrderBy(entry => entry.RelativePath, StringComparer.OrdinalIgnoreCase) + ]; + } + + /// 在询问信任前检查所选文件,不写入重采样器目录。 + public static void ValidateForInstall(string sourcePath) + { + if (!File.Exists(sourcePath) || !IsLoadableFile(sourcePath)) + throw new InvalidDataException(L.S("ResamplerManager.UnsupportedFile")); + // 与内置重采样器同名的文件会在引擎扫描时被忽略,提前拒绝以免出现安装成功却不可用的文件。 + if (BuiltinNames.Contains(Path.GetFileName(sourcePath), StringComparer.OrdinalIgnoreCase)) + throw new InvalidDataException(L.S("ResamplerManager.BuiltinNotAllowed")); + if (PathComparer.Equals(Path.GetFullPath(sourcePath), GetDestination(sourcePath))) + throw new InvalidDataException(L.S("ResamplerManager.AlreadyInstalled")); + } + + /// 安装该文件会覆盖已有的同名重采样器。 + public static bool WillReplace(string sourcePath) => File.Exists(GetDestination(sourcePath)); + + private static string GetDestination(string sourcePath) => Path.Combine(Root, Path.GetFileName(sourcePath)); + + /// + /// 复制重采样器及其同名 YAML 清单到重采样器目录。 + /// 先复制到临时文件,全部成功后再发布;清单先于程序发布,失败时不会留下半个安装。 + /// + public static async Task InstallAsync(string sourcePath, LoadingPopupViewModel loading) + { + // 确认期间文件或目录状态可能变化,复制前再次检查。 + ValidateForInstall(sourcePath); + string root = Root; + Directory.CreateDirectory(root); + + string destination = GetDestination(sourcePath); + // 清单约定与引擎一致:与程序同名、扩展名为 .yaml。 + string manifestSource = Path.ChangeExtension(sourcePath, ".yaml"); + string manifestDestination = Path.ChangeExtension(destination, ".yaml"); + bool hasManifest = File.Exists(manifestSource); + bool manifestExisted = File.Exists(manifestDestination); + + List temporaryFiles = []; + bool manifestPublished = false; + try + { + loading.UpdateProgress(5, L.S("ResamplerManager.Installing")); + string programTemporary = await CopyToTemporaryAsync(sourcePath, root, temporaryFiles); + // 引擎不会调用 CheckPermissions,从选择器复制来的文件没有执行位,需在此补上。 + if (!OperatingSystem.IsWindows() && !OperatingSystem.IsBrowser()) + { + File.SetUnixFileMode(programTemporary, + UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute + | UnixFileMode.GroupRead | UnixFileMode.GroupExecute + | UnixFileMode.OtherRead | UnixFileMode.OtherExecute); + } + loading.UpdateProgress(70); + + string? manifestTemporary = hasManifest + ? await CopyToTemporaryAsync(manifestSource, root, temporaryFiles) + : null; + loading.UpdateProgress(85); + + if (manifestTemporary != null) + { + File.Move(manifestTemporary, manifestDestination, overwrite: true); + manifestPublished = true; + } + File.Move(programTemporary, destination, overwrite: true); + loading.UpdateProgress(95); + } + catch + { + // 程序未能发布时,撤销本次新增的清单,避免遗留无主文件。 + if (manifestPublished && !manifestExisted) DeleteFile(manifestDestination); + throw; + } + finally + { + foreach (string temporary in temporaryFiles) + { + if (File.Exists(temporary)) File.Delete(temporary); + } + } + } + + /// 复制到目录内的临时文件;临时文件以点开头且无可识别扩展名,不会进入列表或被引擎加载。 + private static async Task CopyToTemporaryAsync(string sourcePath, string root, List temporaryFiles) + { + string temporary = Path.Combine(root, $".{Guid.NewGuid():N}.installing"); + temporaryFiles.Add(temporary); + await using FileStream source = new(sourcePath, FileMode.Open, FileAccess.Read, FileShare.Read); + await using FileStream target = new(temporary, FileMode.CreateNew, FileAccess.Write, FileShare.None); + await source.CopyToAsync(target); + await target.FlushAsync(); + return temporary; + } + + /// 删除重采样器及其同名清单;相对路径必须位于重采样器目录内。 + public static void Uninstall(string relativePath) + { + string path = ResolvePath(relativePath); + DeleteFile(path); + DeleteFile(Path.ChangeExtension(path, ".yaml")); + } + + /// 重新扫描引擎注册表。Initialize 自带锁,渲染线程不会读到半更新的映射。 + public static Task RescanAsync() => Task.Run(() => ToolsManager.Inst.Initialize()); + + private static void DeleteFile(string path) + { + if (!File.Exists(path)) return; + // 用户已确认删除;只读属性不应阻止删除。 + FileAttributes attributes = File.GetAttributes(path); + if ((attributes & FileAttributes.ReadOnly) != 0) File.SetAttributes(path, attributes & ~FileAttributes.ReadOnly); + File.Delete(path); + } + + private static string ResolvePath(string relativePath) + { + ArgumentException.ThrowIfNullOrWhiteSpace(relativePath); + string root = Root; + string path = Path.GetFullPath(Path.Combine(root, relativePath)); + string prefix = Path.TrimEndingDirectorySeparator(root) + Path.DirectorySeparatorChar; + if (Path.IsPathRooted(relativePath) || !path.StartsWith(prefix, PathComparison) || !IsCandidate(path)) + throw new InvalidDataException("Invalid resampler path."); + return path; + } +} diff --git a/OpenUtauMobile/ViewLocator.cs b/OpenUtauMobile/ViewLocator.cs index 28af6de3..9bdfdac4 100644 --- a/OpenUtauMobile/ViewLocator.cs +++ b/OpenUtauMobile/ViewLocator.cs @@ -28,6 +28,7 @@ public class ViewLocator : IDataTemplate [typeof(PluginManagerViewModel)] = static () => new PluginManagerView(), [typeof(PluginSingerSetupViewModel)] = static () => new PluginSingerSetupView(), [typeof(PluginWebSettingsViewModel)] = static () => new PluginWebSettingsView(), + [typeof(ResamplerManagerViewModel)] = static () => new ResamplerManagerView(), [typeof(SandboxFileManagerViewModel)] = static () => new SandboxFileManagerView(), [typeof(SettingsViewModel)] = static () => new SettingsView(), [typeof(SingerDetailViewModel)] = static () => new SingerDetailView(), diff --git a/OpenUtauMobile/ViewModels/BatchEditCatalog.cs b/OpenUtauMobile/ViewModels/BatchEditCatalog.cs index 65e6ce13..af4c0d89 100644 --- a/OpenUtauMobile/ViewModels/BatchEditCatalog.cs +++ b/OpenUtauMobile/ViewModels/BatchEditCatalog.cs @@ -182,6 +182,13 @@ public static class BatchEditCatalog NoParameter("refresh-real-curves", BatchEditCategory.Notes, "BatchEdit.Action.RefreshRealCurves", PackIconPhosphorIconsKind.ArrowsClockwise, _ => new RefreshRealCurves(), supportsCancellation: true), + NoParameter("auto-pitch", BatchEditCategory.Notes, + "BatchEdit.Action.AutoPitch", PackIconPhosphorIconsKind.WaveSine, + _ => new AutoPitchEdit(), supportsCancellation: true), + IntegerParameter("harmony-generator", BatchEditCategory.Notes, + "BatchEdit.Action.HarmonyGenerator", "BatchEdit.Parameter.Voices", + PackIconPhosphorIconsKind.WaveSine, 2, 1, 8, + value => new HarmGenCurrentTrackEdit(value)), NoParameter("reset-pitch-bends", BatchEditCategory.Reset, "BatchEdit.Action.ResetPitchBends", PackIconPhosphorIconsKind.ArrowCounterClockwise, diff --git a/OpenUtauMobile/ViewModels/OptionsViewModel.cs b/OpenUtauMobile/ViewModels/OptionsViewModel.cs index f69729bb..511357ea 100644 --- a/OpenUtauMobile/ViewModels/OptionsViewModel.cs +++ b/OpenUtauMobile/ViewModels/OptionsViewModel.cs @@ -1,4 +1,5 @@ -using System.Reactive; +using System; +using System.Reactive; using OpenUtauMobile.Helpers; using OpenUtauMobile.Services; using OpenUtauMobile.Services.Dialogs; @@ -12,6 +13,9 @@ public class OptionsViewModel : NavigateViewModelBase public ReactiveCommand OpenSettingsCommand { get; } public ReactiveCommand OpenDependencyManagerCommand { get; } public ReactiveCommand OpenPluginManagerCommand { get; } + /// iOS 与浏览器无法启动外部程序,不提供自定义重采样器入口。 + public bool IsResamplerManagerAvailable => !OperatingSystem.IsIOS() && !OperatingSystem.IsBrowser(); + public ReactiveCommand OpenResamplerManagerCommand { get; } /// 仅在平台注册了沙箱传输能力时显示文件管理入口。 public bool IsSandboxFileManagerAvailable => ServiceHub.SandboxFileTransferService != null; public ReactiveCommand OpenSandboxFileManagerCommand { get; } @@ -25,6 +29,7 @@ public OptionsViewModel(MainViewModel navigator) : base(navigator) OpenSettingsCommand = ReactiveCommand.Create(OnOpenSettings); OpenDependencyManagerCommand = ReactiveCommand.Create(OnOpenDependencyManager); OpenPluginManagerCommand = ReactiveCommand.Create(() => Navigator.Navigate(new PluginManagerViewModel(Navigator))); + OpenResamplerManagerCommand = ReactiveCommand.Create(() => Navigator.Navigate(new ResamplerManagerViewModel(Navigator))); OpenSandboxFileManagerCommand = ReactiveCommand.Create(() => Navigator.Navigate(new SandboxFileManagerViewModel(Navigator))); OpenHelpCommand = ReactiveCommand.Create(OnOpenHelp); OpenExportLogsCommand = ReactiveCommand.Create(OnOpenExportLogs); diff --git a/OpenUtauMobile/ViewModels/ResamplerCardViewModel.cs b/OpenUtauMobile/ViewModels/ResamplerCardViewModel.cs new file mode 100644 index 00000000..f9c55e98 --- /dev/null +++ b/OpenUtauMobile/ViewModels/ResamplerCardViewModel.cs @@ -0,0 +1,18 @@ +using OpenUtauMobile.Helpers; +using OpenUtauMobile.Services; +using ReactiveUI; + +namespace OpenUtauMobile.ViewModels; + +/// 列表仅展示文件和引擎识别状态,移除操作由页面命令负责。 +public sealed class ResamplerCardViewModel(ResamplerFileEntry entry) : ReactiveObject +{ + public string FileName => entry.FileName; + public string RelativePath => entry.RelativePath; + /// 位于子目录时才单独显示相对路径,避免与文件名重复。 + public bool HasFolder => entry.RelativePath != entry.FileName; + public string SizeText => entry.SizeText; + /// 引擎已识别该文件;不代表程序一定能在当前设备上运行。 + public bool IsAvailable => entry.IsAvailable; + public string StatusText => L.S(IsAvailable ? "ResamplerManager.Available" : "ResamplerManager.Unavailable"); +} diff --git a/OpenUtauMobile/ViewModels/ResamplerManagerViewModel.cs b/OpenUtauMobile/ViewModels/ResamplerManagerViewModel.cs new file mode 100644 index 00000000..ff16c13d --- /dev/null +++ b/OpenUtauMobile/ViewModels/ResamplerManagerViewModel.cs @@ -0,0 +1,200 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reactive; +using System.Threading.Tasks; +using DynamicData.Binding; +using OpenUtau.Core; +using OpenUtauMobile.Helpers; +using OpenUtauMobile.Services; +using OpenUtauMobile.Services.Dialogs; +using OpenUtauMobile.Storage; +using ReactiveUI; +using ReactiveUI.Fody.Helpers; +using Serilog; + +namespace OpenUtauMobile.ViewModels; + +/// 自定义重采样器管理页:添加、移除,并在变更后刷新引擎注册表。 +public sealed class ResamplerManagerViewModel : NavigateViewModelBase, IDisposable +{ + public ObservableCollectionExtended Resamplers { get; } = []; + private bool disposed; + [Reactive] public bool IsBusy { get; private set; } + [Reactive] public bool IsEmpty { get; private set; } + [Reactive] public bool HasError { get; private set; } + [Reactive] public string ErrorMessage { get; private set; } = string.Empty; + /// 当前设备支持的文件类型提示;Wine 路径可能在设置中变化,因此每次刷新时重新生成。 + [Reactive] public string Hint { get; private set; } = string.Empty; + + public ReactiveCommand BackCommand { get; } + public ReactiveCommand RefreshCommand { get; } + public ReactiveCommand AddCommand { get; } + public ReactiveCommand RemoveCommand { get; } + + public ResamplerManagerViewModel(MainViewModel navigator) : base(navigator) + { + IObservable canOperate = this.WhenAnyValue(vm => vm.IsBusy, busy => !busy); + BackCommand = ReactiveCommand.Create(() => Navigator.NavigateBack(this), canOperate); + RefreshCommand = ReactiveCommand.CreateFromTask(RefreshAsync, canOperate); + AddCommand = ReactiveCommand.CreateFromTask(AddAsync, canOperate); + RemoveCommand = ReactiveCommand.CreateFromTask(RemoveAsync, canOperate); + } + + public override void OnNavigatedTo() => _ = RefreshAsync(); + + public override void OnBackRequested() + { + if (!IsBusy) base.OnBackRequested(); + } + + private async Task RefreshAsync() + { + if (IsBusy) return; + IsBusy = true; + try { await RefreshEntriesAsync(); } + finally { IsBusy = false; } + } + + private async Task RefreshEntriesAsync() + { + Hint = string.Format(L.S("ResamplerManager.Hint"), ResamplerManagementService.DescribeSupportedTypes()); + try + { + IReadOnlyList entries = await Task.Run(ResamplerManagementService.ListFiles); + Resamplers.Load([.. entries.Select(entry => new ResamplerCardViewModel(entry))]); + IsEmpty = entries.Count == 0; + HasError = false; + ErrorMessage = string.Empty; + } + catch (Exception exception) + { + Log.Error(exception, "Failed to list resamplers"); + IsEmpty = false; + HasError = true; + ErrorMessage = string.Format(L.S("ResamplerManager.RefreshFailed"), exception.Message); + } + } + + /// 引擎没有全局的渲染空闲边界,工程打开期间不修改重采样器注册表。 + private bool EnsureNoOpenProject() + { + if (Navigator.ActiveEditor == null) return true; + ToastService.Enqueue(L.S("ResamplerManager.CloseProjectFirst")); + return false; + } + + private async Task AddAsync() + { + if (IsBusy || !EnsureNoOpenProject()) return; + IsBusy = true; + try + { + bool installed = false; + try + { + string path = await FilePicker.PickSingleFileAsync( + L.S("ResamplerManager.SelectFile"), ResamplerManagementService.PickerFilters); + if (string.IsNullOrEmpty(path)) return; + ResamplerManagementService.ValidateForInstall(path); + string fileName = Path.GetFileName(path); + + // 重采样器是由应用直接执行的外部程序,添加前需要用户确认信任来源。 + string? answer = await OptionConfirmPopupService.ShowAsync( + L.S("ResamplerManager.TrustTitle"), + string.Format(L.S("ResamplerManager.TrustQuestion"), fileName) + + Environment.NewLine + Environment.NewLine + L.S("ResamplerManager.TrustMessage"), + [ + new OptionConfirmOption(L.S("Common.Cancel"), "cancel", isDefault: true), + new OptionConfirmOption(L.S("ResamplerManager.Continue"), "continue", isDestructive: true), + ]); + if (answer != "continue") return; + + if (ResamplerManagementService.WillReplace(path)) + { + answer = await OptionConfirmPopupService.ShowAsync( + L.S("ResamplerManager.ReplaceTitle"), + string.Format(L.S("ResamplerManager.ReplaceMessage"), fileName), + [ + new OptionConfirmOption(L.S("Common.Cancel"), "cancel", isDefault: true), + new OptionConfirmOption(L.S("ResamplerManager.Replace"), "replace", isDestructive: true), + ]); + if (answer != "replace") return; + } + + await LoadingPopupService.RunAsync(L.S("ResamplerManager.Installing"), 0, async loading => + { + await ResamplerManagementService.InstallAsync(path, loading); + await ResamplerManagementService.RescanAsync(); + await ServiceHub.FlushFileSystemAsync(); + loading.UpdateProgress(100, L.S("ResamplerManager.Completed")); + }); + installed = true; + } + catch (Exception exception) + { + ShowError("ResamplerManager.InstallFailed", exception); + } + finally + { + await RefreshEntriesAsync(); + } + if (installed) ToastService.Enqueue(L.S("ResamplerManager.Installed")); + } + finally { IsBusy = false; } + } + + private async Task RemoveAsync(ResamplerCardViewModel card) + { + if (IsBusy || !EnsureNoOpenProject()) return; + IsBusy = true; + try + { + bool removed = false; + try + { + string? answer = await OptionConfirmPopupService.ShowAsync( + L.S("ResamplerManager.DeleteTitle"), + string.Format(L.S("ResamplerManager.DeleteMessage"), card.FileName), + [ + new OptionConfirmOption(L.S("Common.Cancel"), "cancel", isDefault: true), + new OptionConfirmOption(L.S("Common.Delete"), "delete", isDestructive: true), + ]); + if (answer != "delete") return; + + await LoadingPopupService.RunAsync(L.S("ResamplerManager.Removing"), async _ => + { + await Task.Run(() => ResamplerManagementService.Uninstall(card.RelativePath)); + await ResamplerManagementService.RescanAsync(); + await ServiceHub.FlushFileSystemAsync(); + }); + removed = true; + } + catch (Exception exception) + { + ShowError("ResamplerManager.DeleteFailed", exception); + } + finally + { + await RefreshEntriesAsync(); + } + if (removed) ToastService.Enqueue(L.S("ResamplerManager.Removed")); + } + finally { IsBusy = false; } + } + + private static void ShowError(string key, Exception exception) + { + Log.Error(exception, "Resampler management operation failed: {Operation}", key); + ErrorDialogService.Show(new ErrorDialogViewModel(new ErrorMessageNotification(L.S(key), exception))); + } + + public void Dispose() + { + if (disposed) return; + disposed = true; + BackCommand.Dispose(); RefreshCommand.Dispose(); AddCommand.Dispose(); + RemoveCommand.Dispose(); + } +} diff --git a/OpenUtauMobile/Views/OptionsView.axaml b/OpenUtauMobile/Views/OptionsView.axaml index 4216cab4..a06c2e9b 100644 --- a/OpenUtauMobile/Views/OptionsView.axaml +++ b/OpenUtauMobile/Views/OptionsView.axaml @@ -148,6 +148,39 @@ + + + + + + + + + + + + + + + + +