From 83983cd1928c25073e0ebc099126080cef9a0ae8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Damian=20Kara=C5=9B?= <s176030@student.pg.edu.pl>
Date: Sun, 14 Nov 2021 21:04:43 +0100
Subject: [PATCH] ALSA_support: add the recording algorithm structure
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   | 58 ++++++++++++++++++++++++++++++++++++++
 src/include/ALSA_support.h |  3 ++
 2 files changed, 61 insertions(+)

diff --git a/src/cpp/ALSA_support.cpp b/src/cpp/ALSA_support.cpp
index d7acc0c..8d4816b 100644
--- a/src/cpp/ALSA_support.cpp
+++ b/src/cpp/ALSA_support.cpp
@@ -894,6 +894,55 @@ bool DSP::ALSA_object_t::get_wave_in_raw_buffer(DSP::e::SampleType &InSampleType
   InSampleTypeALSA = InSampleType;
 
   snd_pcm_sframes_t rc;
+    // one spare buffer
+    for (unsigned int ind = 0; ind < DSP::NoOfAudioInputBuffers - 1; ind++)
+    {
+      rc = snd_pcm_readi(alsa_handle, pcm_buffer[ind], pcm_buffer_size_in_frames[ind]);
+
+      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
+              DSP::log << "Short read. Current rc = " << rc << "." << endl;
+            #endif // AUDIO_DEBUG_MESSAGES_ON
+          }
+
+          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;
+          }
+          break;
+      }
+    }
 
   return true;
 }
@@ -960,6 +1009,15 @@ snd_pcm_sframes_t DSP::ALSA_object_t::pcm_writei(const void *buffer, const snd_p
   return rc;
 }
 
+/*
+snd_pcm_sframes_t DSP::ALSA_object_t::pcm_readi(const void *buffer, const snd_pcm_uframes_t &frames)
+{
+  snd_pcm_sframes_t rc;
+
+  return rc;
+}
+*/
+
 void DSP::ALSA_object_t::close_alsa_device(bool do_drain, bool use_log)
 {
   if (alsa_handle != NULL) 
diff --git a/src/include/ALSA_support.h b/src/include/ALSA_support.h
index 0bd6078..99e7e65 100644
--- a/src/include/ALSA_support.h
+++ b/src/include/ALSA_support.h
@@ -97,6 +97,9 @@ namespace DSP {
         
         //! playback
         snd_pcm_sframes_t pcm_writei(const void *buffer, const snd_pcm_uframes_t &frames); // M.B. this will be more transparent
+
+        //! recording
+        //snd_pcm_sframes_t DSP::ALSA_object_t::pcm_readi(const void *buffer, const snd_pcm_uframes_t &frames)
         
         //! Set SND PCM format depending on no of bytes in channel and CPU endianness
         int set_snd_pcm_format(snd_pcm_hw_params_t *params);
-- 
GitLab