How can "purely" electrical circuits emit sound?
What you are really asking is how can electrical circuits cause small motions. After all, sound is motion of the air.
The answer is that there are various ways electric fields or electric currents can cause forces or motions. These effects are harnessed in the design of various transducers, which exist to deliberately cause or sense small motions. However, the laws of physics that allow these transducers to function don't stop outside the transducer case. They exist everywhere, so many things are unintended transducers. The difference is that usually the effect is rather weak without it being deliberately designed for as in a transducer.
Some of these effects are:
- Electrostatic force. Two objects at a different voltage will have a force between them. The force is proportional to the voltage and inversely proportional to distance. This is the same force that allows a balloon to stick to your hair after rubbing it against a cat or something. For ordinary circuits, this force is very weak, and conductors are held in place much more strongly than it. Still, you can sometimes get audible sound from this with high voltage circuits.
- Electrodynamic force. A moving charge creates a circular magnetic field around it. The magnetic field is proportional to the current, and can be made quite strong by looping the wire into a coil. This magnetic field can be made to move things, and is the basis for how solenoids, motors, and loudspeakers work.
Moving charges likewise experience a force if flowing thru a magnetic field of the right orientation. Most loudspeakers actually work on this principle; they are made so that a strong permanent magnet is fixed and the coil moves, which in turn moves the center of the speaker cone. The same thing happens in any inductor. Each piece of wire with current thru it experiences some force due to the overall magnetic field. Some of the buzzing you hear from transformers is individual pieces of wire moving a little bit as a result.
- Piezoelectric effect. Some materials, like quartz for example, will change their size or shape slightly as a function of applied electric field. Some small earphones work on this principle. There are also "crystal" microphones that work on this principle in reverse, meaning applying force to the crystal causes it to create a voltage. Common barbecue grill ignitors work on this principle by whacking a quartz crystal hard and suddenly enough to create a high enough voltage to cause a spark.
Some capacitor materials exhibit enough of this effect that when rigidly mounted on a circuit board can cause audible sound. I had to respin a board once and replace a ceramic cap with a electrolytic just because the ceramic was causing annoying audible whine.
- Magnetostrictive effect. This is the magnetic analog of the piezoelectric effect. Some materials change shape or size depending on the applied magnetic field, and this effect works in reverse too. I have worked on magnetic sensors that exploited this effect.
Materials in transformers and inductors are chosen to not have this effect, but a small amount is there anyway. The core of a inductor actually changes size very slightly as the magnetic field changes. This can cause audible sound, especially if the inductor is mechanically coupled to something that presents a greater area to the air, like a circuit board.
An ideal inductor or transformer might be a purely electronic component, but a real inductor or transformer produces a (rapidly changing) magnetic field. It is a design aim of such a component to keep that magnetic field within the component (for instance inside the ferromagnetic core), but that won't be achieved for 100%. The 'leaking' magnetic field will cause things to move (vibrate), and these things will make the air around them move likewise. Presto: a (unwanted) electromagnetic speaker.
A similar effect can probably be had in high-voltage capacitors, where the conducting plates attract each other depending on the voltage. This corresponds to an electrostatic speaker :)
A third effect is (unwanted) piezoelectric effects in components. I am not sure if this is actually the case in an observable level.
It isn't expanding or contracting the material, that emits the sound in transformer or inductor-based circuits. However the parts are moving.
Transformers are subject to significant mechanical forces caused by the alternatine electromagnetc fields. That causes wires and laminations to move, and hence emit sound. DC-DC converters often have wound inductors, which also move for the same reason.