Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "AudioManager.h"
- /*STATIC VARS*/
- static std::vector<AudioInstance*> _audioQueue;
- //Initializes the platform's audio library.
- void AudioManager::Init()
- {
- //Allocate memory for the audio data.
- audioData = (short*)linearAlloc(sizeof(short) * SAMPLES_PER_BUFFER * 2);
- memset(audioData, 0, sizeof(short) * SAMPLES_PER_BUFFER * 2);
- //Initialize the DSP.
- ndspInit();
- //Configure the DSP for stereo output.
- ndspSetOutputMode(NDSP_OUTPUT_MONO);
- //Set the interpolation?
- ndspChnSetInterp(0, NDSP_INTERP_LINEAR);
- //Set the Sample Rate.
- ndspChnSetRate(0, SAMPLE_RATE);
- //Set the sample format.
- ndspChnSetFormat(0, NDSP_FORMAT_MONO_PCM16);
- //Set the internal mixer to access the left and right speakers.
- float mix[12];
- memset(mix, 0, sizeof(mix));
- mix[0] = 1.0;
- mix[1] = 1.0;
- ndspChnSetMix(0, mix);
- //Set the memory addresses of the buffers to that of the Audio Buffer.
- memset(waveBuf, 0, sizeof(waveBuf));
- waveBuf[0].data_vaddr = &audioData[0];
- waveBuf[0].nsamples = SAMPLES_PER_BUFFER;
- waveBuf[1].data_vaddr = &audioData[SAMPLES_PER_BUFFER];
- waveBuf[1].nsamples = SAMPLES_PER_BUFFER;
- //Flush the data.
- DSP_FlushDataCache(audioData, SAMPLES_PER_BUFFER * 2);
- //Add the buffers to be processed.
- ndspChnWaveBufAdd(0, &waveBuf[0]);
- ndspChnWaveBufAdd(0, &waveBuf[1]);
- }
- //Updates the AudioManager. Used to get rid of audio that has completed playback.
- void AudioManager::Update()
- {
- //If the contents of the buffer have completed playback, update the audioData and swap buffers.
- if (waveBuf[activeBuffer].status == NDSP_WBUF_DONE) {
- //Zero out the data.
- memset(audioData, 0, sizeof(short) * SAMPLES_PER_BUFFER * 2);
- //Loop through the currently playing audio and mix.
- for (unsigned int j = 0; j < _audioQueue.size(); j++){
- //Loop through the desired samples.
- for (unsigned int targetSample = 0; targetSample < SAMPLES_PER_BUFFER * 2; targetSample++){
- if ((targetSample + _audioQueue.at(j)->_offset) < (_audioQueue.at(j)->_audioData.wavData.dataSize / sizeof(short))){
- audioData[targetSample] += _audioQueue.at(j)->_audioData.wavData.wavData_16[targetSample + _audioQueue.at(j)->_offset];
- }
- else{
- break;
- }
- }
- _audioQueue.at(j)->_offset += SAMPLES_PER_BUFFER;
- }
- //Flush the data.
- DSP_FlushDataCache(audioData, SAMPLES_PER_BUFFER * 2);
- //Add the buffer.
- ndspChnWaveBufAdd(0, &waveBuf[activeBuffer]);
- //Switch buffers.
- activeBuffer = !activeBuffer;
- //Iterate through the queued sounds, removing those that have completed playback.
- std::vector<AudioInstance*>::iterator iter = _audioQueue.begin();
- while (iter != _audioQueue.end()){
- if ((*iter)->_offset >= ((*iter)->_audioData.wavData.dataSize / sizeof(short))){
- iter = _audioQueue.erase(iter);
- }
- else{
- iter++;
- }
- }
- }
- }
- //Used to determine whether the audio is currently playing.
- bool AudioManager::isAudioPlaying(AudioInstance* audioInstance){
- bool isPlaying = false;
- for (unsigned int i = 0; i < _audioQueue.size(); i++){
- if (_audioQueue.at(i) == audioInstance){
- isPlaying = true;
- }
- }
- return isPlaying;
- }
- //Plays an Audio file.
- void AudioManager::PlayAudio(AudioInstance* audioInstance)
- {
- //Only add the sound to the vector if an instance isn't already there.
- if (!isAudioPlaying(audioInstance)){
- //Reset the audio object.
- audioInstance->SetRelativePosition(0.0f);
- //Add the object to the vector.
- _audioQueue.push_back(audioInstance);
- }
- else{
- //Reset the audio object.
- audioInstance->SetRelativePosition(0.0f);
- }
- }
- //Pauses a specific AudioInstance’s playback.
- void AudioManager::PauseAudio(AudioInstance* audioObject)
- {
- }
- //Stops an AudioInstance’s, removing it from the queue.
- void AudioManager::StopAudio(AudioInstance* audioObject)
- {
- }
- //Shuts down the platform's audio library and releases memory.
- void AudioManager::Destroy()
- {
- //Stop the DSP.
- ndspExit();
- //Free the AudioData.
- linearFree(audioData);
- }
- //Clears all of the currently playing sounds.
- void AudioManager::Clear(){
- _audioQueue.clear();
- }
- //Constructor
- AudioManager::AudioManager()
- {
- }
- //Destructor
- AudioManager::~AudioManager()
- {
- Destroy();
- }
Add Comment
Please, Sign In to add comment