|
import librosa |
|
import numpy as np |
|
from scipy import signal |
|
from collections import Counter |
|
try: |
|
import matplotlib.pyplot as plt |
|
except ImportError: |
|
plt = None |
|
from scipy.stats import mode |
|
import warnings |
|
warnings.filterwarnings('ignore') |
|
|
|
class MusicAnalyzer: |
|
def __init__(self): |
|
|
|
self.emotion_profiles = { |
|
'happy': {'tempo': (100, 180), 'energy': (0.6, 1.0), 'major_mode': True, 'brightness': (0.6, 1.0)}, |
|
'sad': {'tempo': (40, 90), 'energy': (0, 0.5), 'major_mode': False, 'brightness': (0, 0.5)}, |
|
'calm': {'tempo': (50, 90), 'energy': (0, 0.4), 'major_mode': True, 'brightness': (0.3, 0.6)}, |
|
'energetic': {'tempo': (110, 200), 'energy': (0.7, 1.0), 'major_mode': True, 'brightness': (0.5, 0.9)}, |
|
'tense': {'tempo': (70, 140), 'energy': (0.5, 0.9), 'major_mode': False, 'brightness': (0.3, 0.7)}, |
|
'nostalgic': {'tempo': (60, 100), 'energy': (0.3, 0.7), 'major_mode': None, 'brightness': (0.4, 0.7)} |
|
} |
|
|
|
|
|
self.theme_profiles = { |
|
'love': {'emotion': ['happy', 'nostalgic', 'sad'], 'harmony_complexity': (0.3, 0.7)}, |
|
'triumph': {'emotion': ['energetic', 'happy'], 'harmony_complexity': (0.4, 0.8)}, |
|
'loss': {'emotion': ['sad', 'nostalgic'], 'harmony_complexity': (0.3, 0.7)}, |
|
'adventure': {'emotion': ['energetic', 'tense'], 'harmony_complexity': (0.5, 0.9)}, |
|
'reflection': {'emotion': ['calm', 'nostalgic'], 'harmony_complexity': (0.4, 0.8)}, |
|
'conflict': {'emotion': ['tense', 'energetic'], 'harmony_complexity': (0.6, 1.0)} |
|
} |
|
|
|
|
|
self.key_names = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B'] |
|
|
|
|
|
|
|
self.common_time_signatures = { |
|
"4/4": {"beats_per_bar": 4, "beat_pattern": [1.0, 0.2, 0.5, 0.2], "weight": 0.45}, |
|
"3/4": {"beats_per_bar": 3, "beat_pattern": [1.0, 0.2, 0.3], "weight": 0.25}, |
|
"2/4": {"beats_per_bar": 2, "beat_pattern": [1.0, 0.3], "weight": 0.15}, |
|
"6/8": {"beats_per_bar": 6, "beat_pattern": [1.0, 0.2, 0.3, 0.8, 0.2, 0.3], "weight": 0.15} |
|
} |
|
|
|
|
|
self.accent_patterns = { |
|
"4/4": [[1, 0, 0, 0], [1, 0, 2, 0], [1, 0, 2, 0, 3, 0, 2, 0]], |
|
"3/4": [[1, 0, 0], [1, 0, 2]], |
|
"2/4": [[1, 0], [1, 2]], |
|
"6/8": [[1, 0, 0, 2, 0, 0], [1, 0, 0, 2, 0, 3]] |
|
} |
|
|
|
|
|
self.rhythm_density = { |
|
"4/4": [1.0, 0.7, 0.8, 0.6], |
|
"3/4": [1.0, 0.6, 0.7], |
|
"6/8": [1.0, 0.5, 0.4, 0.8, 0.5, 0.4], |
|
"2/4": [1.0, 0.6] |
|
} |
|
|
|
def load_audio(self, file_path, sr=22050, duration=None): |
|
"""Load audio file and return time series and sample rate""" |
|
try: |
|
y, sr = librosa.load(file_path, sr=sr, duration=duration) |
|
return y, sr |
|
except Exception as e: |
|
print(f"Error loading audio file: {e}") |
|
return None, None |
|
|
|
def analyze_rhythm(self, y, sr): |
|
"""Analyze rhythm-related features: tempo, beats, time signature""" |
|
|
|
onset_env = librosa.onset.onset_strength(y=y, sr=sr) |
|
tempo, beat_frames = librosa.beat.beat_track(onset_envelope=onset_env, sr=sr) |
|
beat_times = librosa.frames_to_time(beat_frames, sr=sr) |
|
|
|
|
|
beat_intervals = np.diff(beat_times) if len(beat_times) > 1 else np.array([0]) |
|
beat_regularity = 1.0 / np.std(beat_intervals) if len(beat_intervals) > 0 and np.std(beat_intervals) > 0 else 0 |
|
|
|
|
|
ac = librosa.autocorrelate(onset_env, max_size=sr // 2) |
|
ac = librosa.util.normalize(ac, norm=np.inf) |
|
|
|
|
|
time_sig_result = self._detect_time_signature(y, sr) |
|
|
|
|
|
estimated_signature = time_sig_result["time_signature"] |
|
time_sig_confidence = time_sig_result["confidence"] |
|
|
|
|
|
rhythm_intensity = np.mean(onset_env) / np.max(onset_env) if np.max(onset_env) > 0 else 0 |
|
|
|
|
|
rhythm_complexity = np.std(onset_env) / np.mean(onset_env) if np.mean(onset_env) > 0 else 0 |
|
|
|
|
|
beat_times_list = [float(t) for t in beat_times.tolist()] |
|
beat_intervals_list = [float(i) for i in beat_intervals.tolist()] |
|
|
|
return { |
|
"tempo": float(tempo), |
|
"beat_times": beat_times_list, |
|
"beat_intervals": beat_intervals_list, |
|
"beat_regularity": float(beat_regularity), |
|
"rhythm_intensity": float(rhythm_intensity), |
|
"rhythm_complexity": float(rhythm_complexity), |
|
"estimated_time_signature": estimated_signature, |
|
"time_signature_confidence": float(time_sig_confidence), |
|
"time_signature_candidates": time_sig_result.get("all_candidates", {}) |
|
} |
|
|
|
def _detect_time_signature(self, y, sr): |
|
""" |
|
Multi-method approach to time signature detection |
|
|
|
Args: |
|
y: Audio signal |
|
sr: Sample rate |
|
|
|
Returns: |
|
dict with detected time signature and confidence |
|
""" |
|
|
|
onset_env = librosa.onset.onset_strength(y=y, sr=sr, hop_length=512) |
|
|
|
|
|
tempo, beat_frames = librosa.beat.beat_track(onset_envelope=onset_env, sr=sr) |
|
beat_times = librosa.frames_to_time(beat_frames, sr=sr) |
|
|
|
|
|
if len(beat_times) < 8: |
|
return {"time_signature": "4/4", "confidence": 0.5} |
|
|
|
|
|
beat_strengths = self._get_beat_strengths(y, sr, beat_times, onset_env) |
|
|
|
|
|
results = {} |
|
|
|
|
|
autocorr_result = self._detect_by_autocorrelation(onset_env, sr) |
|
results["autocorrelation"] = autocorr_result |
|
|
|
|
|
pattern_result = self._detect_by_pattern_matching(beat_strengths) |
|
results["pattern_matching"] = pattern_result |
|
|
|
|
|
spectral_result = self._detect_by_spectral_analysis(onset_env, sr) |
|
results["spectral"] = spectral_result |
|
|
|
|
|
density_result = self._detect_by_note_density(y, sr, beat_times) |
|
results["note_density"] = density_result |
|
|
|
|
|
tempo_result = self._estimate_from_tempo(tempo) |
|
results["tempo_based"] = tempo_result |
|
|
|
|
|
final_result = self._combine_detection_results(results, tempo) |
|
|
|
return final_result |
|
|
|
def _get_beat_strengths(self, y, sr, beat_times, onset_env): |
|
"""Extract normalized strengths at beat positions""" |
|
|
|
beat_frames = librosa.time_to_frames(beat_times, sr=sr, hop_length=512) |
|
beat_frames = [min(f, len(onset_env)-1) for f in beat_frames] |
|
|
|
|
|
beat_strengths = np.array([onset_env[f] for f in beat_frames]) |
|
|
|
|
|
hop_length = 512 |
|
frame_length = 2048 |
|
|
|
|
|
energy = librosa.feature.rms(y=y, frame_length=frame_length, hop_length=hop_length)[0] |
|
beat_energy = np.array([energy[min(f, len(energy)-1)] for f in beat_frames]) |
|
|
|
|
|
beat_strengths = 0.7 * beat_strengths + 0.3 * beat_energy |
|
|
|
|
|
if np.max(beat_strengths) > 0: |
|
beat_strengths = beat_strengths / np.max(beat_strengths) |
|
|
|
return beat_strengths |
|
|
|
def _detect_by_autocorrelation(self, onset_env, sr): |
|
"""Detect meter using autocorrelation of onset strength""" |
|
|
|
hop_length = 512 |
|
ac = librosa.autocorrelate(onset_env, max_size=4 * sr // hop_length) |
|
ac = librosa.util.normalize(ac) |
|
|
|
|
|
peaks = signal.find_peaks(ac, height=0.2, distance=sr//(8*hop_length))[0] |
|
|
|
if len(peaks) < 2: |
|
return {"time_signature": "4/4", "confidence": 0.4} |
|
|
|
|
|
peak_intervals = np.diff(peaks) |
|
|
|
|
|
peak_times = peaks * hop_length / sr |
|
|
|
|
|
time_sig_votes = {} |
|
|
|
|
|
for ts, info in self.common_time_signatures.items(): |
|
beats_per_bar = info["beats_per_bar"] |
|
|
|
|
|
score = 0 |
|
for interval in peak_intervals: |
|
|
|
|
|
expected = beats_per_bar * (hop_length / sr) |
|
tolerance = 0.25 * expected |
|
|
|
if abs(interval * hop_length / sr - expected) < tolerance: |
|
score += 1 |
|
|
|
if len(peak_intervals) > 0: |
|
time_sig_votes[ts] = score / len(peak_intervals) |
|
|
|
|
|
if time_sig_votes: |
|
best_ts = max(time_sig_votes.items(), key=lambda x: x[1]) |
|
return {"time_signature": best_ts[0], "confidence": best_ts[1]} |
|
|
|
return {"time_signature": "4/4", "confidence": 0.4} |
|
|
|
def _detect_by_pattern_matching(self, beat_strengths): |
|
"""Match beat strength patterns against known time signature patterns""" |
|
if len(beat_strengths) < 6: |
|
return {"time_signature": "4/4", "confidence": 0.4} |
|
|
|
results = {} |
|
|
|
|
|
for ts, info in self.common_time_signatures.items(): |
|
beats_per_bar = info["beats_per_bar"] |
|
expected_pattern = info["beat_pattern"] |
|
|
|
|
|
scores = [] |
|
|
|
|
|
if len(beat_strengths) >= beats_per_bar: |
|
|
|
for offset in range(min(beats_per_bar, len(beat_strengths) - beats_per_bar + 1)): |
|
|
|
pattern_scores = [] |
|
|
|
for i in range(offset, len(beat_strengths) - beats_per_bar + 1, beats_per_bar): |
|
segment = beat_strengths[i:i+beats_per_bar] |
|
|
|
|
|
pattern = expected_pattern[:len(segment)] |
|
|
|
|
|
if np.std(segment) > 0 and np.std(pattern) > 0: |
|
|
|
corr = np.corrcoef(segment, pattern)[0, 1] |
|
if not np.isnan(corr): |
|
pattern_scores.append(corr) |
|
|
|
if pattern_scores: |
|
scores.append(np.mean(pattern_scores)) |
|
|
|
|
|
if scores: |
|
confidence = max(scores) |
|
results[ts] = confidence |
|
|
|
|
|
if results: |
|
best_ts = max(results.items(), key=lambda x: x[1]) |
|
return {"time_signature": best_ts[0], "confidence": best_ts[1]} |
|
|
|
|
|
return {"time_signature": "4/4", "confidence": 0.5} |
|
|
|
def _detect_by_spectral_analysis(self, onset_env, sr): |
|
"""Analyze rhythm in frequency domain""" |
|
|
|
|
|
hop_length = 512 |
|
|
|
|
|
fft_size = 2**13 |
|
S = np.abs(np.fft.rfft(onset_env, n=fft_size)) |
|
|
|
|
|
freqs = np.fft.rfftfreq(fft_size, d=hop_length/sr) |
|
tempos = 60 * freqs |
|
|
|
|
|
tempo_mask = (tempos >= 40) & (tempos <= 240) |
|
S_tempo = S[tempo_mask] |
|
tempos = tempos[tempo_mask] |
|
|
|
|
|
peaks = signal.find_peaks(S_tempo, height=np.max(S_tempo)*0.1, distance=5)[0] |
|
|
|
if len(peaks) == 0: |
|
return {"time_signature": "4/4", "confidence": 0.4} |
|
|
|
|
|
peak_tempos = tempos[peaks] |
|
peak_strengths = S_tempo[peaks] |
|
|
|
|
|
peak_indices = np.argsort(peak_strengths)[::-1] |
|
peak_tempos = peak_tempos[peak_indices] |
|
peak_strengths = peak_strengths[peak_indices] |
|
|
|
|
|
|
|
|
|
|
|
time_sig_scores = {} |
|
|
|
|
|
if len(peak_tempos) >= 2: |
|
tempo_ratios = [] |
|
for i in range(len(peak_tempos)): |
|
for j in range(i+1, len(peak_tempos)): |
|
if peak_tempos[j] > 0: |
|
ratio = peak_tempos[i] / peak_tempos[j] |
|
tempo_ratios.append(ratio) |
|
|
|
|
|
for ts in self.common_time_signatures: |
|
score = 0 |
|
|
|
if ts == "4/4" or ts == "2/4": |
|
|
|
for ratio in tempo_ratios: |
|
if abs(ratio - 2) < 0.2 or abs(ratio - 4) < 0.2: |
|
score += 1 |
|
|
|
elif ts == "3/4" or ts == "6/8": |
|
|
|
for ratio in tempo_ratios: |
|
if abs(ratio - 3) < 0.2 or abs(ratio - 6) < 0.3: |
|
score += 1 |
|
|
|
|
|
if tempo_ratios: |
|
time_sig_scores[ts] = min(1.0, score / len(tempo_ratios) + 0.4) |
|
|
|
|
|
if time_sig_scores: |
|
best_ts = max(time_sig_scores.items(), key=lambda x: x[1]) |
|
return {"time_signature": best_ts[0], "confidence": best_ts[1]} |
|
|
|
|
|
return {"time_signature": "4/4", "confidence": 0.4} |
|
|
|
def _detect_by_note_density(self, y, sr, beat_times): |
|
"""Analyze note density patterns between beats""" |
|
if len(beat_times) < 6: |
|
return {"time_signature": "4/4", "confidence": 0.4} |
|
|
|
|
|
onset_times = librosa.onset.onset_detect(y=y, sr=sr, units='time') |
|
|
|
if len(onset_times) < len(beat_times): |
|
return {"time_signature": "4/4", "confidence": 0.4} |
|
|
|
|
|
note_counts = [] |
|
for i in range(len(beat_times) - 1): |
|
start = beat_times[i] |
|
end = beat_times[i+1] |
|
|
|
|
|
count = sum(1 for t in onset_times if start <= t < end) |
|
note_counts.append(count) |
|
|
|
|
|
time_sig_scores = {} |
|
|
|
for ts, info in self.common_time_signatures.items(): |
|
beats_per_bar = info["beats_per_bar"] |
|
|
|
|
|
if len(note_counts) < beats_per_bar: |
|
continue |
|
|
|
|
|
scores = [] |
|
|
|
for offset in range(min(beats_per_bar, len(note_counts) - beats_per_bar + 1)): |
|
similarities = [] |
|
|
|
for i in range(offset, len(note_counts) - beats_per_bar + 1, beats_per_bar): |
|
|
|
pattern = note_counts[i:i+beats_per_bar] |
|
|
|
|
|
expected = self.rhythm_density.get(ts, [1.0] * beats_per_bar) |
|
expected = expected[:len(pattern)] |
|
|
|
|
|
if sum(pattern) > 0 and sum(expected) > 0: |
|
pattern_norm = [p/max(1, sum(pattern)) for p in pattern] |
|
expected_norm = [e/sum(expected) for e in expected] |
|
|
|
|
|
distance = sum(abs(p - e) for p, e in zip(pattern_norm, expected_norm)) / len(pattern) |
|
similarity = 1 - min(1.0, distance) |
|
similarities.append(similarity) |
|
|
|
if similarities: |
|
scores.append(np.mean(similarities)) |
|
|
|
|
|
if scores: |
|
time_sig_scores[ts] = max(scores) |
|
|
|
|
|
if time_sig_scores: |
|
best_ts = max(time_sig_scores.items(), key=lambda x: x[1]) |
|
return {"time_signature": best_ts[0], "confidence": best_ts[1]} |
|
|
|
|
|
return {"time_signature": "4/4", "confidence": 0.4} |
|
|
|
def _estimate_from_tempo(self, tempo): |
|
"""Use tempo to help estimate likely time signature""" |
|
|
|
|
|
|
|
scores = {} |
|
|
|
if tempo < 70: |
|
|
|
scores = { |
|
"4/4": 0.5, |
|
"3/4": 0.4, |
|
"2/4": 0.3, |
|
"6/8": 0.7 |
|
} |
|
elif 70 <= tempo <= 120: |
|
|
|
scores = { |
|
"4/4": 0.7, |
|
"3/4": 0.6, |
|
"2/4": 0.4, |
|
"6/8": 0.3 |
|
} |
|
else: |
|
|
|
scores = { |
|
"4/4": 0.6, |
|
"2/4": 0.7, |
|
"3/4": 0.4, |
|
"6/8": 0.2 |
|
} |
|
|
|
|
|
best_ts = max(scores.items(), key=lambda x: x[1]) |
|
return {"time_signature": best_ts[0], "confidence": best_ts[1]} |
|
|
|
def _combine_detection_results(self, results, tempo): |
|
"""Combine results from different detection methods""" |
|
|
|
method_weights = { |
|
"autocorrelation": 0.25, |
|
"pattern_matching": 0.30, |
|
"spectral": 0.20, |
|
"note_density": 0.20, |
|
"tempo_based": 0.05 |
|
} |
|
|
|
|
|
prior_weights = {ts: info["weight"] for ts, info in self.common_time_signatures.items()} |
|
|
|
|
|
total_votes = {ts: prior_weights.get(ts, 0.1) for ts in self.common_time_signatures} |
|
|
|
for method, result in results.items(): |
|
ts = result["time_signature"] |
|
confidence = result["confidence"] |
|
weight = method_weights.get(method, 0.1) |
|
|
|
|
|
if ts in total_votes: |
|
total_votes[ts] += confidence * weight |
|
else: |
|
total_votes[ts] = confidence * weight |
|
|
|
|
|
if "3/4" in total_votes and "6/8" in total_votes: |
|
|
|
if abs(total_votes["3/4"] - total_votes["6/8"]) < 0.1: |
|
if tempo < 100: |
|
total_votes["6/8"] += 0.1 |
|
else: |
|
total_votes["3/4"] += 0.1 |
|
|
|
|
|
best_ts = max(total_votes.items(), key=lambda x: x[1]) |
|
|
|
|
|
confidence = best_ts[1] / (sum(total_votes.values()) + 0.001) |
|
confidence = min(0.95, max(0.4, confidence)) |
|
|
|
return { |
|
"time_signature": best_ts[0], |
|
"confidence": confidence, |
|
"all_candidates": {ts: float(score) for ts, score in total_votes.items()} |
|
} |
|
|
|
def _evaluate_beat_pattern(self, beat_strengths, pattern_length): |
|
""" |
|
Evaluate how consistently a specific pattern length fits the beat strengths |
|
|
|
Args: |
|
beat_strengths: Array of normalized beat strengths |
|
pattern_length: Length of pattern to evaluate |
|
|
|
Returns: |
|
score: How well this pattern length explains the data (0-1) |
|
""" |
|
if len(beat_strengths) < pattern_length * 2: |
|
return 0.0 |
|
|
|
|
|
correlations = [] |
|
|
|
num_full_patterns = len(beat_strengths) // pattern_length |
|
for i in range(num_full_patterns - 1): |
|
pattern1 = beat_strengths[i*pattern_length:(i+1)*pattern_length] |
|
pattern2 = beat_strengths[(i+1)*pattern_length:(i+2)*pattern_length] |
|
|
|
|
|
if len(pattern1) == len(pattern2) and len(pattern1) > 0: |
|
corr = np.corrcoef(pattern1, pattern2)[0, 1] |
|
if not np.isnan(corr): |
|
correlations.append(corr) |
|
|
|
|
|
variance_score = 0 |
|
if num_full_patterns >= 2: |
|
position_values = [[] for _ in range(pattern_length)] |
|
|
|
for i in range(num_full_patterns): |
|
for pos in range(pattern_length): |
|
idx = i * pattern_length + pos |
|
if idx < len(beat_strengths): |
|
position_values[pos].append(beat_strengths[idx]) |
|
|
|
|
|
between_pos_var = np.var([np.mean(vals) for vals in position_values if vals]) |
|
within_pos_var = np.mean([np.var(vals) for vals in position_values if len(vals) > 1]) |
|
|
|
if within_pos_var > 0: |
|
variance_score = between_pos_var / within_pos_var |
|
variance_score = min(1.0, variance_score / 2.0) |
|
|
|
|
|
if correlations: |
|
correlation_score = np.mean(correlations) |
|
return 0.7 * correlation_score + 0.3 * variance_score |
|
|
|
return 0.5 * variance_score |
|
|
|
def _extract_average_pattern(self, beat_strengths, pattern_length): |
|
""" |
|
Extract the average beat pattern of specified length |
|
|
|
Args: |
|
beat_strengths: Array of beat strengths |
|
pattern_length: Length of pattern to extract |
|
|
|
Returns: |
|
Average pattern of the specified length |
|
""" |
|
if len(beat_strengths) < pattern_length: |
|
return np.array([]) |
|
|
|
|
|
num_patterns = len(beat_strengths) // pattern_length |
|
|
|
if num_patterns == 0: |
|
return np.array([]) |
|
|
|
|
|
patterns = beat_strengths[:num_patterns * pattern_length].reshape((num_patterns, pattern_length)) |
|
return np.mean(patterns, axis=0) |
|
|
|
def analyze_tonality(self, y, sr): |
|
"""Analyze tonal features: key, mode, harmonic features""" |
|
|
|
chroma = librosa.feature.chroma_cqt(y=y, sr=sr) |
|
|
|
|
|
|
|
major_profile = np.array([6.35, 2.23, 3.48, 2.33, 4.38, 4.09, 2.52, 5.19, 2.39, 3.66, 2.29, 2.88]) |
|
minor_profile = np.array([6.33, 2.68, 3.52, 5.38, 2.60, 3.53, 2.54, 4.75, 3.98, 2.69, 3.34, 3.17]) |
|
|
|
|
|
chroma_avg = np.mean(chroma, axis=1) |
|
major_corr = np.zeros(12) |
|
minor_corr = np.zeros(12) |
|
|
|
for i in range(12): |
|
major_corr[i] = np.corrcoef(np.roll(chroma_avg, i), major_profile)[0, 1] |
|
minor_corr[i] = np.corrcoef(np.roll(chroma_avg, i), minor_profile)[0, 1] |
|
|
|
|
|
max_major_idx = np.argmax(major_corr) |
|
max_minor_idx = np.argmax(minor_corr) |
|
|
|
|
|
if major_corr[max_major_idx] > minor_corr[max_minor_idx]: |
|
mode = "major" |
|
key = self.key_names[max_major_idx] |
|
else: |
|
mode = "minor" |
|
key = self.key_names[max_minor_idx] |
|
|
|
|
|
harmony_complexity = np.std(chroma) / np.mean(chroma) if np.mean(chroma) > 0 else 0 |
|
|
|
|
|
tonal_stability = 1.0 / (np.std(chroma_avg) + 0.001) |
|
|
|
|
|
spectral_centroid = librosa.feature.spectral_centroid(y=y, sr=sr)[0] |
|
brightness = np.mean(spectral_centroid) / (sr/2) |
|
|
|
|
|
spectral_contrast = librosa.feature.spectral_contrast(y=y, sr=sr) |
|
dissonance = np.mean(spectral_contrast[0]) |
|
|
|
return { |
|
"key": key, |
|
"mode": mode, |
|
"is_major": mode == "major", |
|
"harmony_complexity": float(harmony_complexity), |
|
"tonal_stability": float(tonal_stability), |
|
"brightness": float(brightness), |
|
"dissonance": float(dissonance) |
|
} |
|
|
|
def analyze_energy(self, y, sr): |
|
"""Analyze energy characteristics of the audio""" |
|
|
|
rms = librosa.feature.rms(y=y)[0] |
|
|
|
|
|
mean_energy = np.mean(rms) |
|
energy_std = np.std(rms) |
|
energy_dynamic_range = np.max(rms) - np.min(rms) if len(rms) > 0 else 0 |
|
|
|
|
|
spec = np.abs(librosa.stft(y)) |
|
|
|
|
|
freq_bins = spec.shape[0] |
|
low_freq_energy = np.mean(spec[:int(freq_bins*0.2), :]) |
|
mid_freq_energy = np.mean(spec[int(freq_bins*0.2):int(freq_bins*0.8), :]) |
|
high_freq_energy = np.mean(spec[int(freq_bins*0.8):, :]) |
|
|
|
|
|
total_energy = low_freq_energy + mid_freq_energy + high_freq_energy |
|
if total_energy > 0: |
|
low_freq_ratio = low_freq_energy / total_energy |
|
mid_freq_ratio = mid_freq_energy / total_energy |
|
high_freq_ratio = high_freq_energy / total_energy |
|
else: |
|
low_freq_ratio = mid_freq_ratio = high_freq_ratio = 1/3 |
|
|
|
return { |
|
"mean_energy": float(mean_energy), |
|
"energy_std": float(energy_std), |
|
"energy_dynamic_range": float(energy_dynamic_range), |
|
"frequency_distribution": { |
|
"low_freq": float(low_freq_ratio), |
|
"mid_freq": float(mid_freq_ratio), |
|
"high_freq": float(high_freq_ratio) |
|
} |
|
} |
|
|
|
def analyze_emotion(self, rhythm_data, tonal_data, energy_data): |
|
"""Classify the emotion based on musical features""" |
|
|
|
tempo = rhythm_data["tempo"] |
|
is_major = tonal_data["is_major"] |
|
energy = energy_data["mean_energy"] |
|
brightness = tonal_data["brightness"] |
|
|
|
|
|
emotion_scores = {} |
|
for emotion, profile in self.emotion_profiles.items(): |
|
score = 0.0 |
|
|
|
|
|
tempo_range = profile["tempo"] |
|
if tempo_range[0] <= tempo <= tempo_range[1]: |
|
score += 1.0 |
|
else: |
|
|
|
distance = min(abs(tempo - tempo_range[0]), abs(tempo - tempo_range[1])) |
|
max_distance = 40 |
|
score += max(0, 1 - (distance / max_distance)) |
|
|
|
|
|
energy_range = profile["energy"] |
|
if energy_range[0] <= energy <= energy_range[1]: |
|
score += 1.0 |
|
else: |
|
|
|
distance = min(abs(energy - energy_range[0]), abs(energy - energy_range[1])) |
|
max_distance = 0.5 |
|
score += max(0, 1 - (distance / max_distance)) |
|
|
|
|
|
if profile["major_mode"] is not None: |
|
score += 1.0 if profile["major_mode"] == is_major else 0.0 |
|
else: |
|
score += 0.5 |
|
|
|
|
|
brightness_range = profile["brightness"] |
|
if brightness_range[0] <= brightness <= brightness_range[1]: |
|
score += 1.0 |
|
else: |
|
|
|
distance = min(abs(brightness - brightness_range[0]), abs(brightness - brightness_range[1])) |
|
max_distance = 0.5 |
|
score += max(0, 1 - (distance / max_distance)) |
|
|
|
|
|
emotion_scores[emotion] = score / 4.0 |
|
|
|
|
|
primary_emotion = max(emotion_scores.items(), key=lambda x: x[1]) |
|
|
|
|
|
|
|
valence_map = { |
|
'happy': 0.8, 'sad': 0.2, 'calm': 0.6, |
|
'energetic': 0.7, 'tense': 0.3, 'nostalgic': 0.5 |
|
} |
|
|
|
arousal_map = { |
|
'happy': 0.7, 'sad': 0.3, 'calm': 0.2, |
|
'energetic': 0.9, 'tense': 0.8, 'nostalgic': 0.4 |
|
} |
|
|
|
|
|
total_weight = sum(emotion_scores.values()) |
|
if total_weight > 0: |
|
valence = sum(score * valence_map[emotion] for emotion, score in emotion_scores.items()) / total_weight |
|
arousal = sum(score * arousal_map[emotion] for emotion, score in emotion_scores.items()) / total_weight |
|
else: |
|
valence = 0.5 |
|
arousal = 0.5 |
|
|
|
return { |
|
"primary_emotion": primary_emotion[0], |
|
"confidence": primary_emotion[1], |
|
"emotion_scores": emotion_scores, |
|
"valence": float(valence), |
|
"arousal": float(arousal) |
|
} |
|
|
|
def analyze_theme(self, rhythm_data, tonal_data, emotion_data): |
|
"""Infer potential themes based on musical features and emotion""" |
|
|
|
primary_emotion = emotion_data["primary_emotion"] |
|
harmony_complexity = tonal_data["harmony_complexity"] |
|
|
|
|
|
theme_scores = {} |
|
for theme, profile in self.theme_profiles.items(): |
|
score = 0.0 |
|
|
|
|
|
if primary_emotion in profile["emotion"]: |
|
|
|
position_weight = 1.0 / (profile["emotion"].index(primary_emotion) + 1) |
|
score += position_weight |
|
|
|
|
|
secondary_emotions = [e for e, s in emotion_data["emotion_scores"].items() |
|
if s > 0.5 and e != primary_emotion] |
|
for emotion in secondary_emotions: |
|
if emotion in profile["emotion"]: |
|
score += 0.3 |
|
|
|
|
|
complexity_range = profile["harmony_complexity"] |
|
if complexity_range[0] <= harmony_complexity <= complexity_range[1]: |
|
score += 1.0 |
|
else: |
|
|
|
distance = min(abs(harmony_complexity - complexity_range[0]), |
|
abs(harmony_complexity - complexity_range[1])) |
|
max_distance = 0.5 |
|
score += max(0, 1 - (distance / max_distance)) |
|
|
|
|
|
theme_scores[theme] = min(1.0, score / 2.5) |
|
|
|
|
|
primary_theme = max(theme_scores.items(), key=lambda x: x[1]) |
|
|
|
|
|
secondary_themes = [(theme, score) for theme, score in theme_scores.items() |
|
if score > 0.5 and theme != primary_theme[0]] |
|
secondary_themes.sort(key=lambda x: x[1], reverse=True) |
|
|
|
return { |
|
"primary_theme": primary_theme[0], |
|
"confidence": primary_theme[1], |
|
"secondary_themes": [t[0] for t in secondary_themes[:2]], |
|
"theme_scores": theme_scores |
|
} |
|
|
|
def analyze_music(self, file_path): |
|
"""Main function to perform comprehensive music analysis""" |
|
|
|
y, sr = self.load_audio(file_path) |
|
if y is None: |
|
return {"error": "Failed to load audio file"} |
|
|
|
|
|
rhythm_data = self.analyze_rhythm(y, sr) |
|
tonal_data = self.analyze_tonality(y, sr) |
|
energy_data = self.analyze_energy(y, sr) |
|
|
|
|
|
emotion_data = self.analyze_emotion(rhythm_data, tonal_data, energy_data) |
|
theme_data = self.analyze_theme(rhythm_data, tonal_data, emotion_data) |
|
|
|
|
|
def convert_numpy_to_python(obj): |
|
if isinstance(obj, dict): |
|
return {k: convert_numpy_to_python(v) for k, v in obj.items()} |
|
elif isinstance(obj, list): |
|
return [convert_numpy_to_python(item) for item in obj] |
|
elif isinstance(obj, np.ndarray): |
|
return obj.tolist() |
|
elif isinstance(obj, np.number): |
|
return float(obj) |
|
else: |
|
return obj |
|
|
|
|
|
rhythm_data = convert_numpy_to_python(rhythm_data) |
|
tonal_data = convert_numpy_to_python(tonal_data) |
|
energy_data = convert_numpy_to_python(energy_data) |
|
emotion_data = convert_numpy_to_python(emotion_data) |
|
theme_data = convert_numpy_to_python(theme_data) |
|
|
|
|
|
return { |
|
"file": file_path, |
|
"rhythm_analysis": rhythm_data, |
|
"tonal_analysis": tonal_data, |
|
"energy_analysis": energy_data, |
|
"emotion_analysis": emotion_data, |
|
"theme_analysis": theme_data, |
|
"summary": { |
|
"tempo": float(rhythm_data["tempo"]), |
|
"time_signature": rhythm_data["estimated_time_signature"], |
|
"key": tonal_data["key"], |
|
"mode": tonal_data["mode"], |
|
"primary_emotion": emotion_data["primary_emotion"], |
|
"primary_theme": theme_data["primary_theme"] |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
analyzer = MusicAnalyzer() |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
demo_file = "path/to/your/audio/file.mp3" |
|
|
|
|
|
results = analyzer.analyze_music(demo_file) |
|
|
|
|
|
print("\n=== MUSIC ANALYSIS SUMMARY ===") |
|
print(f"Tempo: {results['summary']['tempo']:.1f} BPM") |
|
print(f"Time Signature: {results['summary']['time_signature']}") |
|
print(f"Key: {results['summary']['key']} {results['summary']['mode']}") |
|
print(f"Primary Emotion: {results['summary']['primary_emotion']}") |
|
print(f"Primary Theme: {results['summary']['primary_theme']}") |
|
|
|
|
|
import json |
|
print("\n=== DETAILED ANALYSIS ===") |
|
print(json.dumps(results, indent=2)) |
|
|
|
|
|
|