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
|
/*
* This file is part of the OpenKinect Project. http://www.openkinect.org
*
* Copyright (c) 2011 individual OpenKinect contributors. See the CONTRIB file
* for details.
*
* This code is licensed to you under the terms of the Apache License, version
* 2.0, or, at your option, the terms of the GNU General Public License,
* version 2.0. See the APACHE20 and GPL2 files for the text of the licenses,
* or the following URLs:
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.gnu.org/licenses/gpl-2.0.txt
*
* If you redistribute this file in source form, modified or unmodified, you
* may:
* 1) Leave this header intact and distribute it under the same terms,
* accompanying it with the APACHE20 and GPL20 files, or
* 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or
* 3) Delete the GPL v2 clause and accompany it with the APACHE20 file
* In all cases you must keep the copyright notice intact and include a copy
* of the CONTRIB file.
*
* Binary distributions must follow the binary distribution requirements of
* either License.
*/
#include "libfreenect.h"
#include "libfreenect_audio.h"
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <pthread.h>
#if defined(__APPLE__)
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
pthread_t freenect_thread;
volatile int die = 0;
static freenect_context* f_ctx;
static freenect_device* f_dev;
typedef struct {
int32_t* buffers[4];
int max_samples;
int current_idx; // index to the oldest data in the buffer (equivalently, where the next new data will be placed)
int new_data;
} capture;
capture state;
int paused = 0;
pthread_mutex_t audiobuf_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t audiobuf_cond = PTHREAD_COND_INITIALIZER;
int win_h, win_w;
void in_callback(freenect_device* dev, int num_samples,
int32_t* mic1, int32_t* mic2,
int32_t* mic3, int32_t* mic4,
int16_t* cancelled, void *unknown) {
pthread_mutex_lock(&audiobuf_mutex);
capture* c = (capture*)freenect_get_user(dev);
if(num_samples < c->max_samples - c->current_idx) {
memcpy(&(c->buffers[0][c->current_idx]), mic1, num_samples*sizeof(int32_t));
memcpy(&(c->buffers[1][c->current_idx]), mic2, num_samples*sizeof(int32_t));
memcpy(&(c->buffers[2][c->current_idx]), mic3, num_samples*sizeof(int32_t));
memcpy(&(c->buffers[3][c->current_idx]), mic4, num_samples*sizeof(int32_t));
} else {
int first = c->max_samples - c->current_idx;
int left = num_samples - first;
memcpy(&(c->buffers[0][c->current_idx]), mic1, first*sizeof(int32_t));
memcpy(&(c->buffers[1][c->current_idx]), mic2, first*sizeof(int32_t));
memcpy(&(c->buffers[2][c->current_idx]), mic3, first*sizeof(int32_t));
memcpy(&(c->buffers[3][c->current_idx]), mic4, first*sizeof(int32_t));
memcpy(c->buffers[0], &mic1[first], left*sizeof(int32_t));
memcpy(c->buffers[1], &mic2[first], left*sizeof(int32_t));
memcpy(c->buffers[2], &mic3[first], left*sizeof(int32_t));
memcpy(c->buffers[3], &mic4[first], left*sizeof(int32_t));
}
c->current_idx = (c->current_idx + num_samples) % c->max_samples;
c->new_data = 1;
pthread_cond_signal(&audiobuf_cond);
pthread_mutex_unlock(&audiobuf_mutex);
}
void* freenect_threadfunc(void* arg) {
while(!die && freenect_process_events(f_ctx) >= 0) {
// If we did anything else in the freenect thread, it might go here.
}
freenect_stop_audio(f_dev);
freenect_close_device(f_dev);
freenect_shutdown(f_ctx);
return NULL;
}
void DrawMicData() {
if (paused)
return;
pthread_mutex_lock(&audiobuf_mutex);
while(!state.new_data)
pthread_cond_wait(&audiobuf_cond, &audiobuf_mutex);
state.new_data = 0;
// Draw:
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
float xIncr = (float)win_w / state.max_samples;
float x = 0.;
int i;
int base_idx = state.current_idx;
// Technically, we should hold the lock until we're done actually drawing
// the lines, but this is sufficient to ensure that the drawings align
// provided we don't reallocate buffers.
pthread_mutex_unlock(&audiobuf_mutex);
// This is kinda slow. It should be possible to compile each sample
// window into a glCallList, but that's overly complex.
int mic;
for(mic = 0; mic < 4; mic++) {
glBegin(GL_LINE_STRIP);
glColor4f(1.0f, 1.0f, 1.0f, 0.7f);
for(x = 0, i = 0; i < state.max_samples; i++) {
glVertex3f(x, ((float)win_h * (float)(2*mic + 1) / 8. ) + (float)(state.buffers[mic][(base_idx + i) % state.max_samples]) * ((float)win_h/4) /2147483647. , 0);
x += xIncr;
}
glEnd();
}
glutSwapBuffers();
}
void Reshape(int w, int h) {
win_w = w;
win_h = h;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, (float)w, (float)h, 0.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void Keyboard(unsigned char key, int x, int y) {
if(key == 'q') {
die = 1;
pthread_exit(NULL);
}
if(key == 32) {
paused = !paused;
}
}
int main(int argc, char** argv) {
if (freenect_init(&f_ctx, NULL) < 0) {
printf("freenect_init() failed\n");
return 1;
}
freenect_set_log_level(f_ctx, FREENECT_LOG_INFO);
freenect_select_subdevices(f_ctx, FREENECT_DEVICE_AUDIO);
int nr_devices = freenect_num_devices (f_ctx);
printf ("Number of devices found: %d\n", nr_devices);
if (nr_devices < 1) {
freenect_shutdown(f_ctx);
return 1;
}
int user_device_number = 0;
if (freenect_open_device(f_ctx, &f_dev, user_device_number) < 0) {
printf("Could not open device\n");
freenect_shutdown(f_ctx);
return 1;
}
state.max_samples = 256 * 60;
state.current_idx = 0;
state.buffers[0] = (int32_t*)malloc(state.max_samples * sizeof(int32_t));
state.buffers[1] = (int32_t*)malloc(state.max_samples * sizeof(int32_t));
state.buffers[2] = (int32_t*)malloc(state.max_samples * sizeof(int32_t));
state.buffers[3] = (int32_t*)malloc(state.max_samples * sizeof(int32_t));
memset(state.buffers[0], 0, state.max_samples * sizeof(int32_t));
memset(state.buffers[1], 0, state.max_samples * sizeof(int32_t));
memset(state.buffers[2], 0, state.max_samples * sizeof(int32_t));
memset(state.buffers[3], 0, state.max_samples * sizeof(int32_t));
freenect_set_user(f_dev, &state);
freenect_set_audio_in_callback(f_dev, in_callback);
freenect_start_audio(f_dev);
int res = pthread_create(&freenect_thread, NULL, freenect_threadfunc, NULL);
if (res) {
printf("pthread_create failed\n");
freenect_shutdown(f_ctx);
return 1;
}
printf("This is the libfreenect microphone waveform viewer. Press 'q' to quit or spacebar to pause/unpause the view.\n");
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_ALPHA );
glutInitWindowSize(800, 600);
glutInitWindowPosition(0, 0);
glutCreateWindow("Microphones");
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Reshape(800, 600);
glutReshapeFunc(Reshape);
glutDisplayFunc(DrawMicData);
glutIdleFunc(DrawMicData);
glutKeyboardFunc(Keyboard);
glutMainLoop();
return 0;
}
|