-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzer.py
More file actions
263 lines (215 loc) · 9.44 KB
/
analyzer.py
File metadata and controls
263 lines (215 loc) · 9.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# analyzer.py
import librosa
import numpy as np
from transformers import pipeline
from datetime import datetime
def analyze_instruments(file_path: str, top_k: int = 3) -> list:
"""
Uses a pre-trained model to identify instruments in an audio file.
Returns a list of the top K most likely instruments with confidence scores.
"""
# Initialize the audio classification pipeline with a music-specific model
# This model is specifically trained for musical instrument classification
classifier = pipeline(
"audio-classification",
model="MIT/ast-finetuned-audioset-10-10-0.4593"
)
# Run the model on the audio file
predictions = classifier(file_path)
# Format the results: get top K instruments with confidence scores
top_instruments = []
for i in range(min(top_k, len(predictions))):
instrument = predictions[i]['label']
confidence = round(predictions[i]['score'], 4) # Round to 4 decimal places
top_instruments.append({
"instrument": instrument,
"confidence": confidence
})
return top_instruments
def analyze_rhythm_pattern(file_path: str) -> dict:
y, sr = librosa.load(file_path)
# Analyze rhythm complexity
onset_env = librosa.onset.onset_strength(y=y, sr=sr)
tempo, beats = librosa.beat.beat_track(onset_envelope=onset_env, sr=sr)
# Calculate rhythm stability
pulse = librosa.beat.plp(onset_envelope=onset_env, sr=sr)
rhythm_stability = np.std(pulse) # Lower = more stable rhythm
return {
"rhythm_complexity": "high" if rhythm_stability > 0.1 else "medium" if rhythm_stability > 0.05 else "low",
"beat_consistency": "very consistent" if len(beats) > 10 else "moderate",
"percussive_intensity": float(np.mean(onset_env))
}
def analyze_mood(file_path: str) -> dict:
y, sr = librosa.load(file_path)
tempo, _ = librosa.beat.beat_track(y=y, sr=sr)
tempo = float(tempo.item())
spectral_centroid = np.mean(librosa.feature.spectral_centroid(y=y, sr=sr))
# More nuanced mood detection
energy = "low"
if tempo > 130:
energy = "very high"
elif tempo > 110:
energy = "high"
elif tempo > 90:
energy = "medium"
else:
energy = "low"
valence = "positive" if spectral_centroid > 1500 else "negative"
# Combine energy and valence for mood description
mood_map = {
("high", "positive"): "energetic and uplifting",
("high", "negative"): "intense and dark",
("medium", "positive"): "cheerful and bright",
("medium", "negative"): "melancholic and reflective",
("low", "positive"): "calm and peaceful",
("low", "negative"): "sad and somber"
}
primary_mood = mood_map.get((energy, valence), "neutral")
return {
"primary_mood": primary_mood,
"energy_level": energy,
"emotional_valence": valence,
"brightness_score": float(spectral_centroid),
"tempo_bpm": tempo
}
def analyze_genre_simple(file_path: str) -> list:
y, sr = librosa.load(file_path)
tempo, _ = librosa.beat.beat_track(y=y, sr=sr)
tempo = float(tempo.item())
# Additional features for better genre detection
spectral_centroid = np.mean(librosa.feature.spectral_centroid(y=y, sr=sr))
zero_crossing = np.mean(librosa.feature.zero_crossing_rate(y))
# Detect percussive vs melodic content
harmonic, percussive = librosa.effects.hpss(y)
percussive_ratio = np.mean(percussive) / (np.mean(harmonic) + 1e-10)
genre_predictions = []
# High percussion content suggests different genres
if percussive_ratio > 0.8:
if tempo > 115:
genre_predictions.append({"genre": "Hip-Hop", "confidence": 0.8})
genre_predictions.append({"genre": "Electronic", "confidence": 0.7})
else:
genre_predictions.append({"genre": "R&B", "confidence": 0.75})
genre_predictions.append({"genre": "Funk", "confidence": 0.65})
# Jazz/Blues for medium tempo, lower percussion
elif 70 <= tempo <= 120 and percussive_ratio < 0.6:
genre_predictions.append({"genre": "Jazz", "confidence": 0.75})
genre_predictions.append({"genre": "Blues", "confidence": 0.65})
# Rock for brighter, faster tempo
elif tempo > 110 and spectral_centroid > 1800:
genre_predictions.append({"genre": "Rock", "confidence": 0.8})
genre_predictions.append({"genre": "Pop", "confidence": 0.6})
else:
genre_predictions.append({"genre": "Pop", "confidence": 0.6})
genre_predictions.append({"genre": "Contemporary", "confidence": 0.55})
return genre_predictions[:3]
def analyze_structure(file_path: str) -> dict:
"""
Basic song structure analysis identifying sections.
"""
y, sr = librosa.load(file_path)
# Onset detection for section changes
onset_env = librosa.onset.onset_strength(y=y, sr=sr)
# Find tempo and beats
tempo, beats = librosa.beat.beat_track(onset_envelope=onset_env, sr=sr)
# Simple structure analysis
total_beats = len(beats) if beats is not None else 0
# Estimate sections based on beat patterns
estimated_sections = max(3, min(6, total_beats // 32)) # Rough estimate
return {
"estimated_sections": estimated_sections,
"total_beats": total_beats,
"section_length_estimate": f"{total_beats // estimated_sections} beats per section",
"analysis_note": "Basic structure detection - advanced algorithms would provide verse/chorus/bridge identification"
}
def detect_ai_generation(file_path: str) -> dict:
"""
Heuristic approach to detect AI-generated music.
Note: This is a simplified approach - real detection requires specialized models.
"""
y, sr = librosa.load(file_path)
# Features that might indicate AI generation
# 1. Unusual spectral characteristics
spectral_flatness = np.mean(librosa.feature.spectral_flatness(y=y))
# 2. Rhythm patterns
tempo, beats = librosa.beat.beat_track(y=y, sr=sr)
tempo = float(tempo.item()) if hasattr(tempo, 'item') else float(tempo)
# 3. Dynamic range
rms_energy = librosa.feature.rms(y=y)
dynamic_range = np.max(rms_energy) - np.min(rms_energy)
# Heuristic rules
ai_probability = 0.3 # Base probability
# Adjust based on features
if spectral_flatness < 0.1: # Unusually flat spectrum
ai_probability += 0.2
if abs(tempo - 120) < 5: # Very common tempo in AI music
ai_probability += 0.1
if dynamic_range < 0.05: # Compressed dynamics
ai_probability += 0.15
ai_probability = min(0.95, max(0.05, ai_probability)) # Clamp between 5-95%
return {
"ai_generation_probability": round(ai_probability, 2),
"human_generation_probability": round(1 - ai_probability, 2),
"confidence": "low - heuristic approach",
"features_analyzed": ["spectral_characteristics", "tempo_consistency", "dynamic_range"],
"disclaimer": "This is a heuristic approximation. Professional AI detection requires specialized models and datasets."
}
def analyze_audio(file_path: str) -> dict:
"""
Takes a path to an audio file and returns a dictionary
with its tempo, key, loudness, and instruments.
"""
# Load the audio file
y, sr = librosa.load(file_path)
# 1. Calculate Tempo (Beats Per Minute)
tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sr)
tempo_value = float(tempo.item())
# 2. Estimate Key (Tonality)
chromagram = librosa.feature.chroma_stft(y=y, sr=sr)
chroma_agg = np.mean(chromagram, axis=1)
notes = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
key_index = np.argmax(chroma_agg)
estimated_key = notes[key_index]
# 3. Calculate Loudness (Root Mean Square Energy)
rms_energy = librosa.feature.rms(y=y)
average_loudness = float(np.mean(rms_energy))
# 4. Run all analyses
instruments = analyze_instruments(file_path)
rhythm = analyze_rhythm_pattern(file_path)
genres = analyze_genre_simple(file_path)
mood = analyze_mood(file_path)
structure = analyze_structure(file_path)
ai_detection = detect_ai_generation(file_path)
# 5. Package comprehensive results
analysis_results = {
"basic_analysis": {
"tempo_bpm": tempo_value,
"key": estimated_key,
"average_loudness": average_loudness,
"duration_seconds": len(y) / sr
},
"ai_analysis": {
"instruments": instruments,
"genres": genres,
"mood": mood,
"song_structure": structure
},
"generation_analysis": ai_detection,
"metadata": {
"analysis_version": "1.0",
"features_available": ["tempo", "key", "loudness", "instruments", "genre", "mood", "structure", "ai_detection"],
"timestamp": datetime.now().isoformat()
}
}
return analysis_results
# Test the function directly if this file is run
if __name__ == "__main__":
# TEST: Replace with your actual audio file name
test_file = "test_audio.mp3" # Change this to your file name!
try:
results = analyze_audio(test_file)
print("✅ Analysis Successful!")
print("Results:", results)
except Exception as e:
print("❌ Error:", e)
print("Make sure you have an audio file named 'test_audio.mp3' in this folder.")