program mp3test;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
Classes, SysUtils, CustApp
{ you can add units after this }
,mp3;
type
{ TMyApplication }
TMyApplication = class(TCustomApplication)
protected
procedure DoRun; override;
public
constructor Create(TheOwner: TComponent); override;
destructor Destroy; override;
procedure Test();
end;
type
TWaveHeader = record
Marker1: array[0..3] of AnsiChar;
BytesFollowing: LongInt;
Marker2: array[0..3] of AnsiChar;
Marker3: array[0..3] of AnsiChar;
Fixed1: LongInt;
FormatTag: Word;
Channels: Word;
SampleRate: LongInt;
BytesPerSecond: LongInt;
BytesPerSample: Word;
BitsPerSample: Word;
Marker4: array[0..3] of AnsiChar;
DataBytes: LongInt;
end;
procedure CreateWavFile(AFileName: string; ms: TStream; Channels, SampleRate, Resolution: Integer);
var
DataBytes: Integer;
WaveHeader: TWaveHeader;
fsOut: TFileStream;
begin
DataBytes := ms.Size;
WaveHeader.Marker1 := 'RIFF';
WaveHeader.BytesFollowing := DataBytes + 36;
WaveHeader.Marker2 := 'WAVE';
WaveHeader.Marker3 := 'fmt ';
WaveHeader.Fixed1 := 16;
WaveHeader.FormatTag := 1;
WaveHeader.SampleRate := SampleRate;
WaveHeader.Channels := Channels;
WaveHeader.BytesPerSecond := Channels;
WaveHeader.BytesPerSecond := WaveHeader.BytesPerSecond * SampleRate;
WaveHeader.BytesPerSecond := WaveHeader.BytesPerSecond * Resolution;
WaveHeader.BytesPerSecond := WaveHeader.BytesPerSecond div 8;
WaveHeader.BytesPerSample := Channels * Resolution div 8;
WaveHeader.BitsPerSample := Resolution;
WaveHeader.Marker4 := 'data';
WaveHeader.DataBytes := DataBytes;
if FileExists(AFileName) then
DeleteFile(AFileName);
fsOut := TFileStream.Create(AFileName, fmCreate or fmShareDenyNone);
try
fsOut.Write(WaveHeader, Sizeof(WaveHeader));
ms.Position := 0;
fsOut.CopyFrom(ms, ms.Size);
finally
fsOut.Free();
end;
end;
{ TMyApplication }
procedure TMyApplication.DoRun;
var
ErrorMsg: String;
begin
{ add your program here }
Test();
// stop program loop
Terminate;
end;
constructor TMyApplication.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
StopOnException := True;
end;
destructor TMyApplication.Destroy;
begin
inherited Destroy;
end;
procedure TMyApplication.Test;
var
id: TPdmp3Handle;
fsIn, fsOut: TStream;
InBuf, OutBuf: TByteArray;
InSize, OutSize: Integer;
res: Integer;
dt: TDateTime;
begin
if FileExists('out.raw') then
DeleteFile('out.raw');
fsIn := TFileStream.Create('sample-3s.mp3', fmOpenRead or fmShareDenyNone);
fsOut := TMemoryStream.Create();
dt := Now();
try
pdmp3_open_feed(id);
res := PDMP3_NEED_MORE;
while (res = PDMP3_OK) or (res = PDMP3_NEED_MORE) do
begin
// transcode
inSize := fsIn.Read(InBuf, 2048);
FillChar(OutBuf, SizeOf(OutBuf), 0);
res := pdmp3_decode(id, InBuf, inSize, OutBuf, SizeOf(OutBuf), OutSize);
if (res = PDMP3_OK) or (res = PDMP3_NEED_MORE) then
begin
fsOut.Write(OutBuf, OutSize);
end;
end;
CreateWavFile('out.wav', fsOut, 2, 44100, 16);
finally
fsOut.Free();
fsIn.Free();
end;
WriteLn(FormatDateTime('nn:ss.zzz', Now()-dt));
ReadLn();
end;
var
Application: TMyApplication;
begin
Application := TMyApplication.Create(nil);
Application.Title := 'My Application';
Application.Run;
Application.Free;
end.