GLS.FileWAV.pas 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // The graphics engine GLScene
  3. //
  4. unit GLS.FileWAV;
  5. (* Support for Windows WAV format. *)
  6. interface
  7. {$I Stage.Defines.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
  54. begin
  55. {$IFDEF MSWINDOWS}
  56. TGLWAVFile(Result).waveFormat := waveFormat;
  57. {$ENDIF}
  58. TGLWAVFile(Result).data := Copy(data);
  59. end;
  60. end;
  61. class function TGLWAVFile.Capabilities: TGLDataFileCapabilities;
  62. begin
  63. Result := [dfcRead, dfcWrite];
  64. end;
  65. procedure TGLWAVFile.LoadFromStream(Stream: TStream);
  66. {$IFDEF MSWINDOWS}
  67. var
  68. ck: TRIFFChunkInfo;
  69. dw, bytesToGo, startPosition, totalSize: Integer;
  70. id: Cardinal;
  71. dwDataOffset, dwDataSamples, dwDataLength: Integer;
  72. begin
  73. // this WAVE loading code is an adaptation of the 'minimalist' sample from
  74. // the Microsoft DirectX SDK.
  75. Assert(Assigned(Stream));
  76. dwDataOffset := 0;
  77. dwDataLength := 0;
  78. // Check RIFF Header
  79. startPosition := Stream.Position;
  80. Stream.Read(ck, SizeOf(TRIFFChunkInfo));
  81. Assert((ck.ckID = mmioStringToFourCC('RIFF', 0)), 'RIFF required');
  82. totalSize := ck.ckSize + SizeOf(TRIFFChunkInfo);
  83. Stream.Read(id, SizeOf(Integer));
  84. Assert((id = mmioStringToFourCC('WAVE', 0)), 'RIFF-WAVE required');
  85. // lookup for 'fmt '
  86. repeat
  87. Stream.Read(ck, SizeOf(TRIFFChunkInfo));
  88. bytesToGo := ck.ckSize;
  89. if (ck.ckID = mmioStringToFourCC('fmt ', 0)) then
  90. begin
  91. if waveFormat.wFormatTag = 0 then
  92. begin
  93. dw := ck.ckSize;
  94. if dw > SizeOf(TWaveFormatEx) then
  95. dw := SizeOf(TWaveFormatEx);
  96. Stream.Read(waveFormat, dw);
  97. bytesToGo := ck.ckSize - dw;
  98. end;
  99. // other 'fmt ' chunks are ignored (?)
  100. end
  101. else if (ck.ckID = mmioStringToFourCC('fact', 0)) then
  102. begin
  103. if (dwDataSamples = 0) and (waveFormat.wFormatTag = WAVE_Format_ADPCM) then
  104. begin
  105. Stream.Read(dwDataSamples, SizeOf(LongInt));
  106. Dec(bytesToGo, SizeOf(LongInt));
  107. end;
  108. // other 'fact' chunks are ignored (?)
  109. end
  110. else if (ck.ckID = mmioStringToFourCC('data', 0)) then
  111. begin
  112. dwDataOffset := Stream.Position - startPosition;
  113. dwDataLength := ck.ckSize;
  114. Break;
  115. end;
  116. // all other sub-chunks are ignored, move to the next chunk
  117. Stream.Seek(bytesToGo, soFromCurrent);
  118. until Stream.Position = 2048; // this should never be reached
  119. // Only PCM wave format is recognized
  120. // Assert((waveFormat.wFormatTag=Wave_Format_PCM), 'PCM required');
  121. // seek start of data
  122. pcmOffset := dwDataOffset;
  123. FPCMDataLength := dwDataLength;
  124. SetLength(data, totalSize);
  125. Stream.Position := startPosition;
  126. if totalSize > 0 then
  127. Stream.Read(data[0], totalSize);
  128. // update Sampling data
  129. with waveFormat do
  130. begin
  131. Sampling.Frequency := nSamplesPerSec;
  132. Sampling.NbChannels := nChannels;
  133. Sampling.BitsPerSample := wBitsPerSample;
  134. end;
  135. {$ELSE}
  136. begin
  137. Assert(Assigned(Stream));
  138. SetLength(data, Stream.Size);
  139. if Length(data) > 0 then
  140. Stream.Read(data[0], Length(data));
  141. {$ENDIF}
  142. end;
  143. procedure TGLWAVFile.SaveToStream(Stream: TStream);
  144. begin
  145. if Length(data) > 0 then
  146. Stream.Write(data[0], Length(data));
  147. end;
  148. procedure TGLWAVFile.PlayOnWaveOut;
  149. begin
  150. {$IFDEF MSWINDOWS}
  151. PlaySound(WAVData, 0, SND_ASYNC + SND_MEMORY);
  152. {$ENDIF}
  153. // GLSM.SoundFileObjects.PlayOnWaveOut(PCMData, LengthInBytes, waveFormat);
  154. end;
  155. function TGLWAVFile.WAVData: Pointer;
  156. begin
  157. if Length(data) > 0 then
  158. Result := @data[0]
  159. else
  160. Result := nil;
  161. end;
  162. function TGLWAVFile.WAVDataSize: Integer;
  163. begin
  164. Result := Length(data);
  165. end;
  166. function TGLWAVFile.PCMData: Pointer;
  167. begin
  168. {$IFDEF MSWINDOWS}
  169. if Length(data) > 0 then
  170. Result := @data[pcmOffset]
  171. else
  172. Result := nil;
  173. {$ELSE}
  174. Result := nil;
  175. {$ENDIF}
  176. end;
  177. function TGLWAVFile.LengthInBytes: Integer;
  178. begin
  179. Result := FPCMDataLength;
  180. end;
  181. //----------------------------------------------------
  182. initialization
  183. //----------------------------------------------------
  184. RegisterSoundFileFormat('wav', 'Windows WAV files', TGLWAVFile);
  185. end.