bbmusic.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include "bbmusic.h"
  2. #if __APPLE__
  3. #include <OpenAL/al.h>
  4. #include <OpenAL/alc.h>
  5. #else
  6. #include <AL/al.h>
  7. #include <AL/alc.h>
  8. #endif
  9. #include "../../../std/async/native/async.h"
  10. #include "../../../std/async/native/async_cb.h"
  11. #include "../../../stb-vorbis/native/stb-vorbis.h"
  12. #if __ANDROID__
  13. #include "../../../sdl2/SDL/src/core/android/SDL_android.h"
  14. #endif
  15. #include <atomic>
  16. namespace{
  17. struct Counter;
  18. Counter *counters;
  19. //Yikes! We have to make a little atomic counter class!
  20. //
  21. struct Counter{
  22. Counter *succ;
  23. int source;
  24. std::atomic<int> count{};
  25. Counter( int source ):source( source ){
  26. succ=counters;
  27. counters=this;
  28. }
  29. ~Counter(){
  30. Counter **pred=&counters;
  31. while( Counter *c=*pred ){
  32. if( c==this ){
  33. *pred=succ;
  34. }
  35. pred=&c->succ;
  36. }
  37. }
  38. };
  39. Counter *getCounter( int source ){
  40. for( Counter *c=counters;c;c=c->succ ){
  41. if( c->source==source ) return c;
  42. }
  43. return 0;
  44. }
  45. }
  46. namespace bbMusic{
  47. #if __ANDROID__
  48. int android_read( void *cookie,char *buf,int size ){
  49. return AAsset_read( (AAsset*)cookie,buf,size );
  50. }
  51. int android_write( void *cookie,const char* buf,int size ){
  52. return EACCES; // can't provide write access to the apk
  53. }
  54. fpos_t android_seek( void *cookie,fpos_t offset,int whence ){
  55. return AAsset_seek( (AAsset*)cookie,offset,whence );
  56. }
  57. int android_close(void* cookie) {
  58. AAsset_close( (AAsset*)cookie );
  59. return 0;
  60. }
  61. #endif
  62. FILE *fopen( const char *path,const char *mode ){
  63. #if __ANDROID__
  64. if( !strncmp( path,"asset::",7 ) ){
  65. AAssetManager *assetManager=Android_JNI_GetAssetManager();
  66. if( !assetManager ) return 0;
  67. AAsset* asset=AAssetManager_open( assetManager,path+7,0 );
  68. if( !asset ) return 0;
  69. return funopen( asset,android_read,android_write,android_seek,android_close );
  70. }
  71. #endif
  72. return ::fopen( path,mode );
  73. }
  74. int playMusic( const char *path,int callback,int alsource ){
  75. const int buffer_ms=100;
  76. FILE *file=fopen( path,"rb" );
  77. if( !file ) return false;
  78. int error=0;
  79. stb_vorbis *vorbis=stb_vorbis_open_file( file,0,&error,0 );
  80. if( !vorbis ) return false;
  81. stb_vorbis_info info=stb_vorbis_get_info( vorbis );
  82. Counter *buffersProcessed=new Counter( alsource );
  83. std::thread thread( [=](){
  84. ALuint source=alsource;
  85. // int length=stb_vorbis_stream_length_in_samples( vorbis );
  86. // float duration=stb_vorbis_stream_length_in_seconds( vorbis );
  87. // bb_printf( "vorbis length=%i, duration=%f, info.sample_rate=%i, info.channels=%i\n",length,duration,info.sample_rate,info.channels );
  88. ALenum format=(info.channels==2 ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16);
  89. int nsamples=buffer_ms*info.sample_rate/1000;
  90. int buffer_size=nsamples * (info.channels==2 ? 4 : 2);
  91. // bb_printf( "Samples per buffer=%i\n",nsamples );
  92. //polling for paused occasionally fails with only 2 buffers
  93. const int numbufs=3;
  94. ALuint buffers[numbufs];
  95. alGenBuffers( numbufs,buffers );
  96. short *vorbis_data=new short[buffer_size/2];
  97. int n=buffer_size;
  98. for( int i=0;i<numbufs;++i ){
  99. if( n ) n=stb_vorbis_get_samples_short_interleaved( vorbis,info.channels,vorbis_data,buffer_size/2 );
  100. alBufferData( buffers[i],format,vorbis_data,buffer_size,info.sample_rate );
  101. }
  102. alSourceQueueBuffers( source,numbufs,buffers );
  103. alSourcePlay( source );
  104. // bb_printf( "Playing music...\n" );
  105. for(;;){
  106. //decode more...
  107. if( n ) n=stb_vorbis_get_samples_short_interleaved( vorbis,info.channels,vorbis_data,buffer_size/2 );
  108. ALenum state;
  109. for(;;){
  110. alGetSourcei( source,AL_SOURCE_STATE,&state );
  111. if( state==AL_STOPPED ) break;
  112. if( state==AL_PLAYING ){
  113. ALint processed;
  114. alGetSourcei( source,AL_BUFFERS_PROCESSED,&processed );
  115. // if( processed>1 ) bb_printf( "processed=%i\n",processed );
  116. if( processed ) break;
  117. }
  118. std::this_thread::sleep_for( std::chrono::milliseconds( buffer_ms/2 ) );
  119. }
  120. if( state==AL_STOPPED ){
  121. // bb_printf( "AL_STOPPED\n" );
  122. break;
  123. }
  124. buffersProcessed->count+=1;
  125. ALuint buffer;
  126. alSourceUnqueueBuffers( source,1,&buffer );
  127. if( !n ) continue;
  128. alBufferData( buffer,format,vorbis_data,buffer_size,info.sample_rate );
  129. alSourceQueueBuffers( source,1,&buffer );
  130. }
  131. // bb_printf( "Music done.\n" );
  132. alSourceStop( source );
  133. alDeleteBuffers( numbufs,buffers );
  134. delete[] vorbis_data;
  135. stb_vorbis_close( vorbis );
  136. fclose( file );
  137. bbAsync::invokeAsyncCallback( callback );
  138. } );
  139. thread.detach();
  140. return info.sample_rate;
  141. }
  142. int getBuffersProcessed( int source ){
  143. Counter *c=getCounter( source );
  144. if( c ) return c->count;
  145. return 0;
  146. }
  147. void endMusic( int source ){
  148. Counter *c=getCounter( source );
  149. delete c;
  150. }
  151. }