Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0d4316b

Browse files
Merge pull request avinashkranjan#2373 from andoriyaprashant/branch8
AI Based Music Composer Script Added
2 parents f45e149 + ee54751 commit 0d4316b

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

‎AI Based Music Composer/README.md‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# AI-based Music Composer
2+
3+
This project is an AI-based music composer that generates unique pieces of music based on user preferences or specific genres. The script uses the `magenta` library, which provides pre-trained models for music generation.
4+
5+
## Table of Contents
6+
- [Introduction](#introduction)
7+
- [Features](#features)
8+
- [Installation](#installation)
9+
- [Usage](#usage)
10+
- [User Input](#user-input)
11+
- [Chord Progressions](#chord-progressions)
12+
- [Drum Accompaniment](#drum-accompaniment)
13+
- [Generated Music](#generated-music)
14+
- [Contributing](#contributing)
15+
16+
## Introduction
17+
18+
Music is an integral part of human expression, and this project aims to create an AI-based music composer that can generate melodies in various genres. The generated music can be saved as MIDI files and can serve as a starting point for further music composition and creativity.
19+
20+
## Features
21+
22+
- Generates unique melodies based on pre-trained models.
23+
- Allows user input for preferred genre and tempo.
24+
- Adds chord progressions based on the chosen genre to create harmonic accompaniments.
25+
- Includes a simple drum pattern for rhythmic accompaniment.
26+
- Outputs generated music as MIDI files.
27+
28+
## Installation
29+
30+
1. Clone the repository to your local machine:
31+
32+
2. Install the required dependencies:
33+
34+
```bash
35+
pip install magenta
36+
```
37+
38+
## Usage
39+
To generate music pieces using the AI-based music composer, run the following command:
40+
41+
```bash
42+
python music_composer.py
43+
```
44+
The script will prompt you to enter your preferred genre and tempo (BPM) for music generation.
45+
46+
## User Input
47+
The script allows you to input your preferred genre, which can be one of the following:
48+
49+
- Classical
50+
- Jazz
51+
- Rock
52+
Additionally, you can specify the tempo (in BPM) for the generated music.
53+
54+
## Chord Progressions
55+
The chosen genre determines the chord progression for the generated music. Currently, the supported chord progressions are:
56+
57+
- Classical: ["C", "Am", "F", "G"]
58+
- Jazz: ["Cmaj7", "Dm7", "Em7", "A7"]
59+
- Rock: ["C", "G", "Am", "F"]
60+
You can modify or extend these chord progressions in the music_composer.py file.
61+
62+
## Drum Accompaniment
63+
The music pieces include a basic drum pattern for rhythmic accompaniment. The drum pattern consists of a kick drum (36) and hi-hat (42). You can adjust the drum pattern in the music_composer.py file to create different rhythms.
64+
65+
## Generated Music
66+
The generated music will be saved as MIDI files in the generated_music directory. Each music piece will have a unique file name, such as music_piece_1.mid, music_piece_2.mid, and so on.
67+
68+
## Contributing
69+
If you have any ideas, improvements, or bug fixes, feel free to open an issue or submit a pull request. We appreciate your contributions!
70+
71+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import os
2+
import magenta.music as mm
3+
from magenta.models.melody_rnn import melody_rnn_sequence_generator
4+
5+
# Set the directory to save the generated MIDI files
6+
output_dir = 'generated_music'
7+
os.makedirs(output_dir, exist_ok=True)
8+
9+
# Initialize the Melody RNN model
10+
model_name = 'attention_rnn'
11+
melody_rnn = melody_rnn_sequence_generator.MelodyRnnSequenceGenerator(model_name=model_name)
12+
13+
# Set the temperature for music generation (higher values lead to more randomness)
14+
temperature = 1.0
15+
16+
# Set the number of music pieces to generate
17+
num_music_pieces = 3
18+
19+
# Set the number of steps per music piece
20+
steps_per_music_piece = 128
21+
22+
# User input for preferred genre and tempo
23+
preferred_genre = input("Enter your preferred genre (e.g., classical, jazz, rock): ")
24+
preferred_tempo = int(input("Enter your preferred tempo (BPM): "))
25+
26+
# Chord progression for the chosen genre (you can add more genres and progressions)
27+
chord_progressions = {
28+
"classical": ["C", "Am", "F", "G"],
29+
"jazz": ["Cmaj7", "Dm7", "Em7", "A7"],
30+
"rock": ["C", "G", "Am", "F"],
31+
}
32+
33+
# Basic drum pattern for accompaniment
34+
drum_pattern = mm.DrumTrack(
35+
[36, 0, 42, 0, 36, 0, 42, 0], # Kick drum and Hi-hat pattern (adjust as needed)
36+
start_step=0,
37+
steps_per_bar=steps_per_music_piece // 4,
38+
steps_per_quarter=4,
39+
)
40+
41+
# Generate music pieces
42+
for i in range(num_music_pieces):
43+
# Generate a melody sequence
44+
melody_sequence = melody_rnn.generate(
45+
temperature=temperature,
46+
steps=steps_per_music_piece,
47+
primer_sequence=None
48+
)
49+
50+
# Add chords to the melody sequence based on the preferred genre
51+
chords = [chord_progressions.get(preferred_genre, ["C"])[i % len(chord_progressions.get(preferred_genre, ["C"]))] for i in range(steps_per_music_piece)]
52+
chord_sequence = mm.ChordSequence(chords)
53+
melody_with_chords_sequence = mm.sequences_lib.concatenate_sequences(melody_sequence, chord_sequence)
54+
55+
# Create a MIDI file from the melody with chords sequence and drum pattern
56+
music_sequence = mm.sequences_lib.concatenate_sequences(melody_with_chords_sequence, drum_pattern)
57+
music_sequence.tempos[0].qpm = preferred_tempo
58+
59+
midi_file = os.path.join(output_dir, f'music_piece_{i + 1}.mid')
60+
mm.sequence_proto_to_midi_file(music_sequence, midi_file)
61+
print(f'Music piece {i + 1} generated and saved as {midi_file}')
62+
63+
print('Music generation complete!')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
magenta

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /