Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <malloc.h>
  5. #include <inttypes.h>
  6. #include <switch.h>
  7. #include "sample_bin.h"
  8. #include <atomic>
  9.  
  10. // Sample comes from this website:
  11. // https://www.soundjay.com/magic-sound-effect.html
  12.  
  13. static Mutex drvMutex;
  14. static AudioDriver drv;
  15. std::atomic<bool> THREAD_RUN;
  16.  
  17. void DriverThread(void *)
  18. {
  19.     while (THREAD_RUN)
  20.     {
  21.         mutexLock(&drvMutex);
  22.         audrvUpdate(&drv);
  23.         mutexLock(&drvMutex);
  24.     }
  25. }
  26.  
  27. int main(void)
  28. {
  29.     consoleInit(NULL);
  30.  
  31.     printf("Simple audren demonstration program\n");
  32.  
  33.     static const AudioRendererConfig arConfig =
  34.     {
  35.         .output_rate     = AudioRendererOutputRate_48kHz,
  36.         .num_voices      = 24,
  37.         .num_effects     = 0,
  38.         .num_sinks       = 1,
  39.         .num_mix_objs    = 1,
  40.         .num_mix_buffers = 2,
  41.     };
  42.  
  43.     size_t mempool_size = (sample_bin_size + 0xFFF) &~ 0xFFF;
  44.     void* mempool_ptr = memalign(0x1000, mempool_size);
  45.     memcpy(mempool_ptr, sample_bin, sample_bin_size);
  46.     armDCacheFlush(mempool_ptr, mempool_size);
  47.  
  48.     AudioDriverWaveBuf wavebuf = {0};
  49.     wavebuf.data_raw = mempool_ptr;
  50.     wavebuf.size = sample_bin_size;
  51.     wavebuf.start_sample_offset = 0;
  52.     wavebuf.end_sample_offset = sample_bin_size/2;
  53.     //wavebuf.is_looping = true;
  54.  
  55.     Result res;
  56.     res = audrenInitialize(&arConfig);
  57.     bool initedDriver = false;
  58.     bool initedAudren = R_SUCCEEDED(res);
  59.  
  60.     THREAD_RUN = false;
  61.  
  62.     if (!initedAudren)
  63.         printf("audrenInitialize: %08" PRIx32 "\n", res);
  64.     else
  65.     {
  66.         printf("audren initted!\n");
  67.         res = audrvCreate(&drv, &arConfig, 2);
  68.         initedDriver = R_SUCCEEDED(res);
  69.         if (R_FAILED(res))
  70.             printf("audrvCreate: %08" PRIx32 "\n", res);
  71.         else
  72.         {
  73.             int mpid = audrvMemPoolAdd(&drv, mempool_ptr, mempool_size);
  74.             audrvMemPoolAttach(&drv, mpid);
  75.  
  76.             static const u8 sink_channels[] = { 0, 1 };
  77.             int sink = audrvDeviceSinkAdd(&drv, AUDREN_DEFAULT_DEVICE_NAME, 2, sink_channels);
  78.  
  79.             res = audrvUpdate(&drv);
  80.             printf("audrvUpdate: %" PRIx32 "\n", res);
  81.  
  82.             res = audrenStartAudioRenderer();
  83.             printf("audrenStartAudioRenderer: %" PRIx32 "\n", res);
  84.  
  85.             audrvVoiceInit(&drv, 0, 1, PcmFormat_Int16, 48000);
  86.             audrvVoiceSetDestinationMix(&drv, 0, AUDREN_FINAL_MIX_ID);
  87.             audrvVoiceSetMixFactor(&drv, 0, 1.0f, 0, 0);
  88.             audrvVoiceSetMixFactor(&drv, 0, 1.0f, 0, 1);
  89.             audrvVoiceStart(&drv, 0);
  90.  
  91.             mutexInit(&drvMutex);
  92.             THREAD_RUN = true;
  93.  
  94.             u32 priority = 0;
  95.             svcGetThreadPriority(&priority, CUR_THREAD_HANDLE);
  96.             Thread thread;
  97.  
  98.             res = threadCreate(&thread, DriverThread, NULL, NULL, 0x10000, priority - 1, 0);
  99.  
  100.             if (R_SUCCEEDED(res))
  101.                 threadStart(&thread);
  102.         }
  103.     }
  104.     printf("done. Press A to play a sound.\n");
  105.  
  106.     // Main loop
  107.     while (appletMainLoop())
  108.     {
  109.         hidScanInput();
  110.  
  111.         u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
  112.  
  113.         if (kDown & KEY_PLUS)
  114.             break;
  115.  
  116.         if (initedDriver)
  117.         {
  118.             if (kDown & KEY_A)
  119.             {
  120.                 mutexLock(&drvMutex);
  121.  
  122.                 audrvVoiceStop(&drv, 0);
  123.                 audrvVoiceAddWaveBuf(&drv, 0, &wavebuf);
  124.                 audrvVoiceStart(&drv, 0);
  125.  
  126.                 mutexUnlock(&drvMutex);
  127.             }
  128.         }
  129.  
  130.         consoleUpdate(NULL);
  131.     }
  132.  
  133.     if (initedDriver)
  134.         audrvClose(&drv);
  135.     if (initedAudren)
  136.         audrenExit();
  137.  
  138.     consoleExit(NULL);
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement