You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Java中实现和弦与歌词对应音节的精准上方对齐

在Java中实现和弦与歌词的精准对齐展示

完全可以在Java中实现这个需求,核心思路是先解析输入字符串分离和弦与纯歌词,再将和弦精准填充到对应位置的空格行中,最终实现上下对齐。

实现步骤与代码

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ChordLyricsAligner {
    public static String alignChords(String input) {
        if (input == null || input.isEmpty()) {
            return "";
        }

        StringBuilder pureLyrics = new StringBuilder();
        List<ChordPlacement> chordPlacements = new ArrayList<>();
        int currentLyricsLength = 0;
        int index = 0;

        while (index < input.length()) {
            if (input.charAt(index) == '[') {
                int closeBracketIndex = input.indexOf(']', index + 1);
                if (closeBracketIndex == -1) {
                    pureLyrics.append(input.charAt(index));
                    currentLyricsLength++;
                    index++;
                    continue;
                }
                // 提取和弦内容并记录其在纯歌词中的起始位置
                String chord = input.substring(index + 1, closeBracketIndex);
                chordPlacements.add(new ChordPlacement(currentLyricsLength, chord));
                index = closeBracketIndex + 1;
            } else {
                pureLyrics.append(input.charAt(index));
                currentLyricsLength++;
                index++;
            }
        }

        String lyrics = pureLyrics.toString();
        // 初始化和弦行为全空格
        char[] chordLineChars = new char[lyrics.length()];
        Arrays.fill(chordLineChars, ' ');

        // 将和弦填充到对应位置
        for (ChordPlacement cp : chordPlacements) {
            int startPos = cp.position;
            String chord = cp.chord;
            // 处理边界情况,避免数组越界
            if (startPos + chord.length() > chordLineChars.length) {
                startPos = chordLineChars.length - chord.length();
            }
            if (startPos < 0) {
                startPos = 0;
            }
            for (int i = 0; i < chord.length(); i++) {
                chordLineChars[startPos + i] = chord.charAt(i);
            }
        }

        return new String(chordLineChars) + "\n" + lyrics;
    }

    // 内部类存储和弦的位置与内容
    private static class ChordPlacement {
        int position;
        String chord;

        ChordPlacement(int position, String chord) {
            this.position = position;
            this.chord = chord;
        }
    }

    // 测试示例
    public static void main(String[] args) {
        String input = "[G]These are the lyrics o[Am]f the song";
        String alignedResult = alignChords(input);
        System.out.println(alignedResult);
    }
}

代码说明

  1. 解析阶段:遍历输入字符串,将普通字符存入纯歌词缓冲区,遇到[和弦]格式时提取和弦内容,并记录该和弦在纯歌词中对应的起始位置。
  2. 构建和弦行:创建与纯歌词长度一致的全空格字符数组,再将每个和弦覆盖到对应位置的空格上,保证和弦与下方歌词精准对齐。
  3. 边界处理:针对不闭合的括号、和弦长度超出剩余空间的情况做了兼容,避免运行时异常。

运行测试代码后,输出结果与你期望的完全一致:

G                    Am
These are the lyrics of the song

内容的提问来源于stack exchange,提问作者Ricardo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 20:05:11