OSDN Git Service

完成です
[sample-delphi/sample-DELPHI.git] / wav_proj.dpr
1 program wav_proj;
2
3 {$APPTYPE CONSOLE}
4 {$R *.res}
5
6 uses
7   System.SysUtils,
8   System.Classes,
9   wav in 'wav.pas',
10   WriteHeader in 'WriteHeader.pas';
11
12 function effect8BitWav(fpIn, fpOut: TFileStream; sizeOfData: Word): integer;
13 var
14   i: integer;
15   s: Single;
16   c: array [0 .. 1] of Byte;
17 begin
18   result := 0;
19   i := 0;
20   s := sizeOfData / SizeOf(c);
21   while i < s do
22   begin
23     try
24       fpIn.ReadBuffer(c, SizeOf(c));
25       c[0] := 128;
26       fpOut.WriteBuffer(c, SizeOf(c));
27     except
28       result := -1;
29       break;
30     end;
31     Writeln(c[0], ',', c[1]);
32     inc(i);
33   end;
34 end;
35
36 function effect16BitWav(fpIn, fpOut: TFileStream; sizeOfData: integer)
37   : integer;
38 var
39   i: integer;
40   s: Single;
41   c: array [0 .. 1] of ShortInt;
42 begin
43   result := 0;
44   i := 0;
45   s := sizeOfData / SizeOf(c);
46   while i < s do
47   begin
48     try
49       fpIn.ReadBuffer(c, SizeOf(c));
50       c[0] := 0;
51       fpOut.WriteBuffer(c, SizeOf(c));
52     except
53       result := -1;
54       break;
55     end;
56     Writeln(c[0], ',', c[1]);
57     inc(i);
58   end;
59 end;
60
61 function wavDataWrite(fpIn, fpOut: TFileStream; posOfData, sizeOfData: LongInt;
62   bytesPerSingleCh: SmallInt): integer;
63 begin
64   fpIn.Seek(posOfData, soFromCurrent);
65   if bytesPerSingleCh = 1 then
66     result := effect8BitWav(fpIn, fpOut, sizeOfData)
67   else
68     result := effect16BitWav(fpIn, fpOut, sizeOfData);
69 end;
70
71 function wavWrite(inFile, outFile: PChar; sampRate: LongWord; sampBits: Word;
72   posOfData, sizeOfData: integer): integer;
73 var
74   bytesPerSingleCh: Word;
75   fpIn, fpOut: TFileStream;
76 begin
77   try
78     if FileExists(inFile) = true then
79       fpIn := TFileStream.Create(inFile, fmOpenRead)
80     else
81     begin
82       result := -1;
83       Writeln(inFile, '\82ð\83I\81[\83v\83\93\82Å\82«\82Ü\82¹\82ñ');
84       Exit;
85     end;
86     fpOut := TFileStream.Create(outFile, fmCreate);
87     bytesPerSingleCh := sampBits div 8;
88     if waveHeaderWrite(fpOut, sizeOfData, bytesPerSingleCh, sampRate, sampBits)
89       = -1 then
90     begin
91       result := -1;
92       Writeln('\83w\83b\83_\82ð\8f\91\82«\8d\9e\82ß\82Ü\82¹\82ñ');
93       Exit;
94     end;
95     if wavDataWrite(fpIn, fpOut, posOfData, sizeOfData, bytesPerSingleCh) = -1
96     then
97     begin
98       result := -1;
99       Write('\83G\83\89\81[\94­\90¶');
100       Exit;
101     end;
102   finally
103     fpIn.Free;
104     fpOut.Free;
105   end;
106   result:=0;
107 end;
108
109 var
110   sampRate, sampBits: SmallInt;
111   posOfData, sizeOfData: LongWord;
112
113 begin
114   try
115     { TODO -oUser -cConsole \83\81\83C\83\93 : \82±\82±\82É\83R\81[\83h\82ð\8bL\8fq\82µ\82Ä\82­\82¾\82³\82¢ }
116     wavHdrRead(PChar(ParamStr(1)), sampRate, sampBits, posOfData, sizeOfData);
117     wavWrite(PChar(ParamStr(1)), PChar(ParamStr(2)), sampRate, sampBits,
118       posOfData, sizeOfData);
119     Writeln('\8a®\97¹');
120   except
121     on E: Exception do
122       Writeln(E.ClassName, ': ', E.Message);
123   end;
124
125 end.