I constructed a Raspberry Pi that composes music based mostly on the climate

Once each hour, the Raspberry Pi sitting on my desk checks the climate after which performs a track about it. It is a bit like a cuckoo clock, besides it tells me the climate as an alternative of the time.
The total factor runs regionally on the Pi itself. There aren’t any cloud AI companies, no subscriptions, and no streaming audio. The solely factor that requires an web connection is the climate examine itself, although you might substitute a DIY climate station constructed with an ESP32 as an alternative.
Creating a Pi that performs music
How it turns climate right into a temper
I began by organising the Pi’s entry to the climate. I opted for Open-Meteo as a result of it would not require a key or account, which makes it fairly easy to make use of. A plain textual content doc then maps climate to a temper. For instance: sunny is joyful, rain is sombre, snow is delicate, and storms are tense.
Each temper has a algorithm. Tense has a quicker tempo, “performs” completely different devices, and has a definite musical register. Those guidelines are what outline the overall vibe of every form of climate.
A neighborhood AI composes the melody
Actually, creating the music requires a small language mannequin (synthetic intelligence), which I’m operating regionally on my Pi by way of Ollama. That means there aren’t any prices related to API access. Basically, the mannequin receives the present temper (which is decided by climate) after which outputs a melody to a JSON file.
The AI additionally confirms that the output has the qualities it ought to have, so {that a} sunny day’s track is at all times joyous, and a hallucination would not by accident create a grim track for a sunny day.
Critically, the mannequin is run at a reasonably excessive temperature (0.9), which ensures that the output from an identical prompts will not be an identical. So, two sunny days in a row will not generate an identical songs, however they’ll use related devices, fall inside a selected tempo vary, and usually sound “upbeat.”
The AI writes notes; it would not generate sound
Generating sound on a Pi is just too demanding
There are two completely different ways in which AI can create music. The first is immediately synthesizing the audio; the second writes a MIDI file with the notes (like sheet music for a pc, mainly) that’s performed again by a program.
If you might have the computational assets out there, the primary method generates extra sensible, compelling music. However, I’m operating this on a Raspberry Pi 4—it would not precisely have a ton of additional processing energy out there.
So, I opted for the MIDI method, which might nonetheless produce respectable music. FluidSynth, a conventional software synthesizer, then turns the MIDI notes into sound utilizing sampled devices. Splitting composition from playback is essential for practicality. Note information is tiny, straightforward to generate, and simple to double-check earlier than a track is performed. If the AI produces a non-functional JSON or out-of-range notes, the code can reject or repair them.
MIDI information are additionally utterly moveable. I might save them to my PC or transfer them over to my digital piano if I wished.
Playing the track
At the highest of each hour, FluidSynth performs the generated track. After about sixty seconds, the audio fades out and the Pi goes into an idle state till the subsequent track is required.
The {hardware} defines the bounds
The largest limitation is the Pi itself. I’m utilizing a Pi 4 with 8GB of RAM; it could possibly deal with small fashions if you happen to do not want them to run in actual time, however the Pi struggles with bigger fashions.
However, since I solely want one track per hour, the Pi’s {hardware} limitations aren’t actually a big challenge.
Additionally, a small LLM is not Mozart or John Williams. The output songs shall be “tremendous,” however if you happen to’re anticipating one thing good, you will want a a lot smarter AI.
A Pi that sings in regards to the rain
By combining a climate API, a constrained native language mannequin, and a synthesizer, you may create a tool with one thing like a each day or hourly temper. This venture additionally reinforces the purpose that small native fashions might be genuinely helpful if you constrain them to a selected job.
If you do need one thing smarter, you might construct one thing that runs on the cloud after which switch the file to your Raspberry Pi for playback. If you might have a ChatGPT or Claude subscription, producing MIDI music in all probability will not use sufficient tokens to place a dent in your utilization quota. However, I wished one thing that composed music regionally.
You might create a setup that runs on a desktop-class GPU—the additional reminiscence and computational energy would go a great distance.
