Short answer: Yes — and it can evolve based on your taste.
This project demonstrates how Artificial Intelligence can generate music using a Genetic Algorithm (GA).
Instead of learning from large datasets, the system evolves melodies over generations, guided by human feedback.
Users listen to generated music, rate it, and the algorithm improves future melodies using principles inspired by natural selection.
- 🎵 AI-generated melodies using Genetic Algorithms
- 🧬 User-driven evolution (rate melodies from 0–5)
- 🎹 Real-time playback in the browser
- 🔁 Auto-play after generation
- 📤 Export melodies as MIDI files
- 🎨 Interactive Next.js UI
- ⚡ FastAPI backend
- 🎼 Musical constraints: key, scale, tempo, bars
Run the project locally and open:
- Frontend: http://localhost:3000
- Backend: http://127.0.0.1:8000
This project uses a Genetic Algorithm (GA) to generate short musical sequences that evolve over time based on user feedback.
-
Initialization
A population of random musical candidates is generated.
Each candidate is a sequence of notes constrained by:- Key
- Scale
- Tempo
- Bars and notes per bar
-
Playback & Evaluation
Generated melodies are played in the browser using Tone.js. -
User Rating (Fitness Function)
The user rates each melody (0–5).
Higher-rated melodies are treated as fitter individuals. -
Selection, Crossover & Mutation
- High-fitness melodies are selected
- Notes are recombined (crossover)
- Random mutations introduce variation
-
Evolution
The process repeats, gradually producing more musically pleasing results.🎶
- Python
- FastAPI
- Genetic Algorithms
- music21 / MIDI processing
- Next.js (App Router)
- TypeScript
- Tone.js
- @tonejs/midi
- Modern responsive UI
Music-Generator-using-Genetic-Algorithm/
│
├── algorithms/ # Genetic algorithm logic
├── backend/ # FastAPI server
│ ├── main.py # API entry point
│ └── requirements.txt
│
├── frontend/ # Next.js frontend
│ ├── app/
│ │ ├── layout.tsx
│ │ └── page.tsx
│ ├── public/
│ ├── package.json
│ └── README.md
│
├── mgen.py # Music generation logic
├── cleanup_older_pyo_versions.py
├── LICENSE
└── README.md- Python 3.9+
- Node.js 18+
- npm or yarn
cd backend
python -m venv venv
source venv/bin/activate # macOS/Linux
# venv\Scripts\activate # Windows
pip install -r requirements.txt
uvicorn main:app --reload --port 8000cd frontend
npm install
npm run dev- Multi-instrument support
- Rhythm-aware fitness scoring
- Model-based fitness prediction
- Save & reload evolution sessions
- Cloud deployment (Vercel + FastAPI)
This project is licensed under the MIT License.
- Tone.js
- music21
- Genetic Algorithm research literature