Commit f0b4d8c7fbad83c15905e93ea66785447de49dcb

Thomas de Grivel 2018-07-26T14:14:06

split rtbuf_signal

diff --git a/build/rtbuf_signal/Makefile b/build/rtbuf_signal/Makefile
new file mode 100644
index 0000000..f5a839c
--- /dev/null
+++ b/build/rtbuf_signal/Makefile
@@ -0,0 +1,30 @@
+
+PROGRAM = signal.so
+SRCDIR = ../..
+
+CPPFLAGS =
+CFLAGS = -O0 -ggdb -W -Wall -Werror -fpic
+
+LDFLAGS = -fPIC -shared
+LIBS = -lm
+HEADERS = rtbuf.h rtbuf_lib.h rtbuf_signal.h
+OBJECTS = rtbuf_signal.o rtbuf_signal_sinus.o
+
+SRC = ${HEADERS} ${OBJECTS:%.o=%.c}
+
+${PROGRAM}: ${OBJECTS}
+	${CC} ${LDFLAGS} ${OBJECTS} ${LIBS} -o ${PROGRAM}
+
+${OBJECTS}: ${HEADERS}
+
+${SRC}: %: ${SRCDIR}/%
+	if [ -f $@ ]; then chmod u+w $@; fi
+	cp $< $@
+	chmod a-w $@
+
+CLEANFILES += ${PROGRAM} ${OBJECTS} ${SRC}
+
+clean:
+	rm -f ${CLEANFILES}
+
+.PHONY: clean
diff --git a/rtbuf_signal.c b/rtbuf_signal.c
index 3a56bd7..ae51ffc 100644
--- a/rtbuf_signal.c
+++ b/rtbuf_signal.c
@@ -5,55 +5,12 @@
 #include "rtbuf_lib.h"
 #include "rtbuf_signal.h"
 
-enum {
-  SINUS_FREQUENCY = 0,
-  SINUS_AMPLITUDE,
-  SINUS_ARITY
-};
-
-typedef struct sinus_data {
-  double samples[RTBUF_SIGNAL_SAMPLES];
-  double phase;
-} s_sinus_data;
-
-extern int sinus (s_rtbuf *rtb);
-extern int sinus_start (s_rtbuf *rtb);
-
-const char *sinus_vars[] = { "frequency", "amplitude", 0 };
-
 const char     *rtbuf_lib_name = "signal";
 unsigned long   rtbuf_lib_ver = RTBUF_LIB_VER;
 s_rtbuf_lib_fun rtbuf_lib_fun[] = {
-  { "sinus", sinus, sinus_start, 0,
-    sizeof(s_sinus_data) / sizeof(double),
-    sizeof(double), sinus_vars },
+  { "sinus", rtbuf_signal_sinus, rtbuf_signal_sinus_start, 0,
+    sizeof(s_rtbuf_signal_sinus_data) / sizeof(double),
+    sizeof(double),
+    (const char*[]) { "frequency", "amplitude", 0 } },
   { 0, 0, 0, 0, 0, 0, 0 }
 };
-
-int sinus (s_rtbuf *rtb)
-{
-  double phase;
-  unsigned int i = 0;
-  s_sinus_data *data = (s_sinus_data*) rtb->data;
-  int freq = rtb->var[SINUS_FREQUENCY];
-  double *freq_samples = freq < 0 ? 0 : (double*) g_rtbuf[freq].data;
-  phase = data->phase;
-  while (i < RTBUF_SIGNAL_SAMPLES) {
-    double f = freq_samples ? freq_samples[i] : 220;
-    //printf(" i=%u freq=%f", i, f);
-    f /= (double) RTBUF_SIGNAL_SAMPLERATE;
-    phase = phase + 2.0 * M_PI * f;
-    data->samples[i] = sin(phase);
-    //printf(" sin=%f", data->samples[i]);
-    i++;
-  }
-  data->phase = phase;
-  return 0;
-}
-
-int sinus_start (s_rtbuf *rtb)
-{
-  s_sinus_data *data = (s_sinus_data*) rtb->data;
-  data->phase = 0;
-  return 0;
-}
diff --git a/rtbuf_signal.h b/rtbuf_signal.h
index cd677c7..23dc781 100644
--- a/rtbuf_signal.h
+++ b/rtbuf_signal.h
@@ -4,4 +4,22 @@
 #define RTBUF_SIGNAL_SAMPLES 256
 #define RTBUF_SIGNAL_SAMPLERATE 44100
 
+/* sinus */
+
+enum {
+  RTBUF_SIGNAL_SINUS_VAR_FREQUENCY = 0,
+  RTBUF_SIGNAL_SINUS_VAR_AMPLITUDE,
+  RTBUF_SIGNAL_SINUS_VAR_N
+};
+
+typedef struct rtbuf_signal_sinus_data {
+  double samples[RTBUF_SIGNAL_SAMPLES];
+  double phase;
+} s_rtbuf_signal_sinus_data;
+
+int rtbuf_signal_sinus (s_rtbuf *rtb);
+int rtbuf_signal_sinus_start (s_rtbuf *rtb);
+
+const char **g_rtbuf_signal_sinus_vars;
+
 #endif /* RTBUF_SIGNAL_H */
diff --git a/rtbuf_signal_sinus.c b/rtbuf_signal_sinus.c
new file mode 100644
index 0000000..8e6e1f4
--- /dev/null
+++ b/rtbuf_signal_sinus.c
@@ -0,0 +1,36 @@
+
+#include <math.h>
+#include <stdio.h>
+#include "rtbuf.h"
+#include "rtbuf_lib.h"
+#include "rtbuf_signal.h"
+
+int rtbuf_signal_sinus (s_rtbuf *rtb)
+{
+  double phase;
+  unsigned int i = 0;
+  s_rtbuf_signal_sinus_data *data;
+  int freq = rtb->var[RTBUF_SIGNAL_SINUS_VAR_FREQUENCY];
+  double *freq_samples = freq < 0 ? 0 : (double*) g_rtbuf[freq].data;
+  data = (s_rtbuf_signal_sinus_data*) rtb->data;
+  phase = data->phase;
+  while (i < RTBUF_SIGNAL_SAMPLES) {
+    double f = freq_samples ? freq_samples[i] : 220;
+    //printf(" i=%u freq=%f", i, f);
+    f /= (double) RTBUF_SIGNAL_SAMPLERATE;
+    phase = phase + 2.0 * M_PI * f;
+    data->samples[i] = sin(phase);
+    //printf(" sin=%f", data->samples[i]);
+    i++;
+  }
+  data->phase = phase;
+  return 0;
+}
+
+int rtbuf_signal_sinus_start (s_rtbuf *rtb)
+{
+  s_rtbuf_signal_sinus_data *data;
+  data = (s_rtbuf_signal_sinus_data*) rtb->data;
+  data->phase = 0;
+  return 0;
+}