added: autoconf environment
[lcr.git] / port.cpp
index abd480d..a10a6f0 100644 (file)
--- a/port.cpp
+++ b/port.cpp
@@ -57,6 +57,9 @@ Functions:
 #include <errno.h>
 #include "main.h"
 
+#define SHORT_MIN -32768
+#define SHORT_MAX 32767
+
 class Port *port_first = NULL;
 
 unsigned long port_serial = 1; /* must be 1, because 0== no port */
@@ -88,8 +91,7 @@ void Port::free_epointlist(struct epoint_list *epointlist)
 
        /* free */
        PDEBUG(DEBUG_EPOINT, "PORT(%d) removed epoint from port\n", p_serial);
-       memset(temp, 0, sizeof(struct epoint_list));
-       free(temp);
+       FREE(temp, sizeof(struct epoint_list));
        ememuse--;
 }
 
@@ -110,7 +112,7 @@ void Port::free_epointid(unsigned long epoint_id)
        }
        if (temp == 0)
        {
-               PERROR("epoint_id not in port's list, exitting.\n");
+               PERROR("epoint_id not in port's list.\n");
                return;
        }
        /* detach */
@@ -118,8 +120,7 @@ void Port::free_epointid(unsigned long epoint_id)
 
        /* free */
        PDEBUG(DEBUG_EPOINT, "PORT(%d) removed epoint from port\n", p_serial);
-       memset(temp, 0, sizeof(struct epoint_list));
-       free(temp);
+       FREE(temp, sizeof(struct epoint_list));
        ememuse--;
 }
 
@@ -131,15 +132,11 @@ struct epoint_list *Port::epointlist_new(unsigned long epoint_id)
        struct epoint_list *epointlist, **epointlistpointer;
 
        /* epointlist structure */
-       epointlist = (struct epoint_list *)calloc(1, sizeof(struct epoint_list));
+       epointlist = (struct epoint_list *)MALLOC(sizeof(struct epoint_list));
        if (!epointlist)
-       {
-               PERROR("no mem for allocating epoint_list\n");
-               return(0);
-       }
+               FATAL("No memory for epointlist\n");
        ememuse++;
        PDEBUG(DEBUG_EPOINT, "PORT(%d) allocating epoint_list.\n", p_serial);
-       memset(epointlist, 0, sizeof(struct epoint_list));
 
        /* add epoint_list to chain */
        epointlist->next = NULL;
@@ -175,11 +172,8 @@ Port::Port(int type, char *portname, struct port_settings *settings)
        }
        SCPY(p_name, portname);
        SCPY(p_tone_dir, p_settings.tones_dir); // just to be sure
-       p_last_tv_sec = 0;
-       p_last_tv_msec = 0;
        p_type = type;
        p_serial = port_serial++;
-       p_debug_nothingtosend = 0;
        p_tone_fh = -1;
        p_tone_fetched = NULL;
        p_tone_name[0] = '\0';
@@ -255,10 +249,7 @@ Port::~Port(void)
                temp = temp->next;
        }
        if (temp == NULL)
-       {
-               PERROR("PORT(%s) port not in port's list.\n", p_name);
-               exit(-1);
-       }
+               FATAL("PORT(%s) port not in port's list.\n", p_name);
        /* detach */
        *tempp=this->next;
 
@@ -495,7 +486,7 @@ void Port::set_vbox_speed(int speed)
 
 /*
  * read from the given file as specified in port_set_tone and return sample data
- * silence is appended if sample ends, but only the number of samples with tones are returned
+ * if the tone ends, the result may be less samples than requested
  */
 int Port::read_audio(unsigned char *buffer, int length)
 {
@@ -511,12 +502,8 @@ int Port::read_audio(unsigned char *buffer, int length)
        len = length;
 
        /* if there is no tone set, use silence */
-       if (p_tone_name[0] == 0)
-       {
-rest_is_silence:
-               memset(buffer, (options.law=='a')?0x2a:0xff, len); /* silence */
-               goto done;
-       }
+       if (!p_tone_name[0])
+               return(0);
 
        /* if the file pointer is not open, we open it */
        if (p_tone_fh<0 && p_tone_fetched==NULL)
@@ -586,7 +573,7 @@ read_more:
        }
 
        if (len==0)
-               goto done;
+               return(length-len);
 
        if (p_tone_fh >= 0)
        {
@@ -605,7 +592,7 @@ read_more:
                PDEBUG(DEBUG_PORT, "PORT(%s) 0-length loop: %s\n", p_name, filename);
                p_tone_name[0]=0;
                p_tone_dir[0]=0;
-               goto rest_is_silence;
+               return(length-len);
        }
 
        /* if eof is reached, or if the normal file cannot be opened, continue with the loop file if possible */
@@ -630,7 +617,7 @@ try_loop:
                                PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename);
                                p_tone_dir[0] = '\0';
                                p_tone_name[0] = '\0';
-                               goto rest_is_silence;
+                               return(length-len);
                        }
                        fhuse++;
                }
@@ -643,7 +630,7 @@ try_loop:
                        PDEBUG(DEBUG_PORT, "PORT(%s) no tone loop: %s\n",p_name, filename);
                        p_tone_dir[0] = '\0';
                        p_tone_name[0] = '\0';
-                       goto rest_is_silence;
+                       return(length-len);
                }
                fhuse++;
        }
@@ -652,9 +639,6 @@ try_loop:
 
        /* now we have opened the loop */
        goto read_more;
-
-done:
-       return(length-len);
 }
 
 
@@ -1062,6 +1046,8 @@ void Port::record(unsigned char *data, int length, int dir_fromup)
 
        free = ((p_record_buffer_readp - p_record_buffer_writep - 1) & RECORD_BUFFER_MASK);
 
+//PDEBUG(DEBUG_PORT, "record(data,%d,%d): free=%d, p_record_buffer_dir=%d, p_record_buffer_readp=%d, p_record_buffer_writep=%d.\n", length, dir_fromup, free, p_record_buffer_dir, p_record_buffer_readp, p_record_buffer_writep);
+
        /* the buffer stores the same data stream */
        if (dir_fromup == p_record_buffer_dir)
        {
@@ -1159,6 +1145,8 @@ void Port::record(unsigned char *data, int length, int dir_fromup)
        ii = (p_record_buffer_writep - p_record_buffer_readp) & RECORD_BUFFER_MASK;
        if (length < ii)
                ii = length;
+//PDEBUG(DEBUG_PORT, "record(data,%d,%d): free=%d, p_record_buffer_dir=%d, p_record_buffer_readp=%d, p_record_buffer_writep=%d: mixing %d bytes.\n", length, dir_fromup, free, p_record_buffer_dir, p_record_buffer_readp, p_record_buffer_writep, ii);
+
        /* write data mixed with the buffer */
        switch(p_record_type)
        {
@@ -1170,10 +1158,8 @@ void Port::record(unsigned char *data, int length, int dir_fromup)
                        sample = p_record_buffer[p_record_buffer_readp]
                                + audio_law_to_s32[*data++];
                        p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
-                       if (sample < 32767)
-                               sample = -32767;
-                       if (sample > 32768)
-                               sample = 32768;
+                       if (sample < SHORT_MIN) sample = SHORT_MIN;
+                       if (sample > SHORT_MAX) sample = SHORT_MAX;
                        *s++ = sample;
                        i++;
                }
@@ -1199,6 +1185,7 @@ void Port::record(unsigned char *data, int length, int dir_fromup)
                        {
                                *s++ = p_record_buffer[p_record_buffer_readp];
                                *s++ = audio_law_to_s32[*data++];
+                               p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
                                i++;
                        }
                }
@@ -1213,10 +1200,8 @@ void Port::record(unsigned char *data, int length, int dir_fromup)
                        sample = p_record_buffer[p_record_buffer_readp]
                                + audio_law_to_s32[*data++];
                        p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
-                       if (sample < 32767)
-                               sample = -32767;
-                       if (sample > 32768)
-                               sample = 32768;
+                       if (sample < SHORT_MIN) sample = SHORT_MIN;
+                       if (sample > SHORT_MAX) sample = SHORT_MAX;
                        *d++ = (sample+0x8000) >> 8;
                        i++;
                }
@@ -1231,10 +1216,8 @@ void Port::record(unsigned char *data, int length, int dir_fromup)
                        sample = p_record_buffer[p_record_buffer_readp]
                                + audio_law_to_s32[*data++];
                        p_record_buffer_readp = (p_record_buffer_readp + 1) & RECORD_BUFFER_MASK;
-                       if (sample < 32767)
-                               sample = -32767;
-                       if (sample > 32768)
-                               sample = 32768;
+                       if (sample < SHORT_MIN) sample = SHORT_MIN;
+                       if (sample > SHORT_MAX) sample = SHORT_MAX;
                        *d++ = audio_s16_to_law[sample & 0xffff];
                        i++;
                }