diff --git a/chirp.cpp b/chirp.cpp index a9792cf..1f53040 100644 --- a/chirp.cpp +++ b/chirp.cpp @@ -13,7 +13,7 @@ static uint8_t octave = 4; static uint32_t tempo = 44100; static char* song_ptr = NULL; static char* song = NULL; - +static bool audio_lock = false; #define AUDIO_NONE 0 @@ -46,7 +46,7 @@ void audioCallback(void * userdata, uint8_t * stream, int len) { while( len > 0 ) { while( play_len == 0 ) { play_len = interpret_next_token(play_buffer); - if (play_len == -1) { audio_state=AUDIO_NONE; SDL_memset( stream, 0, len ); return; } + if (play_len == -1) { audio_state=AUDIO_NONE; audio_lock=false; SDL_memset( stream, 0, len ); return; } play_pos = play_buffer; } const int actual_len = ( len > play_len ? play_len : len ); @@ -73,17 +73,20 @@ void chirp_init() { uint32_t interpret_note(uint8_t* buffer, const char note, const char param ) { const uint32_t length = ( param == -1 ? default_length : ((float)lengths[param])/10000.0f ) * tempo; if( note == 100 ) { memset( buffer, 0, length ); return length; } + if( note == 101 ) { for( int i = 0; i < length; i++ ) buffer[i] = rand()%2==0 ? volume : -volume; return length; } const uint16_t period = periods[note + octave*12]; for( int i = 0; i < length; i++ ) buffer[i] = ( (i % period) < (period >> 1) ? volume : -volume ); return length; } -void chirp_play(const char* new_song) { +void chirp_play(const char* new_song, const bool lock) { + if (audio_lock and !lock) return; + audio_lock = lock; play_pos = play_buffer; play_len = 0; - volume=64; + volume=32; octave=4; tempo=44100; if (song != NULL) free(song); @@ -115,6 +118,10 @@ int32_t interpret_next_token(uint8_t* buffer) { param = *++*token; if( param >= 48 && param <= 57 ) { param -= 48; ++*token; } else { param = -1; } return interpret_note( buffer, 100, param ); + case 'n': + param = *++*token; + if( param >= 48 && param <= 57 ) { param -= 48; ++*token; } else { param = -1; } + return interpret_note( buffer, 101, param ); case 'o': param = *++*token; if( param >= 48 && param <= 57 ) { octave = (param - 48) % 8; ++*token; } @@ -131,7 +138,7 @@ int32_t interpret_next_token(uint8_t* buffer) { return 0; case 'v': param = *++*token; - if( param >= 48 && param <= 57 ) { volume = (param - 48) << 4; ++*token; } + if( param >= 48 && param <= 57 ) { volume = (param - 48) << 3; ++*token; } return 0; case 't': param = *++*token; diff --git a/chirp.h b/chirp.h index 30e7120..7fedcf9 100644 --- a/chirp.h +++ b/chirp.h @@ -3,6 +3,6 @@ void chirp_init(); -void chirp_play(const char* new_song); +void chirp_play(const char* new_song, const bool lock); int32_t interpret_next_token(uint8_t* buffer); diff --git a/lua.cpp b/lua.cpp index b749de5..976a125 100644 --- a/lua.cpp +++ b/lua.cpp @@ -651,7 +651,9 @@ extern "C" { static int cpp_playchirp(lua_State *L) { const char* str = luaL_checkstring(L, 1); - playchirp(str); + bool value = false; + if (lua_gettop(L) > 1) value = lua_toboolean(L, 2); + playchirp(str, value); return 0; } diff --git a/mini.cpp b/mini.cpp index a7bc87b..4a1bff9 100644 --- a/mini.cpp +++ b/mini.cpp @@ -983,8 +983,8 @@ bool freadb() { return strcmp(fstr, "true")==0?true:false; } -void playchirp(const char *song) { - chirp_play(song); +void playchirp(const char *song, const bool lock) { + chirp_play(song, lock); } void exit() { diff --git a/mini.h b/mini.h index 42a72ad..dfc0f00 100644 --- a/mini.h +++ b/mini.h @@ -257,6 +257,6 @@ const char *freads(); const char *freadw(); bool freadb(); -void playchirp(const char *song); +void playchirp(const char *song, const bool lock); void exit();