Convert stereo audio data to mono

Wirtschaft Ist TotWirtschaft Ist Tot Posts: 3Member

Hi friends, I have an AudioStreamSample with stereo data. How can I convert it to mono? This will reduce the size by half, I think.


Tags :

Comments

  • TwistedTwiglegTwistedTwigleg Posts: 2,627Admin

    Welcome to the forums!

    In the import tab on the Godot Editor, I believe there is an option to convert audio to mono. From what I remember, it is near the bottom of the import settings for audio files.

  • Wirtschaft Ist TotWirtschaft Ist Tot Posts: 3Member
    edited March 20

    Hello TwistedTwigleg =) !

    Thanks for your help! But this is not quite for me. I need to convert throughout the gameplay.

  • Wirtschaft Ist TotWirtschaft Ist Tot Posts: 3Member
    edited March 20

    I already wrote a small function for this:

    func convert_stereo_to_mono(stream:AudioStreamSample):
        var old_data = stream.get_data()
        var new_data = range(old_data.size() / 2)
        if stream.format == stream.FORMAT_16_BITS:
            for i in new_data.size() / 2:
                new_data[i * 2] = (old_data[i * 4] + old_data[i * 4 + 2]) / 2.0
                new_data[i * 2+1] = (old_data[i * 4 + 1] + old_data[i * 4 + 3])/2.0
        else:
            for i in new_data.size():
                new_data[i] = (old_data[i * 2] + old_data[i * 2 + 1]) / 2.0
        stream.data = new_data
        stream.stereo = false
    

    It works great.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file