summaryrefslogtreecommitdiff
path: root/plugins/Watrack/formats/fmt_mpc.pas
blob: 7d8671dde6aa9abeda48d16e494f082d47224db8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
{MPC file format}
unit fmt_MPC;
{$include compilers.inc}

interface
uses wat_api;

function ReadMPC(var Info:tSongInfo):boolean; cdecl;

implementation
uses windows,common,io,tags,srv_format;

const
  DefID = $002B504D;// 'MP+'

function ReadMPC(var Info:tSongInfo):boolean; cdecl;
var
  f:THANDLE;
  tmp:array [0..5] of dword;
  samples,TotalFrames:dword;
  lastframe:dword;
begin
  result:=false;
  f:=Reset(Info.mfile);
  if f=THANDLE(INVALID_HANDLE_VALUE) then
    exit;
  ReadID3v2(f,Info);

  BlockRead(f,tmp,SizeOf(tmp));
  if ((tmp[0] and $FFFFFF)=DefID) and
     (((tmp[0] shr 24) and $0F)>=7) then // sv7-sv8
  begin
    Info.kbps:=0;
    if (tmp[2] and 2)<>0 then
      Info.channels:=2
    else
      Info.channels:=1;
    case (tmp[2] and $3000) shr 12 of //C000-14?
      00: Info.khz:=44100;
      01: Info.khz:=48000;
      02: Info.khz:=37800;
      03: Info.khz:=32000;
    end;
    lastframe:=(tmp[5] and $FFF) shr 1;
    samples:=tmp[1]*1152+lastframe;
  end
  else
  begin //4-6
    if not ((tmp[0] and $1FFF) and $3FF) in [4..6] then
      exit;
    Info.khz:=44100;
    Info.kbps:=tmp[1] and $1F;
    if ((tmp[0] and $1FFF) and $3FF)=4 then
      TotalFrames:=loword(tmp[2])
    else
      TotalFrames:=tmp[2];
    samples:=TotalFrames*1152;
  end;

  if Info.khz<>0 then
    Info.total:=samples div Info.khz;
  Info.khz:=Info.khz div 1000;
  if (Info.kbps=0) and (samples<>0) then
// if fs=samples*channels*deep/8 then kbps=khz*deep*channels/1152
// Info.kbps:=(Info.khz*8)*taginfo.FileSize/1152/samples;

    Info.kbps:=(Info.khz div 8)*FileSize(f) div samples;  //!!
  ReadAPEv2(f,Info);
  ReadID3v1(f,Info);
  CloseHandle(f);
  result:=true;
end;

var
  LocalFormatLink:twFormat;

procedure InitLink;
begin
  LocalFormatLink.Next:=FormatLink;

  LocalFormatLink.This.proc :=@ReadMPC;
  LocalFormatLink.This.ext  :='MPC';
  LocalFormatLink.This.flags:=0;

  FormatLink:=@LocalFormatLink;
end;

initialization
  InitLink;
end.