From d4c0501d1fcc8c98d39b2b7ba627cccb29cbc476 Mon Sep 17 00:00:00 2001
From: dhewg
Date: Sat, 26 Feb 2011 22:23:17 +0100
Subject: ANDROID: Check audio buffer for silence

Most games register a music channel, and when there is no music,
they still stream silence (and run through all the Converter::flow
code!). Scan the buffer for that to pause the AudioTrack. Ugly, but
worth it - reduces CPU usage on many games and hence saves battery life.
---
 backends/platform/android/android.cpp | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

(limited to 'backends')

diff --git a/backends/platform/android/android.cpp b/backends/platform/android/android.cpp
index 860ff1f7ee..f20cf848d4 100644
--- a/backends/platform/android/android.cpp
+++ b/backends/platform/android/android.cpp
@@ -176,7 +176,7 @@ void *OSystem_Android::audioThreadFunc(void *arg) {
 
 	byte *buf;
 	int offset, left, written;
-	int samples;
+	int samples, i;
 
 	struct timespec tv_delay;
 	tv_delay.tv_sec = 0;
@@ -188,6 +188,7 @@ void *OSystem_Android::audioThreadFunc(void *arg) {
 	tv_full.tv_sec = 0;
 	tv_full.tv_nsec = msecs_full * 1000 * 1000;
 
+	bool silence;
 	uint silence_count = 0;
 
 	while (!system->_audio_thread_exit) {
@@ -196,9 +197,24 @@ void *OSystem_Android::audioThreadFunc(void *arg) {
 
 		samples = mixer->mixCallback(buf, buf_size);
 
+		silence = samples < 1;
+
+		// looks stupid, and it is, but currently there's no way to detect
+		// silence-only buffers from the mixer
+		if (!silence) {
+			silence = true;
+
+			for (i = 0; i < samples; i += 2)
+				// SID streams constant crap
+				if (READ_UINT16(buf + i) > 32) {
+					silence = false;
+					break;
+				}
+		}
+
 		env->ReleasePrimitiveArrayCritical(bufa, buf, 0);
 
-		if (samples < 1) {
+		if (silence) {
 			if (!paused)
 				silence_count++;
 
-- 
cgit v1.2.3