EDFlib
Loading...
Searching...
No Matches
edflib.h
Go to the documentation of this file.
1/*
2*****************************************************************************
3*
4* Copyright (c) 2009 - 2023 Teunis van Beelen
5* All rights reserved.
6*
7* Email: teuniz@protonmail.com
8*
9* Redistribution and use in source and binary forms, with or without
10* modification, are permitted provided that the following conditions are met:
11* * Redistributions of source code must retain the above copyright
12* notice, this list of conditions and the following disclaimer.
13* * Redistributions in binary form must reproduce the above copyright
14* notice, this list of conditions and the following disclaimer in the
15* documentation and/or other materials provided with the distribution.
16* * Neither the name of the copyright holder nor the names of its
17* contributors may be used to endorse or promote products derived from
18* this software without specific prior written permission.
19*
20* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS'' AND ANY
21* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
24* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30*
31*****************************************************************************
32*/
33
111/* compile with options "-D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE" */
112
113
114#ifndef EDFLIB_INCLUDED
115#define EDFLIB_INCLUDED
116
117#include <stdio.h>
118#include <stdlib.h>
119#include <string.h>
120#include <time.h>
121
122
123/*
124 * If both EDFLIB_SO_DLL and EDFLIB_BUILD are defined: compile only EDFlib as a shared library (so, dll).
125 * When compiling on unix-like systems, add the -fvisibility=hidden to hide all symbols by
126 * default so that this macro can reveal them.
127 *
128 * If only EDFLIB_SO_DLL is defined: link with EDFlib as an external library (so, dll).
129 * EDFlib must be installed on your system (as an .so or .dll) when running your program.
130 *
131 * If both EDFLIB_SO_DLL and EDFLIB_BUILD are not defined: EDFlib will not be used as a shared library,
132 * it will be an integral part of your program instead.
133 *
134 */
135
136/*
137#define EDFLIB_SO_DLL
138#define EDFLIB_BUILD
139*/
140
141#if defined(EDFLIB_SO_DLL)
142# if defined(EDFLIB_BUILD)
143# if defined(_WIN32)
144# define EDFLIB_API __declspec(dllexport)
145# elif defined(__ELF__)
146# define EDFLIB_API __attribute__ ((visibility ("default")))
147# else
148# define EDFLIB_API
149# endif
150# else
151# if defined(_WIN32)
152# define EDFLIB_API __declspec(dllimport)
153# else
154# define EDFLIB_API
155# endif
156# endif
157#else
158# define EDFLIB_API
159#endif
160
161
162
163#define EDFLIB_TIME_DIMENSION (10000000LL)
164#define EDFLIB_MAXSIGNALS (640)
165#define EDFLIB_MAX_ANNOTATION_LEN (512)
166
167#define EDFSEEK_SET (0)
168#define EDFSEEK_CUR (1)
169#define EDFSEEK_END (2)
170
171/* the following defines are used in the member "filetype" of the edf_hdr_struct
172 and as return value for the function edfopen_file_readonly() */
173#define EDFLIB_FILETYPE_EDF (0)
174#define EDFLIB_FILETYPE_EDFPLUS (1)
175#define EDFLIB_FILETYPE_BDF (2)
176#define EDFLIB_FILETYPE_BDFPLUS (3)
177#define EDFLIB_MALLOC_ERROR (-1)
178#define EDFLIB_NO_SUCH_FILE_OR_DIRECTORY (-2)
179
180/* when this error occurs, try to open the file with EDFbrowser (https://www.teuniz.net/edfbrowser/),
181 it will give you full details about the cause of the error. It can also fix most errors. */
182#define EDFLIB_FILE_CONTAINS_FORMAT_ERRORS (-3)
183
184#define EDFLIB_MAXFILES_REACHED (-4)
185#define EDFLIB_FILE_READ_ERROR (-5)
186#define EDFLIB_FILE_ALREADY_OPENED (-6)
187#define EDFLIB_FILETYPE_ERROR (-7)
188#define EDFLIB_FILE_WRITE_ERROR (-8)
189#define EDFLIB_NUMBER_OF_SIGNALS_INVALID (-9)
190#define EDFLIB_FILE_IS_DISCONTINUOUS (-10)
191#define EDFLIB_INVALID_READ_ANNOTS_VALUE (-11)
192#define EDFLIB_ARCH_ERROR (-12)
193
194/* values for annotations */
195#define EDFLIB_DO_NOT_READ_ANNOTATIONS (0)
196#define EDFLIB_READ_ANNOTATIONS (1)
197#define EDFLIB_READ_ALL_ANNOTATIONS (2)
198
199/* the following defines are possible errors returned by the first sample write action */
200#define EDFLIB_NO_SIGNALS (-20)
201#define EDFLIB_TOO_MANY_SIGNALS (-21)
202#define EDFLIB_NO_SAMPLES_IN_RECORD (-22)
203#define EDFLIB_DIGMIN_IS_DIGMAX (-23)
204#define EDFLIB_DIGMAX_LOWER_THAN_DIGMIN (-24)
205#define EDFLIB_PHYSMIN_IS_PHYSMAX (-25)
206#define EDFLIB_DATARECORD_SIZE_TOO_BIG (-26)
207
208#ifdef __cplusplus
209extern "C" {
210#endif
211
215typedef struct edf_param_struct
216{
217 char label[17];
218 long long smp_in_file;
219 double phys_max;
220 double phys_min;
225 char prefilter[81];
226 char transducer[81];
228
233{
234 long long onset;
235 long long duration_l;
236 char duration[20];
237 char annotation[EDFLIB_MAX_ANNOTATION_LEN + 1];
239
243typedef struct edf_hdr_struct
244{
245 int handle;
248 long long file_duration;
256 char patient[81];
257 char recording[81];
258 char patientcode[81];
259 char sex[16];
260#if defined(__GNUC__)
261 char gender[16] __attribute__ ((deprecated ("use sex")));
262#else
263 char gender[16];
264#endif
265 char birthdate[16];
269 char patient_name[81];
271 char admincode[81];
272 char technician[81];
273 char equipment[81];
278 edflib_param_t signalparam[EDFLIB_MAXSIGNALS];
280
281/***************** the following functions are used to read files **************************/
282
307EDFLIB_API int edfopen_file_readonly(const char *path, edflib_hdr_t *edfhdr, int read_annotations);
308
325EDFLIB_API int edfread_physical_samples(int handle, int edfsignal, int n, double *buf);
326
343EDFLIB_API int edfread_digital_samples(int handle, int edfsignal, int n, int *buf);
344
367EDFLIB_API long long edfseek(int handle, int edfsignal, long long offset, int whence);
368
381EDFLIB_API long long edftell(int handle, int edfsignal);
382
396EDFLIB_API int edfrewind(int handle, int edfsignal);
397
413EDFLIB_API int edf_get_annotation(int handle, int n, edflib_annotation_t *annot);
414
415/***************** the following functions are used in read and write mode **************************/
416
430EDFLIB_API int edfclose_file(int handle);
431
438EDFLIB_API int edflib_version(void);
439
449EDFLIB_API int edflib_is_file_used(const char *path);
450
458
468EDFLIB_API int edflib_get_handle(int file_number);
469
470/***************** the following functions are used to write files **************************/
471
495EDFLIB_API int edfopen_file_writeonly(const char *path, int filetype, int number_of_signals);
496
531EDFLIB_API int edfopen_file_writeonly_with_params(const char *path, int filetype, int number_of_signals, int samplefrequency, double phys_max_min, const char *phys_dim);
532
552EDFLIB_API int edf_set_samplefrequency(int handle, int edfsignal, int samplefrequency);
553
574EDFLIB_API int edf_set_physical_maximum(int handle, int edfsignal, double phys_max);
575
596EDFLIB_API int edf_set_physical_minimum(int handle, int edfsignal, double phys_min);
597
618EDFLIB_API int edf_set_digital_maximum(int handle, int edfsignal, int dig_max);
619
640EDFLIB_API int edf_set_digital_minimum(int handle, int edfsignal, int dig_min);
641
659EDFLIB_API int edf_set_label(int handle, int edfsignal, const char *label);
660
678EDFLIB_API int edf_set_prefilter(int handle, int edfsignal, const char *prefilter);
679
697EDFLIB_API int edf_set_transducer(int handle, int edfsignal, const char *transducer);
698
716EDFLIB_API int edf_set_physical_dimension(int handle, int edfsignal, const char *phys_dim);
717
749EDFLIB_API int edf_set_startdatetime(int handle, int startdate_year, int startdate_month, int startdate_day,
750 int starttime_hour, int starttime_minute, int starttime_second);
751
766EDFLIB_API int edf_set_patientname(int handle, const char *patientname);
767
782EDFLIB_API int edf_set_patientcode(int handle, const char *patientcode);
783
798EDFLIB_API int edf_set_sex(int handle, int sex);
799
800#if defined(__GNUC__)
801EDFLIB_API int edf_set_gender(int handle, int sex) __attribute__ ((deprecated ("use edf_set_sex()")));
802#else
803EDFLIB_API int edf_set_gender(int handle, int sex);
804#endif
805/* DEPRECATED!! USE edf_set_sex()
806 * Sets the sex. 1 is male, 0 is female.
807 * Returns 0 on success, otherwise -1
808 * This function is optional and can be called only after opening a file in writemode
809 * and before the first sample write action
810 */
811
832EDFLIB_API int edf_set_birthdate(int handle, int birthdate_year, int birthdate_month, int birthdate_day);
833
848EDFLIB_API int edf_set_patient_additional(int handle, const char *patient_additional);
849
864EDFLIB_API int edf_set_admincode(int handle, const char *admincode);
865
880EDFLIB_API int edf_set_technician(int handle, const char *technician);
881
897EDFLIB_API int edf_set_equipment(int handle, const char *equipment);
898
913EDFLIB_API int edf_set_recording_additional(int handle, const char *recording_additional);
914
936EDFLIB_API int edfwrite_physical_samples(int handle, double *buf);
937
957EDFLIB_API int edf_blockwrite_physical_samples(int handle, double *buf);
958
978EDFLIB_API int edfwrite_digital_short_samples(int handle, short *buf);
979
1001EDFLIB_API int edfwrite_digital_samples(int handle, int *buf);
1002
1023EDFLIB_API int edf_blockwrite_digital_3byte_samples(int handle, void *buf);
1024
1045EDFLIB_API int edf_blockwrite_digital_short_samples(int handle, short *buf);
1046
1066EDFLIB_API int edf_blockwrite_digital_samples(int handle, int *buf);
1067
1088EDFLIB_API int edfwrite_annotation_utf8_hr(int handle, long long onset, long long duration, const char *description);
1089
1090#if defined(__GNUC__)
1091EDFLIB_API int edfwrite_annotation_utf8(int handle, long long onset, long long duration, const char *description) __attribute__ ((deprecated ("use edfwrite_annotation_utf8_hr()")));
1092#else
1093EDFLIB_API int edfwrite_annotation_utf8(int handle, long long onset, long long duration, const char *description);
1094#endif
1095/* DEPRECATED!! USE edfwrite_annotation_utf8_hr()
1096 * writes an annotation/event to the file
1097 * onset is relative to the start of the file
1098 * onset and duration are in units of 100 microseconds! resolution is 0.0001 second!
1099 * for example: 34.071 seconds must be written as 340710
1100 * if duration is unknown or not applicable: set a negative number (-1)
1101 * description is a null-terminated UTF8-string containing the text that describes the event
1102 * This function is optional and can be called only after opening a file in writemode
1103 * and before closing the file
1104 */
1105
1126EDFLIB_API int edfwrite_annotation_latin1_hr(int handle, long long onset, long long duration, const char *description);
1127
1128#if defined(__GNUC__)
1129EDFLIB_API int edfwrite_annotation_latin1(int handle, long long onset, long long duration, const char *description) __attribute__ ((deprecated ("use edfwrite_annotation_latin1_hr()")));
1130#else
1131EDFLIB_API int edfwrite_annotation_latin1(int handle, long long onset, long long duration, const char *description);
1132#endif
1133/* DEPRECATED!! USE edfwrite_annotation_latin1_hr()
1134 * writes an annotation/event to the file
1135 * onset is relative to the start of the file
1136 * onset and duration are in units of 100 microseconds! resolution is 0.0001 second!
1137 * for example: 34.071 seconds must be written as 340710
1138 * if duration is unknown or not applicable: set a negative number (-1)
1139 * description is a null-terminated Latin1-string containing the text that describes the event
1140 * This function is optional and can be called only after opening a file in writemode
1141 * and before closing the file
1142 */
1143
1165EDFLIB_API int edf_set_datarecord_duration(int handle, int duration);
1166
1188EDFLIB_API int edf_set_micro_datarecord_duration(int handle, int duration);
1189
1207EDFLIB_API int edf_set_number_of_annotation_signals(int handle, int annot_signals);
1208
1227EDFLIB_API int edf_set_subsecond_starttime(int handle, int subsecond);
1228
1229#ifdef __cplusplus
1230} /* extern "C" */
1231#endif
1232
1233#endif
1234
1235
1236
1237
1238
1239
struct edf_hdr_struct edflib_hdr_t
EDFLIB_API int edf_set_samplefrequency(int handle, int edfsignal, int samplefrequency)
EDFLIB_API int edfread_physical_samples(int handle, int edfsignal, int n, double *buf)
EDFLIB_API int edf_set_birthdate(int handle, int birthdate_year, int birthdate_month, int birthdate_day)
EDFLIB_API int edfwrite_annotation_utf8_hr(int handle, long long onset, long long duration, const char *description)
EDFLIB_API int edf_set_physical_dimension(int handle, int edfsignal, const char *phys_dim)
EDFLIB_API int edflib_get_handle(int file_number)
EDFLIB_API int edf_set_equipment(int handle, const char *equipment)
EDFLIB_API int edf_get_annotation(int handle, int n, edflib_annotation_t *annot)
EDFLIB_API int edfopen_file_writeonly_with_params(const char *path, int filetype, int number_of_signals, int samplefrequency, double phys_max_min, const char *phys_dim)
EDFLIB_API long long edftell(int handle, int edfsignal)
EDFLIB_API int edf_blockwrite_digital_samples(int handle, int *buf)
EDFLIB_API int edf_set_sex(int handle, int sex)
EDFLIB_API int edfread_digital_samples(int handle, int edfsignal, int n, int *buf)
EDFLIB_API int edf_set_subsecond_starttime(int handle, int subsecond)
EDFLIB_API int edf_set_physical_minimum(int handle, int edfsignal, double phys_min)
EDFLIB_API int edf_set_number_of_annotation_signals(int handle, int annot_signals)
EDFLIB_API int edf_set_digital_maximum(int handle, int edfsignal, int dig_max)
EDFLIB_API int edf_set_micro_datarecord_duration(int handle, int duration)
EDFLIB_API int edfclose_file(int handle)
EDFLIB_API int edflib_is_file_used(const char *path)
EDFLIB_API int edflib_get_number_of_open_files(void)
EDFLIB_API int edfwrite_annotation_latin1_hr(int handle, long long onset, long long duration, const char *description)
EDFLIB_API int edfopen_file_readonly(const char *path, edflib_hdr_t *edfhdr, int read_annotations)
EDFLIB_API int edf_blockwrite_digital_3byte_samples(int handle, void *buf)
EDFLIB_API int edfwrite_physical_samples(int handle, double *buf)
EDFLIB_API int edf_set_patient_additional(int handle, const char *patient_additional)
EDFLIB_API int edfrewind(int handle, int edfsignal)
EDFLIB_API int edfopen_file_writeonly(const char *path, int filetype, int number_of_signals)
EDFLIB_API int edf_set_physical_maximum(int handle, int edfsignal, double phys_max)
EDFLIB_API int edf_set_admincode(int handle, const char *admincode)
EDFLIB_API int edf_set_datarecord_duration(int handle, int duration)
EDFLIB_API int edf_set_technician(int handle, const char *technician)
struct edf_annotation_struct edflib_annotation_t
EDFLIB_API int edf_set_recording_additional(int handle, const char *recording_additional)
struct edf_param_struct edflib_param_t
EDFLIB_API int edfwrite_digital_short_samples(int handle, short *buf)
EDFLIB_API long long edfseek(int handle, int edfsignal, long long offset, int whence)
EDFLIB_API int edf_blockwrite_digital_short_samples(int handle, short *buf)
EDFLIB_API int edflib_version(void)
EDFLIB_API int edfwrite_digital_samples(int handle, int *buf)
EDFLIB_API int edf_set_patientcode(int handle, const char *patientcode)
EDFLIB_API int edf_set_startdatetime(int handle, int startdate_year, int startdate_month, int startdate_day, int starttime_hour, int starttime_minute, int starttime_second)
EDFLIB_API int edf_set_transducer(int handle, int edfsignal, const char *transducer)
EDFLIB_API int edf_blockwrite_physical_samples(int handle, double *buf)
EDFLIB_API int edf_set_digital_minimum(int handle, int edfsignal, int dig_min)
EDFLIB_API int edf_set_patientname(int handle, const char *patientname)
EDFLIB_API int edf_set_prefilter(int handle, int edfsignal, const char *prefilter)
EDFLIB_API int edf_set_label(int handle, int edfsignal, const char *label)
Definition edflib.h:233
char duration[20]
Definition edflib.h:236
long long duration_l
Definition edflib.h:235
char annotation[EDFLIB_MAX_ANNOTATION_LEN+1]
Definition edflib.h:237
long long onset
Definition edflib.h:234
Definition edflib.h:244
char patient_additional[81]
Definition edflib.h:270
int starttime_second
Definition edflib.h:253
long long datarecords_in_file
Definition edflib.h:276
long long file_duration
Definition edflib.h:248
char admincode[81]
Definition edflib.h:271
char technician[81]
Definition edflib.h:272
char gender[16]
Definition edflib.h:263
int startdate_month
Definition edflib.h:250
char patientcode[81]
Definition edflib.h:258
int starttime_minute
Definition edflib.h:254
int edfsignals
Definition edflib.h:247
long long starttime_subsecond
Definition edflib.h:252
char recording[81]
Definition edflib.h:257
char sex[16]
Definition edflib.h:259
int birthdate_month
Definition edflib.h:267
int handle
Definition edflib.h:245
int starttime_hour
Definition edflib.h:255
long long annotations_in_file
Definition edflib.h:277
char recording_additional[81]
Definition edflib.h:274
long long datarecord_duration
Definition edflib.h:275
int startdate_day
Definition edflib.h:249
char birthdate[16]
Definition edflib.h:265
int birthdate_year
Definition edflib.h:268
char patient_name[81]
Definition edflib.h:269
int filetype
Definition edflib.h:246
char equipment[81]
Definition edflib.h:273
edflib_param_t signalparam[EDFLIB_MAXSIGNALS]
Definition edflib.h:278
char patient[81]
Definition edflib.h:256
int startdate_year
Definition edflib.h:251
int birthdate_day
Definition edflib.h:266
Definition edflib.h:216
int smp_in_datarecord
Definition edflib.h:223
double phys_min
Definition edflib.h:220
char physdimension[9]
Definition edflib.h:224
int dig_min
Definition edflib.h:222
int dig_max
Definition edflib.h:221
char label[17]
Definition edflib.h:217
char prefilter[81]
Definition edflib.h:225
char transducer[81]
Definition edflib.h:226
double phys_max
Definition edflib.h:219
long long smp_in_file
Definition edflib.h:218