EDFlib
Loading...
Searching...
No Matches
edflib.h
Go to the documentation of this file.
1/*
2*****************************************************************************
3*
4* Copyright (c) 2009 - 2024 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#define EDF_ANNOT_IDX_POS_END (0)
172#define EDF_ANNOT_IDX_POS_MIDDLE (1)
173#define EDF_ANNOT_IDX_POS_START (2)
174
175/* the following defines are used in the member "filetype" of the edf_hdr_struct
176 and as return value for the function edfopen_file_readonly() */
177#define EDFLIB_FILETYPE_EDF (0)
178#define EDFLIB_FILETYPE_EDFPLUS (1)
179#define EDFLIB_FILETYPE_BDF (2)
180#define EDFLIB_FILETYPE_BDFPLUS (3)
181#define EDFLIB_MALLOC_ERROR (-1)
182#define EDFLIB_NO_SUCH_FILE_OR_DIRECTORY (-2)
183
184/* when this error occurs, try to open the file with EDFbrowser (https://www.teuniz.net/edfbrowser/),
185 it will give you full details about the cause of the error. It can also fix most errors. */
186#define EDFLIB_FILE_CONTAINS_FORMAT_ERRORS (-3)
187
188#define EDFLIB_MAXFILES_REACHED (-4)
189#define EDFLIB_FILE_READ_ERROR (-5)
190#define EDFLIB_FILE_ALREADY_OPENED (-6)
191#define EDFLIB_FILETYPE_ERROR (-7)
192#define EDFLIB_FILE_WRITE_ERROR (-8)
193#define EDFLIB_NUMBER_OF_SIGNALS_INVALID (-9)
194#define EDFLIB_FILE_IS_DISCONTINUOUS (-10)
195#define EDFLIB_INVALID_READ_ANNOTS_VALUE (-11)
196#define EDFLIB_ARCH_ERROR (-12)
197
198/* values for annotations */
199#define EDFLIB_DO_NOT_READ_ANNOTATIONS (0)
200#define EDFLIB_READ_ANNOTATIONS (1)
201#define EDFLIB_READ_ALL_ANNOTATIONS (2)
202
203/* the following defines are possible errors returned by the first sample write action */
204#define EDFLIB_NO_SIGNALS (-20)
205#define EDFLIB_TOO_MANY_SIGNALS (-21)
206#define EDFLIB_NO_SAMPLES_IN_RECORD (-22)
207#define EDFLIB_DIGMIN_IS_DIGMAX (-23)
208#define EDFLIB_DIGMAX_LOWER_THAN_DIGMIN (-24)
209#define EDFLIB_PHYSMIN_IS_PHYSMAX (-25)
210#define EDFLIB_DATARECORD_SIZE_TOO_BIG (-26)
211
212#ifdef __cplusplus
213extern "C" {
214#endif
215
219typedef struct edf_param_struct
220{
221 char label[17];
222 long long smp_in_file;
223 double phys_max;
224 double phys_min;
229 char prefilter[81];
230 char transducer[81];
232
237{
238 long long onset;
239 long long duration_l;
240 char duration[20];
241 char annotation[EDFLIB_MAX_ANNOTATION_LEN + 1];
243
247typedef struct edf_hdr_struct
248{
249 int handle;
252 long long file_duration;
260 char patient[81];
261 char recording[81];
262 char patientcode[81];
263 char sex[16];
264#if defined(__GNUC__)
265 char gender[16] __attribute__ ((deprecated ("use sex")));
266#else
267 char gender[16];
268#endif
269 char birthdate[16];
273 char patient_name[81];
275 char admincode[81];
276 char technician[81];
277 char equipment[81];
282 edflib_param_t signalparam[EDFLIB_MAXSIGNALS];
284
285/***************** the following functions are used to read files **************************/
286
311EDFLIB_API int edfopen_file_readonly(const char *path, edflib_hdr_t *edfhdr, int read_annotations);
312
329EDFLIB_API int edfread_physical_samples(int handle, int edfsignal, int n, double *buf);
330
347EDFLIB_API int edfread_digital_samples(int handle, int edfsignal, int n, int *buf);
348
371EDFLIB_API long long edfseek(int handle, int edfsignal, long long offset, int whence);
372
385EDFLIB_API long long edftell(int handle, int edfsignal);
386
400EDFLIB_API int edfrewind(int handle, int edfsignal);
401
417EDFLIB_API int edf_get_annotation(int handle, int n, edflib_annotation_t *annot);
418
419/***************** the following functions are used in read and write mode **************************/
420
434EDFLIB_API int edfclose_file(int handle);
435
442EDFLIB_API int edflib_version(void);
443
453EDFLIB_API int edflib_is_file_used(const char *path);
454
462
472EDFLIB_API int edflib_get_handle(int file_number);
473
474/***************** the following functions are used to write files **************************/
475
499EDFLIB_API int edfopen_file_writeonly(const char *path, int filetype, int number_of_signals);
500
535EDFLIB_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);
536
556EDFLIB_API int edf_set_samplefrequency(int handle, int edfsignal, int samplefrequency);
557
578EDFLIB_API int edf_set_physical_maximum(int handle, int edfsignal, double phys_max);
579
600EDFLIB_API int edf_set_physical_minimum(int handle, int edfsignal, double phys_min);
601
622EDFLIB_API int edf_set_digital_maximum(int handle, int edfsignal, int dig_max);
623
644EDFLIB_API int edf_set_digital_minimum(int handle, int edfsignal, int dig_min);
645
663EDFLIB_API int edf_set_label(int handle, int edfsignal, const char *label);
664
682EDFLIB_API int edf_set_prefilter(int handle, int edfsignal, const char *prefilter);
683
701EDFLIB_API int edf_set_transducer(int handle, int edfsignal, const char *transducer);
702
720EDFLIB_API int edf_set_physical_dimension(int handle, int edfsignal, const char *phys_dim);
721
753EDFLIB_API int edf_set_startdatetime(int handle, int startdate_year, int startdate_month, int startdate_day,
754 int starttime_hour, int starttime_minute, int starttime_second);
755
770EDFLIB_API int edf_set_patientname(int handle, const char *patientname);
771
786EDFLIB_API int edf_set_patientcode(int handle, const char *patientcode);
787
802EDFLIB_API int edf_set_sex(int handle, int sex);
803
804#if defined(__GNUC__)
805EDFLIB_API int edf_set_gender(int handle, int sex) __attribute__ ((deprecated ("use edf_set_sex()")));
806#else
807EDFLIB_API int edf_set_gender(int handle, int sex);
808#endif
809/* DEPRECATED!! USE edf_set_sex()
810 * Sets the sex. 1 is male, 0 is female.
811 * Returns 0 on success, otherwise -1
812 * This function is optional and can be called only after opening a file in writemode
813 * and before the first sample write action
814 */
815
836EDFLIB_API int edf_set_birthdate(int handle, int birthdate_year, int birthdate_month, int birthdate_day);
837
852EDFLIB_API int edf_set_patient_additional(int handle, const char *patient_additional);
853
868EDFLIB_API int edf_set_admincode(int handle, const char *admincode);
869
884EDFLIB_API int edf_set_technician(int handle, const char *technician);
885
901EDFLIB_API int edf_set_equipment(int handle, const char *equipment);
902
917EDFLIB_API int edf_set_recording_additional(int handle, const char *recording_additional);
918
940EDFLIB_API int edfwrite_physical_samples(int handle, double *buf);
941
961EDFLIB_API int edf_blockwrite_physical_samples(int handle, double *buf);
962
982EDFLIB_API int edfwrite_digital_short_samples(int handle, short *buf);
983
1005EDFLIB_API int edfwrite_digital_samples(int handle, int *buf);
1006
1027EDFLIB_API int edf_blockwrite_digital_3byte_samples(int handle, void *buf);
1028
1049EDFLIB_API int edf_blockwrite_digital_short_samples(int handle, short *buf);
1050
1070EDFLIB_API int edf_blockwrite_digital_samples(int handle, int *buf);
1071
1092EDFLIB_API int edfwrite_annotation_utf8_hr(int handle, long long onset, long long duration, const char *description);
1093
1094#if defined(__GNUC__)
1095EDFLIB_API int edfwrite_annotation_utf8(int handle, long long onset, long long duration, const char *description) __attribute__ ((deprecated ("use edfwrite_annotation_utf8_hr()")));
1096#else
1097EDFLIB_API int edfwrite_annotation_utf8(int handle, long long onset, long long duration, const char *description);
1098#endif
1099/* DEPRECATED!! USE edfwrite_annotation_utf8_hr()
1100 * writes an annotation/event to the file
1101 * onset is relative to the start of the file
1102 * onset and duration are in units of 100 microseconds! resolution is 0.0001 second!
1103 * for example: 34.071 seconds must be written as 340710
1104 * if duration is unknown or not applicable: set a negative number (-1)
1105 * description is a null-terminated UTF8-string containing the text that describes the event
1106 * This function is optional and can be called only after opening a file in writemode
1107 * and before closing the file
1108 */
1109
1130EDFLIB_API int edfwrite_annotation_latin1_hr(int handle, long long onset, long long duration, const char *description);
1131
1132#if defined(__GNUC__)
1133EDFLIB_API int edfwrite_annotation_latin1(int handle, long long onset, long long duration, const char *description) __attribute__ ((deprecated ("use edfwrite_annotation_latin1_hr()")));
1134#else
1135EDFLIB_API int edfwrite_annotation_latin1(int handle, long long onset, long long duration, const char *description);
1136#endif
1137/* DEPRECATED!! USE edfwrite_annotation_latin1_hr()
1138 * writes an annotation/event to the file
1139 * onset is relative to the start of the file
1140 * onset and duration are in units of 100 microseconds! resolution is 0.0001 second!
1141 * for example: 34.071 seconds must be written as 340710
1142 * if duration is unknown or not applicable: set a negative number (-1)
1143 * description is a null-terminated Latin1-string containing the text that describes the event
1144 * This function is optional and can be called only after opening a file in writemode
1145 * and before closing the file
1146 */
1147
1169EDFLIB_API int edf_set_datarecord_duration(int handle, int duration);
1170
1192EDFLIB_API int edf_set_micro_datarecord_duration(int handle, int duration);
1193
1211EDFLIB_API int edf_set_number_of_annotation_signals(int handle, int annot_signals);
1212
1231EDFLIB_API int edf_set_subsecond_starttime(int handle, int subsecond);
1232
1251EDFLIB_API int edf_set_annot_chan_idx_pos(int handle, int pos);
1252
1253#ifdef __cplusplus
1254} /* extern "C" */
1255#endif
1256
1257#endif
1258
1259
1260
1261
1262
1263
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_annot_chan_idx_pos(int handle, int pos)
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:237
char duration[20]
Definition: edflib.h:240
long long duration_l
Definition: edflib.h:239
char annotation[EDFLIB_MAX_ANNOTATION_LEN+1]
Definition: edflib.h:241
long long onset
Definition: edflib.h:238
Definition: edflib.h:248
char patient_additional[81]
Definition: edflib.h:274
int starttime_second
Definition: edflib.h:257
long long datarecords_in_file
Definition: edflib.h:280
long long file_duration
Definition: edflib.h:252
char admincode[81]
Definition: edflib.h:275
char technician[81]
Definition: edflib.h:276
char gender[16]
Definition: edflib.h:267
int startdate_month
Definition: edflib.h:254
char patientcode[81]
Definition: edflib.h:262
int starttime_minute
Definition: edflib.h:258
int edfsignals
Definition: edflib.h:251
long long starttime_subsecond
Definition: edflib.h:256
char recording[81]
Definition: edflib.h:261
char sex[16]
Definition: edflib.h:263
int birthdate_month
Definition: edflib.h:271
int handle
Definition: edflib.h:249
int starttime_hour
Definition: edflib.h:259
long long annotations_in_file
Definition: edflib.h:281
char recording_additional[81]
Definition: edflib.h:278
long long datarecord_duration
Definition: edflib.h:279
int startdate_day
Definition: edflib.h:253
char birthdate[16]
Definition: edflib.h:269
int birthdate_year
Definition: edflib.h:272
char patient_name[81]
Definition: edflib.h:273
int filetype
Definition: edflib.h:250
char equipment[81]
Definition: edflib.h:277
edflib_param_t signalparam[EDFLIB_MAXSIGNALS]
Definition: edflib.h:282
char patient[81]
Definition: edflib.h:260
int startdate_year
Definition: edflib.h:255
int birthdate_day
Definition: edflib.h:270
Definition: edflib.h:220
int smp_in_datarecord
Definition: edflib.h:227
double phys_min
Definition: edflib.h:224
char physdimension[9]
Definition: edflib.h:228
int dig_min
Definition: edflib.h:226
int dig_max
Definition: edflib.h:225
char label[17]
Definition: edflib.h:221
char prefilter[81]
Definition: edflib.h:229
char transducer[81]
Definition: edflib.h:230
double phys_max
Definition: edflib.h:223
long long smp_in_file
Definition: edflib.h:222