1 /*
2  * GNUitar
3  * Backbuf - circular buffer for delay
4  * Copyright (C) 2000,2001,2003 Max Rudensky         <fonin@ziet.zhitomir.ua>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * $Id: backbuf.c,v 1.5 2003/02/03 11:39:25 fonin Exp $
21  *
22  * $Log: backbuf.c,v $
23  * Revision 1.5  2003/02/03 11:39:25  fonin
24  * Copyright year changed.
25  *
26  * Revision 1.4  2003/01/29 19:34:00  fonin
27  * Win32 port.
28  *
29  * Revision 1.3  2001/06/02 14:05:59  fonin
30  * Added GNU disclaimer.
31  *
32  * Revision 1.2  2001/03/25 12:10:49  fonin
33  * Effect window control ignores delete event.
34  *
35  * Revision 1.1.1.1  2001/01/11 13:21:06  fonin
36  * Version 0.1.0 Release 1 beta
37  *
38  */
39 
40 #include "backbuf.h"
41 #include <string.h>
42 #include <stdlib.h>
43 #include <assert.h>
44 
45 void
backbuff_init(struct backBuf * b,unsigned int maxDelay)46 backbuff_init(struct backBuf *b, unsigned int maxDelay)
47 {
48     b->nstor = maxDelay + 1;
49     b->storage = (BUF_TYPE *) malloc(sizeof(BUF_TYPE) * b->nstor);
50     memset(b->storage, 0, b->nstor * sizeof(BUF_TYPE));
51     b->curpos = 0;
52 }
53 
54 void
backbuff_done(struct backBuf * b)55 backbuff_done(struct backBuf *b)
56 {
57     free(b->storage);
58 }
59 
60 void
backbuff_add(struct backBuf * b,BUF_TYPE d)61 backbuff_add(struct backBuf *b, BUF_TYPE d)
62 {
63     b->curpos++;
64     if (b->curpos == b->nstor)
65 	b->curpos = 0;
66     b->storage[b->curpos] = d;
67 }
68 
69 BUF_TYPE
backbuff_get(struct backBuf * b,unsigned int Delay)70 backbuff_get(struct backBuf *b, unsigned int Delay)
71 {
72     int             getpos;
73     assert(Delay < b->nstor);
74     getpos = (int) b->curpos;
75     getpos -= Delay;
76     if (getpos < 0)
77 	getpos += b->nstor;
78 
79     assert(getpos >= 0 && getpos < (int) b->nstor);
80 
81     return b->storage[getpos];
82 }
83