-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathqueue.c
66 lines (55 loc) · 1.11 KB
/
queue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdlib.h>
#include <math.h>
#include <complex.h>
#include <fftw3.h>
#include <string.h>
#include "sdr.h"
/**
* Audio sampling queues for playback and recording
*/
void q_empty(struct Queue *p){
p->head = 0;
p->tail = 0;
p->stall = 1;
p->underflow = 0;
p->overflow = 0;
}
void q_init(struct Queue *p, int length){
/* p->head = 0;
p->tail = 0;
p->stall = 1;
p->underflow = 0;
p->overflow = 0;*/
p->max_q = length;
p->data = malloc((length+1) * sizeof(int32_t));
memset(p->data, 0, p->max_q+1);
q_empty(p);
}
int q_length(struct Queue *p){
if ( p->head >= p->tail)
return p->head - p->tail;
else
return ((p->head + p->max_q) - p->tail);
}
int q_write(struct Queue *p, int32_t w){
if (p->head + 1 == p->tail || p->tail == 0 && p->head == p->max_q-1){
p->overflow++;
return -1;
}
p->data[p->head++] = w;
if (p->head > p->max_q){
p->head = 0;
}
return 0;
}
int32_t q_read(struct Queue *p){
int32_t data;
if (p->tail == p->head){
p->underflow++;
return (int)0;
}
data = p->data[p->tail++];
if (p->tail > p->max_q)
p->tail = 0;
return data;
}