You are currently viewing MTT – Making Music with AI

MTT – Making Music with AI

Macro Tech Titan — Knoxville, TN 8/8/2025– We have all been hearing about AI this and AI that – but what can it actually do?  In this blog post we are going to show you how to actually make music in AI.  This will require you have a DAW such as Ableton or FL Studio.  Here is the How to video:

Creating Music with AI: Generating MIDI Files Using Grok Prompts and Downloading via Base64 or Windows PowerShell

In the era of artificial intelligence, composing music has become more accessible than ever. Tools like Grok, built by xAI, allow users to generate musical ideas through simple text prompts. By leveraging Grok’s capabilities, you can create MIDI (Musical Instrument Digital Interface) files, which represent music as digital data that can be played back on synthesizers, digital audio workstations (DAWs), or software instruments. MIDI files are lightweight and editable, making them perfect for experimentation.

This article walks you through the process of prompting Grok to generate MIDI files, encoding them in Base64 for easy sharing, and downloading them using Windows PowerShell. No advanced programming knowledge is required—just a bit of curiosity and access to Grok via platforms like x.com or the Grok app. We’ll use Python libraries like midiutil (available in Grok’s code execution environment) to create the MIDI data.

Why Use Grok for MIDI Generation?

Grok excels at understanding natural language prompts and can generate code or data based on your descriptions. For music, you can describe melodies, chords, rhythms, or even entire compositions, and Grok can translate that into MIDI format. This is ideal for beginners, hobbyists, or professionals seeking inspiration. Once generated, the MIDI can be imported into software like GarageBand, FL Studio, or Ableton Live for further refinement.

Step 1: Crafting Effective Prompts for Grok

The key to great results is a clear, detailed prompt. Describe the music you want: genre, tempo, instruments, melody structure, and duration. Grok will handle the technical side by generating Python code to create the MIDI file.

Example Prompts:

  • Simple Melody: “Generate a MIDI file for a cheerful C major scale melody at 120 BPM, using a piano instrument, lasting 10 seconds.”
  • Complex Composition: “Create a MIDI file for a jazz chord progression in A minor, with a walking bass line, piano chords, and a drum beat at 90 BPM for 30 seconds.”
  • Themed Music: “Make a MIDI file inspired by a starry night: slow ambient synth pads in E major, with subtle arpeggios, at 60 BPM.”

When you send the prompt to Grok, it might respond with Python code that uses libraries like midiutil to build the MIDI file. If needed, Grok can execute the code internally to produce the actual MIDI data.

Step 2: Generating the MIDI File with Grok

Grok uses a code execution environment with access to MIDI libraries. Here’s how the generation works under the hood:

  1. Prompt Grok: Submit your description.
  2. Grok Generates Code: It creates Python code to define notes, durations, tempos, and tracks.
  3. Execute and Output: Grok runs the code to produce binary MIDI data, which can then be encoded.

Sample Grok-Generated Code

Suppose you prompt: “Generate a simple MIDI melody in C major.”Grok might produce something like this (simplified for illustration):

python

from midiutil import MIDIFile

Create a MIDI file with one track

midi = MIDIFile(1)

track = 0

channel = 0

time = 0  # Start at beat 0

midi.addTempo(track, time, 120)  # Set tempo to 120 BPM

Add notes: C major scale

notes = [60, 62, 64, 65, 67, 69, 71, 72]  # MIDI note numbers for C4 to C5

for note in notes:

midi.addNote(track, channel, note, time, 1, 100)  # Note, time, duration (1 beat), velocity (100)

time += 1

Write to file (in memory for Base64)

with open(“melody.mid”, “wb”) as output_file:

midi.writeFile(output_file)

In a real interaction, Grok would execute this and provide the output.To get the MIDI as Base64, add to your prompt: “Encode the generated MIDI file as Base64 string.”Grok can then run code like:

python

import base64

After generating melody.mid…

with open(“melody.mid”, “rb”) as midi_file:

base64_midi = base64.b64encode(midi_file.read()).decode(‘utf-8’)

print(base64_midi)

This outputs a long string like: TVRoZAAAAAYAAQACAeBNVHJrAAAAGgD/LwAA… (truncated).Step 3: Encoding MIDI in Base64 for SharingBase64 encoding converts binary data (like MIDI files) into a text string that’s easy to copy-paste. This is useful because Grok’s responses are text-based, and direct file downloads aren’t always supported in chat interfaces.

  • Why Base64? It’s platform-agnostic and avoids corruption during transmission.
  • In Your Prompt: Always request Base64 output to get the encoded string directly.

Once you have the Base64 string from Grok, copy it to your clipboard.

Step 4: Downloading the MIDI File Using Windows PowerShell

Windows PowerShell is a built-in tool for automating tasks, including decoding Base64 and saving files. No installation needed—it’s available on Windows 10 and later.

Steps to Download:

  1. Open PowerShell: Search for “PowerShell” in the Start menu and run it.
  2. Decode and Save: Use this command, replacing <base64_string> with your copied string and <filename.mid> with your desired file name:

[System.Convert]::FromBase64String(“<base64_string>”) | Set-Content -Path “<filename.mid>” -Encoding Byte

Example:

[System.Convert]::FromBase64String(“TVRoZAAAAAYAAQACAeBNVHJrAAAAGgD/LwAA…”) | Set-Content -Path “my_melody.mid” -Encoding Byte

  1. Run the Command: Paste and press Enter. The file will save in your current directory (use cd to change folders if needed).
  2. Verify: Open the .mid file in a media player like Windows Media Player or a DAW. If it plays, success!

Tips for PowerShell:

  • If the string is very long, paste it into a text file first and read from there: $base64 = Get-Content “base64.txt”, then use $base64 in the command.
  • Handle errors: Ensure the Base64 string is complete and not wrapped (PowerShell handles line breaks fine).
  • Alternatives: If you’re not on Windows, use tools like base64 command in Linux/Mac terminals: echo “<base64_string>” | base64 -d > filename.mid.

Advanced Tips for Better Music Generation

  • Iterate on Prompts: If the first result isn’t perfect, refine it: “Make the melody faster and add harmony.”
  • Multi-Track MIDI: Prompt for drums, bass, and melody on separate tracks for richer compositions.
  • Combine with Other Tools: Import the MIDI into free software like LMMS or MuseScore for editing and rendering to audio (MP3/WAV).
  • Limitations: MIDI doesn’t include actual sounds—just instructions. You’ll need a synthesizer to hear it properly.
  • Ethical Considerations: Use generated music for personal projects or as inspiration; credit AI tools if sharing publicly.

Conclusion

Generating MIDI files with Grok is a fun, creative way to explore music composition without needing instruments or software upfront. By prompting effectively, encoding in Base64, and decoding via PowerShell, you can quickly turn ideas into playable files. Experiment with different genres and complexities—Grok’s flexibility makes it a powerful ally for musicians of all levels. Start prompting today and let your musical ideas come to life!

Example Base64-Encoded MIDI File

As an example, here’s a Base64-encoded MIDI file for a simple C major scale melody at 120 BPM:

TVRoZAAAAAYAAQACA8BNVHJrAAAACwD/UQMHoSAA/y8ATVRyawAAAEwAkDxkh0CAPGQAkD5kh0CAPmQAkEBkh0CAQGQAkEFkh0CAQWQAkENkh0CAQ2QAkEVkh0CARWQAkEdkh0CAR2QAkEhkh0CASGQA/y8A

Copy this string and use the PowerShell command from Step 4 to decode it into a playable example.mid file.

Get our AI Consulting package to integrate AI in your organization

Frequency Entanglement


Discover more from Macro Tech Titan Blog

Subscribe to get the latest posts sent to your email.