Advertisement
iMackshun

AudioManager.cpp

Jan 3rd, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.10 KB | None | 0 0
  1. #include "AudioManager.h"
  2.  
  3. /*STATIC VARS*/
  4. static std::vector<AudioInstance*> _audioQueue;
  5.  
  6. //The callback function. Called whenever audio data is captured, or when audio is needed to output.
  7. static int paStreamCallback(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags, void *userData)
  8. {
  9.     //Cast the userdata to a short pointer. This is the data that was passed into the stream upon the call Pa_OpenStream.
  10.     short* inputData = (short*) userData;
  11.     //Cast the outputData to a short pointer. This is the data that will be outputted to the speaker.
  12.     short *outputData = (short*)outputBuffer;
  13.     //Fill the buffer with data, based on the currently queued sounds. Essentially mixing.
  14.     for (unsigned int i = 0; i < SAMPLES_PER_BUFFER; i++){
  15.         //Zero out the sample.
  16.         inputData[i] = 0;
  17.         //Loop through the currently playing audio.
  18.         for (unsigned int j = 0; j < _audioQueue.size(); j++){
  19.             //Loop through the desired samples.
  20.             for (unsigned int targetSample = _audioQueue.at(j)->_offset; targetSample < _audioQueue.at(j)->_offset + SAMPLES_PER_BUFFER; targetSample++){
  21.                 if (targetSample < (_audioQueue.at(j)->_audioData.wavData.dataSize / sizeof(short))){
  22.                     inputData[i] += _audioQueue.at(j)->_audioData.wavData.wavData_16[targetSample];
  23.                 }
  24.                 else{
  25.                     break;
  26.                 }
  27.             }
  28.             _audioQueue.at(j)->_offset += SAMPLES_PER_BUFFER;
  29.         }
  30.     }
  31.     //Fill the buffer with data from the mixing.
  32.     for (unsigned int i = 0; i < SAMPLES_PER_BUFFER; i++){
  33.         outputData[i] = inputData[i];
  34.     }
  35.     return paContinue;
  36. }
  37.  
  38. //Initializes the platform's audio library.
  39. void AudioManager::Init()
  40. {
  41.     //Allocate memory for the audio data.
  42.     audioData = (short*)malloc(sizeof(short) * SAMPLES_PER_BUFFER);
  43.     memset(audioData, 0, sizeof(short) * SAMPLES_PER_BUFFER);
  44.  
  45.     //Initialize PortAudio.
  46.     PaError error = Pa_Initialize();
  47.     //Error handling.
  48.     if (error != paNoError){
  49.         printf("PortAudio could not be initialized!\n");
  50.     }
  51.    
  52.     /*Set the output parameters.*/
  53.     //Create the parameters object.
  54.     PaStreamParameters outputParameters;
  55.     //Set the device to the default output device.
  56.     outputParameters.device = Pa_GetDefaultOutputDevice();
  57.     //If no default device was found, output an error.
  58.     if (outputParameters.device == paNoDevice){
  59.         printf("The default audio device could not be accessed!\n");
  60.     }
  61.     //Set output settings.
  62.     outputParameters.channelCount = 2;
  63.     outputParameters.sampleFormat = paInt16;
  64.     outputParameters.suggestedLatency = Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency;
  65.     outputParameters.hostApiSpecificStreamInfo = NULL;
  66.  
  67.     //Create the Stream.
  68.     error = Pa_OpenStream(&outputStream, NULL, &outputParameters, SAMPLE_RATE, FRAMES_PER_BUFFER, paClipOff, paStreamCallback, audioData);
  69.     //Error handling.
  70.     if (error != paNoError){
  71.         printf("The stream could not be created!\n");
  72.     }
  73.  
  74.     //Begin the streaming of data.
  75.     error = Pa_StartStream(outputStream);
  76.     //Error handling.
  77.     if (error != paNoError){
  78.         printf("The stream could not be started!\n");
  79.     }
  80. }
  81.  
  82. //Updates the AudioManager. Used to get rid of audio that has completed playback.
  83. void AudioManager::Update()
  84. {
  85.     //Iterate through the queued sounds, removing those that have completed playback.
  86. }
  87.  
  88. //Plays an Audio file.
  89. void AudioManager::PlayAudio(AudioInstance* audioObject)
  90. {
  91.     //Add the object to the vector.
  92.     _audioQueue.push_back(audioObject);
  93. }
  94.  
  95. //Shuts down the platform's audio library and releases memory.
  96. void AudioManager::Destroy()
  97. {
  98.     //End the streaming of data.
  99.     PaError error = Pa_StopStream(outputStream);
  100.     //Error handling.
  101.     if (error != paNoError){
  102.         printf("The stream could not be stopped!\n");
  103.     }
  104.  
  105.     //Close the stream.
  106.     error = Pa_CloseStream(outputStream);
  107.     //Error handling.
  108.     if (error != paNoError){
  109.         printf("The stream could not be closed!\n");
  110.     }
  111.  
  112.     //Shutdown PortAudio.
  113.     error = Pa_Terminate();
  114.     if (error != paNoError){
  115.         printf("PortAudio could not be termindated!\n");
  116.     }
  117. }
  118.  
  119.  
  120.  
  121. //Constructor
  122. AudioManager::AudioManager()
  123. {
  124.  
  125. }
  126.  
  127. //Destructor
  128. AudioManager::~AudioManager()
  129. {
  130.     Destroy();
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement