瀏覽代碼

Build sherpa-ncnn-alsa for 32-bit arm (#75)

* Build sherpa-ncnn-alsa for 32-bit arm

* Fix setting channel count
Fangjun Kuang 2 年之前
父節點
當前提交
0277e6f4ae
共有 4 個文件被更改,包括 43 次插入11 次删除
  1. 1 1
      build-aarch64-linux-gnu.sh
  2. 18 0
      build-arm-linux-gnueabihf.sh
  3. 22 10
      sherpa-ncnn/csrc/alsa.cc
  4. 2 0
      sherpa-ncnn/csrc/alsa.h

+ 1 - 1
build-aarch64-linux-gnu.sh

@@ -35,6 +35,6 @@ cmake \
   ..
 cp -v $SHERPA_NCNN_ALSA_LIB_DIR/libasound.so* ./install/lib/
 
-make VERBOSE=1 -j10
+make VERBOSE=1 -j4
 make install/strip
 

+ 18 - 0
build-arm-linux-gnueabihf.sh

@@ -13,6 +13,22 @@ set -ex
 dir=build-arm-linux-gnueabihf
 mkdir -p $dir
 cd $dir
+
+if [ ! -f alsa-lib/src/.libs/libasound.so ]; then
+  echo "Start to cross-compile alsa-lib"
+  if [ ! -d alsa-lib ]; then
+    git clone --depth 1 https://github.com/alsa-project/alsa-lib
+  fi
+  pushd alsa-lib
+  CC=arm-linux-gnueabihf-gcc ./gitcompile --host=arm-linux-gnueabihf
+  popd
+  echo "Finish cross-compiling alsa-lib"
+fi
+
+export CPLUS_INCLUDE_PATH=$PWD/alsa-lib/include:$CPLUS_INCLUDE_PATH
+export SHERPA_NCNN_ALSA_LIB_DIR=$PWD/alsa-lib/src/.libs
+
+
 cmake \
   -DCMAKE_INSTALL_PREFIX=./install \
   -DCMAKE_BUILD_TYPE=Release \
@@ -21,3 +37,5 @@ cmake \
 
 make VERBOSE=1 -j4
 make install/strip
+
+cp -v $SHERPA_NCNN_ALSA_LIB_DIR/libasound.so* ./install/lib/

+ 22 - 10
sherpa-ncnn/csrc/alsa.cc

@@ -24,11 +24,13 @@
 
 namespace sherpa_ncnn {
 
-void ToFloat(const std::vector<int16_t> &in, std::vector<float> *out) {
-  out->resize(in.size());
+void ToFloat(const std::vector<int16_t> &in, int32_t num_channels,
+             std::vector<float> *out) {
+  out->resize(in.size() / num_channels);
+
   int32_t n = in.size();
-  for (int32_t i = 0; i != n; ++i) {
-    (*out)[i] = in[i] / 32768.;
+  for (int32_t i = 0, k = 0; i < n; i += num_channels, ++k) {
+    (*out)[k] = in[i] / 32768.;
   }
 }
 
@@ -87,7 +89,17 @@ and if you want to select card 3 and the device 0 on that card, please use:
   if (err) {
     fprintf(stderr, "Failed to set number of channels to 1. %s\n",
             snd_strerror(err));
-    exit(-1);
+
+    err = snd_pcm_hw_params_set_channels(capture_handle_, hw_params, 2);
+    if (err) {
+      fprintf(stderr, "Failed to set number of channels to 2. %s\n",
+              snd_strerror(err));
+
+      exit(-1);
+    }
+    actual_channel_count_ = 2;
+    fprintf(stderr,
+            "Channel count is set to 2. Will use only 1 channel of it.\n");
   }
 
   uint32_t actual_sample_rate = expected_sample_rate_;
@@ -140,14 +152,14 @@ and if you want to select card 3 and the device 0 on that card, please use:
 Alsa::~Alsa() { snd_pcm_close(capture_handle_); }
 
 const std::vector<float> &Alsa::Read(int32_t num_samples) {
-  samples_.resize(num_samples);
+  samples_.resize(num_samples * actual_channel_count_);
 
-  int32_t count =
-      snd_pcm_readi(capture_handle_, samples_.data(), samples_.size());
+  // count is in frames. Each frame contains actual_channel_count_ samples
+  int32_t count = snd_pcm_readi(capture_handle_, samples_.data(), num_samples);
 
-  samples_.resize(count);
+  samples_.resize(count * actual_channel_count_);
 
-  ToFloat(samples_, &samples1_);
+  ToFloat(samples_, actual_channel_count_, &samples1_);
 
   if (!resampler_) {
     return samples1_;

+ 2 - 0
sherpa-ncnn/csrc/alsa.h

@@ -47,6 +47,8 @@ class Alsa {
   int32_t expected_sample_rate_ = 16000;
   int32_t actual_sample_rate_;
 
+  int32_t actual_channel_count_ = 1;
+
   std::unique_ptr<LinearResample> resampler_;
   std::vector<int16_t> samples_;  // directly from the microphone
   std::vector<float> samples1_;   // normalized version of samples_