-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecorder.cpp
More file actions
536 lines (449 loc) · 16.1 KB
/
Recorder.cpp
File metadata and controls
536 lines (449 loc) · 16.1 KB
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
//#include <iostream> // for standard I/O
//#include <sstream> // std::stringstream, std::stringbuf
//#include <string> // for strings
#include <time.h>
#include <thread>
#include <fstream>
#include <stdio.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <stdexcept>
//#include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat)
//#include <opencv2/highgui/highgui.hpp> // Video write
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
void Help();
int CreateDirectories(String, String, String);//this function will create the necessary folders for the recorder
void SensorStream();
void Timer();
void WriteSRT(int lineCounter, String data);
String FromMillisecondsToSRTFormat(unsigned long val);
void WriteLittleEndian(unsigned int word, int num_bytes);
void WriteWavHeader(const char * filename, int s_rate, int no_input);
unsigned int FloatToBits(float x);
//Globals
FILE * srtFile;
//FILE * trainingFile;//TODO: Delete later
FILE * audioFile;
bool streamComing = false;
bool recordingVideo = false;//to help know when the sensor starts sending stream data
bool recordingStream = false;//to help the SensorStream thread know when to write the
bool recording = false;//to help the SensorStream thread know when to write the
bool terminating = false;//to terminate the timer thread when program exits adequately
bool streaming = true;//to terminate the SensorStream thread when program exits adequately
bool startWriting = false;//this is to align the stream with the recordingx
int sensorVectorSize;//the number of expected inputs per 1 feed from the sensor
int samplingFrequency;//the frequency of which a set of sensor feed is sent
int audioValuesWritten;
double lastReceivedVal;//because I'm stupid, and didn't compile my opencv with c++11, I have to create this variable, and hope that a race condition will not occur
struct timeval tv;
bool startedRecording = false;//to recognize a start recording event
bool stoppedRecording = false;//to recognize a stop recording event
unsigned long long startTime;
unsigned long timeNow;
int main(int argc, const char * argv[]){
Help();
String recorderName = "";
if (argc > 3)
{
recorderName = argv[1];
//recorderObject = argv[2];
sensorVectorSize = atoi(argv[2]);//TODO: handle error later
samplingFrequency = atof(argv[3]);//TODO: handle error later
if (argc > 4)
{
printf("Too much arguments. Please enter the name of the recorder, then a string indicating what is being recorded, and finally the sensor data vector size (3 arguments)");
return 1;
}
}
else
{
printf("Too few arguments. Please enter the name of the recorder, then a string indicating what is being recorded, and finally the sensor data vector size (3 arguments)");
return 1;
}
//--------------------------------------
time_t session;
char buffer [80];
struct tm * timeinfo;
time (&session);
timeinfo = localtime (&session);
//%d%m%H%M%S
strftime (buffer,80,"%d%m%H%M%S",timeinfo);
String sessionName = String(buffer);
String folderName = "/Workspace/Recorder/Videos";
char *home = getenv ("HOME");
String str(home);
folderName = str + folderName;
int status = CreateDirectories(folderName, recorderName, sessionName);
if (status == 1)
{
printf("The was an error creating the necessary folders for the recorder, the program will now exit.\n");
return 1;
}
int fps = 10;
int frameCounter = 0;
double timeIntegral_FPS = 1000/fps;
VideoCapture vcap(0);
if(!vcap.isOpened()){
cout << "Error opening video stream or file" << endl;
return -1;
}
vcap.set(CV_CAP_PROP_CONVERT_RGB , false);
//vcap.set(CV_CAP_PROP_FPS , fps);
//allocate memory and start threads here after passing all cases in which program might exit
int frame_width= vcap.get(CV_CAP_PROP_FRAME_WIDTH);
int frame_height= vcap.get(CV_CAP_PROP_FRAME_HEIGHT);
ifstream infile("Gestures");
string gestureString;
thread ss (SensorStream);
thread tr (Timer);
ss.detach();
tr.detach();
int recordingsCounter = 1;
unsigned long timeDiff;
//SRT related variables
bool readyForGesture = false; //this will be used to indicate whether the recorder is ready to record a gesture or not.
bool writingSRT = false;
int srtLineCounter = 1;
String srtData = "";
VideoWriter * video;
for(;;){
if (startedRecording)
{
//TODO: make the files naming also suitable for windows
String vidFileName = folderName + "/" + recorderName + "/" + sessionName + "/" + std::to_string(recordingsCounter) + ".avi";
String srtFileName = folderName + "/" + recorderName + "/" + sessionName + "/" + std::to_string(recordingsCounter) + ".srt";
String audioFileName = folderName + "/" + recorderName + "/" + sessionName + "/" + std::to_string(recordingsCounter) + ".wav";
//String trainingFileName = "Output/" + recorderName + "/" + sessionName + "/Training/" + std::to_string(recordingsCounter);//TODO: Delete later
srtFile = fopen (srtFileName.c_str(),"w");
//trainingFile = fopen (trainingFileName.c_str() ,"w");
WriteWavHeader(audioFileName.c_str(), samplingFrequency, sensorVectorSize);
video = new VideoWriter(vidFileName,CV_FOURCC('M','J','P','G'),fps, Size(frame_width,frame_height),true);
startedRecording = false;
}
if (stoppedRecording)
{
fclose (srtFile);
//fclose (trainingFile);
fclose (audioFile);
srtLineCounter = 1;
recordingsCounter++;
stoppedRecording = false;
}
Mat frame;
vcap >> frame;
if (startWriting)
{
timeDiff = timeNow - startTime;
String disp1 = "Timer: " + FromMillisecondsToSRTFormat(timeDiff);
putText(frame, disp1, Point(20, 40), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 255, 255), 1);
if (frameCounter * timeIntegral_FPS < timeDiff)
{
video->write(frame); //Record the video till here. It is not needed to have a recording indicator in the recoring output
frameCounter++;
}
//Recording Indicator
circle(frame, Point(26, 16), 8, Scalar(0, 0, 255), -1, 8, 0);
putText(frame, "RECORDING", Point(40, 20), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 255), 1);
}
if(streamComing)
{
circle(frame, Point(426, 16), 8, Scalar(255, 0, 0), -1, 8, 0);
putText(frame, "SENSOR STREAMING", Point(440, 20), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 0, 0), 1);
}
imshow( "Recorder", frame );
char c = (char)waitKey(33);
if( c == 27 ) //Escape button pressed
{
if (recording)
{
//fclose (srtFile);
//fclose (trainingFile);
//fclose(audioFile);
//recording = false;
printf("Please end the recording session first.\n");
}
else
{
terminating = true;
streaming = false;
break;
}
}
else if (c == 'r')
{
if (recording)
{
//TODO: make a loop that will block untill our audio file reaches the time at which we decided to end the recording
//take time, and get only the seconds part
unsigned long endTime;
struct timeval now;
gettimeofday(&now, NULL);
endTime = (unsigned long long)(now.tv_sec) * 1000 + (unsigned long long)(now.tv_usec) / 1000;
endTime -= startTime;
endTime /= 1000;
printf("Please wait while the stream catches up with the video...\n");
int num_channels_sampling_frequency = sensorVectorSize*samplingFrequency;
while (audioValuesWritten/(num_channels_sampling_frequency) <= endTime){}
stoppedRecording = true;
delete video;
printf("Stopped recording!\n");
}
else
{
startedRecording = true;
printf("Started recording!\n");
}
recording = !recording;
}
else if (c == 'n')
{
//TODO: if writingSRT == true
if (recording && !writingSRT)
{
if(getline(infile, gestureString))
{
printf("Recording gesture %s. Press s when ready to record the gesture, and then s again when finished.\n", gestureString.c_str());
readyForGesture = true;
}
else
{
printf("No more gestures to record, you can now end the recording session.\n");
}
}
else
{
printf("Please start a recording session before trying to record a gestrue.\n");
}
}
else if(c == 's')
{
if (readyForGesture && !writingSRT)
{
//record the time at this instance for the srt file, and write it there
srtData += FromMillisecondsToSRTFormat(timeNow - startTime);
srtData += " --> ";
writingSRT = true;
readyForGesture = false;
printf("In\n");
}
else if (!readyForGesture && writingSRT)
{
srtData += FromMillisecondsToSRTFormat(timeNow - startTime);
srtData += "\n";
srtData += gestureString + "\n";
WriteSRT(srtLineCounter, srtData);
srtData = "";
srtLineCounter++;
writingSRT = false;
readyForGesture = true;
printf("Out\n");
}
}
}
return 0;
}
void SensorStream()
{
//double sensorData[sensorVectorSize];
char buffer[256];
float val;
bool activateStream = true; //to activate the stream indicator only once, and avoid data races.
//Data specific to write the srt file
//int srtLineCounter = 1;
//String srtData = "";
//String srtTime = "";
while (streaming)
{
for(int i = 0; i < sensorVectorSize; i++)
{
scanf("%s", buffer);
if(activateStream)
{
streamComing = true;
activateStream = false;
}
//cout <<buffer<<endl;
try
{
val = stof(buffer);
if(recording)
{
if (startWriting)
{
WriteLittleEndian(FloatToBits(val), 4);
audioValuesWritten++;
}
else if (i == sensorVectorSize - 1)//this ensures that if the recording started in the middle of a vector, that the stream will be aligned with the recording
{
gettimeofday(&tv, NULL);
startTime = (unsigned long long)(tv.tv_sec) * 1000 + (unsigned long long)(tv.tv_usec) / 1000;
startWriting = true;
audioValuesWritten = 0;
//cout<<"started..."<<endl;
}
}
else
{
//srtLineCounter = 1;
startWriting = false;
}
}
catch (const std::invalid_argument& ia)
{
printf("Sensor has finshed sending data!\n Please restart the Recorder.\n");
//printf("Sensor has finshed sending data!\n If you are planning on restarting the sensor feed,"
// " please make sure to press the 's' key in advance, otherwise the feed will not be synchronized.\n");
streaming = false;
}
}
}
}
void Timer()
{
struct timeval now;
while(!terminating)//TODO, check if there is a better practice
{
gettimeofday(&now, NULL);
timeNow = (unsigned long long)(now.tv_sec) * 1000 + (unsigned long long)(now.tv_usec) / 1000;
}
}
void WriteSRT(int lineCounter, String data)
{
fprintf (srtFile, "%d\n", lineCounter);
fprintf (srtFile, "%s\n", data.c_str());
}
String FromMillisecondsToSRTFormat(unsigned long val)
{
int milliseconds = val/1000;
milliseconds = val - milliseconds*1000;
int seconds = (int) (val / 1000) % 60 ;
int minutes = (int) ((val / (1000*60)) % 60);
int hours = (int) ((val / (1000*60*60)) % 24);
char str[20];
sprintf(str, "%02d:%02d:%02d,%03d", hours, minutes, seconds, milliseconds);
//ss<< setfill('0') << setw(2) <<hours<<":"<< setfill('0') << setw(2) <<minutes<<":"<< setfill('0') << setw(2) <<seconds<<","<< milliseconds<<endl;
return str;
}
unsigned int FloatToBits(float x)
{
unsigned int y;
memcpy(&y, &x, 4);
return y;
}
void WriteLittleEndian(unsigned int word, int num_bytes)
{
unsigned buf;
while(num_bytes>0)
{
buf = word & 0xff;
fwrite(&buf, 1,1, audioFile);
num_bytes--;
word >>= 8;
}
}
void WriteWavHeader(const char * filename, int s_rate, int no_input)
{
unsigned int sample_rate;
unsigned int num_channels;
unsigned int bytes_per_sample;
unsigned int byte_rate;
num_channels = no_input;//1 /* monoaural */
bytes_per_sample = 4;
if (s_rate<=0) sample_rate = 44100;
else sample_rate = (unsigned int) s_rate;
byte_rate = sample_rate*num_channels*bytes_per_sample;
//byte_rate = 0;
audioFile = fopen(filename, "w");
assert(audioFile); /* make sure it opened */
/* write RIFF header */
fwrite("RIFF", 1, 4, audioFile);
//WriteLittleEndian(36 + bytes_per_sample* num_samples*num_channels, 4);
WriteLittleEndian(0, 4);
fwrite("WAVE", 1, 4, audioFile);
/* write fmt subchunk */
fwrite("fmt ", 1, 4, audioFile);
WriteLittleEndian(16, 4); /* SubChunk1Size is 16 */
WriteLittleEndian(3, 2); /* PCM is format 1. IEEE float is format 3*/
WriteLittleEndian(num_channels, 2);
WriteLittleEndian(sample_rate, 4);
WriteLittleEndian(byte_rate, 4);
WriteLittleEndian(num_channels*bytes_per_sample, 2); /* block align */
WriteLittleEndian(8*bytes_per_sample, 2); /* bits/sample */
/* write data subchunk */
fwrite("data", 1, 4, audioFile);
//WriteLittleEndian(bytes_per_sample* num_samples*num_channels, 4);
WriteLittleEndian(0, 4);
}
void Help()
{
cout << "\nThis program helps you train your sensor data by piping the sensor stream directly into this program.\n\n"
"Please make sure that the Recorder is active before starting the sensor stream feed, otherwise the feed will not be synchronized.\n\n"
"Usage:\n"
"\t./Recorder [recorder_name] [expected_#_inputs] [sampling_frequency]\n" << endl;
//"./Recorder [recorder_name] [symbol_to_train] [expected_#_inputs_from_sensor]\n" << endl;
cout << "Control keys (Recorder window must be active for these controls to work):\n"
"\tESC - quit the program\n"
"\tr - start, or stop a recording\n"
"\tn - get ready for the next item in the list found in the file called `gesture`\n"
"\ts - start/end annotating the recording with the current gesture pointed to by using the key `n`\n"
//"\ts - start stream again, in case the sensor was interrupted.\n"
<< endl;
cout << "Example:\n"
"\tunbuffer python SerialPort.py | ./Recorder Hassan 9 20"
<< endl;
}
int CreateDirectories(String folderName, String recorderName, String sessionName)//TODO: make it also suitable for windows
{
int returnValue = 0;
printf("%s\n", folderName.c_str());
int status = mkdir(folderName.c_str(), 0777);
if ( status == 0)
{
printf("New folder 'Videos' created in the following location:\n%s\n", folderName.c_str());
}
else if (errno == EEXIST)
{
//printf("");//Here we don't print anything, because it is going to be distrubing whenever we get notfied about the Output folder being already there.
}
else
{
printf("Couldn't create folder the 'Videos' folder\n");
returnValue = 1;
}
String directoryName = folderName + "/" + recorderName;
status = mkdir(directoryName.c_str(), 0777);
if ( status == 0)
{
printf("A new folder created for the recorder\n");
}
else if (errno == EEXIST)
{
//printf("");//Here we don't print anything, because it is going to be distrubing whenever we get notfied about the Output folder being already there.
}
else
{
printf("Couldn't create folder for the recorder\n");
returnValue = 1;
}
directoryName = directoryName + "/" + sessionName;
status = mkdir(directoryName.c_str(), 0777);
if ( status == 0)
{
printf("A new folder created for this session\n");
}
else if (errno == EEXIST)
{
//printf("");//Here we don't print anything, because it is going to be distrubing whenever we get notfied about the Output folder being already there.
}
else
{
printf("Couldn't create folder for the new session\n");
returnValue = 1;
}
printf("\n");
return returnValue;
}