I'd love to know/see some example code from the lowest level. By this I mean the code that for example, sets the voltage to the speakers, or something equivalent. I can't imagine how this would look/work.
How could a piece of code possibly set/change a physical quantity? I'm not looking for some driver code, but the actual code that makes hardware 'work', or does it work in a different way?
Math, Luck and Magic
Read up on Computer Organization and Computer Architecture and also Hardware Description Languages.