It is an interactive music generation app powered by Python. Using data structures and algorithms, it creates unique melodies based on user inputs, such as mood, seed notes, and duration. The app visualizes the generation process, allowing users to understand and hear the steps behind creating a melody.
- Interactive UI using Streamlit.
- Generate melodies based on mood and user-defined parameters.
- Dynamic visualization of melody creation through animations.
- Export generated melodies as MIDI files.
- Educational: Illustrates how algorithms can be applied creatively.
-
Clone this repository: bash git clone https://github.com/VanishaParwal/MusicAlgorithm cd melodify
-
Install the required dependencies: bash pip install -r requirements.txt
-
Run the application: bash streamlit run music.py
- User Input:
- Select mood: Happy, Sad, Energetic, Calm.
- Define duration and optionally input a seed note.
- Algorithm Selection:
- Choose between graph traversal (e.g., DFS/BFS) or dynamic programming techniques.
- Melody Generation:
- Notes are generated step-by-step, visualized in real-time.
- Musical transitions are represented as weighted edges in a graph.
- Playback and Export:
- Users can listen to the generated melody or export it as a MIDI file.
Below is a simplified version of the app.py script:
python import streamlit as st import networkx as nx import random from music21 import stream, note, midi
st.title("Melodify: Algorithmic Music Composer") st.sidebar.header("User Input")
mood = st.sidebar.selectbox("Choose Mood", ["Happy", "Sad", "Energetic", "Calm"]) duration = st.sidebar.slider("Melody Duration (in seconds)", 5, 60, 15) seed_note = st.sidebar.text_input("Seed Note (Optional)", "C")
def create_note_graph(): G = nx.DiGraph() notes = ["C", "D", "E", "F", "G", "A", "B"] for i in notes: for j in notes: G.add_edge(i, j, weight=random.uniform(0.1, 1)) return G
note_graph = create_note_graph()
def generate_melody(graph, start_note, duration): melody = [] current_note = start_note for _ in range(duration): melody.append(current_note) neighbors = list(graph.neighbors(current_note)) current_note = random.choices(neighbors, weights=[graph[current_note][n]['weight'] for n in neighbors])[0] return melody
if st.button("Generate Melody"): melody = generate_melody(note_graph, seed_note, duration) st.write("Generated Melody:", melody)
# Create a MIDI File
midi_stream = stream.Stream()
for n in melody:
midi_stream.append(note.Note(n))
midi_file = midi.translate.streamToMidiFile(midi_stream)
midi_path = "melody.mid"
midi_file.open(midi_path, 'wb')
midi_file.write()
midi_file.close()
# Download Link
with open(midi_path, "rb") as file:
st.download_button(label="Download Melody as MIDI", data=file, file_name="melody.mid")
st.audio(midi_path, format="audio/midi")
st.write("### Note Transition Graph") st.graphviz_chart(nx.nx_agraph.to_agraph(note_graph))
Below is a conceptual representation of the UI:
- Top Section: Title and description.
- Sidebar: User inputs for mood, duration, and seed notes.
- Main Area:
- Visualization of the melody generation process.
- Playback and download options.
- Select your preferences in the sidebar.
- Click the "Generate Melody" button to visualize and listen to the melody.
- Export the generated melody as a MIDI file.
- Add support for more moods and scales.
- Integrate deep learning models for melody generation.
- Provide more advanced visualization options.
This project is licensed under the MIT License. Feel free to use, modify, and distribute it.
Enjoy composing with Music
