Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.97 KB | None | 0 0
  1. /*
  2.  * This file is part of EasyRPG Player.
  3.  *
  4.  * EasyRPG Player is free software: you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation, either version 3 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * EasyRPG Player is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
  16.  */
  17.  
  18. #if defined(SWITCH) && defined(SUPPORT_AUDIO)
  19. #include "audio_switch.h"
  20. #include "output.h"
  21.  
  22. #include <switch.h>
  23. #include <vector>
  24. #include <cstdlib>
  25. #include <malloc.h>
  26.  
  27. #define ALIGN_TO(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
  28.  
  29. namespace {
  30.     const int buf_size = 8192;
  31.     const int samplerate = 48000;
  32.     const int bytes_per_sample = 4;
  33.     const int samples_per_buf = buf_size / bytes_per_sample;
  34.     NxAudio* instance = nullptr;
  35. }
  36.  
  37. void NxAudio::Update() {
  38.     uint8_t *buffer1 = (uint8_t*)memalign(0x1000, ALIGN_TO(buf_size, 0x1000));
  39.     uint8_t *buffer2 = (uint8_t*)memalign(0x1000, ALIGN_TO(buf_size, 0x1000));
  40.     uint8_t idx = 0;
  41.    
  42.     AudioOutBuffer source_buffers[2];
  43.    
  44.     // Init audio buffer
  45.     source_buffers[0].next = 0;
  46.     source_buffers[0].buffer = buffer1;
  47.     source_buffers[0].buffer_size = buf_size;
  48.     source_buffers[0].data_size = samples_per_buf * 2;
  49.     source_buffers[0].data_offset = 0;
  50.     source_buffers[1].next = 0;
  51.     source_buffers[1].buffer = buffer2;
  52.     source_buffers[1].buffer_size = buf_size;
  53.     source_buffers[1].data_size = samples_per_buf * 2;
  54.     source_buffers[1].data_offset = 0;
  55.  
  56.     //for(;;) {
  57.         // A pretty bad way to close thread
  58.         if (instance->termStream) {
  59.             instance->termStream = false;
  60.             free(buffer1);
  61.             free(buffer2);
  62.             return;
  63.         }
  64.  
  65.         instance->LockMutex();
  66.         instance->Decode((uint8_t*)source_buffers[idx].buffer, buf_size);
  67.         instance->UnlockMutex();
  68.  
  69.         if (R_FAILED(audoutAppendAudioOutBuffer(&source_buffers[idx]))){
  70.             Output::Error("An error occurred during audio playback.");
  71.         }
  72.        
  73.         idx = (idx + 1) % 2;
  74.     //}
  75. }
  76.  
  77. NxAudio::NxAudio() :
  78.     GenericAudio()
  79. {
  80.     instance = this;
  81.  
  82.     audoutInitialize();
  83.     audoutStartAudioOut();
  84.    
  85.     mutexInit(&audio_mutex);
  86.  
  87.     //threadCreate(&audio_thread, switch_audio_thread, NULL, 0x10000, 0x2C, -2);
  88.    
  89.     SetFormat(samplerate, AudioDecoder::Format::S16, 2);
  90.  
  91.     /*if (R_FAILED(threadStart(&audio_thread))) {
  92.         Output::Error("Failed to init audio thread.");
  93.         return;
  94.     }*/
  95. }
  96.  
  97. NxAudio::~NxAudio() {
  98.     // Closing streaming thread
  99.     termStream = true;
  100.     threadWaitForExit(&audio_thread);
  101.    
  102.     // Deleting thread
  103.     threadClose(&audio_thread);
  104. }
  105.  
  106. void NxAudio::LockMutex() const {
  107.     mutexLock((Mutex*)&audio_mutex);
  108. }
  109.  
  110. void NxAudio::UnlockMutex() const {
  111.     mutexUnlock((Mutex*)&audio_mutex);
  112. }
  113.  
  114. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement