iMackshun

AudioManager.cpp

Jan 8th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. #include "AudioManager.h"
  2.  
  3. /*STATIC VARS*/
  4. static std::vector<AudioInstance*> _audioQueue;
  5.  
  6. //Initializes the platform's audio library.
  7. void AudioManager::Init()
  8. {
  9. //Allocate memory for the audio data.
  10. audioData = (short*)linearAlloc(sizeof(short) * SAMPLES_PER_BUFFER * 2);
  11. memset(audioData, 0, sizeof(short) * SAMPLES_PER_BUFFER * 2);
  12.  
  13. //Initialize the DSP.
  14. ndspInit();
  15. //Configure the DSP for stereo output.
  16. ndspSetOutputMode(NDSP_OUTPUT_MONO);
  17. //Set the interpolation?
  18. ndspChnSetInterp(0, NDSP_INTERP_LINEAR);
  19. //Set the Sample Rate.
  20. ndspChnSetRate(0, SAMPLE_RATE);
  21. //Set the sample format.
  22. ndspChnSetFormat(0, NDSP_FORMAT_MONO_PCM16);
  23. //Set the internal mixer to access the left and right speakers.
  24. float mix[12];
  25. memset(mix, 0, sizeof(mix));
  26. mix[0] = 1.0;
  27. mix[1] = 1.0;
  28. ndspChnSetMix(0, mix);
  29. //Set the memory addresses of the buffers to that of the Audio Buffer.
  30. memset(waveBuf, 0, sizeof(waveBuf));
  31. waveBuf[0].data_vaddr = &audioData[0];
  32. waveBuf[0].nsamples = SAMPLES_PER_BUFFER;
  33. waveBuf[1].data_vaddr = &audioData[SAMPLES_PER_BUFFER];
  34. waveBuf[1].nsamples = SAMPLES_PER_BUFFER;
  35. //Flush the data.
  36. DSP_FlushDataCache(audioData, SAMPLES_PER_BUFFER * 2);
  37. //Add the buffers to be processed.
  38. ndspChnWaveBufAdd(0, &waveBuf[0]);
  39. ndspChnWaveBufAdd(0, &waveBuf[1]);
  40. }
  41.  
  42. //Updates the AudioManager. Used to get rid of audio that has completed playback.
  43. void AudioManager::Update()
  44. {
  45. //If the contents of the buffer have completed playback, update the audioData and swap buffers.
  46. if (waveBuf[activeBuffer].status == NDSP_WBUF_DONE) {
  47. //Zero out the data.
  48. memset(audioData, 0, sizeof(short) * SAMPLES_PER_BUFFER * 2);
  49. //Loop through the currently playing audio and mix.
  50. for (unsigned int j = 0; j < _audioQueue.size(); j++){
  51. //Loop through the desired samples.
  52. for (unsigned int targetSample = 0; targetSample < SAMPLES_PER_BUFFER * 2; targetSample++){
  53. if ((targetSample + _audioQueue.at(j)->_offset) < (_audioQueue.at(j)->_audioData.wavData.dataSize / sizeof(short))){
  54. audioData[targetSample] += _audioQueue.at(j)->_audioData.wavData.wavData_16[targetSample + _audioQueue.at(j)->_offset];
  55. }
  56. else{
  57. break;
  58. }
  59. }
  60. _audioQueue.at(j)->_offset += SAMPLES_PER_BUFFER;
  61. }
  62. //Flush the data.
  63. DSP_FlushDataCache(audioData, SAMPLES_PER_BUFFER * 2);
  64. //Add the buffer.
  65. ndspChnWaveBufAdd(0, &waveBuf[activeBuffer]);
  66. //Switch buffers.
  67. activeBuffer = !activeBuffer;
  68. //Iterate through the queued sounds, removing those that have completed playback.
  69. std::vector<AudioInstance*>::iterator iter = _audioQueue.begin();
  70. while (iter != _audioQueue.end()){
  71. if ((*iter)->_offset >= ((*iter)->_audioData.wavData.dataSize / sizeof(short))){
  72. iter = _audioQueue.erase(iter);
  73. }
  74. else{
  75. iter++;
  76. }
  77. }
  78. }
  79. }
  80.  
  81. //Used to determine whether the audio is currently playing.
  82. bool AudioManager::isAudioPlaying(AudioInstance* audioInstance){
  83. bool isPlaying = false;
  84. for (unsigned int i = 0; i < _audioQueue.size(); i++){
  85. if (_audioQueue.at(i) == audioInstance){
  86. isPlaying = true;
  87. }
  88. }
  89. return isPlaying;
  90. }
  91.  
  92. //Plays an Audio file.
  93. void AudioManager::PlayAudio(AudioInstance* audioInstance)
  94. {
  95. //Only add the sound to the vector if an instance isn't already there.
  96. if (!isAudioPlaying(audioInstance)){
  97. //Reset the audio object.
  98. audioInstance->SetRelativePosition(0.0f);
  99. //Add the object to the vector.
  100. _audioQueue.push_back(audioInstance);
  101. }
  102. else{
  103. //Reset the audio object.
  104. audioInstance->SetRelativePosition(0.0f);
  105. }
  106. }
  107.  
  108. //Pauses a specific AudioInstance’s playback.
  109. void AudioManager::PauseAudio(AudioInstance* audioObject)
  110. {
  111.  
  112. }
  113. //Stops an AudioInstance’s, removing it from the queue.
  114. void AudioManager::StopAudio(AudioInstance* audioObject)
  115. {
  116.  
  117. }
  118.  
  119. //Shuts down the platform's audio library and releases memory.
  120. void AudioManager::Destroy()
  121. {
  122. //Stop the DSP.
  123. ndspExit();
  124. //Free the AudioData.
  125. linearFree(audioData);
  126. }
  127.  
  128. //Clears all of the currently playing sounds.
  129. void AudioManager::Clear(){
  130. _audioQueue.clear();
  131. }
  132.  
  133.  
  134. //Constructor
  135. AudioManager::AudioManager()
  136. {
  137.  
  138. }
  139.  
  140. //Destructor
  141. AudioManager::~AudioManager()
  142. {
  143. Destroy();
  144. }
Add Comment
Please, Sign In to add comment