Sound is created by vibrating something very quickly, such as a string or a reed. This movement compresses the air around them, which then spreads out over time. These changes in air pressure can then be heard as sound.
Computers generally produce sound using a speaker, which is just a thin surface that can be wiggled up and down by controlling its position. Audio is stored for computers in a variety of ways, usually compressed, but at it's core the key to making sounds is to use the positions of the speaker over time.
Creating a program to make sounds directly is difficult, and how to do so is pretty situation specific (for example, love2d's sound module allows you to create and play raw sound data). On the other hand, most audio formats are more complicated than comfortable to work with directly. Introducing the canonical Wave file format, which is simple enough to be generated by a simple program in basically any programming language.
(to be continued...)
local function encode1(num) return string.char(math.floor(num%256)) end local function encode2(num) return string.char( math.floor(num%256), math.floor(num/256)%256 ) end local function encode4(num) return string.char( math.floor(num)%256, math.floor(num/256)%256, math.floor(num/256/256)%256, math.floor(num/256/256/256)%256 ) end function towav(sound, time) local rate = 16000 local data = {} for i = 1, time * rate do data[i] = encode2( sound(i/rate) * 127*256) end return "RIFF" .. encode4(36 + time*rate*2) .. "WAVE" .. "fmt " .. encode4(16) .. encode2(1) .. encode2(1) .. encode4(rate) .. encode4(rate*2) .. encode2(2) .. encode2(16) .. "data" .. encode4(time*rate*2) .. table.concat(data,"") end