Advertisement
CapsAdmin

Untitled

May 5th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.28 KB | None | 0 0
  1. polyphony = {}
  2.  
  3. polyphony.Instruments = {
  4.     "wowozela/samples/bass.wav",
  5.     "wowozela/samples/bass.wav",
  6.     "wowozela/samples/bass3.wav",
  7.     "wowozela/samples/bassguitar2.wav",
  8.     "wowozela/samples/bell.wav",
  9.     "wowozela/samples/coolchiff.wav",
  10.     "wowozela/samples/crackpiano.wav",
  11.     "wowozela/samples/dingdong.wav",
  12.     "wowozela/samples/dooooooooh.wav",
  13.     "wowozela/samples/dusktodawn.wav",
  14.     "wowozela/samples/flute.wav",
  15.     "wowozela/samples/fmbass.wav",
  16.     "wowozela/samples/fuzz.wav",
  17.     "wowozela/samples/guitar.wav",
  18.     "wowozela/samples/hit.wav",
  19.     "wowozela/samples/honkytonk.wav",
  20.     "wowozela/samples/horn.wav",
  21.     "wowozela/samples/justice.wav",
  22.     "wowozela/samples/littleflower.wav",
  23.     "wowozela/samples/meow.wav",
  24.     "wowozela/samples/miku.wav",
  25.     "wowozela/samples/mmm.wav",
  26.     "wowozela/samples/oohh.wav",
  27.     "wowozela/samples/overdrive.wav",
  28.     "wowozela/samples/pianostab.wav",
  29.     "wowozela/samples/prima.wav",
  30.     "wowozela/samples/quack.wav",
  31.     "wowozela/samples/saw_880.wav",
  32.     "wowozela/samples/sine_880.wav",
  33.     "wowozela/samples/skull.wav",
  34.     "wowozela/samples/slap.wav",
  35.     "wowozela/samples/square_880.wav",
  36.     "wowozela/samples/string.wav",
  37.     "wowozela/samples/toypiano.wav",
  38.     "wowozela/samples/triangle_880.wav",
  39.     "wowozela/samples/trumpet.wav",
  40.     "wowozela/samples/woof.wav",
  41. }
  42.  
  43. if CLIENT then
  44.     function polyphony.Start(instrument, key, pitch, volume)
  45.         polyphony.ModifyVoice(LocalPlayer(), instrument, key, pitch, volume)
  46.         polyphony.NetSend(instrument, key, pitch, volume)
  47.     end
  48.  
  49.     function polyphony.Stop(instrument, key)
  50.         polyphony.ModifyVoice(LocalPlayer(), instrument, key, 0, 0)
  51.         polyphony.NetSend(instrument, key, 0, 0)
  52.     end
  53.    
  54.     for key, ply in pairs(player.GetAll()) do
  55.         if ply.poly_voices then
  56.             for key, instrument in pairs(ply.poly_voices) do
  57.                 for key, ent in pairs(instrument) do
  58.                     if ent:IsValid() then
  59.                         if ent.poly_sound then
  60.                             ent.poly_sound:Stop()
  61.                         end
  62.                         ent:Remove()
  63.                     end
  64.                 end
  65.             end
  66.         end
  67.        
  68.         ply.poly_voices = nil
  69.     end
  70.    
  71.     function polyphony.ModifyVoice(ply, instrument, key, pitch, volume)
  72.         ply.poly_voices = ply.poly_voices or {}
  73.        
  74.         ply.poly_voices[instrument] = ply.poly_voices[instrument] or {}
  75.        
  76.         local ent = ply.poly_voices[instrument][key] or NULL
  77.        
  78.         if ent.poly_sound then
  79.             ent.poly_sound:Stop()
  80.         end
  81.        
  82.         SafeRemoveEntity(ent)
  83.        
  84.         if not ent:IsValid() then
  85.             ent = ents.CreateClientProp()
  86.             ent:SetPos(ply:EyePos())
  87.             ent:SetModel("models/props_junk/PopCan01a.mdl")
  88.         end
  89.        
  90.         local snd = CreateSound(ent, polyphony.Instruments[instrument])
  91.                        
  92.         local mult = -(ply:EyePos():Distance(LocalPlayer():EyePos()) / 1000) + 2
  93.        
  94.         if pitch > 0 and volume > 0 then
  95.             snd:Stop()
  96.             snd:Play()
  97.             snd:ChangeVolume((volume / 255) * mult, 0)
  98.             snd:ChangePitch(pitch, 0)
  99.         else
  100.             snd:Stop()
  101.             ent:Remove()
  102.         end
  103.        
  104.         ent.poly_sound = snd
  105.        
  106.         ply.poly_voices[instrument][key] = ent     
  107.     end
  108.        
  109.     function polyphony.NetSend(instrument, key, pitch, volume)
  110.         net.Start("polyphony")
  111.             net.WriteInt(instrument - 128, 8)
  112.             net.WriteInt(key - 128, 8)
  113.             net.WriteInt(pitch - 128, 8)
  114.             net.WriteInt(volume - 128, 8)
  115.         net.SendToServer()
  116.     end
  117.    
  118.     net.Receive("polyphony", function()
  119.         local ply = net.ReadEntity()
  120.        
  121.         if ply:IsValid() then
  122.             local instrument = net.ReadInt(8) + 128
  123.             local key = net.ReadInt(8) + 128
  124.             local pitch = net.ReadInt(8) + 128
  125.             local volume = net.ReadInt(8) + 128
  126.            
  127.             --prints(ply, instrument, key, pitch, volume)
  128.            
  129.             polyphony.ModifyVoice(ply, instrument, key, pitch, volume)
  130.         end
  131.     end)
  132. end
  133.  
  134. if SERVER then
  135.  
  136.     util.AddNetworkString("polyphony")
  137.  
  138.     net.Receive("polyphony", function(len, ply)
  139.         if ply:IsValid() then
  140.             local instrument = net.ReadInt(8)
  141.             local key = net.ReadInt(8)
  142.             local pitch = net.ReadInt(8)
  143.             local volume = net.ReadInt(8)
  144.            
  145.             print(ply, instrument + 128, key + 128, pitch + 128, volume + 128)
  146.            
  147.             local filter = {}
  148.             for _, v in pairs(player.GetAll()) do
  149.                 local mult = -(ply:EyePos():Distance(v:EyePos()) / 1000) + 2
  150.                
  151.                 if v ~= ply and mult > 0 then
  152.                     table.insert(filter, v)
  153.                 end
  154.             end
  155.            
  156.             net.Start("polyphony")
  157.                 net.WriteEntity(ply)
  158.                 net.WriteInt(instrument, 8)
  159.                 net.WriteInt(key, 8)
  160.                 net.WriteInt(pitch, 8)
  161.                 net.WriteInt(volume, 8)
  162.             net.Send(filter)
  163.         end
  164.     end)
  165.  
  166. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement