ソースを参照

PlayMusic tweaks + docs.

Mark Sibly 8 年 前
コミット
05248f96ad
2 ファイル変更26 行追加10 行削除
  1. 12 3
      modules/mojo/audio/audio.monkey2
  2. 14 7
      modules/mojo/audio/native/bbmusic.cpp

+ 12 - 3
modules/mojo/audio/audio.monkey2

@@ -43,13 +43,23 @@ An instance of the AudioDevice class is automatically created when an [[AppInsta
 
 #end
 Class AudioDevice
+	
+	#rem monkeydoc Starts streaming audio playback.
+	
+	PlayMusic starts a piece of audio streaming from a file in the background.
 
+	When the audio finishes, the optional `finished` function is invoked.
+	
+	The returned [[Channel]] instance is automatically discarded when the audio stops, so should not be discarded by your code.
+	
+	The audio file must be in .ogg format.
+	
+	#end
 	Method PlayMusic:Channel( path:String,finished:Void()=Null,paused:Bool=False )
 		
-		Local channel:=New Channel( Null )
+		Local channel:=New Channel( ChannelFlags.AutoDiscard )
 		
 		Local callback:=async.CreateAsyncCallback( Lambda()
-			channel.Discard()
 			finished()
 		End,True )
 		
@@ -57,7 +67,6 @@ Class AudioDevice
 		
 		If Not playMusic( path,callback,channel._alSource ) 
 			async.DestroyAsyncCallback( callback )
-			channel.Discard()
 			Return Null
 		Endif
 		

+ 14 - 7
modules/mojo/audio/native/bbmusic.cpp

@@ -75,6 +75,7 @@ namespace bbMusic{
 			ALuint source=alsource;
 	
 			stb_vorbis_info info=stb_vorbis_get_info( vorbis );
+			
 //			int length=stb_vorbis_stream_length_in_samples( vorbis );
 //			float duration=stb_vorbis_stream_length_in_seconds( vorbis );
 //			bb_printf( "vorbis length=%i, duration=%f, info.sample_rate=%i, info.channels=%i\n",length,duration,info.sample_rate,info.channels );
@@ -85,24 +86,27 @@ namespace bbMusic{
 //			int nsamples=buffer_size / (info.channels==2 ? 4 : 2);
 //			int buffer_ms=nsamples*1000/info.sample_rate;
 			
-			int buffer_ms=50;
+			int buffer_ms=100;
 			int nsamples=buffer_ms*info.sample_rate/1000;
 			int buffer_size=nsamples * (info.channels==2 ? 4 : 2);
 			
 //			bb_printf( "buffer_size=%i, buffer_ms=%i\n",buffer_size,buffer_ms );
 			
 			//polling for paused occasionally fails with only 2 buffers
-			ALuint buffers[3];
-			alGenBuffers( 3,buffers );
+			
+			const int numbufs=3;
+			
+			ALuint buffers[numbufs];
+			alGenBuffers( numbufs,buffers );
 			
 			short *vorbis_data=new short[buffer_size/2];
 			
-			for( int i=0;i<3;++i ){
+			for( int i=0;i<numbufs;++i ){
 				int n=stb_vorbis_get_samples_short_interleaved( vorbis,info.channels,vorbis_data,buffer_size/2 );
 				alBufferData( buffers[i],format,vorbis_data,buffer_size,info.sample_rate );
 			}
 			
-			alSourceQueueBuffers( source,3,buffers );
+			alSourceQueueBuffers( source,numbufs,buffers );
 			
 			alSourcePlay( source );
 			
@@ -118,21 +122,24 @@ namespace bbMusic{
 					for(;;){
 						alGetSourcei( source,AL_SOURCE_STATE,&state );
 						if( state==AL_STOPPED ) break;
-						std::this_thread::sleep_for( std::chrono::milliseconds( buffer_ms ) );
+						std::this_thread::sleep_for( std::chrono::milliseconds( buffer_ms/2 ) );
 					}
 					break;
 				}
 				
 				for(;;){
+				
 					alGetSourcei( source,AL_SOURCE_STATE,&state );
 					if( state==AL_STOPPED ) break;
 
 					ALint processed;
 					alGetSourcei( source,AL_BUFFERS_PROCESSED,&processed );
 					
+//					if( processed>1 )  bb_printf( "processed=%i\n",processed );
+					
 					if( state==AL_PLAYING && processed ) break;
 						
-					std::this_thread::sleep_for( std::chrono::milliseconds( buffer_ms ) );
+					std::this_thread::sleep_for( std::chrono::milliseconds( buffer_ms/2 ) );
 				}
 				if( state==AL_STOPPED ){
 //					bb_printf( "AL_STOPPED\n" );