From 1a86ac921e28ae259d3ec2125812fdf5e3b75f1f Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sat, 11 Apr 2015 14:55:33 +0200 Subject: stop garbage collector temporarily during play() This improves the real-time properties of sample generation since it avoids CPU spkikes. On the other hand, this may not be ideal as playing a long streams could have non-constant space requirements now. A proper solution would probably involve calling the garbage collector incrementally during the play() loop. --- applause.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'applause.c') diff --git a/applause.c b/applause.c index b0a07dc..b57076d 100644 --- a/applause.c +++ b/applause.c @@ -169,6 +169,14 @@ l_Stream_play(lua_State *L) /* the tick generator function should now be on top of the stack */ luaL_checktype(L, -1, LUA_TFUNCTION); + /* + * Perform garbage collection cycle and turn it off + * temporarily. This improves the realtime properties + * of the sample generation loop below. + */ + lua_gc(L, LUA_GCCOLLECT, 0); + lua_gc(L, LUA_GCSTOP, 0); + for (;;) { jack_default_audio_sample_t sample; @@ -183,7 +191,12 @@ l_Stream_play(lua_State *L) break; /* copy sample into ring buffer */ - sample = (jack_default_audio_sample_t)luaL_checknumber(L, -1); + /* + * FIXME: What if sample isn't a number. This + * should be handled. But don't forget to restart + * the garbage collector + */ + sample = (jack_default_audio_sample_t)lua_tonumber(L, -1); /* * FIXME: Buffer may be full -- perhaps we should wait on a * semaphore @@ -194,6 +207,8 @@ l_Stream_play(lua_State *L) lua_pop(L, 1); } + lua_gc(L, LUA_GCRESTART, 0); + /* any remaining stack elements are automatically popped */ return 0; } -- cgit v1.2.3