xmath result as float

Programming related discussions related to game research
StreamThread
Posts: 54
Joined: Fri May 27, 2016 2:28 pm

xmath result as float

Post by StreamThread »

Hello

How with xmath can get result as float?

E.g. my code:

Code: Select all

xmath sndTime "1.0f * subchunk2Size / (bitsPerSample / 8.0f) / numChannels / sampleRate"

xmath sndTime "sndTime - 0.100f"


Vars:
Subchunk2Size 860160

BitsPerSample / 8.0 = 2

NumChannels 1

SampleRate 44100

Result must be ~= 9,752 but xmath return rounded value "9". It's wrong for next calculations with this result.

Is it possible to set a type of "sndTime" var as float?
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: xmath result as float

Post by aluigi »

quickbms works only with integers.
StreamThread
Posts: 54
Joined: Fri May 27, 2016 2:28 pm

Re: xmath result as float

Post by StreamThread »

Maybe possible add support of float values in the next Quick BMS update? For example, it will works only if Quick BMS starting with certain option.
That could be good innovation. I find Quick bms as the perfect tool for more different things.
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: xmath result as float

Post by aluigi »

Unfortunately that's not possible.
Sorry.
StreamThread
Posts: 54
Joined: Fri May 27, 2016 2:28 pm

Re: xmath result as float

Post by StreamThread »

Returning to this answer.

Maybe in that situation is possible use CallDLL function? Me need do only calculation with float values, result will written as integer value.
I'm tryed make this with C' like code, but BMS give errors:

Code: Select all

set memory_file10 binary "
void fSubchunk2Size (int subchunk2Size2, int bitsPerSample2, int numChannels2, int sampleRate2) {
float sndTime;

 sndTime = 1.0 * subchunk2Size2 / (bitsPerSample2 / 8.0) / numChannels2 / sampleRate2;
 sndTime - 0.100;
 subchunk2Size2 = sndTime * sampleRate2 * numChannels2 * (bitsPerSample2 / 8.0);

 };"

get oggsize asize
get oggname filename
log MEMORY_FILE 0 oggsize
getDString chunkId 0x4 MEMORY_FILE #RIFF
get chunkSize long MEMORY_FILE
getDString format 0x4 MEMORY_FILE #WAVE
getDString subchunk1ID 0x4 MEMORY_FILE #fmt
get sunchunk1Size long MEMORY_FILE
get audioFormat short MEMORY_FILE
get numChannels short MEMORY_FILE
get sampleRate long MEMORY_FILE
get byteRate long MEMORY_FILE
get blockAlign short MEMORY_FILE
get bitsPerSample short MEMORY_FILE
getDString subchunk2ID 0x4 MEMORY_FILE #data
get subchunk2Size long MEMORY_FILE

#xmath sndTime "1.0f * subchunk2Size / (bitsPerSample / 8.0f) / numChannels / sampleRate"

#xmath sndTime "sndTime - 0.100f"


#xmath fSubchunk2Size "sndTime * sampleRate * numChannels * (bitsPerSample / 8.0)"

calldll MEMORY_FILE10 "fSubchunk2Size" "tcc" RET subchunk2Size bitsPerSample numChannels sampleRate

print "Event: %subchunk2size% , %RET%"
putVarChr MEMORY_FILE 0x28 subchunk2Size Long

#log oggname 0 oggsize MEMORY_FILE


I'm sure error can be in my C function. I hope, what BMS can work with this "trick" if my errors will corrected.
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: xmath result as float

Post by aluigi »

I don't suggest you to use quickbms for these things.

The error you get is caused by the necessity of using some external library because tcc is fantastic but requires to load some libraries (even the basic libc) from file... something that is not good with quickbms where you have no dependencies. Honestly I didn't know tcc had dependencies even for using floats, this was a "bad" surprise :(

So my advice is to NOT use quickbms for that thing, use a programming language.

If you want to continue with quickbms you have 2 other choices: using a dll or using just a pre-compiled function.
The dll is very easy but it will make your code big while for the pre-compiled function use the following:

Code: Select all

set MEMORY_FILE10 binary "\x55\x89\xe5\x83\xec\x20\xdb\x45\x08\xdb\x45\x0c\xdd\x05\xc8\xb0\x40\x00\xde\xf9\xde\xf9\xdb\x45\x10\xde\xf9\xdb\x45\x14\xde\xf9\xd9\x5d\xfc\xd9\x45\xfc\xdd\x05\xd0\xb0\x40\x00\xde\xe9\xd9\x5d\xfc\xdb\x45\x14\xd8\x4d\xfc\xdb\x45\x10\xde\xc9\xdb\x45\x0c\xdd\x05\xc8\xb0\x40\x00\xde\xf9\xde\xc9\xd9\x7d\xea\x0f\xb7\x45\xea\xb4\x0c\x66\x89\x45\xe8\xd9\x6d\xe8\xdb\x5d\xe4\xd9\x6d\xea\x8b\x45\xe4\xc9\xc3"
calldll MEMORY_FILE10 0 "cdecl" subchunk2Size subchunk2Size bitsPerSample numChannels sampleRate

Yes, subchunk2Size is going to take the return value.

Original C code used to compile that data:

Code: Select all

int fSubchunk2Size (int subchunk2Size2, int bitsPerSample2, int numChannels2, int sampleRate2) {
 float sndTime;
 sndTime = 1.0 * subchunk2Size2 / (bitsPerSample2 / 8.0) / numChannels2 / sampleRate2;
 sndTime -= 0.100;
 return sndTime * sampleRate2 * numChannels2 * (bitsPerSample2 / 8.0);
}