V2m file duration

category: code [glöplog]
Well guys, I need your help again.

I´d like to calculate the playback time of v2m files, just as someone did with this source:

------------------------------------------------------------------------------ -

unit v2minfo;


uses Windows, Messages, KOL;

function GetV2Minfo(v2mfile:string):dword;


function GetV2Minfo(v2mfile:string):dword;
tdl, tdh, tdh2, tsign, tsigd, tpq:byte;
fract, maxt, gevsno, usecs, ttime:dword;
if (fileexists(v2mfile)) then begin
fst.Read(maxt, 4);
fst.Read(tdl, 1);
fst.Read(tdh, 1);
fst.Read(tdh2, 1);
fst.Read(usecs, 4);
fst.Read(tsign, 1);
fst.Read(tsigd, 1);
fst.Read(tpq, 1);
ttime:=((((maxt shl 3) div fract +1) div tpq)*usecs div 1000 +2000 ) div 1000;


------------------------------------------------------------------------------ -

I tried understand the code I think, but when I try it on some modules it calculates wrong values. Any suggestions how to translate this old Delphi code to any newer environment? C# e.g.?
added on the 2013-03-16 04:10:34 by Freefall Freefall
Seems fairly obvious:

<no error checking + potential mistakes ahead, not checked with actual C compiler :) >

uchar8_t tdl, tdh, tdh2, tsign, tsigd, tpq;
uint32_t fract, maxt, gevsno, usecs, ttime;
FILE* f = fopen(v2mfile, "rb");

fread(&fract, sizeof(fract), 1, f);
fread(&maxt, sizeof(maxt), 1, f);
fread(&gevsno, sizeof(gevsno), 1, f);
fread(&tdl, sizeof(tdl), 1, f);
fread(&tdh, sizeof(tdh), 1, f);
fread(&tdh2, sizeof(tdh2), 1, f);
fread(&usecs, sizeof(usecs), 1, f);
fread(&tsign, sizeof(tsign), 1, f);
fread(&tsigd, sizeof(tsigd), 1, f);
fread(&tpq, sizeof(tpq), 1, f);

uint32_t ttime = ((((maxt << 3) / fract + 1) / (uint32_t)tpq) * usecs / 1000 + 2000) / 1000;
added on the 2013-03-16 10:24:09 by kbi kbi
What does "fract" and "<<" mean? C# code please :(
added on the 2013-03-16 13:29:32 by Freefall Freefall
uh, fract was unit32 variable^^ but still don´t come along with These Operators...
added on the 2013-03-16 13:42:16 by Freefall Freefall
<< is the left bitshift operator, it should be available in C#. "<< 3" is basically a multiplication by 8, just faster. fopen, fread are the functions to open and read a file.
added on the 2013-03-16 13:43:05 by MsK` MsK`
freefall - just in case its not clear fract is the first 4 bytes of the file not a command
added on the 2013-03-16 13:45:06 by Canopy Canopy
Jep it was clear to me but thx. Never saw These strange bitshift Operator before, I wouldn´t use it. I would instead read only what I need. Thank you guys.
added on the 2013-03-16 13:50:02 by Freefall Freefall
btw bit shift left 3 is basically a cheat to * 4 i think
added on the 2013-03-16 14:33:27 by Canopy Canopy
Well it still doesn´t work. My VB .NET Code:


Dim path As String = ""

Dim opendlg As New OpenFileDialog
opendlg.Filter = "V2m files (*.v2m)|*.v2m"
If opendlg.ShowDialog = Windows.Forms.DialogResult.OK Then
path = opendlg.FileName
End If
Dim read() As Byte = My.Computer.FileSystem.ReadAllBytes(path)

Dim tdl(0) As Byte, tdh(0) As Byte, tdh2(0) As Byte, tsign(0) As Byte, tsigd(0) As Byte, tpq(0) As Byte
Dim fract(3) As UInteger, maxt(3) As UInteger, gevsno(3) As UInteger, usecs(3) As UInteger

Array.Copy(read, 0, fract, 0, 4)
Array.Copy(read, 4, maxt, 0, 4)
Array.Copy(read, 8, gevsno, 0, 4)
Array.Copy(read, 12, tdl, 0, 1)
Array.Copy(read, 13, tdh, 0, 1)
Array.Copy(read, 14, tdh2, 0, 1)
Array.Copy(read, 15, usecs, 0, 4)
Array.Copy(read, 19, tsign, 0, 1)
Array.Copy(read, 20, tsigd, 0, 1)
Array.Copy(read, 21, tpq, 0, 1)

Dim maxtVal As Double = maxt(0) + maxt(1) + maxt(2) + maxt(3)
Dim fractVal As Double = fract(0) + fract(1) + fract(2) + fract(3)
Dim tpqVal As Double = tpq(0)
Dim usecsVal As Double = usecs(0) + usecs(1) + usecs(2) + usecs(3)
Dim ttime As Double = ((((maxtVal * 8) / fractVal + 1) / tpqVal) * usecsVal / 1000 + 2000) / 1000
Label1.Text = ttime.ToString

added on the 2013-03-16 14:35:38 by Freefall Freefall
Output: 2,00069459375
V2m duration: 2m 45s
added on the 2013-03-16 14:37:21 by Freefall Freefall

Dim maxtVal As Double = maxt(0) + maxt(1) * 256 + maxt(2) * 256 * 256 + maxt(3) * 256 * 256 * 256

with the other arrays as well. but optimally read up on things first
added on the 2013-03-16 16:38:27 by Gargaj Gargaj
Thank you very much Sir Coding Hero :) <3 Works now. Btw: cool synth you made, too :) Like the conspiracy Demos. Have fun!
added on the 2013-03-16 19:13:13 by Freefall Freefall
Aaargh! Still has some mistakes in it. Sometimes it calculates almost right values, sometimes it calcs a mistake range of +- 5 or +-7 seconds....
added on the 2013-03-16 19:42:47 by Freefall Freefall
Update: mistake range is + 5 or +7 seconds....
added on the 2013-03-16 19:43:33 by Freefall Freefall
Never saw These strange bitshift Operator before, I wouldn´t use it.

This saddens me. Especially in a thread about a heavily optimized synthesizer.
Well I never got away from .NET programming ;) C/C++ ppl of course don´t understand this :D
added on the 2013-03-16 21:13:09 by Freefall Freefall
.Net languages (VB.Net, C#) also have bit shift operators, you know?

btw bit shift left 3 is basically a cheat to * 4 i think

Ok, this one saddens me even more.
The maxtime isn't very precise (and I'm surprised to find that I actually put it in :). Deal with it, or alternatively parse all notes, all envelopes and the delay/reverb settings, and calculate it correctly - good luck :P
added on the 2013-03-17 03:24:17 by kb_ kb_
added on the 2013-03-17 04:11:20 by yumeji yumeji
added on the 2013-03-17 04:57:45 by reed reed
@ Saga Musix: :"D

@ kb_: I wonder why the original program with the SC on the top of this thread calculates exact values and my translated one doesn´t...
added on the 2013-03-17 14:31:06 by Freefall Freefall
Yeah, knowing how to deserialize binary files and how to use a debugger might help there.
added on the 2013-03-17 17:29:25 by kb_ kb_
I don´t need a debugger as I have the source code...
All I need is to understand how it works.

added on the 2013-03-17 17:35:51 by Freefall Freefall
I don´t need a debugger as I have the source code...

Yes, you need a debugger, because apparently you have no idea what you are doing. Gargaj actually gave you a nice hint how to fix your code. Did you read and understand it?
My actual translation:

added on the 2013-03-17 17:40:36 by Freefall Freefall