DEV Community

Cover image for AI That Sings: Creating a Text-to-Music Generator with Code 🎵
devresurrect
devresurrect

Posted on

AI That Sings: Creating a Text-to-Music Generator with Code 🎵

Artificial Intelligence (AI) isn’t just for chatbots and automation—it can also compose music! With advancements in AI, we can now generate melodies, harmonies, and even full songs from simple text inputs. In this post, we’ll explore how AI can turn text into music using Python and walk through a simple AI-powered music generator.


🎶 How Does AI Generate Music?

AI music generation involves several techniques:

  • Text-to-Music Mapping: AI converts text (e.g., song lyrics or descriptions) into musical patterns.
  • Neural Networks for Music: Models like Google Magenta and OpenAI Jukebox generate melodies based on patterns in training data.
  • MIDI and Audio Processing: AI composes music in MIDI format, which can then be converted into sound using synthesizers.

We’ll use Magenta, a TensorFlow-based library, to create AI-generated music from text.


🎼 Step 1: Install Dependencies

Before we start coding, install the required Python libraries:

pip install magenta tensorflow pretty_midi pygame
Enter fullscreen mode Exit fullscreen mode

🎹 Step 2: Convert Text to Music

Let’s create an AI-powered music generator that takes text input and transforms it into MIDI notes.

import magenta.music as mm  
import pretty_midi  

def text_to_music(text):  
    """Convert input text into a simple melody."""  
    notes = [ord(char) % 12 + 60 for char in text]  # Map text characters to MIDI notes  
    sequence = mm.NoteSequence()  

    start_time = 0  
    for note in notes:  
        sequence.notes.add(
            pitch=note, start_time=start_time,  
            end_time=start_time + 0.5, velocity=80  
        )  
        start_time += 0.5  

    mm.sequence_proto_to_midi_file(sequence, 'ai_music.mid')  
    print("MIDI file created: ai_music.mid")  

# Example usage  
text_to_music("AI makes music fun!")  
Enter fullscreen mode Exit fullscreen mode

This function maps each character in the text to a MIDI note, creating a melody based on the text input.


🎵 Step 3: Play Your AI-Generated Music

To listen to the generated music, you can use a MIDI player like Online MIDI Player or a DAW (Digital Audio Workstation).

Alternatively, play it in Python using MIDI playback:

import pygame  

def play_midi(file):  
    """Play a MIDI file using pygame."""  
    pygame.init()  
    pygame.mixer.init()  
    pygame.mixer.music.load(file)  
    pygame.mixer.music.play()  
    while pygame.mixer.music.get_busy():  
        continue  

# Play generated music  
play_midi("ai_music.mid")  
Enter fullscreen mode Exit fullscreen mode

🎶 Step 4: Make It More Advanced

You can enhance the AI music generator by:

✅ Adding chords and harmonies to enrich the melody.

✅ Using machine learning models like LSTM to generate structured compositions.

✅ Combining lyrics and music for a full AI-powered song!


🚀 Final Thoughts

AI-powered music is an exciting frontier where technology meets creativity. Whether you're experimenting with simple melodies or training deep learning models for full compositions, AI makes music generation more accessible than ever.

💡 What would you like your AI to compose? Let me know in the comments! 🎼

Top comments (0)