Re: playback captured audio from mic

Jason Daly <[email protected]>
Newsgroups gmane.comp.lib.openal
Message-ID <[email protected]>
[email protected] wrote:
> Hi all,
>
> I'm fighting for a week now with openal to be able to do this exact 
> thing "Capture (from mic) and playback the recorded audio" or at least 
> "Capture (from mic) and save the audio to a .wav file". I read a bunch 
> of post on different forums about it, but non of them shows a complete 
> working example, the just say "try to fix this or that" or "you miss to 
> do that..", so, it becoming a nightmare to try to mix different answers 
> to diferent potential problems in the code...
>
> So, anyone have a fully working simple sample about how to do that?, 
> hopefully a linux one...
>   

Mauricio,

Here's a really simple program that captures audio for 5 seconds, 
pauses, then captures 5 more seconds of audio, and finally plays it all 
back (I used it to debug a problem with capture one time).

Maybe it'll be useful for you...

--"J"

_______________________________________________
Openal mailing list
[email protected]
http://opensource.creative.com/mailman/listinfo/openal
testcapture.c (text/x-csrc, 5.6 KB)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <AL/al.h>
#include <AL/alc.h>
#include <sys/time.h>

int main(void)
{
   const ALCchar *   devices;
   ALCchar *         ptr;
   ALCdevice *       mainDev;
   ALCcontext *      mainContext;
   ALCdevice *       captureDev;
   ALubyte           captureBuffer[1048576];
   ALubyte           *captureBufPtr;
   ALint             samplesAvailable;
   ALint             samplesCaptured;
   time_t            currentTime;
   time_t            lastTime;
   ALuint            buffer;
   ALuint            source;
   ALint             playState;
   int               i;

   // Print the list of capture devices
   printf("Available playback devices:\n");
   devices = alcGetString(NULL, ALC_DEVICE_SPECIFIER);
   ptr = devices;
   while (ptr[0] != NULL)
   {
       printf("   %s\n", ptr);
       ptr += strlen(ptr) + 1;
   }

   // Open a playback device and create a context first
   printf("Opening playback device:\n");
   mainDev = alcOpenDevice(NULL);
   if (mainDev == NULL)
   {
      printf("Unable to open playback device!\n");
      exit(1);
   }
   devices = alcGetString(mainDev, ALC_DEVICE_SPECIFIER);
   printf("   opened device '%s'\n", devices);
   mainContext = alcCreateContext(mainDev, NULL);
   if (mainContext == NULL)
   {
      printf("Unable to create playback context!\n");
      exit(1);
   }
   printf("   created playback context\n");

   // Make the playback context current
   alcMakeContextCurrent(mainContext);
   alcProcessContext(mainContext);

   // Print the list of capture devices
   printf("Available capture devices:\n");
   devices = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER);
   ptr = devices;
   while (ptr[0] != NULL)
   {
       printf("   %s\n", ptr);
       ptr += strlen(ptr) + 1;
   }

   // Open the default device
   printf("Opening capture device:\n");
   captureDev = alcCaptureOpenDevice(NULL, 8000, AL_FORMAT_MONO16, 800);
   if (captureDev == NULL)
   {
      printf("   Unable to open device!\n");
      exit(1);
   }
   devices = alcGetString(captureDev, ALC_CAPTURE_DEVICE_SPECIFIER);
   printf("   opened device %s\n", devices);

   // Wait for three seconds to prompt the user
   for (i = 3; i > 0; i--)
   {
      printf("Starting capture in %d...\r", i);
      fflush(stdout);
      lastTime = time(NULL);
      currentTime = lastTime;
      while (currentTime == lastTime)
      {
         currentTime = time(NULL);
         usleep(100000);
      }
   }

   printf("Starting capture NOW!\n");
   fflush(stdout);
   lastTime = currentTime;

   // Capture (roughly) five seconds of audio
   alcCaptureStart(captureDev);
   samplesCaptured = 0;
   captureBufPtr = captureBuffer;
   while (currentTime < (lastTime + 5))
   {
      // Get the number of samples available
      alcGetIntegerv(captureDev, ALC_CAPTURE_SAMPLES, 1, &samplesAvailable);

      // Copy the samples to our capture buffer
      if (samplesAvailable > 0)
      {
         alcCaptureSamples(captureDev, captureBufPtr, samplesAvailable);
         samplesCaptured += samplesAvailable;
         printf("Captured %d samples (adding %d)\r", samplesCaptured,
            samplesAvailable);
         fflush(stdout);

         // Advance the buffer (two bytes per sample * number of samples)
         captureBufPtr += samplesAvailable * 2;
      }

      // Wait for a bit
      usleep(10000);

      // Update the clock
      currentTime = time(NULL);
   }
   printf("\nPausing capture.\n");
   alcCaptureStop(captureDev);

   // Wait for three seconds to prompt the user
   for (i = 3; i > 0; i--)
   {
      printf("Resuming capture in %d...\r", i);
      fflush(stdout);
      lastTime = time(NULL);
      currentTime = lastTime;
      while (currentTime == lastTime)
      {
         currentTime = time(NULL);
         usleep(100000);
      }
   }

   printf("Resuming capture NOW!\n");
   fflush(stdout);
   lastTime = currentTime;

   // Capture (roughly) five seconds of audio
   alcCaptureStart(captureDev);
   while (currentTime < (lastTime + 5))
   {
      // Get the number of samples available
      alcGetIntegerv(captureDev, ALC_CAPTURE_SAMPLES, 1, &samplesAvailable);

      // Copy the samples to our capture buffer
      if (samplesAvailable > 0)
      {
         alcCaptureSamples(captureDev, captureBufPtr, samplesAvailable);
         samplesCaptured += samplesAvailable;
         printf("Captured %d samples (adding %d)\r", samplesCaptured,
            samplesAvailable);
         fflush(stdout);

         // Advance the buffer (two bytes per sample * number of samples)
         captureBufPtr += samplesAvailable * 2;
      }

      // Wait for a bit
      usleep(10000);

      // Update the clock
      currentTime = time(NULL);
   }

   printf("\nDone capturing.\n");
   alcCaptureStop(captureDev);

   // Play back the captured data
   printf("Starting playback...\n");
   fflush(stdout);
  
   // Generate an OpenAL buffer for the captured data
   alGenBuffers(1, &buffer);
   alGenSources(1, &source);
   alBufferData(buffer, AL_FORMAT_MONO16, captureBuffer,
      samplesCaptured*2, 8000);
   alSourcei(source, AL_BUFFER, buffer);
   alSourcePlay(source);

   // Wait for the source to stop playing
   playState = AL_PLAYING;
   while (playState == AL_PLAYING)
   {
      printf("  source %d is playing...\r", source);
      fflush(stdout);
      alGetSourcei(source, AL_SOURCE_STATE, &playState);
      usleep(100000);
   }
   printf("\nDone with playback.\n");
   fflush(stdout);

   // Shut down OpenAL
   alDeleteSources(1, &source);
   alDeleteBuffers(1, &buffer);
   alcMakeContextCurrent(NULL);
   alcCloseDevice(mainDev);
   alcCaptureCloseDevice(captureDev);
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.