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;
interface
uses Windows, Messages, KOL;
function GetV2Minfo(v2mfile:string):dword;
implementation
function GetV2Minfo(v2mfile:string):dword;
var
tdl, tdh, tdh2, tsign, tsigd, tpq:byte;
fst:PStream;
fract, maxt, gevsno, usecs, ttime:dword;
begin
if (fileexists(v2mfile)) then begin
fst:=NewReadFileStream(v2mfile);
fst.Read(fract,4);
fst.Read(maxt, 4);
fst.Read(gevsno,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;
end;
fst.Free;
result:=ttime;
end;
end.
-------------------------------------------------------------------------------
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.?
I´d like to calculate the playback time of v2m files, just as someone did with this source:
-------------------------------------------------------------------------------
unit v2minfo;
interface
uses Windows, Messages, KOL;
function GetV2Minfo(v2mfile:string):dword;
implementation
function GetV2Minfo(v2mfile:string):dword;
var
tdl, tdh, tdh2, tsign, tsigd, tpq:byte;
fst:PStream;
fract, maxt, gevsno, usecs, ttime:dword;
begin
if (fileexists(v2mfile)) then begin
fst:=NewReadFileStream(v2mfile);
fst.Read(fract,4);
fst.Read(maxt, 4);
fst.Read(gevsno,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;
end;
fst.Free;
result:=ttime;
end;
end.
-------------------------------------------------------------------------------
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.?
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;
fclose(f);
<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;
fclose(f);
What does "fract" and "<<" mean? C# code please :(
uh, fract was unit32 variable^^ but still don´t come along with These Operators...
<< 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.
freefall - just in case its not clear fract is the first 4 bytes of the file not a command
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.
btw bit shift left 3 is basically a cheat to * 4 i think
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
-------------------------------------------------------------
-------------------------------------------------------------
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
-------------------------------------------------------------
Output: 2,00069459375
V2m duration: 2m 45s
V2m duration: 2m 45s
try
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
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
Thank you very much Sir Coding Hero :) <3 Works now. Btw: cool synth you made, too :) Like the conspiracy Demos. Have fun!
Aaargh! Still has some mistakes in it. Sometimes it calculates almost right values, sometimes it calcs a mistake range of +- 5 or +-7 seconds....
Update: mistake range is + 5 or +7 seconds....
Quote:
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
.Net languages (VB.Net, C#) also have bit shift operators, you know?
Ok, this one saddens me even more.
Quote:
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
ofc.
@ 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...
@ 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...
Yeah, knowing how to deserialize binary files and how to use a debugger might help there.
I don´t need a debugger as I have the source code...
All I need is to understand how it works.
SC:
http://keygenmusic.net/soft/MODlistv0.34src.rar
All I need is to understand how it works.
SC:
http://keygenmusic.net/soft/MODlistv0.34src.rar
Quote:
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:
ftp://www.untergrund.net/users/Freefall/News%20Section/V2m%20duration.rar
ftp://www.untergrund.net/users/Freefall/News%20Section/V2m%20duration.rar