-
Notifications
You must be signed in to change notification settings - Fork 35
/
biosemi2bdfplus.cpp
836 lines (672 loc) · 23.6 KB
/
biosemi2bdfplus.cpp
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
/*
***************************************************************************
*
* Author: Teunis van Beelen
*
* Copyright (C) 2010, 2011, 2012, 2013 Teunis van Beelen
*
*
***************************************************************************
*
* 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 version 2 of the License.
*
* 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
***************************************************************************
*
* This version of GPL is at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
*
***************************************************************************
*/
#include "biosemi2bdfplus.h"
UI_BIOSEMI2BDFPLUSwindow::UI_BIOSEMI2BDFPLUSwindow(QWidget *w_parent)
{
int i;
char str[128];
mainwindow = (UI_Mainwindow *)w_parent;
recent_opendir = mainwindow->recent_opendir;
recent_savedir = mainwindow->recent_savedir;
myobjectDialog = new QDialog;
myobjectDialog->setMinimumSize(600, 630);
myobjectDialog->setMaximumSize(600, 630);
myobjectDialog->setWindowTitle("Biosemi to BDF+ converter");
myobjectDialog->setModal(TRUE);
myobjectDialog->setAttribute(Qt::WA_DeleteOnClose, TRUE);
label2 = new QLabel(myobjectDialog);
label2->setGeometry(20, 20, 200, 26);
label2->setText("Trigger Input descriptions:");
label3 = new QLabel(myobjectDialog);
label3->setGeometry(290, 350, 200, 26);
label3->setText("Annotations will be generated at");
label4 = new QLabel(myobjectDialog);
label4->setGeometry(290, 440, 200, 26);
label4->setText("of trigger input.");
for(i=0; i<16; i++)
{
label1[i] = new QLabel(myobjectDialog);
label1[i]->setGeometry(20, 50 + (i * 30), 20, 26);
sprintf(str, "%i", i + 1);
label1[i]->setText(str);
lineEdit1[i] = new QLineEdit(myobjectDialog);
lineEdit1[i]->setGeometry(70, 50 + (i * 30), 120, 26);
sprintf(str, "Trigger Input %i", i + 1);
lineEdit1[i]->setText(str);
lineEdit1[i]->setMaxLength(16);
}
radioButton1 = new QRadioButton(myobjectDialog);
radioButton1->setGeometry(290, 385, 150, 20);
radioButton1->setText("rising edge");
radioButton1->setChecked(TRUE);
radioButton2 = new QRadioButton(myobjectDialog);
radioButton2->setGeometry(290, 415, 150, 20);
radioButton2->setText("falling edge");
checkBox1 = new QCheckBox(myobjectDialog);
checkBox1->setGeometry(290, 500, 200, 20);
checkBox1->setText("measure event duration");
checkBox1->setTristate(FALSE);
checkBox1->setChecked(FALSE);
selectButton = new QPushButton(myobjectDialog);
selectButton->setGeometry(20, 584, 100, 26);
selectButton->setText("Select File");
closeButton = new QPushButton(myobjectDialog);
closeButton->setGeometry(480, 584, 100, 26);
closeButton->setText("Close");
QObject::connect(selectButton, SIGNAL(clicked()), this, SLOT(SelectFileButton()));
QObject::connect(closeButton, SIGNAL(clicked()), myobjectDialog, SLOT(close()));
inputpath[0] = 0;
outputpath[0] = 0;
myobjectDialog->exec();
}
void UI_BIOSEMI2BDFPLUSwindow::SelectFileButton()
{
int i, j, k,
error,
hdl_in,
hdl_out,
edfsignals,
status_signal=0,
status_samples_in_datarecord=0,
rising_edge,
set_duration,
status[24],
totalSamplesInDatarecord,
*buf,
buf_offset[EDFLIB_MAXSIGNALS],
sf,
new_sf,
samplerate_divider;
char str[2048],
triggerlabel[24][64],
outputfilename[MAX_PATH_LENGTH];
long long datarecords,
status_sample_duration,
trigger_cnt,
progress_steps;
struct edf_hdr_struct hdr;
struct annotationblock *annotlist=NULL;
struct annotationblock *annotation;
for(i=0; i<16; i++)
{
if(!lineEdit1[i]->text().length())
{
sprintf(str, "Trigger Input label %i is empty!", i + 1);
QMessageBox messagewindow(QMessageBox::Critical, "Error", str);
messagewindow.exec();
return;
}
}
for(i=0; i<16; i++)
{
for(j=0; j<16; j++)
{
if(i != j)
{
if(!strcmp(lineEdit1[i]->text().toLocal8Bit().data(), lineEdit1[j]->text().toLocal8Bit().data()))
{
sprintf(str, "Trigger Input labels %i and %i are the same!", i + 1, j + 1);
QMessageBox messagewindow(QMessageBox::Critical, "Error", str);
messagewindow.exec();
return;
}
}
}
}
str[0] = 0;
strcpy(inputpath, QFileDialog::getOpenFileName(0, "Select inputfile", QString::fromLocal8Bit(recent_opendir), "BDF files (*.bdf *.BDF)").toLocal8Bit().data());
if(!strcmp(inputpath, ""))
{
return;
}
get_directory_from_path(recent_opendir, inputpath, MAX_PATH_LENGTH);
error = edfopen_file_readonly(inputpath, &hdr, EDFLIB_DO_NOT_READ_ANNOTATIONS);
if(error < 0)
{
error = hdr.filetype;
switch(error)
{
case EDFLIB_MALLOC_ERROR : strcpy(str, "EDFlib: malloc error.");
break;
case EDFLIB_NO_SUCH_FILE_OR_DIRECTORY : strcpy(str, "EDFlib: no such file or directory.");
break;
case EDFLIB_FILE_CONTAINS_FORMAT_ERRORS : strcpy(str, "EDFlib: file contains format errors.\nOpen the file in EDFbrowser to get more info.");
break;
case EDFLIB_MAXFILES_REACHED : strcpy(str, "EDFlib: maximum amount of files reached.");
break;
case EDFLIB_FILE_READ_ERROR : strcpy(str, "EDFlib: a file read error occurred.");
break;
case EDFLIB_FILE_ALREADY_OPENED : strcpy(str, "EDFlib: file is already opened.");
break;
case EDFLIB_FILETYPE_ERROR : strcpy(str, "EDFlib: filetype error.");
break;
case EDFLIB_FILE_WRITE_ERROR : strcpy(str, "EDFlib: file write error.");
break;
case EDFLIB_NUMBER_OF_SIGNALS_INVALID : strcpy(str, "EDFlib: invalid number of signals.");
break;
case EDFLIB_FILE_IS_DISCONTINUOUS : strcpy(str, "EDFlib: file is discontinuous.");
break;
case EDFLIB_INVALID_READ_ANNOTS_VALUE : strcpy(str, "EDFlib: invalid read annotations argument.");
break;
default : strcpy(str, "EDFlib: unknown error.");
}
QMessageBox messagewindow(QMessageBox::Critical, "Error", str);
messagewindow.exec();
return;
}
hdl_in = hdr.handle;
/////////////////// check file /////////////////////////////////////////////
if(hdr.filetype == EDFLIB_FILETYPE_BDFPLUS)
{
QMessageBox messagewindow(QMessageBox::Critical, "Error", "Selected file is already a BDF-plus file.");
messagewindow.exec();
edfclose_file(hdl_in);
return;
}
if(hdr.filetype != EDFLIB_FILETYPE_BDF)
{
QMessageBox messagewindow(QMessageBox::Critical, "Error", "Selected file is not a BDF file.");
messagewindow.exec();
edfclose_file(hdl_in);
return;
}
if(hdr.datarecord_duration != EDFLIB_TIME_DIMENSION)
{
QMessageBox messagewindow(QMessageBox::Critical, "Error", "Datarecord duration of inputfile must be 1 second.");
messagewindow.exec();
edfclose_file(hdl_in);
return;
}
edfsignals = hdr.edfsignals;
if(edfsignals < 1)
{
QMessageBox messagewindow(QMessageBox::Critical, "Error", "There are no signals in the selected file.");
messagewindow.exec();
edfclose_file(hdl_in);
return;
}
sf = hdr.signalparam[0].smp_in_datarecord;
for(i=1; i<edfsignals; i++)
{
if(hdr.signalparam[i].smp_in_datarecord != sf)
{
QMessageBox messagewindow(QMessageBox::Critical, "Error", "All signals must have the same samplefrequency.");
messagewindow.exec();
edfclose_file(hdl_in);
return;
}
}
error = 1;
switch(sf)
{
case 16384 : error = 0;
break;
case 8192 : error = 0;
break;
case 4096 : error = 0;
break;
case 2048 : error = 0;
break;
case 1024 : error = 0;
break;
case 512 : error = 0;
break;
case 256 : error = 0;
break;
case 128 : error = 0;
break;
case 64 : error = 0;
break;
case 32 : error = 0;
break;
}
if(error)
{
QMessageBox messagewindow(QMessageBox::Critical, "Error", "Samplefrequency must be 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64 or 32 Hz.");
messagewindow.exec();
edfclose_file(hdl_in);
return;
}
for(i=0; i<edfsignals; i++)
{
if(!(strcmp(hdr.signalparam[i].label, "Status ")))
{
status_signal = i;
break;
}
}
if(i == edfsignals)
{
QMessageBox messagewindow(QMessageBox::Critical, "Error", "There is no Status signal in the selected file.");
messagewindow.exec();
edfclose_file(hdl_in);
return;
}
totalSamplesInDatarecord = 0;
for(i=0; i<edfsignals; i++)
{
buf_offset[i] = totalSamplesInDatarecord;
if(i == status_signal)
{
status_samples_in_datarecord = hdr.signalparam[i].smp_in_datarecord;
}
totalSamplesInDatarecord += hdr.signalparam[i].smp_in_datarecord;
}
status_sample_duration = EDFLIB_TIME_DIMENSION / (long long)status_samples_in_datarecord;
for(i=0; i<16; i++)
{
strcpy(&triggerlabel[i][0], lineEdit1[i]->text().toUtf8().data());
triggerlabel[i][16] = 0;
}
strcpy(&triggerlabel[16][0], "new epoch");
if(radioButton1->isChecked() == TRUE)
{
rising_edge = 1;
for(i=0; i<16; i++)
{
status[i] = 1;
}
}
else
{
rising_edge = 0;
for(i=0; i<16; i++)
{
status[i] = 0;
}
}
if(checkBox1->isChecked() == TRUE)
{
set_duration = 1;
}
else
{
set_duration = 0;
}
for(i=16; i<24; i++)
{
status[i] = 1;
}
strcpy(outputfilename, inputpath);
remove_extension_from_filename(outputfilename);
strcat(outputfilename, "_+.bdf");
outputpath[0] = 0;
if(recent_savedir[0]!=0)
{
strcpy(outputpath, recent_savedir);
strcat(outputpath, "/");
}
strcat(outputpath, outputfilename);
strcpy(outputpath, QFileDialog::getSaveFileName(0, "Output file", QString::fromLocal8Bit(outputpath), "BDF files (*.bdf *.BDF)").toLocal8Bit().data());
if(!strcmp(outputpath, ""))
{
edfclose_file(hdl_in);
return;
}
get_directory_from_path(recent_savedir, outputpath, MAX_PATH_LENGTH);
if(mainwindow->file_is_opened(outputpath))
{
QMessageBox messagewindow(QMessageBox::Critical, "Error", "Outputfile is already opened in EDFbrowser.\nClose the file and try again.");
messagewindow.exec();
edfclose_file(hdl_in);
return;
}
if(!(strcmp(inputpath, outputpath)))
{
QMessageBox messagewindow(QMessageBox::Critical, "Error", "Inputfile and outputfile are the same.");
messagewindow.exec();
edfclose_file(hdl_in);
return;
}
hdl_out = edfopen_file_writeonly(outputpath, EDFLIB_FILETYPE_BDFPLUS, edfsignals);
if(hdl_out < 0)
{
switch(hdl_out)
{
case EDFLIB_MALLOC_ERROR : strcpy(str, "EDFlib: malloc error.");
break;
case EDFLIB_NO_SUCH_FILE_OR_DIRECTORY : strcpy(str, "EDFlib: no such file or directory.");
break;
case EDFLIB_MAXFILES_REACHED : strcpy(str, "EDFlib: maximum amount of files reached.");
break;
case EDFLIB_FILE_READ_ERROR : strcpy(str, "EDFlib: a file read error occurred.");
break;
case EDFLIB_FILE_ALREADY_OPENED : strcpy(str, "EDFlib: file is already opened.");
break;
case EDFLIB_FILETYPE_ERROR : strcpy(str, "EDFlib: filetype error.");
break;
case EDFLIB_FILE_WRITE_ERROR : strcpy(str, "EDFlib: file write error.");
break;
case EDFLIB_NUMBER_OF_SIGNALS_INVALID : strcpy(str, "EDFlib: invalid number of signals.");
break;
default : strcpy(str, "EDFlib: unknown error.");
}
QMessageBox messagewindow(QMessageBox::Critical, "Error", str);
messagewindow.exec();
edfclose_file(hdl_in);
return;
}
/////////////////// copy header /////////////////////////////////////////////
for(i=0; i<edfsignals; i++)
{
edf_set_samplefrequency(hdl_out, i, hdr.signalparam[i].smp_in_datarecord);
edf_set_physical_maximum(hdl_out, i, hdr.signalparam[i].phys_max);
edf_set_physical_minimum(hdl_out, i, hdr.signalparam[i].phys_min);
edf_set_digital_maximum(hdl_out, i, hdr.signalparam[i].dig_max);
edf_set_digital_minimum(hdl_out, i, hdr.signalparam[i].dig_min);
edf_set_label(hdl_out, i, hdr.signalparam[i].label);
edf_set_prefilter(hdl_out, i, hdr.signalparam[i].prefilter);
edf_set_transducer(hdl_out, i, hdr.signalparam[i].transducer);
edf_set_physical_dimension(hdl_out, i, hdr.signalparam[i].physdimension);
}
edf_set_startdatetime(hdl_out, hdr.startdate_year, hdr.startdate_month, hdr.startdate_day, hdr.starttime_hour, hdr.starttime_minute, hdr.starttime_second);
edf_set_patientname(hdl_out, hdr.patient);
edf_set_recording_additional(hdl_out, hdr.recording);
/////////////////// collect triggers /////////////////////////////////////////////
buf = (int *)malloc(sizeof(int) * status_samples_in_datarecord);
if(buf == NULL)
{
QMessageBox messagewindow(QMessageBox::Critical, "Error", "Malloc error (buf).");
messagewindow.exec();
edfclose_file(hdl_in);
edfclose_file(hdl_out);
return;
}
QProgressDialog progress("Collecting triggers...", "Abort", 0, (int)hdr.datarecords_in_file);
progress.setWindowModality(Qt::WindowModal);
progress.setMinimumDuration(200);
progress_steps = hdr.datarecords_in_file / 100LL;
if(progress_steps < 1LL)
{
progress_steps = 1LL;
}
trigger_cnt = 0;
for(datarecords = 0LL; datarecords < hdr.datarecords_in_file; datarecords++)
{
if(trigger_cnt >= ((hdr.datarecords_in_file * 32) - 2))
{
break;
}
if(trigger_cnt >= 100000)
{
break;
}
if(!(datarecords % progress_steps))
{
progress.setValue((int)datarecords);
qApp->processEvents();
if(progress.wasCanceled())
{
edfclose_file(hdl_in);
edfclose_file(hdl_out);
free(buf);
edfplus_annotation_delete_list(&annotlist);
return;
}
}
if(edfread_digital_samples(hdl_in, status_signal, status_samples_in_datarecord, buf) < 0)
{
progress.reset();
QMessageBox messagewindow(QMessageBox::Critical, "Error", "A read error occurred during the collection of triggers.");
messagewindow.exec();
edfclose_file(hdl_in);
edfclose_file(hdl_out);
free(buf);
edfplus_annotation_delete_list(&annotlist);
return;
}
for(i=0; i<status_samples_in_datarecord; i++)
{
for(j=0; j<17; j++)
{
if(((buf[i] & (1 << j)) && !status[j]) || (!(buf[i] & (1 << j)) && status[j])) // rising or falling edge detected
{
if(status[j]) // falling edge detected
{
if((!rising_edge) && (j < 16))
{
annotation = (struct annotationblock *)calloc(1, sizeof(struct annotationblock));
if(annotation == NULL)
{
progress.reset();
QMessageBox messagewindow(QMessageBox::Critical, "Error", "Malloc error (annotation).");
messagewindow.exec();
edfclose_file(hdl_in);
edfclose_file(hdl_out);
free(buf);
edfplus_annotation_delete_list(&annotlist);
return;
}
annotation->onset = (datarecords * EDFLIB_TIME_DIMENSION) + ((long long)i * status_sample_duration);
annotation->onset += hdr.starttime_subsecond;
strcpy(annotation->annotation, triggerlabel[j]);
edfplus_annotation_add_item(&annotlist, annotation);
trigger_cnt++;
}
else
{
if(set_duration)
{
k = edfplus_annotation_count(&annotlist);
for(; k>0; k--)
{
annotation = edfplus_annotation_item(&annotlist, k - 1);
if(annotation == NULL)
{
break;
}
if(!strcmp(annotation->annotation, triggerlabel[j]))
{
sprintf(str, "%.4f", (double)((datarecords * EDFLIB_TIME_DIMENSION) + ((long long)i * status_sample_duration) - annotation->onset) / (double)EDFLIB_TIME_DIMENSION);
str[15] = 0;
strcpy(annotation->duration, str);
break;
}
}
}
}
status[j] = 0;
}
else // rising edge detected
{
if(rising_edge || (j == 16))
{
annotation = (struct annotationblock *)calloc(1, sizeof(struct annotationblock));
if(annotation == NULL)
{
progress.reset();
QMessageBox messagewindow(QMessageBox::Critical, "Error", "Malloc error (annotation).");
messagewindow.exec();
edfclose_file(hdl_in);
edfclose_file(hdl_out);
free(buf);
edfplus_annotation_delete_list(&annotlist);
return;
}
annotation->onset = (datarecords * EDFLIB_TIME_DIMENSION) + ((long long)i * status_sample_duration);
annotation->onset += hdr.starttime_subsecond;
strcpy(annotation->annotation, triggerlabel[j]);
edfplus_annotation_add_item(&annotlist, annotation);
trigger_cnt++;
}
else
{
if(set_duration)
{
k = edfplus_annotation_count(&annotlist);
for(; k>0; k--)
{
annotation = edfplus_annotation_item(&annotlist, k - 1);
if(annotation == NULL)
{
break;
}
if(!strcmp(annotation->annotation, triggerlabel[j]))
{
sprintf(str, "%.4f", (double)((datarecords * EDFLIB_TIME_DIMENSION) + ((long long)i * status_sample_duration) - annotation->onset) / (double)EDFLIB_TIME_DIMENSION);
str[15] = 0;
strcpy(annotation->duration, str);
break;
}
}
}
}
status[j] = 1;
}
}
}
}
}
edfwrite_annotation_latin1(hdl_out, 0LL, -1LL, "Recording starts");
j = edfplus_annotation_count(&annotlist);
for(i=0; i<j; i++)
{
annotation = edfplus_annotation_item(&annotlist, i);
if(annotation->duration[0] == 0)
{
edfwrite_annotation_utf8(hdl_out, annotation->onset / 1000LL, -1LL, annotation->annotation);
}
else
{
edfwrite_annotation_utf8(hdl_out, annotation->onset / 1000LL, (long long)(atof(annotation->duration) * 10000.0), annotation->annotation);
}
}
free(buf);
edfwrite_annotation_latin1(hdl_out, hdr.datarecords_in_file * 10000LL, -1LL, "Recording ends");
/////////////////// choose datarecord duration /////////////////////////////////////////////
samplerate_divider = 1;
i = edfplus_annotation_count(&annotlist);
edfplus_annotation_delete_list(&annotlist);
annotlist = NULL;
if(i % 2)
{
i++;
}
i += 2;
while(i > hdr.datarecords_in_file)
{
samplerate_divider *= 2;
i /= 2;
if(samplerate_divider == 32)
{
break;
}
}
if(samplerate_divider > 1)
{
for(i=0; i<edfsignals; i++)
{
edf_set_samplefrequency(hdl_out, i, hdr.signalparam[i].smp_in_datarecord / samplerate_divider);
}
if(edf_set_datarecord_duration(hdl_out, 100000 / samplerate_divider) == -1)
{
QMessageBox messagewindow(QMessageBox::Critical, "Error", "edf_set_datarecord_duration() returned an error.");
messagewindow.exec();
edfclose_file(hdl_in);
edfclose_file(hdl_out);
return;
}
}
new_sf = sf / samplerate_divider;
/////////////////// start conversion /////////////////////////////////////////////
buf = (int *)malloc(sizeof(int) * totalSamplesInDatarecord);
if(buf == NULL)
{
QMessageBox messagewindow(QMessageBox::Critical, "Error", "Malloc error (buf).");
messagewindow.exec();
edfclose_file(hdl_in);
edfclose_file(hdl_out);
return;
}
edfrewind(hdl_in, status_signal);
progress.setLabelText("Converting...");
progress.setValue(0);
for(datarecords = 0LL; datarecords < hdr.datarecords_in_file; datarecords++)
{
if(!(datarecords % progress_steps))
{
progress.setValue((int)datarecords);
qApp->processEvents();
if(progress.wasCanceled())
{
edfclose_file(hdl_in);
edfclose_file(hdl_out);
free(buf);
return;
}
}
for(i=0; i<edfsignals; i++)
{
if(edfread_digital_samples(hdl_in, i, hdr.signalparam[i].smp_in_datarecord, buf + buf_offset[i]) < 0)
{
progress.reset();
QMessageBox messagewindow(QMessageBox::Critical, "Error", "A read error occurred during the conversion.");
messagewindow.exec();
edfclose_file(hdl_in);
edfclose_file(hdl_out);
free(buf);
return;
}
}
for(j=0; j<samplerate_divider; j++)
{
for(i=0; i<edfsignals; i++)
{
if(edfwrite_digital_samples(hdl_out, buf + buf_offset[i] + (j * new_sf)) < 0)
{
progress.reset();
QMessageBox messagewindow(QMessageBox::Critical, "Error", "A write error occurred during the conversion.");
messagewindow.exec();
edfclose_file(hdl_in);
edfclose_file(hdl_out);
free(buf);
return;
}
}
}
}
QApplication::setOverrideCursor(Qt::WaitCursor);
edfclose_file(hdl_in);
edfclose_file(hdl_out);
free(buf);
QApplication::restoreOverrideCursor();
progress.reset();
#ifdef Q_WS_WIN
snprintf(str, 2048, "Done. Converted %I64d input trigger events to BDF+ annotations.\n"
"\nBDF+ file is located at %s", trigger_cnt, outputpath);
#else
snprintf(str, 2048, "Done. Converted %lli input trigger events to BDF+ annotations.\n"
"\nBDF+ file is located at %s", trigger_cnt, outputpath);
#endif
QMessageBox messagewindow(QMessageBox::Information, "Ready", str);
messagewindow.setIconPixmap(QPixmap(":/images/ok.png"));
messagewindow.exec();
}