GXS.FileWAV.pas 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // The graphics engine GLXEngine. The unit of GXScene for Delphi
  3. //
  4. unit GXS.FileWAV;
  5. (* Support for Windows WAV format *)
  6. interface
  7. {$I Stage.Defines.inc}
  8. uses
  9. Winapi.MMSystem,
  10. System.Classes,
  11. GXS.ApplicationFileIO,
  12. GXS.SoundFileObjects;
  13. type
  14. // Support for Windows WAV format.
  15. TgxWAVFile = class(TgxSoundFile)
  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): TgxDataFile; override;
  26. class function Capabilities: TDataFileCapabilities; 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. // ------------------ TgxWAVFile ------------------
  49. // ------------------
  50. function TgxWAVFile.CreateCopy(AOwner: TPersistent): TgxDataFile;
  51. begin
  52. Result := inherited CreateCopy(AOwner);
  53. if Assigned(Result) then
  54. begin
  55. {$IFDEF MSWINDOWS}
  56. TgxWAVFile(Result).waveFormat := waveFormat;
  57. {$ENDIF}
  58. TgxWAVFile(Result).data := Copy(data);
  59. end;
  60. end;
  61. class function TgxWAVFile.Capabilities: TDataFileCapabilities;
  62. begin
  63. Result := [dfcRead, dfcWrite];
  64. end;
  65. procedure TgxWAVFile.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)
  104. then
  105. begin
  106. Stream.Read(dwDataSamples, SizeOf(LongInt));
  107. Dec(bytesToGo, SizeOf(LongInt));
  108. end;
  109. // other 'fact' chunks are ignored (?)
  110. end
  111. else if (ck.ckID = mmioStringToFourCC('data', 0)) then
  112. begin
  113. dwDataOffset := Stream.Position - startPosition;
  114. dwDataLength := ck.ckSize;
  115. Break;
  116. end;
  117. // all other sub-chunks are ignored, move to the next chunk
  118. Stream.Seek(bytesToGo, soFromCurrent);
  119. until Stream.Position = 2048; // this should never be reached
  120. // Only PCM wave format is recognized
  121. // Assert((waveFormat.wFormatTag=Wave_Format_PCM), 'PCM required');
  122. // seek start of data
  123. pcmOffset := dwDataOffset;
  124. FPCMDataLength := dwDataLength;
  125. SetLength(data, totalSize);
  126. Stream.Position := startPosition;
  127. if totalSize > 0 then
  128. Stream.Read(data[0], totalSize);
  129. // update Sampling data
  130. with waveFormat do
  131. begin
  132. Sampling.Frequency := nSamplesPerSec;
  133. Sampling.NbChannels := nChannels;
  134. Sampling.BitsPerSample := wBitsPerSample;
  135. end;
  136. {$ELSE}
  137. begin
  138. Assert(Assigned(Stream));
  139. SetLength(data, Stream.Size);
  140. if Length(data) > 0 then
  141. Stream.Read(data[0], Length(data));
  142. {$ENDIF}
  143. end;
  144. procedure TgxWAVFile.SaveToStream(Stream: TStream);
  145. begin
  146. if Length(data) > 0 then
  147. Stream.Write(data[0], Length(data));
  148. end;
  149. procedure TgxWAVFile.PlayOnWaveOut;
  150. begin
  151. {$IFDEF MSWINDOWS}
  152. PlaySound(WAVData, 0, SND_ASYNC + SND_MEMORY);
  153. {$ENDIF}
  154. // GLSoundFileObjects.PlayOnWaveOut(PCMData, LengthInBytes, waveFormat);
  155. end;
  156. function TgxWAVFile.WAVData: Pointer;
  157. begin
  158. if Length(data) > 0 then
  159. Result := @data[0]
  160. else
  161. Result := nil;
  162. end;
  163. function TgxWAVFile.WAVDataSize: Integer;
  164. begin
  165. Result := Length(data);
  166. end;
  167. function TgxWAVFile.PCMData: Pointer;
  168. begin
  169. {$IFDEF MSWINDOWS}
  170. if Length(data) > 0 then
  171. Result := @data[pcmOffset]
  172. else
  173. Result := nil;
  174. {$ELSE}
  175. Result := nil;
  176. {$ENDIF}
  177. end;
  178. function TgxWAVFile.LengthInBytes: Integer;
  179. begin
  180. Result := FPCMDataLength;
  181. end;
  182. //-------------------------------------------------
  183. initialization
  184. //-------------------------------------------------
  185. RegisterSoundFileFormat('wav', 'Windows WAV files', TgxWAVFile);
  186. end.