OSDN Git Service

chapter 9-2
[sample-delphi/sample-DELPHI.git] / wav_proj.dpr
index a93819b..1d65e61 100644 (file)
@@ -6,82 +6,118 @@ program wav_proj;
 uses
   System.SysUtils,
   System.Classes,
-  wav in 'wav.pas',
-  WriteHeader in 'WriteHeader.pas';
+  spWav in 'spWav.pas';
 
-function cut(fpIn, fpOut: TFileStream; sp: SpParam): integer;
-begin
-
-end;
-
-function checkRange(var sp: SpParam): integer;
+function wavDataWrite(fpOut: TFileStream; const sp: SpParam): integer;
+var
+  i: integer;
+  s, tempsamplePerCycle, deltaAdd, curLevel: Single;
+  curSampling, samplePerCycle: LongInt;
+  c: array [0..1] of ShortInt;
 begin
-  result := 0;
-  if sp.startpos * sp.bytesPerSec > sp.sizeOfData then
+  tempsamplePerCycle:=sp.samplePerSec*sp.cycleuSec div 1000000;
+  samplePerCycle:=Trunc(tempsamplePerCycle);
+  if samplePerCycle <= 0 then
   begin
-    Writeln('\8aJ\8en\88Ê\92u\82ª\83t\83@\83C\83\8b\83T\83C\83Y\82ð\92´\82¦\82Ä\82¢\82Ü\82·');
-    result := -1;
-  end
-  else if (sp.endpos + 1) * sp.bytesPerSec > sp.sizeOfData then
+    Writeln('\8eü\94g\90\94\82ª\8d\82\82·\82¬');
+    result:=-1;
+    Exit;
+  end;
+  deltaAdd:=65535/samplePerCycle;
+  curLevel:=0;
+  curSampling:=0;
+  i:=0;
+  s:=sp.sizeOfData/SizeOf(@c);
+  while i < s do
   begin
-    Writeln('\8fI\97¹\88Ê\92u\82ª\83t\83@\83C\83\8b\83T\83C\83Y\82ð\92´\82¦\82Ä\82¢\82Ü\82·');
-    Writeln('\8fI\97¹\82ð\83t\83@\83C\83\8b\82Ì\8dÅ\8cã\82É\92²\90®\82µ\82Ü\82µ\82½');
-    sp.endpos := (sp.sizeOfData div sp.bytesPerSec) - 1;
+    inc(i);
+    c[0]:=ShortInt(Trunc(curLevel-32788));
+    c[1]:=c[0];
+    fpOut.WriteBuffer(c,SizeOf(@c));
+    curLevel:=curLevel+deltaAdd;
+    inc(curSampling);
+    if curSampling > samplePerCycle then
+    begin
+      curLevel:=0;
+      curSampling:=0;
+    end;
   end;
 end;
 
-function wavDataWrite(fpIn, fpOut: TFileStream; sp: SpParam): integer;
-begin
-  fpIn.Position := sp.posOfData;
- // fpOut.Position := sp.posOfData;
-  result:=cut(fpIn,fpOut,sp);
-end;
-
-function wavWrite(inFile, outFile: PChar; sp: SpParam): integer;
+function wavWrite(outFile: PChar; const wHdr: WrSWaveFileHeader;
+  var sp: SpParam): integer;
 var
   fpIn, fpOut: TFileStream;
 begin
+  result := 0;
   try
-    fpIn := TFileStream.Create(inFile, fmOpenRead);
     fpOut := TFileStream.Create(outFile, fmCreate);
-    sp.sizeOfData := (sp.endpos - sp.startpos + 1) * sp.bytesPerSec;
-    if waveHeaderWrite(fpOut, sp) > 44 then
-      raise EWriteError.Create('\83w\83b\83_\82ð\8f\91\82«\8d\9e\82ß\82Ü\82¹\82ñ');
-    if wavDataWrite(fpIn, fpOut, sp) = -1 then
-      raise EWriteError.Create('\83G\83\89\81[\94­\90¶');
+    fpOut.WriteBuffer(wHdr, SizeOf(WrSWaveFileHeader));
+    if wavDataWrite(fpOut, sp) = -1 then
+      raise EWriteError.Create('');
   except
     on EFOpenError do
-      Writeln(inFile, '\82ð\83I\81[\83v\83\93\82Å\82«\82Ü\82¹\82ñ');
-    on EFOpenError do
-      fpIn.Free;
-    else
-
     begin
-      fpIn.Free;
-      fpOut.Free;
+      Writeln(outFile, '\82ð\83I\81[\83v\83\93\82Å\82«\82Ü\82¹\82ñ');
+      result := -1;
+    end;
+    on EWriteError do
+    begin
+      Writeln('\83w\83b\83_\82ð\8f\91\82«\8d\9e\82ß\82Ü\82¹\82ñ');
+      result := -1;
     end;
-    result := -1;
-    Exit;
   end;
-  result := 0;
+  fpOut.Free;
+end;
+
+procedure usage;
+begin
+  Writeln('\82Ì\82±\82¬\82è\94g');
+  Writeln('\97á\81Feffect.wav 100 2000');
+end;
+
+procedure setupHeader(var wHdr: WrSWaveFileHeader; var sp: SpParam);
+var
+  bytes: Byte;
+begin
+  wHdr.hdrRiff := STR_RIFF;
+  wHdr.sizeOfFile := sp.sizeOfData + SizeOf(WrSWaveFileHeader) - 8;
+  wHdr.hdrWave := STR_WAVE;
+  wHdr.hdrFmt := STR_fmt;
+  wHdr.sizeOfFmt := SizeOf(tWaveFormatPcm);
+  wHdr.stWaveFormat.formatTag := 1;
+  wHdr.stWaveFormat.channels := sp.channels;
+  wHdr.stWaveFormat.sampleParSec := sp.samplePerSec;
+  bytes := sp.bitsPerSample div 8;
+  wHdr.stWaveFormat.bytesPerSec := bytes * sp.channels * sp.samplePerSec;
+  wHdr.stWaveFormat.blockAlign := bytes * sp.channels;
+  wHdr.stWaveFormat.bitsPerSample := sp.bitsPerSample;
+  wHdr.hdrData := STR_data;
+  wHdr.sizeOfData := sp.sizeOfData;
 end;
 
 var
   sp: SpParam;
+  totalLength: integer;
+  hdrHeader: WrSWaveFileHeader;
 
 begin
   try
     { TODO -oUser -cConsole \83\81\83C\83\93 : \82±\82±\82É\83R\81[\83h\82ð\8bL\8fq\82µ\82Ä\82­\82¾\82³\82¢ }
-    sp.startpos := LongInt(ParamStr(3));
-    sp.endpos := LongInt(ParamStr(4));
-    if sp.startpos > sp.endpos then
+    if ParamCount <> 3 then
     begin
-      Writeln('\8aJ\8en\95b\82Í\8fI\97¹\95b\82ð\92´\82¦\82Ä\82Í\82È\82è\82Ü\82¹\82ñ');
+      usage;
       Exit;
     end;
-    if wavHdrRead(PChar(ParamStr(1)), sp) = -1 then
-      Exit;
-    if wavWrite(PChar(ParamStr(1)), PChar(ParamStr(2)), sp) = -1 then
+    totalLength := StrToInt(ParamStr(2));
+    sp.cycleuSec := StrToInt(ParamStr(3));
+    sp.channels := WAV_STEREO;
+    sp.samplePerSec := 44100;
+    sp.bitsPerSample := 16;
+    sp.sizeOfData := sp.bitsPerSample * sp.channels * sp.samplePerSec *
+      totalLength div 8;
+    setupHeader(hdrHeader, sp);
+    if wavWrite(PChar(ParamStr(1)), hdrHeader, sp) = -1 then
       Exit;
     Writeln('\8a®\97¹');
   except