From d12856af4c35c92b62782609f1f1d0dc38c88be0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Damian=20Kara=C5=9B?= <s176030@student.pg.edu.pl>
Date: Thu, 18 Nov 2021 21:04:54 +0100
Subject: [PATCH] ALSA_support: improved capture buffer
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   | 102 ++++++++++++++++++-------------------
 src/include/ALSA_support.h |   2 +-
 2 files changed, 52 insertions(+), 52 deletions(-)

diff --git a/src/cpp/ALSA_support.cpp b/src/cpp/ALSA_support.cpp
index a5bd7fe..ffe5ac9 100644
--- a/src/cpp/ALSA_support.cpp
+++ b/src/cpp/ALSA_support.cpp
@@ -904,69 +904,69 @@ bool DSP::ALSA_object_t::get_wave_in_raw_buffer(DSP::e::SampleType &InSampleType
 
   snd_pcm_sframes_t rc;
 
+  wave_in_raw_buffer.resize(capture_buffer.size());
+
   for (unsigned int ind = 0; ind < pcm_buffer.size(); ind++)
-   {
-      while (pcm_buffer_size_in_frames[ind])
-      {
-     
+  {
+    while (pcm_buffer_size_in_frames[ind])
+    {
       rc = snd_pcm_readi(alsa_handle, pcm_buffer[ind], pcm_buffer_size_in_frames[ind]);
 
-        switch (-rc)
-        {
-          case EPIPE:
+      switch (-rc)
+      {
+        case EPIPE:
+        
+          #ifdef AUDIO_DEBUG_MESSAGES_ON
+            // EPIPE means underrun
+            DSP::log << "Underrun occurred" << endl;
+          #endif // AUDIO_DEBUG_MESSAGES_ON
         
+           snd_pcm_prepare(alsa_handle);
+           break;
+
+        case EAGAIN:
+
+          #ifdef AUDIO_DEBUG_MESSAGES_ON
+            DSP::log << "EAGAIN occurred. Waiting for a free buffer." << endl;
+          #endif // AUDIO_DEBUG_MESSAGES_ON
+        
+          DSP::f::Sleep(0);
+          break;
+      
+        default:
+          if (rc > 0)
+          {
+            pcm_buffer_size_in_frames[ind] -= rc;
+            pcm_buffer[ind] += rc * no_of_channels_alsa * no_of_bytes_in_channel;
+
             #ifdef AUDIO_DEBUG_MESSAGES_ON
-              // EPIPE means underrun
-              DSP::log << "Underrun occurred" << endl;
+              DSP::log << "Short read. Current rc = " << rc << "." << endl;
             #endif // AUDIO_DEBUG_MESSAGES_ON
-        
-            snd_pcm_prepare(alsa_handle);
-            break;
 
-          case EAGAIN:
+            if (pcm_buffer_size_in_frames[ind] == 0)
+            {
+              std::swap(wave_in_raw_buffer, capture_buffer);
+              DSP::log << "Inbuffer is full." << endl;
 
+              return true;
+            }
+          }
+
+          else
+          {
             #ifdef AUDIO_DEBUG_MESSAGES_ON
-              DSP::log << "EAGAIN occurred. Waiting for a free buffer." << endl;
+              // EPIPE means underrun
+              DSP::log << "Unsupported error." << endl;
+              DSP::log << "Error from readi: " << snd_strerror(rc) << endl;
             #endif // AUDIO_DEBUG_MESSAGES_ON
-        
-            DSP::f::Sleep(0);
-            break;
-      
-          default:
-            if (rc > 0)
-              {
-                pcm_buffer_size_in_frames[ind] -= rc;
-                pcm_buffer[ind] += rc * no_of_channels_alsa * no_of_bytes_in_channel;
-
-                #ifdef AUDIO_DEBUG_MESSAGES_ON
-                  DSP::log << "Short read. Current rc = " << rc << "." << endl;
-                #endif // AUDIO_DEBUG_MESSAGES_ON
-
-                if (pcm_buffer_size_in_frames[ind] == 0)
-                {
-                  std::swap(wave_in_raw_buffer, capture_buffer);
-                  DSP::log << "Inbuffer is full." << endl;
-
-                  return true;
-                }
-
-              }
-
-              else
-              {
-                #ifdef AUDIO_DEBUG_MESSAGES_ON
-                  // EPIPE means underrun
-                  DSP::log << "Unsupported error." << endl;
-                  DSP::log << "Error from readi: " << snd_strerror(rc) << endl;
-                #endif // AUDIO_DEBUG_MESSAGES_ON
-
-                pcm_buffer_size_in_frames[ind] = 0;
-                return false;
-              }
-            break;
-        }
+
+            pcm_buffer_size_in_frames[ind] = 0;
+            return false;
+          }
+          break;
       }
     }
+  }
 }
 
 snd_pcm_sframes_t DSP::ALSA_object_t::pcm_writei(const void *buffer, const snd_pcm_uframes_t &frames)
diff --git a/src/include/ALSA_support.h b/src/include/ALSA_support.h
index e7be181..e06cc1e 100644
--- a/src/include/ALSA_support.h
+++ b/src/include/ALSA_support.h
@@ -56,7 +56,7 @@ namespace DSP {
         std::vector<std::vector<double>> buffers_64bit;
 
         //! inbuffer
-        std::vector<uint8_t> capture_buffer;
+        std::vector<char> capture_buffer;
         
         //! samples are integers rather than float values  
         bool IsHigherQualityMode;
-- 
GitLab