Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Audio API version 1.0 by CrazedProgrammer
- -- You can find info and documentation on these pages:
- --
- -- You may use this in your ComputerCraft programs and modify it without asking.
- -- However, you may not publish this API under your name without asking me.
- -- If you have any suggestions, bug reports or questions then please send an email to:
- -- crazedprogrammer@gmail.com
- local _volume = 1
- local _outputs = { }
- local _music = { }
- pitch = {fs3=0.5,g3=0.529731547179648,gs3=0.561231024154687,a3=0.594603557501361,as3=0.629960524947437,b3=0.667419927085017,c4=0.707106781186547,cs4=0.749153538438341,d4=0.7937005259841,ds4=0.840896415253715,e4=0.890898718140339,f4=0.943874312681693,fs4=1,g4=1.0594630943593,gs4=1.12246204830937,a4=1.18920711500272,as4=1.25992104989487,b4=1.33483985417003,c5=1.41421356237309,cs5=1.49830707687668,d5=1.5874010519682,ds5=1.68179283050743,e5=1.78179743628068,f5=1.88774862536339,fs5=2}
- local _const = math.log(2) / 12
- function setOutput(command, output)
- command = command or commands
- output = output or #_outputs + 1
- if command == commands then
- _outputs[output] = {cmd = "", setCommand = function (command) cmd = command end, runCommand = function() commands.execAsync(cmd) end}
- else
- _outputs[output] = command
- end
- end
- function setVolume(vol)
- _volume = vol
- end
- function getVolume()
- return _volume
- end
- function playSound(sound, pitch, vol, output)
- pitch = pitch or 1
- vol = vol or 1
- output = output or 1
- if _outputs[output] then
- _outputs[output].setCommand("/playsound "..sound.." @a ~ ~ ~ "..tostring(_volume * vol).." "..tostring(pitch))
- _outputs[output].runCommand()
- end
- end
- function addMusic(m)
- table.insert(_music, m)
- end
- function removeMusic(m)
- local index = 0
- for k,v in pairs(_music) do
- if v == m then
- index = k
- end
- end
- if index ~= 0 then
- table.remove(_music, index)
- end
- end
- function loadMusic(file)
- local f = fs.open(file, "r")
- if not f then return end
- local str = f.readAll()
- f.close()
- local length = tonumber(str:sub(1, 4), 16)
- local m = {length = length, volume = 1, playing = false, offset = 1, channels = { }}
- local channels = tonumber(str:sub(5, 6), 16)
- local i = 7
- for k=1,channels,1 do
- local volumeId = tonumber(str:sub(i, i), 16)
- i = i + 1
- local output = tonumber(str:sub(i, i), 16)
- i = i + 1
- local len = tonumber(str:sub(i, i + 1), 16)
- i = i + 2
- local sound = str:sub(i, i + len - 1)
- i = i + len
- local channel = {sound = sound, volumeId = volumeId, output = output, notes = { }}
- table.insert(m.channels, channel)
- for j=1,length,1 do
- if str:sub(i, i) == " " then
- i = i + 1
- else
- local pitch = tonumber(str:sub(i, i + 1), 16)
- i = i + 2
- local vol = tonumber(str:sub(i, i), 16)
- channel.notes[j * 2 - 1] = pitch
- channel.notes[j * 2] = vol
- i = i + 1
- end
- end
- end
- return m
- end
- function update()
- for k,v in pairs(_music) do
- if v.playing then
- for l,w in pairs(v.channels) do
- if w.notes[v.offset * 2] then
- local pitch = 0.5 * math.exp(_const * (w.notes[v.offset * 2 - 1] - 1))
- local vol = w.notes[v.offset * 2] / 15
- playSound(w.sound, pitch, v.volume * (w.volumeId / 15) * vol, w.output)
- end
- end
- v.offset = v.offset + 1
- if v.offset > v.length then
- v.playing = false
- v.offset = 1
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement