Advertisement
GeeckoDev

glibaudio v0

Oct 29th, 2011
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.39 KB | None | 0 0
  1. // gLibAudio by Geecko - A rock-solid, light-weight audio library.
  2. //
  3. // This work is licensed under the Creative Commons BY-SA 3.0 Unported License.
  4. // See LICENSE for more details.
  5. //
  6. // Please report bugs at : geecko.dev@free.fr
  7.  
  8. #include "glibaudio.h"
  9.  
  10. #include <pspkernel.h>
  11. #include <pspaudio.h>
  12. #include <psputility.h>
  13. #include <pspmp3.h>
  14. #include <pspdebug.h> // TODO remove
  15.  
  16. #include <malloc.h>
  17.  
  18. /* Global variables */
  19.  
  20. static bool init=false;
  21.  
  22. /* Internal functions */
  23.  
  24. bool _gaRead(gaSound *snd)
  25. {
  26.   unsigned char* data;
  27.   int size, offset;
  28.  
  29.   sceMp3GetInfoToAddStreamData(snd->handle, &data, &size, &offset);
  30.   sceIoLseek32(snd->file, offset, PSP_SEEK_SET);
  31.   sceMp3NotifyAddStreamData(snd->handle, sceIoRead(snd->file, data, size));
  32.  
  33.   return false;
  34. }
  35.  
  36. /* Main functions */
  37.  
  38. void gaInit()
  39. {
  40.   if (init) return;
  41.  
  42.   sceUtilityLoadModule(PSP_MODULE_AV_AVCODEC);
  43.   sceUtilityLoadModule(PSP_MODULE_AV_MP3);
  44.  
  45.   sceMp3InitResource();
  46.  
  47.   init = true;
  48. }
  49.  
  50.  
  51. void gaTerm()
  52. {
  53.   if (!init) return;
  54.  
  55.   sceMp3TermResource();
  56.  
  57.   sceUtilityUnloadModule(PSP_MODULE_AV_AVCODEC);
  58.   sceUtilityUnloadModule(PSP_MODULE_AV_MP3);
  59.  
  60.   init = false;
  61. }
  62.  
  63.  
  64. gaSound* gaLoad(const char *path)
  65. {
  66.   if (path == NULL) return NULL;
  67.   if (!init) gaInit();
  68.  
  69.   gaSound* snd = malloc(sizeof(gaSound));
  70.  
  71.   snd->file = sceIoOpen(path, PSP_O_RDONLY, 0777);
  72.   snd->init.mp3StreamStart = 0;
  73.   snd->init.mp3StreamEnd = sceIoLseek32(snd->file, 0, PSP_SEEK_END);
  74.   snd->init.unk1 = 0;
  75.   snd->init.unk2 = 0;
  76.   snd->init.mp3Buf = malloc(8192);
  77.   snd->init.mp3BufSize = 8192;
  78.   snd->init.pcmBuf = malloc(9216);
  79.   snd->init.pcmBufSize = 9216;
  80.   snd->handle = sceMp3ReserveMp3Handle(&snd->init);
  81.  
  82.   _gaRead(snd);
  83.   sceMp3Init(snd->handle);
  84.  
  85.   return snd;
  86. }
  87.  
  88.  
  89. void gaFree(gaSound** snd)
  90. {
  91.   if (snd == NULL) return;
  92.   if (!init) gaInit();
  93.  
  94.   sceMp3ReleaseMp3Handle((*snd)->handle);
  95.   free((*snd)->init.mp3Buf);
  96.   free((*snd)->init.pcmBuf);
  97.   sceIoClose((*snd)->file);
  98.  
  99.   free(*snd);
  100.   *snd = NULL;
  101. }
  102.  
  103.  
  104. void gaPlay(gaSound *snd)
  105. {
  106.   if (snd == NULL) return;
  107.   if (!init) gaInit();
  108.  
  109.   short *buf;
  110.   sceAudioChReserve(0, 1152, PSP_AUDIO_FORMAT_STEREO);
  111.  
  112.   while (1)
  113.   {
  114.     if (sceMp3CheckStreamDataNeeded(snd->handle) > 0)
  115.     {
  116.       _gaRead(snd);
  117.     }
  118.  
  119.     sceMp3Decode(snd->handle, &buf);
  120.     sceAudioOutputBlocking(0, 0x8000, buf);
  121.   }
  122. }
  123.  
  124. // EOF
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement