Sound is waves, and waves can be numbers
How does AI make music? The short answer: an AI music generator learns patterns from huge amounts of existing music, then predicts new sound one small piece at a time. But a computer can't "hear" a song the way you do. Before any AI can learn from music, the music has to become numbers. So let's start there.
Sound is vibration. When a guitar string shakes, it pushes the air back and forth, and those pushes travel to your ear as a sound wave. How fast the wave wiggles is its frequency, measured in hertz (Hz), or wiggles per second. Fast wiggles sound high, like a whistle. Slow wiggles sound low, like a bass drum. Most people can hear from about 20 Hz up to about 20,000 Hz, and the top of that range shrinks as people get older.
A microphone turns those air pushes into an electrical signal. Then the computer measures that signal over and over, very quickly. Each measurement is one number, called a sample. The number of samples taken every second is the sample rate. It's like a flipbook: each page is one frozen moment, and when you flip fast enough, you see smooth motion.
CD-quality audio uses 44,100 samples every second. A three-minute song is about 8 million numbers for each speaker. There's a rule behind that choice: to capture a pitch, you need more than twice as many samples per second as that pitch's frequency. Twice 20,000 Hz is 40,000, so 44,100 covers everything people can hear.
Try it below. Drag the slider to lower the sample rate. The white dots are the samples the computer keeps. The bright line is the sound you get back when you play only those numbers. Press play to hear the difference. At low rates, high sounds get lost or turn into strange buzzing tones.
Two ways to store music: recordings and notes
There's a second, much smaller way to store music. Instead of recording the wave, you can write down which notes to play. That's what sheet music does, and computers have their own version called MIDI. MIDI has been a standard since the 1980s. It stores each note as a few numbers: which key, how hard it was pressed, and when it started and stopped. Middle C is note number 60.
Audio: a recording
Thousands of samples per second. It captures everything: voices, drums, the room's echo. Like a photo of the sound.
0.00, 0.71, 1.00, 0.71, 0.00, -0.71 ...MIDI: a list of notes
Just a few numbers per note. It doesn't hold any actual sound, only instructions. Like a recipe or sheet music.
note 60 on, note 64 on, note 67 on ...Some AI music tools work with notes, like MIDI, and write melodies or chords for a person or a virtual instrument to play. Others work directly with audio, so they can produce singing voices and full mixes. Either way, the music is now numbers, and numbers are something a computer can learn patterns from.
Learning to predict the next note
Here's the heart of most AI music generators: given the music so far, guess what comes next. Then add that guess to the music and guess again. Repeat thousands of times, and you have a song.
If that sounds familiar, it should. Chatbots work the same way with words. They look at the text so far and predict the next small piece of text, called a token. (Our tutorial on how chatbots read tokens explains this in detail.) A music model does the same thing, but its tokens are notes or tiny chunks of sound.
How does a model get good at guessing? Through training. A model is a computer program with millions or billions of adjustable settings. During training, it sees enormous amounts of music, called training data. Again and again, it hides the next bit, makes a guess, checks the answer, and nudges its settings to guess a little better next time. Nobody types in the rules of music. The model soaks up patterns on its own: that a chorus repeats, that drums often hit on certain beats, that some chords love to follow other chords.
Build a tiny next-note predictor
Real AI music models are huge and complex, but you can build a baby version right now. Tap a melody on the keyboard below (or pick a song you know). The program counts which note followed which. That table of counts is the whole "model." This kind of model is called a Markov chain: it chooses the next note based only on the current note. Then press Generate and the model writes a brand-new melody in the style of yours.
Play with the surprise level. At zero, the model always picks its single most likely next note, so it tends to loop the same few notes. In the middle, it rolls weighted dice using your counts. Turn it all the way up and it takes wild chances, even jumps it never heard. Real AI tools have a similar knob, often called temperature. Too low sounds boring. Too high sounds random.
Notice the big limit, too. Your Markov chain only remembers one note back, so it has no idea where it is in the song. Modern AI models look back over a long stretch of music at once, so they can repeat a chorus, build up to a drop, and end on a strong note. That longer memory is a big part of why their songs sound so much more complete.
From a text prompt to a song
Most popular AI music generators work from a prompt: a short description you type, like "chill lo-fi beat with rain sounds" or "energetic rock song about a robot." Here's what happens next, in simplified form.
- Read the prompt The words are turned into numbers that capture their meaning: lo-fi, rain, chill.
- Predict sound tokens The model predicts audio tokens one after another, each guess shaped by the prompt and by everything it has made so far.
- Decode to sound A decoder turns the tokens back into tens of thousands of samples per second: a real waveform.
- Play the song You hear the result. Ask again with the same prompt and you'll usually get a different song, because randomness is part of every step.
What is an audio token?
Remember that one second of CD-quality audio is 44,100 numbers. Predicting every single sample, one at a time, would be painfully slow. So many systems first use an audio codec, a program that squeezes a tiny slice of sound into a short code, like a nickname for "this little bit of sound." Those codes are the audio tokens. They're far fewer than raw samples, but they still hold enough detail to rebuild the sound. Research models such as Google's MusicLM and Meta's MusicGen, both shared in 2023, used this token approach.
Not every generator predicts tokens in a line. Some use diffusion: they start with pure noise, like TV static, and clean it up step by step until music appears. That's the same basic trick many image generators use. If you've read how AI image generators work, you already know the idea. Many tools mix several methods, and companies don't always share exactly how their systems work.
Singing is extra tricky. To sing lyrics, a model has to line up words with the melody and the beat, and make the voice sound natural. That's why some tools write the lyrics with a language model first, then generate the vocals to match. All of this is a kind of generative AI: AI that creates new content instead of just sorting or labeling it.
Can AI write songs? What it's good and bad at
Yes, AI can write songs, and some of them sound surprisingly professional. But "sounds polished" isn't the same as "means something." Here's an honest scorecard.
Often good at
- Speed: a full track in seconds or minutes.
- Copying a style: it can sound like "80s synth-pop" or "film score" because it saw lots of examples.
- Background music: short loops for videos and games.
- Brainstorming: dozens of beat or chord ideas to pick from.
- Cleanup jobs: removing noise or separating vocals from instruments.
Often bad at
- Real meaning: lyrics can be generic or not make sense.
- Long-term structure: songs can wander or end awkwardly.
- Truly new styles: it remixes patterns it learned. Inventing a genre is much harder.
- Exact control: "make just the second chorus quieter" is hard to get right.
- Glitches: mushy words, warbly voices, or odd sounds can slip in.
The deeper point: AI doesn't feel anything. When a human writes a song about losing a friend, the song carries a real experience. An AI can only produce something that statistically resembles songs about loss. Whether that matters to you as a listener is one of the interesting questions in the next section.
Creativity, consent, and AI music copyright
AI music raises questions that thoughtful people answer differently. Courts, lawmakers, artists, and tech companies are still arguing about many of them, so be wary of anyone who says the answers are simple.
Training data: learning or copying?
AI music models learn from huge collections of songs. Many of those songs are protected by copyright, the law that gives creators control over copying and sharing their work. In 2024, major record labels sued some AI music companies, arguing that their recordings were used for training without permission or payment. The AI companies argued that learning patterns from music is allowed. Questions like this are still being settled through court cases, licensing deals, and new laws, and the answers may differ from country to country. (Our training data tutorial covers where this data comes from.)
Some people argue
- Humans learn by listening to other music, so AI should be allowed to, too.
- The AI makes new songs, not copies of old ones.
- Strict rules would slow down useful new tools.
Others argue
- Artists should be asked first and paid when their work trains a product.
- AI songs could flood streaming services and compete with the very artists they learned from.
- A machine copying at huge scale is different from one person learning.
Who owns an AI song?
In the United States, the Copyright Office has said that copyright protects work made by humans. Music generated entirely by AI, with little human creative input, may not be protected at all. If a person makes real creative choices, like writing the lyrics or reworking the AI's output, those human parts can count. Other countries handle this differently, and each AI app's terms of service add their own rules. The lines are still being drawn. For the same question about pictures, see Who owns that picture?
Voice cloning and consent
A voice clone is an AI copy of a real person's voice. In 2023, a song that used AI to imitate the voices of Drake and The Weeknd went viral before being pulled from streaming services. Neither artist had agreed to it. Cases like this raise questions of consent: a person's voice is part of their identity, and using it without permission can fool fans and hurt the artist's reputation and income. Some places, such as the state of Tennessee, have passed laws to protect people's voices from unauthorized copies. Voice clones are also used in scams, which you can learn about in What is an AI voice clone scam? and Deepfakes and synthetic media.
Now try the sorter. For each situation, decide who really made the music. Then read the discussion question and talk it over with a classmate. There's no scoreboard here, because the interesting part is the reasons.
A tool, not a replacement?
Music has been shaken up by new tech before. Drum machines, synthesizers, sampling, and auto-tune were each called "not real music" at first. Today they're just instruments. Some musicians see AI the same way. Others point out that AI is different, because it can produce a whole finished song with no musician involved at all. That worries people who earn a living making music for ads, videos, and games.
For a student, the most useful question is: does this tool help me make my music, or does it make the music for me? Here are some ways to use AI as a helper:
- Practice partner: generate a backing track in your key so you can practice soloing.
- Idea starter: ask for a few drum patterns, then rewrite the one you like.
- Learning tool: ask why a chord progression sounds happy or sad, then test it on a real instrument.
- Be honest: if AI helped, say so, and follow your school's and contest's rules.
- Respect people: don't clone a real person's voice without clear permission.
Make music with code
The best way to understand AI music is to build some yourself. Here's the same Markov idea from the keyboard demo in a few lines of Python. The dictionary lists which notes can follow each note. A note that appears twice in a list is twice as likely to be picked. Every run gives a new melody, thanks to randomization in Python.
import random # Which notes followed each note in a melody we "trained" on follows = { "C": ["D", "E", "E", "G"], "D": ["E", "C"], "E": ["F", "D", "G"], "F": ["E", "G"], "G": ["C", "A", "E"], "A": ["G"], } note = "C" melody = [note] for i in range(11): note = random.choice(follows[note]) # predict the next note melody.append(note) print(" ".join(melody))
C E F G A G C D E G E D
From here you could add rhythms, learn the table from a real song instead of typing it, or look back two notes instead of one. Each upgrade moves your program a step closer to how real AI music generators think.
Key terms
- Sound wave
- Vibrations traveling through the air that your ears hear as sound.
- Frequency
- How many times a wave wiggles per second, measured in hertz (Hz). Higher frequency means higher pitch.
- Sample
- One measurement of a sound wave at one instant, stored as a number.
- Sample rate
- How many samples are taken each second. CD audio uses 44,100.
- MIDI
- A standard way to store music as note instructions (which key, how hard, how long) instead of recorded sound.
- Model
- A program with many adjustable settings that learns patterns from examples.
- Training data
- The examples a model learns from. For music AI, that's large collections of songs and sounds.
- Token
- A small piece of data a model reads or predicts, like a word piece, a note, or a short chunk of sound.
- Audio codec
- A program that squeezes sound into compact codes and can rebuild sound from those codes.
- Markov chain
- A simple model that picks what comes next using only what's happening right now, based on counted odds.
- Temperature
- A setting that controls how adventurous a model's choices are. Low is safe and repetitive. High is surprising.
- Diffusion
- A way of generating content by starting from random noise and cleaning it up step by step.
- Voice clone
- An AI-made copy of a real person's voice.
- Copyright
- Laws that give creators control over how their work is copied and shared.
Frequently asked questions
How does AI make music?
AI music generators learn patterns from huge amounts of existing music. Sound is first turned into numbers, or into short codes called tokens. Then the model predicts what should come next, one small chunk at a time, guided by your prompt, until it has built a whole song. A decoder turns those predictions back into sound you can hear.
Can AI write songs?
Yes. AI tools can produce melodies, beats, lyrics, and even full songs with singing from a short text prompt. The results can sound polished, but they are built from patterns in training data. AI often struggles with long-term structure, real meaning, and personal stories, which is where human songwriters still shine.
Who owns the copyright to AI-generated music?
This is still being worked out. In the United States, the Copyright Office has said copyright protects work made by humans, so music generated entirely by AI with little human creative input may not be protected. Songs where a person makes real creative choices are treated differently. Rules differ by country and keep changing, and each app's terms of service matter too.
Is it okay to use AI to clone a singer's voice?
Copying a real person's voice without their permission raises serious consent problems and can fool listeners. Many music platforms remove fake songs that pretend to be real artists, and some places have passed laws that protect a person's voice. Using your own voice, or a voice shared with clear permission, is a very different situation.
Will AI replace musicians?
People disagree. AI can already make background music cheaply, which worries some working musicians. Others see AI as a new instrument, the way drum machines and synthesizers once were. Many listeners still value the human story, live performance, and personal connection behind music, which AI cannot supply.
How can I make music with code?
Start with a few lines of Python or JavaScript. Store notes in a list, use randomness to pick the next note, and play them back. The same programming basics taught in CodeHS courses, such as lists, loops, dictionaries, and random choices, are enough to build a simple music generator.
Quick quiz
1. What does "sample rate" mean?
2. Old phone calls used about 8,000 samples per second. Why did voices sound muffled?
3. In your melody, C was followed by E three times and by G once. What chance does the Markov model give E after C (at normal surprise)?
4. What is an audio token?
5. A fan makes an AI song that sounds exactly like a famous singer and posts it as a "leaked track." What is the biggest concern?
Keep learning
Build the AI, don't just use it
Lists, loops, randomness, and data are the building blocks of every music generator. CodeHS courses teach programming and AI from the ground up, so you can make tools like this yourself.
Part of the AI Literacy Skills tutorial series.