Facepunch / garrysmod-issues

Garry's Mod issue tracker
143 stars 56 forks source link

Using CreateSound with DSP and a #-prefixed wav causes audio issues #5846

Open KiwifruitDev opened 5 months ago

KiwifruitDev commented 5 months ago

Details

When attempting to create a clientside CSoundPatch object that can utilize the "Music volume" setting, I tried using the "#" soundscript character.

This did not work, instead it made the audio play faster with artifacting.

Audio Information

kiwifruitdev/kiwi_f1_menu/memories.mp3 is located in the sound folder of a local addon.

It plays for 6:55, and has a size of 6.32 MB.

The stats below were given by VLC media player:

Codec: MPEG Audio layer 1/2 (mpga)
Channels: Stereo
Sample rate: 44100 Hz
Bits per sample: 32
Bitrate: 128 kb/s

Code

The code below was re-formatted for readability:

local BackgroundMusic = CreateSound(LocalPlayer(), "#kiwifruitdev/kiwi_f1_menu/memories.mp3")
BackgroundMusic:SetDSP(1)
BackgroundMusic:Play()
Original Code The code below is simplified but retains the original variable structure: ```lua KIWI_F1_MENU = {} KIWI_F1_MENU.CONVARS = {} KIWI_F1_MENU.CONVARS.BackgroundMusic = CreateClientConVar("kiwi_f1_menu_bgm", "1", true, false, "Should the background music be enabled?", 0, 1) KIWI_F1_MENU.MUSIC = {"kiwifruitdev/kiwi_f1_menu/memories.mp3"} KIWI_F1_MENU.CONST = {} KIWI_F1_MENU.CONST.DSP = 1 KIWI_F1_MENU.CONST.FadeTime = 0.8 KIWI_F1_MENU.VARIABLES = {} KIWI_F1_MENU.VARIABLES.LocalPlayer = LocalPlayer() KIWI_F1_MENU.VARIABLES.MusicExtension = table.Copy(KIWI_F1_MENU.MUSIC) -- Start playback KIWI_F1_MENU.VARIABLES.BackgroundMusic = CreateSound(KIWI_F1_MENU.VARIABLES.LocalPlayer, "#"..table.Random(KIWI_F1_MENU.VARIABLES.MusicExtension)) KIWI_F1_MENU.VARIABLES.BackgroundMusic:SetDSP(KIWI_F1_MENU.CONST.DSP) if KIWI_F1_MENU.CONVARS.BackgroundMusic:GetBool() then KIWI_F1_MENU.VARIABLES.BackgroundMusic:Play() end -- End playback if KIWI_F1_MENU.VARIABLES.BackgroundMusic then if KIWI_F1_MENU.VARIABLES.BackgroundMusic:IsPlaying() and KIWI_F1_MENU.CONVARS.BackgroundMusic:GetBool() then KIWI_F1_MENU.VARIABLES.BackgroundMusic:FadeOut(KIWI_F1_MENU.CONST.FadeTime) else KIWI_F1_MENU.VARIABLES.BackgroundMusic:Stop() end end ```

Demonstration

The audio is played and stopped when this menu opens and closes:

Video [demonstration.webm](https://github.com/Facepunch/garrysmod-issues/assets/25839651/918982cc-e20f-464a-819f-9105b94e1ef9)

Version

Here is what the version command reports:

Protocol version 24
Exe version 2023.06.28 (garrysmod)
Exe build: 06:17:43 Apr  1 2024 (9246) (4000)
GMod version 2024.04.22, branch: x86-64, multicore: 1
Windows 64bit
robotboy655 commented 5 months ago

In order to make the sound be marked as a "music" track, it must be non directional. i.e.:

local BackgroundMusic = CreateSound(game.GetWorld(), "#music/hl2_song29.mp3")
BackgroundMusic:SetSoundLevel(0)
// BackgroundMusic:SetDSP(1)
BackgroundMusic:Play()  

I am not sure it is worth the effort to look into the DSP issue, as sounds marked as music are not meant to support DSPs anyway.