|
import librosa |
|
import numpy as np |
|
import pronouncing |
|
import re |
|
from functools import lru_cache |
|
import string |
|
from nltk.corpus import cmudict |
|
import nltk |
|
from scipy import signal |
|
|
|
try: |
|
nltk.data.find('corpora/cmudict') |
|
except LookupError: |
|
nltk.download('cmudict') |
|
|
|
class BeatAnalyzer: |
|
def __init__(self): |
|
|
|
|
|
self.stress_patterns = { |
|
|
|
"4/4": [1.0, 0.0, 0.5, 0.0], |
|
"3/4": [1.0, 0.0, 0.0], |
|
"6/8": [1.0, 0.0, 0.0, 0.5, 0.0, 0.0] |
|
} |
|
|
|
self.cmudict = None |
|
try: |
|
self.cmudict = cmudict.dict() |
|
except: |
|
pass |
|
|
|
|
|
self.genre_syllable_ratios = { |
|
|
|
'pop': (0.5, 1.0, 1.5), |
|
'rock': (0.5, 0.9, 1.3), |
|
'country': (0.6, 0.9, 1.2), |
|
'disco': (0.7, 1.0, 1.3), |
|
'metal': (0.6, 1.0, 1.3), |
|
|
|
|
|
'hiphop': (1.8, 2.5, 3.5), |
|
'rap': (2.0, 3.0, 4.0), |
|
'folk': (0.8, 1.0, 1.3), |
|
'jazz': (0.7, 1.0, 1.5), |
|
'reggae': (0.7, 1.0, 1.3), |
|
'soul': (0.8, 1.2, 1.6), |
|
'r&b': (1.0, 1.5, 2.0), |
|
'electronic': (0.7, 1.0, 1.5), |
|
'classical': (0.7, 1.0, 1.4), |
|
'blues': (0.6, 0.8, 1.2), |
|
'default': (0.6, 1.0, 1.3) |
|
} |
|
|
|
|
|
|
|
|
|
self.supported_genres = ['pop', 'rock', 'country', 'disco', 'metal'] |
|
|
|
|
|
|
|
self.common_time_signatures = { |
|
"4/4": {"beats_per_bar": 4, "beat_pattern": [1.0, 0.2, 0.5, 0.2], "weight": 0.55}, |
|
"3/4": {"beats_per_bar": 3, "beat_pattern": [1.0, 0.2, 0.3], "weight": 0.30}, |
|
"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]], |
|
"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] |
|
} |
|
|
|
@lru_cache(maxsize=128) |
|
def count_syllables(self, word): |
|
"""Count syllables in a word using CMU dictionary if available, otherwise use rule-based method.""" |
|
word = word.lower().strip() |
|
word = re.sub(r'[^a-z]', '', word) |
|
|
|
if not word: |
|
return 0 |
|
|
|
|
|
if self.cmudict and word in self.cmudict: |
|
return max([len(list(y for y in x if y[-1].isdigit())) for x in self.cmudict[word]]) |
|
|
|
|
|
|
|
vowels = "aeiouy" |
|
double_vowels = ['aa', 'ae', 'ai', 'ao', 'au', 'ay', 'ea', 'ee', 'ei', 'eo', 'eu', 'ey', 'ia', 'ie', 'ii', 'io', 'iu', 'oa', 'oe', 'oi', 'oo', 'ou', 'oy', 'ua', 'ue', 'ui', 'uo', 'uy'] |
|
prev_was_vowel = False |
|
count = 0 |
|
final_e = False |
|
|
|
if word.endswith('e') and not word.endswith('le'): |
|
final_e = True |
|
|
|
for i, char in enumerate(word): |
|
if char in vowels: |
|
|
|
if prev_was_vowel and i > 0 and (word[i-1:i+1] in double_vowels): |
|
prev_was_vowel = True |
|
continue |
|
|
|
if not prev_was_vowel: |
|
count += 1 |
|
prev_was_vowel = True |
|
else: |
|
prev_was_vowel = False |
|
|
|
|
|
if word.endswith('le') and len(word) > 2 and word[-3] not in vowels: |
|
count += 1 |
|
elif final_e: |
|
count = max(count-1, 1) |
|
elif word.endswith('y') and not prev_was_vowel: |
|
count += 1 |
|
|
|
|
|
return max(count, 1) |
|
|
|
def detect_time_signature(self, audio_path, sr=22050): |
|
""" |
|
Advanced multi-method approach to time signature detection |
|
|
|
Args: |
|
audio_path: Path to audio file |
|
sr: Sample rate |
|
|
|
Returns: |
|
dict with detected time signature and confidence |
|
""" |
|
|
|
y, sr = librosa.load(audio_path, sr=sr) |
|
|
|
|
|
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 == "6/8": |
|
|
|
for ratio in tempo_ratios: |
|
if abs(ratio - 4) < 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, |
|
"6/8": 0.7 |
|
} |
|
elif 70 <= tempo <= 120: |
|
|
|
scores = { |
|
"4/4": 0.7, |
|
"3/4": 0.6, |
|
"6/8": 0.3 |
|
} |
|
else: |
|
|
|
scores = { |
|
"4/4": 0.8, |
|
"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 analyze_beat_pattern(self, audio_path, sr=22050, time_signature="4/4", auto_detect=False): |
|
"""Analyze beat patterns and stresses in music using the provided time signature.""" |
|
|
|
if auto_detect: |
|
time_sig_result = self.detect_time_signature(audio_path, sr) |
|
time_signature = time_sig_result["time_signature"] |
|
|
|
|
|
y, sr = librosa.load(audio_path, sr=sr) |
|
|
|
|
|
tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sr) |
|
beat_times = librosa.frames_to_time(beat_frames, sr=sr) |
|
|
|
|
|
onset_env = librosa.onset.onset_strength(y=y, sr=sr) |
|
beat_strengths = onset_env[beat_frames] |
|
|
|
|
|
if len(beat_strengths) > 0 and np.max(beat_strengths) > np.min(beat_strengths): |
|
beat_strengths = (beat_strengths - np.min(beat_strengths)) / (np.max(beat_strengths) - np.min(beat_strengths)) |
|
|
|
|
|
if '/' in time_signature: |
|
num, denom = map(int, time_signature.split('/')) |
|
else: |
|
num, denom = 4, 4 |
|
|
|
|
|
bars = [] |
|
current_bar = [] |
|
|
|
for i, (time, strength) in enumerate(zip(beat_times, beat_strengths)): |
|
|
|
metrical_position = i % num |
|
|
|
|
|
if time_signature == "4/4": |
|
if metrical_position == 0: |
|
stress = "S" |
|
elif metrical_position == 2: |
|
stress = "M" |
|
else: |
|
stress = "W" |
|
elif time_signature == "3/4": |
|
if metrical_position == 0: |
|
stress = "S" |
|
else: |
|
stress = "W" |
|
elif time_signature == "6/8": |
|
if metrical_position == 0: |
|
stress = "S" |
|
elif metrical_position == 3: |
|
stress = "M" |
|
else: |
|
stress = "W" |
|
else: |
|
|
|
if metrical_position == 0: |
|
stress = "S" |
|
else: |
|
stress = "W" |
|
|
|
|
|
current_bar.append({ |
|
'time': time, |
|
'strength': strength, |
|
'stress': stress, |
|
'metrical_position': metrical_position |
|
}) |
|
|
|
|
|
if metrical_position == num - 1 or i == len(beat_times) - 1: |
|
if current_bar: |
|
bars.append(current_bar) |
|
current_bar = [] |
|
|
|
|
|
if current_bar: |
|
bars.append(current_bar) |
|
|
|
|
|
phrases = [] |
|
|
|
for i, bar in enumerate(bars): |
|
phrase_beats = bar |
|
|
|
if not phrase_beats: |
|
continue |
|
|
|
|
|
phrase = { |
|
'id': i, |
|
'num_beats': len(phrase_beats), |
|
'beats': phrase_beats, |
|
'stress_pattern': ''.join(beat['stress'] for beat in phrase_beats), |
|
'start_time': phrase_beats[0]['time'], |
|
'end_time': phrase_beats[-1]['time'] + (phrase_beats[-1]['time'] - phrase_beats[-2]['time'] if len(phrase_beats) > 1 else 0.5), |
|
} |
|
|
|
phrases.append(phrase) |
|
|
|
return { |
|
'tempo': tempo, |
|
'time_signature': time_signature, |
|
'num_beats': len(beat_times), |
|
'beat_times': beat_times.tolist(), |
|
'beat_strengths': beat_strengths.tolist(), |
|
'phrases': phrases |
|
} |
|
|
|
def create_lyric_template(self, beat_analysis): |
|
"""Create templates for lyrics based on beat phrases.""" |
|
templates = [] |
|
|
|
if not beat_analysis or 'phrases' not in beat_analysis: |
|
return templates |
|
|
|
phrases = beat_analysis['phrases'] |
|
|
|
for i, phrase in enumerate(phrases): |
|
duration = phrase['end_time'] - phrase['start_time'] |
|
|
|
template = { |
|
'id': phrase['id'], |
|
'start_time': phrase['start_time'], |
|
'end_time': phrase['end_time'], |
|
'duration': duration, |
|
'num_beats': phrase['num_beats'], |
|
'stress_pattern': phrase['stress_pattern'], |
|
'syllable_guide': self.generate_phrase_guide(phrase) |
|
} |
|
|
|
templates.append(template) |
|
|
|
return templates |
|
|
|
def generate_phrase_guide(self, template, words_per_beat=0.5): |
|
"""Generate a guide for each phrase to help the LLM.""" |
|
num_beats = template['num_beats'] |
|
stress_pattern = template['stress_pattern'] |
|
|
|
|
|
|
|
visual_pattern = "" |
|
for i, stress in enumerate(stress_pattern): |
|
if stress == "S": |
|
visual_pattern += "STRONG " |
|
elif stress == "M": |
|
visual_pattern += "medium " |
|
else: |
|
visual_pattern += "weak " |
|
|
|
|
|
est_words = max(1, int(num_beats * 0.3)) |
|
|
|
|
|
|
|
if stress_pattern == "SWMW": |
|
min_syllables = max(1, int(num_beats * 0.4)) |
|
max_syllables = min(6, int(num_beats * 1.2)) |
|
else: |
|
min_syllables = max(1, int(num_beats * 0.4)) |
|
max_syllables = min(6, int(num_beats * 1.1)) |
|
|
|
|
|
template['min_expected'] = min_syllables |
|
template['max_expected'] = max_syllables |
|
|
|
guide = f"~{est_words} words, ~{min_syllables}-{max_syllables} syllables | Pattern: {visual_pattern}" |
|
|
|
|
|
template['phrasing_guide'] = "ULTRA SHORT LINES. One thought per line. Use FRAGMENTS not sentences." |
|
|
|
return guide |
|
|
|
def check_syllable_stress_match(self, text, template, genre="pop"): |
|
"""Check if lyrics match the syllable and stress pattern with genre-specific flexibility.""" |
|
|
|
words = text.split() |
|
syllable_count = sum(self.count_syllables(word) for word in words) |
|
|
|
|
|
expected_count = template['num_beats'] |
|
|
|
|
|
genre_lower = genre.lower() |
|
if genre_lower in self.genre_syllable_ratios: |
|
min_ratio, typical_ratio, max_ratio = self.genre_syllable_ratios[genre_lower] |
|
else: |
|
min_ratio, typical_ratio, max_ratio = self.genre_syllable_ratios['default'] |
|
|
|
|
|
|
|
min_expected = max(1, int(expected_count * min_ratio)) |
|
max_expected = min(6, int(expected_count * max_ratio)) |
|
|
|
|
|
if template['stress_pattern'] == "SWMW": |
|
max_expected = min(max_expected, 6) |
|
|
|
|
|
template['min_expected'] = min_expected |
|
template['max_expected'] = max_expected |
|
|
|
|
|
within_range = min_expected <= syllable_count <= max_expected |
|
|
|
|
|
ideal_count = int(expected_count * typical_ratio) |
|
|
|
ideal_count = max(min_expected, min(max_expected, ideal_count)) |
|
|
|
|
|
|
|
close_to_ideal = abs(syllable_count - ideal_count) <= 1 |
|
|
|
closeness_to_ideal = 1.0 - min(abs(syllable_count - ideal_count) / (max_expected - min_expected + 1), 1.0) |
|
|
|
|
|
word_syllables = [] |
|
for word in words: |
|
count = self.count_syllables(word) |
|
word_syllables.append(count) |
|
|
|
|
|
stress_pattern = template['stress_pattern'] |
|
|
|
|
|
|
|
syllable_to_beat_mapping = self._map_syllables_to_beats(word_syllables, stress_pattern) |
|
|
|
|
|
stress_match_percentage = self._calculate_stress_match(words, word_syllables, syllable_to_beat_mapping, stress_pattern) |
|
|
|
|
|
stress_matches = stress_match_percentage >= 0.6 |
|
|
|
return { |
|
'syllable_count': syllable_count, |
|
'expected_count': expected_count, |
|
'min_expected': min_expected, |
|
'max_expected': max_expected, |
|
'within_range': within_range, |
|
'matches_beat_count': syllable_count == expected_count, |
|
'close_match': within_range, |
|
'stress_matches': stress_matches, |
|
'stress_match_percentage': stress_match_percentage, |
|
'closeness_to_ideal': closeness_to_ideal, |
|
'word_syllables': word_syllables, |
|
'ideal_syllable_count': ideal_count, |
|
'close_to_ideal': close_to_ideal |
|
} |
|
|
|
def _map_syllables_to_beats(self, word_syllables, stress_pattern): |
|
"""Map syllables to beats in a flexible way.""" |
|
total_syllables = sum(word_syllables) |
|
total_beats = len(stress_pattern) |
|
|
|
|
|
if total_syllables <= total_beats: |
|
|
|
mapping = [] |
|
syllable_index = 0 |
|
for beat_index in range(total_beats): |
|
if syllable_index < total_syllables: |
|
mapping.append((syllable_index, beat_index)) |
|
syllable_index += 1 |
|
return mapping |
|
else: |
|
|
|
mapping = [] |
|
syllables_per_beat = total_syllables / total_beats |
|
for beat_index in range(total_beats): |
|
start_syllable = int(beat_index * syllables_per_beat) |
|
end_syllable = int((beat_index + 1) * syllables_per_beat) |
|
for syllable_index in range(start_syllable, end_syllable): |
|
if syllable_index < total_syllables: |
|
mapping.append((syllable_index, beat_index)) |
|
return mapping |
|
|
|
def _calculate_stress_match(self, words, word_syllables, syllable_to_beat_mapping, stress_pattern): |
|
"""Calculate how well syllable stresses match beat stresses.""" |
|
|
|
|
|
|
|
|
|
syllable_stresses = [] |
|
for word, syllable_count in zip(words, word_syllables): |
|
|
|
for i in range(syllable_count): |
|
if i == 0: |
|
syllable_stresses.append(1) |
|
else: |
|
syllable_stresses.append(0) |
|
|
|
|
|
matches = 0 |
|
total_mapped = 0 |
|
|
|
for syllable_index, beat_index in syllable_to_beat_mapping: |
|
if syllable_index < len(syllable_stresses): |
|
syllable_stress = syllable_stresses[syllable_index] |
|
beat_stress = 1 if stress_pattern[beat_index] == 'S' else (0.5 if stress_pattern[beat_index] == 'M' else 0) |
|
|
|
|
|
|
|
|
|
|
|
if (syllable_stress == 1 and beat_stress > 0.5) or (syllable_stress == 0 and beat_stress < 0.5): |
|
matches += 1 |
|
elif syllable_stress == 1 and beat_stress == 0.5: |
|
matches += 0.7 |
|
|
|
total_mapped += 1 |
|
|
|
if total_mapped == 0: |
|
return 0 |
|
|
|
return matches / total_mapped |