[med-svn] [Git][med-team/edflib][upstream] New upstream version 1.25

Étienne Mollier (@emollier) gitlab at salsa.debian.org
Sat Jan 13 15:40:47 GMT 2024



Étienne Mollier pushed to branch upstream at Debian Med / edflib


Commits:
9089cd7f by Étienne Mollier at 2024-01-13T16:33:27+01:00
New upstream version 1.25
- - - - -


12 changed files:

- LICENSE
- README.md
- edflib.c
- edflib.h
- lib/README
- lib/makefile
- sine_generator.c
- sweep_generator.c
- test_edflib.c
- test_generator.c
- unittest/LICENSE
- unittest/unittest.c


Changes:

=====================================
LICENSE
=====================================
@@ -1,6 +1,6 @@
 BSD 3-Clause License
 
-Copyright (c) 2009 - 2023 Teunis van Beelen
+Copyright (c) 2009 - 2024 Teunis van Beelen
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without


=====================================
README.md
=====================================
@@ -24,7 +24,7 @@ Compilation has been tested using GCC on Linux, Mingw-w64 on Windows, and LLVM G
 
 Big-endian systems are not supported.
 
-To understand how to use the library, read the comments in `edflib.h`.
+Documentation is here: https://www.teuniz.net/edflib/doc/edflib_8h.html
 
 
 ## Examples
@@ -42,7 +42,7 @@ sample frequency of 2048 Hz.
 `sweep_generator` creates a linear or logarithmic sweep through a range of frequencies in EDF+ or
 BDF+ format.
 
-Use EDFbrowser to view these files: http://www.teuniz.net/edfbrowser/
+Use EDFbrowser to view these files: https://www.teuniz.net/edfbrowser/
 
 `test_edflib <filename> <signalnumber>` will print the properties of the EDF/BDF header, the
 annotations, and the values of 200 samples of the chosen signal. For example, running
@@ -66,7 +66,7 @@ offset = (physical max / units per bit) - digital max
 For a better explanation about the relation between digital data and physical data,
 read the document "Coding Schemes Used with Data Converters" (PDF):
 
-http://www.ti.com/general/docs/lit/getliterature.tsp?baseLiteratureNumber=sbaa042
+https://www.ti.com/lit/pdf/sbaa042
 
 An EDF file usually contains multiple so-called datarecords. One datarecord usually has a duration of one second (this is the default but it is not mandatory!).
 In that case a file with a duration of five minutes contains 300 datarecords. The duration of a datarecord can be freely chosen but, if possible, use values from
@@ -85,7 +85,7 @@ The description/name of an EDF+ annotation on the other hand, is encoded in UTF-
 
 ## License
 
-Copyright (c) 2009 - 2023 Teunis van Beelen
+Copyright (c) 2009 - 2024 Teunis van Beelen
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without


=====================================
edflib.c
=====================================
@@ -1,7 +1,7 @@
 /*
 *****************************************************************************
 *
-* Copyright (c) 2009 - 2023 Teunis van Beelen
+* Copyright (c) 2009 - 2024 Teunis van Beelen
 * All rights reserved.
 *
 * Email: teuniz at protonmail.com
@@ -35,7 +35,7 @@
 
 #include "edflib.h"
 
-#define EDFLIB_VERSION  (124)
+#define EDFLIB_VERSION  (125)
 #define EDFLIB_MAXFILES  (64)
 
 #if defined(__APPLE__) || defined(__MACH__) || defined(__APPLE_CC__) || defined(__HAIKU__) || defined(__ANDROID__)
@@ -67,11 +67,19 @@
 
 #endif
 
-/* max size of annotationtext */
+/* max length of annotation's description in bytes
+ * you may modify this number in order to create more space for longer description strings
+ */
 #define EDFLIB_WRITE_MAX_ANNOTATION_LEN  (40)
 
-/* bytes in datarecord for EDF annotations, must be an integer multiple of three and two */
-#define EDFLIB_ANNOTATION_BYTES  (120)
+/* number of bytes in datarecord for the annotations track, must be a multiple of six
+ * do not modify this macro
+ */
+#if ((EDFLIB_WRITE_MAX_ANNOTATION_LEN + 80) % 6)
+#define EDFLIB_ANNOTATION_BYTES  ((((EDFLIB_WRITE_MAX_ANNOTATION_LEN + 80) / 6) + 1) * 6)
+#else
+#define EDFLIB_ANNOTATION_BYTES  (EDFLIB_WRITE_MAX_ANNOTATION_LEN + 80)
+#endif
 
 /* for writing only */
 #define EDFLIB_MAX_ANNOTATION_CHANNELS  (64)
@@ -148,6 +156,7 @@ typedef struct
   int       eq_sf;
   char      *wrbuf;
   int       wrbufsize;
+  int       annot_chan_idx_pos;
   edfparamblock_t *edfparam;
 } edfhdrblock_t;
 
@@ -256,6 +265,8 @@ EDFLIB_API int edfopen_file_readonly(const char *path, edflib_hdr_t *edfhdr, int
 
   memset(edfhdr, 0, sizeof(edflib_hdr_t));
 
+  edfhdr->handle = -1;
+
   /* avoid surprises! */
   if((sizeof(char)      != 1) ||
      (sizeof(short)     != 2) ||
@@ -564,13 +575,33 @@ EDFLIB_API int edfclose_file(int handle)
     {
       if(hdr->edf)
       {
-        offset += (long long)(hdr->edfparam[i].smp_per_record * 2);
+        if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_END)
+        {
+          offset += (long long)(hdr->edfparam[i].smp_per_record * 2);
+        }
+        else if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_MIDDLE)
+          {
+            if(i < (hdr->edfsignals / 2))
+            {
+              offset += (long long)(hdr->edfparam[i].smp_per_record * 2);
+            }
+          }
 
         datrecsize += (hdr->edfparam[i].smp_per_record * 2);
       }
       else
       {
-        offset += (long long)(hdr->edfparam[i].smp_per_record * 3);
+        if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_END)
+        {
+          offset += (long long)(hdr->edfparam[i].smp_per_record * 3);
+        }
+        else if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_MIDDLE)
+          {
+            if(i < (hdr->edfsignals / 2))
+            {
+              offset += (long long)(hdr->edfparam[i].smp_per_record * 3);
+            }
+          }
 
         datrecsize += (hdr->edfparam[i].smp_per_record * 3);
       }
@@ -760,23 +791,25 @@ EDFLIB_API long long edftell(int handle, int edfsignal)
 }
 
 
-EDFLIB_API void edfrewind(int handle, int edfsignal)
+EDFLIB_API int edfrewind(int handle, int edfsignal)
 {
   int channel;
 
-  if((handle<0)||(handle>=EDFLIB_MAXFILES))  return;
+  if((handle<0)||(handle>=EDFLIB_MAXFILES))  return -1;
 
-  if(hdrlist[handle]==NULL)  return;
+  if(hdrlist[handle]==NULL)  return -1;
 
-  if(edfsignal<0)  return;
+  if(edfsignal<0)  return -1;
 
-  if(hdrlist[handle]->writemode)  return;
+  if(hdrlist[handle]->writemode)  return -1;
 
-  if(edfsignal>=(hdrlist[handle]->edfsignals - hdrlist[handle]->nr_annot_chns))  return;
+  if(edfsignal>=(hdrlist[handle]->edfsignals - hdrlist[handle]->nr_annot_chns))  return -1;
 
   channel = hdrlist[handle]->mapped_signals[edfsignal];
 
   hdrlist[handle]->edfparam[channel].sample_pntr = 0LL;
+
+  return 0;
 }
 
 
@@ -2127,7 +2160,7 @@ static edfhdrblock_t * edflib_check_edf_file(FILE *inputfile, int *edf_error)
     strncpy(scratchpad, edf_hdr + 256 + (edfhdr->edfsignals * 224) + (i * 32), 32);
     for(j=0; j<32; j++)
     {
-      if((scratchpad[j]<32)||(scratchpad[j]>126))
+      if(scratchpad[j] != ' ')
       {
         *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS;
         free(edf_hdr);
@@ -3114,6 +3147,16 @@ static int edflib_get_annotations(edfhdrblock_t *edfhdr, int hdl, int read_annot
                 {
                   if(j==EDFLIB_MAX_ANNOTATION_LEN)  break;
                   new_annotation->annotation[j] = scratchpad[j];
+                  if((((unsigned char *)new_annotation->annotation)[j] < 32) || (((unsigned char *)new_annotation->annotation)[j] == 127))
+                  {
+                    if((new_annotation->annotation[j] != '\t') &&
+                       (new_annotation->annotation[j] != '\n') &&
+                       (new_annotation->annotation[j] != '\r'))
+                    {
+                      error = 38;  /* non-printable characters in description string */
+                      goto END;
+                    }
+                  }
                 }
                 new_annotation->annotation[j] = 0;
 
@@ -3802,6 +3845,21 @@ EDFLIB_API int edfwrite_digital_short_samples(int handle, short *buf)
 
   digmin = hdr->edfparam[edfsignal].dig_min;
 
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_START)
+  {
+    if(hdr->signal_write_sequence_pos == 0)
+    {
+      if(edflib_write_tal(hdr, file))  return -1;
+    }
+  }
+  else if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_MIDDLE)
+  {
+    if(hdr->signal_write_sequence_pos == (hdr->edfsignals / 2))
+    {
+      if(edflib_write_tal(hdr, file))  return -1;
+    }
+  }
+
   if(hdr->edf)
   {
     if((digmax != 0x7fff) || (digmin != -0x8000))
@@ -3855,7 +3913,10 @@ EDFLIB_API int edfwrite_digital_short_samples(int handle, short *buf)
   {
     hdr->signal_write_sequence_pos = 0;
 
-    if(edflib_write_tal(hdr, file))  return -1;
+    if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_END)
+    {
+      if(edflib_write_tal(hdr, file))  return -1;
+    }
 
     hdr->datarecords++;
 
@@ -3910,6 +3971,21 @@ EDFLIB_API int edfwrite_digital_samples(int handle, int *buf)
 
   digmin = hdr->edfparam[edfsignal].dig_min;
 
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_START)
+  {
+    if(hdr->signal_write_sequence_pos == 0)
+    {
+      if(edflib_write_tal(hdr, file))  return -1;
+    }
+  }
+  else if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_MIDDLE)
+  {
+    if(hdr->signal_write_sequence_pos == (hdr->edfsignals / 2))
+    {
+      if(edflib_write_tal(hdr, file))  return -1;
+    }
+  }
+
   if(hdr->edf)
   {
     if(hdr->wrbufsize < (sf * 2))
@@ -3979,7 +4055,10 @@ EDFLIB_API int edfwrite_digital_samples(int handle, int *buf)
   {
     hdr->signal_write_sequence_pos = 0;
 
-    if(edflib_write_tal(hdr, file))  return -1;
+    if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_END)
+    {
+      if(edflib_write_tal(hdr, file))  return -1;
+    }
 
     hdr->datarecords++;
 
@@ -4028,9 +4107,12 @@ EDFLIB_API int edf_blockwrite_digital_samples(int handle, int *buf)
     if(error)  return error;
   }
 
-  buf_offset = 0;
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_START)
+  {
+    if(edflib_write_tal(hdr, file))  return -1;
+  }
 
-  for(j=0; j<edfsignals; j++)
+  for(j=0, buf_offset=0; j<edfsignals; j++)
   {
     sf = hdr->edfparam[j].smp_per_record;
 
@@ -4038,6 +4120,14 @@ EDFLIB_API int edf_blockwrite_digital_samples(int handle, int *buf)
 
     digmin = hdr->edfparam[j].dig_min;
 
+    if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_MIDDLE)
+    {
+      if(j == (edfsignals / 2))
+      {
+        if(edflib_write_tal(hdr, file))  return -1;
+      }
+    }
+
     if(hdr->edf)
     {
       if(hdr->wrbufsize < (sf * 2))
@@ -4104,7 +4194,10 @@ EDFLIB_API int edf_blockwrite_digital_samples(int handle, int *buf)
     buf_offset += sf;
   }
 
-  if(edflib_write_tal(hdr, file))  return -1;
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_END)
+  {
+    if(edflib_write_tal(hdr, file))  return -1;
+  }
 
   hdr->datarecords++;
 
@@ -4157,9 +4250,12 @@ EDFLIB_API int edf_blockwrite_digital_short_samples(int handle, short *buf)
     }
   }
 
-  buf_offset = 0;
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_START)
+  {
+    if(edflib_write_tal(hdr, file))  return -1;
+  }
 
-  for(j=0; j<edfsignals; j++)
+  for(j=0, buf_offset=0; j<edfsignals; j++)
   {
     sf = hdr->edfparam[j].smp_per_record;
 
@@ -4167,6 +4263,14 @@ EDFLIB_API int edf_blockwrite_digital_short_samples(int handle, short *buf)
 
     digmin = hdr->edfparam[j].dig_min;
 
+    if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_MIDDLE)
+    {
+      if(j == (edfsignals / 2))
+      {
+        if(edflib_write_tal(hdr, file))  return -1;
+      }
+    }
+
     if(hdr->edf)
     {
       if((digmax != 0x7fff) || (digmin != -0x8000))
@@ -4223,7 +4327,10 @@ EDFLIB_API int edf_blockwrite_digital_short_samples(int handle, short *buf)
     buf_offset += sf;
   }
 
-  if(edflib_write_tal(hdr, file))  return -1;
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_END)
+  {
+    if(edflib_write_tal(hdr, file))  return -1;
+  }
 
   hdr->datarecords++;
 
@@ -4238,7 +4345,8 @@ EDFLIB_API int edf_blockwrite_digital_3byte_samples(int handle, void *buf)
   int  j,
        error,
        edfsignals,
-       total_samples=0;
+       total_samples,
+       mid_samples;
 
   FILE *file;
 
@@ -4269,14 +4377,47 @@ EDFLIB_API int edf_blockwrite_digital_3byte_samples(int handle, void *buf)
     if(error)  return error;
   }
 
-  for(j=0; j<edfsignals; j++)
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_START)
+  {
+    if(edflib_write_tal(hdr, file))  return -1;
+  }
+
+  for(j=0, total_samples=0, mid_samples=0; j<edfsignals; j++)
   {
     total_samples += hdr->edfparam[j].smp_per_record;
+
+    if(j < (edfsignals / 2))  mid_samples = total_samples;
   }
 
-  if(fwrite(buf, total_samples * 3, 1, file) != 1)  return -1;
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_MIDDLE)
+  {
+    if(mid_samples)
+    {
+      if(fwrite(buf, mid_samples * 3, 1, file) != 1)  return -1;
 
-  if(edflib_write_tal(hdr, file))  return -1;
+      if(edflib_write_tal(hdr, file))  return -1;
+
+      if(total_samples > mid_samples)
+      {
+        if(fwrite(((unsigned char *)buf) + (mid_samples * 3), (total_samples - mid_samples) * 3, 1, file) != 1)  return -1;
+      }
+    }
+    else
+    {
+      if(edflib_write_tal(hdr, file))  return -1;
+
+      if(fwrite(buf, total_samples * 3, 1, file) != 1)  return -1;
+    }
+  }
+  else
+  {
+    if(fwrite(buf, total_samples * 3, 1, file) != 1)  return -1;
+  }
+
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_END)
+  {
+    if(edflib_write_tal(hdr, file))  return -1;
+  }
 
   hdr->datarecords++;
 
@@ -4337,6 +4478,21 @@ EDFLIB_API int edfwrite_physical_samples(int handle, double *buf)
 
   phys_offset = hdr->edfparam[edfsignal].offset;
 
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_START)
+  {
+    if(hdr->signal_write_sequence_pos == 0)
+    {
+      if(edflib_write_tal(hdr, file))  return -1;
+    }
+  }
+  else if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_MIDDLE)
+  {
+    if(hdr->signal_write_sequence_pos == (hdr->edfsignals / 2))
+    {
+      if(edflib_write_tal(hdr, file))  return -1;
+    }
+  }
+
   if(hdr->edf)
   {
     if(hdr->wrbufsize < (sf * 2))
@@ -4406,7 +4562,10 @@ EDFLIB_API int edfwrite_physical_samples(int handle, double *buf)
   {
     hdr->signal_write_sequence_pos = 0;
 
-    if(edflib_write_tal(hdr, file))  return -1;
+    if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_END)
+    {
+      if(edflib_write_tal(hdr, file))  return -1;
+    }
 
     hdr->datarecords++;
 
@@ -4458,9 +4617,12 @@ EDFLIB_API int edf_blockwrite_physical_samples(int handle, double *buf)
     if(error)  return error;
   }
 
-  buf_offset = 0;
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_START)
+  {
+    if(edflib_write_tal(hdr, file))  return -1;
+  }
 
-  for(j=0; j<edfsignals; j++)
+  for(j=0, buf_offset=0; j<edfsignals; j++)
   {
     sf = hdr->edfparam[j].smp_per_record;
 
@@ -4472,6 +4634,14 @@ EDFLIB_API int edf_blockwrite_physical_samples(int handle, double *buf)
 
     phys_offset = hdr->edfparam[j].offset;
 
+    if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_MIDDLE)
+    {
+      if(j == (edfsignals / 2))
+      {
+        if(edflib_write_tal(hdr, file))  return -1;
+      }
+    }
+
     if(hdr->edf)
     {
       if(hdr->wrbufsize < (sf * 2))
@@ -4538,7 +4708,10 @@ EDFLIB_API int edf_blockwrite_physical_samples(int handle, double *buf)
     buf_offset += sf;
   }
 
-  if(edflib_write_tal(hdr, file))  return -1;
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_END)
+  {
+    if(edflib_write_tal(hdr, file))  return -1;
+  }
 
   hdr->datarecords++;
 
@@ -5003,8 +5176,39 @@ static int edflib_write_edf_header(edfhdrblock_t *hdr)
     fputc(' ', file);
   }
 
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_START)
+  {
+    for(j=0; j<hdr->nr_annot_chns; j++)
+    {
+      if(hdr->edf)
+      {
+        fprintf(file, "EDF Annotations ");
+      }
+      else
+      {
+        fprintf(file, "BDF Annotations ");
+      }
+    }
+  }
   for(i=0; i<edfsignals; i++)
   {
+    if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_MIDDLE)
+    {
+      if(i == (edfsignals / 2))
+      {
+        for(j=0; j<hdr->nr_annot_chns; j++)
+        {
+          if(hdr->edf)
+          {
+            fprintf(file, "EDF Annotations ");
+          }
+          else
+          {
+            fprintf(file, "BDF Annotations ");
+          }
+        }
+      }
+    }
     len = strlen(hdr->edfparam[i].label);
     edflib_latin1_to_ascii(hdr->edfparam[i].label, len);
     for(j=0; j<len; j++)
@@ -5016,19 +5220,39 @@ static int edflib_write_edf_header(edfhdrblock_t *hdr)
       fputc(' ', file);
     }
   }
-  for(j=0; j<hdr->nr_annot_chns; j++)
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_END)
   {
-    if(hdr->edf)
+    for(j=0; j<hdr->nr_annot_chns; j++)
     {
-      fprintf(file, "EDF Annotations ");
+      if(hdr->edf)
+      {
+        fprintf(file, "EDF Annotations ");
+      }
+      else
+      {
+        fprintf(file, "BDF Annotations ");
+      }
     }
-    else
+  }
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_START)
+  {
+    for(j=0; j<(80 * hdr->nr_annot_chns); j++)
     {
-      fprintf(file, "BDF Annotations ");
+      fputc(' ', file);
     }
   }
   for(i=0; i<edfsignals; i++)
   {
+    if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_MIDDLE)
+    {
+      if(i == (edfsignals / 2))
+      {
+        for(j=0; j<(80 * hdr->nr_annot_chns); j++)
+        {
+          fputc(' ', file);
+        }
+      }
+    }
     len = strlen(hdr->edfparam[i].transducer);
     edflib_latin1_to_ascii(hdr->edfparam[i].transducer, len);
     for(j=0; j<len; j++)
@@ -5040,15 +5264,32 @@ static int edflib_write_edf_header(edfhdrblock_t *hdr)
       fputc(' ', file);
     }
   }
-  for(j=0; j<hdr->nr_annot_chns; j++)
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_END)
   {
-    for(i=0; i<80; i++)
+    for(j=0; j<(80 * hdr->nr_annot_chns); j++)
     {
       fputc(' ', file);
     }
   }
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_START)
+  {
+    for(j=0; j<hdr->nr_annot_chns; j++)
+    {
+      fprintf(file, "        ");
+    }
+  }
   for(i=0; i<edfsignals; i++)
   {
+    if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_MIDDLE)
+    {
+      if(i == (edfsignals / 2))
+      {
+        for(j=0; j<hdr->nr_annot_chns; j++)
+        {
+          fprintf(file, "        ");
+        }
+      }
+    }
     len = strlen(hdr->edfparam[i].physdimension);
     edflib_latin1_to_ascii(hdr->edfparam[i].physdimension, len);
     for(j=0; j<len; j++)
@@ -5060,12 +5301,32 @@ static int edflib_write_edf_header(edfhdrblock_t *hdr)
       fputc(' ', file);
     }
   }
-  for(j=0; j<hdr->nr_annot_chns; j++)
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_END)
   {
-    fprintf(file, "        ");
+    for(j=0; j<hdr->nr_annot_chns; j++)
+    {
+      fprintf(file, "        ");
+    }
+  }
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_START)
+  {
+    for(j=0; j<hdr->nr_annot_chns; j++)
+    {
+      fprintf(file, "-1      ");
+    }
   }
   for(i=0; i<edfsignals; i++)
   {
+    if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_MIDDLE)
+    {
+      if(i == (edfsignals / 2))
+      {
+        for(j=0; j<hdr->nr_annot_chns; j++)
+        {
+          fprintf(file, "-1      ");
+        }
+      }
+    }
     p = edflib_snprint_number_nonlocalized(str, hdr->edfparam[i].phys_min, 128);
     for(; p<8; p++)
     {
@@ -5074,12 +5335,32 @@ static int edflib_write_edf_header(edfhdrblock_t *hdr)
     str[8] = 0;
     fprintf(file, "%s", str);
   }
-  for(j=0; j<hdr->nr_annot_chns; j++)
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_END)
   {
-    fprintf(file, "-1      ");
+    for(j=0; j<hdr->nr_annot_chns; j++)
+    {
+      fprintf(file, "-1      ");
+    }
+  }
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_START)
+  {
+    for(j=0; j<hdr->nr_annot_chns; j++)
+    {
+      fprintf(file, "1       ");
+    }
   }
   for(i=0; i<edfsignals; i++)
   {
+    if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_MIDDLE)
+    {
+      if(i == (edfsignals / 2))
+      {
+        for(j=0; j<hdr->nr_annot_chns; j++)
+        {
+          fprintf(file, "1       ");
+        }
+      }
+    }
     p = edflib_snprint_number_nonlocalized(str, hdr->edfparam[i].phys_max, 128);
     for(; p<8; p++)
     {
@@ -5088,50 +5369,141 @@ static int edflib_write_edf_header(edfhdrblock_t *hdr)
     str[8] = 0;
     fprintf(file, "%s", str);
   }
-  for(j=0; j<hdr->nr_annot_chns; j++)
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_END)
   {
-    fprintf(file, "1       ");
+    for(j=0; j<hdr->nr_annot_chns; j++)
+    {
+      fprintf(file, "1       ");
+    }
+  }
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_START)
+  {
+    for(j=0; j<hdr->nr_annot_chns; j++)
+    {
+      if(hdr->edf)
+      {
+        fprintf(file, "-32768  ");
+      }
+      else
+      {
+        fprintf(file, "-8388608");
+      }
+    }
   }
   for(i=0; i<edfsignals; i++)
   {
+    if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_MIDDLE)
+    {
+      if(i == (edfsignals / 2))
+      {
+        for(j=0; j<hdr->nr_annot_chns; j++)
+        {
+          if(hdr->edf)
+          {
+            fprintf(file, "-32768  ");
+          }
+          else
+          {
+            fprintf(file, "-8388608");
+          }
+        }
+      }
+    }
     p = edflib_fprint_int_number_nonlocalized(file, hdr->edfparam[i].dig_min, 0, 0);
     for(; p<8; p++)
     {
       fputc(' ', file);
     }
   }
-  for(j=0; j<hdr->nr_annot_chns; j++)
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_END)
   {
-    if(hdr->edf)
+    for(j=0; j<hdr->nr_annot_chns; j++)
     {
-      fprintf(file, "-32768  ");
+      if(hdr->edf)
+      {
+        fprintf(file, "-32768  ");
+      }
+      else
+      {
+        fprintf(file, "-8388608");
+      }
     }
-    else
+  }
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_START)
+  {
+    for(j=0; j<hdr->nr_annot_chns; j++)
     {
-      fprintf(file, "-8388608");
+      if(hdr->edf)
+      {
+        fprintf(file, "32767   ");
+      }
+      else
+      {
+        fprintf(file, "8388607 ");
+      }
     }
   }
   for(i=0; i<edfsignals; i++)
   {
+    if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_MIDDLE)
+    {
+      if(i == (edfsignals / 2))
+      {
+        for(j=0; j<hdr->nr_annot_chns; j++)
+        {
+          if(hdr->edf)
+          {
+            fprintf(file, "32767   ");
+          }
+          else
+          {
+            fprintf(file, "8388607 ");
+          }
+        }
+      }
+    }
     p = edflib_fprint_int_number_nonlocalized(file, hdr->edfparam[i].dig_max, 0, 0);
     for(; p<8; p++)
     {
       fputc(' ', file);
     }
   }
-  for(j=0; j<hdr->nr_annot_chns; j++)
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_END)
   {
-    if(hdr->edf)
+    for(j=0; j<hdr->nr_annot_chns; j++)
     {
-      fprintf(file, "32767   ");
+      if(hdr->edf)
+      {
+        fprintf(file, "32767   ");
+      }
+      else
+      {
+        fprintf(file, "8388607 ");
+      }
     }
-    else
+  }
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_START)
+  {
+    for(i=0; i<hdr->nr_annot_chns; i++)
     {
-      fprintf(file, "8388607 ");
+      for(j=0; j<80; j++)
+      {
+        fputc(' ', file);
+      }
     }
   }
   for(i=0; i<edfsignals; i++)
   {
+    if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_MIDDLE)
+    {
+      if(i == (edfsignals / 2))
+      {
+        for(j=0; j<(80 * hdr->nr_annot_chns); j++)
+        {
+          fputc(' ', file);
+        }
+      }
+    }
     len = strlen(hdr->edfparam[i].prefilter);
     edflib_latin1_to_ascii(hdr->edfparam[i].prefilter, len);
     for(j=0; j<len; j++)
@@ -5143,45 +5515,94 @@ static int edflib_write_edf_header(edfhdrblock_t *hdr)
       fputc(' ', file);
     }
   }
-  for(i=0; i<hdr->nr_annot_chns; i++)
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_END)
   {
-    for(j=0; j<80; j++)
+    for(i=0; i<hdr->nr_annot_chns; i++)
     {
-      fputc(' ', file);
+      for(j=0; j<80; j++)
+      {
+        fputc(' ', file);
+      }
+    }
+  }
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_START)
+  {
+    for(j=0; j<hdr->nr_annot_chns; j++)
+    {
+      if(hdr->edf)
+      {
+        p = edflib_fprint_int_number_nonlocalized(file, EDFLIB_ANNOTATION_BYTES / 2, 0, 0);
+        for(; p<8; p++)
+        {
+          fputc(' ', file);
+        }
+      }
+      else
+      {
+        p = edflib_fprint_int_number_nonlocalized(file, EDFLIB_ANNOTATION_BYTES / 3, 0, 0);
+        for(; p<8; p++)
+        {
+          fputc(' ', file);
+        }
+      }
     }
   }
   for(i=0; i<edfsignals; i++)
   {
+    if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_MIDDLE)
+    {
+      if(i == (edfsignals / 2))
+      {
+        for(j=0; j<hdr->nr_annot_chns; j++)
+        {
+          if(hdr->edf)
+          {
+            p = edflib_fprint_int_number_nonlocalized(file, EDFLIB_ANNOTATION_BYTES / 2, 0, 0);
+            for(; p<8; p++)
+            {
+              fputc(' ', file);
+            }
+          }
+          else
+          {
+            p = edflib_fprint_int_number_nonlocalized(file, EDFLIB_ANNOTATION_BYTES / 3, 0, 0);
+            for(; p<8; p++)
+            {
+              fputc(' ', file);
+            }
+          }
+        }
+      }
+    }
     p = edflib_fprint_int_number_nonlocalized(file, hdr->edfparam[i].smp_per_record, 0, 0);
     for(; p<8; p++)
     {
       fputc(' ', file);
     }
   }
-  for(j=0; j<hdr->nr_annot_chns; j++)
+  if(hdr->annot_chan_idx_pos == EDF_ANNOT_IDX_POS_END)
   {
-    if(hdr->edf)
+    for(j=0; j<hdr->nr_annot_chns; j++)
     {
-      p = edflib_fprint_int_number_nonlocalized(file, EDFLIB_ANNOTATION_BYTES / 2, 0, 0);
-      for(; p<8; p++)
+      if(hdr->edf)
       {
-        fputc(' ', file);
+        p = edflib_fprint_int_number_nonlocalized(file, EDFLIB_ANNOTATION_BYTES / 2, 0, 0);
+        for(; p<8; p++)
+        {
+          fputc(' ', file);
+        }
       }
-    }
-    else
-    {
-      p = edflib_fprint_int_number_nonlocalized(file, EDFLIB_ANNOTATION_BYTES / 3, 0, 0);
-      for(; p<8; p++)
+      else
       {
-        fputc(' ', file);
+        p = edflib_fprint_int_number_nonlocalized(file, EDFLIB_ANNOTATION_BYTES / 3, 0, 0);
+        for(; p<8; p++)
+        {
+          fputc(' ', file);
+        }
       }
     }
   }
-  for(i=0; i<(edfsignals * 32); i++)
-  {
-    fputc(' ', file);
-  }
-  for(i=0; i<(hdr->nr_annot_chns * 32); i++)
+  for(i=0; i<((edfsignals + hdr->nr_annot_chns) * 32); i++)
   {
     fputc(' ', file);
   }
@@ -5202,6 +5623,10 @@ EDFLIB_API int edf_set_label(int handle, int edfsignal, const char *label)
 
   if((edfsignal<0) || (edfsignal>=hdrlist[handle]->edfsignals))  return -1;
 
+  if(!strncmp(label, "EDF Annotations", 15))  return -1;
+
+  if(!strncmp(label, "BDF Annotations", 15))  return -1;
+
   strncpy(hdrlist[handle]->edfparam[edfsignal].label, label, 16);
 
   hdrlist[handle]->edfparam[edfsignal].label[16] = 0;
@@ -5668,6 +6093,24 @@ EDFLIB_API int edfwrite_annotation_latin1_hr(int handle, long long onset, long l
 }
 
 
+EDFLIB_API int edf_set_annot_chan_idx_pos(int handle, int pos)
+{
+  if((handle<0)||(handle>=EDFLIB_MAXFILES))  return -1;
+
+  if(hdrlist[handle]==NULL)  return -1;
+
+  if(!hdrlist[handle]->writemode)  return -1;
+
+  if(hdrlist[handle]->datarecords)  return -1;
+
+  if((pos < 0) || (pos > 2))  return -1;
+
+  hdrlist[handle]->annot_chan_idx_pos = pos;
+
+  return 0;
+}
+
+
 static void edflib_remove_padding_trailing_spaces(char *str)
 {
   int i;


=====================================
edflib.h
=====================================
@@ -1,7 +1,7 @@
 /*
 *****************************************************************************
 *
-* Copyright (c) 2009 - 2023 Teunis van Beelen
+* Copyright (c) 2009 - 2024 Teunis van Beelen
 * All rights reserved.
 *
 * Email: teuniz at protonmail.com
@@ -31,75 +31,79 @@
 *****************************************************************************
 */
 
-/****************************************************************************
+/**
+ * @file edflib.h
  *
- * For more info about the EDF and EDF+ format, visit: http://edfplus.info/specs/
+ * In EDF, the resolution (or sensitivity) (e.g. uV/bit) and offset are stored using four parameters:<br>
+ * digital maximum and minimum, and physical maximum and minimum.<br>
+ * Here, digital means the raw data coming from a sensor or ADC. Physical means the units like uV.<br>
+ * The resolution in units per least significant bit is calculated as follows:<br>
  *
- * For more info about the BDF and BDF+ format, visit: http://www.teuniz.net/edfbrowser/bdfplus%20format%20description.html
+ * units per bit = (physical max - physical min) / (digital max - digital min)<br>
  *
- * note: In EDF, the resolution (or sensitivity) (e.g. uV/bit) and offset are stored using four parameters:
- * digital maximum and minimum, and physical maximum and minimum.
- * Here, digital means the raw data coming from a sensor or ADC. Physical means the units like uV.
- * The resolution in units per least significant bit is calculated as follows:
+ * The digital offset is calculated as follows:<br>
  *
- * units per bit = (physical max - physical min) / (digital max - digital min)
+ * offset = (physical max / units per bit) - digital max<br>
  *
- * The digital offset is calculated as follows:
+ * For a better explanation about the relation between digital data and physical data,<br>
+ * read the document "Coding Schemes Used with Data Converters" (PDF):<br>
  *
- * offset = (physical max / units per bit) - digital max
+ * https://www.ti.com/general/docs/lit/getliterature.tsp?baseLiteratureNumber=sbaa042<br>
  *
- * For a better explanation about the relation between digital data and physical data,
- * read the document "Coding Schemes Used with Data Converters" (PDF):
+ * An EDF file usually contains multiple so-called datarecords. One datarecord usually has a duration of one second (this is the default but it's not mandatory).<br>
+ * In that case a file with a duration of five minutes contains 300 datarecords. The duration of a datarecord can be freely chosen but, if possible, use values from<br>
+ * 0.1 to 1 second for easier handling. Just make sure that the total size of one datarecord, expressed in bytes, does not exceed 10 MByte (15 MBytes for BDF(+)).<br>
  *
- * https://www.ti.com/general/docs/lit/getliterature.tsp?baseLiteratureNumber=sbaa042
+ * The <b>recommendation</b> of a maximum datarecord size of 61440 bytes in the EDF(+) specification was useful in the time people were still using DOS as their main operating system.<br>
+ * Using DOS and fast (near) pointers (16-bit pointers), the maximum allocatable block of memory was 64KByte.<br>
+ * This is not a concern anymore so the maximum datarecord size now is limited to 10 MByte for EDF(+) and 15 MByte for BDF(+). This helps to accommodate for higher sampling rates<br>
+ * used by modern Analog to Digital Converters.<br>
  *
- * note: An EDF file usually contains multiple so-called datarecords. One datarecord usually has a duration of one second (this is the default but it is not mandatory!).
- * In that case a file with a duration of five minutes contains 300 datarecords. The duration of a datarecord can be freely chosen but, if possible, use values from
- * 0.1 to 1 second for easier handling. Just make sure that the total size of one datarecord, expressed in bytes, does not exceed 10MByte (15MBytes for BDF(+)).
+ * EDF header character encoding: The EDF specification says that only (printable) ASCII characters are allowed.<br>
+ * When writing the header info, EDFlib will assume you are using Latin1 encoding and it will automatically convert<br>
+ * characters with accents, umlauts, tilde, etc. to their "normal" equivalent without the accent/umlaut/tilde/etc.<br>
+ * in order to create a valid EDF file.<br>
+ * The description of an EDF+ annotation/event/trigger on the other hand, is always encoded in UTF-8 (which is forward compatible with ASCII).<br>
  *
- * The RECOMMENDATION of a maximum datarecord size of 61440 bytes in the EDF and EDF+ specification was useful in the time people were still using DOS as their main operating system.
- * Using DOS and fast (near) pointers (16-bit pointers), the maximum allocatable block of memory was 64KByte.
- * This is not a concern anymore so the maximum datarecord size now is limited to 10MByte for EDF(+) and 15MByte for BDF(+). This helps to accommodate for higher sampling rates
- * used by modern Analog to Digital Converters.
+ * The sample frequency of a signal is calculated as follows: sf = (smp_in_datarecord * EDFLIB_TIME_DIMENSION) / datarecord_duration<br>
  *
- * EDF header character encoding: The EDF specification says that only (printable) ASCII characters are allowed.
- * When writing the header info, EDFlib will assume you are using Latin1 encoding and it will automatically convert
- * characters with accents, umlauts, tilde, etc. to their "normal" equivalent without the accent/umlaut/tilde/etc.
- * in order to create a valid EDF file.
- * The description of an EDF+ annotation on the other hand, is always encoded in UTF-8 (which is forward compatible with ASCII).
+ * Annotation signals<br>
+ * ==================<br>
  *
- * The sample frequency of a signal is calculated as follows: sf = (smp_in_datarecord * EDFLIB_TIME_DIMENSION) / datarecord_duration
+ * EDF+ and BDF+ store the annotations/events/triggers in one or more signals (in order to be backwards compatible with EDF and BDF)<br>
+ * and they can appear anywhere in the list of signals.<br>
+ * The numbering of the signals in the file is zero based (starts at 0). Signals used for annotations are skipped by EDFlib.<br>
+ * This means that the annotationsignal(s) in the file are hidden.<br>
+ * Use the function edf_get_annotation() to get the annotations.<br>
  *
- * Annotation signals
- * ==================
+ * So, when a file contains 7 signals and the third and fifth signal are annotation signals, the library will<br>
+ * report that there are only 5 signals in the file.<br>
+ * The library will "map" the (zero-based) signal numbers as follows: 0->0, 1->1, 2->3, 3->4, 4->6.<br>
+ * This way you don't need to worry about which signals are annotationsignals, the library will take care of it.<br>
  *
- * EDF+ and BDF+ store the annotations in one or more signals (in order to be backwards compatible with EDF and BDF).
- * The numbering of the signals in the file is zero based (starts at 0). Signals used for annotations are skipped by EDFlib.
- * This means that the annotationsignal(s) in the file are hidden.
- * Use the function edf_get_annotation() to get the annotations.
+ * How the library stores time values<br>
+ * ==================================<br>
  *
- * So, when a file contains 5 signals and the third signal is an annotations signal, the library will
- * report that there are only 4 signals in the file.
- * The library will "map" the signal numbers as follows: 0->0, 1->1, 2->3, 3->4.
- * This way you don't need to worry about which signals are annotationsignals, the library will take care of it.
+ * To avoid rounding errors and to be able to compare values, the library stores some time values in variables of type long long int.<br>
+ * In order not to lose the sub-second precision, all time values are scaled with a scaling factor: 10000000.<br>
+ * This will limit the time resolution to 100 nanoseconds. To calculate the amount of seconds, divide<br>
+ * the timevalue by 10000000 or use the macro EDFLIB_TIME_DIMENSION which is declared in edflib.h.<br>
+ * The following variables use this scaling when you open a file in read mode: "file_duration", "starttime_subsecond" and "onset".<br>
  *
- * How the library stores time values
- * ==================================
+ * EDFlib and thread-safety<br>
+ * ========================<br>
+ * The following functions are always MT-unsafe:<br>
+ * edfopen_file_readonly()   (race condition)<br>
+ * edfclose_file()           (race condition)<br>
+ * edflib_get_handle()       (race condition)<br>
  *
- * To avoid rounding errors, the library stores some time values in variables of type long long int.
- * In order not to lose the sub-second precision, all time values are scaled with a scaling factor: 10000000.
- * This will limit the time resolution to 100 nanoseconds. To calculate the amount of seconds, divide
- * the timevalue by 10000000 or use the macro EDFLIB_TIME_DIMENSION which is declared in edflib.h.
- * The following variables use this scaling when you open a file in read mode: "file_duration", "starttime_subsecond" and "onset".
+ * When writing to or reading from the same file, all EDFlib functions are MT-unsafe (race condition).<br>
  *
- * EDFlib and thread-safety
- * ========================
- * The following functions are always MT-unsafe:
- * edfopen_file_readonly()   (race condition)
- * edfclose_file()           (race condition)
- * edflib_get_handle()       (race condition)
+ * When accessing EDFlib from different threads, use a mutex.<br>
  *
- * When writing to or reading from the same file, all EDFlib functions are MT-unsafe (race condition).
+ * For more info about the EDF and EDF+ format, visit: https://edfplus.info/specs/<br>
+ *
+ * For more info about the BDF and BDF+ format, visit: https://www.teuniz.net/edfbrowser/bdfplus%20format%20description.html<br>
  *
  */
 
@@ -164,6 +168,10 @@
 #define EDFSEEK_CUR  (1)
 #define EDFSEEK_END  (2)
 
+#define EDF_ANNOT_IDX_POS_END     (0)
+#define EDF_ANNOT_IDX_POS_MIDDLE  (1)
+#define EDF_ANNOT_IDX_POS_START   (2)
+
 /* the following defines are used in the member "filetype" of the edf_hdr_struct
    and as return value for the function edfopen_file_readonly() */
 #define EDFLIB_FILETYPE_EDF                  (0)
@@ -173,8 +181,8 @@
 #define EDFLIB_MALLOC_ERROR                 (-1)
 #define EDFLIB_NO_SUCH_FILE_OR_DIRECTORY    (-2)
 
-/* when this error occurs, try to open the file with EDFbrowser,
-   it will give you full details about the cause of the error. */
+/* when this error occurs, try to open the file with EDFbrowser (https://www.teuniz.net/edfbrowser/),
+   it will give you full details about the cause of the error. It can also fix most errors. */
 #define EDFLIB_FILE_CONTAINS_FORMAT_ERRORS  (-3)
 
 #define EDFLIB_MAXFILES_REACHED             (-4)
@@ -205,311 +213,593 @@
 extern "C" {
 #endif
 
+/**
+ * This structure contains the signal parameters.
+ */
 typedef struct edf_param_struct
-{                                /* this structure contains all the relevant EDF-signal parameters of one signal */
-  char   label[17];              /* label (name) of the signal, null-terminated string */
-  long long smp_in_file;         /* number of samples of this signal in the file */
-  double phys_max;               /* physical maximum, usually the maximum input of the ADC */
-  double phys_min;               /* physical minimum, usually the minimum input of the ADC */
-  int    dig_max;                /* digital maximum, usually the maximum output of the ADC, cannot not be higher than 32767 for EDF or 8388607 for BDF */
-  int    dig_min;                /* digital minimum, usually the minimum output of the ADC, cannot not be lower than -32768 for EDF or -8388608 for BDF */
-  int    smp_in_datarecord;      /* number of samples of this signal in a datarecord, if the datarecord has a duration of one second (default), then it equals the sample rate */
-  char   physdimension[9];       /* physical dimension (uV, bpm, mA, etc.), null-terminated string */
-  char   prefilter[81];          /* null-terminated string */
-  char   transducer[81];         /* null-terminated string */
+{
+  char   label[17];              /*!< Label (name) of the signal, null-terminated string. */
+  long long smp_in_file;         /*!< Number of samples in the file. */
+  double phys_max;               /*!< Physical maximum, usually the maximum input of the ADC. */
+  double phys_min;               /*!< Physical minimum, usually the minimum input of the ADC. */
+  int    dig_max;                /*!< Digital maximum, usually the maximum output of the ADC, cannot not be higher than 32767 for EDF or 8388607 for BDF. */
+  int    dig_min;                /*!< Digital minimum, usually the minimum output of the ADC, cannot not be lower than -32768 for EDF or -8388608 for BDF. */
+  int    smp_in_datarecord;      /*!< Number of samplesin a datarecord, if the datarecord has a duration of one second (default), then it equals the sample rate. */
+  char   physdimension[9];       /*!< Physical dimension (unit, e.g. uV, bpm, mA, etc.), null-terminated string. */
+  char   prefilter[81];          /*!< Prefilter settings, null-terminated string. */
+  char   transducer[81];         /*!< Transducer (sensor), null-terminated string. */
 } edflib_param_t;
 
+/**
+ * This structure is used for annotations/events/triggers.
+ */
 typedef struct edf_annotation_struct
-{                                                       /* this structure is used for annotations */
-        long long onset;                                /* onset time of the event, expressed in units of 100 nanoseconds and relative to the start of the file */
-        long long duration_l;                           /* duration time, expressed in units of 100 nanoseconds, if less than zero: unused or not applicable */
-        char duration[20];                              /* duration time, this is a null-terminated ASCII text-string */
-        char annotation[EDFLIB_MAX_ANNOTATION_LEN + 1]; /* description of the event in UTF-8, this is a null terminated string */
+{
+        long long onset;                                /*!< Onset time of the event, expressed in units of 100 nanoseconds and relative to the start of the recording. */
+        long long duration_l;                           /*!< Duration, expressed in units of 100 nanoseconds, if less than zero: unused or not applicable. */
+        char duration[20];                              /*!< Duration, expressed in seconds, this is a null-terminated ASCII string. */
+        char annotation[EDFLIB_MAX_ANNOTATION_LEN + 1]; /*!< Description of the annotation/event/trigger, this is a null-terminated UTF8 string. */
 } edflib_annotation_t;
 
+/**
+ * This structure contains the general header info and parameters. It will be filled when calling the function edfopen_file_readonly().
+ */
 typedef struct edf_hdr_struct
-{                                 /* this structure contains all the relevant EDF header info and will be filled when calling the function edf_open_file_readonly() */
-  int       handle;               /* a handle (identifier) used to distinguish the different files */
-  int       filetype;             /* 0: EDF, 1: EDF+, 2: BDF, 3: BDF+, a negative number means an error */
-  int       edfsignals;           /* number of EDF signals in the file, annotation channels are NOT included */
-  long long file_duration;        /* duration of the file expressed in units of 100 nanoseconds */
-  int       startdate_day;
-  int       startdate_month;
-  int       startdate_year;
-  long long starttime_subsecond;  /* starttime offset expressed in units of 100 nanoseconds. Is always less than 10000000 (one second). Only used by EDF+ and BDF+ */
-  int       starttime_second;
-  int       starttime_minute;
-  int       starttime_hour;
-  char      patient[81];                                  /* null-terminated string, contains patient field of header, is always empty when filetype is EDFPLUS or BDFPLUS */
-  char      recording[81];                                /* null-terminated string, contains recording field of header, is always empty when filetype is EDFPLUS or BDFPLUS */
-  char      patientcode[81];                              /* null-terminated string, is always empty when filetype is EDF or BDF */
-  char      sex[16];                                      /* null-terminated string, is always empty when filetype is EDF or BDF */
+{
+  int       handle;               /*!< A handle (identifier) used to distinguish the different files or -1 in case of an error. */
+  int       filetype;             /*!< 0: EDF, 1: EDF+, 2: BDF, 3: BDF+, a negative number indicates an error code. */
+  int       edfsignals;           /*!< Number of signals in the file, annotation channels are not included. */
+  long long file_duration;        /*!< Duration of the file expressed in units of 100 nanoseconds. */
+  int       startdate_day;        /*!< Startdate: day: 1 - 31 */
+  int       startdate_month;      /*!< Startdate: month: 1 - 12 */
+  int       startdate_year;       /*!< Startdate: year: 1985 - 2084 */
+  long long starttime_subsecond;  /*!< Starttime subsecond expressed in units of 100 nanoseconds. Is always less than 10000000 (one second). Only used by EDF+ and BDF+. */
+  int       starttime_second;     /*!< Starttime: second: 0 - 59 */
+  int       starttime_minute;     /*!< Starttime: minute: 0 - 59 */
+  int       starttime_hour;       /*!< Starttime: hour: 0 - 23 */
+  char      patient[81];          /*!< Null-terminated string, contains patient field of header, is always empty when filetype is EDFPLUS or BDFPLUS. */
+  char      recording[81];        /*!< Null-terminated string, contains recording field of header, is always empty when filetype is EDFPLUS or BDFPLUS. */
+  char      patientcode[81];      /*!< Null-terminated string, is always empty when filetype is EDF or BDF. */
+  char      sex[16];              /*!< Null-terminated string, is always empty when filetype is EDF or BDF. */
 #if defined(__GNUC__)
-  char      gender[16] __attribute__ ((deprecated ("use sex")));      /* DEPRECATED!! use "sex" */
+  char      gender[16] __attribute__ ((deprecated ("use sex")));      /*!< Deprecated, use \p sex. */
 #else
-  char      gender[16];  /* DEPRECATED!! use "sex" */
+  char      gender[16];  /*!< Deprecated, use \p sex. */
 #endif
-  char      birthdate[16];                                /* null-terminated string, is always empty when filetype is EDF or BDF */
-  int       birthdate_day;                                /* 1 - 31 */
-  int       birthdate_month;                              /* 1 - 12 */
-  int       birthdate_year;
-  char      patient_name[81];                             /* null-terminated string, is always empty when filetype is EDF or BDF */
-  char      patient_additional[81];                       /* null-terminated string, is always empty when filetype is EDF or BDF */
-  char      admincode[81];                                /* null-terminated string, is always empty when filetype is EDF or BDF */
-  char      technician[81];                               /* null-terminated string, is always empty when filetype is EDF or BDF */
-  char      equipment[81];                                /* null-terminated string, is always empty when filetype is EDF or BDF */
-  char      recording_additional[81];                     /* null-terminated string, is always empty when filetype is EDF or BDF */
-  long long datarecord_duration;                          /* duration of a datarecord expressed in units of 100 nanoseconds */
-  long long datarecords_in_file;                          /* number of datarecords in the file */
-  long long annotations_in_file;                          /* number of annotations in the file */
-  edflib_param_t signalparam[EDFLIB_MAXSIGNALS];             /* array of structs which contain the relevant signal parameters */
+  char      birthdate[16];             /*!< Null-terminated string, is always empty when filetype is EDF or BDF. */
+  int       birthdate_day;             /*!< Birthdate: day: 1 - 31 (zero in case of EDF or BDF). */
+  int       birthdate_month;           /*!< Birthdate: month: 1 - 12 (zero in case of EDF or BDF). */
+  int       birthdate_year;            /*!< Birthdate: year: 1800 - 3000 (zero in case of EDF or BDF). */
+  char      patient_name[81];          /*!< Null-terminated string, is always empty when filetype is EDF or BDF. */
+  char      patient_additional[81];    /*!< Null-terminated string, is always empty when filetype is EDF or BDF. */
+  char      admincode[81];             /*!< Null-terminated string, is always empty when filetype is EDF or BDF. */
+  char      technician[81];            /*!< Null-terminated string, is always empty when filetype is EDF or BDF. */
+  char      equipment[81];             /*!< Null-terminated string, is always empty when filetype is EDF or BDF. */
+  char      recording_additional[81];  /*!< Null-terminated string, is always empty when filetype is EDF or BDF. */
+  long long datarecord_duration;       /*!< Duration of a datarecord expressed in units of 100 nanoseconds. */
+  long long datarecords_in_file;       /*!< Number of datarecords in the file. */
+  long long annotations_in_file;       /*!< Number of annotations/events/triggers in the file. */
+  edflib_param_t signalparam[EDFLIB_MAXSIGNALS];  /*!< array of structs containing the signal parameters. */
 } edflib_hdr_t;
 
 /*****************  the following functions are used to read files **************************/
 
-EDFLIB_API int edfopen_file_readonly(const char *path, edflib_hdr_t *edfhdr, int read_annotations);
-/* opens an existing file for reading
- * path is a null-terminated string containing the path to the file
- * hdr is a pointer to an edf_hdr_struct, all fields in this struct will be overwritten
- * the edf_hdr_struct will be filled with all the relevant header- and signalinfo/parameters
-
- * read_annotations must have one of the following values:
- *   EDFLIB_DO_NOT_READ_ANNOTATIONS      annotations will not be read (this saves time when opening a very large EDF+ or BDF+ file
- *   EDFLIB_READ_ANNOTATIONS             annotations will be read immediately, stops when an annotation has
+/**
+ * Opens an existing file for reading.
+ *
+ * @param[in] path
+ * null-terminated string containing the \p path to the file
+ *
+ * @param[out] edfhdr
+ * pointer to an \p edflib_hdr_t struct, all fields in this struct will be overwritten,
+ * it will be filled with all the relevant header- and signalinfo/parameters
+ *
+ * @param[in] read_annotations
+ * Must have one of the following values:
+ * - EDFLIB_DO_NOT_READ_ANNOTATIONS      annotations will not be read (this can save time when opening a very large EDF+ or BDF+ file
+ * - EDFLIB_READ_ANNOTATIONS             annotations will be read immediately, stops when an annotation has
  *                                       been found which contains the description "Recording ends"
- *   EDFLIB_READ_ALL_ANNOTATIONS         all annotations will be read immediately
-
- * returns 0 on success, in case of an error it returns -1 and an error code will be set in the member "filetype" of edflib_hdr_t
+ * - EDFLIB_READ_ALL_ANNOTATIONS         all annotations will be read immediately
+ *
+ * @return
+ * 0 on success, in case of an error it returns -1 and an error code will be set in the member "filetype" of edfhdr.
  * This function is required if you want to read a file
+ *
+ * In case of a file format error (-3), try to open the file with EDFbrowser: https://www.teuniz.net/edfbrowser/
+ * It will give you full details about the cause of the error and it can also fix most errors.
  */
+EDFLIB_API int edfopen_file_readonly(const char *path, edflib_hdr_t *edfhdr, int read_annotations);
 
-EDFLIB_API int edfread_physical_samples(int handle, int edfsignal, int n, double *buf);
-/* reads n samples from edfsignal, starting from the current sample position indicator, into buf (edfsignal starts at 0)
- * the values are converted to their physical values e.g. microVolts, beats per minute, etc.
- * bufsize should be equal to or bigger than sizeof(double[n])
- * the sample position indicator will be increased with the amount of samples read
- * returns the amount of samples read (this can be less than n or zero!)
- * or -1 in case of an error
+/**
+ * Reads \p n samples from \p edfsignal, starting from the current sample position indicator, into \p buf (edfsignal starts at 0).
+ * The values are converted to their physical values e.g. microVolts, beats per minute, etc.
+ *
+ * @param[in] handle
+ * File handle.
+ * @param[in] edfsignal
+ * The zero-based index of the signal.
+ * @param[in] n
+ * Number of samples to read. The sample position indicator will be increased with the same amount.
+ * @param[out] buf
+ * Pointer to a buffer, size must be equal to, or bigger than, sizeof(double[n])
+ *
+ * @return
+ * The number of samples read (this can be less than \p n or zero!) or -1 in case of an error
  */
+EDFLIB_API int edfread_physical_samples(int handle, int edfsignal, int n, double *buf);
 
-EDFLIB_API int edfread_digital_samples(int handle, int edfsignal, int n, int *buf);
-/* reads n samples from edfsignal, starting from the current sample position indicator, into buf (edfsignal starts at 0)
- * the values are the "raw" digital values
- * bufsize should be equal to or bigger than sizeof(int[n])
- * the sample position indicator will be increased with the amount of samples read
- * returns the amount of samples read (this can be less than n or zero!)
- * or -1 in case of an error
+/**
+ * Reads \p n samples from \p edfsignal, starting from the current sample position indicator, into \p buf (edfsignal starts at 0).
+ * The values are the "raw" digital values (e.g. from an ADC).
+ *
+ * @param[in] handle
+ * File handle.
+ * @param[in] edfsignal
+ * The zero-based index of the signal.
+ * @param[in] n
+ * Number of samples to read. The sample position indicator will be increased with the same amount.
+ * @param[out] buf
+ * Pointer to a buffer, size must be equal to, or bigger than, sizeof(double[n])
+ *
+ * @return
+ * The number of samples read (this can be less than \p n or zero!) or -1 in case of an error
  */
+EDFLIB_API int edfread_digital_samples(int handle, int edfsignal, int n, int *buf);
 
-EDFLIB_API long long edfseek(int handle, int edfsignal, long long offset, int whence);
-/* The edfseek() function sets the sample position indicator for the edfsignal pointed to by edfsignal.
- * The new position, measured in samples, is obtained by adding offset samples to the position specified by whence.
- * If whence is set to EDFSEEK_SET, EDFSEEK_CUR, or EDFSEEK_END, the offset is relative to the start of the file,
+/**
+ * Sets the sample position indicator for the edfsignal pointed to by \p edfsignal.
+ * The new position, measured in samples, is obtained by adding offset samples to the position specified by \p whence.
+ * If \p whence is set to EDFSEEK_SET, EDFSEEK_CUR, or EDFSEEK_END, the offset is relative to the start of the file,
  * the current position indicator, or end-of-file, respectively.
- * Returns the current offset. Otherwise, -1 is returned.
- * note that every signal has it's own independent sample position indicator and edfseek() affects only one of them
+ * Note that every signal has it's own independent sample position indicator and \p edfseek() affects only one of them.
+ *
+ * @param[in] handle
+ * File handle.
+ * @param[in] edfsignal
+ * The zero-based index of the signal.
+ * @param[in] offset
+ * Offset measured in samples.
+ * @param[in] whence
+ * Reference for \p offset:
+ * - EDFSEEK_SET start of the file
+ * - EDFSEEK_CUR current position
+ * - EDFSEEK_END end of the file
+ *
+ * @return
+ * The current offset or -1 in case of an error.
  */
+EDFLIB_API long long edfseek(int handle, int edfsignal, long long offset, int whence);
 
-EDFLIB_API long long edftell(int handle, int edfsignal);
-/* The edftell() function obtains the current value of the sample position indicator for the edfsignal pointed to by edfsignal.
- * Returns the current offset. Otherwise, -1 is returned
- * note that every signal has it's own independent sample position indicator and edftell() affects only one of them
+/**
+ * Obtains the current value of the sample position indicator for the edfsignal pointed to by \p edfsignal.
+ * Note that every signal has it's own independent sample position indicator and \p edftell() affects only one of them.
+ *
+ * @param[in] handle
+ * File handle.
+ * @param[in] edfsignal
+ * The zero-based index of the signal.
+ *
+ * @return
+ * The current offset or -1 in case of an error.
  */
+EDFLIB_API long long edftell(int handle, int edfsignal);
 
-EDFLIB_API void edfrewind(int handle, int edfsignal);
-/* The edfrewind() function sets the sample position indicator for the edfsignal pointed to by edfsignal to the beginning of the file.
- * It is equivalent to: (void) edfseek(int handle, int edfsignal, 0LL, EDFSEEK_SET)
- * note that every signal has it's own independent sample position indicator and edfrewind() affects only one of them
+/**
+ * Sets the sample position indicator for the edfsignal pointed to by \p edfsignal to the beginning of the file.
+ * It is equivalent to: \p edfseek(handle, edfsignal, 0LL, EDFSEEK_SET).
+ * Note that every signal has it's own independent sample position indicator and \p edfrewind() affects only one of them.
+ *
+ * @param[in] handle
+ * File handle.
+ * @param[in] edfsignal
+ * The zero-based index of the signal.
+ *
+ * @return
+ * 0 on success or -1 in case of an error.
  */
+EDFLIB_API int edfrewind(int handle, int edfsignal);
 
-EDFLIB_API int edf_get_annotation(int handle, int n, edflib_annotation_t *annot);
-/* Fills the edflib_annotation_t structure with the annotation n, returns 0 on success, otherwise -1
- * The string that describes the annotation/event is encoded in UTF-8
+/**
+ * Fills the edflib_annotation_t structure with the annotation \p n.
+ * The string that describes the annotation/event is encoded in UTF-8.
  * To obtain the number of annotations in a file, check edf_hdr_struct -> annotations_in_file.
- * returns 0 on success or -1 in case of an error
+ *
+ * @param[in] handle
+ * File handle.
+ * @param[in] n
+ * The zero-based index number of the list of annotations.
+ * @param[out] annot
+ * Pointer to a struct that will be filled with the annotation.
+ *
+ * @return
+ * 0 on success or -1 in case of an error.
  */
+EDFLIB_API int edf_get_annotation(int handle, int n, edflib_annotation_t *annot);
 
 /*****************  the following functions are used in read and write mode **************************/
 
-EDFLIB_API int edfclose_file(int handle);
-/* closes (and in case of writing, finalizes) the file
- * returns -1 in case of an error, 0 on success
- * this function MUST be called when you are finished reading or writing
+/**
+ * Closes (and in case of writing, finalizes) the file.
+ *
+ * This function MUST be called when you have finished reading or writing
  * This function is required after reading or writing. Failing to do so will cause
- * unnecessary memory usage and in case of writing it will cause a corrupted and incomplete file
+ * unnecessary memory usage and in case of writing it will cause a corrupted or incomplete file.
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @return
+ * 0 on success or -1 in case of an error.
  */
+EDFLIB_API int edfclose_file(int handle);
 
-EDFLIB_API int edflib_version(void);
-/* Returns the version number of this library, multiplied by hundred. if version is "1.00" than it will return 100
+/**
+ * Returns the version number of this library, multiplied by hundred. if version is "1.00" then it will return 100.
+ *
+ * @return
+ * The version number.
  */
+EDFLIB_API int edflib_version(void);
 
-EDFLIB_API int edflib_is_file_used(const char *path);
-/* returns 1 if the file is used, either for reading or writing, otherwise returns 0
+/**
+ * Returns 1 if the file is in use, either for reading or writing, otherwise returns 0.
+ *
+ * @param[in] path
+ * Pointer to a null-terminated string that contains the path to the file.
+ *
+ * @return
+ * 1 if the file is in use (either for reading or writing), otherwise 0.
  */
+EDFLIB_API int edflib_is_file_used(const char *path);
 
-EDFLIB_API int edflib_get_number_of_open_files(void);
-/* returns the number of open files, either for reading or writing
+/**
+ * Returns the number of open files.
+ *
+ * @return
+ * The number of open files, either for reading or writing.
  */
+EDFLIB_API int edflib_get_number_of_open_files(void);
 
-EDFLIB_API int edflib_get_handle(int file_number);
-/* returns the handle of an opened file, either for reading or writing
- * file_number is zero based (starts with 0)
- * returns -1 if the file is not opened
+/**
+ * Returns the handle of an open file, either for reading or writing.
+ *
+ * @param[in] file_number
+ * A zero based index number of the list of open files.
+ *
+ * @return
+ * The file handle or -1 if the file_number >= number of open files.
  */
+EDFLIB_API int edflib_get_handle(int file_number);
 
 /*****************  the following functions are used to write files **************************/
 
-EDFLIB_API int edfopen_file_writeonly(const char *path, int filetype, int number_of_signals);
-/* opens an new file for writing. warning, an already existing file with the same name will be silently overwritten without advance warning!
- * path is a null-terminated string containing the path and name of the file
- * filetype must be EDFLIB_FILETYPE_EDFPLUS or EDFLIB_FILETYPE_BDFPLUS
- * returns a handle on success, you need this handle for the other functions
- * in case of an error it returns a negative number corresponding to one of the following values:
- * EDFLIB_MALLOC_ERROR
- * EDFLIB_NO_SUCH_FILE_OR_DIRECTORY
- * EDFLIB_MAXFILES_REACHED
- * EDFLIB_FILE_ALREADY_OPENED
- * EDFLIB_NUMBER_OF_SIGNALS_INVALID
- * EDFLIB_ARCH_ERROR
+/**
+ * Opens an new file for writing. Warning: an already existing file with the same name will be silently overwritten without advance warning!<br>
  * This function is required if you want to write a file (or use edfopen_file_writeonly_with_params())
+ *
+ * @param[in] path
+ * A null-terminated string containing the path and name of the file
+ *
+ * @param[in] filetype
+ * Must be EDFLIB_FILETYPE_EDFPLUS or EDFLIB_FILETYPE_BDFPLUS.
+ *
+ * @param[in] number_of_signals
+ * The number of signals you want to store into the file<br>
+ * (excluding annotation signals, the library will take care of that).
+ *
+ * @return
+ * A file handle on success or a negative number in case of an error:
+ * - EDFLIB_MALLOC_ERROR
+ * - EDFLIB_NO_SUCH_FILE_OR_DIRECTORY
+ * - EDFLIB_MAXFILES_REACHED
+ * - EDFLIB_FILE_ALREADY_OPENED
+ * - EDFLIB_NUMBER_OF_SIGNALS_INVALID
+ * - EDFLIB_ARCH_ERROR
  */
+EDFLIB_API int edfopen_file_writeonly(const char *path, int filetype, int number_of_signals);
 
-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);
-/* this is a convenience function that can create a new EDF file and initializes the most important parameters.
- * it assumes that all signals are sharing the same parameters (you can still change them though).
- * warning, an already existing file with the same name will be silently overwritten without advance warning!
- * path is a null-terminated string containing the path and name of the file
- * filetype must be EDFLIB_FILETYPE_EDFPLUS or EDFLIB_FILETYPE_BDFPLUS
- * Sets the sample frequency of all signals. (In reality, it sets the number of samples per datarecord which equals the sample frequency only when
- * the datarecords have a duration of 1 second)
- * Sets the physical maximum of all signals to phys_max_min.
- * Sets the physical minimum of all signals to -phys_max_min.
- * Sets the physical dimension (unit) of all signals ("uV", "BPM", "mA", "Degr.", etc.).
- * phys_dim is a pointer to a NULL-terminated ASCII-string containing the physical dimension of the signals
- * returns a handle on success, you need this handle for the other functions
- * in case of an error it returns a negative number corresponding to one of the following values:
- * EDFLIB_MALLOC_ERROR
- * EDFLIB_NO_SUCH_FILE_OR_DIRECTORY
- * EDFLIB_MAXFILES_REACHED
- * EDFLIB_FILE_ALREADY_OPENED
- * EDFLIB_NUMBER_OF_SIGNALS_INVALID
- * EDFLIB_ARCH_ERROR
- * This function is required if you want to write a file (or use edfopen_file_writeonly())
+/**
+ * This is a convenience function that can create a new EDF file and initializes the most important parameters.<br>
+ * It assumes that all signals are sharing the same parameters (you can still change them though).<br>
+ * Warning: an already existing file with the same name will be silently overwritten without advance warning!<br>
+ *
+ * @param[in] path
+ * A null-terminated string containing the path and name of the file.
+ *
+ * @param[in] filetype
+ * Must be EDFLIB_FILETYPE_EDFPLUS or EDFLIB_FILETYPE_BDFPLUS.
+ *
+ * @param[in] number_of_signals
+ * The number of signals you want to store into the file<br>
+ * (excluding annotation signals, the library will take care of that).
+ *
+ * @param[in] samplefrequency
+ * Sample frequency for all signals. (In reality, it sets the number of samples per datarecord which equals the sample frequency only when<br>
+ * the datarecords have a duration of one second which is the default here.)
+ *
+ * @param[in] phys_max_min
+ * Physical maximum and minimum for all signals.
+ *
+ * @param[in] phys_dim
+ * Pointer to a NULL-terminated ASCII-string containing the physical dimension (unit) for all signals ("uV", "BPM", "mA", "Degr.", etc.).
+ *
+ * @return
+ * A file handle on success or a negative number in case of an error:
+ * - EDFLIB_MALLOC_ERROR
+ * - EDFLIB_NO_SUCH_FILE_OR_DIRECTORY
+ * - EDFLIB_MAXFILES_REACHED
+ * - EDFLIB_FILE_ALREADY_OPENED
+ * - EDFLIB_NUMBER_OF_SIGNALS_INVALID
+ * - EDFLIB_ARCH_ERROR
  */
+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 int edf_set_samplefrequency(int handle, int edfsignal, int samplefrequency);
-/* Sets the sample frequency of signal edfsignal. In reality, it sets the number of samples in a datarecord
- * which equals the sample frequency only when the datarecords have a duration of 1 second.
- * The effective sample frequency is: samplefrequency / datarecord duration
- * Returns 0 on success, otherwise -1
- * This function is required for every signal and can be called only after opening a
- * file in write mode and before the first sample write action
+/**
+ * Sets the sample frequency of signal edfsignal. In reality, it sets the number of samples in a datarecord<br>
+ * which equals the sample frequency only when the datarecords have a duration of one second.<br>
+ * The effective sample frequency is: samplefrequency / datarecord duration<br>
+ * This function is required for every signal (except when using edfopen_file_writeonly_with_params()) and can be called<br>
+ * only after opening a file in write mode and before the first sample write action.<br>
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] edfsignal
+ * The zero-based index of the signal.
+ *
+ * @param[in] samplefrequency
+ * Sample frequency, must be > 0;
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edf_set_samplefrequency(int handle, int edfsignal, int samplefrequency);
 
-EDFLIB_API int edf_set_physical_maximum(int handle, int edfsignal, double phys_max);
-/* Sets the maximum physical value of signal edfsignal. (the value of the input of the ADC when the output equals the value of "digital maximum")
- * It is the highest value that the equipment is able to record. It does not necessarily mean the signal recorded reaches this level
- * Must be un-equal to physical minimum
- * Returns 0 on success, otherwise -1
- * This function is required for every signal and can be called only after opening a
- * file in write mode and before the first sample write action
+/**
+ * Sets the maximum physical value of signal edfsignal. (the value of the input of the ADC when the output equals the value of "digital maximum")<br>
+ * It is the highest value that the equipment is able to record. It does not necessarily mean the signal recorded reaches this level.<br>
+ * In other words, it is the highest value that CAN occur in the recording.<br>
+ * Must be un-equal to physical minimum.<br>
+ * This function is required for every signal (except when using edfopen_file_writeonly_with_params()) and can be called<br>
+ * only after opening a file in write mode and before the first sample write action.<br>
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] edfsignal
+ * The zero-based index of the signal.
+ *
+ * @param[in] phys_max
+ * Physical maximum, must be != physical minimum;
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edf_set_physical_maximum(int handle, int edfsignal, double phys_max);
 
-EDFLIB_API int edf_set_physical_minimum(int handle, int edfsignal, double phys_min);
-/* Sets the minimum physical value of signal edfsignal. (the value of the input of the ADC when the output equals the value of "digital minimum")
- * It is the lowest value that the equipment is able to record. It does not necessarily mean the signal recorded reaches this level
- * Usually this will be (-(phys_max))
- * Must be un-equal to physical maximum
- * Returns 0 on success, otherwise -1
- * This function is required for every signal and can be called only after opening a
- * file in write mode and before the first sample write action
+/**
+ * Sets the minimum physical value of signal edfsignal. (the value of the input of the ADC when the output equals the value of "digital minimum")<br>
+ * It is the lowest value that the equipment is able to record. It does not necessarily mean the signal recorded reaches this level.<br>
+ * In other words, it is the lowest value that CAN occur in the recording.<br>
+ * Must be un-equal to physical maximum.<br>
+ * This function is required for every signal (except when using edfopen_file_writeonly_with_params()) and can be called<br>
+ * only after opening a file in write mode and before the first sample write action.<br>
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] edfsignal
+ * The zero-based index of the signal.
+ *
+ * @param[in] phys_min
+ * Physical minimum, must be != physical maximum;
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edf_set_physical_minimum(int handle, int edfsignal, double phys_min);
 
-EDFLIB_API int edf_set_digital_maximum(int handle, int edfsignal, int dig_max);
-/* Sets the maximum digital value of signal edfsignal. The maximum value is 32767 for EDF+ and 8388607 for BDF+
- * It is the highest value that the equipment is able to record. It does not necessarily mean the signal recorded reaches this level
- * Usually it's the extreme output of the ADC
- * Must be higher than digital minimum
- * Returns 0 on success, otherwise -1
- * This function is required for every signal and can be called only after opening a file in write mode
- * and before the first sample write action
+/**
+ * Sets the maximum digital value of signal edfsignal. The maximum value is 32767 for EDF+ and 8388607 for BDF+.<br>
+ * It is the highest value that the equipment is able to record. It does not necessarily mean the signal recorded reaches this level.<br>
+ * In other words, it is the highest value that CAN occur in the recording.<br>
+ * Must be higher than digital minimum.<br>
+ * This function is required for every signal (except when using edfopen_file_writeonly_with_params()) and can be called<br>
+ * only after opening a file in write mode and before the first sample write action.<br>
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] edfsignal
+ * The zero-based index of the signal.
+ *
+ * @param[in] dig_max
+ * Digital maximum, must be > digital minimum;
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edf_set_digital_maximum(int handle, int edfsignal, int dig_max);
 
-EDFLIB_API int edf_set_digital_minimum(int handle, int edfsignal, int dig_min);
-/* Sets the minimum digital value of signal edfsignal. The minimum value is -32768 for EDF+ and -8388608 for BDF+
- * It is the lowest value that the equipment is able to record. It does not necessarily mean the signal recorded reaches this level
- * Usually it's the extreme output of the ADC
- * Usually this will be (-(dig_max + 1))
- * Must be lower than digital maximum
- * Returns 0 on success, otherwise -1
- * This function is required for every signal and can be called only after opening a file in write mode
- * and before the first sample write action
+/**
+ * Sets the minimum digital value of signal edfsignal. The minimum value is -32768 for EDF+ and -8388608 for BDF+.<br>
+ * It is the lowest value that the equipment is able to record. It does not necessarily mean the signal recorded reaches this level.<br>
+ * In other words, it is the lowest value that CAN occur in the recording.<br>
+ * Must be lower than digital maximum.<br>
+ * This function is required for every signal (except when using edfopen_file_writeonly_with_params()) and can be called<br>
+ * only after opening a file in write mode and before the first sample write action.<br>
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] edfsignal
+ * The zero-based index of the signal.
+ *
+ * @param[in] dig_min
+ * Digital minimum, must be < digital maximum;
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edf_set_digital_minimum(int handle, int edfsignal, int dig_min);
 
-EDFLIB_API int edf_set_label(int handle, int edfsignal, const char *label);
-/* Sets the label (name) of signal edfsignal. ("FP1", "SaO2", etc.)
- * label is a pointer to a NULL-terminated ASCII-string containing the label (name) of the signal edfsignal
- * Returns 0 on success, otherwise -1
- * This function is recommended for every signal when you want to write a file
- * and can be called only after opening a file in write mode and before the first sample write action
+/**
+ * Sets the label (name) of signal \p edfsignal. ("EEG FP1", "SaO2", etc.).<br>
+ * This function is recommended for every signal when you want to write a file<br>
+ * and can be called only after opening a file in write mode and before the first sample write action.<br>
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] edfsignal
+ * The zero-based index of the signal.
+ *
+ * @param[in] label
+ * A pointer to a NULL-terminated ASCII-string containing the label (name) of the signal \p edfsignal.
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edf_set_label(int handle, int edfsignal, const char *label);
 
-EDFLIB_API int edf_set_prefilter(int handle, int edfsignal, const char *prefilter);
-/* Sets the prefilter of signal edfsignal ("HP:0.1Hz", "LP:75Hz N:50Hz", etc.).
- * prefilter is a pointer to a NULL-terminated ASCII-string containing the prefilter text of the signal edfsignal
- * Returns 0 on success, otherwise -1
- * This function is optional and can be called only after opening a file in write mode and before
- * the first sample write action
+/**
+ * Sets the prefilter of signal \p edfsignal e.g. "HP:0.1Hz", "LP:75Hz N:50Hz", etc.<br>
+ * This function is optional and can be called only after opening a file in writemode and<br>
+ * before the first sample write action.
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] edfsignal
+ * The zero-based index of the signal.
+ *
+ * @param[in] prefilter
+ * A pointer to a NULL-terminated ASCII-string containing the prefilter text of the signal \p edfsignal.
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edf_set_prefilter(int handle, int edfsignal, const char *prefilter);
 
-EDFLIB_API int edf_set_transducer(int handle, int edfsignal, const char *transducer);
-/* Sets the transducer of signal edfsignal ("AgAgCl cup electrodes", etc.).
- * transducer is a pointer to a NULL-terminated ASCII-string containing the transducer text of the signal edfsignal
- * Returns 0 on success, otherwise -1
- * This function is optional and can be called only after opening a file in writemode and before
- * the first sample write action
+/**
+ * Sets the transducer of signal \p edfsignal e.g. "AgAgCl cup electrodes", etc.<br>
+ * This function is optional and can be called only after opening a file in writemode and<br>
+ * before the first sample write action.
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] edfsignal
+ * The zero-based index of the signal.
+ *
+ * @param[in] transducer
+ * A pointer to a NULL-terminated ASCII-string containing the transducer text of the signal \p edfsignal.
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edf_set_transducer(int handle, int edfsignal, const char *transducer);
 
-EDFLIB_API int edf_set_physical_dimension(int handle, int edfsignal, const char *phys_dim);
-/* Sets the physical dimension (unit) of signal edfsignal. ("uV", "BPM", "mA", "Degr.", etc.)
- * phys_dim is a pointer to a NULL-terminated ASCII-string containing the physical dimension of the signal edfsignal
- * Returns 0 on success, otherwise -1
- * This function is recommended for every signal when you want to write a file
- * and can be called only after opening a file in write mode and before the first sample write action
+/**
+ * Sets the physical dimension (unit) of signal \p edfsignal. ("uV", "BPM", "mA", "Degr.", etc.).<br>
+ * This function is recommended for every signal when you want to write a file<br>
+ * and can be called only after opening a file in write mode and before the first sample write action.<br>
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] edfsignal
+ * The zero-based index of the signal.
+ *
+ * @param[in] phys_dim
+ * A pointer to a NULL-terminated ASCII-string containing the physical dimension (unit) of the signal \p edfsignal.
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edf_set_physical_dimension(int handle, int edfsignal, const char *phys_dim);
 
-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);
-/* Sets the startdate and starttime.
- * year: 1985 - 2084, month: 1 - 12, day: 1 - 31
- * hour: 0 - 23, minute: 0 - 59, second: 0 - 59
- * If not called, the library will use the system date and time at runtime
- * Returns 0 on success, otherwise -1
- * This function is optional and can be called only after opening a file in write mode
- * and before the first sample write action
+/**
+ * Sets the startdate and starttime.<br>
+ * If not called, the library will use the system date and time at runtime.<br>
+ * This function is optional and can be called only after opening a file in write mode<br>
+ * and before the first sample write action.<br>
  * Note: for anonymization purposes, the consensus is to use 1985-01-01 00:00:00 for the startdate and starttime.
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] startdate_year
+ * 1985 - 2084 inclusive
+ *
+ * @param[in] startdate_month
+ * 1 - 12 inclusive
+ *
+ * @param[in] startdate_day
+ * 1 - 31 inclusive
+ *
+ * @param[in] starttime_hour
+ * 0 - 23 inclusive
+ *
+ * @param[in] starttime_minute
+ * 0 - 59 inclusive
+ *
+ * @param[in] starttime_second
+ * 0 - 59 inclusive
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+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_patientname(int handle, const char *patientname);
-/* Sets the patientname. patientname is a pointer to a null-terminated ASCII-string.
- * Returns 0 on success, otherwise -1
- * This function is optional and can be called only after opening a file in writemode
- * and before the first sample write action
+/**
+ * Sets the subject name<br>
+ * This function is optional and can be called only after opening a file in writemode and<br>
+ * before the first sample write action.
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] patientname
+ * A pointer to a NULL-terminated ASCII-string containing the subject name.
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edf_set_patientname(int handle, const char *patientname);
 
-EDFLIB_API int edf_set_patientcode(int handle, const char *patientcode);
-/* Sets the patientcode. patientcode is a pointer to a null-terminated ASCII-string.
- * Returns 0 on success, otherwise -1
- * This function is optional and can be called only after opening a file in writemode
- * and before the first sample write action
+/**
+ * Sets the subject code<br>
+ * This function is optional and can be called only after opening a file in writemode and<br>
+ * before the first sample write action.
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] patientcode
+ * A pointer to a NULL-terminated ASCII-string containing the subject code.
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edf_set_patientcode(int handle, const char *patientcode);
 
-EDFLIB_API int edf_set_sex(int handle, int sex);
-/* Sets the sex. 1 is male, 0 is female.
- * Returns 0 on success, otherwise -1
- * This function is optional and can be called only after opening a file in writemode
- * and before the first sample write action
+/**
+ * Sets the sex of the subject. 1 is male, 0 is female.<br>
+ * This function is optional and can be called only after opening a file in writemode<br>
+ * and before the first sample write action.<br>
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] sex
+ * 1: male, 0: female.
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edf_set_sex(int handle, int sex);
 
 #if defined(__GNUC__)
 EDFLIB_API int edf_set_gender(int handle, int sex) __attribute__ ((deprecated ("use edf_set_sex()")));
@@ -523,149 +813,283 @@ EDFLIB_API int edf_set_gender(int handle, int sex);
  * and before the first sample write action
  */
 
-EDFLIB_API int edf_set_birthdate(int handle, int birthdate_year, int birthdate_month, int birthdate_day);
-/* Sets the birthdate.
- * year: 1800 - 3000, month: 1 - 12, day: 1 - 31
- * This function is optional
- * Returns 0 on success, otherwise -1
- * This function is optional and can be called only after opening a file in writemode
- * and before the first sample write action
+/**
+ * Sets the subject birthdate.<br>
+ * This function is optional and can be called only after opening a file in write mode<br>
+ * and before the first sample write action.<br>
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] birthdate_year
+ * 1800 - 3000 inclusive
+ *
+ * @param[in] birthdate_month
+ * 1 - 12 inclusive
+ *
+ * @param[in] birthdate_day
+ * 1 - 31 inclusive
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edf_set_birthdate(int handle, int birthdate_year, int birthdate_month, int birthdate_day);
 
-EDFLIB_API int edf_set_patient_additional(int handle, const char *patient_additional);
-/* Sets the additional patientinfo. patient_additional is a pointer to a null-terminated ASCII-string.
- * Returns 0 on success, otherwise -1
- * This function is optional and can be called only after opening a file in writemode
- * and before the first sample write action
+/**
+ * Sets the additional subject info<br>
+ * This function is optional and can be called only after opening a file in writemode and<br>
+ * before the first sample write action.
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] patient_additional
+ * A pointer to a NULL-terminated ASCII-string containing the additional subject info.
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edf_set_patient_additional(int handle, const char *patient_additional);
 
-EDFLIB_API int edf_set_admincode(int handle, const char *admincode);
-/* Sets the admincode. admincode is a pointer to a null-terminated ASCII-string.
- * Returns 0 on success, otherwise -1
- * This function is optional and can be called only after opening a file in writemode
- * and before the first sample write action
+/**
+ * Sets the administration code<br>
+ * This function is optional and can be called only after opening a file in writemode and<br>
+ * before the first sample write action.
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] admincode
+ * A pointer to a NULL-terminated ASCII-string containing the administration code.
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edf_set_admincode(int handle, const char *admincode);
 
-EDFLIB_API int edf_set_technician(int handle, const char *technician);
-/* Sets the technicians name. technician is a pointer to a null-terminated ASCII-string.
- * Returns 0 on success, otherwise -1
- * This function is optional and can be called only after opening a file in writemode
- * and before the first sample write action
+/**
+ * Sets the technicians name or code<br>
+ * This function is optional and can be called only after opening a file in writemode and<br>
+ * before the first sample write action.
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] technician
+ * A pointer to a NULL-terminated ASCII-string containing the technicians name or code.
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edf_set_technician(int handle, const char *technician);
 
-EDFLIB_API int edf_set_equipment(int handle, const char *equipment);
-/* Sets the name of the equipment used during the acquisition. equipment is a pointer to a null-terminated ASCII-string.
- * Returns 0 on success, otherwise -1
- * This function is optional and can be called only after opening a file in writemode
- * and before the first sample write action
+/**
+ * Sets the equipment brand and/or model<br>
+ * This function is optional and can be called only after opening a file in writemode and<br>
+ * before the first sample write action.
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] equipment
+ * A pointer to a NULL-terminated ASCII-string containing the equipment brand and/or model<br>
+ * used for the recording.
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edf_set_equipment(int handle, const char *equipment);
 
-EDFLIB_API int edf_set_recording_additional(int handle, const char *recording_additional);
-/* Sets the additional recordinginfo. recording_additional is a pointer to a null-terminated ASCII-string.
- * Returns 0 on success, otherwise -1
- * This function is optional and can be called only after opening a file in writemode
- * and before the first sample write action
+/**
+ * Sets the additional info about the recording.<br>
+ * This function is optional and can be called only after opening a file in writemode and<br>
+ * before the first sample write action.
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] recording_additional
+ * A pointer to a NULL-terminated ASCII-string containing the additional info about the recording.
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edf_set_recording_additional(int handle, const char *recording_additional);
 
-EDFLIB_API int edfwrite_physical_samples(int handle, double *buf);
-/* Writes n physical samples (uV, mA, Ohm) from *buf belonging to one signal
- * where n is the samplefrequency of that signal.
- * The physical samples will be converted to digital samples using the
- * values of physical maximum, physical minimum, digital maximum and digital minimum
- * The number of samples written is equal to the samplefrequency of the signal
- * Size of buf should be equal to or bigger than sizeof(double[samplefrequency])
- * Call this function for every signal in the file. The order is important!
- * When there are 4 signals in the file,  the order of calling this function
- * must be: signal 0, signal 1, signal 2, signal 3, signal 0, signal 1, signal 2, etc.
- * Returns 0 on success, otherwise -1
+/**
+ * Writes n physical samples (uV, mA, Ohm) from \p buf belonging to one signal<br>
+ * where n is the samplefrequency of that signal.<br>
+ * Actually, n equals the number of samples per datarecord which equals the samplefrequency only<br>
+ * when the datarecord duration has the default value of one second!<br>
+ * The physical samples will be converted to digital samples using the<br>
+ * values of physical maximum, physical minimum, digital maximum and digital minimum.<br>
+ * Size of \p buf must be equal to or bigger than sizeof(double[samples per datarecord]).<br>
+ * Call this function for every signal in the file. The order is important:<br>
+ * When there are 4 signals in the file,  the order of calling this function<br>
+ * must be: signal 0, signal 1, signal 2, signal 3, signal 0, signal 1, signal 2, etc.<br>
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] buf
+ * A pointer to a buffer containing the samples.
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edfwrite_physical_samples(int handle, double *buf);
 
-EDFLIB_API int edf_blockwrite_physical_samples(int handle, double *buf);
-/* Writes physical samples (uV, mA, Ohm) from *buf
- * buf must be filled with samples from all signals, starting with n samples of signal 0, n samples of signal 1, n samples of signal 2, etc.
- * where n is the samplefrequency of that signal.
- * buf must be filled with samples from all signals, starting with signal 0, 1, 2, etc.
- * one block equals one second
- * The physical samples will be converted to digital samples using the
- * values of physical maximum, physical minimum, digital maximum and digital minimum
- * The number of samples written is equal to the sum of the samplefrequencies of all signals
- * Size of buf should be equal to or bigger than sizeof(double) multiplied by the sum of the samplefrequencies of all signals
- * Returns 0 on success, otherwise -1
+/**
+ * Writes physical samples (uV, mA, Ohm) from \p buf <br>
+ * \p buf must be filled with samples from all signals, starting with n samples of signal 0, n samples of signal 1, n samples of signal 2, etc.<br>
+ * where n is the samplefrequency of that signal.<br>
+ * Actually, n equals the number of samples per datarecord which equals the samplefrequency only<br>
+ * when the datarecord duration has the default value of one second!<br>
+ * The physical samples will be converted to digital samples using the<br>
+ * values of physical maximum, physical minimum, digital maximum and digital minimum.<br>
+ * The number of samples written equals the sum of the samples per datarecord of all signals.
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] buf
+ * A pointer to a buffer containing the samples.
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edf_blockwrite_physical_samples(int handle, double *buf);
 
-EDFLIB_API int edfwrite_digital_short_samples(int handle, short *buf);
-/* Writes n "raw" digital samples from *buf belonging to one signal
- * where n is the samplefrequency of that signal.
- * The samples will be written to the file without any conversion.
- * Because the size of a short is 16-bit, do not use this function with BDF (24-bit)
- * The number of samples written is equal to the samplefrequency of the signal
- * Size of buf should be equal to or bigger than sizeof(short[samplefrequency])
- * Call this function for every signal in the file. The order is important!
- * When there are 4 signals in the file,  the order of calling this function
- * must be: signal 0, signal 1, signal 2, signal 3, signal 0, signal 1, signal 2, etc.
- * Returns 0 on success, otherwise -1
+/**
+ * Writes n "raw" digital samples from \p buf belonging to one signal<br>
+ * where n is the samplefrequency of that signal.<br>
+ * Actually, n equals the number of samples per datarecord which equals the samplefrequency only<br>
+ * when the datarecord duration has the default value of one second!<br>
+ * Size of \p buf should be equal to or bigger than sizeof(short[samples per datarecord]).<br>
+ * Call this function for every signal in the file. The order is important:<br>
+ * When there are 4 signals in the file,  the order of calling this function<br>
+ * must be: signal 0, signal 1, signal 2, signal 3, signal 0, signal 1, signal 2, etc.<br>
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] buf
+ * A pointer to a buffer containing the samples.
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edfwrite_digital_short_samples(int handle, short *buf);
 
-EDFLIB_API int edfwrite_digital_samples(int handle, int *buf);
-/* Writes n "raw" digital samples from *buf belonging to one signal
- * where n is the samplefrequency of that signal.
- * The 16 (or 24 in case of BDF) least significant bits of the sample will be written to the
- * file without any conversion.
- * The number of samples written is equal to the samplefrequency of the signal
- * Size of buf should be equal to or bigger than sizeof(int[samplefrequency])
- * Call this function for every signal in the file. The order is important!
- * When there are 4 signals in the file,  the order of calling this function
- * must be: signal 0, signal 1, signal 2, signal 3, signal 0, signal 1, signal 2, etc.
- * Returns 0 on success, otherwise -1
+/**
+ * Writes n "raw" digital samples from \p buf belonging to one signal<br>
+ * where n is the samplefrequency of that signal.<br>
+ * Actually, n equals the number of samples per datarecord which equals the samplefrequency only<br>
+ * when the datarecord duration has the default value of one second!<br>
+ * The 16 (or 24 in case of BDF+) least significant bits of the samples will be written to the<br>
+ * file without any conversion.<br>
+ * Size of \p buf should be equal to or bigger than sizeof(int[samples per datarecord]).<br>
+ * Call this function for every signal in the file. The order is important:<br>
+ * When there are 4 signals in the file,  the order of calling this function<br>
+ * must be: signal 0, signal 1, signal 2, signal 3, signal 0, signal 1, signal 2, etc.<br>
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] buf
+ * A pointer to a buffer containing the samples.
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edfwrite_digital_samples(int handle, int *buf);
 
-EDFLIB_API int edf_blockwrite_digital_3byte_samples(int handle, void *buf);
-/* Writes "raw" digital samples from *buf.
- * buf must be filled with samples from all signals, starting with n samples of signal 0, n samples of signal 1, n samples of signal 2, etc.
- * where n is the samplefrequency of that signal.
- * One block equals one second. One sample equals 3 bytes, order is little endian (least significant byte first)
- * Encoding is second's complement, most significant bit of most significant byte is the sign-bit
- * The samples will be written to the file without any conversion.
- * Because the size of a 3-byte sample is 24-bit, this function can only be used when writing a BDF file
- * The number of samples written is equal to the sum of the samplefrequencies of all signals.
- * Size of buf should be equal to or bigger than: the sum of the samplefrequencies of all signals x 3 bytes
- * Returns 0 on success, otherwise -1
+/**
+ * Writes "raw" digital samples from \p buf <br>
+ * \p buf must be filled with samples from all signals, starting with n samples of signal 0, n samples of signal 1, n samples of signal 2, etc.<br>
+ * where n is the samplefrequency of that signal.<br>
+ * Actually, n equals the number of samples per datarecord which equals the samplefrequency only<br>
+ * when the datarecord duration has the default value of one second!<br>
+ * One sample equals 3 bytes, order is little endian (least significant byte first).<br>
+ * Encoding is second's complement, most significant bit of most significant byte is the sign-bit.<br>
+ * Because the size of a 3-byte sample is 24-bit, this function can only be used when writing a BDF+ file.<br>
+ * The number of samples written equals the sum of the samples per datarecord of all signals.
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] buf
+ * A pointer to a buffer containing the samples.
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edf_blockwrite_digital_3byte_samples(int handle, void *buf);
 
-EDFLIB_API int edf_blockwrite_digital_short_samples(int handle, short *buf);
-/* Writes "raw" digital samples from *buf.
- * buf must be filled with samples from all signals, starting with n samples of signal 0, n samples of signal 1, n samples of signal 2, etc.
- * where n is the samplefrequency of that signal.
- * One block equals one second.
- * The samples will be written to the file without any conversion.
- * Because the size of a short is 16-bit, do not use this function with BDF (24-bit)
- * The number of samples written is equal to the sum of the samplefrequencies of all signals.
- * Size of buf should be equal to or bigger than sizeof(short) multiplied by the sum of the samplefrequencies of all signals
- * Returns 0 on success, otherwise -1
+/**
+ * Writes "raw" digital samples from \p buf <br>
+ * \p buf must be filled with samples from all signals, starting with n samples of signal 0, n samples of signal 1, n samples of signal 2, etc.<br>
+ * where n is the samplefrequency of that signal.<br>
+ * Actually, n equals the number of samples per datarecord which equals the samplefrequency only<br>
+ * when the datarecord duration has the default value of one second!<br>
+ * One sample equals 2 bytes, order is little endian (least significant byte first).<br>
+ * Encoding is second's complement, most significant bit of most significant byte is the sign-bit.<br>
+ * Because the size of a 2-byte sample is 16-bit, this function can only be used when writing an EDF+ file.<br>
+ * The number of samples written equals the sum of the samples per datarecord of all signals.
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] buf
+ * A pointer to a buffer containing the samples.
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edf_blockwrite_digital_short_samples(int handle, short *buf);
 
-EDFLIB_API int edf_blockwrite_digital_samples(int handle, int *buf);
-/* Writes "raw" digital samples from *buf.
- * buf must be filled with samples from all signals, starting with n samples of signal 0, n samples of signal 1, n samples of signal 2, etc.
- * where n is the samplefrequency of that signal.
- * One block equals one second.
- * The 16 (or 24 in case of BDF) least significant bits of the sample will be written to the
- * file without any conversion.
- * The number of samples written is equal to the sum of the samplefrequencies of all signals.
- * Size of buf should be equal to or bigger than sizeof(int) multiplied by the sum of the samplefrequencies of all signals
- * Returns 0 on success, otherwise -1
+/**
+ * Writes "raw" digital samples from \p buf <br>
+ * \p buf must be filled with samples from all signals, starting with n samples of signal 0, n samples of signal 1, n samples of signal 2, etc.<br>
+ * where n is the samplefrequency of that signal.<br>
+ * Actually, n equals the number of samples per datarecord which equals the samplefrequency only<br>
+ * when the datarecord duration has the default value of one second!<br>
+ * The 16 (or 24 in case of BDF+) least significant bits of the samples will be written to the<br>
+ * file without any conversion.<br>
+ * The number of samples written equals the sum of the samples per datarecord of all signals.
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] buf
+ * A pointer to a buffer containing the samples.
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edf_blockwrite_digital_samples(int handle, int *buf);
 
-EDFLIB_API int edfwrite_annotation_utf8_hr(int handle, long long onset, long long duration, const char *description);
-/* writes an annotation/event to the file
- * onset is relative to the start of the file
- * onset and duration are in units of 1 microSecond     resolution is 0.000001 second
- * for example: 34.071 seconds must be written as 34071000
- * if duration is unknown or not applicable: set a negative number (-1)
- * description is a null-terminated UTF8-string containing the text that describes the event
- * This function is optional and can be called only after opening a file in writemode
- * and before closing the file
+/**
+ * Writes an annotation/event to the file.<br>
+ * This function is optional and can be called only after opening a file in writemode<br>
+ * and before closing the file.<br>
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] onset
+ * microseconds since start of recording.
+ *
+ * @param[in] duration
+ * microseconds, > 0 or -1 if not used.
+ *
+ * @param[in] description
+ * A null-terminated UTF8-string containing the text that describes the event.
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edfwrite_annotation_utf8_hr(int handle, long long onset, long long duration, const char *description);
 
 #if defined(__GNUC__)
 EDFLIB_API int edfwrite_annotation_utf8(int handle, long long onset, long long duration, const char *description) __attribute__ ((deprecated ("use edfwrite_annotation_utf8_hr()")));
@@ -675,7 +1099,7 @@ EDFLIB_API int edfwrite_annotation_utf8(int handle, long long onset, long long d
 /* DEPRECATED!! USE edfwrite_annotation_utf8_hr()
  * writes an annotation/event to the file
  * onset is relative to the start of the file
- * onset and duration are in units of 100 microSeconds!     resolution is 0.0001 second!
+ * onset and duration are in units of 100 microseconds!     resolution is 0.0001 second!
  * for example: 34.071 seconds must be written as 340710
  * if duration is unknown or not applicable: set a negative number (-1)
  * description is a null-terminated UTF8-string containing the text that describes the event
@@ -683,16 +1107,27 @@ EDFLIB_API int edfwrite_annotation_utf8(int handle, long long onset, long long d
  * and before closing the file
  */
 
-EDFLIB_API int edfwrite_annotation_latin1_hr(int handle, long long onset, long long duration, const char *description);
-/* writes an annotation/event to the file
- * onset is relative to the start of the file
- * onset and duration are in units of 1 microSecond     resolution is 0.000001 second
- * for example: 34.071 seconds must be written as 34071000
- * if duration is unknown or not applicable: set a negative number (-1)
- * description is a null-terminated Latin1-string containing the text that describes the event
- * This function is optional and can be called only after opening a file in writemode
- * and before closing the file
+/**
+ * Writes an annotation/event to the file.<br>
+ * This function is optional and can be called only after opening a file in writemode<br>
+ * and before closing the file.
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] onset
+ * microseconds since start of recording.
+ *
+ * @param[in] duration
+ * microseconds, > 0 or -1 if not used.
+ *
+ * @param[in] description
+ * A null-terminated Latin1-string containing the text that describes the event.
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edfwrite_annotation_latin1_hr(int handle, long long onset, long long duration, const char *description);
 
 #if defined(__GNUC__)
 EDFLIB_API int edfwrite_annotation_latin1(int handle, long long onset, long long duration, const char *description) __attribute__ ((deprecated ("use edfwrite_annotation_latin1_hr()")));
@@ -702,7 +1137,7 @@ EDFLIB_API int edfwrite_annotation_latin1(int handle, long long onset, long long
 /* DEPRECATED!! USE edfwrite_annotation_latin1_hr()
  * writes an annotation/event to the file
  * onset is relative to the start of the file
- * onset and duration are in units of 100 microSeconds!     resolution is 0.0001 second!
+ * onset and duration are in units of 100 microseconds!     resolution is 0.0001 second!
  * for example: 34.071 seconds must be written as 340710
  * if duration is unknown or not applicable: set a negative number (-1)
  * description is a null-terminated Latin1-string containing the text that describes the event
@@ -710,59 +1145,110 @@ EDFLIB_API int edfwrite_annotation_latin1(int handle, long long onset, long long
  * and before closing the file
  */
 
-EDFLIB_API int edf_set_datarecord_duration(int handle, int duration);
-/* Sets the datarecord duration. The default value is 1 second.
- * ATTENTION: the argument "duration" is expressed in units of 10 microSeconds!
- * So, if you want to set the datarecord duration to 0.1 second, you must give
- * the argument "duration" a value of "10000".
- * This function is optional, normally you don't need to change the default value.
- * The datarecord duration must be in the range 0.001 to 60 seconds.
- * Returns 0 on success, otherwise -1
- * This function is NOT REQUIRED but can be called after opening a
+/**
+ * Sets the datarecord duration. The default value is 1 second.<br>
+ * ATTENTION: the argument \p duration is expressed in units of 10 microseconds.<br>
+ * So, if you want to set the datarecord duration to 0.1 second, you must write a value of 10000.<br>
+ * The datarecord duration must be in the range 0.001 to 60 seconds.<br>
+ * This function can be used when you want to use a samplerate<br>
+ * which is not an integer. For example, if you want to use a samplerate of 0.5 Hz,<br>
+ * set the samplefrequency to 5 Hz and the datarecord duration to 10 seconds,<br>
+ * or set the samplefrequency to 1 Hz and the datarecord duration to 2 seconds.<br>
+ * This function is optional and can be called after opening a<br>
  * file in writemode and before the first sample write action.
- * This function can be used when you want to use a samplerate
- * which is not an integer. For example, if you want to use a samplerate of 0.5 Hz,
- * set the samplefrequency to 5 Hz and the datarecord duration to 10 seconds,
- * or set the samplefrequency to 1 Hz and the datarecord duration to 2 seconds.
- * Do not use this function if not necessary.
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] duration
+ * Datarecord duration expressed in units of 10 microSecond.
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edf_set_datarecord_duration(int handle, int duration);
 
-EDFLIB_API int edf_set_micro_datarecord_duration(int handle, int duration);
-/* Sets the datarecord duration to a very small value.
- * ATTENTION: the argument "duration" is expressed in units of 1 microSecond!
- * This function is optional, normally you don't need to change the default value.
- * The datarecord duration must be in the range 1 to 9999 micro-seconds.
- * Returns 0 on success, otherwise -1
- * This function is NOT REQUIRED but can be called after opening a
+/**
+ * Sets the datarecord duration to a very small value.<br>
+ * ATTENTION: the argument \p duration is expressed in units of 1 microSecond.<br>
+ * The datarecord duration must be in the range 1 to 9999 microseconds.<br>
+ * This function can be used when you want to use a very high samplerate.<br>
+ * For example, if you want to use a samplerate of 5 GHz,<br>
+ * set the samplefrequency to 5000 Hz and the datarecord duration to 1 micro-second.<br>
+ * Do not use this function if not necessary.<br>
+ * This function was added to accommodate for high speed ADC's e.g. Digital Sampling Oscilloscopes<br>
+ * This function is optional and can be called after opening a<br>
  * file in writemode and before the first sample write action.
- * This function can be used when you want to use a very high samplerate.
- * For example, if you want to use a samplerate of 5 GHz,
- * set the samplefrequency to 5000 Hz and the datarecord duration to 1 micro-second.
- * Do not use this function if not necessary.
- * This function was added to accommodate for high speed ADC's e.g. Digital Sampling Oscilloscopes
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] duration
+ * Datarecord duration expressed in units of 10 microSecond.
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edf_set_micro_datarecord_duration(int handle, int duration);
 
-EDFLIB_API int edf_set_number_of_annotation_signals(int handle, int annot_signals);
-/* Sets the number of annotation signals. The default value is 1
- * This function is optional and can be called only after opening a file in writemode
- * and before the first sample write action
- * Normally you don't need to change the default value. Only when the number of annotations
- * you want to write is higher than the number of datarecords in the recording, you can use
- * this function to increase the storage space for annotations
- * Minimum is 1, maximum is 64
- * Returns 0 on success, otherwise -1
+/**
+ * Sets the number of annotation signals. The default value is 1.<br>
+ * This function is optional and can be called only after opening a file in writemode<br>
+ * and before the first sample write action.<br>
+ * Normally you don't need to change the default value. Only when the number of annotations<br>
+ * you expect to write is more than the number of datarecords in the recording, you can use<br>
+ * this function to increase the storage space for annotations.<br>
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] annot_signals
+ * Number of annotation signals, must be in the range 1 - 64 inclusive.
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edf_set_number_of_annotation_signals(int handle, int annot_signals);
 
+/**
+ * Sets the subsecond starttime expressed in units of 100 nanoseconds.<br>
+ * Valid range is 0 to 9999999 inclusive. Default is 0.<br>
+ * This function is optional and can be called only after opening a file in writemode<br>
+ * and before the first sample write action.<br>
+ * It is recommended to use a maximum resolution of not more than 100 microseconds.<br>
+ * E.g. use 1234000  to set a starttime offset of 0.1234 seconds (instead of for example 1234217).<br>
+ * In other words, leave the last 3 digits at zero.<br>
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] subsecond
+ * Subsecond starttime expressed in units of 100 nanoseconds.
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
+ */
 EDFLIB_API int edf_set_subsecond_starttime(int handle, int subsecond);
-/* Sets the subsecond starttime expressed in units of 100 nanoseconds
- * Valid range is 0 to 9999999 inclusive. Default is 0
- * This function is optional and can be called only after opening a file in writemode
- * and before the first sample write action
- * Returns 0 on success, otherwise -1
- * It is strongly recommended to use a maximum resolution of no more than 100 micro-Seconds.
- * e.g. use 1234000  to set a starttime offset of 0.1234 seconds (instead of 1234567)
- * in other words, leave the last 3 digits at zero
+
+/**
+ * Sets the preferred position of the annotation channels(s) before, after or in the middle of the list<br>
+ * of regular signals. The default is to put them at the end (after the regular signals).<br>
+ * This function is optional and can be called only after opening a file in writemode<br>
+ * and before the first sample write action.<br>
+ *
+ * @param[in] handle
+ * File handle.
+ *
+ * @param[in] pos
+ * Preferred position of the annotation channel(s):<br>
+ * EDF_ANNOT_IDX_POS_START<br>
+ * EDF_ANNOT_IDX_POS_MIDDLE<br>
+ * EDF_ANNOT_IDX_POS_END<br>
+ *
+ * @return
+ * 0 on success, otherwise -1.<br>
  */
+EDFLIB_API int edf_set_annot_chan_idx_pos(int handle, int pos);
 
 #ifdef __cplusplus
 } /* extern "C" */


=====================================
lib/README
=====================================
@@ -1,5 +1,5 @@
 
-This makefile creates a shared libary of EDFlib.
+This makefile creates a shared library of EDFlib.
 
 usage:
 


=====================================
lib/makefile
=====================================
@@ -9,16 +9,16 @@
 #
 # https://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html
 #
-# nm -D libedf.so.1.2.4
+# nm -D libedf.so.1.2.5
 #
-# objdump -T libedf.so.1.2.4
+# objdump -T libedf.so.1.2.5
 #
 
 CC := gcc
 CFLAGS := -O2 -fpic -fvisibility=hidden -std=gnu11 -Wall -Wextra -Wshadow -Wformat-nonliteral -Wformat-security \
           -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE -DEDFLIB_SO_DLL -DEDFLIB_BUILD
 LDLIBS := -lc
-TARGET := libedf.so.1.2.4
+TARGET := libedf.so.1.2.5
 
 LBITS := $(shell getconf LONG_BIT)
 


=====================================
sine_generator.c
=====================================
@@ -1,7 +1,7 @@
 /*
 *****************************************************************************
 *
-* Copyright (c) 2009 - 2021 Teunis van Beelen
+* Copyright (c) 2009 - 2024 Teunis van Beelen
 * All rights reserved.
 *
 * Email: teuniz at protonmail.com
@@ -145,6 +145,13 @@ int main(void)
     }
   }
 
+  if(edf_set_annot_chan_idx_pos(hdl, EDF_ANNOT_IDX_POS_START))
+  {
+    printf("error: edf_set_annot_chan_idx_pos()\n");
+
+    return(1);
+  }
+
   for(j=0; j<10; j++)
   {
     for(i=0; i<SMP_FREQ; i++)


=====================================
sweep_generator.c
=====================================
@@ -1,7 +1,7 @@
 /*
 *****************************************************************************
 *
-* Copyright (c) 2009 - 2021 Teunis van Beelen
+* Copyright (c) 2009 - 2024 Teunis van Beelen
 * All rights reserved.
 *
 * Email: teuniz at protonmail.com


=====================================
test_edflib.c
=====================================
@@ -1,7 +1,7 @@
 /*
 *****************************************************************************
 *
-* Copyright (c) 2009 - 2021 Teunis van Beelen
+* Copyright (c) 2009 - 2024 Teunis van Beelen
 * All rights reserved.
 *
 * Email: teuniz at protonmail.com


=====================================
test_generator.c
=====================================
@@ -1,7 +1,7 @@
 /*
 *****************************************************************************
 *
-* Copyright (c) 2009 - 2021 Teunis van Beelen
+* Copyright (c) 2009 - 2024 Teunis van Beelen
 * All rights reserved.
 *
 * Email: teuniz at protonmail.com
@@ -378,13 +378,26 @@ int main(void)
 
   if(edf_set_equipment(hdl, "test generator"))
   {
-    printf("edf_set_equipment()\n");
+    printf("error: edf_set_equipment()\n");
 
     return(1);
   }
 
   edf_set_birthdate(hdl, 1969, 6, 30);
 
+  if(edf_set_annot_chan_idx_pos(hdl, EDF_ANNOT_IDX_POS_MIDDLE))
+  {
+    printf("error: edf_set_annot_chan_idx_pos()\n");
+
+    return(1);
+  }
+
+  if(edf_set_number_of_annotation_signals(hdl, 2))
+  {
+    printf("error: edf_set_number_of_annotation_signals()\n");
+
+    return(1);
+  }
 
   sine_1 = 0.0;
   sine_8 = 0.0;


=====================================
unittest/LICENSE
=====================================
@@ -1,7 +1,7 @@
                     GNU GENERAL PUBLIC LICENSE
                        Version 3, 29 June 2007
 
- Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
  Everyone is permitted to copy and distribute verbatim copies
  of this license document, but changing it is not allowed.
 
@@ -645,7 +645,7 @@ the "copyright" line and a pointer to where the full notice is found.
     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, see <http://www.gnu.org/licenses/>.
+    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
 Also add information on how to contact you by electronic and paper mail.
 
@@ -664,11 +664,11 @@ might be different; for a GUI interface, you would use an "about box".
   You should also get your employer (if you work as a programmer) or school,
 if any, to sign a "copyright disclaimer" for the program, if necessary.
 For more information on this, and how to apply and follow the GNU GPL, see
-<http://www.gnu.org/licenses/>.
+<https://www.gnu.org/licenses/>.
 
   The GNU General Public License does not permit incorporating your program
 into proprietary programs.  If your program is a subroutine library, you
 may consider it more useful to permit linking proprietary applications with
 the library.  If this is what you want to do, use the GNU Lesser General
 Public License instead of this License.  But first, please read
-<http://www.gnu.org/philosophy/why-not-lgpl.html>.
+<https://www.gnu.org/licenses/why-not-lgpl.html>.


=====================================
unittest/unittest.c
=====================================
@@ -3,7 +3,7 @@
 *
 * Author: Teunis van Beelen
 *
-* Copyright (C) 2017- 2023 Teunis van Beelen
+* Copyright (C) 2017- 2024 Teunis van Beelen
 *
 * Email: teuniz at protonmail.com
 *
@@ -20,7 +20,7 @@
 * 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, see <http://www.gnu.org/licenses/>.
+* along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 ***************************************************************************
 */
@@ -36,16 +36,29 @@
 
 #define  JUMP_TO_EXIT_ERROR_PROC   {line = __LINE__; goto OUT_ERROR;}
 
-#define EDFLIB_ANNOTATION_BYTES  (120)
-
 #if defined(__GNUC__)
 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
 #endif
 
+typedef struct raw_header_struct
+{
+  int total_chans;
+  int regular_chans;
+  int annot_chans;
+  int regular_chans_idx_list[EDFLIB_MAXSIGNALS];
+  int annot_chans_idx_list[EDFLIB_MAXSIGNALS];
+  int bytes_in_datrec[EDFLIB_MAXSIGNALS];
+  int datrec_offset[EDFLIB_MAXSIGNALS];
+  int hdr_sz;
+  int datrecs;
+  int datrec_sz;
+  int sample_width;
+} raw_hdr_t;
 
 int dblcmp(double, double);
 int dblcmp_lim(double, double, double);
-
+int get_raw_header(const char *, raw_hdr_t *);
+int get_file_offset(int, int, int, raw_hdr_t *);
 
 
 int main(void)
@@ -60,7 +73,8 @@ int main(void)
       ival2;
 
   char *str=NULL,
-       *pbuf=NULL;
+       *pbuf=NULL,
+       *i3buf=NULL;
 
   short *sbuf=NULL;
 
@@ -68,13 +82,17 @@ int main(void)
 
   double *dbuf=NULL;
 
-  union {
-          unsigned int one;
-          signed int one_signed;
-          unsigned short two[2];
-          signed short two_signed[2];
-          unsigned char four[4];
-        } var;
+  raw_hdr_t rawhdr;
+
+  union
+  {
+    unsigned int one;
+    signed int one_signed;
+    unsigned short two[2];
+    signed short two_signed[2];
+    unsigned char four[4];
+    signed char four_signed[4];
+  } var;
 
   edflib_hdr_t hdr;
 
@@ -84,21 +102,21 @@ int main(void)
 
   setlocale(LC_ALL, "C");
 
-  if(edflib_version() != 124)  JUMP_TO_EXIT_ERROR_PROC
+  if(edflib_version() != 125)  JUMP_TO_EXIT_ERROR_PROC
 
-  ibuf = (int *)malloc(100 * sizeof(int));
+  ibuf = (int *)malloc(50000 * sizeof(int));
   if(ibuf == NULL)
   {
     JUMP_TO_EXIT_ERROR_PROC;
   }
 
-  sbuf = (short *)malloc(100 * sizeof(short));
+  sbuf = (short *)malloc(50000 * sizeof(short));
   if(sbuf == NULL)
   {
     JUMP_TO_EXIT_ERROR_PROC;
   }
 
-  dbuf = (double *)malloc(10240 * sizeof(double));
+  dbuf = (double *)malloc(50000 * sizeof(double));
   if(dbuf == NULL)
   {
     JUMP_TO_EXIT_ERROR_PROC;
@@ -117,18 +135,269 @@ int main(void)
     JUMP_TO_EXIT_ERROR_PROC;
   }
 
+  i3buf = (char *)malloc(50000 * 3);
+  if(i3buf == NULL)
+  {
+    JUMP_TO_EXIT_ERROR_PROC;
+  }
+
+/********************************** BDF writing ******************************/
+
+  hdl = edfopen_file_writeonly_with_params("test.bdf", EDFLIB_FILETYPE_BDFPLUS, 17, 2799, 300000, "uV");
+
+  if(hdl < 0)  JUMP_TO_EXIT_ERROR_PROC
+
+  if(edf_set_annot_chan_idx_pos(hdl, EDF_ANNOT_IDX_POS_START))  JUMP_TO_EXIT_ERROR_PROC
+
+  if(edf_set_number_of_annotation_signals(hdl, 3))  JUMP_TO_EXIT_ERROR_PROC
+
+  for(i=0; i<2799; i++)
+  {
+    dbuf[i] = i;
+  }
+
+  for(i=0; i<25; i++)
+  {
+    for(j=0; j<17; j++)
+    {
+      if(edfwrite_physical_samples(hdl, dbuf))  JUMP_TO_EXIT_ERROR_PROC
+    }
+  }
+
+  for(i=0; i<75; i++)
+  {
+    snprintf(str, 4096, "test %i", i + 1);
+
+    if(edfwrite_annotation_latin1_hr(hdl, i * 1000000, -1, str))  JUMP_TO_EXIT_ERROR_PROC
+  }
+
+  if(edfclose_file(hdl))
+  {
+    hdl = -1;
+
+    JUMP_TO_EXIT_ERROR_PROC
+  }
+
+/********************************** BDF reading ******************************/
+
+  if(get_raw_header("test.bdf", &rawhdr))  JUMP_TO_EXIT_ERROR_PROC
+
+  if(rawhdr.annot_chans != 3)  JUMP_TO_EXIT_ERROR_PROC
+
+  for(i=0; i<rawhdr.annot_chans; i++)
+  {
+    if(rawhdr.annot_chans_idx_list[i] != i)  JUMP_TO_EXIT_ERROR_PROC
+  }
+
+  if(edfopen_file_readonly("test.bdf", &hdr, EDFLIB_READ_ALL_ANNOTATIONS))  JUMP_TO_EXIT_ERROR_PROC
+
+  hdl = hdr.handle;
+
+  if(hdr.filetype != 3)  JUMP_TO_EXIT_ERROR_PROC
+
+  if(hdr.edfsignals != 17)  JUMP_TO_EXIT_ERROR_PROC
+
+  if(hdr.file_duration != 250000000)  JUMP_TO_EXIT_ERROR_PROC
+
+  if(hdr.datarecord_duration != 10000000)  JUMP_TO_EXIT_ERROR_PROC
+
+  if(hdr.datarecords_in_file != 25)  JUMP_TO_EXIT_ERROR_PROC
+
+  if(hdr.signalparam[0].smp_in_file != 69975)  JUMP_TO_EXIT_ERROR_PROC
+
+  if(hdr.signalparam[0].phys_max != 300000)  JUMP_TO_EXIT_ERROR_PROC
+
+  if(hdr.signalparam[0].phys_min != -300000)  JUMP_TO_EXIT_ERROR_PROC
+
+  if(hdr.signalparam[0].dig_max != 8388607)  JUMP_TO_EXIT_ERROR_PROC
+
+  if(hdr.signalparam[0].dig_min != -8388608)  JUMP_TO_EXIT_ERROR_PROC
+
+  if(hdr.signalparam[0].smp_in_datarecord != 2799)  JUMP_TO_EXIT_ERROR_PROC
+
+  if(strcmp(hdr.signalparam[0].physdimension, "uV      "))  JUMP_TO_EXIT_ERROR_PROC
+
+  if(strcmp(hdr.patientcode, ""))  JUMP_TO_EXIT_ERROR_PROC
+
+  if(strcmp(hdr.patient_name, "X"))  JUMP_TO_EXIT_ERROR_PROC
+
+  if(strcmp(hdr.admincode, ""))  JUMP_TO_EXIT_ERROR_PROC
+
+  if(strcmp(hdr.technician, ""))  JUMP_TO_EXIT_ERROR_PROC
+
+  if(strcmp(hdr.equipment, ""))  JUMP_TO_EXIT_ERROR_PROC
+
+  if(hdr.annotations_in_file != 75)  JUMP_TO_EXIT_ERROR_PROC
+
+  if(edfclose_file(hdl))
+  {
+    hdl = -1;
+
+    JUMP_TO_EXIT_ERROR_PROC
+  }
+
+  hdl = -1;
+
+/********************************** BDF writing ******************************/
+
+  hdl = edfopen_file_writeonly_with_params("test.bdf", EDFLIB_FILETYPE_BDFPLUS, 17, 2799, 300000, "uV");
+
+  if(hdl < 0)  JUMP_TO_EXIT_ERROR_PROC
+
+  if(edf_set_annot_chan_idx_pos(hdl, EDF_ANNOT_IDX_POS_MIDDLE))  JUMP_TO_EXIT_ERROR_PROC
+
+  if(edf_set_number_of_annotation_signals(hdl, 2))  JUMP_TO_EXIT_ERROR_PROC
+
+  for(j=0; j<17; j++)
+  {
+    for(i=0; i<2799; i++)
+    {
+      dbuf[(j * 2799) + i] = i;
+    }
+  }
+
+  for(i=0; i<5; i++)
+  {
+    for(j=0; j<17; j++)
+    {
+      if(edfwrite_physical_samples(hdl, dbuf))  JUMP_TO_EXIT_ERROR_PROC
+    }
+  }
+
+  for(i=0; i<5; i++)
+  {
+    if(edf_blockwrite_physical_samples(hdl, dbuf))  JUMP_TO_EXIT_ERROR_PROC
+  }
+
+  for(j=0; j<17; j++)
+  {
+    for(i=0; i<2799; i++)
+    {
+      ibuf[i + (j * 2799)] = (i * -27.96202667) + 0.5;
+    }
+  }
+
+  for(i=0; i<5; i++)
+  {
+    for(j=0; j<17; j++)
+    {
+      if(edfwrite_digital_samples(hdl, ibuf))  JUMP_TO_EXIT_ERROR_PROC
+    }
+  }
+
+  for(i=0; i<5; i++)
+  {
+    if(edf_blockwrite_digital_samples(hdl, ibuf))  JUMP_TO_EXIT_ERROR_PROC
+  }
+
+  for(j=0; j<17; j++)
+  {
+    for(i=0; i<2799; i++)
+    {
+      var.one_signed = (i * 27.96202667) + 0.5;
+
+      i3buf[(i * 3) + (j * 2799 * 3) + 0] = var.four[0];
+      i3buf[(i * 3) + (j * 2799 * 3) + 1] = var.four[1];
+      i3buf[(i * 3) + (j * 2799 * 3) + 2] = var.four[2];
+    }
+  }
+
+  for(i=0; i<5; i++)
+  {
+    if(edf_blockwrite_digital_3byte_samples(hdl, i3buf))  JUMP_TO_EXIT_ERROR_PROC
+  }
+
+  for(i=0; i<50; i++)
+  {
+    snprintf(str, 4096, "test %i", i + 1);
+
+    if(edfwrite_annotation_latin1_hr(hdl, i * 1000000, -1, str))  JUMP_TO_EXIT_ERROR_PROC
+  }
+
+  if(edfclose_file(hdl))
+  {
+    hdl = -1;
+
+    JUMP_TO_EXIT_ERROR_PROC
+  }
+
+/********************************** BDF reading ******************************/
+
+  if(get_raw_header("test.bdf", &rawhdr))  JUMP_TO_EXIT_ERROR_PROC
+
+  if(rawhdr.annot_chans != 2)  JUMP_TO_EXIT_ERROR_PROC
+
+  for(i=0; i<rawhdr.annot_chans; i++)
+  {
+    if(rawhdr.annot_chans_idx_list[i] != i + 8)  JUMP_TO_EXIT_ERROR_PROC
+  }
+
+  if(edfopen_file_readonly("test.bdf", &hdr, EDFLIB_READ_ALL_ANNOTATIONS))  JUMP_TO_EXIT_ERROR_PROC
+
+  hdl = hdr.handle;
+
+  if(hdr.filetype != 3)  JUMP_TO_EXIT_ERROR_PROC
+
+  if(hdr.edfsignals != 17)  JUMP_TO_EXIT_ERROR_PROC
+
+  if(hdr.file_duration != 250000000)  JUMP_TO_EXIT_ERROR_PROC
+
+  if(hdr.datarecord_duration != 10000000)  JUMP_TO_EXIT_ERROR_PROC
+
+  if(hdr.datarecords_in_file != 25)  JUMP_TO_EXIT_ERROR_PROC
+
+  if(hdr.signalparam[0].smp_in_file != 69975)  JUMP_TO_EXIT_ERROR_PROC
+
+  if(hdr.signalparam[0].phys_max != 300000)  JUMP_TO_EXIT_ERROR_PROC
+
+  if(hdr.signalparam[0].phys_min != -300000)  JUMP_TO_EXIT_ERROR_PROC
+
+  if(hdr.signalparam[0].dig_max != 8388607)  JUMP_TO_EXIT_ERROR_PROC
+
+  if(hdr.signalparam[0].dig_min != -8388608)  JUMP_TO_EXIT_ERROR_PROC
+
+  if(hdr.signalparam[0].smp_in_datarecord != 2799)  JUMP_TO_EXIT_ERROR_PROC
+
+  if(strcmp(hdr.signalparam[0].physdimension, "uV      "))  JUMP_TO_EXIT_ERROR_PROC
+
+  if(strcmp(hdr.patientcode, ""))  JUMP_TO_EXIT_ERROR_PROC
+
+  if(strcmp(hdr.patient_name, "X"))  JUMP_TO_EXIT_ERROR_PROC
+
+  if(strcmp(hdr.admincode, ""))  JUMP_TO_EXIT_ERROR_PROC
+
+  if(strcmp(hdr.technician, ""))  JUMP_TO_EXIT_ERROR_PROC
+
+  if(strcmp(hdr.equipment, ""))  JUMP_TO_EXIT_ERROR_PROC
+
+  if(hdr.annotations_in_file != 50)  JUMP_TO_EXIT_ERROR_PROC
+
+  if(edfclose_file(hdl))
+  {
+    hdl = -1;
+
+    JUMP_TO_EXIT_ERROR_PROC
+  }
+
+  hdl = -1;
+
 /********************************** EDF writing ******************************/
 
   hdl = edfopen_file_writeonly_with_params("test.edf", EDFLIB_FILETYPE_EDFPLUS, 65, 633, 3000, "uV");
 
   if(hdl < 0)  JUMP_TO_EXIT_ERROR_PROC
 
-  for(i=0; i<633; i++)
+  if(edf_set_annot_chan_idx_pos(hdl, EDF_ANNOT_IDX_POS_MIDDLE))  JUMP_TO_EXIT_ERROR_PROC
+
+  for(j=0; j<65; j++)
   {
-    dbuf[i] = i;
+    for(i=0; i<633; i++)
+    {
+      dbuf[(j * 633) + i] = i;
+    }
   }
 
-  for(i=0; i<10; i++)
+  for(i=0; i<5; i++)
   {
     for(j=0; j<65; j++)
     {
@@ -136,6 +405,53 @@ int main(void)
     }
   }
 
+  for(i=0; i<5; i++)
+  {
+    if(edf_blockwrite_physical_samples(hdl, dbuf))  JUMP_TO_EXIT_ERROR_PROC
+  }
+
+  for(j=0; j<65; j++)
+  {
+    for(i=0; i<633; i++)
+    {
+      ibuf[i + (j * 633)] = (i * -10.92266667) + 0.5;
+    }
+  }
+
+  for(i=0; i<5; i++)
+  {
+    for(j=0; j<65; j++)
+    {
+      if(edfwrite_digital_samples(hdl, ibuf))  JUMP_TO_EXIT_ERROR_PROC
+    }
+  }
+
+  for(i=0; i<5; i++)
+  {
+    if(edf_blockwrite_digital_samples(hdl, ibuf))  JUMP_TO_EXIT_ERROR_PROC
+  }
+
+  for(j=0; j<65; j++)
+  {
+    for(i=0; i<633; i++)
+    {
+      sbuf[i + (j * 633)] = (i * -10.92266667) + 0.5;
+    }
+  }
+
+  for(i=0; i<5; i++)
+  {
+    for(j=0; j<65; j++)
+    {
+      if(edfwrite_digital_short_samples(hdl, sbuf))  JUMP_TO_EXIT_ERROR_PROC
+    }
+  }
+
+  for(i=0; i<5; i++)
+  {
+    if(edf_blockwrite_digital_short_samples(hdl, sbuf))  JUMP_TO_EXIT_ERROR_PROC
+  }
+
   if(edfclose_file(hdl))
   {
     hdl = -1;
@@ -153,13 +469,13 @@ int main(void)
 
   if(hdr.edfsignals != 65)  JUMP_TO_EXIT_ERROR_PROC
 
-  if(hdr.file_duration != 100000000)  JUMP_TO_EXIT_ERROR_PROC
+  if(hdr.file_duration != 300000000)  JUMP_TO_EXIT_ERROR_PROC
 
   if(hdr.datarecord_duration != 10000000)  JUMP_TO_EXIT_ERROR_PROC
 
-  if(hdr.datarecords_in_file != 10)  JUMP_TO_EXIT_ERROR_PROC
+  if(hdr.datarecords_in_file != 30)  JUMP_TO_EXIT_ERROR_PROC
 
-  if(hdr.signalparam[0].smp_in_file != 6330)  JUMP_TO_EXIT_ERROR_PROC
+  if(hdr.signalparam[0].smp_in_file != 18990)  JUMP_TO_EXIT_ERROR_PROC
 
   if(hdr.signalparam[0].phys_max != 3000)  JUMP_TO_EXIT_ERROR_PROC
 
@@ -1600,13 +1916,10 @@ int main(void)
   fseek(fp, 0xad, SEEK_SET);
 
   fputc('.', fp);
-#if (EDFLIB_ANNOTATION_BYTES == 114)
-  fseek(fp, 0x803, SEEK_SET);
-#endif
-#if (EDFLIB_ANNOTATION_BYTES == 120)
-  fseek(fp, 0x815, SEEK_SET);
-#endif
-  fwrite("0.12", 4, 1, fp);
+
+  fseek(fp, 0x5fe, SEEK_SET);
+
+  fputc('z', fp);
 
   fclose(fp);
 
@@ -1619,13 +1932,22 @@ int main(void)
   fp = fopen("test.edf", "r+b");
 
   if(fp == NULL)  JUMP_TO_EXIT_ERROR_PROC
-#if (EDFLIB_ANNOTATION_BYTES == 114)
-  fseek(fp, 0x803, SEEK_SET);
-#endif
-#if (EDFLIB_ANNOTATION_BYTES == 120)
-  fseek(fp, 0x815, SEEK_SET);
-#endif
-  fwrite("0.131", 5, 1, fp);
+
+  fseek(fp, 0x5fe, SEEK_SET);
+
+  fputc(' ', fp);
+
+  fclose(fp);
+
+  if(get_raw_header("test.edf", &rawhdr))  JUMP_TO_EXIT_ERROR_PROC
+
+  fp = fopen("test.edf", "r+b");
+
+//  printf("offset: %i\n", get_file_offset(1, 0, 1, &rawhdr));
+
+  fseek(fp, get_file_offset(1, 0, 1, &rawhdr) + 1, SEEK_SET);
+
+  fwrite("0.12", 4, 1, fp);
 
   fclose(fp);
 
@@ -1636,21 +1958,29 @@ int main(void)
   /****************************************/
 
   fp = fopen("test.edf", "r+b");
+  if(fp == NULL)  JUMP_TO_EXIT_ERROR_PROC
+
+  fseek(fp, get_file_offset(1, 0, 1, &rawhdr) + 1, SEEK_SET);
+
+  fwrite("0.131", 5, 1, fp);
+
+  fclose(fp);
+
+  if(edfopen_file_readonly("test.edf", &hdr, EDFLIB_READ_ALL_ANNOTATIONS) == 0)  JUMP_TO_EXIT_ERROR_PROC
 
+  if(hdr.filetype != EDFLIB_FILE_CONTAINS_FORMAT_ERRORS)  JUMP_TO_EXIT_ERROR_PROC
+
+  /****************************************/
+
+  fp = fopen("test.edf", "r+b");
   if(fp == NULL)  JUMP_TO_EXIT_ERROR_PROC
-#if (EDFLIB_ANNOTATION_BYTES == 114)
-  fseek(fp, 0x803, SEEK_SET);
-#endif
-#if (EDFLIB_ANNOTATION_BYTES == 120)
-  fseek(fp, 0x815, SEEK_SET);
-#endif
+
+  fseek(fp, get_file_offset(1, 0, 1, &rawhdr) + 1, SEEK_SET);
+
   fwrite("0.130", 5, 1, fp);
-#if (EDFLIB_ANNOTATION_BYTES == 114)
-  fseek(fp, 0x802, SEEK_SET);
-#endif
-#if (EDFLIB_ANNOTATION_BYTES == 120)
-  fseek(fp, 0x814, SEEK_SET);
-#endif
+
+  fseek(fp, get_file_offset(1, 0, 1, &rawhdr), SEEK_SET);
+
   fputc('0', fp);
 
   fclose(fp);
@@ -1662,14 +1992,10 @@ int main(void)
   /****************************************/
 
   fp = fopen("test.edf", "r+b");
-
   if(fp == NULL)  JUMP_TO_EXIT_ERROR_PROC
-#if (EDFLIB_ANNOTATION_BYTES == 114)
-  fseek(fp, 0x802, SEEK_SET);
-#endif
-#if (EDFLIB_ANNOTATION_BYTES == 120)
-  fseek(fp, 0x814, SEEK_SET);
-#endif
+
+  fseek(fp, get_file_offset(1, 0, 1, &rawhdr), SEEK_SET);
+
   fputc('-', fp);
 
   fclose(fp);
@@ -1681,21 +2007,14 @@ int main(void)
   /****************************************/
 
   fp = fopen("test.edf", "r+b");
-
   if(fp == NULL)  JUMP_TO_EXIT_ERROR_PROC
-#if (EDFLIB_ANNOTATION_BYTES == 114)
-  fseek(fp, 0x802, SEEK_SET);
-#endif
-#if (EDFLIB_ANNOTATION_BYTES == 120)
-  fseek(fp, 0x814, SEEK_SET);
-#endif
+
+  fseek(fp, get_file_offset(1, 0, 1, &rawhdr), SEEK_SET);
+
   fputc('+', fp);
-#if (EDFLIB_ANNOTATION_BYTES == 114)
-  fseek(fp, 0x750, SEEK_SET);
-#endif
-#if (EDFLIB_ANNOTATION_BYTES == 120)
-  fseek(fp, 0x75e, SEEK_SET);
-#endif
+
+  fseek(fp, get_file_offset(0, 2, 1, &rawhdr) + 24, SEEK_SET);
+
   fputc(0, fp);
 
   fclose(fp);
@@ -1709,12 +2028,9 @@ int main(void)
   fp = fopen("test.edf", "r+b");
 
   if(fp == NULL)  JUMP_TO_EXIT_ERROR_PROC
-#if (EDFLIB_ANNOTATION_BYTES == 114)
-  fseek(fp, 0x750, SEEK_SET);
-#endif
-#if (EDFLIB_ANNOTATION_BYTES == 120)
-  fseek(fp, 0x75e, SEEK_SET);
-#endif
+
+  fseek(fp, get_file_offset(0, 2, 1, &rawhdr) + 24, SEEK_SET);
+
   fputc(0x14, fp);
 
   fputc(1, fp);
@@ -1728,14 +2044,40 @@ int main(void)
   /****************************************/
 
   fp = fopen("test.edf", "r+b");
+  if(fp == NULL)  JUMP_TO_EXIT_ERROR_PROC
+
+  fseek(fp, get_file_offset(0, 2, 1, &rawhdr) + 25, SEEK_SET);
+
+  fputc(0, fp);
+
+  fseek(fp, get_file_offset(0, 2, 1, &rawhdr) + 20, SEEK_SET);
+
+  fputc(3, fp);
+
+  fclose(fp);
+
+  if(edfopen_file_readonly("test.edf", &hdr, EDFLIB_READ_ALL_ANNOTATIONS) == 0)  JUMP_TO_EXIT_ERROR_PROC
+
+  fp = fopen("test.edf", "r+b");
+  if(fp == NULL)  JUMP_TO_EXIT_ERROR_PROC
+
+  fseek(fp, get_file_offset(0, 2, 1, &rawhdr) + 20, SEEK_SET);
+
+  fputc('e', fp);
+
+  fseek(fp, get_file_offset(0, 2, 1, &rawhdr) + 32, SEEK_SET);
+
+  fputc(3, fp);
+
+  fclose(fp);
+
+  if(edfopen_file_readonly("test.edf", &hdr, EDFLIB_READ_ALL_ANNOTATIONS) == 0)  JUMP_TO_EXIT_ERROR_PROC
 
+  fp = fopen("test.edf", "r+b");
   if(fp == NULL)  JUMP_TO_EXIT_ERROR_PROC
-#if (EDFLIB_ANNOTATION_BYTES == 114)
-  fseek(fp, 0x751, SEEK_SET);
-#endif
-#if (EDFLIB_ANNOTATION_BYTES == 120)
-  fseek(fp, 0x75f, SEEK_SET);
-#endif
+
+  fseek(fp, get_file_offset(0, 2, 1, &rawhdr) + 32, SEEK_SET);
+
   fputc(0, fp);
 
   fclose(fp);
@@ -1911,14 +2253,10 @@ int main(void)
   /****************************************/
 
   fp = fopen("test.edf", "rb");
-
   if(fp == NULL)  JUMP_TO_EXIT_ERROR_PROC
-#if (EDFLIB_ANNOTATION_BYTES == 114)
-  fseek(fp, 0x7ac, SEEK_SET);
-#endif
-#if (EDFLIB_ANNOTATION_BYTES == 120)
-  fseek(fp, 0x7be, SEEK_SET);
-#endif
+
+  fseek(fp, get_file_offset(1, 0, 0, &rawhdr), SEEK_SET);
+
   if(fread(str, 40, 1, fp) != 1)
   {
     fclose(fp);
@@ -1967,14 +2305,10 @@ int main(void)
   /****************************************/
 
   fp = fopen("test.edf", "rb");
-
   if(fp == NULL)  JUMP_TO_EXIT_ERROR_PROC
-#if (EDFLIB_ANNOTATION_BYTES == 114)
-  fseek(fp, 0x7d4, SEEK_SET);
-#endif
-#if (EDFLIB_ANNOTATION_BYTES == 120)
-  fseek(fp, 0x7e6, SEEK_SET);
-#endif
+
+  fseek(fp, get_file_offset(1, 1, 0, &rawhdr), SEEK_SET);
+
   if(fread(str, 46, 1, fp) != 1)
   {
     fclose(fp);
@@ -2011,14 +2345,10 @@ int main(void)
   /****************************************/
 
   fp = fopen("test.edf", "rb");
-
   if(fp == NULL)  JUMP_TO_EXIT_ERROR_PROC
-#if (EDFLIB_ANNOTATION_BYTES == 114)
-  fseek(fp, 0x958, SEEK_SET);
-#endif
-#if (EDFLIB_ANNOTATION_BYTES == 120)
-  fseek(fp, 0x97c, SEEK_SET);
-#endif
+
+  fseek(fp, get_file_offset(2, 0, 0, &rawhdr), SEEK_SET);
+
   if(fread(str, 40, 1, fp) != 1)
   {
     fclose(fp);
@@ -2055,14 +2385,10 @@ int main(void)
   /****************************************/
 
   fp = fopen("test.edf", "rb");
-
   if(fp == NULL)  JUMP_TO_EXIT_ERROR_PROC
-#if (EDFLIB_ANNOTATION_BYTES == 114)
-  fseek(fp, 0x980, SEEK_SET);
-#endif
-#if (EDFLIB_ANNOTATION_BYTES == 120)
-  fseek(fp, 0x9a4, SEEK_SET);
-#endif
+
+  fseek(fp, get_file_offset(2, 1, 0, &rawhdr), SEEK_SET);
+
   if(fread(str, 46, 1, fp) != 1)
   {
     fclose(fp);
@@ -2111,14 +2437,10 @@ int main(void)
   /****************************************/
 
   fp = fopen("test.edf", "rb");
-
   if(fp == NULL)  JUMP_TO_EXIT_ERROR_PROC
-#if (EDFLIB_ANNOTATION_BYTES == 114)
-  fseek(fp, 0xb04, SEEK_SET);
-#endif
-#if (EDFLIB_ANNOTATION_BYTES == 120)
-  fseek(fp, 0xb3a, SEEK_SET);
-#endif
+
+  fseek(fp, get_file_offset(3, 0, 0, &rawhdr), SEEK_SET);
+
   if(fread(str, 40, 1, fp) != 1)
   {
     fclose(fp);
@@ -2155,14 +2477,10 @@ int main(void)
   /****************************************/
 
   fp = fopen("test.edf", "rb");
-
   if(fp == NULL)  JUMP_TO_EXIT_ERROR_PROC
-#if (EDFLIB_ANNOTATION_BYTES == 114)
-  fseek(fp, 0xb2c, SEEK_SET);
-#endif
-#if (EDFLIB_ANNOTATION_BYTES == 120)
-  fseek(fp, 0xb62, SEEK_SET);
-#endif
+
+  fseek(fp, get_file_offset(3, 1, 0, &rawhdr), SEEK_SET);
+
   if(fread(str, 46, 1, fp) != 1)
   {
     fclose(fp);
@@ -2211,14 +2529,10 @@ int main(void)
   /****************************************/
 
   fp = fopen("test.edf", "rb");
-
   if(fp == NULL)  JUMP_TO_EXIT_ERROR_PROC
-#if (EDFLIB_ANNOTATION_BYTES == 114)
-  fseek(fp, 0xcb0, SEEK_SET);
-#endif
-#if (EDFLIB_ANNOTATION_BYTES == 120)
-  fseek(fp, 0xcf8, SEEK_SET);
-#endif
+
+  fseek(fp, get_file_offset(4, 0, 0, &rawhdr), SEEK_SET);
+
   if(fread(str, 40, 1, fp) != 1)
   {
     fclose(fp);
@@ -2255,14 +2569,10 @@ int main(void)
   /****************************************/
 
   fp = fopen("test.edf", "rb");
-
   if(fp == NULL)  JUMP_TO_EXIT_ERROR_PROC
-#if (EDFLIB_ANNOTATION_BYTES == 114)
-  fseek(fp, 0xcd8, SEEK_SET);
-#endif
-#if (EDFLIB_ANNOTATION_BYTES == 120)
-  fseek(fp, 0xd20, SEEK_SET);
-#endif
+
+  fseek(fp, get_file_offset(4, 1, 0, &rawhdr), SEEK_SET);
+
   if(fread(str, 46, 1, fp) != 1)
   {
     fclose(fp);
@@ -2311,14 +2621,10 @@ int main(void)
   /****************************************/
 
   fp = fopen("test.edf", "rb");
-
   if(fp == NULL)  JUMP_TO_EXIT_ERROR_PROC
-#if (EDFLIB_ANNOTATION_BYTES == 114)
-  fseek(fp, 0xe5c, SEEK_SET);
-#endif
-#if (EDFLIB_ANNOTATION_BYTES == 120)
-  fseek(fp, 0xeb6, SEEK_SET);
-#endif
+
+  fseek(fp, get_file_offset(5, 0, 0, &rawhdr), SEEK_SET);
+
   if(fread(str, 40, 1, fp) != 1)
   {
     fclose(fp);
@@ -2355,14 +2661,10 @@ int main(void)
   /****************************************/
 
   fp = fopen("test.edf", "rb");
-
   if(fp == NULL)  JUMP_TO_EXIT_ERROR_PROC
-#if (EDFLIB_ANNOTATION_BYTES == 114)
-  fseek(fp, 0xe84, SEEK_SET);
-#endif
-#if (EDFLIB_ANNOTATION_BYTES == 120)
-  fseek(fp, 0xede, SEEK_SET);
-#endif
+
+  fseek(fp, get_file_offset(5, 1, 0, &rawhdr), SEEK_SET);
+
   if(fread(str, 46, 1, fp) != 1)
   {
     fclose(fp);
@@ -3463,6 +3765,7 @@ int main(void)
   free(dbuf);
   free(str);
   free(pbuf);
+  free(i3buf);
 
   return EXIT_SUCCESS;
 
@@ -3478,6 +3781,7 @@ OUT_ERROR:
   free(dbuf);
   free(str);
   free(pbuf);
+  free(i3buf);
 
   fprintf(stderr, "Error, line %i file %s\n", line, __FILE__);
 
@@ -3523,8 +3827,188 @@ int dblcmp_lim(double val1, double val2, double lim)
 }
 
 
+int get_raw_header(const char *f_path, raw_hdr_t *hdr)
+{
+  int i, err=-999;
+
+  char *buf=NULL,
+       str[128]={""};
+
+  FILE *f=NULL;
+
+  if((f_path == NULL) || (hdr == NULL))  return -1;
+
+  if(strlen(f_path) < 5)  return -2;
 
+  memset(hdr, 0, sizeof(raw_hdr_t));
 
+  buf = malloc(300);
+  if(buf==NULL)
+  {
+    err = -3;
+    goto EXIT_ERROR;
+  }
+
+  f = fopen(f_path, "rb");
+  if(f==NULL)
+  {
+    err = -4;
+    goto EXIT_ERROR;
+  }
+
+  if(fread(buf, 256, 1, f) != 1)
+  {
+    err = -11;
+    goto EXIT_ERROR;
+  }
+
+  if(!memcmp(buf, "0       ", 8))
+  {
+    hdr->sample_width = 2;
+  }
+  else if(!memcmp(buf, "\xff" "BIOSEMI", 8))
+    {
+      hdr->sample_width = 3;
+    }
+    else
+    {
+      err = -11;
+      goto EXIT_ERROR;
+    }
+
+  memcpy(str, buf+236, 8);
+  str[8] = 0;
+  hdr->datrecs = atoi(str);
+  if(hdr->datrecs < 1)
+  {
+    err = -12;
+    goto EXIT_ERROR;
+  }
+
+  memcpy(str, buf+252, 4);
+  str[4] = 0;
+  hdr->total_chans = atoi(str);
+  if((hdr->total_chans < 1) || (hdr->total_chans > EDFLIB_MAXSIGNALS))
+  {
+    err = -13;
+    goto EXIT_ERROR;
+  }
+
+  memcpy(str, buf+184, 8);
+  str[8] = 0;
+  hdr->hdr_sz = atoi(str);
+  if((hdr->hdr_sz < 512) || (hdr->hdr_sz != ((hdr->total_chans + 1) * 256)))
+  {
+    err = -14;
+    goto EXIT_ERROR;
+  }
+
+  free(buf);
+
+  buf = malloc(hdr->hdr_sz);
+  if(buf==NULL)
+  {
+    err = -15;
+    goto EXIT_ERROR;
+  }
+
+  rewind(f);
+
+  if(fread(buf, hdr->hdr_sz, 1, f) != 1)
+  {
+    err = -16;
+    goto EXIT_ERROR;
+  }
+
+  for(i=0; i<hdr->total_chans; i++)
+  {
+    hdr->bytes_in_datrec[i] = atoi(buf+256+(hdr->total_chans*216)+(i*8)) * hdr->sample_width;
+
+    hdr->datrec_offset[i] = hdr->datrec_sz;
+
+    hdr->datrec_sz += hdr->bytes_in_datrec[i];
+
+    if(hdr->sample_width == 2)
+    {
+      if(memcmp(buf+256+(i*16), "EDF Annotations ", 16))
+      {
+        hdr->regular_chans_idx_list[hdr->regular_chans] = i;
+
+        hdr->regular_chans++;
+      }
+      else
+      {
+        hdr->annot_chans_idx_list[hdr->annot_chans] = i;
+
+        hdr->annot_chans++;
+      }
+    }
+    else
+    {
+      if(memcmp(buf+256+(i*16), "BDF Annotations ", 16))
+      {
+        hdr->regular_chans_idx_list[hdr->regular_chans] = i;
+
+        hdr->regular_chans++;
+      }
+      else
+      {
+        hdr->annot_chans_idx_list[hdr->annot_chans] = i;
+
+        hdr->annot_chans++;
+      }
+    }
+  }
+
+  if(!hdr->annot_chans)
+  {
+    err = -17;
+    goto EXIT_ERROR;
+  }
+
+  if(f)  fclose(f);
+
+  free(buf);
+
+  return 0;
+
+EXIT_ERROR:
+
+  if(f)  fclose(f);
+
+  free(buf);
+
+  memset(hdr, 0, sizeof(raw_hdr_t));
+
+//  printf("get_raw_header(): error: %i\n", err);
+
+  return err;
+}
+
+/* datrec and chan are zero based
+ * is_annot: 0: regular channel 1: annotation channel
+ */
+int get_file_offset(int datrec, int idx, int is_annot, raw_hdr_t *hdr)
+{
+  if((datrec < 0) || (datrec >= hdr->datrecs))  return -1;
+
+  if(!hdr)  return -2;
+
+  if((is_annot < 0) || (is_annot > 1))  return -3;
+
+  if(is_annot)
+  {
+    if((idx < 0) || (idx >= hdr->annot_chans))  return -4;
+
+    return (hdr->hdr_sz + (datrec * hdr->datrec_sz) + (hdr->datrec_offset[hdr->annot_chans_idx_list[idx]]));
+  }
+  else
+  {
+    if((idx < 0) || (idx >= hdr->regular_chans))  return -5;
+
+    return (hdr->hdr_sz + (datrec * hdr->datrec_sz) + (hdr->datrec_offset[hdr->regular_chans_idx_list[idx]]));
+  }
+}
 
 
 



View it on GitLab: https://salsa.debian.org/med-team/edflib/-/commit/9089cd7fe4d7108f82c095408fe824ce9ca28bd0

-- 
View it on GitLab: https://salsa.debian.org/med-team/edflib/-/commit/9089cd7fe4d7108f82c095408fe824ce9ca28bd0
You're receiving this email because of your account on salsa.debian.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://alioth-lists.debian.net/pipermail/debian-med-commit/attachments/20240113/063fc04e/attachment-0001.htm>


More information about the debian-med-commit mailing list