Playing with ChatGPT and Sonic Pi

Had a play with ChatGPT and Sonic Pi recently. Some asnwers quite impressive others less so. Here is one sequence. (It even coped with my miss-types!

Question: Gnerate a melody part using minor_pentatonic scale and add a percussion rhythm part in Sonic Pi

Response: Sure! Here’s an example code that generates a melody part using the minor pentatonic scale and adds a percussion rhythm part in Sonic Pi:

Code Generated:

use_bpm 120

# Define the minor pentatonic scale
minor_pentatonic = scale(:A3, :minor_pentatonic)

# Define the percussion rhythm
rhythm = (ring 1, 0.5, 0.5, 1, 0.5, 0.5)

# Define the live loop for percussion
live_loop :percussion do
  rhythm.each do |duration|
    sample :bd_haus if duration == 1
    sample :sn_dub if duration == 0.5
    sleep duration
  end
end

# Define the live loop for melody
live_loop :melody do
  # Choose a random note from the minor pentatonic scale
  note = minor_pentatonic.choose
  
  # Play the note with a random duration
  duration = [0.25, 0.5].choose
  with_fx :reverb, mix: 0.5 do
    synth :pluck, note: note, sustain: duration, release: duration
  end
  
  # Sleep for a random amount of time
  sleep [0.25, 0.5].choose
end

Explanation:
This code defines the minor_pentatonic scale and the rhythm array for the percussion part. In the live_loop for percussion, the rhythm is played using the sample command with different drum samples, creating a simple but groovy rhythm.

In the live_loop for melody, a random note is chosen from the minor_pentatonic scale and played using the synth command with a plucked sound. The with_fx command is used to add some reverb to the sound, and the sleep command is used to create a rhythmic pattern for the melody.

You can customize the minor_pentatonic scale and the rhythm array to create your own unique melody and percussion rhythm. Additionally, you can experiment with different synth sounds and effects to create a more varied and interesting sound.

I thought this was not bad at all.

2 Likes

Looks like ChatGPT did a good job with your request. I have been using it to write pieces of code in Sonic Pi and Processing, with varying degrees of success. Since I only need it to help with the more complicated code, the hardest problem is phrasing the specifications so that the AI will give a correct solution. It never says it doesn’t understand what I want. It happily (bravely? blindly? mindlessly?) spits out code, and it’s up to me to run it and see if it is what I want. Or if it runs at all. It always has complete confidence that it’s doing the right thing. It sometimes takes many iterations to work through a task.

I once asked it how it knows if it’s writing correct code. Here’s what it said:

As an AI language model, I use a combination of techniques to ensure that the code I provide is correct and meets the requirements of the task. These techniques include:

  1. Syntax checking: I perform basic syntax checking to ensure that the code is valid and free of syntax errors.

  2. Logic checking: I check that the logic of the code makes sense and is consistent with the requirements of the task.

  3. Testing: I use various test cases to verify that the code produces the correct output for different input values and scenarios. I can also verify the output provided by users to check for correctness.

  4. Experience and knowledge: I draw on my experience and knowledge of programming languages and best practices to ensure that the code is written in an appropriate and efficient manner.

While I cannot actually run the code, I can provide guidance and feedback on whether the code is likely to work correctly based on the above techniques.
<

1 Like

Hi Ken
I agree entirely with your comments. chatGPT is good to ive you some ideas, but it can produce some questionable code too. IT’s often a matter of trying to refine your qeustion to get a reasonable result. But it is fun to play with!

1 Like

I don’t think those steps describe how it actually works, just how people think a bot would write code, so that’s how ChatGPT describes the process. Reason I’m fairly confident is that it doesn’t process its output recursively. It just goes for it.

Hi, I just gave the same question to Perplexity
https://www.perplexity.ai/
The first version was very mechanical, so I asked “Could you vary the code with some fx effects?”
I had to correct the code, because Perplexity used the reserved word “scale” as a variable name.
That’s what I got (I’m still not really convinced):

# Code generated with Perplexity
# Prompts:
# - Generate a melody part using minor_pentatonic scale and add a percussion rhythm part in Sonic Pi
# - Could you vary the code with some fx effects?
# SR 05.09.2024
# Sources:
# - https://in-thread.sonic-pi.net/t/playing-with-chatgpt-and-sonic-pi/7803/3
#
# File: ai_perplexity_minor_pentatonic.txt
use_bpm 120

# Define the minor pentatonic scale
my_scale = [:a3, :c4, :d4, :e4, :g4, :a4]

# Melody part with reverb and occasional distortion
live_loop :melody do
  use_synth :piano
  with_fx :reverb, room: 0.7 do
    with_fx :distortion, distort: 0.3, amp: 0.6 do
      4.times do
        play my_scale.choose, release: 0.3
        sleep 0.5
      end
      
      play my_scale.choose, release: 1
      sleep 1
    end
  end
end

# Bass part with wobble effect
live_loop :bass do
  use_synth :fm
  with_fx :wobble, phase: 0.375, wave: 0 do
    play :a2, release: 4, amp: 0.5
    sleep 4
  end
end

# Percussion rhythm part with slicer effect
live_loop :drums do
  with_fx :slicer, phase: 0.25, wave: 1, mix: 0.5 do
    sample :drum_heavy_kick
    sleep 1
    
    sample :drum_snare_hard
    sleep 1
    
    sample :drum_heavy_kick
    sleep 0.5
    sample :drum_heavy_kick
    sleep 0.5
    
    sample :drum_snare_hard
    sleep 1
  end
end

# Hi-hat pattern with echo effect
live_loop :hihat do
  with_fx :echo, phase: 0.125, decay: 2 do
    sample :drum_cymbal_closed, amp: 0.5
    sleep 0.25
  end
end

# Ambient pad with ring modulator
live_loop :pad do
  use_synth :hollow
  with_fx :ring_mod, freq: 30 do
    play chord(:a3, :minor), release: 8, amp: 0.3
    sleep 8
  end
end