-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtqueue.c
More file actions
129 lines (108 loc) · 3.09 KB
/
rtqueue.c
File metadata and controls
129 lines (108 loc) · 3.09 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* rtqueue.c
This file is a part of 'rtqueue'
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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 General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'rtqueue' is a simple FIFO linked list intended to hold
JACK sample data for process()'ing.
Copyright 2014 murray foster */
#include <stdio.h>
#include <stdlib.h>
#include <jack/jack.h>
#include <pthread.h>
int dequeue_is_waiting = 0;
pthread_mutex_t dequeue_is_waiting_mutex;
pthread_cond_t dequeue_is_waiting_cond;
int enqueue_is_waiting = 0;
pthread_mutex_t enqueue_is_waiting_mutex;
pthread_cond_t enqueue_is_waiting_cond;
/* JACK sample size, set by JACK server */
const size_t smpl_size = sizeof (jack_default_audio_sample_t) ;
typedef struct queue
{
int head;
int tail;
int recordlimit;
int records;
float *queue;
} rtqueue_t;
rtqueue_t *
rtqueue_init(int recordlimit)
{
rtqueue_t *rtq = (rtqueue_t *) malloc(sizeof(rtqueue_t));
rtq->queue = (float *) malloc(smpl_size * (recordlimit + 1));
rtq->head = 0;
rtq->tail = 0;
rtq->recordlimit = recordlimit;
return rtq;
}
int
rtqueue_numrecords(rtqueue_t *rtq)
{
return rtq->records;
}
int
rtqueue_isfull(rtqueue_t *rtq)
{
/* if queue is full, return 1 */
if ((rtq->tail + 1) % (rtq->recordlimit + 1) == rtq->head)
return 1;
else
return 0;
}
int
rtqueue_isempty(rtqueue_t *rtq)
{
/* if queue is empty, return 1 */
if (rtq->head == rtq->tail)
return 1;
else
return 0;
}
int
rtqueue_enq(rtqueue_t *rtq, float data)
{
/* if queue is full, wait */
if ((rtq->tail + 1) % (rtq->recordlimit + 1) == rtq->head)
{
enqueue_is_waiting = 1;
pthread_mutex_lock(&enqueue_is_waiting_mutex);
pthread_cond_wait(&enqueue_is_waiting_cond, &enqueue_is_waiting_mutex);
pthread_mutex_unlock(&enqueue_is_waiting_mutex);
enqueue_is_waiting = 0;
}
rtq->queue[rtq->tail] = data;
rtq->tail = (rtq->tail + 1) % (rtq->recordlimit + 1);
rtq->records+=1;
if (dequeue_is_waiting)
pthread_cond_signal(&dequeue_is_waiting_cond);
return 0;
}
float
rtqueue_deq(rtqueue_t *rtq)
{
float data;
/* if queue is empty, wait */
while (rtq->head == rtq->tail)
{
dequeue_is_waiting = 1;
pthread_mutex_lock(&dequeue_is_waiting_mutex);
pthread_cond_wait(&dequeue_is_waiting_cond, &dequeue_is_waiting_mutex);
pthread_mutex_unlock(&dequeue_is_waiting_mutex);
dequeue_is_waiting = 0;
}
/* dequeue and return data at the head */
data = rtq->queue[rtq->head];
rtq->head = (rtq->head + 1) % (rtq->recordlimit + 1);
rtq->records-=1;
if (enqueue_is_waiting)
pthread_cond_signal(&enqueue_is_waiting_cond);
return data;
}