Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "AudioManager.h"
- /*STATIC VARS*/
- static std::vector<AudioInstance*> _audioQueue;
- //The callback function. Called whenever audio data is captured, or when audio is needed to output.
- static int paStreamCallback(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags, void *userData)
- {
- //Cast the userdata to a short pointer. This is the data that was passed into the stream upon the call Pa_OpenStream.
- short* inputData = (short*) userData;
- //Cast the outputData to a short pointer. This is the data that will be outputted to the speaker.
- short *outputData = (short*)outputBuffer;
- //Fill the buffer with data, based on the currently queued sounds. Essentially mixing.
- for (unsigned int i = 0; i < SAMPLES_PER_BUFFER; i++){
- //Zero out the sample.
- inputData[i] = 0;
- //Loop through the currently playing audio.
- for (unsigned int j = 0; j < _audioQueue.size(); j++){
- //Loop through the desired samples.
- for (unsigned int targetSample = _audioQueue.at(j)->_offset; targetSample < _audioQueue.at(j)->_offset + SAMPLES_PER_BUFFER; targetSample++){
- if (targetSample < (_audioQueue.at(j)->_audioData.wavData.dataSize / sizeof(short))){
- inputData[i] += _audioQueue.at(j)->_audioData.wavData.wavData_16[targetSample];
- }
- else{
- break;
- }
- }
- _audioQueue.at(j)->_offset += SAMPLES_PER_BUFFER;
- }
- }
- //Fill the buffer with data from the mixing.
- for (unsigned int i = 0; i < SAMPLES_PER_BUFFER; i++){
- outputData[i] = inputData[i];
- }
- return paContinue;
- }
- //Initializes the platform's audio library.
- void AudioManager::Init()
- {
- //Allocate memory for the audio data.
- audioData = (short*)malloc(sizeof(short) * SAMPLES_PER_BUFFER);
- memset(audioData, 0, sizeof(short) * SAMPLES_PER_BUFFER);
- //Initialize PortAudio.
- PaError error = Pa_Initialize();
- //Error handling.
- if (error != paNoError){
- printf("PortAudio could not be initialized!\n");
- }
- /*Set the output parameters.*/
- //Create the parameters object.
- PaStreamParameters outputParameters;
- //Set the device to the default output device.
- outputParameters.device = Pa_GetDefaultOutputDevice();
- //If no default device was found, output an error.
- if (outputParameters.device == paNoDevice){
- printf("The default audio device could not be accessed!\n");
- }
- //Set output settings.
- outputParameters.channelCount = 2;
- outputParameters.sampleFormat = paInt16;
- outputParameters.suggestedLatency = Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency;
- outputParameters.hostApiSpecificStreamInfo = NULL;
- //Create the Stream.
- error = Pa_OpenStream(&outputStream, NULL, &outputParameters, SAMPLE_RATE, FRAMES_PER_BUFFER, paClipOff, paStreamCallback, audioData);
- //Error handling.
- if (error != paNoError){
- printf("The stream could not be created!\n");
- }
- //Begin the streaming of data.
- error = Pa_StartStream(outputStream);
- //Error handling.
- if (error != paNoError){
- printf("The stream could not be started!\n");
- }
- }
- //Updates the AudioManager. Used to get rid of audio that has completed playback.
- void AudioManager::Update()
- {
- //Iterate through the queued sounds, removing those that have completed playback.
- }
- //Plays an Audio file.
- void AudioManager::PlayAudio(AudioInstance* audioObject)
- {
- //Add the object to the vector.
- _audioQueue.push_back(audioObject);
- }
- //Shuts down the platform's audio library and releases memory.
- void AudioManager::Destroy()
- {
- //End the streaming of data.
- PaError error = Pa_StopStream(outputStream);
- //Error handling.
- if (error != paNoError){
- printf("The stream could not be stopped!\n");
- }
- //Close the stream.
- error = Pa_CloseStream(outputStream);
- //Error handling.
- if (error != paNoError){
- printf("The stream could not be closed!\n");
- }
- //Shutdown PortAudio.
- error = Pa_Terminate();
- if (error != paNoError){
- printf("PortAudio could not be termindated!\n");
- }
- }
- //Constructor
- AudioManager::AudioManager()
- {
- }
- //Destructor
- AudioManager::~AudioManager()
- {
- Destroy();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement