Re: Selecting speaker output as capture device

keghn <[email protected]> Fri, 26 Apr 2013 19:28:39 -0700 (PDT)
Newsgroups gmane.comp.lib.openal
Message-ID <[email protected]>
/*
hello.

 i compiled and ran the program. it works. my microphone pick up sound
and was outputed to my head phones. I have not tried through the speakers.
 I ran it on my linux like ubuntu 12.04 like system. To be exact i ran it on
Mint 14.1 mate.
 


		microphone  player
*/

/*
Compile with:

g++ microphone_player.cpp -lalut -lopenal -o microphone_player

*/

// Compile as: g++ p.cpp -lalut -lopenal -o p


#include <AL/al.h>    // OpenAL header files
#include <AL/alc.h>
#include <list>
#include <iostream>
#include <stdio.h>
//#include "stdafx.h"// for windows and visual c i think?
//#include <windows.h>// for windows
//#include <al.h>// for windows
//#include <alc.h>// for windows


using std::list;

#define FREQ 22050   // Sample rate
#define CAP_SIZE 2048 // How much to capture at a time (affects latency)


int main(int argC,char* argV[])
{


	list<ALuint> bufferQueue;                                                                                          
// A quick and dirty queue of buffer objects 
	ALenum errorCode=0; 
	ALuint helloBuffer[16], helloSource[1]; 

	const char* devices = alcGetString(NULL,ALC_DEFAULT_ALL_DEVICES_SPECIFIER); 
	printf("Available capture devices: %s\n",devices); 

	ALCdevice* audioDevice = alcOpenDevice(devices);                                                                   
// Request default audio device 
	errorCode = alcGetError(audioDevice); 
	ALCcontext* audioContext = alcCreateContext(audioDevice,NULL);                                                     
// Create the audio context 
	alcMakeContextCurrent(audioContext); 
	errorCode = alcGetError(audioDevice); 
	ALCdevice* inputDevice =
alcCaptureOpenDevice(NULL,FREQ,AL_FORMAT_MONO16,FREQ/2);                     
// Request the default capture device with a half-second buffer 
	errorCode = alcGetError(inputDevice); 
	alcCaptureStart(inputDevice);   
	errorCode = alcGetError(inputDevice); 

	alGenBuffers(16,&helloBuffer[0]);                                                                             
// Create some buffer-objects 
	errorCode = alGetError(); 

	for (int ii=0;ii<16;++ii) { 
    bufferQueue.push_back(helloBuffer[ii]);                                                                        
// Queue our buffers onto an STL list 
	} 

	alGenSources (1, &helloSource[0]);                                                                                 
// Create a sound source 
	errorCode = alGetError(); 

	short buffer[FREQ*2];                                                                                              
// A buffer to hold captured audio 
	ALCint samplesIn=0;                                                                                                
// How many samples are captured 
	ALint availBuffers=0;                                                                                              
// Buffers to be recovered 
	ALuint myBuff;                                                                                                     
// The buffer we're using 
	ALuint buffHolder[16];                                                                                             
// An array to hold catch the unqueued buffers 
	bool done = false; 
	while (!done) { 
    alGetSourcei(helloSource[0],AL_BUFFERS_PROCESSED,&availBuffers);                                               
// Poll for recoverable buffers 
    if (availBuffers>0) { 
        alSourceUnqueueBuffers(helloSource[0],availBuffers,buffHolder); 
        for (int ii=0;ii<availBuffers;++ii) { 
            bufferQueue.push_back(buffHolder[ii]);                                                                 
// Push the recovered buffers back on the queue 
        } 
    }       
    alcGetIntegerv(inputDevice,ALC_CAPTURE_SAMPLES,1,&samplesIn);                                                  
// Poll for captured audio 
    if (samplesIn>CAP_SIZE) { 
        alcCaptureSamples(inputDevice,buffer,CAP_SIZE);                                                            
// Grab the sound 
        printf("Grabbed %d samples\n",samplesIn); 

        //***** Process/filter captured data here *****// 
        for (int ii=0;ii<CAP_SIZE;++ii) { 
          buffer[ii]*= 1.01;                                                                                         
// Make it quieter 
        } 

        if (!bufferQueue.empty()) {                                                                                
// Stuff the captured data in a buffer-object 
            myBuff = bufferQueue.front(); bufferQueue.pop_front();                                                 
// We just drop the data if no buffers are available 
           
alBufferData(myBuff,AL_FORMAT_MONO16,buffer,CAP_SIZE*sizeof(short),FREQ); 

            alSourceQueueBuffers(helloSource[0],1,&myBuff);                                                        
// Queue the buffer 

            ALint sState=0; 
            alGetSourcei(helloSource[0],AL_SOURCE_STATE,&sState);                                                  
// Restart the source if needed 
            if (sState!=AL_PLAYING) alSourcePlay(helloSource[0]); 
        } 
    } 
	} 

	alcCaptureStop(inputDevice);                                                                                       
// Stop capture 
	alcCaptureCloseDevice(inputDevice); 

	alSourceStopv(1,&helloSource[0]);                                                                                  
// Stop the sources 
	for (int ii=0;ii<1;++ii) { 
    alSourcei(helloSource[ii],AL_BUFFER,0); 
	} 

	alDeleteSources(1,&helloSource[0]);                                                                                
// Clean-up 
	alDeleteBuffers(16,&helloBuffer[0]); 
	errorCode = alGetError(); 
	alcMakeContextCurrent(NULL); 
	errorCode = alGetError(); 
	alcDestroyContext(audioContext); 
	alcCloseDevice(audioDevice); 

	return 0;
	}

-- 
View this message in context: http://old.nabble.com/Selecting-speaker-output-as-capture-device-tp35337095p35338604.html
Sent from the OpenAL - User mailing list archive at Nabble.com.

_______________________________________________
Openal mailing list
[email protected]
http://opensource.creative.com/mailman/listinfo/openal