blob: de0ee8c230efaea625f897ca5157f180f709dd19 (
plain)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
#include "fische_internal.h"
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#ifdef DEBUG
#include <stdio.h>
#endif
struct fische__audiobuffer*
fische__audiobuffer_new (struct fische* parent) {
struct fische__audiobuffer* retval = malloc (sizeof (struct fische__audiobuffer));
retval->priv = malloc (sizeof (struct _fische__audiobuffer_));
struct _fische__audiobuffer_* P = retval->priv;
P->fische = parent;
P->buffer = 0;
P->buffer_size = 0;
P->format = parent->audio_format;
P->is_locked = 0;
P->puts = 0;
P->gets = 0;
P->last_get = 0;
retval->front_sample_count = 0;
retval->front_samples = 0;
retval->back_sample_count = 0;
retval->back_samples = 0;
return retval;
}
void
fische__audiobuffer_free (struct fische__audiobuffer* self)
{
if (!self)
return;
fische__audiobuffer_lock( self );
free (self->priv->buffer);
free (self->priv);
free (self);
}
void
fische__audiobuffer_insert (struct fische__audiobuffer* self, const void* data, uint_fast32_t size)
{
struct _fische__audiobuffer_* P = self->priv;
if (P->buffer_size > 44100)
return;
uint_fast8_t width = 1;
switch (P->format) {
case FISCHE_AUDIOFORMAT_DOUBLE:
width = 8;
break;
case FISCHE_AUDIOFORMAT_FLOAT:
case FISCHE_AUDIOFORMAT_S32:
case FISCHE_AUDIOFORMAT_U32:
width = 4;
break;
case FISCHE_AUDIOFORMAT_S16:
case FISCHE_AUDIOFORMAT_U16:
width = 2;
}
uint_fast32_t old_bufsize = P->buffer_size;
P->buffer_size += size / width;
P->buffer = realloc (P->buffer, P->buffer_size * sizeof (double));
uint_fast32_t i;
for (i = 0; i < size / width; ++ i) {
double* dest = (P->buffer + old_bufsize + i);
switch (P->format) {
case FISCHE_AUDIOFORMAT_FLOAT:
*dest = * ( (float*) data + i);
break;
case FISCHE_AUDIOFORMAT_DOUBLE:
*dest = * ( (double*) data + i);
break;
case FISCHE_AUDIOFORMAT_S32:
*dest = * ( (int32_t*) data + i);
*dest /= INT32_MAX;
break;
case FISCHE_AUDIOFORMAT_U32:
*dest = * ( (uint32_t*) data + i);
*dest -= INT32_MAX;
*dest /= INT32_MAX;
break;
case FISCHE_AUDIOFORMAT_S16:
*dest = * ( (int16_t*) data + i);
*dest /= INT16_MAX;
break;
case FISCHE_AUDIOFORMAT_U16:
*dest = * ( (uint16_t*) data + i);
*dest -= INT16_MAX;
*dest /= INT16_MAX;
break;
case FISCHE_AUDIOFORMAT_S8:
*dest = * ( (int8_t*) data + i);
*dest /= INT8_MAX;
break;
case FISCHE_AUDIOFORMAT_U8:
*dest = * ( (uint8_t*) data + i);
*dest /= INT8_MAX;
*dest /= INT8_MAX;
break;
}
}
++ P->puts;
}
void
fische__audiobuffer_get (struct fische__audiobuffer* self)
{
struct _fische__audiobuffer_* P = self->priv;
if (P->buffer_size == 0)
return;
double* new_start = P->buffer + P->last_get * 2;
P->buffer_size -= P->last_get * 2;
// pop used data off front
memmove (P->buffer, new_start, P->buffer_size * sizeof (double));
P->buffer = realloc (P->buffer, P->buffer_size * sizeof (double));
if (!P->puts)
return;
// fallback for first get
if (P->gets == 0) {
P->gets = 3;
P->puts = 1;
}
// get/put ratio
double d_ratio = ( (double) P->gets ) / P->puts;
uint_fast8_t ratio = ceil( d_ratio );
// how many samples to return
uint_fast32_t n_samples = P->buffer_size / 2 / ratio;
// set return data size and remember
self->front_sample_count = n_samples;
self->back_sample_count = n_samples;
P->last_get = n_samples;
// set export buffer
self->front_samples = P->buffer;
self->back_samples = P->buffer + P->buffer_size - n_samples * 2;
// increment get counter
++ P->gets;
}
void
fische__audiobuffer_lock (struct fische__audiobuffer* self)
{
#ifdef __GNUC__
while ( !__sync_bool_compare_and_swap( &self->priv->is_locked, 0, 1 ) )
usleep( 1 );
#else
while( self->priv->is_locked )
usleep( 1 );
self->priv->is_locked = 1;
#endif
}
void
fische__audiobuffer_unlock (struct fische__audiobuffer* self)
{
self->priv->is_locked = 0;
}
|