From cf84d88dd68bc4c8fc52803832083c43a796b997 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Damian=20Kara=C5=9B?= <s176030@student.pg.edu.pl>
Date: Thu, 4 Nov 2021 19:03:49 +0100
Subject: [PATCH] :ALSA_support: buffers convertion for input
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Damian Karaś <s176030@student.pg.edu.pl>
---
 src/cpp/ALSA_support.cpp   | 183 ++++++++++++++++++++++++++-----------
 src/include/ALSA_support.h |   6 +-
 2 files changed, 132 insertions(+), 57 deletions(-)

diff --git a/src/cpp/ALSA_support.cpp b/src/cpp/ALSA_support.cpp
index 786a48c..236b1cc 100644
--- a/src/cpp/ALSA_support.cpp
+++ b/src/cpp/ALSA_support.cpp
@@ -350,75 +350,149 @@ int DSP::ALSA_object_t::open_alsa_device(snd_pcm_stream_t stream_type)
   
   size_b = audio_inbuffer_size_in_frames * no_of_channels_alsa * no_of_bytes_in_channel;
 
-  pcm_buffer.resize(DSP::NoOfAudioOutputBuffers);
-  pcm_buffer_size_in_frames.resize(DSP::NoOfAudioOutputBuffers);
-
-  switch (no_of_bytes_in_channel)
+  if (stream_type == SND_PCM_STREAM_PLAYBACK)
   {
-    case 1:
-      buffers_8bit.resize(DSP::NoOfAudioOutputBuffers);
-
-      for(unsigned int ind = 0; ind < DSP::NoOfAudioOutputBuffers; ind++)
-      {
-        buffers_8bit[ind].resize(size_b / no_of_bytes_in_channel);
-        pcm_buffer[ind] = (uint8_t *)(buffers_8bit[ind].data());
-        pcm_buffer_size_in_frames[ind] = (snd_pcm_sframes_t) buffers_8bit[ind].size() / no_of_channels_alsa;
-      }
-      break;
-
-     case 2:
-      buffers_16bit.resize(DSP::NoOfAudioOutputBuffers);
+    pcm_buffer.resize(DSP::NoOfAudioOutputBuffers);
+    pcm_buffer_size_in_frames.resize(DSP::NoOfAudioOutputBuffers);
+    switch (no_of_bytes_in_channel)
+    {
+      case 1:
+        buffers_8bit.resize(DSP::NoOfAudioOutputBuffers);
 
-      for(unsigned int ind = 0; ind < DSP::NoOfAudioOutputBuffers; ind++)
-      {
-        buffers_16bit[ind].resize(size_b / no_of_bytes_in_channel);
-        pcm_buffer[ind] = (uint8_t *)(buffers_16bit[ind].data());
-        pcm_buffer_size_in_frames[ind] = (snd_pcm_sframes_t) buffers_16bit[ind].size() / no_of_channels_alsa; // buffer_size_in_frames should also be a vector since this value is not always used in this call and might be needed in the future
-      }
-      break;
+        for(unsigned int ind = 0; ind < DSP::NoOfAudioOutputBuffers; ind++)
+        {
+          buffers_8bit[ind].resize(size_b / no_of_bytes_in_channel);
+          pcm_buffer[ind] = (uint8_t *)(buffers_8bit[ind].data());
+          pcm_buffer_size_in_frames[ind] = (snd_pcm_sframes_t) buffers_8bit[ind].size() / no_of_channels_alsa;
+        }
+        break;
 
-    case 3:
-    case 4:
-      if (IsHigherQualityMode)
-      {
-        buffers_32bit.resize(DSP::NoOfAudioOutputBuffers);
+      case 2:
+        buffers_16bit.resize(DSP::NoOfAudioOutputBuffers);
 
         for(unsigned int ind = 0; ind < DSP::NoOfAudioOutputBuffers; ind++)
         {
-          buffers_32bit[ind].resize(size_b / no_of_bytes_in_channel);
-          pcm_buffer[ind] = (uint8_t *)(buffers_32bit[ind].data());
-          pcm_buffer_size_in_frames[ind] = (snd_pcm_sframes_t) buffers_32bit[ind].size() / no_of_channels_alsa;
+          buffers_16bit[ind].resize(size_b / no_of_bytes_in_channel);
+          pcm_buffer[ind] = (uint8_t *)(buffers_16bit[ind].data());
+          pcm_buffer_size_in_frames[ind] = (snd_pcm_sframes_t) buffers_16bit[ind].size() / no_of_channels_alsa; // buffer_size_in_frames should also be a vector since this value is not always used in this call and might be needed in the future
+        }
+        break;
+
+      case 3:
+      case 4:
+        if (IsHigherQualityMode)
+        {
+          buffers_32bit.resize(DSP::NoOfAudioOutputBuffers);
+
+          for(unsigned int ind = 0; ind < DSP::NoOfAudioOutputBuffers; ind++)
+          {
+            buffers_32bit[ind].resize(size_b / no_of_bytes_in_channel);
+            pcm_buffer[ind] = (uint8_t *)(buffers_32bit[ind].data());
+            pcm_buffer_size_in_frames[ind] = (snd_pcm_sframes_t) buffers_32bit[ind].size() / no_of_channels_alsa;
+          }
         }
-      }
  
-      else //! native mode
-      {
-        buffers_32bit_f.resize(DSP::NoOfAudioOutputBuffers);
+        else //! native mode
+        {
+          buffers_32bit_f.resize(DSP::NoOfAudioOutputBuffers);
+
+          for(unsigned int ind = 0; ind < DSP::NoOfAudioOutputBuffers; ind++)
+          {
+            buffers_32bit_f[ind].resize(size_b / no_of_bytes_in_channel);
+            pcm_buffer[ind] = (uint8_t *)(buffers_32bit_f[ind].data());
+            pcm_buffer_size_in_frames[ind] = (snd_pcm_sframes_t) buffers_32bit_f[ind].size() / no_of_channels_alsa;
+          }
+        }
+        break;
+
+      case 8:
+        buffers_64bit.resize(DSP::NoOfAudioOutputBuffers);
 
         for(unsigned int ind = 0; ind < DSP::NoOfAudioOutputBuffers; ind++)
         {
-          buffers_32bit_f[ind].resize(size_b / no_of_bytes_in_channel);
-          pcm_buffer[ind] = (uint8_t *)(buffers_32bit_f[ind].data());
-          pcm_buffer_size_in_frames[ind] = (snd_pcm_sframes_t) buffers_32bit_f[ind].size() / no_of_channels_alsa;
+          buffers_64bit[ind].resize(size_b / no_of_bytes_in_channel);
+          pcm_buffer[ind] = (uint8_t *)(buffers_64bit[ind].data());
+          pcm_buffer_size_in_frames[ind] = (snd_pcm_sframes_t) buffers_64bit[ind].size() / no_of_channels_alsa;
         }
-      }
-      break;
+        break;
 
-    case 8:
-      buffers_64bit.resize(DSP::NoOfAudioOutputBuffers);
+      default:
+        DSP::log << "Unsupported no of bytes in channel" << endl;
+        return -1;
+        break;
+    }
+  }
+  else // (stream_type == SND_PCM_STREAM_CAPTURE)
+  {
+    pcm_buffer.resize(DSP::NoOfAudioInputBuffers);
+    pcm_buffer_size_in_frames.resize(DSP::NoOfAudioInputBuffers);
+        switch (no_of_bytes_in_channel)
+    {
+      case 1:
+        buffers_8bit.resize(DSP::NoOfAudioInputBuffers);
 
-      for(unsigned int ind = 0; ind < DSP::NoOfAudioOutputBuffers; ind++)
-      {
-        buffers_64bit[ind].resize(size_b / no_of_bytes_in_channel);
-        pcm_buffer[ind] = (uint8_t *)(buffers_64bit[ind].data());
-        pcm_buffer_size_in_frames[ind] = (snd_pcm_sframes_t) buffers_64bit[ind].size() / no_of_channels_alsa;
-      }
-      break;
+        for(unsigned int ind = 0; ind < DSP::NoOfAudioInputBuffers; ind++)
+        {
+          buffers_8bit[ind].resize(size_b / no_of_bytes_in_channel);
+          pcm_buffer[ind] = (uint8_t *)(buffers_8bit[ind].data());
+          pcm_buffer_size_in_frames[ind] = (snd_pcm_sframes_t) buffers_8bit[ind].size() / no_of_channels_alsa;
+        }
+        break;
 
-    default:
-      DSP::log << "Unsupported no of bytes in channel" << endl;
-      return -1;
-      break;
+      case 2:
+        buffers_16bit.resize(DSP::NoOfAudioInputBuffers);
+
+        for(unsigned int ind = 0; ind < DSP::NoOfAudioInputBuffers; ind++)
+        {
+          buffers_16bit[ind].resize(size_b / no_of_bytes_in_channel);
+          pcm_buffer[ind] = (uint8_t *)(buffers_16bit[ind].data());
+          pcm_buffer_size_in_frames[ind] = (snd_pcm_sframes_t) buffers_16bit[ind].size() / no_of_channels_alsa; // buffer_size_in_frames should also be a vector since this value is not always used in this call and might be needed in the future
+        }
+        break;
+
+      case 3:
+      case 4:
+        if (IsHigherQualityMode)
+        {
+          buffers_32bit.resize(DSP::NoOfAudioInputBuffers);
+
+          for(unsigned int ind = 0; ind < DSP::NoOfAudioInputBuffers; ind++)
+          {
+            buffers_32bit[ind].resize(size_b / no_of_bytes_in_channel);
+            pcm_buffer[ind] = (uint8_t *)(buffers_32bit[ind].data());
+            pcm_buffer_size_in_frames[ind] = (snd_pcm_sframes_t) buffers_32bit[ind].size() / no_of_channels_alsa;
+          }
+        }
+ 
+        else //! native mode
+        {
+          buffers_32bit_f.resize(DSP::NoOfAudioInputBuffers);
+
+          for(unsigned int ind = 0; ind < DSP::NoOfAudioInputBuffers; ind++)
+          {
+            buffers_32bit_f[ind].resize(size_b / no_of_bytes_in_channel);
+            pcm_buffer[ind] = (uint8_t *)(buffers_32bit_f[ind].data());
+            pcm_buffer_size_in_frames[ind] = (snd_pcm_sframes_t) buffers_32bit_f[ind].size() / no_of_channels_alsa;
+          }
+        }
+        break;
+
+      case 8:
+        buffers_64bit.resize(DSP::NoOfAudioInputBuffers);
+
+        for(unsigned int ind = 0; ind < DSP::NoOfAudioInputBuffers; ind++)
+        {
+          buffers_64bit[ind].resize(size_b / no_of_bytes_in_channel);
+          pcm_buffer[ind] = (uint8_t *)(buffers_64bit[ind].data());
+          pcm_buffer_size_in_frames[ind] = (snd_pcm_sframes_t) buffers_64bit[ind].size() / no_of_channels_alsa;
+        }
+        break;
+
+      default:
+        DSP::log << "Unsupported no of bytes in channel" << endl;
+        return -1;
+        break;
+    }
   }
 
   if (blocking_mode == false)
@@ -435,6 +509,7 @@ int DSP::ALSA_object_t::open_alsa_device(snd_pcm_stream_t stream_type)
   else
   {
     rc = snd_pcm_nonblock(alsa_handle, 1);
+
     if (rc < 0)
     {
       DSP::log << "Unable to set non blocking mode" << endl;
diff --git a/src/include/ALSA_support.h b/src/include/ALSA_support.h
index 60199be..90d1e97 100644
--- a/src/include/ALSA_support.h
+++ b/src/include/ALSA_support.h
@@ -79,9 +79,9 @@ namespace DSP {
 
         /***************************************************/
         /****************AUDIO INPUT TEST*******************/
-        DSP::e::SampleType InSampleType;
-        static void CALLBACK waveInProc(HWAVEIN hwi, UINT uMsg, uint32_t dwInstance, uint32_t dwParam1, uint32_t dwParam2);
-        std::vector<std::vector<int8_t>> input_buffers;
+        //DSP::e::SampleType InSampleType;
+        //static void CALLBACK waveInProc(HWAVEIN hwi, UINT uMsg, uint32_t dwInstance, uint32_t dwParam1, uint32_t dwParam2);
+        //std::vector<std::vector<int8_t>> input_buffers;
         /**********************END**************************/
         /***************************************************/
 
-- 
GitLab