From bd2c3feb4999830bb3c1b587746beb9f216af8ec Mon Sep 17 00:00:00 2001 From: sekelsenmat Date: Wed, 25 Jan 2012 15:03:16 +0000 Subject: [PATCH] fpsound: develops further, still only noise git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@2271 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- components/fpsound/example/mainform.lfm | 2 +- components/fpsound/example/mainform.pas | 9 +++------ components/fpsound/fpsound.pas | 18 ++++++++++++++++-- components/fpsound/fpsound_openal.pas | 20 +++++++++++++++----- 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/components/fpsound/example/mainform.lfm b/components/fpsound/example/mainform.lfm index 83639f04b..f0dcfdf4f 100644 --- a/components/fpsound/example/mainform.lfm +++ b/components/fpsound/example/mainform.lfm @@ -13,7 +13,7 @@ object Form1: TForm1 Height = 25 Top = 48 Width = 292 - Caption = 'Open, Play and Close' + Caption = 'Load and Play' OnClick = btnOpenPlayAndCloseClick TabOrder = 0 end diff --git a/components/fpsound/example/mainform.pas b/components/fpsound/example/mainform.pas index 3c73790cd..b0144220f 100644 --- a/components/fpsound/example/mainform.pas +++ b/components/fpsound/example/mainform.pas @@ -35,13 +35,10 @@ uses fpsound, fpsound_wav, fpsound_openal; { TForm1 } procedure TForm1.btnOpenPlayAndCloseClick(Sender: TObject); -var - lSoundDoc: TSoundDocument; begin - lSoundDoc := TSoundDocument.Create; - lSoundDoc.LoadFromFile(pathEdit.FileName); - lSoundDoc.SetSoundPlayer(spOpenAL); - lSoundDoc.Play; + SoundPlayer.LoadFromFile(pathEdit.FileName); + SoundPlayer.SetSoundPlayer(spOpenAL); + SoundPlayer.Play; end; procedure TForm1.FormCreate(Sender: TObject); diff --git a/components/fpsound/fpsound.pas b/components/fpsound/fpsound.pas index 097729115..fdca0de59 100644 --- a/components/fpsound/fpsound.pas +++ b/components/fpsound/fpsound.pas @@ -28,11 +28,13 @@ type procedure ReadFromStream(AStream: TStream; ADest: TSoundDocument); virtual; abstract; end; - TSoundPlayerKind = (spOpenAL, spMPlayer, spFMod, spExtra1, spExtra2); + TSoundPlayerKind = (spNone, spOpenAL, spMPlayer, spFMod, spExtra1, spExtra2); { TSoundPlayer } TSoundPlayer = class + protected + FInitialized: Boolean; public constructor Create; virtual; procedure Initialize; virtual; abstract; @@ -79,6 +81,7 @@ type private AStream: TStream; FPlayer: TSoundPlayer; + FPlayerKind: TSoundPlayerKind; FCurElementIndex: Integer; FSoundData: TFPList; // of TSoundElement public @@ -103,6 +106,9 @@ type procedure SetSoundPlayer(AKind: TSoundPlayerKind); end; +var + SoundPlayer: TSoundDocument; + procedure RegisterSoundPlayer(APlayer: TSoundPlayer; AKind: TSoundPlayerKind); procedure RegisterSoundReader(AReader: TSoundReader; AFormat: TSoundFormat); function GetSoundPlayer(AKind: TSoundPlayerKind): TSoundPlayer; @@ -111,7 +117,7 @@ function GetSoundReader(AFormat: TSoundFormat): TSoundReader; implementation var - GSoundPlayers: array[TSoundPlayerKind] of TSoundPlayer = (nil, nil, nil, nil, nil); + GSoundPlayers: array[TSoundPlayerKind] of TSoundPlayer = (nil, nil, nil, nil, nil, nil); GSoundReaders: array[TSoundFormat] of TSoundReader = (nil, nil, nil, nil, nil, nil, nil); // GSoundWriter: array[TSoundFormat] of TSoundWriter = (nil, nil, nil, nil, nil, nil, nil); @@ -168,6 +174,7 @@ end; destructor TSoundDocument.Destroy; begin FSoundData.Free; + if FPlayer <> nil then FPlayer.Finalize; inherited Destroy; end; @@ -272,6 +279,8 @@ end; procedure TSoundDocument.SetSoundPlayer(AKind: TSoundPlayerKind); begin + if AKind = FPlayerKind then Exit; + FPlayerKind := AKind; Stop; FPlayer := GSoundPlayers[AKind]; end; @@ -280,7 +289,12 @@ var lReaderIndex: TSoundFormat; lPlayerIndex: TSoundPlayerKind; +initialization + SoundPlayer := TSoundDocument.Create; + finalization + SoundPlayer.Free; + for lReaderIndex := Low(TSoundFormat) to High(TSoundFormat) do if GSoundReaders[lReaderIndex] <> nil then GSoundReaders[lReaderIndex].Free; diff --git a/components/fpsound/fpsound_openal.pas b/components/fpsound/fpsound_openal.pas index d7dd7a556..47b3fffc6 100644 --- a/components/fpsound/fpsound_openal.pas +++ b/components/fpsound/fpsound_openal.pas @@ -1,4 +1,12 @@ { +OpenAL player for the fpsound library + +License: The same modified LGPL as the LCL + +Authors: + +JiXian Yang +Felipe Monteiro de Carvalho } unit fpsound_openal; @@ -103,6 +111,8 @@ end; procedure TOpenALPlayer.Initialize; begin + if FInitialized then Exit; + al_device := alcOpenDevice(nil); al_context := alcCreateContext(al_device, nil); alcMakeContextCurrent(al_context); @@ -122,6 +132,8 @@ begin // alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED); al_bufcount := 1; alGenBuffers(al_bufcount, @al_buffers); + + FInitialized := True; end; procedure TOpenALPlayer.Finalize; @@ -132,6 +144,7 @@ begin if al_readbuf <> nil then FreeMem(al_readbuf); alcDestroyContext(al_context); alcCloseDevice(al_device); + FInitialized := False; end; procedure TOpenALPlayer.Play(ASound: TSoundDocument); @@ -140,7 +153,7 @@ var done : Boolean; lKeyElement: TSoundKeyElement; begin - Initialize(); + Initialize; // First adjust to the first key element lKeyElement := ASound.GetFirstSoundElement(); @@ -162,10 +175,7 @@ end; procedure TOpenALPlayer.AdjustToKeyElement(ASound: TSoundDocument; AKeyElement: TSoundKeyElement); begin - // define codec - //source := AStream; - - // inittialize codec + // initialize codec if AKeyElement.Channels = 1 then begin if AKeyElement.BitsPerSample=8 then al_format:=AL_FORMAT_MONO8