captureplaybackopenal.pas 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. program CaptureAndPlayback;
  2. {$mode objfpc}
  3. uses
  4. sysutils, openal;
  5. const
  6. Seconds = 5; //- We'll record for 5 seconds
  7. Frequency = 8000; //- Recording a frequency of 8000
  8. Format = AL_FORMAT_MONO16; //- Recording 16-bit mono
  9. BufferSize = (Frequency*2)*(Seconds+1); //- (frequency * 2bytes(16-bit)) * seconds
  10. var
  11. pCaptureDevice: pALCDevice; //- Device used to capture audio
  12. pPlaybackDevice: pALCDevice; //- Device used to playback audio
  13. pPlaybackContext: pALCContext; //- Playback context
  14. pPlaybackSource: ALuint; //- Source for playback (in 3D sound would be located)
  15. CaptureBuffer: array[0..BufferSize] of ALubyte; //- Capture buffer external from openAL, sized as calculated above for 5 second recording
  16. PlayBuffer: ALInt; //- openAL internal playback buffer
  17. //- These two are used to control when to begin/end recording and playback
  18. Samples: ALInt; //- count of the number of samples recorded
  19. PlayState: ALInt; //- playback state
  20. begin
  21. //- Find out which extensions are supported and print them (could error check for capture extension here)
  22. writeln('OpenAL Extensions = ',PChar(alGetString(AL_EXTENSIONS)));
  23. //- Print device specifiers for default devices
  24. writeln('ALC_DEFAULT_DEVICE_SPECIFIER = ',PChar(alcGetString(nil, ALC_DEFAULT_DEVICE_SPECIFIER )));
  25. writeln('ALC_CAPTURE_DEVICE_SPECIFIER = ',PChar(alcGetString(nil, ALC_CAPTURE_DEVICE_SPECIFIER )));
  26. //- Setup the input capture device (default device)
  27. writeln('Setting up alcCaptureOpenDevice to use default device');
  28. pcaptureDevice:=alcCaptureOpenDevice(nil, Frequency, Format, BufferSize);
  29. if pcaptureDevice=nil then begin
  30. raise exception.create('Capture device is nil!');
  31. exit;
  32. end;
  33. //- Setup the output player device (default device)
  34. writeln('Setting up alcOpenDevice to use default device');
  35. pPlaybackDevice:=alcOpenDevice(nil);
  36. if pPlaybackDevice=nil then
  37. raise exception.create('Playback device is nil!');
  38. //- Setup the output context, not sure why a context is needed, it just is ok?
  39. writeln('Setting up alcCreateContext');
  40. pPlaybackContext:=alcCreateContext(pPlaybackDevice,nil);
  41. writeln('Making the playback context the current context (alcMakeContextCurrent)');
  42. alcMakeContextCurrent(pPlaybackContext);
  43. // Generate Buffer(s) for playback
  44. alGetError(); // clear error code
  45. alGenBuffers( 1, @PlayBuffer );
  46. if alGetError() <> AL_NO_ERROR then
  47. raise exception.create('Ack!! Error creating playback buffer(s)!');
  48. // Generate Playback Sources - single source, not adjusting locational information for 3D sound
  49. writeln('Setting up playback source (alGenSources)');
  50. alGenSources(1, @pPlaybackSource);
  51. if alGetError() <> AL_NO_ERROR then
  52. raise exception.create('Ack an error creating a playback source!');
  53. //===========================================================================
  54. // Here's where we do the recording bit :)
  55. //===========================================================================
  56. //- Start capturing data
  57. alcCaptureStart(PCaptureDevice);
  58. repeat
  59. alcGetIntegerv(pCaptureDevice, ALC_CAPTURE_SAMPLES, ALsizei(sizeof(ALint)), @samples);
  60. Writeln(IntToStr(samples)+'/'+IntToStr(Seconds*Frequency)+' samples');
  61. until samples>=seconds*frequency;
  62. //- Capture the samples into our capture buffer
  63. alcCaptureSamples(pCaptureDevice, @CaptureBuffer, samples);
  64. //- Done recording
  65. alcCaptureStop(pCaptureDevice);
  66. //===========================================================================
  67. // Here's where we do the playback bit :)
  68. //===========================================================================
  69. //- Load up the playback buffer from our capture buffer
  70. alBufferData( PlayBuffer, Format, @CaptureBuffer, Samples*2, Frequency);
  71. //- Queue the buffer for playback
  72. alSourcei( pPlaybackSource, AL_BUFFER, PlayBuffer );
  73. //- Play the sound
  74. alSourcePlay(ALuint(pPlaybackSource));
  75. //- Wait for the player to stop
  76. repeat
  77. alGetSourcei( pPlaybackSource, AL_SOURCE_STATE, PlayState);
  78. until (PlayState <> AL_INITIAL) and (PlayState <> AL_PLAYING);
  79. //===========================================================================
  80. //- Shutdown/Clean up the playback stuff
  81. pPlaybackContext:=alcGetCurrentContext();
  82. pPlaybackDevice:=alcGetContextsDevice(pPlaybackContext);
  83. alcMakeContextCurrent(nil);
  84. alcDestroyContext(pPlaybackContext);
  85. alcCloseDevice(pPlaybackDevice);
  86. //- Shutdown/Clean up the capture stuff
  87. alcCaptureStop( pCaptureDevice );
  88. alcCaptureCloseDevice( pCaptureDevice );
  89. end.