Facepunch / sbox-issues

177 stars 12 forks source link

SoundFile.GetSamplesAsync() only returns 44.1khz mono #2390

Open MuffinTastic opened 2 years ago

MuffinTastic commented 2 years ago

Describe the bug

When grabbing the samples from a sound file, the engine squashes everything down to 44100hz mono and returns that instead, even if it's originally something else e.g. 48000hz stereo. This causes a mismatch between SoundFile.Rate / .Channels and the actual returned sample data.

To Reproduce

  1. Grab a 48000hz stereo track, it illustrates the problem better. I've attached two files, one mp3 and one wav of some random royalty free jingle
  2. Paste this code in a game

    [ConCmd.Client]
    public static async void soundtest( string file )
    {
    var soundFile = SoundFile.Load( file );
    Assert.NotNull( soundFile );
    
    var loaded = await soundFile.LoadAsync();
    Assert.True( loaded );
    
    var samples = await soundFile.GetSamplesAsync();
    
    var soundOrigin = Sound.FromScreen( "core.soundscape_2d" );
    var soundStream = soundOrigin.CreateStream( soundFile.Rate, soundFile.Channels );
    
    soundStream.WriteData( samples.AsSpan() );
    
    await GameTask.DelaySeconds( soundFile.Duration );
    
    soundStream.Delete();
    soundOrigin.Stop();
    }
  3. Start the game, run soundtest <path/to/dumbjangle.vsnd> in console
  4. Hear it be pitched up when it shouldn't be due to bad data

Expected behavior

It should return the sound samples as-is, especially no forced mixing down from stereo to mono. That being said though, letting the developer choose options like resampling and mixing to mono might be useful/convenient in some cases, so if that's possible to add as an option that'd be nice.

Media/Files

sounds.zip

Additional context

Build b0aa68

walllable commented 2 years ago

Conveniently enough, I have some extra 48 and 96khz wav files on hand, figure they might come in handy for testing FOLDER03.zip