Music-Analyzer is a Python-based machine learning project for analyzing audio files and predicting their genres. It supports both feature-based ML models (Sklearn) and image-based CNN models using mel-spectrograms. The project also includes a Streamlit Web App for easy demo and exploration.
This project is an upgrade and inspired by my first ML project: music_recommender
Check out a quick preview of the project:
Song
|
Mel-Spectrogram Preview
|
- Features
- Project Structure
- Installation
- Dataset Preparation
- Training Models
- Using the Streamlit App
- App Demo
- File Organization & Git
- Contributing
- License
- Extracts audio features (MFCCs, Chroma, etc.) for ML models.
- Generates mel-spectrogram images for CNN-based classification.
- Supports multiple model types:
- Sklearn model (
sklearn_model.pkl) for classical ML. - CNN model (
cnn_model.pth) for deep learning on spectrograms.
- Sklearn model (
- Streamlit web app for uploading audio files and predicting genres.
- Clean project structure for reproducibility and experimentation.
Music-Analyzer/
│
├─ data/
│ ├─ raw/ # Raw audio files (ignored by Git)
│ └─ processed/ # Processed features & spectrograms
│
├─ models/
│ ├─ sklearn\_model.pkl
│ └─ cnn\_model.pth
│
├─ src/
│ ├─ dataset\_builder.py # Build features dataset and save CSV
│ ├─ train\_sklearn.py # Train Sklearn ML model
│ ├─ train\_cnn.py # Train CNN on mel-spectrograms
│ ├─ feature\_extraction.py# Extract audio features & save spectrograms
│ └─ app.py # Streamlit demo app
│
├─ docs/
│ └─ images/ # Screenshots for README demo
│
├─ .gitignore
├─ requirements.txt
└─ README.md
- Clone the repository:
git clone https://github.com/Longman-max/Music-Analyzer.git
cd Music-Analyzer- Create a virtual environment (recommended):
python -m venv venv
source venv/bin/activate # Linux/Mac
venv\Scripts\activate # Windows- Install dependencies:
pip install -r requirements.txt- Additional libraries (if not included in
requirements.txt):
pip install librosa numpy pandas scikit-learn torch torchvision matplotlib pillow streamlit-
Place your raw audio files in
data/raw/.- If you have genre folders, you can keep them as subfolders.
- Example:
data/raw/rock/song1.wav,data/raw/jazz/song2.mp3
-
Build the dataset:
python src/dataset_builder.py- This extracts features for ML and generates mel-spectrograms for CNN.
- Features CSV is saved in
data/processed/features.csv. - Mel-spectrogram images are saved in
data/processed/melspecs/.
python src/train_sklearn.py- Trains a Logistic Regression classifier on extracted features.
- Saves the model, scaler, and label encoder in
models/sklearn_model.pkl.
python src/train_cnn.py- Trains a ResNet18 CNN on mel-spectrogram images.
- Saves model weights and class labels in
models/cnn_model.pth.
Run the app:
streamlit run src/app.pyFeatures:
- Upload a
.wavor.mp3file. - Choose between Sklearn or CNN models.
- View predicted genre and feature/spectrogram visualization.
Layout highlights:
- Clean, modern interface with sidebar model selection.
- Columns display predicted genre and spectrogram side by side for CNN.
- Feature vector expandable view for Sklearn predictions.
Here’s a quick walkthrough of the app in action:
Choose between Sklearn (feature-based) or CNN (spectrogram-based) from the sidebar.
Upload a .wav or .mp3 file to analyze.
The predicted genre is displayed along with the visualization:
- For Sklearn, you’ll see the extracted feature vector.
- For CNN, you’ll see the mel-spectrogram and predicted label.
- Ignore raw audio:
data/raw/is ignored via.gitignore. - Keep processed data:
data/processed/is tracked for reproducibility. - Ignore large models:
models/*.pth,models/*.pklare ignored to reduce repo size.
Remove already tracked raw files from Git:
git rm -r --cached data/raw/
git commit -m "Stop tracking raw audio files"- Fork the repo and create a feature branch.
- Ensure your code is clean and well-documented.
- Submit pull requests for review.
This project is licensed under the MIT License. See LICENSE for details.




