Pārlūkot izejas kodu

* OpenAL example from Mantis 15306 by Craig Chapman.

git-svn-id: trunk@14416 -
marco 15 gadi atpakaļ
vecāks
revīzija
3fc150d920

+ 1 - 0
.gitattributes

@@ -4478,6 +4478,7 @@ packages/openal/Makefile svneol=native#text/plain
 packages/openal/Makefile.fpc svneol=native#text/plain
 packages/openal/examples/Makefile svneol=native#text/plain
 packages/openal/examples/Makefile.fpc svneol=native#text/plain
+packages/openal/examples/captureplaybackopenal.pas svneol=native#text/plain
 packages/openal/examples/madopenal.pas svneol=native#text/plain
 packages/openal/fpmake.pp svneol=native#text/plain
 packages/openal/src/alch.inc svneol=native#text/plain

+ 61 - 61
packages/openal/examples/Makefile

@@ -1,5 +1,5 @@
 #
-# Don't edit, this file is generated by FPCMake Version 2.0.0 [2009/12/10]
+# Don't edit, this file is generated by FPCMake Version 2.0.0 [2009/12/11]
 #
 default: all
 MAKEFILETARGETS=i386-linux i386-go32v2 i386-win32 i386-os2 i386-freebsd i386-beos i386-haiku i386-netbsd i386-solaris i386-qnx i386-netware i386-openbsd i386-wdosx i386-darwin i386-emx i386-watcom i386-netwlibc i386-wince i386-embedded i386-symbian m68k-linux m68k-freebsd m68k-netbsd m68k-amiga m68k-atari m68k-openbsd m68k-palmos m68k-embedded powerpc-linux powerpc-netbsd powerpc-amiga powerpc-macos powerpc-darwin powerpc-morphos powerpc-embedded sparc-linux sparc-netbsd sparc-solaris sparc-embedded x86_64-linux x86_64-freebsd x86_64-solaris x86_64-darwin x86_64-win64 x86_64-embedded arm-linux arm-palmos arm-darwin arm-wince arm-gba arm-nds arm-embedded arm-symbian powerpc64-linux powerpc64-darwin powerpc64-embedded avr-embedded armeb-linux armeb-embedded mipsel-linux
@@ -265,184 +265,184 @@ UNITSDIR:=$(wildcard $(FPCDIR)/units/$(OS_TARGET))
 endif
 PACKAGESDIR:=$(wildcard $(FPCDIR) $(FPCDIR)/packages $(FPCDIR)/packages/base $(FPCDIR)/packages/extra)
 ifeq ($(FULL_TARGET),i386-linux)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),i386-go32v2)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),i386-win32)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),i386-os2)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),i386-freebsd)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),i386-beos)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),i386-haiku)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),i386-netbsd)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),i386-solaris)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),i386-qnx)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),i386-netware)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),i386-openbsd)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),i386-wdosx)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),i386-darwin)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),i386-emx)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),i386-watcom)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),i386-netwlibc)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),i386-wince)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),i386-embedded)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),i386-symbian)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),m68k-linux)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),m68k-freebsd)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),m68k-netbsd)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),m68k-amiga)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),m68k-atari)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),m68k-openbsd)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),m68k-palmos)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),m68k-embedded)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),powerpc-linux)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),powerpc-netbsd)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),powerpc-amiga)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),powerpc-macos)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),powerpc-darwin)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),powerpc-morphos)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),powerpc-embedded)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),sparc-linux)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),sparc-netbsd)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),sparc-solaris)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),sparc-embedded)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),x86_64-linux)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),x86_64-freebsd)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),x86_64-solaris)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),x86_64-darwin)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),x86_64-win64)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),x86_64-embedded)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),arm-linux)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),arm-palmos)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),arm-darwin)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),arm-wince)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),arm-gba)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),arm-nds)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),arm-embedded)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),arm-symbian)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),powerpc64-linux)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),powerpc64-darwin)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),powerpc64-embedded)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),avr-embedded)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),armeb-linux)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),armeb-embedded)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 ifeq ($(FULL_TARGET),mipsel-linux)
-override TARGET_PROGRAMS+=madopenal
+override TARGET_PROGRAMS+=madopenal captureplaybackopenal
 endif
 override INSTALL_FPCPACKAGE=y
 ifdef REQUIRE_UNITSDIR

+ 1 - 1
packages/openal/examples/Makefile.fpc

@@ -3,7 +3,7 @@
 #
 
 [target]
-programs=madopenal
+programs=madopenal captureplaybackopenal
 
 [require]
 packages=openal mad oggvorbis a52 dts modplug matroska

+ 116 - 0
packages/openal/examples/captureplaybackopenal.pas

@@ -0,0 +1,116 @@
+program CaptureAndPlayback;
+{$mode objfpc}
+
+uses
+  sysutils, openal;
+
+const
+  Seconds = 5;                            //- We'll record for 5 seconds
+  Frequency = 8000;                       //- Recording a frequency of 8000
+  Format = AL_FORMAT_MONO16;              //- Recording 16-bit mono
+  BufferSize = (Frequency*2)*(Seconds+1); //- (frequency * 2bytes(16-bit)) * seconds
+
+var
+  pCaptureDevice: pALCDevice;                  //- Device used to capture audio
+  pPlaybackDevice: pALCDevice;                 //- Device used to playback audio
+  pPlaybackContext: pALCContext;               //- Playback context
+  pPlaybackSource: ALuint;                     //- Source for playback (in 3D sound would be located)
+  CaptureBuffer: array[0..BufferSize] of ALubyte; //- Capture buffer external from openAL, sized as calculated above for 5 second recording
+  PlayBuffer: ALInt;                           //- openAL internal playback buffer
+
+  //- These two are used to control when to begin/end recording and playback
+  Samples: ALInt;                //- count of the number of samples recorded
+  PlayState: ALInt;              //- playback state
+
+begin
+
+  //- Find out which extensions are supported and print them (could error check for capture extension here)
+  writeln('OpenAL Extensions = ',PChar(alGetString(AL_EXTENSIONS)));
+
+  //- Print device specifiers for default devices
+  writeln('ALC_DEFAULT_DEVICE_SPECIFIER = ',PChar(alcGetString(nil, ALC_DEFAULT_DEVICE_SPECIFIER )));
+  writeln('ALC_CAPTURE_DEVICE_SPECIFIER = ',PChar(alcGetString(nil, ALC_CAPTURE_DEVICE_SPECIFIER )));
+
+  //- Setup the input capture device (default device)
+  writeln('Setting up alcCaptureOpenDevice to use default device');
+  pcaptureDevice:=alcCaptureOpenDevice(nil, Frequency, Format, BufferSize);
+  if pcaptureDevice=nil then begin
+    raise exception.create('Capture device is nil!');
+    exit;
+  end;
+
+  //- Setup the output player device (default device)
+  writeln('Setting up alcOpenDevice to use default device');
+  pPlaybackDevice:=alcOpenDevice(nil);
+  if pPlaybackDevice=nil then
+    raise exception.create('Playback device is nil!');
+
+  //- Setup the output context, not sure why a context is needed, it just is ok?
+  writeln('Setting up alcCreateContext');
+  pPlaybackContext:=alcCreateContext(pPlaybackDevice,nil);
+  writeln('Making the playback context the current context (alcMakeContextCurrent)');
+  alcMakeContextCurrent(pPlaybackContext);
+
+  // Generate Buffer(s) for playback
+  alGetError(); // clear error code
+  alGenBuffers( 1, @PlayBuffer );
+  if alGetError() <> AL_NO_ERROR then
+    raise exception.create('Ack!! Error creating playback buffer(s)!');
+
+  // Generate Playback Sources - single source, not adjusting locational information for 3D sound
+  writeln('Setting up playback source (alGenSources)');
+  alGenSources(1, @pPlaybackSource);
+  if alGetError() <> AL_NO_ERROR then
+    raise exception.create('Ack an error creating a playback source!');
+
+
+  //===========================================================================
+  // Here's where we do the recording bit :)
+  //===========================================================================
+
+  //- Start capturing data
+  alcCaptureStart(PCaptureDevice);
+  repeat
+    alcGetIntegerv(pCaptureDevice, ALC_CAPTURE_SAMPLES, ALsizei(sizeof(ALint)), @samples);
+    Writeln(IntToStr(samples)+'/'+IntToStr(Seconds*Frequency)+' samples');
+  until samples>=seconds*frequency;
+
+  //- Capture the samples into our capture buffer
+  alcCaptureSamples(pCaptureDevice, @CaptureBuffer, samples);
+
+  //- Done recording
+  alcCaptureStop(pCaptureDevice);
+
+
+  //===========================================================================
+  // Here's where we do the playback bit :)
+  //===========================================================================
+
+  //- Load up the playback buffer from our capture buffer
+  alBufferData( PlayBuffer, Format, @CaptureBuffer, Samples*2, Frequency);
+
+  //- Queue the buffer for playback
+  alSourcei( pPlaybackSource, AL_BUFFER, PlayBuffer );
+
+  //- Play the sound
+  alSourcePlay(ALuint(pPlaybackSource));
+
+  //- Wait for the player to stop
+  repeat
+    alGetSourcei( pPlaybackSource, AL_SOURCE_STATE, PlayState);
+  until (PlayState <> AL_INITIAL) and (PlayState <> AL_PLAYING);
+
+
+  //===========================================================================
+  
+  //- Shutdown/Clean up the playback stuff
+  pPlaybackContext:=alcGetCurrentContext();
+  pPlaybackDevice:=alcGetContextsDevice(pPlaybackContext);
+  alcMakeContextCurrent(nil);
+  alcDestroyContext(pPlaybackContext);
+  alcCloseDevice(pPlaybackDevice);
+
+  //- Shutdown/Clean up the capture stuff
+  alcCaptureStop( pCaptureDevice );
+  alcCaptureCloseDevice( pCaptureDevice );
+end.