caudio/Examples/Tutorial4_AudioCapture/main.cpp
Joshua Jones 0c3df1d430 Added audio capture capabilities. The user can now get an interface for audio capture from cAudioManager and record audio from the default recording device.
Added a raw audio decoder, allowing raw audio to be played if the format and frequency is known.  Will also load from .raw files on disk, but will assume the format is 16 bit mono and the frequency is 22050 hz
Added a tutorial to show how recording and playback can be done.
Added the ability for the user to unregister audio codecs that they will not be using.  Also added methods to test for the existance of a decoder and get the decoder factory back.
Moved cVector3.h to include instead of Headers to make using it in user projects easier (one less include path)
Moved the audio format enum to its own header file.
Small fix in the wav decoder to prevent the file header from being played as audio data.
2009-08-11 17:57:20 +00:00

104 lines
3.1 KiB
C++

#include <iostream>
#include <string>
//Include IAudioManager so we can easily work with cAudio
#include "../../include/IAudioManager.h"
//Include IAudio so we can create cAudio objects
#include "../../include/IAudio.h"
using namespace std;
#define CAPTURE_FREQUENCY 22050
#define CAPTURE_DURATION 10
#define CAPTURE_FORMAT cAudio::EAF_16BIT_MONO
int main(int argc, char* argv[])
{
//To make visual studio happy
cAudio::IAudio* mysound = NULL;
//Some fancy text
cout << "cAudio 1.7.1 Tutorial 4: Capturing Audio \n \n";
std::string formatName;
if(CAPTURE_FORMAT == cAudio::EAF_8BIT_MONO)
formatName = "8 Bit Mono";
else if(CAPTURE_FORMAT == cAudio::EAF_8BIT_STEREO)
formatName = "8 Bit Stereo";
else if(CAPTURE_FORMAT == cAudio::EAF_16BIT_MONO)
formatName = "16 Bit Mono";
else
formatName = "16 Bit Stereo";
//Grap the cAudioManager
cAudio::IAudioManager* manager = cAudio::getAudioManager();
//Init the cAudio Engine
manager->init(argc,argv);
//! The capture interface should be grabbed after the manager has been initialized
cAudio::IAudioCapture* capture = manager->getAudioCapture();
bool captureReady = false;
cout << "Capturing Supported: " << std::boolalpha << capture->isSupported() << "\n";
if(capture->isSupported())
{
captureReady = capture->initialize(CAPTURE_FREQUENCY, CAPTURE_FORMAT);
cout << "Ready to capture audio: " << std::boolalpha << captureReady << "\n \n";
//Quick math to figure out how large our data should be for the duration of the record time
const int targetRecordSize = CAPTURE_FREQUENCY * CAPTURE_DURATION * capture->getSampleSize();
cout << "Capture Frequency: " << CAPTURE_FREQUENCY << "\n";
cout << "Capture Duration: " << CAPTURE_DURATION << "\n";
cout << "Capture Format: " << formatName << "\n";
cout << "Sample Size: " << capture->getSampleSize() << "\n";
cout << "Total size of audio: " << targetRecordSize << "\n";
cout << std::endl;
int currentsize = 0;
cout << "Starting capture... \n";
if(capture->beginCapture())
{
while(currentsize < targetRecordSize)
{
currentsize = capture->getCurrentCapturedAudioSize();
//Run the main update loop to keep audio data flowing in
manager->update();
}
}
capture->stopCapture();
cout << "Capture stopped... \n \n";
char* buffer = new char[currentsize];
capture->getCapturedAudio(buffer, currentsize);
//Create a IAudio object and load a sound from a file
mysound = manager->createFromRaw("sound1", buffer, currentsize, CAPTURE_FREQUENCY, CAPTURE_FORMAT);
delete buffer;
if(mysound)
{
cout << "Playing back captured audio... \n \n";
mysound->setVolume(1.0);
//Set the IAudio Sound to play2d and loop
mysound->play2d(false);
while(mysound->playing())
{
//Playback sound
manager->update();
}
}
}
//Delete all IAudio sounds
manager->release();
//Shutdown cAudio
manager->shutDown();
std::cout << "Press any key to quit \n";
std::cin.get();
return 0;
}