0 votes

Hello :)

I'm trying to generate some sound through script

I'm stuck because I have no idea how to cast a float into byte (to add to a RawArray)

Any pointers ?

Thanks

in Engine by (31 points)

1 Answer

0 votes
Best answer

From what I can tell bytes are treated as integers in GDScipt. The RawArray's append accepts an integer argument.

So all you have to do is cast the float to int and then append it.

var my_int = int(12.38)
var ra = RawArray()

ra.append(my_int)

for byte in ra:
    print(byte)
by (5,206 points)
selected by

I was checking whatever was inside a wav file and I found out that it was only integers indeed

The Sample documentation was misleading for me when talking about "endians"

http://docs.godotengine.org/en/stable/classes/class_sample.html?highlight=Sample#class-sample-set-data

Thanks for the answer :)

No problem. In these cases, the integer would be something different if you flipped the endian. So something like 1 would be as 128 the other way around.

How did you convert bytes to int to read the wav data? I was just experimenting with this and tried writing each four byte chuck to a new PoolByteArray, then tried to convert that using bytes2var so I could read the value of the sample. But I get an error from the bytes2var call saying "Not enough bytes for decoding bytes, or invalid format." According to the docs I found, an int = four bytes.

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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.