garrysmod-issues icon indicating copy to clipboard operation
garrysmod-issues copied to clipboard

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

Open KiwifruitDev opened this issue 10 months ago • 1 comments

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:

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

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

KiwifruitDev avatar Apr 23 '24 18:04 KiwifruitDev