GLFileMP3.pas 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // This unit is part of the GLScene Engine, http://glscene.org
  3. //
  4. {
  5. Support for MP3 format.
  6. }
  7. unit GLFileMP3;
  8. interface
  9. {$I GLScene.inc}
  10. uses
  11. System.Classes,
  12. GLApplicationFileIO,
  13. GLSoundFileObjects;
  14. type
  15. {Support for MP3 format.
  16. *Partial* support only, access to PCMData is NOT supported. }
  17. TGLMP3File = class (TGLSoundFile)
  18. private
  19. data : array of Byte; // used to store MP3 bitstream
  20. protected
  21. public
  22. function CreateCopy(AOwner: TPersistent) : TGLDataFile; override;
  23. class function Capabilities : TGLDataFileCapabilities; override;
  24. procedure LoadFromStream(Stream: TStream); override;
  25. procedure SaveToStream(Stream: TStream); override;
  26. procedure PlayOnWaveOut; override;
  27. function WAVData : Pointer; override;
  28. function WAVDataSize : Integer; override;
  29. function PCMData : Pointer; override;
  30. function LengthInBytes : Integer; override;
  31. end;
  32. //========================================================
  33. implementation
  34. //========================================================
  35. // ------------------
  36. // ------------------ TGLMP3File ------------------
  37. // ------------------
  38. function TGLMP3File.CreateCopy(AOwner: TPersistent) : TGLDataFile;
  39. begin
  40. Result:=inherited CreateCopy(AOwner);
  41. if Assigned(Result) then begin
  42. TGLMP3File(Result).data := Copy(data);
  43. end;
  44. end;
  45. class function TGLMP3File.Capabilities : TGLDataFileCapabilities;
  46. begin
  47. Result:=[dfcRead, dfcWrite];
  48. end;
  49. procedure TGLMP3File.LoadFromStream(stream : TStream);
  50. begin
  51. // MP3 isn't actually, just loaded directly...
  52. Assert(Assigned(stream));
  53. SetLength(data, stream.Size);
  54. if Length(data)>0 then
  55. stream.Read(data[0], Length(data));
  56. end;
  57. procedure TGLMP3File.SaveToStream(stream: TStream);
  58. begin
  59. if Length(data)>0 then
  60. stream.Write(data[0], Length(data));
  61. end;
  62. procedure TGLMP3File.PlayOnWaveOut;
  63. begin
  64. Assert(False, 'MP3 playback on WaveOut not supported.');
  65. end;
  66. function TGLMP3File.WAVData : Pointer;
  67. begin
  68. if Length(data)>0 then
  69. Result:=@data[0]
  70. else Result:=nil;
  71. end;
  72. function TGLMP3File.WAVDataSize : Integer;
  73. begin
  74. Result:=Length(data);
  75. end;
  76. function TGLMP3File.PCMData : Pointer;
  77. begin
  78. Result:=nil;
  79. end;
  80. function TGLMP3File.LengthInBytes : Integer;
  81. begin
  82. Result:=0;
  83. end;
  84. //================================================================
  85. initialization
  86. //================================================================
  87. RegisterSoundFileFormat('mp3', 'MPEG Layer3 files', TGLMP3File);
  88. end.