GLS.FileMP3.pas 2.5 KB

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