wavrecord.c
Go to the documentation of this file.
1 /*
2  * This file is part of the OpenKinect Project. http://www.openkinect.org
3  *
4  * Copyright (c) 2011 individual OpenKinect contributors. See the CONTRIB file
5  * for details.
6  *
7  * This code is licensed to you under the terms of the Apache License, version
8  * 2.0, or, at your option, the terms of the GNU General Public License,
9  * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses,
10  * or the following URLs:
11  * http://www.apache.org/licenses/LICENSE-2.0
12  * http://www.gnu.org/licenses/gpl-2.0.txt
13  *
14  * If you redistribute this file in source form, modified or unmodified, you
15  * may:
16  * 1) Leave this header intact and distribute it under the same terms,
17  * accompanying it with the APACHE20 and GPL20 files, or
18  * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or
19  * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file
20  * In all cases you must keep the copyright notice intact and include a copy
21  * of the CONTRIB file.
22  *
23  * Binary distributions must follow the binary distribution requirements of
24  * either License.
25  */
26 
27 #include "libfreenect.h"
28 #include "libfreenect_audio.h"
29 #include <stdio.h>
30 #include <signal.h>
31 
34 int die = 0;
35 
36 char wavheader[] = {
37  0x52, 0x49, 0x46, 0x46, // ChunkID = "RIFF"
38  0x00, 0x00, 0x00, 0x00, // Chunksize (will be overwritten later)
39  0x57, 0x41, 0x56, 0x45, // Format = "WAVE"
40  0x66, 0x6d, 0x74, 0x20, // Subchunk1ID = "fmt "
41  0x10, 0x00, 0x00, 0x00, // Subchunk1Size = 16
42  0x01, 0x00, 0x01, 0x00, // AudioFormat = 1 (linear quantization) | NumChannels = 1
43  0x80, 0x3e, 0x00, 0x00, // SampleRate = 16000 Hz
44  0x00, 0xfa, 0x00, 0x00, // ByteRate = SampleRate * NumChannels * BitsPerSample/8 = 64000
45  0x04, 0x00, 0x20, 0x00, // BlockAlign = NumChannels * BitsPerSample/8 = 4 | BitsPerSample = 32
46  0x64, 0x61, 0x74, 0x61, // Subchunk2ID = "data"
47  0x00, 0x00, 0x00, 0x00, // Subchunk2Size = NumSamples * NumChannels * BitsPerSample / 8 (will be overwritten later)
48 };
49 
50 typedef struct {
51  FILE* logfiles[4];
52  int samples;
53 } capture;
54 
55 void in_callback(freenect_device* dev, int num_samples,
56  int32_t* mic1, int32_t* mic2,
57  int32_t* mic3, int32_t* mic4,
58  int16_t* cancelled, void *unknown) {
59  capture* c = (capture*)freenect_get_user(dev);
60  fwrite(mic1, 1, num_samples*sizeof(int32_t), c->logfiles[0]);
61  fwrite(mic2, 1, num_samples*sizeof(int32_t), c->logfiles[1]);
62  fwrite(mic3, 1, num_samples*sizeof(int32_t), c->logfiles[2]);
63  fwrite(mic4, 1, num_samples*sizeof(int32_t), c->logfiles[3]);
64  c->samples += num_samples;
65  printf("Sample received. Total samples recorded: %d\n", c->samples);
66 }
67 
68 void cleanup(int sig) {
69  printf("Caught SIGINT, cleaning up\n");
70  die = 1;
71 }
72 
73 int main(int argc, char** argv) {
74  if (freenect_init(&f_ctx, NULL) < 0) {
75  printf("freenect_init() failed\n");
76  return 1;
77  }
80 
81  int nr_devices = freenect_num_devices (f_ctx);
82  printf ("Number of devices found: %d\n", nr_devices);
83  if (nr_devices < 1) {
84  freenect_shutdown(f_ctx);
85  return 1;
86  }
87 
88  int user_device_number = 0;
89  if (freenect_open_device(f_ctx, &f_dev, user_device_number) < 0) {
90  printf("Could not open device\n");
91  freenect_shutdown(f_ctx);
92  return 1;
93  }
94 
95  capture state;
96  state.samples = 0;
97  state.logfiles[0] = fopen("channel1.wav", "wb");
98  state.logfiles[1] = fopen("channel2.wav", "wb");
99  state.logfiles[2] = fopen("channel3.wav", "wb");
100  state.logfiles[3] = fopen("channel4.wav", "wb");
101  fwrite(wavheader, 1, 44, state.logfiles[0]);
102  fwrite(wavheader, 1, 44, state.logfiles[1]);
103  fwrite(wavheader, 1, 44, state.logfiles[2]);
104  fwrite(wavheader, 1, 44, state.logfiles[3]);
105  freenect_set_user(f_dev, &state);
106 
108  freenect_start_audio(f_dev);
109  signal(SIGINT, cleanup);
110 
111  while(!die && freenect_process_events(f_ctx) >= 0) {
112  // If we did anything else, it might go here.
113  // Alternately, we might split off another thread
114  // to do this loop while the main thread did something
115  // interesting.
116  }
117 
118  // Make the WAV header valid for each of the four files
119  int i;
120  for(i = 0; i < 4 ; i++) {
121  char buf[4];
122  fseek(state.logfiles[i], 4, SEEK_SET);
123  // Write ChunkSize = 36 + subchunk2size
124  int chunksize = state.samples * 4 + 36;
125  buf[0] = (chunksize & 0x000000ff);
126  buf[1] = (chunksize & 0x0000ff00) >> 8;
127  buf[2] = (chunksize & 0x00ff0000) >> 16;
128  buf[3] = (chunksize & 0xff000000) >> 24;
129  fwrite(buf, 1, 4,state.logfiles[i]);
130 
131  fseek(state.logfiles[i], 40, SEEK_SET);
132  // Write Subchunk2Size = NumSamples * NumChannels (1) * BitsPerSample/8 (4)
133  int subchunk2size = state.samples * 4;
134  buf[0] = (subchunk2size & 0x000000ff);
135  buf[1] = (subchunk2size & 0x0000ff00) >> 8;
136  buf[2] = (subchunk2size & 0x00ff0000) >> 16;
137  buf[3] = (subchunk2size & 0xff000000) >> 24;
138  fwrite(buf, 1, 4,state.logfiles[i]);
139  fclose(state.logfiles[i]);
140  }
141 
142  freenect_shutdown(f_ctx);
143  return 0;
144 }
int main(int argc, char **argv)
Definition: wavrecord.c:73
void freenect_select_subdevices(freenect_context *ctx, freenect_device_flags subdevs)
Definition: fakenect.c:338
static freenect_device * f_dev
Definition: wavrecord.c:33
short int16_t
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)
Definition: wavrecord.c:55
int freenect_num_devices(freenect_context *ctx)
Definition: fakenect.c:308
int freenect_shutdown(freenect_context *ctx)
Definition: fakenect.c:402
int samples
Definition: wavrecord.c:52
char wavheader[]
Definition: wavrecord.c:36
void freenect_set_log_level(freenect_context *ctx, freenect_loglevel level)
Definition: fakenect.c:401
FREENECTAPI void freenect_set_audio_in_callback(freenect_device *dev, freenect_audio_in_cb callback)
Definition: audio.c:142
void * freenect_get_user(freenect_device *dev)
Definition: fakenect.c:360
int int32_t
FREENECTAPI int freenect_start_audio(freenect_device *dev)
Definition: audio.c:149
void cleanup(int sig)
Definition: wavrecord.c:68
int freenect_open_device(freenect_context *ctx, freenect_device **dev, int index)
Definition: fakenect.c:314
static freenect_context * f_ctx
Definition: wavrecord.c:32
FILE * logfiles[4]
Definition: wavrecord.c:51
int die
Definition: wavrecord.c:34
capture state
Definition: micview.c:53
int freenect_process_events(freenect_context *ctx)
Definition: fakenect.c:140
void freenect_set_user(freenect_device *dev, void *user)
Definition: fakenect.c:355
int freenect_init(freenect_context **ctx, freenect_usb_context *usb_ctx)
Definition: fakenect.c:327


libfreenect
Author(s): Hector Martin, Josh Blake, Kyle Machulis, OpenKinect community
autogenerated on Thu Jun 6 2019 19:25:38