From 3157f521b063d361dde5aa1a43b19e56e58d1b7c Mon Sep 17 00:00:00 2001
From: Marek Blok <Marek.Blok@pg.edu.pl>
Date: Thu, 13 May 2021 22:21:15 +0200
Subject: [PATCH] Added sound_input.cpp example

---
 .gitignore                  |  5 ++++
 CHANGELOG                   |  1 +
 examples/makefile           |  9 +++++-
 examples/makefile-m64.linux | 10 ++++++-
 examples/rundot.bat         |  1 +
 examples/sound_input.cpp    | 60 +++++++++++++++++++++++++++++++++++++
 6 files changed, 84 insertions(+), 2 deletions(-)
 create mode 100644 examples/sound_input.cpp

diff --git a/.gitignore b/.gitignore
index ab7eb80..edeeaf1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -24,6 +24,8 @@ examples/socket_server_2.exe
 examples/socket_server_2.o
 examples/socket_server.exe
 examples/socket_server.o
+examples/sound_input.exe
+examples/sound_input.o
 examples/matlab/LPF_8000_11025.coef
 examples/matlab/LPF_8000_48000.coef
 examples/asynchronous_CT.dot
@@ -66,3 +68,6 @@ examples/DSP_socket_client
 examples/DSP_socket_client_2
 examples/DSP_socket_server
 examples/DSP_socket_server_2
+examples/DSP_sound_input
+examples/captured_sample.wav
+examples/sound_input.dot
diff --git a/CHANGELOG b/CHANGELOG
index 1f233a4..766bdcc 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -12,6 +12,7 @@ CHANGES:
   - Added to DSP_types.h a SOUND_object_t abstract class, a wrapper for different sound cards' API
   - Added WMM_object_t abstract class, a wrapper for Windows MultiMedia API
   - DSP::u::AudioOutput now uses SOUND_object_t (currently just WMM_object_t) for soundcard API
+  - Added examples/sound_input.cpp with DSP::u::AudioInput example
 
 - ver. 0.20.004 - <b>2021.04.06</b> Changed: 
   - Fixed makefiles for Linux - install and examples compilation works
diff --git a/examples/makefile b/examples/makefile
index 720617f..e746da4 100644
--- a/examples/makefile
+++ b/examples/makefile
@@ -27,7 +27,7 @@ endif
 
 SRCS = hello.cpp
 
-all: hello.exe echo.exe multirate.exe callbacks.exe socket_server.exe socket_client.exe socket_server_2.exe socket_client_2.exe macro_example.exe asynchronous.exe asynchronous_CT.exe
+all: hello.exe echo.exe sound_input.exe multirate.exe callbacks.exe socket_server.exe socket_client.exe socket_server_2.exe socket_client_2.exe macro_example.exe asynchronous.exe asynchronous_CT.exe
 #all: echo.exe  
 
 hello.o: $(SRC_DIR)/hello.cpp
@@ -40,6 +40,11 @@ echo.o: $(SRC_DIR)/echo.cpp
 echo.exe: echo.o
 	$(CC) -L"$(DSPELIB_DIR_ROOT)/$(TYPE)" "$(SRC_DIR)/echo.o" -o"$(SRC_DIR)/echo.exe" $(LINKER_FLAGS) -lDSPE -lwinmm
 
+sound_input.o: $(SRC_DIR)/sound_input.cpp
+	$(CC) -DWIN32 -I"$(DSPELIB_DIR_ROOT)/include" -I"$(DSPELIB_DIR_ROOT)/include/$(TYPE)" $(CFLAGS) -c $(SRC_DIR)/sound_input.cpp -o "$(SRC_DIR)/sound_input.o"
+sound_input.exe: sound_input.o
+	$(CC) -L"$(DSPELIB_DIR_ROOT)/$(TYPE)" "$(SRC_DIR)/sound_input.o" -o"$(SRC_DIR)/sound_input.exe" $(LINKER_FLAGS) -lDSPE -lwinmm
+
 multirate.o: $(SRC_DIR)/multirate.cpp
 	$(CC) -DWIN32 -I"$(DSPELIB_DIR_ROOT)/include" -I"$(DSPELIB_DIR_ROOT)/include/$(TYPE)" $(CFLAGS) -c "$(SRC_DIR)/multirate.cpp" -o "$(SRC_DIR)/multirate.o"
 multirate.exe: multirate.o
@@ -89,6 +94,7 @@ asynchronous_CT.exe: asynchronous_CT.o
 clean:
 	rm -f $(SRC_DIR)/hello.exe $(SRC_DIR)/hello.o 
 	rm -f $(SRC_DIR)/echo.exe $(SRC_DIR)/echo.o 
+	rm -f $(SRC_DIR)/sound_input.exe $(SRC_DIR)/sound_input.o 
 	rm -f $(SRC_DIR)/asynchronous.exe $(SRC_DIR)/asynchronous.o
 	rm -f $(SRC_DIR)/asynchronous_CT.exe $(SRC_DIR)/asynchronous_CT.o
 	rm -f $(SRC_DIR)/multirate.exe $(SRC_DIR)/multirate.o
@@ -104,5 +110,6 @@ clean:
 	rm -f $(SRC_DIR)/morse.wav
 	rm -f $(SRC_DIR)/morse_key.wav
 	rm -f $(SRC_DIR)/test_out.wav
+	rm -f $(SRC_DIR)/captured_sample.wav
 	rm -f $(SRC_DIR)/*.dot
 	rm -f $(SRC_DIR)/*.gif
diff --git a/examples/makefile-m64.linux b/examples/makefile-m64.linux
index 26854ec..f2fecda 100644
--- a/examples/makefile-m64.linux
+++ b/examples/makefile-m64.linux
@@ -32,7 +32,7 @@ LIBS_SOCKETS   = -lDSPEsockets # -lws2_32
 SRCS = hello.cpp
 
 #all: hello.exe echo.exe multirate.exe callbacks.exe socket_server.exe socket_client.exe #socket_server_2.exe socket_client_2.exe macro_example.exe asynchronous.exe asynchronous_CT.exe
-all: DSP_hello DSP_echo DSP_multirate DSP_callbacks DSP_socket_server DSP_socket_client DSP_socket_server_2 DSP_socket_client_2 DSP_macro_example DSP_asynchronous DSP_asynchronous_CT  
+all: DSP_hello DSP_echo DSP_sount_input DSP_multirate DSP_callbacks DSP_socket_server DSP_socket_client DSP_socket_server_2 DSP_socket_client_2 DSP_macro_example DSP_asynchronous DSP_asynchronous_CT  
 
 hello.o: $(SRC_DIR)/hello.cpp
 	$(CC) $(DFLAGS) -I"$(DSPELIB_DIR_ROOT)/include" -I"$(DSPELIB_DIR_ROOT)/include/$(TYPE)" $(CFLAGS) -c "$(SRC_DIR)/hello.cpp" -o "$(SRC_DIR)/hello.o"
@@ -46,6 +46,12 @@ echo.o: $(SRC_DIR)/echo.cpp
 DSP_echo: echo.o
 	$(CC) -L"$(DSPELIB_DIR_ROOT)/$(TYPE)" "$(SRC_DIR)/echo.o" -o "$(SRC_DIR)/DSP_echo" $(LINKER_FLAGS) $(LIBS)
 
+sount_input.o: $(SRC_DIR)/sount_input.cpp
+	$(CC) $(DFLAGS) -I"$(DSPELIB_DIR_ROOT)/include" -I"$(DSPELIB_DIR_ROOT)/include/$(TYPE)" $(CFLAGS) -c "$(SRC_DIR)/sount_input.cpp" -o "$(SRC_DIR)/sount_input.o"
+
+DSP_sount_input: sount_input.o
+	$(CC) -L"$(DSPELIB_DIR_ROOT)/$(TYPE)" "$(SRC_DIR)/sount_input.o" -o "$(SRC_DIR)/DSP_sount_input" $(LINKER_FLAGS) $(LIBS)
+
 multirate.o: $(SRC_DIR)/multirate.cpp
 	$(CC) $(DFLAGS) -I"$(DSPELIB_DIR_ROOT)/include" -I"$(DSPELIB_DIR_ROOT)/include/$(TYPE)" $(CFLAGS) -c "$(SRC_DIR)/multirate.cpp" -o "$(SRC_DIR)/multirate.o"
 
@@ -104,6 +110,7 @@ DSP_asynchronous_CT: asynchronous_CT.o
 clean:
 	rm -f $(SRC_DIR)/DSP_hello $(SRC_DIR)/hello.o 
 	rm -f $(SRC_DIR)/DSP_echo $(SRC_DIR)/echo.o 
+	rm -f $(SRC_DIR)/DSP_sound_input $(SRC_DIR)/sound_input.o 
 	rm -f $(SRC_DIR)/DSP_asynchronous $(SRC_DIR)/asynchronous.o
 	rm -f $(SRC_DIR)/DSP_asynchronous_CT $(SRC_DIR)/asynchronous_CT.o
 	rm -f $(SRC_DIR)/DSP_multirate $(SRC_DIR)/multirate.o
@@ -119,5 +126,6 @@ clean:
 	rm -f $(SRC_DIR)/morse.wav
 	rm -f $(SRC_DIR)/morse_key.wav
 	rm -f $(SRC_DIR)/test_out.wav
+	rm -f $(SRC_DIR)/captured_sample.wav
 	rm -f $(SRC_DIR)/*.dot
 	rm -f $(SRC_DIR)/*.gif
diff --git a/examples/rundot.bat b/examples/rundot.bat
index 0e6889d..f748853 100644
--- a/examples/rundot.bat
+++ b/examples/rundot.bat
@@ -9,4 +9,5 @@ dot -Tgif socket_client_2.dot -osocket_client_2.gif
 dot -Tgif callbacks_scheme.dot -ocallbacks_scheme.gif
 dot -Tgif hello.dot -ohello.gif
 dot -Tgif echo.dot -oecho.gif
+dot -Tgif captured_sample.dot -ocaptured_sample.gif
 dot -Tgif multirate.dot -omultirate.gif
diff --git a/examples/sound_input.cpp b/examples/sound_input.cpp
new file mode 100644
index 0000000..f1f1bc5
--- /dev/null
+++ b/examples/sound_input.cpp
@@ -0,0 +1,60 @@
+/*! Digital Signal Processing Engine Sound capture usage example.
+ * \author Marek Blok
+ * \date 2021.05.13
+ * \date updated 2021.01.18
+ */
+#include <DSP_lib.h>
+
+#ifndef _DSPE_TEST_
+int main(void)
+#else
+#include "DSPE_examples.h"
+int test_sound_input(void)
+#endif // _DSPE_TEST_
+{
+  DSP::Clock_ptr MasterClock, AudioInClock;
+  string tekst;
+  int temp;
+  long int Fp, Fp2;
+
+  DSP::log.SetLogState(DSP::e::LogState::console | DSP::e::LogState::file);
+  DSP::log.SetLogFileName("log_file.log");
+
+  DSP::log << DSP::lib_version_string() << endl << endl;
+  DSP::log << "Sound input example" << endl;
+ 
+  MasterClock=DSP::Clock::CreateMasterClock();
+
+  DSP::u::WaveInput WaveIn(MasterClock, "DSPElib.wav", ".");
+  Fp = WaveIn.GetSamplingRate();
+
+  DSP::u::AudioOutput AudioOut(Fp);
+
+  WaveIn.Output("out") >> AudioOut.Input("in");
+
+  Fp2 = 8000;
+  long Fp_gcd = DSP::f::gcd(Fp, Fp2);
+  AudioInClock=DSP::Clock::GetClock(MasterClock, Fp2 / Fp_gcd, Fp / Fp_gcd);
+  DSP::u::AudioInput AudioIn(AudioInClock, 8000, 1);
+  DSP::u::FileOutput WaveOut("captured_sample.wav",DSP::e::SampleType::ST_short, 1, DSP::e::FileType::FT_wav, Fp2);
+
+  AudioIn.Output("out") >> WaveOut.Input("in");
+
+  DSP::Component::CheckInputsOfAllComponents();
+  DSP::Clock::SchemeToDOTfile(MasterClock, "sound_input.dot");
+
+  temp=1;
+  do
+  {
+    DSP::Clock::Execute(MasterClock, Fp/8);
+
+    DSP::log << "MAIN" << DSP::e::LogMode::second << temp << endl;
+    temp++;
+  }
+  while (WaveIn.GetBytesRead() != 0);
+
+  DSP::Clock::FreeClocks();
+  DSP::log << DSP::e::LogMode::Error << "MAIN" << DSP::e::LogMode::second << "end" << endl;
+
+  return 0;
+}
-- 
GitLab