OSDN Git Service

Add non-zero sample and frame counting functions
authorGlenn Kasten <gkasten@google.com>
Fri, 21 Feb 2014 18:00:51 +0000 (10:00 -0800)
committerGlenn Kasten <gkasten@google.com>
Fri, 21 Feb 2014 18:00:51 +0000 (10:00 -0800)
Change-Id: Ib140f83d04bb98f01f15fa87f02fbd85ca597bd3

audio_utils/include/audio_utils/primitives.h
audio_utils/primitives.c

index 358563f..d5b7285 100644 (file)
@@ -101,6 +101,22 @@ void downmix_to_mono_i16_from_stereo_i16(int16_t *dst, const int16_t *src, size_
  */
 void upmix_to_stereo_i16_from_mono_i16(int16_t *dst, const int16_t *src, size_t count);
 
+/* Return the total number of non-zero 32-bit samples */
+size_t nonZeroMono32(const int32_t *samples, size_t count);
+
+/* Return the total number of non-zero 16-bit samples */
+size_t nonZeroMono16(const int16_t *samples, size_t count);
+
+/* Return the total number of non-zero stereo frames, where a frame is considered non-zero
+ * if either of its constituent 32-bit samples is non-zero
+ */
+size_t nonZeroStereo32(const int32_t *frames, size_t count);
+
+/* Return the total number of non-zero stereo frames, where a frame is considered non-zero
+ * if either of its constituent 16-bit samples is non-zero
+ */
+size_t nonZeroStereo16(const int16_t *frames, size_t count);
+
 /**
  * Clamp (aka hard limit or clip) a signed 32-bit sample to 16-bit range.
  */
index 90ad75a..e488c2d 100644 (file)
@@ -87,3 +87,49 @@ void upmix_to_stereo_i16_from_mono_i16(int16_t *dst, const int16_t *src, size_t
         dst += 2;
     }
 }
+
+size_t nonZeroMono32(const int32_t *samples, size_t count)
+{
+    size_t nonZero = 0;
+    while (count-- > 0) {
+        if (*samples++ != 0) {
+            nonZero++;
+        }
+    }
+    return nonZero;
+}
+
+size_t nonZeroMono16(const int16_t *samples, size_t count)
+{
+    size_t nonZero = 0;
+    while (count-- > 0) {
+        if (*samples++ != 0) {
+            nonZero++;
+        }
+    }
+    return nonZero;
+}
+
+size_t nonZeroStereo32(const int32_t *frames, size_t count)
+{
+    size_t nonZero = 0;
+    while (count-- > 0) {
+        if (frames[0] != 0 || frames[1] != 0) {
+            nonZero++;
+        }
+        frames += 2;
+    }
+    return nonZero;
+}
+
+size_t nonZeroStereo16(const int16_t *frames, size_t count)
+{
+    size_t nonZero = 0;
+    while (count-- > 0) {
+        if (frames[0] != 0 || frames[1] != 0) {
+            nonZero++;
+        }
+        frames += 2;
+    }
+    return nonZero;
+}