Search code examples
c++c++17godottaglibbytebuffer

Is there a better way to convert between two Byte Buffers of two different classes?


So, I'm using the TagLib Library in combination with the Godot Engine and I want to send big amounts of Data between those two in the form of Byte Arrays. The Problem is both have their own variables to store Bytes:

Godot: PoolByteArray TagLib: ByteVector

To return Data from TagLib functions into my Godot Project I have to convert from ByteVector to PoolByteArray.

My current way of doing it is like this:

godot::PoolByteArray ByteVector2PoolByte(TagLib::ByteVector data) {
godot::PoolByteArray x;
for (size_t i = 0; i < data.size(); i++) {
    x.push_back(data[i]);
}
return x;

This seems terrible inefficient to me and I thought there has to be a more efficient way of doing this.

Any help on this would be really appreciated :)

EDIT: Heres a link to waht I think is the header file for both types:

PoolByteArray / PoolArrays: https://pastebin.com/8ieW0yfS

ByteVector: https://taglib.org/api/tbytevector_8h_source.html

EDIT 2:

Also, someone pointed out that passing that huge amount of data by value instead of by reference is also pretty ineffecient, which is of course true.

EDIT 3:

As far as I can tell there is no constructor for godots array that would take const char * so I can't convert them this way.

SOLUTION:

godot::PoolByteArray ByteVector2PoolByte(TagLib::ByteVector && godot::PoolByteArray ByteVector2PoolByte(TagLib::ByteVector && data) {
  godot::PoolByteArray converted_buffer = godot::PoolByteArray();
  const  char* original_buffer = data.data();
  converted_buffer.resize(data.size());
  memcpy((uint8_t*)converted_buffer.write().ptr(), original_buffer, data.size());
  return converted_buffer;

Solution

  • Use memcpy to add data to PoolByteArray:

    char buf[4500] = {};
    PoolByteArray raw;
    raw.resize(4500);
    memcpy((uint8_t *)raw.write().ptr(), buf, sizeof(buf));