Advertisement
CrazedProgrammer

Audio API

Mar 28th, 2015
1,019
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.49 KB | None | 0 0
  1. -- Audio API version 1.0 by CrazedProgrammer
  2. -- You can find info and documentation on these pages:
  3. --
  4. -- You may use this in your ComputerCraft programs and modify it without asking.
  5. -- However, you may not publish this API under your name without asking me.
  6. -- If you have any suggestions, bug reports or questions then please send an email to:
  7. -- crazedprogrammer@gmail.com
  8.  
  9. local _volume = 1
  10. local _outputs = { }
  11. local _music = { }
  12. 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}
  13. local _const = math.log(2) / 12
  14.  
  15. function setOutput(command, output)
  16.   command = command or commands
  17.   output = output or #_outputs + 1
  18.   if command == commands then
  19.     _outputs[output] = {cmd = "", setCommand = function (command) cmd = command end, runCommand = function() commands.execAsync(cmd) end}
  20.   else
  21.     _outputs[output] = command
  22.   end
  23. end
  24.  
  25. function setVolume(vol)
  26.   _volume = vol
  27. end
  28.  
  29. function getVolume()
  30.   return _volume
  31. end
  32.  
  33. function playSound(sound, pitch, vol, output)
  34.   pitch = pitch or 1
  35.   vol = vol or 1
  36.   output = output or 1
  37.   if _outputs[output] then
  38.     _outputs[output].setCommand("/playsound "..sound.." @a ~ ~ ~ "..tostring(_volume * vol).." "..tostring(pitch))
  39.     _outputs[output].runCommand()
  40.   end
  41. end
  42.  
  43. function addMusic(m)
  44.   table.insert(_music, m)
  45. end
  46.  
  47. function removeMusic(m)
  48.   local index = 0
  49.   for k,v in pairs(_music) do
  50.     if v == m then
  51.       index = k
  52.     end
  53.   end
  54.   if index ~= 0 then
  55.     table.remove(_music, index)
  56.   end
  57. end
  58.  
  59. function loadMusic(file)
  60.   local f = fs.open(file, "r")
  61.   if not f then return end
  62.   local str = f.readAll()
  63.   f.close()
  64.   local length = tonumber(str:sub(1, 4), 16)
  65.   local m = {length = length, volume = 1, playing = false, offset = 1, channels = { }}
  66.   local channels = tonumber(str:sub(5, 6), 16)
  67.   local i = 7
  68.   for k=1,channels,1 do
  69.     local volumeId = tonumber(str:sub(i, i), 16)
  70.     i = i + 1
  71.     local output = tonumber(str:sub(i, i), 16)
  72.     i = i + 1
  73.     local len = tonumber(str:sub(i, i + 1), 16)
  74.     i = i + 2
  75.     local sound = str:sub(i, i + len - 1)
  76.     i = i + len
  77.     local channel = {sound = sound, volumeId = volumeId, output = output, notes = { }}
  78.     table.insert(m.channels, channel)
  79.     for j=1,length,1 do
  80.       if str:sub(i, i) == " " then
  81.         i = i + 1
  82.       else
  83.         local pitch = tonumber(str:sub(i, i + 1), 16)
  84.         i = i + 2
  85.         local vol = tonumber(str:sub(i, i), 16)
  86.         channel.notes[j * 2 - 1] = pitch
  87.         channel.notes[j * 2] = vol
  88.         i = i + 1
  89.       end
  90.     end
  91.   end
  92.   return m
  93. end
  94.  
  95. function update()
  96.   for k,v in pairs(_music) do
  97.     if v.playing then
  98.       for l,w in pairs(v.channels) do
  99.         if w.notes[v.offset * 2] then
  100.           local pitch = 0.5 * math.exp(_const * (w.notes[v.offset * 2 - 1] - 1))
  101.           local vol = w.notes[v.offset * 2] / 15
  102.           playSound(w.sound, pitch, v.volume * (w.volumeId / 15) * vol, w.output)
  103.         end
  104.       end
  105.       v.offset = v.offset + 1
  106.       if v.offset > v.length then
  107.         v.playing = false
  108.         v.offset = 1
  109.       end
  110.     end
  111.   end
  112. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement