home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 1995 May / pcw-0595.bin / demos / databeck / wsounds / setup.dir / wswsrc.exe / WAVEIO.PAS < prev    next >
Pascal/Delphi Source File  |  1992-12-02  |  17KB  |  483 lines

  1. {
  2.           Unit WAVEIO
  3.           WAV file input and output
  4. }
  5.  
  6.  
  7. Unit WAVEIO;
  8.  
  9. Interface
  10.  
  11. Uses WObjects, WinDos, Wincrt, WinTypes, WinProcs, Strings, MMSystem, BWCC,
  12.      WaveUtil, StrTool, DataObj, RiffComm;
  13.  
  14. FUNCTION  WD_GetFileInfo(HW : Hwnd; VAR WData : WaveDataType) : BOOLEAN;
  15. PROCEDURE WD_ReadRecDir(HW : HWnd;SPt : DirStr; Lvl : Word; CurrCollect : PWaveCollection);
  16. PROCEDURE WD_ReadOneDir(HW : HWnd;DPath : DirStr;Level : Word; CurrCollect : PWaveCollection);
  17. PROCEDURE WD_ReadOneFile(HW : HWnd;DPath : DirStr; Pfile : PChar;Level : Word; CurrCollect : PWaveCollection);
  18. {Direct DirStr?}
  19. PROCEDURE WD_ReadOneFileIntoWD(HW : HWnd;DPath : DirStr; PFile : Pchar;VAR WDum : WaveDataType);
  20.  
  21. IMPLEMENTATION
  22.  
  23. {----------------------------------------------------------------------------------------------}
  24.  
  25. PROCEDURE GetFileInfoRIFF (HW : Hwnd;Var Data : WaveDataType; VAR MMessage : INTEGER );
  26. Var InFile : THMMIO;
  27.     Parent : TMMCKINFO;
  28.     Infos  : TMMIOInfo;
  29.     Result : Integer;
  30.     FileP : Array[0..140] of char;
  31. Begin
  32. FileP[0] := Data.DiskDrive;
  33. FileP[1] := #0;
  34. StrCat  (FileP,':');
  35. StrCat  (FileP,Data.PathName);
  36. StrCat  (FileP,Data.FileName);
  37. InFile := mmioOpen (FileP,NIL,MMIO_READ);
  38. IF InFile <> 0 then
  39.  Begin
  40.   FillChar(Parent,SizeOf(Parent),0);
  41.   Parent.fccType := mmioStringToFourCC ('RIFF',0);
  42.   Result := mmioDescend (InFile,@Parent,NIL,MMIO_FINDLIST);
  43.   if result = 0 then
  44.    Begin
  45.     MMessage := MMReadOK;
  46.     mmioRead (InFile,@Data.FileComment,64);
  47.    End
  48.   ELSE BEGIN
  49.      MMessage := MMReadNoSuccess;
  50.      END;
  51. End
  52. ELSE BEGIN
  53.    MMessage := MMReadCancel;
  54.    END;
  55. mmioClose (InFile,0);
  56. End;
  57.  
  58. FUNCTION WD_GetFileInfo(HW : Hwnd; VAR WData : WaveDataType) : BOOLEAN;
  59. VAR
  60.    TDummy  : ARRAY[0..255] OF Char;
  61.    MDummy  : ARRAY[0..255] OF Char;
  62.    WStream : TDosStream;
  63.    CName   : FourCC;
  64.    DSR     : WORD;
  65.    DChan   : BYTE; 
  66. BEGIN
  67.    TDummy[0] := WData.DiskDrive;
  68.    TDummy[1] := #0;
  69. {   WriteLn(TDummy);}
  70.    DSR := 0;
  71.    DChan := 1;
  72.    StrCat(TDummy, ':');
  73. {   WriteLn(TDummy);}
  74.    StrCat(TDummy,WData.PathName);
  75.    StrCat(TDummy,WData.FileName);
  76. {   WriteLn(TDummy);}
  77.    IF WAVOpenFile(WStream, TDummy) = FALSE THEN BEGIN
  78.       StrCopy(MDummy,'Error while opening file ');
  79.       StrLCat(MDummy,TDummy, SizeOf(MDummy)-1);
  80.       StrCat(MDummy,', or file cannot be handled as a RIFF WAV file.');
  81.       BWCCMessageBox(HW, MDummy,'Wicked Sounds for Windows: Note', mb_Ok + mb_IconExclamation);
  82.       WAVCloseFile(WStream);
  83.       WD_GetFileInfo := FALSE;
  84.       Exit;
  85.       END;
  86.    IF WAVSkip2fmtChunk(WStream, CName) = FALSE THEN BEGIN
  87.       StrCopy(MDummy,TDummy);
  88.       StrCat(MDummy, #10#13);
  89.       StrCat(MDummy,'WAV information could not be read. WAV file may be corrupt.');
  90.       BWCCMessageBox(HW, MDummy,'Wicked Sounds for Windows: Note', mb_Ok + mb_IconExclamation);
  91.       WD_GetFileInfo := FALSE;
  92.       WAVCloseFile(WStream);
  93.       Exit;
  94.       END
  95.    ELSE IF StrLComp(CName, 'fmt ',4) = 0 THEN BEGIN
  96.          IF (WAVGetInfo(WStream, DSR, DChan) = FALSE) THEN BEGIN
  97.             StrCopy(MDummy,'WAV information could not be read. WAV file may be corrupt.');
  98.             StrLCat(MDummy,TDummy, StrLen(TDummy));
  99.             BWCCMessageBox(HW, MDummy,'Wicked Sounds for Windows: Note', mb_Ok + mb_IconExclamation);
  100.             WD_GetFileInfo := FALSE;
  101.             END;
  102.          END;
  103.    WData.SampRate := DSR;
  104.    WData.Channels := DChan;
  105.    WAVCloseFile(WStream);
  106.    END;
  107. {----------------------------------------------------------------------------------------------}
  108.  
  109. PROCEDURE WD_ReadRecDir(HW : HWnd;SPt : DirStr; Lvl : Word; CurrCollect : PWaveCollection);
  110. {
  111.  *** Input     : SPt : String with starting path
  112.                  Lvl : Starting level in directory tree (not needed for function)
  113.                  CurrCollect : Pointer to collection in which insertion should be made
  114.  *** Output    : None
  115.  *** Remarks   : Places all *.WAV files from current directory in the collection
  116. }
  117.  
  118. VAR
  119.    DInfo : TSearchRec;
  120.    Dummy : DirStr;
  121.    PPasStr : String[12];
  122.    VDummy : String[12];
  123.    VolID : Array[0..12] of Char;
  124.    Message : Integer;
  125.  
  126. Procedure ReadRecDir(DPath : DirStr;Level : Word);
  127. VAR
  128.    DirInfo : TSearchRec;   { DIR information buffer                      }
  129.    Dummy : DirStr;         { Path transfer dummy                         }
  130.    PasStr : String;        { Dummy for Pascal string operations          }
  131.    PasPath : String[128];
  132.    PWTest : PWaveData;     { Dummy for data parameter in TWaveData inst. }
  133. BEGIN
  134. { Read current DIR info }
  135.    StrCopy(Dummy,DPath);
  136.    StrLCat(Dummy,WildCard, SizeOf(Dummy)-1);
  137.    FindFirst(Dummy,faAnyFile,DirInfo);
  138.  
  139. { Data available? Continue }
  140.    While DosError = 0 DO BEGIN
  141.  
  142. { Is the file a subdirectory other than "." and ".."? }
  143.       IF ((DirInfo.Name[0] <> '.') AND (DirInfo.Attr = faDirectory)) THEN BEGIN
  144.          StrCopy(Dummy,DPath);
  145.          StrLCat(Dummy,DirInfo.Name, SizeOf(Dummy)-1);
  146.          StrLCat(Dummy,'\', SizeOf(Dummy)-1);
  147.          SetDlgItemText(HW, 1100, Dummy);
  148.          {WriteLn('Level ',Level,' - Directory : ', Dummy);}
  149.  
  150. { Yes-> Continue recursion and read next subdirectory }
  151.          ReadRecDir(Dummy, Level+1);
  152.          END;
  153.  
  154. { Prepare file information for transfer to list }
  155.       WITH DirInfo DO BEGIN
  156.          PasStr := StrPas(Name);
  157.          IF ((Name[0] <> '.') AND (Attr <> faDirectory)) THEN
  158.             IF Copy(PasStr,Pos('.',PasStr)+1,Length(PasStr)) = ChooseWild THEN BEGIN
  159.                FillChar(WaveDummy,SizeOf(WaveDummy),0);
  160.                StrCopy(WaveDummy.FileName, Name);
  161.  
  162.                PasPath := StrPas(DPath);
  163.                Delete(PasPath,1,2);
  164.                StrPCopy(Dummy, PasPath);
  165.                StrUpper(Dummy);
  166.                StrCopy(WaveDummy.PathName, Dummy);
  167.                WaveDummy.CreationDate := Time;
  168.                WaveDummy.FileSize     := Size;
  169.                {StrCopy(WaveDummy.FileComment, '.');}
  170.                WaveDummy.FileComment[0] := #0;
  171.                StrCopy(WaveDummy.DiskLabel, VolID);
  172.                WaveDummy.DiskDrive    := UpCase(DPath[0]);
  173.                WaveDummy.Save2Wave := 0;
  174.  
  175.                SetDlgItemText(HW, 1101, Name);
  176. { Read sample rate and channels from file }
  177.                IF (WD_GetFileInfo(HW, WaveDummy) = TRUE) THEN BEGIN
  178.                   GetRiffCommentICMT(WaveDummy, Message);
  179.                   IF Message = MMReadNoSuccess THEN GetRiffCommentINAM(WaveDummy, Message);
  180.                   IF Message = MMReadOK THEN WaveDummy.Save2Wave := C2W_IsSaved
  181.                   ELSE WaveDummy.Save2Wave := C2W_DontSave;
  182.                   Message := MMReadCancel;
  183.                   PWTest := New(PWaveData, Init(WaveDummy));
  184.                   CurrCollect^.Insert(PWTest);
  185.                   END;
  186.                END;
  187.          END;
  188. { Read next directory entry }
  189.       FindNext(DirInfo);
  190.       END;
  191.    END;
  192. BEGIN
  193. { Read volume label }
  194.    StrCopy(Dummy,SPt);
  195.    StrLCat(Dummy,WildCard, SizeOf(Dummy)-1);
  196.    DInfo.Name[0] := #0;
  197.    FindFirst(Dummy,faVolumeID,DInfo);
  198.  
  199. { Convert volume label }
  200.    IF (StrLen(DInfo.Name)>0) THEN BEGIN
  201.       PPasStr := StrPas(DInfo.Name);
  202.       IF Pos('.',PPasStr) > 0 THEN Delete(PPasStr,Pos('.',PPasStr),1);
  203.       StrPCopy(VolID, PPasStr);
  204.       StrUpper(VolID);
  205.       VolId[11] := #0;
  206.       VolId[12] := #0;
  207.       END
  208.    ELSE
  209.       StrCopy(VolID,'No label');
  210.  
  211. { Start file search }
  212.    ReadRecDir(SPt,Lvl);
  213.  
  214.    END;
  215. {----------------------------------------------------------------------------------------------}
  216. PROCEDURE WD_ReadOneDir(HW : HWnd;DPath : DirStr;Level : Word; CurrCollect : PWaveCollection);
  217. {
  218.  *** Input     : DPath : String with starting path
  219.                  Level : Starting level in directory tree (not needed for function)
  220.                  CurrCollect : Pointer to collection in which insertion should be made
  221.  *** Output    : None
  222.  *** Remarks   : Places all *.WAV files from current directory in the collection
  223.                  For code commentary, see WD_ReadRecDir
  224. }
  225.  
  226. VAR
  227.    DirInfo : TSearchRec;
  228.    Dummy : DirStr;
  229.    PasStr : String[12];
  230.    PPasStr : String[12];
  231.    PasPath : String[128];
  232.    PWTest : PWaveData;
  233.    VolID : Array[0..12] of Char;
  234.    Message : Integer;
  235. BEGIN
  236.    StrCopy(Dummy,DPath);
  237.    StrLCat(Dummy,WildCard, SizeOf(Dummy)-1);
  238.    DirInfo.Name[0] := #0;
  239.    FindFirst(Dummy,faVolumeID,DirInfo);
  240. { Convert volume label }
  241.    IF (StrLen(DirInfo.Name)>0) THEN BEGIN
  242.       PasStr := StrPas(DirInfo.Name);
  243.       IF Pos('.',PasStr) > 0 THEN Delete(PasStr,Pos('.',PasStr),1);
  244.       StrPCopy(VolID, PasStr);
  245.       StrUpper(VolID);
  246.       VolId[11] := #0;
  247.       VolId[12] := #0;
  248.       END
  249.    ELSE
  250.       StrCopy(VolID,'No label');
  251.  
  252. {
  253.    StrCopy(Dummy,DPath);
  254.    StrLCat(Dummy,WildCard, SizeOf(Dummy)-1);
  255. }
  256.    SetDlgItemText(HW, 1100, Dummy);
  257.    FindFirst(Dummy,faAnyFile,DirInfo);
  258.    While DosError = 0 DO BEGIN
  259.       WITH DirInfo DO BEGIN
  260.          PasStr := StrPas(Name);
  261.          IF ((Name[0] <> '.') AND (Attr <> faDirectory)) THEN
  262.             IF Copy(PasStr,Pos('.',PasStr)+1,Length(PasStr)) = ChooseWild THEN BEGIN
  263.                FillChar(WaveDummy,SizeOf(WaveDummy),0);
  264.                StrCopy(WaveDummy.FileName, Name);
  265.  
  266.                PasPath := StrPas(DPath);
  267.                Delete(PasPath,1,2);
  268.                StrPCopy(Dummy, PasPath);
  269.                StrUpper(Dummy);
  270.                StrCopy(WaveDummy.PathName, Dummy);
  271.  
  272.                WaveDummy.CreationDate := Time;
  273.                WaveDummy.FileSize     := Size;
  274.  
  275.                {StrCopy(WaveDummy.FileComment, '.');}
  276.  
  277.                WaveDummy.FileComment[0] := #0;
  278.  
  279.                StrCopy(WaveDummy.DiskLabel, VolID);
  280.  
  281.                WaveDummy.DiskDrive    := UpCase(DPath[0]);
  282.                WaveDummy.Save2Wave := 0;
  283.  
  284. { Read sample rate and channels from file }
  285.  
  286.                SetDlgItemText(HW, 1101, Name);
  287.                IF (WD_GetFileInfo(HW, WaveDummy) = TRUE) THEN BEGIN
  288.                   GetRiffCommentICMT(WaveDummy, Message);
  289.                   IF Message = MMReadNoSuccess THEN GetRiffCommentINAM(WaveDummy, Message);
  290.                   IF Message = MMReadOK THEN WaveDummy.Save2Wave := C2W_IsSaved
  291.                   ELSE WaveDummy.Save2Wave := C2W_DontSave;
  292.                   Message := MMReadCancel;
  293.                   PWTest := New(PWaveData, Init(WaveDummy));
  294.                   CurrCollect^.Insert(PWTest);
  295.                   END;
  296.                END;
  297.          END;
  298.       FindNext(DirInfo);
  299.       END;
  300.    END;
  301. {----------------------------------------------------------------------------------------------}
  302. PROCEDURE WD_ReadOneFile(HW : HWnd;DPath : DirStr; Pfile : PChar;Level : Word; CurrCollect : PWaveCollection);
  303. {
  304.  *** Input     : DPath : String with starting path
  305.                  PFile: Search file name
  306.                  Level : Starting level in directory tree (not needed for function)
  307.                  CurrCollect : Pointer to collection in which insertion should be made
  308.  *** Output    : None
  309.  *** Remarks   : Places all *.WAV files from current directory in the collection
  310.                  For code commentary, see WD_ReadRecDir
  311. }
  312.  
  313. VAR
  314.    DirInfo : TSearchRec;
  315.    Dummy : DirStr;
  316.    PasStr : String[12];
  317.    PasPath : String[128];
  318.    PWTest : PWaveData;
  319.    VolID : Array[0..12] of Char;
  320.    count : Integer;
  321.    Message : Integer;
  322. BEGIN
  323.    StrCopy(Dummy,DPath);
  324.    StrLCat(Dummy,WildCard, SizeOf(Dummy)-1);
  325.    DirInfo.Name[0] := #0;
  326.    FindFirst(Dummy,faVolumeID,DirInfo);
  327. {
  328.    StrCopy(Dummy,DPath);
  329.    StrCat(Dummy,'*.*');
  330.    For Count := 0 to StrLen(DirInfo.Name) DO DirInfo.Name[Count] := #0;
  331. }
  332.    FindFirst(Dummy,faVolumeID,DirInfo);
  333.  
  334.    { Convert volume label }
  335.    IF (StrLen(DirInfo.Name)>0) THEN BEGIN
  336.       PasStr := StrPas(DirInfo.Name);
  337.       IF Pos('.',PasStr) > 0 THEN Delete(PasStr,Pos('.',PasStr),1);
  338.       StrPCopy(VolID, PasStr);
  339.       StrUpper(VolID);
  340.       VolId[11] := #0;
  341.       VolId[12] := #0;
  342. {
  343.       For Count := 0 to StrLen(DirInfo.Name) DO BEGIN
  344.          IF (DirInfo.Name[Count] <> '.') THEN VolID[Count] := DirInfo.Name[Count];
  345.          END;
  346.       StrUpper(VolID);
  347. }
  348.       END
  349.    ELSE
  350.       StrCopy(VolID,'No label');
  351.  
  352.    StrCopy(Dummy,DPath);
  353.    StrCat(Dummy, PFile);
  354.  
  355.    FindFirst(Dummy,faAnyFile,DirInfo);
  356.    IF DosError = 0 THEN BEGIN
  357.       WITH DirInfo DO BEGIN
  358.          PasStr := StrPas(Name);
  359.          IF ((Name[0] <> '.') AND (Attr <> faDirectory)) THEN
  360.             IF Copy(PasStr,Pos('.',PasStr)+1,Length(PasStr)) = ChooseWild THEN BEGIN
  361.                FillChar(WaveDummy,SizeOf(WaveDummy),0);
  362.                StrCopy(WaveDummy.FileName, Name);
  363.  
  364.                PasPath := StrPas(DPath);
  365.                Delete(PasPath,1,2);
  366.                StrPCopy(Dummy, PasPath);
  367.                StrUpper(Dummy);
  368.                StrCopy(WaveDummy.PathName, Dummy);
  369.  
  370.                WaveDummy.CreationDate := Time;
  371.                WaveDummy.FileSize     := Size;
  372.                {StrCopy(WaveDummy.FileComment, '.');}
  373.                WaveDummy.FileComment[0] := #0;
  374.  
  375.                StrCopy(WaveDummy.DiskLabel, VolID);
  376.  
  377.                WaveDummy.DiskDrive    := UpCase(DPath[0]);
  378.                WaveDummy.Save2Wave := 0;
  379.  
  380. {
  381.                WriteLn(WaveDummy.DiskDrive);
  382.                WriteLn(WaveDummy.PathName);
  383. }
  384. { Read sample rate and channels from file }
  385.                IF (WD_GetFileInfo(HW, WaveDummy) = TRUE) THEN BEGIN
  386.                   GetRiffCommentICMT(WaveDummy, Message);
  387.                   IF Message = MMReadNoSuccess THEN GetRiffCommentINAM(WaveDummy, Message);
  388.                   IF Message = MMReadOK THEN WaveDummy.Save2Wave := C2W_IsSaved
  389.                   ELSE WaveDummy.Save2Wave := C2W_DontSave;
  390.                   Message := MMReadCancel;
  391.                   PWTest := New(PWaveData, Init(WaveDummy));
  392.                   CurrCollect^.Insert(PWTest);
  393.                   END;
  394.                END;
  395.          END;
  396.       END;
  397.    END;
  398. {----------------------------------------------------------------------------------------------}
  399. PROCEDURE WD_ReadOneFileIntoWD(HW : HWnd;DPath : DirStr; PFile : Pchar;VAR WDum : WaveDataType);
  400. {
  401.  *** Input     : DPath : String with starting path
  402.                  PFile: Search file name
  403.  *** Output    : None
  404.  *** Remarks   : Places all *.WAV files from current directory in the collection
  405.                  For code commentary, see WD_ReadRecDir
  406. }
  407.  
  408. VAR
  409.    DirInfo : TSearchRec;
  410.    Dummy : DirStr;
  411.    PasStr : String[12];
  412.    PasPath : String[128];
  413.    PWTest : PWaveData;
  414.    VolID : Array[0..12] of Char;
  415.    count : Integer;
  416.    Message : Integer;
  417. BEGIN
  418.    StrCopy(Dummy,DPath);
  419.    StrCat(Dummy,'*.*');
  420.    For Count := 0 to StrLen(DirInfo.Name) DO DirInfo.Name[Count] := #0;
  421.  
  422.    FindFirst(Dummy,faVolumeID,DirInfo);
  423.  
  424.    { Convert volume label }
  425.    IF (StrLen(DirInfo.Name)>0) THEN BEGIN
  426.       PasStr := StrPas(DirInfo.Name);
  427.       IF Pos('.',PasStr) > 0 THEN Delete(PasStr,Pos('.',PasStr),1);
  428.       StrPCopy(VolID, PasStr);
  429.       StrUpper(VolID);
  430.       VolId[11] := #0;
  431.       VolId[12] := #0;
  432. {
  433.       For Count := 0 to StrLen(DirInfo.Name) DO BEGIN
  434.          IF (DirInfo.Name[Count] <> '.') THEN VolID[Count] := DirInfo.Name[Count];
  435.          END;
  436.       StrUpper(VolID);
  437. }
  438.       END
  439.    ELSE
  440.       StrCopy(VolID,'No label');
  441.  
  442.    StrCopy(Dummy,DPath);
  443.    StrCat(Dummy, PFile);
  444.    FindFirst(Dummy,faAnyFile,DirInfo);
  445.    IF DosError = 0 THEN BEGIN
  446.       WITH DirInfo DO BEGIN
  447.          PasStr := StrPas(Name);
  448.          IF ((Name[0] <> '.') AND (Attr <> faDirectory)) THEN
  449.             IF Copy(PasStr,Pos('.',PasStr)+1,Length(PasStr)) = ChooseWild THEN BEGIN
  450.                FillChar(WaveDummy,SizeOf(WaveDummy),0);
  451.                StrCopy(WaveDummy.FileName, Name);
  452.  
  453.                PasPath := StrPas(DPath);
  454.                Delete(PasPath,1,2);
  455.                StrPCopy(Dummy, PasPath);
  456.                StrUpper(Dummy);
  457.                StrCopy(WaveDummy.PathName, Dummy);
  458.  
  459.                WaveDummy.CreationDate := Time;
  460.                WaveDummy.FileSize     := Size;
  461.                {StrCopy(WaveDummy.FileComment, '.');}
  462.                WaveDummy.FileComment[0] := #0;
  463.  
  464.                StrCopy(WaveDummy.DiskLabel, VolID);
  465.  
  466.                WaveDummy.DiskDrive    := UpCase(DPath[0]);
  467.                WaveDummy.Save2Wave := 0;
  468.  
  469. { Read sample rate and channels from file }
  470.                IF (WD_GetFileInfo(HW, WaveDummy) = TRUE) THEN BEGIN
  471.                   GetRiffCommentICMT(WaveDummy, Message);
  472.                   IF Message = MMReadNoSuccess THEN GetRiffCommentINAM(WaveDummy, Message);
  473.                   IF Message = MMReadOK THEN WaveDummy.Save2Wave := C2W_IsSaved
  474.                   ELSE WaveDummy.Save2Wave := C2W_DontSave;
  475.                   Message := MMReadCancel;
  476.                   END;
  477.                END;
  478.          END;
  479.       END;
  480.    END;
  481. BEGIN
  482. END.
  483.