From 43d33129903823d162df132ab9293a903e053776 Mon Sep 17 00:00:00 2001
From: Ludovic Pouzenc <ludovic@pouzenc.fr>
Date: Sat, 4 May 2013 21:04:19 +0000
Subject: Ajout Copyright 2013 + amelioration capture pulse

git-svn-id: file:///var/svn/2012-violon-leds/trunk@34 6be1fa4d-33ac-4c33-becc-79fcb3794bb6
---
 src/Makefile       |  5 ++---
 src/capture.c      | 62 ++++++++++++++++++++++++++++++++++++++++++++++--------
 src/compute.c      |  6 +++---
 src/gui.glade      |  2 +-
 src/hsv2rgb.c      |  2 +-
 src/illuminate.c   |  2 +-
 src/instru2light.c |  4 ++--
 src/win_main.c     |  2 +-
 8 files changed, 64 insertions(+), 21 deletions(-)

diff --git a/src/Makefile b/src/Makefile
index 17a38f5..e7ddab8 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -1,7 +1,6 @@
 CC=gcc
-# For debug
-#CFLAGS=-W -Wall -Werror -Wno-error=unused-parameter -g
-CFLAGS=-W -Wall -Wno-error=unused-parameter -g
+# For working
+CFLAGS=-W -Wall -Werror -Wno-error=unused-parameter -g
 #LDFLAGS=-Werror -g
 
 # For release
diff --git a/src/capture.c b/src/capture.c
index f9bb8f5..347cc4c 100644
--- a/src/capture.c
+++ b/src/capture.c
@@ -1,6 +1,6 @@
 /*
     Instru2Light - Illumine un instrument de musique en temps réel
-    Copyright (C) 2012 Ludovic Pouzenc <lpouzenc@gmail.com>    
+    Copyright (C) 2012-2013 Ludovic Pouzenc <lpouzenc@gmail.com>    
 
     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -18,6 +18,7 @@
 #include "capture.h"
 #include <pulse/pulseaudio.h>
 #include <gtk/gtk.h>
+#include <string.h> // memcpy()
 
 #define APP_TITLE "Test 5 lpo"
 #define BUFSIZE 1024
@@ -64,7 +65,7 @@ int capture_init(pa_mainloop **m, pa_context **c) {
 	*m=pa_mainloop_new();
 	g_assert(*m);
 
-        *c = pa_context_new(pa_mainloop_get_api(*m), APP_TITLE);
+	*c = pa_context_new(pa_mainloop_get_api(*m), APP_TITLE);
 	g_assert(*c);
 
 	pa_context_set_state_callback(*c, context_state_callback, NULL);
@@ -158,11 +159,11 @@ void context_get_source_info_callback(pa_context *c, const pa_source_info *si, i
 
 pa_stream *create_stream(pa_context *c, const pa_source_info *si) {
 	static const pa_buffer_attr ba={
+		//.maxlength=-1,
 		.maxlength=-1,
 		.tlength=1024,
 		.prebuf=-1,
 		.minreq=-1,
-		//.minreq=256, //For FFT calculus
 		.fragsize=512
 	};
 
@@ -223,19 +224,62 @@ void stream_state_callback(pa_stream *s, void *userdata) {
 	}
 }
 
-void stream_read_callback(pa_stream *s, size_t nbytes, void *userdata) {
-	const void *p;
+void stream_read_callback(pa_stream *s, size_t bytes_new, void *userdata) {
+	const void *pa_data=NULL;
+	void *buf=NULL, *process_data=NULL;
+	size_t bytes_readable, bytes_read, process_size, buf_index=0;
 	//printf("stream_read_callback %i\n", nbytes);
 
-	if (pa_stream_peek(s, &p, &nbytes) < 0) {
+	bytes_readable = pa_stream_readable_size(s);
+	if (bytes_readable/sizeof(float) < 256) {
+		// If buffer is too small for FFT analysis, wait next time
+		printf("s"); fflush(stdout);
+		return;
+	}
+
+	bytes_read=bytes_readable;
+	if (pa_stream_peek(s, &pa_data, &bytes_read) < 0) {
 		printf("pa_stream_peek() failed\n");//: %s", pa_strerror(pa_context_errno(context)));
 		return;
 	}
+	assert(pa_data);
+	assert(bytes_read>0);
+
+	if (bytes_read == bytes_readable) {
+		// If all is catched at once, no malloc, no memcpy
+		process_data=(void *)pa_data; // FIXME : loss of const modifier is problematic
+		process_size=bytes_read;
+
+		//printf("o"); fflush(stdout);
+	} else {
+		// Need to concatenate buffers
+		printf("M"); fflush(stdout);
+		do {
+			printf("."); fflush(stdout);
+			if (!buf) {
+				buf_index=0;
+				buf = pa_xmalloc(bytes_read);
+			} else {
+				buf = pa_xrealloc(buf, buf_index + bytes_read);
+			}
+
+			memcpy(buf+buf_index, pa_data, bytes_read);
+			buf_index += bytes_read;
+
+			pa_stream_drop(s);
+			if (pa_stream_peek(s, &pa_data, &bytes_read) < 0) {
+				bytes_read=0;
+				 printf("!"); fflush(stdout);
+			}
+		} while (bytes_read>0);
+
+		process_data=buf;
+		process_size=buf_index;
+	}
 
-//printf("debug : before call capture_sound_level_cb==%p\n", capture_sound_level_cb);
-	my_process((float *)p,nbytes/sizeof(float), pa_stream_get_sample_spec(s)->channels);
-//printf("debug : after call capture_sound_level_cb==%p\n", capture_sound_level_cb);
+	my_process((float *)process_data, process_size/sizeof(float), pa_stream_get_sample_spec(s)->channels);
 
+	if (buf) pa_xfree(buf);
 	pa_stream_drop(s);
 }
 
diff --git a/src/compute.c b/src/compute.c
index e574838..8ea0afb 100644
--- a/src/compute.c
+++ b/src/compute.c
@@ -1,6 +1,6 @@
 /*
     Instru2Light - Illumine un instrument de musique en temps réel
-    Copyright (C) 2012 Ludovic Pouzenc <lpouzenc@gmail.com>    
+    Copyright (C) 2012-2013 Ludovic Pouzenc <lpouzenc@gmail.com>    
 
     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -34,11 +34,11 @@ float compute_level(const float *data, size_t nsamples, int rate) {
 	int f, min_f_index, max_f_index;
 
 	if (nsamples >= MAX_SAMPLES) {
-		printf("WARN : nsamples >= MAX_SAMPLES : %i >= %i\n", nsamples, MAX_SAMPLES);
+		printf("WARN : nsamples >= MAX_SAMPLES : %zu >= %i\n", nsamples, MAX_SAMPLES);
 		nsamples=MAX_SAMPLES;
 	}
 	if (nsamples < MIN_SAMPLES) {
-		printf("WARN : nsamples < MIN_SAMPLES : %i >= %i\n", nsamples, MIN_SAMPLES);
+		printf("WARN : nsamples < MIN_SAMPLES : %zu >= %i\n", nsamples, MIN_SAMPLES);
 		return -120.f;
 	}
 		/* Replicate with symmetry the sound to obtain an input buffer of the minimal len
diff --git a/src/gui.glade b/src/gui.glade
index 7db627c..416fc89 100644
--- a/src/gui.glade
+++ b/src/gui.glade
@@ -30,7 +30,7 @@
     <property name="has_separator">True</property>
     <property name="program_name">Instru2Light</property>
     <property name="version">1.0</property>
-    <property name="copyright" translatable="yes">Copyright (C) 2012 Ludovic Pouzenc &lt;lpouzenc@gmail.com&gt;</property>
+    <property name="copyright" translatable="yes">Copyright (C) 2012-2013 Ludovic Pouzenc &lt;lpouzenc@gmail.com&gt;</property>
     <property name="comments" translatable="yes">Illumine un instrument de musique en temps réel</property>
     <property name="website">http://sf.net/projects/instru2light</property>
     <property name="license">GNU GENERAL PUBLIC LICENSE
diff --git a/src/hsv2rgb.c b/src/hsv2rgb.c
index 9f445e9..c24aabd 100644
--- a/src/hsv2rgb.c
+++ b/src/hsv2rgb.c
@@ -1,7 +1,7 @@
 /* hsv2rgb.c
  * Convert Hue Saturation Value to Red Green Blue
  *
- * P.J. 08-Aug-98
+ * P.J. 08-Aug-98 ( P. A. Jacobs <peterj@mech.uq.edu.au> )
  *
  * Reference:
  * D. F. Rogers
diff --git a/src/illuminate.c b/src/illuminate.c
index d5d40b2..1205063 100644
--- a/src/illuminate.c
+++ b/src/illuminate.c
@@ -1,6 +1,6 @@
 /*
     Instru2Light - Illumine un instrument de musique en temps réel
-    Copyright (C) 2012 Ludovic Pouzenc <lpouzenc@gmail.com>    
+    Copyright (C) 2012-2013 Ludovic Pouzenc <lpouzenc@gmail.com>    
 
     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
diff --git a/src/instru2light.c b/src/instru2light.c
index a72fd0f..1914b10 100644
--- a/src/instru2light.c
+++ b/src/instru2light.c
@@ -1,6 +1,6 @@
 /*
     Instru2Light - Illumine un instrument de musique en temps réel
-    Copyright (C) 2012 Ludovic Pouzenc <lpouzenc@gmail.com>    
+    Copyright (C) 2012-2013 Ludovic Pouzenc <lpouzenc@gmail.com>    
 
     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -75,7 +75,7 @@ void my_process(float *data, size_t nsamples, size_t nchan) {
 	struct rgb_colour rgb;
 
 	//FIXME : rate should came from PulseAudio
-	sound_level=compute_level(data, nsamples, 44100);
+	sound_level=compute_level(data, nsamples, 44100); //FIXME : no fixed rate here !
 
 //	printf("sound_level==%+8f nsamples==%8i\n", sound_level, nsamples);
 
diff --git a/src/win_main.c b/src/win_main.c
index 01d5561..7906325 100644
--- a/src/win_main.c
+++ b/src/win_main.c
@@ -1,6 +1,6 @@
 /*
     Instru2Light - Illumine un instrument de musique en temps réel
-    Copyright (C) 2012 Ludovic Pouzenc <lpouzenc@gmail.com>    
+    Copyright (C) 2012-2013 Ludovic Pouzenc <lpouzenc@gmail.com>    
 
     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
-- 
cgit v1.2.3