GLS.FileWAV.pas 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // The multimedia graphics platform GLScene https://github.com/glscene
  3. //
  4. unit GLS.FileWAV;
  5. (* Support for Windows WAV format. *)
  6. interface
  7. {$I GLScene.inc}
  8. uses
  9. Winapi.MMSystem,
  10. System.Classes,
  11. GLS.ApplicationFileIO,
  12. GLS.SoundFileObjects;
  13. type
  14. // Support for Windows WAV format.
  15. TGLWAVFile = class (TGLSoundFile)
  16. private
  17. {$IFDEF MSWINDOWS}
  18. waveFormat : TWaveFormatEx;
  19. pcmOffset : Integer;
  20. {$ENDIF}
  21. FPCMDataLength: Integer;
  22. data : array of Byte; // used to store WAVE bitstream
  23. protected
  24. public
  25. function CreateCopy(AOwner: TPersistent) : TGLDataFile; override;
  26. class function Capabilities : TGLDataFileCapabilities; override;
  27. procedure LoadFromStream(Stream: TStream); override;
  28. procedure SaveToStream(Stream: TStream); override;
  29. procedure PlayOnWaveOut; override;
  30. function WAVData : Pointer; override;
  31. function WAVDataSize : Integer; override;
  32. function PCMData : Pointer; override;
  33. function LengthInBytes : Integer; override;
  34. end;
  35. //------------------------------------------------------
  36. implementation
  37. //------------------------------------------------------
  38. {$IFDEF MSWINDOWS}
  39. type
  40. TRIFFChunkInfo = packed record
  41. ckID : FOURCC;
  42. ckSize : LongInt;
  43. end;
  44. const
  45. WAVE_Format_ADPCM = 2;
  46. {$ENDIF}
  47. // ------------------
  48. // ------------------ TGLWAVFile ------------------
  49. // ------------------
  50. function TGLWAVFile.CreateCopy(AOwner: TPersistent) : TGLDataFile;
  51. begin
  52. Result:=inherited CreateCopy(AOwner);
  53. if Assigned(Result) then begin
  54. {$IFDEF MSWINDOWS}
  55. TGLWAVFile(Result).waveFormat:=waveFormat;
  56. {$ENDIF}
  57. TGLWAVFile(Result).data := Copy(data);
  58. end;
  59. end;
  60. class function TGLWAVFile.Capabilities : TGLDataFileCapabilities;
  61. begin
  62. Result:=[dfcRead, dfcWrite];
  63. end;
  64. procedure TGLWAVFile.LoadFromStream(stream : TStream);
  65. {$IFDEF MSWINDOWS}
  66. var
  67. ck : TRIFFChunkInfo;
  68. dw, bytesToGo, startPosition, totalSize : Integer;
  69. id : Cardinal;
  70. dwDataOffset, dwDataSamples, dwDataLength : Integer;
  71. begin
  72. // this WAVE loading code is an adaptation of the 'minimalist' sample from
  73. // the Microsoft DirectX SDK.
  74. Assert(Assigned(stream));
  75. dwDataOffset:=0;
  76. dwDataLength:=0;
  77. // Check RIFF Header
  78. startPosition:=stream.Position;
  79. stream.Read(ck, SizeOf(TRIFFChunkInfo));
  80. Assert((ck.ckID=mmioStringToFourCC('RIFF',0)), 'RIFF required');
  81. totalSize:=ck.ckSize+SizeOf(TRIFFChunkInfo);
  82. stream.Read(id, SizeOf(Integer));
  83. Assert((id=mmioStringToFourCC('WAVE',0)), 'RIFF-WAVE required');
  84. // lookup for 'fmt '
  85. repeat
  86. stream.Read(ck, SizeOf(TRIFFChunkInfo));
  87. bytesToGo:=ck.ckSize;
  88. if (ck.ckID = mmioStringToFourCC('fmt ',0)) then begin
  89. if waveFormat.wFormatTag=0 then begin
  90. dw:=ck.ckSize;
  91. if dw>SizeOf(TWaveFormatEx) then
  92. dw:=SizeOf(TWaveFormatEx);
  93. stream.Read(waveFormat, dw);
  94. bytesToGo:=ck.ckSize-dw;
  95. end;
  96. // other 'fmt ' chunks are ignored (?)
  97. end else if (ck.ckID = mmioStringToFourCC('fact',0)) then begin
  98. if (dwDataSamples = 0) and (waveFormat.wFormatTag = WAVE_Format_ADPCM) then begin
  99. stream.Read(dwDataSamples, SizeOf(LongInt));
  100. Dec(bytesToGo, SizeOf(LongInt));
  101. end;
  102. // other 'fact' chunks are ignored (?)
  103. end else if (ck.ckID = mmioStringToFourCC('data',0)) then begin
  104. dwDataOffset:=stream.Position-startPosition;
  105. dwDataLength := ck.ckSize;
  106. Break;
  107. end;
  108. // all other sub-chunks are ignored, move to the next chunk
  109. stream.Seek(bytesToGo, soFromCurrent);
  110. until Stream.Position = 2048; // this should never be reached
  111. // Only PCM wave format is recognized
  112. // Assert((waveFormat.wFormatTag=Wave_Format_PCM), 'PCM required');
  113. // seek start of data
  114. pcmOffset:=dwDataOffset;
  115. FPCMDataLength:=dwDataLength;
  116. SetLength(data, totalSize);
  117. stream.Position:=startPosition;
  118. if totalSize>0 then
  119. stream.Read(data[0], totalSize);
  120. // update Sampling data
  121. with waveFormat do begin
  122. Sampling.Frequency:=nSamplesPerSec;
  123. Sampling.NbChannels:=nChannels;
  124. Sampling.BitsPerSample:=wBitsPerSample;
  125. end;
  126. {$ELSE}
  127. begin
  128. Assert(Assigned(stream));
  129. SetLength(data, stream.Size);
  130. if Length(data)>0 then
  131. stream.Read(data[0], Length(data));
  132. {$ENDIF}
  133. end;
  134. procedure TGLWAVFile.SaveToStream(stream: TStream);
  135. begin
  136. if Length(data)>0 then
  137. stream.Write(data[0], Length(data));
  138. end;
  139. procedure TGLWAVFile.PlayOnWaveOut;
  140. begin
  141. {$IFDEF MSWINDOWS}
  142. PlaySound(WAVData, 0, SND_ASYNC+SND_MEMORY);
  143. {$ENDIF}
  144. // GLSM.SoundFileObjects.PlayOnWaveOut(PCMData, LengthInBytes, waveFormat);
  145. end;
  146. function TGLWAVFile.WAVData : Pointer;
  147. begin
  148. if Length(data)>0 then
  149. Result:=@data[0]
  150. else Result:=nil;
  151. end;
  152. function TGLWAVFile.WAVDataSize : Integer;
  153. begin
  154. Result:=Length(data);
  155. end;
  156. function TGLWAVFile.PCMData : Pointer;
  157. begin
  158. {$IFDEF MSWINDOWS}
  159. if Length(data)>0 then
  160. Result:=@data[pcmOffset]
  161. else Result:=nil;
  162. {$ELSE}
  163. Result:=nil;
  164. {$ENDIF}
  165. end;
  166. function TGLWAVFile.LengthInBytes : Integer;
  167. begin
  168. Result:=FPCMDataLength;
  169. end;
  170. initialization
  171. RegisterSoundFileFormat('wav', 'Windows WAV files', TGLWAVFile);
  172. end.