float to byte ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By leg0
:warning: Old Version Published before Godot 3 was released.

Hello :slight_smile:

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

:bust_in_silhouette: Reply From: avencherus

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)

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 :slight_smile:

leg0 | 2017-02-12 19:17

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.

avencherus | 2017-02-12 19:29

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.

CrazyM | 2018-08-10 02:36