DEV Community

Cover image for Crafting Songs with ChatGPT and Sonic Pi: A Creative Collaboration
Philip John Basile
Philip John Basile

Posted on • Updated on

Crafting Songs with ChatGPT and Sonic Pi: A Creative Collaboration

Music is a universal language that has the power to evoke emotions, communicate stories, and connect people. With advances in artificial intelligence, songwriting is no longer limited to humans alone. CHATGPT, a powerful language model developed by OpenAI, is revolutionizing the way we approach songwriting. When combined with Sonic Pi, an innovative live coding music synthesizer, the possibilities are endless. Here we'll explore how you can use CHATGPT and Sonic Pi to craft unique songs that resonate with your creative spirit.

Getting Started with CHATGPT:
CHATGPT is an AI model designed to understand and generate human-like text based on a given context or prompt. It's versatile enough to help you write anything from blog posts to song lyrics. To get started, you need to have access to the CHATGPT API. There are numerous platforms and tools that can help you interact with the model, such as OpenAI's website or other third-party applications.

Crafting Lyrics with CHATGPT:
Once you have access to CHATGPT, you can begin generating lyrics by providing it with a prompt or context. For example, if you want to write a love song, you can simply provide a prompt like "Write a love song chorus." CHATGPT will then generate several lines of lyrics that fit your request. You can continue the process to develop verses, bridges, and other sections of your song. Feel free to experiment with different themes, styles, and genres to create a diverse range of songs.

Discovering Sonic Pi:
Sonic Pi is an open-source programming environment that allows you to create music through code. Designed for both beginners and experienced musicians, Sonic Pi provides an accessible platform for composing, improvising, and performing music. To get started, download and install Sonic Pi from their official website (www.sonic-pi.net.

Composing Melodies & Harmonies with Sonic Pi:

Sonic Pi uses the Ruby programming language, making it easy to create music by writing simple lines of code. Start by exploring the built-in samples and synthesizers to create unique sounds. You can experiment with different musical elements such as pitch, duration, and amplitude to craft your melodies and harmonies.

For example, to create a simple melody, you can use the play function followed by a note value:

play 60
sleep 1
play 62
sleep 1
play 64
Enter fullscreen mode Exit fullscreen mode

This code snippet will play three notes (C4, D4, and E4) with a one-second gap between each note.

Adding Rhythms and Percussion:

Sonic Pi makes it easy to add rhythm and percussion to your songs. You can either use built-in drum samples or create your own percussive sounds using synthesizers. Create loops and use various timing functions to control the rhythm and pace of your song.

For example, to create a basic drum beat, you can use the following code:

loop do
  sample :drum_heavy_kick
  sleep 1
  sample :drum_snare_hard
  sleep 1
end
Enter fullscreen mode Exit fullscreen mode

Experiment and Refine:

The true beauty of using CHATGPT and Sonic Pi lies in the ability to experiment and refine your creations. Feel free to play around with different synthesizers, samples, and code structures to create the perfect song. As you become more comfortable with both tools, you can even incorporate live coding techniques to improvise and perform your songs in real-time.

Creating a whole song with lyrics and music using CHATGPT and Sonic Pi involves multiple steps. Here's a step-by-step guide to help you craft your masterpiece:

Step 1: Generate Lyrics with CHATGPT
First, use CHATGPT to create lyrics for your song. Provide a prompt with the theme or mood you want to convey. For example, let's create an uplifting song about resilience:

Prompt: "Write an uplifting verse and chorus about resilience."

Generated Lyrics:

Verse 1:

When the storm arrives, and the winds grow strong,
Remember, my friend, you're where you belong.
Through the darkest nights and the heaviest rain,
You'll find your strength and rise once again.
Enter fullscreen mode Exit fullscreen mode

Chorus:

Rise up, rise up, let your spirit soar,
Break the chains that hold you, let your heart roar.
No storm can keep you down, no wind can blow you away,
You are resilient, and you're here to stay.
Enter fullscreen mode Exit fullscreen mode

Step 2: Compose Music with Sonic Pi
Next, compose music for your song using Sonic Pi. We'll create a simple melody, harmony, and drum beat.

Melody:

define :melody do
  play_pattern_timed [60, 62, 64, 65], [0.5, 0.5, 0.5, 1]
  play_pattern_timed [67, 65, 64, 62], [0.5, 0.5, 0.5, 1]
end
Enter fullscreen mode Exit fullscreen mode

Harmony:

define :harmony do
  play_chord [48, 52, 55], release: 4
  sleep 4
  play_chord [50, 53, 57], release: 4
  sleep 4
end
Enter fullscreen mode Exit fullscreen mode

Drum Beat:

define :drum_beat do
  sample :drum_heavy_kick
  sleep 1
  sample :drum_snare_hard
  sleep 1
end
Enter fullscreen mode Exit fullscreen mode

Step 3: Structure the Song
Organize the different sections to create your song's structure. For example, let's arrange our song as: Verse 1, Chorus, Verse 1, Chorus.

# Song Structure
in_thread do
  loop do
    drum_beat
  end
end

in_thread do
  loop do
    harmony
  end
end

in_thread do
  loop do
    melody
  end
end

sleep 4

2.times do
  melody
  sleep 4
end
Enter fullscreen mode Exit fullscreen mode

This code snippet defines multiple threads using the in_thread function to allow the melody, harmony, and drum beat to play simultaneously. The loop function repeats each part indefinitely. The sleep function is used to create pauses between sections (verse1 and chorus).

To hear your creation, save the code above in Sonic Pi and press the "Run" button. Remember to adjust the melody, harmony, drum beat, and song structure according to your preferences. You can also create additional verses or sections to further develop your song.

Unfortunately, Sonic Pi does not have built-in text-to-speech or singing synthesis functionality. To create a song with singing, you would need to use external tools or software to generate vocals or use pre-recorded vocal samples.

One option is to use a Digital Audio Workstation (DAW) like Ableton Live, FL Studio, or Logic Pro to combine the Sonic Pi-generated music with vocals generated or recorded using another tool. You can use text-to-speech or singing synthesis software like Vocaloid to create vocal tracks, and then import those tracks into your DAW. From there, you can mix the vocals with the Sonic Pi-generated music to create your final song.

Incorporating AI-powered lyric generation with CHATGPT and the live coding capabilities of Sonic Pi opens up a world of creative possibilities for songwriters and musicians. Whether you're a seasoned composer or a beginner exploring the world of music, this unique collaboration between artificial intelligence and coding can help you craft one-of-a-kind songs that truly express your artistic vision. So, dive in, experiment, and let your creativity soar! Fly my little birds!! Fly!! Let me hear you sing!!

If you enjoy my technology-focused articles and insights and wish to support my work, feel free to visit my Ko-fi page at https://ko-fi.com/philipjohnbasile. Every coffee you buy me helps keep the tech wisdom flowing and allows me to continue sharing valuable content with our community. Your support is greatly appreciated!

Top comments (13)

Collapse
 
michaeltharrington profile image
Michael Tharrington

Wow, it's absolutely wild that you can use AI to write lyrics, compose the music, structure the song, and even play/sing the tune for ya when you're done. I'm gonna have to experiment with this later!

As you become more comfortable with both tools, you can even incorporate live coding techniques to improvise and perform your songs in real-time.

That's a seriously cool idea! I can imagine a DJ set being run like this.

Really appreciate you writing this post up and walking us through Sonic Pi. This has been super interesting!

Collapse
 
philipjohnbasile profile image
Philip John Basile

It's pretty cool Sam Aaron does live performances too!!

Live Sonic Pi Performance

Collapse
 
michaeltharrington profile image
Michael Tharrington

So dope!! Really cool tune, I feel like I'm floating through space... as a lover of funky synthesizer sounds, this is def tickling that itch.

The visualizer is really cool too. Not sure if that's built into SonicPi or not, but it fits well!

Collapse
 
highcenburg profile image
Vicente G. Reyes • Edited

This is super wild!!!! I tried this with both bing and chatGPT and chatGPT wins. Normally I'd use the one from bing but this time, I liked what chatGPT responded with and with how the answer was presented

Collapse
 
philipjohnbasile profile image
Philip John Basile

Try it with google Bard too. So far Bard has been terrible.

Collapse
 
highcenburg profile image
Vicente G. Reyes

Just joined the waitlist! Can't wait!! Thanks for the suggestion

Collapse
 
emmaly profile image
Emmaly

I asked GPT-4 via the Playground to help me make a Majestic Wakeup Routine and it gave me this: gist.github.com/emmaly/24947176d2c...

While I would say it's neat, it's not majestic. But I guess it would suitably wake someone up. Using the kalimba would probably be better for putting someone to sleep, though. 😜

use_bpm 60
use_synth :kalimba

define :calm_section do
  play_pattern_timed [:c4, :e4, :g4, :c5], [0.5, 0.5, 0.5, 1]
  play_pattern_timed [:c4, :e4, :g4, :b4], [0.5, 0.5, 0.5, 1]
  play_pattern_timed [:c4, :d4, :g4, :a4], [0.5, 0.5, 0.5, 1]
  play_pattern_timed [:c4, :d4, :g4, :g4], [0.5, 0.5, 0.5, 1]
end

define :majestic_section do
  play_pattern_timed [:c4, :e4, :g4, :c5], [0.25, 0.25, 0.25, 0.5]
  play_pattern_timed [:c4, :e4, :g4, :b4], [0.25, 0.25, 0.25, 0.5]
  play_pattern_timed [:c4, :d4, :g4, :a4], [0.25, 0.25, 0.25, 0.5]
  play_pattern_timed [:c4, :d4, :g4, :g4], [0.25, 0.25, 0.25, 0.5]
end

define :motivating_section do
  play_pattern_timed [:c4, :e4, :g4, :c5], [0.125, 0.125, 0.125, 0.25]
  play_pattern_timed [:c4, :e4, :g4, :b4], [0.125, 0.125, 0.125, 0.25]
  play_pattern_timed [:c4, :d4, :g4, :a4], [0.125, 0.125, 0.125, 0.25]
  play_pattern_timed [:c4, :d4, :g4, :g4], [0.125, 0.125, 0.125, 0.25]
end

# Wakeup Routine
live_loop :thing do
  2.times do
    calm_section
  end

  3.times do
    majestic_section
  end

  6.times do
    motivating_section
  end

  2.times do
    majestic_section
  end

  1.times do
    motivating_section
  end

  1.times do
    majestic_section
  end

  1.times do
    calm_section
  end

  1.times do
    majestic_section
  end

  1.times do
    motivating_section
  end

  1.times do
    majestic_section
  end
end
Enter fullscreen mode Exit fullscreen mode
Collapse
 
philipjohnbasile profile image
Philip John Basile

But you made music! That's what counts. I wouldn't call the music maker majestic either. It's def more chip tune'y.

Collapse
 
sebelga profile image
Sébastien Loix

The future is looking more sad every day with this AI and computers trying to replace art. The other day I went to see a concert of 4 musicians (actual musicians, you know, those who know the notes of a scale of Eb and its chords) and that was awesome.

Who knows, maybe in 50 years it won't be possible to feel that live energy coming from 4 human souls anymore.

Everything AI, everything the same, everything soulless.

Collapse
 
philipjohnbasile profile image
Philip John Basile

AI is created from known human creations. Nothing willl replace the soul of human design.

Collapse
 
valeriavg profile image
Valeria

Precisely what I needed! Thank you for this post! I’ve been planning on arranging my songs for a while now and couldn’t sit down and learn how to do so in Garage Band, but I completely forgot about Sonic Pi! Makes so much sense to incorporate my two passions together!

I’m not sure I’m entirely fine with using AI generated lyrics as is, but it can definitely be a source of great inspiration!

Just like this article :)

P.S. Do you have the final version of the song available somewhere? I’d love to listen!

Collapse
 
philipjohnbasile profile image
Philip John Basile

I dont but if you copy the code above into sonic pi you can listen to it live! Feel free to tinker with it. It's freeeeeee.

Collapse
 
alex_escalante profile image
Alex Escalante

Thank you for letting me know about Sonic Pi!