Skip to content

Commit

Permalink
Merge pull request #90 from blitz-research/develop
Browse files Browse the repository at this point in the history
develop up
  • Loading branch information
abakobo committed Nov 13, 2017
2 parents 0048ece + 7f30129 commit fe52fb6
Show file tree
Hide file tree
Showing 6 changed files with 325 additions and 169 deletions.
416 changes: 265 additions & 151 deletions bananas/gamecontroller/gamecontrollerdb.txt

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions bananas/joystick/joystick.monkey2
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ Class Player
Field id:Int
Field joystick:Joystick

Global used:=New StringMap<Bool>

Method New( id:Int )
Self.id=id
Self.joystick=Joystick.Open( id )
Expand Down
34 changes: 29 additions & 5 deletions bananas/musictest/musictest.monkey2
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,42 @@ Class MyWindow Extends Window

Method StartMusic()

_channel=Audio.PlayMusic( "asset::ACDC_-_Back_In_Black-sample.ogg",StartMusic )
_channel=Audio.PlayMusic( "asset::ACDC_-_Back_In_Black-sample.ogg",Lambda()

If _channel StartMusic()
End )

End

Method OnRender( canvas:Canvas ) Override

If Keyboard.KeyHit( Key.Space ) or Mouse.ButtonHit( MouseButton.Left ) _channel.Paused=Not _channel.Paused

RequestRender()

canvas.DrawText( "Hello World!",Width/2,Height/2,.5,.5 )
canvas.DrawText(
"Music Test: Hit [Enter] to "+
(_channel ? "stop" Else "start")+
(_channel ? ", [Space] to "+(_channel.Paused ? "resume" Else "pause") Else ""),
0,0 )

'Stop/Start?
If Keyboard.KeyHit( Key.Enter ) Or Mouse.ButtonHit( MouseButton.Left )
If _channel
_channel.Stop()
_channel=Null
Else
StartMusic()
Endif
Endif

'Pause/Resume?
If Keyboard.KeyHit( Key.Space ) Or Mouse.ButtonHit( MouseButton.Right )
If _channel
_channel.Paused=Not _channel.Paused
Endif
Endif

End

End

Function Main()
Expand Down
15 changes: 12 additions & 3 deletions modules/mojo/audio/audio.monkey2
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,30 @@ 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 )

path=filesystem.RealPath( path )

If Not playMusic( path,callback,channel._alSource )
async.DestroyAsyncCallback( callback )
channel.Discard()
Return Null
Endif

Expand Down
21 changes: 14 additions & 7 deletions modules/mojo/audio/native/bbmusic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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 );

Expand All @@ -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" );
Expand Down
6 changes: 5 additions & 1 deletion modules/mojo/input/gamecontroller.monkey2
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ Class GameController

Local n:=0

For Local mapping:=Eachin LoadString( "asset::gamecontrollerdb.txt",True ).Split( "~n" )
For Local mapping:=Eachin LoadString( path,True ).Split( "~n" )

mapping=mapping.Trim()

If Not mapping Or mapping.StartsWith( "#" ) Continue

n+=AddMapping( mapping )>0 ? 1 Else 0
Next
Expand Down

0 comments on commit fe52fb6

Please sign in to comment.