Advertisement
Guest User

punymusiclang.lua

a guest
May 4th, 2021
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.71 KB | None | 0 0
  1. local comp = require('component')
  2. local snd = comp.sound
  3. local tap = comp.tape_drive
  4. local no = require('note')
  5. local thrd = require('thread')
  6. local tbl = require('table')
  7.  
  8. local commsize = 2
  9. local channs = 1
  10.  
  11. local channadsr = {{[1]=0,[2]=0,[3]=0,[4]=0,['chg']=false},
  12. {[1]=0,[2]=0,[3]=0,[4]=0,['chg']=false},
  13. {[1]=0,[2]=0,[3]=0,[4]=0,['chg']=false},
  14. {[1]=0,[2]=0,[3]=0,[4]=0,['chg']=false},
  15. {[1]=0,[2]=0,[3]=0,[4]=0,['chg']=false},
  16. {[1]=0,[2]=0,[3]=0,[4]=0,['chg']=false},
  17. {[1]=0,[2]=0,[3]=0,[4]=0,['chg']=false},
  18. {[1]=0,[2]=0,[3]=0,[4]=0,['chg']=false}}
  19.  
  20. function setChanns(chn)
  21.   channs = chn
  22. end
  23.  
  24. function rnoteplay(midn, chn)
  25.   if channadsr[chn].chg then
  26.     channadsr[chn].chg = false
  27.     local adsr = channadsr[chn]
  28.     snd.setADSR(adsr[1], adsr[2], adsr[3], adsr[4])
  29.   end
  30.   snd.open(chn)
  31.   snd.setFrequency(chn, note.freq(midn))
  32. end
  33.  
  34. function nnoteplay(midn, chn)
  35.   snd.setFrequency(chn, note.freq(midn))
  36. end
  37.  
  38. function noterel(chn)
  39.   snd.close(chn)
  40. end
  41.  
  42. function volume(vol, chn)
  43.   snd.setVolume(chn, (vol / 255)*0.2)
  44. end
  45.  
  46. function sattack(att, chn)
  47.   channadsr[chn][1] = att * 8
  48.   channadsr[chn].chg = true
  49. end
  50.  
  51. function sdelay(del, chn)
  52.   channadsr[chn][2] = del * 8
  53.   channadsr[chn].chg = true
  54. end
  55.  
  56. function ssustain(sus, chn)
  57.   channadsr[chn][3] = (sus / 255)*0.2
  58.   channadsr[chn].chg = true
  59. end
  60.  
  61. function srelease(rel, chn)
  62.   channadsr[chn][4] = rel * 8
  63.   channadsr[chn].chg = true
  64. end
  65.  
  66. tap.seek(-(tap.getSize()))
  67.  
  68. local tapesize = string.unpack("<I4", tap.read(4))
  69.  
  70. function TICK()
  71.   snd.delay(50)
  72.   snd.process()
  73.   os.sleep(0.05)
  74. end
  75.  
  76. local currChan = 1
  77. local tickwait = 1
  78.  
  79. function decComm(bytecomm, data)
  80.   if bytecomm == 254 then
  81.     setChanns(data)
  82.     currChan = 1
  83.   elseif bytecomm == 100 then
  84.     tickwait = data
  85.     currChan = 1
  86.   elseif bytecomm == 130 then
  87.     sattack(data, currChan)
  88.   elseif bytecomm == 131 then
  89.     sdelay(data, currChan)
  90.   elseif bytecomm == 132 then
  91.     ssustain(data, currChan)
  92.   elseif bytecomm == 133 then
  93.     srelease(data, currChan)
  94.   elseif bytecomm == 155 then
  95.     volume(data, currChan)
  96.   elseif bytecomm == 2 then
  97.     rnoteplay(data, currChan)
  98.   elseif bytecomm == 3 then
  99.     nnoteplay(data, currChan)
  100.   elseif bytecomm == 4 then
  101.     noterel(currChan)
  102.   elseif bytecomm == 0 then
  103.     local linesize = commsize * channs
  104.     tap.seek(-(linesize * data))
  105.   end
  106. end
  107.  
  108. print("OK, starting")
  109.  
  110. local tickstowait = 1
  111.  
  112. while true do
  113.   tickstowait = tickstowait - 1
  114.   local tick = thrd.create(TICK)
  115.   if tickstowait <= 0 then
  116.     while currChan <= channs do
  117.       local co, coval = tap.read(2)
  118.       --local comma = string.unpack("<B", co)
  119.       --local comval = string.unpack("<I1", coval)
  120.       decComm(co, coval)
  121.       currChan = currChan + 1
  122.     end
  123.     tickstowait = tickwait
  124.   end
  125.   currChan = 1
  126.   thrd.waitForAll(tick)
  127. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement