From fe5a4bebe63e9d4d4a2d35590fa860c233487254 Mon Sep 17 00:00:00 2001 From: drowe67 Date: Mon, 15 Oct 2012 02:53:00 +0000 Subject: [PATCH] fifo functiosn useful for gluing portaudio to modem and codec functions git-svn-id: https://svn.code.sf.net/p/freetel/code@750 01035d8c-6547-0410-b346-abe4f91aad63 --- codec2-dev/src/fifo.c | 133 ++++++++++++++++++++++++++++++++++++++++++ codec2-dev/src/fifo.h | 40 +++++++++++++ 2 files changed, 173 insertions(+) create mode 100644 codec2-dev/src/fifo.c create mode 100644 codec2-dev/src/fifo.h diff --git a/codec2-dev/src/fifo.c b/codec2-dev/src/fifo.c new file mode 100644 index 00000000..2773f994 --- /dev/null +++ b/codec2-dev/src/fifo.c @@ -0,0 +1,133 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: fifo.c + AUTHOR......: David Rowe + DATE CREATED: Oct 15 2012 + + A FIFO design useful in gluing the FDMDV modem and codec together in + integrated applications. + +\*---------------------------------------------------------------------------*/ + +/* + Copyright (C) 2012 David Rowe + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2.1, as + published by the Free Software Foundation. This program is + distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; if not, see . +*/ + +#include +#include +#include +#include "fifo.h" + +struct FIFO { + short *buf; + short *pin; + short *pout; + int nshort; + int n; +}; + +struct FIFO *fifo_create(int nshort) { + struct FIFO *fifo; + + fifo = (struct FIFO *)malloc(sizeof(struct FIFO)); + assert(fifo != NULL); + + fifo->buf = (short*)malloc(sizeof(short)*nshort); + assert(fifo->buf != NULL); + fifo->pin = fifo->buf; + fifo->pout = fifo->buf; + fifo->nshort = nshort; + fifo->n = 0; + + return fifo; +} + +void fifo_destroy(struct FIFO *fifo) { + assert(fifo != NULL); + free(fifo->buf); + free(fifo); +} + +int fifo_write(struct FIFO *fifo, short data[], int n) { + int i; + int fifo_free; + short *pdata; + short *pin = fifo->pin; + + //printf(" write %d in: %d\n", n, pin-fifo->pin); + assert(fifo != NULL); + assert(data != NULL); + + fifo_free = fifo->nshort - fifo->n; + + if (n > fifo_free) { + return -1; + } + else { + + /* This could be made more efficient with block copies + using memcpy */ + + pdata = data; + for(i=0; ibuf + fifo->nshort)) + pin = fifo->buf; + } + fifo->n += n; + fifo->pin = pin; + } + + return 0; +} + +int fifo_read(struct FIFO *fifo, short data[], int n) +{ + int i; + short *pdata; + short *pout = fifo->pout; + + //printf(" read %d pout: %d\n", n, pout-fifo->buf); + assert(fifo != NULL); + assert(data != NULL); + + if (n > fifo->n) { + return -1; + } + else { + + /* This could be made more efficient with block copies + using memcpy */ + + pdata = data; + for(i=0; ibuf + fifo->nshort)) + pout = fifo->buf; + } + fifo->n -= n; + fifo->pout = pout; + } + + return 0; +} + +int fifo_n(struct FIFO *fifo) +{ + assert(fifo != NULL); + return fifo->n; +} + diff --git a/codec2-dev/src/fifo.h b/codec2-dev/src/fifo.h new file mode 100644 index 00000000..d8fbcf74 --- /dev/null +++ b/codec2-dev/src/fifo.h @@ -0,0 +1,40 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: fifo.h + AUTHOR......: David Rowe + DATE CREATED: Oct 15 2012 + + A FIFO design useful in gluing the FDMDV modem and codec together in + integrated applications. + +\*---------------------------------------------------------------------------*/ + +/* + Copyright (C) 2012 David Rowe + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2.1, as + published by the Free Software Foundation. This program is + distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; if not, see . +*/ + +#ifndef __FIFO__ +#define __FIFO__ + +struct FIFO; + +struct FIFO *fifo_create(int nshort); +void fifo_destroy(struct FIFO *fifo); +int fifo_write(struct FIFO *fifo, short data[], int n); +int fifo_read(struct FIFO *fifo, short data[], int n); +int fifo_n(struct FIFO *fifo); + +#endif -- 2.25.1