0 votes

I wrote a GDNative plugin that generates an audio sample in a raw PCM format (2 channels 16 bit at a specified sampling rate) and stores it in a buffer.

Now, I want to create an audio sample in Godot from this data.
Is there any way to do this?

Solutions using either GDScript or GDNative would be great.

asked May 18, 2018 in Engine by Akorian (99 points)
edited May 18, 2018 by Akorian

1 Answer

+1 vote
Best answer

After messing around with it some more I managed to solve this issue myself, here's what I did:

I generated the PCM audio data as usual and returned it from GDNative back to GDScript as pool byte array. I then created a new AudioSampleStream in GDScript, set all the appropriate parameters and set the .data property to the pool byte array I mentioned above:

func RenderToSample():
    var Sample = AudioStreamSample.new()
    Sample.data = NativeObj_IPxtone.RenderToSample()
    Sample.format = AudioStreamSample.FORMAT_16_BITS
    Sample.loop_mode = AudioStreamSample.LOOP_DISABLED
    Sample.loop_begin = 0
    Sample.loop_end = 0
    Sample.mix_rate = SamplesPerSecond

    if NumChannels == 2:
        Sample.stereo = true
    else:
        Sample.stereo = false

    return Sample

! Note that doing this might be unsafe, as the .data property is not found anywhere in Godot's documentation.

answered May 19, 2018 by Akorian (99 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.