summaryrefslogtreecommitdiff
path: root/src/capture.c
blob: 347cc4c8fbac7006eb323d7d36b4fee7947a4d0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
    Instru2Light - Illumine un instrument de musique en temps réel
    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
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "capture.h"
#include <pulse/pulseaudio.h>
#include <gtk/gtk.h>
#include <string.h> // memcpy()

#define APP_TITLE "Test 5 lpo"
#define BUFSIZE 1024

extern void my_process(float *data, size_t nsamples, size_t nchan);

capture_sound_level_cb_t *capture_sound_level_cb=NULL;

int capture_init(pa_mainloop **m, pa_context **c);
void context_state_callback(pa_context *c, void *userdata);
void context_get_server_info_callback(pa_context *c, const pa_server_info*si, void *userdata);
void context_get_source_info_callback(pa_context *c, const pa_source_info *si, int is_last, void *userdata);
void context_get_source_info_list_callback(pa_context *c, const pa_source_info *si, int is_last, void *userdata);
pa_stream * create_stream(pa_context *c, const pa_source_info *si);
void stream_state_callback(pa_stream *s, void *userdata);
void stream_read_callback(pa_stream *s, size_t nbytes, void *userdata);

char *device_name=NULL;

// Main procedure of this thread
void audio_thread(void *args) {
	pa_context *c;
	pa_mainloop *m;
	int res, retval;

	capture_sound_level_cb=(capture_sound_level_cb_t *)args;

	//printf("debug : args==%p capture_sound_level_cb==%p\n", args, capture_sound_level_cb);
	
	res=capture_init(&m, &c);
	g_assert(res==0);

	res=pa_mainloop_run(m,&retval);
	g_assert(res==0);
	g_assert(retval==0);

//	pa_context_disconnect(pa_ct);
	pa_mainloop_free(m);
}

int capture_init(pa_mainloop **m, pa_context **c) {
	int res=0;

	*m=pa_mainloop_new();
	g_assert(*m);

	*c = pa_context_new(pa_mainloop_get_api(*m), APP_TITLE);
	g_assert(*c);

	pa_context_set_state_callback(*c, context_state_callback, NULL);
	res=pa_context_connect(*c, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL);

	return res;
}

void context_state_callback(pa_context *c, void *userdata) {
	switch (pa_context_get_state(c)) {
		case PA_CONTEXT_UNCONNECTED:
			printf("PA_CONTEXT_UNCONNECTED\n");
			break;
		case PA_CONTEXT_CONNECTING:
			printf("PA_CONTEXT_CONNECTING\n");
			break;
		case PA_CONTEXT_AUTHORIZING:
			printf("PA_CONTEXT_AUTHORIZING\n");
			break;
		case PA_CONTEXT_SETTING_NAME:
			printf("PA_CONTEXT_SETTING_NAME\n");
			break;
		case PA_CONTEXT_READY:
			printf("PA_CONTEXT_READY\n");
//			if (!device_name) {
				pa_operation_unref(pa_context_get_server_info(c, context_get_server_info_callback, NULL));
/*			} else {
				pa_operation_unref(pa_context_get_source_info_by_name(c, device_name, context_get_source_info_callback, NULL));
			}
*/
			break;

		case PA_CONTEXT_FAILED:
			printf("PA_CONTEXT_FAILED\n");
			break;

		case PA_CONTEXT_TERMINATED:
			printf("PA_CONTEXT_TERMINATED\n");
			break;
	}
}

void context_get_server_info_callback(pa_context *c, const pa_server_info*si, void *userdata) {
	if (!si) {
		printf("Failed to get server information\n");
		return;
	}

	if (!si->default_source_name) {
		printf("No default source set\n");
		return;
	}

	//Temp :
	pa_operation_unref(pa_context_get_source_info_list(c,context_get_source_info_list_callback,NULL));


	pa_operation_unref(pa_context_get_source_info_by_name(c, si->default_source_name, context_get_source_info_callback, NULL));

}

//Temp
void context_get_source_info_list_callback(pa_context *c, const pa_source_info *si, int is_last, void *userdata) {
	if (is_last < 0) {
		printf("Failed to get sink information\n");
		return;
	}

	if (!si) {
		return;
	}

	printf("%i : '%s' (%s)\n", si->index, si->name, si->description);

}


void context_get_source_info_callback(pa_context *c, const pa_source_info *si, int is_last, void *userdata) {

	if (is_last < 0) {
		printf("Failed to get sink information\n");
		return;
	}

	if (!si) {
		return;
	}

	create_stream(c,si);
}

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,
		.fragsize=512
	};

	pa_sample_spec nss = {
		.format = PA_SAMPLE_FLOAT32LE,
		.rate = si->sample_spec.rate,
		.channels = si->sample_spec.channels
	};

	pa_stream *stream=NULL;
	//pa_proplist *pl=NULL;
	char t[256];

/*
	pa_channel_map cmap;
	pa_channel_map_init_mono(&cmap);
	//pa_channel_map_init_stereo(&cmap);
*/

	printf("Source : %s (%s)\n", si->name, si->description);
	printf("Using sample format: %s\n", pa_sample_spec_snprint(t, sizeof(t), &nss));
	printf("Using channel map: %s\n", pa_channel_map_snprint(t, sizeof(t), &(si->channel_map)));
/*
	pl=pa_proplist_new();
	pa_proplist_set(pl, "maxlength", &pl_maxlength, sizeof(pl_maxlength));
	pa_proplist_set(pl, "tlength", &pl_tlength, sizeof(pl_tlength));
	pa_proplist_set(pl, "fragsize", &pl_fragsize, sizeof(pl_fragsize));
*/
	stream=pa_stream_new(c, APP_TITLE, &nss, &(si->channel_map));
	//stream=pa_stream_new_with_proplist(c, APP_TITLE, &(si->sample_spec), &(si->channel_map), pl);
	pa_stream_set_state_callback(stream, stream_state_callback, NULL);
	pa_stream_set_read_callback(stream, stream_read_callback, NULL);
	//pa_stream_connect_record(stream, APP_TITLE, NULL, (enum pa_stream_flags) 0);
	pa_stream_connect_record(stream, si->name, &ba, PA_STREAM_ADJUST_LATENCY);

	return stream;
}

void stream_state_callback(pa_stream *s, void *userdata) {
	switch (pa_stream_get_state(s)) {
		case PA_STREAM_UNCONNECTED:
			printf("PA_STREAM_UNCONNECTED\n");
			break;
		case PA_STREAM_CREATING:
			printf("PA_STREAM_CREATING\n");
			break;
		case PA_STREAM_READY:
			printf("PA_STREAM_READY\n");
			//g_timeout_add(100, latency_func, NULL);
			//pa_operation_unref(pa_stream_update_timing_info(stream, stream_update_timing_info_callback, NULL));
			break;
		case PA_STREAM_FAILED:
			printf("PA_STREAM_FAILED\n");
			break;
		case PA_STREAM_TERMINATED:
			printf("PA_STREAM_TERMINATED\n");
			break;
	}
}

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);

	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;
	}

	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);
}