Advertisement
fatboychummy

soundTesting.lua

Nov 5th, 2018
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.82 KB | None | 0 0
  1. local sound = peripheral.find("sound")
  2. local modes = sound.getModes()
  3. local delay = 200
  4. local sleepTime = delay/1000
  5. local last = false
  6. local modeOverride = modes.square
  7. local openChannels = 0
  8.  
  9. local sineDif = 600
  10. local sawDif = 300
  11. local squareDif = 0
  12.  
  13. local tArgs = {...}
  14. if #tArgs > 0 then
  15.   if modes[tArgs[1]] then
  16.     modeOverride = modes[tArgs[1]]
  17.   else
  18.     local b = tonumber(tArgs[1])
  19.     if type(b) == "number" then
  20.       delay = b
  21.       sleepTime = delay/1000
  22.     else
  23.       printError("Expected mode or delay for argument 1")
  24.       printError("Usage: " .. shell.getRunningProgram() .. " <mode / delay> [delay]")
  25.       printError("Valid types: <sawtooth,square,sine>")
  26.       return -1
  27.     end
  28.   end
  29.   if tArgs[2] then
  30.     if type(tonumber(tArgs[2])) == "number" then
  31.       delay = tonumber(tArgs[2])
  32.       sleepTime = delay/1000
  33.     else
  34.       printError("Expected delay for argument 2")
  35.       printError("Usage: " .. shell.getRunningProgram() .. " <mode / delay> [delay]")
  36.       return -1
  37.     end
  38.   end
  39. end
  40.  
  41. for i = 1,8 do
  42.   sound.close(i)
  43.   sound.setVolume(i,0)
  44. end
  45.  
  46. local notes = {}
  47.  
  48. local function loadNotes()
  49.   local h = fs.open("notes","r")
  50.   local lines = 0
  51.   if h then
  52.     while h.readLine() do
  53.       lines = lines + 1
  54.     end
  55.     h.close()
  56.     openChannels = lines
  57.     if lines > 8 then
  58.       error("Only 8 channels.")
  59.     end
  60.   end
  61.   --[[
  62.     SAW
  63.     SIN
  64.     SQU
  65.   ]]
  66.   h = fs.open("notes","r")
  67.   if h and lines > 0 then
  68.     for i = 1,openChannels do
  69.       print("Reading...")
  70.       local a = h.readLine()
  71.       print(a)
  72.       sound.open(i)
  73.       sound.setVolume(i,1)
  74.       notes[i] = {}
  75.       local b = string.sub(a,1,3)
  76.       a = string.sub(a,4)
  77.       if b == "SAW" then
  78.         notes[i].t = modes.sawtooth
  79.       elseif b == "SQU" then
  80.         notes[i].t = modes.square
  81.       elseif b == "SIN" then
  82.         notes[i].t = modes.sine
  83.       elseif b == "TRI" then
  84.         notes[i].t = modes.triangle
  85.       else notes[i].t = modes.noise
  86.       end
  87.       os.sleep(0.5)
  88.       local o = 1
  89.       for k in a:gmatch(".") do
  90.         print(i,k)
  91.         notes[i][o] = k
  92.         o = o + 1
  93.       end
  94.       print("Done")
  95.     end
  96.     h.close()
  97.   else
  98.     error("Could not open file.")
  99.   end
  100. end
  101.  
  102.  
  103. local function ls(c,f,m)
  104.   c = c or 1
  105.   f = f or 500
  106.   last = f
  107.   m = m or modeOverride or modes.sine
  108.   if m == modes.sine then
  109.     f = f + sineDif
  110.   elseif m == modes.sawtooth then
  111.     f = f + sawDif
  112.   end
  113.   sound.setFrequency(c,f)
  114.   sound.setWave(c,m)
  115.   sound.setVolume(c,1)
  116. end
  117.  
  118. local function playNote(c,n,tp)
  119.   if n then
  120.     n = string.lower(n)
  121.     local function no()
  122.       print("no for now.")
  123.     end
  124.     if n == "a" then
  125.       ls(c,55,tp)
  126.     elseif n == "b" then
  127.       ls(c,61.73541,tp)
  128.     elseif n == "c" then
  129.       ls(c,32.70320,tp)
  130.     elseif n == "d" then
  131.       ls(c,36.70810,tp)
  132.     elseif n == "e" then
  133.       ls(c,41.20344,tp)
  134.     elseif n == "f" then
  135.       ls(c,43.65353,tp)
  136.     elseif n == "g" then
  137.       ls(c,48.99943,tp)
  138.     elseif n == "_" then
  139.       print("Silence, but wait a minute...")
  140.       ls(c,0.1,tp)
  141.     elseif n == "r" then
  142.       print("Repeat last note")
  143.       if last then
  144.         ls(c,last,tp)
  145.       else
  146.         sound.setVolume(c,0)
  147.         print("No note to repeat.")
  148.       end
  149.     else
  150.  
  151.       sound.setVolume(c,0)
  152.       print("Unavailable:",n)
  153.     end
  154.   else
  155.     sound.setVolume(c,0)
  156.     print("blankness my dudes: ",n)
  157.   end
  158. end
  159.  
  160.  
  161. local function playNotes(tab)
  162.   for o = 1,#tab[1] do
  163.     for i = 1,openChannels do
  164.       print("---")
  165.       print("Playing note",tab[i][o])
  166.       playNote(i,tab[i][o],tab[i].t)
  167.     end
  168.     sound.delay(delay)
  169.     sound.process()
  170.     os.sleep(sleepTime)
  171.   end
  172. end
  173.  
  174.  
  175. loadNotes()
  176.  
  177. print(textutils.serialize(notes))
  178. while true do
  179.   playNotes(notes)
  180.   --os.sleep()
  181. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement