OSDN Git Service

Hearing Aid: Combine audio from stereo to mono when only one side connect
authorweichinweng <weichinweng@google.com>
Fri, 26 Oct 2018 09:21:13 +0000 (17:21 +0800)
committerStanley Tng <stng@google.com>
Thu, 8 Nov 2018 23:47:57 +0000 (23:47 +0000)
* On the design of the Hearing Aids, the phone will separate the stereo
audio samples and stream the left audio samples to the left device and
right audio samples to the right device. When only one side connected,
phone will only stream the one side audio sample to one hearing aid
device.
* This CL adds a functionality for checking the left/right hearing aid
and combine audio samples from stereo to mono when only on side connect.

Bug: 117178490
Bug: 116317072
Test: disconnected/reconnected one side hearing when play stereo test
sound.

Change-Id: Idb3871951617680aba3ca82f801a70c95403d889
Merged-In: Idb3871951617680aba3ca82f801a70c95403d889

bta/hearing_aid/hearing_aid.cc

index 22038b3..3643184 100644 (file)
@@ -945,20 +945,6 @@ class HearingAidImpl : public HearingAid {
     if (num_samples % 2 != 0)
       LOG(FATAL) << "num_samples is not even: " << num_samples;
 
-    std::vector<uint16_t> chan_left;
-    std::vector<uint16_t> chan_right;
-    // TODO: encode data into G.722 left/right or mono.
-    for (int i = 0; i < num_samples; i++) {
-      const uint8_t* sample = data.data() + i * 4;
-
-      uint16_t left = (int16_t)((*(sample + 1) << 8) + *sample) >> 1;
-      chan_left.push_back(left);
-
-      sample += 2;
-      uint16_t right = (int16_t)((*(sample + 1) << 8) + *sample) >> 1;
-      chan_right.push_back(right);
-    }
-
     // TODO: we should cache left/right and current state, instad of recomputing
     // it for each packet, 100 times a second.
     HearingDevice* left = nullptr;
@@ -978,6 +964,34 @@ class HearingAidImpl : public HearingAid {
       return;
     }
 
+    std::vector<uint16_t> chan_left;
+    std::vector<uint16_t> chan_right;
+    if (left == nullptr || right == nullptr) {
+      for (int i = 0; i < num_samples; i++) {
+        const uint8_t* sample = data.data() + i * 4;
+
+        int16_t left = (int16_t)((*(sample + 1) << 8) + *sample) >> 1;
+
+        sample += 2;
+        int16_t right = (int16_t)((*(sample + 1) << 8) + *sample) >> 1;
+
+        uint16_t mono_data = (int16_t)(((uint32_t)left + (uint32_t)right) >> 1);
+        chan_left.push_back(mono_data);
+        chan_right.push_back(mono_data);
+      }
+    } else {
+      for (int i = 0; i < num_samples; i++) {
+        const uint8_t* sample = data.data() + i * 4;
+
+        uint16_t left = (int16_t)((*(sample + 1) << 8) + *sample) >> 1;
+        chan_left.push_back(left);
+
+        sample += 2;
+        uint16_t right = (int16_t)((*(sample + 1) << 8) + *sample) >> 1;
+        chan_right.push_back(right);
+      }
+    }
+
     // TODO: monural, binarual check
 
     // divide encoded data into packets, add header, send.