mcpField
A
audio-analyzer

audio-analyzer

github.com/dkyazzentwatwa创意与媒体12490 次安装29 周前更新
代码仓库

关于 audio-analyzer

audio-analyzer 是用于分析音频文件的 Python 工具包和命令行工具,适合音乐及音频处理场景。它将节拍、响度和频率测量与可视化及报告导出结合起来。

主要功能

  1. 01检测速度和节拍
  2. 02识别调性和大小调
  3. 03分析频率与响度指标
  4. 04生成波形图和频谱图
  5. 05导出 JSON、PNG、SVG 和 TXT

适用场景

  1. 01查看歌曲的速度和调性
  2. 02检查播客音量水平
  3. 03批量分析音频文件

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/dkyazzentwatwa/chatgpt-skills --skill audio-analyzer

安装后在 Claude Code 中输入/audio-analyzer确认已加载。

附带文件scripts/audio_analyzer.pyscripts/requirements.txt

详细介绍

音频分析器

一套用于分析音频文件的综合工具包。提取音频的详细信息,包括节奏、调性、频率内容、响度指标,并生成专业的可视化图表。

快速开始

from scripts.audio_analyzer import AudioAnalyzer

# Analyze an audio file
analyzer = AudioAnalyzer("song.mp3")
analyzer.analyze()

# Get all analysis results
results = analyzer.get_results()
print(f"BPM: {results['tempo']['bpm']}")
print(f"Key: {results['key']['key']} {results['key']['mode']}")

# Generate visualizations
analyzer.plot_waveform("waveform.png")
analyzer.plot_spectrogram("spectrogram.png")

# Full report
analyzer.save_report("analysis_report.json")

功能

  • 节奏/BPM 检测:精准的节拍跟踪,并提供置信度分数
  • 调性检测:识别音乐调性和调式(大调/小调)
  • 频率分析:频谱、主导频率、频段
  • 响度指标:RMS、峰值、LUFS、动态范围
  • 波形可视化:多声道波形图
  • 频谱图:可自定义的时频可视化
  • 色度图:用于和声分析的音级可视化
  • 节拍网格:叠加在波形上的可视化节拍标记
  • 导出格式:JSON 报告、PNG/SVG 可视化图表

API 参考

初始化
# From file
analyzer = AudioAnalyzer("audio.mp3")

# With custom sample rate
analyzer = AudioAnalyzer("audio.wav", sr=44100)
分析方法
# Run full analysis
analyzer.analyze()

# Individual analyses
analyzer.analyze_tempo()      # BPM and beat positions
analyzer.analyze_key()        # Musical key detection
analyzer.analyze_loudness()   # RMS, peak, LUFS
analyzer.analyze_frequency()  # Spectrum analysis
analyzer.analyze_dynamics()   # Dynamic range
结果访问
# Get all results as dict
results = analyzer.get_results()

# Individual results
tempo = analyzer.get_tempo()        # {'bpm': 120, 'confidence': 0.85, 'beats': [...]}
key = analyzer.get_key()            # {'key': 'C', 'mode': 'major', 'confidence': 0.72}
loudness = analyzer.get_loudness()  # {'rms_db': -14.2, 'peak_db': -0.5, 'lufs': -14.0}
freq = analyzer.get_frequency()     # {'dominant_freq': 440, 'spectrum': [...]}
可视化方法
# Waveform
analyzer.plot_waveform(
    output="waveform.png",
    figsize=(12, 4),
    color="#1f77b4",
    show_rms=True
)

# Spectrogram
analyzer.plot_spectrogram(
    output="spectrogram.png",
    figsize=(12, 6),
    cmap="magma",           # viridis, plasma, inferno, magma
    freq_scale="log",       # linear, log, mel
    max_freq=8000           # Hz
)

# Chromagram (pitch classes)
analyzer.plot_chromagram(
    output="chromagram.png",
    figsize=(12, 4)
)

# Onset strength / beat grid
analyzer.plot_beats(
    output="beats.png",
    figsize=(12, 4),
    show_strength=True
)

# Combined dashboard
analyzer.plot_dashboard(
    output="dashboard.png",
    figsize=(14, 10)
)
导出
# JSON report with all analysis
analyzer.save_report("report.json")

# Summary text
summary = analyzer.get_summary()
print(summary)

分析详情

节奏检测

使用节拍跟踪算法检测:

  • BPM:每分钟节拍数(节奏)
  • 节拍位置:检测到的节拍时间戳
  • 置信度:可靠性分数(0-1)
tempo = analyzer.get_tempo()
# {
#     'bpm': 128.0,
#     'confidence': 0.89,
#     'beats': [0.0, 0.469, 0.938, 1.406, ...],  # seconds
#     'beat_count': 256
# }
调性检测

分析和声内容以识别:

  • 调性:主音(C、C#、D 等)
  • 调式:大调或小调
  • 置信度:检测置信度
  • 调性轮廓:与各调性的相关性
key = analyzer.get_key()
# {
#     'key': 'A',
#     'mode': 'minor',
#     'confidence': 0.76,
#     'profile': {'C': 0.12, 'C#': 0.08, ...}
# }
响度指标

全面的响度分析:

  • RMS dB:均方根电平
  • 峰值 dB:最大采样电平
  • LUFS:综合响度(广播标准)
  • 动态范围:响亮部分与安静部分之间的差异
loudness = analyzer.get_loudness()
# {
#     'rms_db': -14.2,
#     'peak_db': -0.3,
#     'lufs': -14.0,
#     'dynamic_range_db': 12.5,
#     'crest_factor': 8.2
# }
频率分析

频谱分析包括:

  • 主导频率:最强的频率分量
  • 频段:低音、中音、高音中的能量
  • 谱质心:音频的“明亮度”
  • 谱滚降:低于该频率的部分包含 85% 能量
freq = analyzer.get_frequency()
# {
#     'dominant_freq': 440.0,
#     'spectral_centroid': 2150.3,
#     'spectral_rolloff': 4200.5,
#     'bands': {
#         'sub_bass': -28.5,      # 20-60 Hz
#         'bass': -18.2,          # 60-250 Hz
#         'low_mid': -12.1,       # 250-500 Hz
#         'mid': -10.8,           # 500-2000 Hz
#         'high_mid': -14.3,      # 2000-4000 Hz
#         'high': -22.1           # 4000-20000 Hz
#     }
# }

CLI 用法

# Full analysis with all visualizations
python audio_analyzer.py --input song.mp3 --output-dir ./analysis/

# Just tempo and key
python audio_analyzer.py --input song.mp3 --analyze tempo key --output report.json

# Generate specific visualization
python audio_analyzer.py --input song.mp3 --plot spectrogram --output spec.png

# Dashboard view
python audio_analyzer.py --input song.mp3 --dashboard --output dashboard.png

# Batch analyze directory
python audio_analyzer.py --input-dir ./songs/ --output-dir ./reports/
CLI 参数
参数说明默认值
--input输入音频文件必填
--input-dir音频文件目录-
--output输出文件路径-
--output-dir输出目录.
--analyze分析类型:tempo、key、loudness、frequency、allall
--plot图表类型:waveform、spectrogram、chromagram、beats、dashboard-
--format输出格式:json、txtjson
--sr分析采样率22050

示例

歌曲分析
analyzer = AudioAnalyzer("track.mp3")
analyzer.analyze()

print(f"Tempo: {analyzer.get_tempo()['bpm']:.1f} BPM")
print(f"Key: {analyzer.get_key()['key']} {analyzer.get_key()['mode']}")
print(f"Loudness: {analyzer.get_loudness()['lufs']:.1f} LUFS")

analyzer.plot_dashboard("track_analysis.png")
播客质量检查
analyzer = AudioAnalyzer("podcast.mp3")
analyzer.analyze_loudness()

loudness = analyzer.get_loudness()
if loudness['lufs'] > -16:
    print("Warning: Audio may be too loud for podcast standards")
elif loudness['lufs'] < -20:
    print("Warning: Audio may be too quiet")
else:
    print("Loudness is within podcast standards (-16 to -20 LUFS)")
批量分析
import os
from scripts.audio_analyzer import AudioAnalyzer

results = []
for filename in os.listdir("./songs"):
    if filename.endswith(('.mp3', '.wav', '.flac')):
        analyzer = AudioAnalyzer(f"./songs/{filename}")
        analyzer.analyze()
        results.append({
            'file': filename,
            'bpm': analyzer.get_tempo()['bpm'],
            'key': f"{analyzer.get_key()['key']} {analyzer.get_key()['mode']}",
            'lufs': analyzer.get_loudness()['lufs']
        })

# Sort by BPM for DJ set
results.sort(key=lambda x: x['bpm'])

支持的格式

输入格式(通过 librosa/soundfile):

  • MP3
  • WAV
  • FLAC
  • OGG
  • M4A/AAC
  • AIFF

输出格式:

  • JSON(分析报告)
  • PNG(可视化图表)
  • SVG(可视化图表)
  • TXT(摘要)

依赖项

librosa>=0.10.0
soundfile>=0.12.0
matplotlib>=3.7.0
numpy>=1.24.0
scipy>=1.10.0

局限性

  • 调性检测最适用于包含旋律的内容(对鼓/打击乐的准确度较低)
  • BPM 检测可能难以处理自由节奏或复杂拍号
  • 很短的片段(<5 秒)准确度可能较低
  • LUFS 计算经过简化(并未完整实现 ITU-R BS.1770-4)

评论

支持 Markdown