Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- RadioWavs = {};
- RadioWavs.soundCache = {};
- RadioWavs.files = {};
- RadioWavs.files[1] = "RadioMusic/ComeAsYouAre"
- RadioWavs.stopSoundNumber = -1; -- number that triggers sounds to stop
- RadioWavs.cacheSize = 10; -- sound id's to keep in cache
- function RadioWavs.PlaySound(_number, _source)
- local device = _source:getDeviceData()
- local emitter
- local sound3d
- local volume
- device:setBaseVolumeRange(0)
- if device:isVehicleDevice() == false then
- emitter = device:getEmitter()
- volume = device:getDeviceVolume() * 0.4F
- sound3d = true
- else
- local vehiclePart = device:getParent()
- if vehiclePart then
- local vehicle = vehiclePart:getVehicle()
- if vehicle then
- emitter = vehicle:getEmitter() -- use car's emitter because car radios don't have one
- if vehicle == getPlayer():getVehicle() then -- player is in the car
- volume = device:getDeviceVolume() * 0.4F
- sound3d = false -- no 3d sound while in car, it sounds glitchy
- else
- volume = device:getDeviceVolume() * 0.2F
- sound3d = true
- end
- end
- end
- if not emitter then
- print("RadioWavs mod ERROR: no SoundEmitter found!")
- return
- end
- end
- if _number==RadioWavs.stopSoundNumber then
- for _,t in ipairs(RadioWavs.soundCache) do
- emitter:stopSound(t.soundID);
- end
- elseif RadioWavs.files[_number] then
- emitter:setPos(_source:getX(), _source:getY(), _source:getZ())
- for _,t in ipairs(RadioWavs.soundCache) do
- if emitter:isPlaying(t.soundID) then
- if t.soundID == RadioWavs.files[_number] then
- emitter:setVolume(soundID, volume) -- song is already active, just pump up the volume
- return
- else
- emitter:stopSound(t.soundID); -- an old song, stop it
- end
- end
- end
- local soundID = emitter:playSound(RadioWavs.files[_number])
- emitter:setVolume(soundID, volume)
- emitter:set3D(soundID, sound3d)
- local t = {}
- t.device = device
- t.emitter = emitter
- t.soundID = soundID
- t.channel = device:getChannel()
- table.insert( RadioWavs.soundCache, 1, t )
- if #RadioWavs.soundCache>RadioWavs.cacheSize then
- for i=RadioWavs.cacheSize+1,#RadioWavs.soundCache do
- table.remove(RadioWavs.soundCache,i)
- end
- end
- end
- end
- function RadioWavs.split(str,sep)
- local sep, fields = sep or ":", {};
- local pattern = string.format("([^%s]+)", sep);
- str:gsub(pattern, function(c) fields[#fields+1] = c end);
- return fields;
- end
- function RadioWavs.OnDeviceText(_interactCodes, _x, _y, _z, _line, _source)
- local codes = RadioWavs.split(_interactCodes, ",");
- local rData = _source:getDeviceData();
- for _,_v in ipairs(codes) do
- if _v:len() > 4 then
- local code = string.sub(_v, 1, 3);
- local op = string.sub(_v, 4, 4);
- local amount = tonumber(string.sub(_v, 5, _v:len()));
- if op=="-" then
- amount = -amount
- end
- if amount ~= nil and code=="DRU" then
- RadioWavs.PlaySound(amount, _source);
- end
- end
- end
- end
- Events.OnDeviceText.Add( RadioWavs.OnDeviceText );
- function RadioWavs.OnEnterVehicle(_player)
- local emitter = _player:getVehicle():getEmitter()
- for _,t in ipairs(RadioWavs.soundCache) do
- if t.emitter == emitter then
- t.emitter:setVolume(t.soundID, t.device:getDeviceVolume() * 0.4F);
- t.emitter:set3D(t.soundID, false) -- no 3d sound while in car, it sounds glitchy
- break
- end
- end
- end
- Events.OnEnterVehicle.Add( RadioWavs.OnEnterVehicle );
- local originalExitVehicle = ISExitVehicle.perform;
- function ISExitVehicle:perform()
- local emitter = self.character:getVehicle():getEmitter()
- for _,t in ipairs(RadioWavs.soundCache) do
- if t.emitter == emitter then
- t.emitter:setVolume(t.soundID, t.device:getDeviceVolume() * 0.2F);
- t.emitter:set3D(t.soundID, true)-- enable 3d sound when outside the car
- break
- end
- end
- originalExitVehicle(self)
- end
- local originalToggle = ISRadioAction.performToggleOnOff;
- function ISRadioAction:performToggleOnOff()
- originalToggle(self);
- local volume = 0
- if self.deviceData:getIsTurnedOn() then
- volume = self.deviceData:getDeviceVolume() * 0.4F
- end
- for _,t in ipairs(RadioWavs.soundCache) do
- if t.device == self.deviceData then
- t.emitter:setVolume(t.soundID, volume); -- mute/ummute sound
- end
- end
- end
- local originalSetChannel = ISRadioAction.performSetChannel;
- function ISRadioAction:performSetChannel()
- for _,t in ipairs(RadioWavs.soundCache) do
- if t.device == self.deviceData then
- if t.channel == self.secondaryItem then -- switching back to chanel
- t.emitter:setVolume(t.soundID, t.device:getDeviceVolume() * 0.4F); -- ummute sound
- else
- t.emitter:setVolume(t.soundID, 0); -- mute sound, but keep playing
- end
- end
- end
- originalSetChannel(self);
- end
- local originalSetVolume = ISRadioAction.performSetVolume;
- function ISRadioAction:performSetVolume()
- if self:isValidSetVolume() then
- originalSetVolume(self);
- for _,t in ipairs(RadioWavs.soundCache) do
- if t.device == self.deviceData then
- t.emitter:setVolume(t.soundID, t.device:getDeviceVolume() * 0.4F);
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement