CrazedProgrammer

Audio API

Mar 28th, 2015
1,145
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.  
  8. local _volume = 1
  9. local _outputs = { }
  10. local _music = { }
  11. 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}
  12. local _const = math.log(2) / 12
  13.  
  14. function setOutput(command, output)
  15.   command = command or commands
  16.   output = output or #_outputs + 1
  17.   if command == commands then
  18.     _outputs[output] = {cmd = "", setCommand = function (command) cmd = command end, runCommand = function() commands.execAsync(cmd) end}
  19.   else
  20.     _outputs[output] = command
  21.   end
  22. end
  23.  
  24. function setVolume(vol)
  25.   _volume = vol
  26. end
  27.  
  28. function getVolume()
  29.   return _volume
  30. end
  31.  
  32. function playSound(sound, pitch, vol, output)
  33.   pitch = pitch or 1
  34.   vol = vol or 1
  35.   output = output or 1
  36.   if _outputs[output] then
  37.     _outputs[output].setCommand("/playsound "..sound.." @a ~ ~ ~ "..tostring(_volume * vol).." "..tostring(pitch))
  38.     _outputs[output].runCommand()
  39.   end
  40. end
  41.  
  42. function addMusic(m)
  43.   table.insert(_music, m)
  44. end
  45.  
  46. function removeMusic(m)
  47.   local index = 0
  48.   for k,v in pairs(_music) do
  49.     if v == m then
  50.       index = k
  51.     end
  52.   end
  53.   if index ~= 0 then
  54.     table.remove(_music, index)
  55.   end
  56. end
  57.  
  58. function loadMusic(file)
  59.   local f = fs.open(file, "r")
  60.   if not f then return end
  61.   local str = f.readAll()
  62.   f.close()
  63.   local length = tonumber(str:sub(1, 4), 16)
  64.   local m = {length = length, volume = 1, playing = false, offset = 1, channels = { }}
  65.   local channels = tonumber(str:sub(5, 6), 16)
  66.   local i = 7
  67.   for k=1,channels,1 do
  68.     local volumeId = tonumber(str:sub(i, i), 16)
  69.     i = i + 1
  70.     local output = tonumber(str:sub(i, i), 16)
  71.     i = i + 1
  72.     local len = tonumber(str:sub(i, i + 1), 16)
  73.     i = i + 2
  74.     local sound = str:sub(i, i + len - 1)
  75.     i = i + len
  76.     local channel = {sound = sound, volumeId = volumeId, output = output, notes = { }}
  77.     table.insert(m.channels, channel)
  78.     for j=1,length,1 do
  79.       if str:sub(i, i) == " " then
  80.         i = i + 1
  81.       else
  82.         local pitch = tonumber(str:sub(i, i + 1), 16)
  83.         i = i + 2
  84.         local vol = tonumber(str:sub(i, i), 16)
  85.         channel.notes[j * 2 - 1] = pitch
  86.         channel.notes[j * 2] = vol
  87.         i = i + 1
  88.       end
  89.     end
  90.   end
  91.   return m
  92. end
  93.  
  94. function update()
  95.   for k,v in pairs(_music) do
  96.     if v.playing then
  97.       for l,w in pairs(v.channels) do
  98.         if w.notes[v.offset * 2] then
  99.           local pitch = 0.5 * math.exp(_const * (w.notes[v.offset * 2 - 1] - 1))
  100.           local vol = w.notes[v.offset * 2] / 15
  101.           playSound(w.sound, pitch, v.volume * (w.volumeId / 15) * vol, w.output)
  102.         end
  103.       end
  104.       v.offset = v.offset + 1
  105.       if v.offset > v.length then
  106.         v.playing = false
  107.         v.offset = 1
  108.       end
  109.     end
  110.   end
  111. end
Advertisement
Add Comment
Please, Sign In to add comment