Barrios AI

Artificial Intelligence for Musicians

  1. Introduction
  2. Emergent Harmonic Structures | Software 1.0
  3. Pythagoras and the Music of the Spheres
  4. Building the Modes from Ancient Theory
  5. Harmonic Ratios in Nature and Music
  6. Musical Mathematics
  7. Exploring Chance and Randomness
  8. Statistics of Sound
  9. Software 2.0 | Deep Learning: Teaching Computers to See
  10. Software 2.0 | Deep Learning: Teaching Computers to Hear
  11. Software 3.0 | Generative AI: Teaching Computers to Read & Write
  12. Software 3.0 | NotebookLM as RAG

Chapter 1: Emergent Harmonic Structures | Software 1.0

Code-Along Notebook

1.1 Harmonic Structures

Before the universe existed, everything was compressed into a single point known as a singularity. There was no space, no matter, and no sound. Then, in an instant, this point expanded rapidly in an event we call the Big Bang. Physicist Stephon Alexander describes this event in The Jazz of Physics as similar to the opening note of a jazz improvisation, one event triggering complex patterns.

These initial patterns influenced how galaxies and stars formed. Tiny quantum fluctuations during the early rapid expansion created variations that later became structures like galaxies. We can still detect echoes of these early patterns today as cosmic microwave background radiation.

Alexander also compares fundamental cosmic forces to musical instruments playing together. Gravity is like the bass line, holding everything together, while electromagnetic and nuclear forces shape and refine the structure of the universe, similar to rhythm and melody in music.

Inspired by these ideas, we'll learn the basics of math, programming, and artificial intelligence (AI). This foundation will allow you to understand how complex patterns, like music or AI, emerge from simple rules.

1.2 Variables and Basic Data Types

Variables store values. They can store numbers, text, or lists.

beat = 1
note = "C"

print(beat)
print(note)

1.3 Booleans – True or False

Boolean values represent truth: either True or False.
They come in handy for rule-based music, like asking “is this pitch in the scale?”

is_tonic = True
is_blue_note = False
print(is_tonic, is_blue_note)

1.4 Working with Lists

Lists store multiple items in order.

beats = [1, 2, 3, 4]

print(beats)
print(beats[0])

1.5 Tuples – Locked Sequences

A tuple is an ordered, immutable sequence. Use one when the data should never change, for example, an interval name and its ratio:

perfect_fifth = ("perfect fifth", "3:2")
print(perfect_fifth[0])   # 'perfect fifth'

1.6 Sets – Unique Collections

A set stores each element only once, perfect for comparing scales.

ionian = {"C", "D", "E", "F", "G", "A", "B"}
dorian = {"D", "E", "F", "G", "A", "B", "C"}

common_notes = ionian & dorian   # intersection
print(common_notes)

1.7 Basic Arithmetic and Order of Operations (PEMDAS)

Python follows the usual math rules: multiplication and division happen before addition and subtraction, and parentheses override that order, just like grouping the sections of a song.

# Your song: 4 verses + 2 choruses, each section is 8 bars long
total_bars = (4 + 2) * 8
print(total_bars)   # 48

Watch how the parentheses change everything:

4 + 2 * 8     # 20  → Python multiplies first (2 * 8), then adds 4
(4 + 2) * 8   # 48  → parentheses add the sections first, then multiply

Floor division // returns a whole number and drops the remainder. Since an octave is 12 half steps (semitones), it's a quick way to see how many full octaves a MIDI note covers:

midi_note = 60          # middle C
print(midi_note // 12)  # 5  (regular / would give 5.0)

1.8 Dictionaries: Storing Related Information

Dictionaries store key-value pairs.

note_frequencies = {
    "C": 261.63,
    "D": 293.66,
    "E": 329.63,
}

print(note_frequencies["C"])

1.9 Functions and Methods

Functions let you reuse code.

def double_value(x):
    return x * 2

result = double_value(5)
print(result)

1.10 Using input() for Interaction

input() lets your program get user responses.

favorite_note = input("What is your favorite musical note? ")
print("Your favorite note is:", favorite_note)

1.11 Simple Interactive Quiz

Combine functions and user input to create quizzes.

def music_quiz():
    answer = input("Which note has a frequency of approximately 261.63 Hz? ")
    if answer.lower() == "c":
        print("Correct!")
    else:
        print("The correct answer is C.")

music_quiz()

1.12 Final Challenge: Interactive Project

Use what you learned to create an interactive game or quiz. Ideas include:

  • Music quiz
  • Math challenge
  • Number guessing game

Example: Number Guessing Game

import random

def guessing_game():
    secret_number = random.randint(1, 10)
    guess = int(input("Guess a number between 1 and 10: "))

    if guess == secret_number:
        print("You guessed it!", secret_number)
    else:
        print("Try again! The number was:", secret_number)

guessing_game()

Completed Notebook

1.13 Why This Matters

Everything you just wrote is Software 1.0: explicit deterministic rules you wrote and can depend on not changing. You told Python exactly what a variable holds, when a Boolean is true, and how to count bars with PEMDAS. Nothing was hidden. You know why every line did what it did and more important how it will behave, every time.

Later in this book, AI starts writing rules you didn't define or even write. The only way to steer AI then is to know Software 1.0 clearly, because that will serve as your reference point for why Software 2.0 and 3.0 are non-deterministic.

Next, in Chapter 2, you'll put your Python skills to work while following Pythagoras and Kepler as they uncover the math hiding inside music and cosmology: the whole-number ratios that make two notes sound good together. The code stays simple. The ideas get super interesting.