Creating an AI to generate Sonic Pi code based on FNF music involves several steps, as you mentioned. This includes downloading music from a YouTube channel, converting it to MIDI, converting MIDI into Sonic Pi arrays, and then training a model to generate Sonic Pi code.
Again, I can't provide you with the complete solution, but I can guide you step by step on creating this AI in a Colab notebook.
- Install required libraries:
!pip install youtube-dl librosa pretty_midi midi2audio
- Download the songs using the youtube-dl library:
import youtube_dl
def download_songs(channel_url, output_format='%(title)s.%(ext)s'):
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'wav',
'preferredquality': '192',
}],
'outtmpl': output_format,
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([channel_url])
channel_url = 'https://www.youtube.com/c/CommunityGame/videos'
download_songs(channel_url)
- Convert the audio files to MIDI files:
Please note that converting an audio file to MIDI can be challenging because it involves recognizing pitches and rhythms. There are no perfect automatic methods for this conversion, but you can explore libraries like audio-to-midi or any other similar library.
- Convert MIDI files to Sonic Pi arrays:
import pretty_midi
def midi_to_sonic_pi_arrays(path):
midi_data = pretty_midi.PrettyMIDI(path)
arrays = {}
for instrument in midi_data.instruments:
arrays[instrument.name] = {
'pitch_array': [],
'velocity_array': [],
'duration_array': []
}
for note in instrument.notes:
arrays[instrument.name]['pitch_array'].append(note.pitch)
arrays[instrument.name]['velocity_array'].append(note.velocity)
arrays[instrument.name]['duration_array'].append(note.end - note.start)
return arrays
midi_path = 'path/to/your_midi_file.mid'
sonic_pi_arrays = midi_to_sonic_pi_arrays(midi_path)
- Train an AI model off Sonic Pi arrays:
To train an AI model off Sonic Pi arrays, you will need a text-based representation of these arrays. You could use a code generation approach to achieve this goal, for example:
- Train a language model like GPT-2 using the OpenAI's GPT-2 fine-tuning guide
- Use your text dataset where each entry is a Sonic Pi array converted to a textual format (use the output of the previous step)
- Generate new Sonic Pi code snippets after the fine-tuning process
- Generate functional Sonic Pi code:
After training the model, you can use it to generate new Sonic Pi code snippets that will sound like FNF songs. You can use the GPT-2 simple library to make your work on this step easier.
Remember that these steps are not meant to be a complete solution. You will need to adjust and improve the code samples according to your needs and state of the art methods like audio-to-midi conversion libraries.