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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
/*
LV2 audio peaks utilities
Copyright 2016 David Robillard <d@drobilla.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/**
This file defines utilities for sending and receiving audio peaks for
waveform display. The functionality is divided into two objects:
PeaksSender, for sending peaks updates from the plugin, and PeaksReceiver,
for receiving such updates and caching the peaks.
This allows peaks for a waveform of any size at any resolution to be
requested, with reasonably sized incremental updates sent over plugin ports.
*/
#ifndef PEAKS_H_INCLUDED
#define PEAKS_H_INCLUDED
#include <math.h>
#include "lv2/atom/forge.h"
#define PEAKS_URI "http://lv2plug.in/ns/peaks#"
#define PEAKS__PeakUpdate PEAKS_URI "PeakUpdate"
#define PEAKS__magnitudes PEAKS_URI "magnitudes"
#define PEAKS__offset PEAKS_URI "offset"
#define PEAKS__total PEAKS_URI "total"
#ifndef MIN
# define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
#ifndef MAX
# define MAX(a, b) (((a) > (b)) ? (a) : (b))
#endif
typedef struct {
LV2_URID atom_Float;
LV2_URID atom_Int;
LV2_URID atom_Vector;
LV2_URID peaks_PeakUpdate;
LV2_URID peaks_magnitudes;
LV2_URID peaks_offset;
LV2_URID peaks_total;
} PeaksURIs;
typedef struct {
PeaksURIs uris; ///< URIDs used in protocol
const float* samples; ///< Sample data
uint32_t n_samples; ///< Total number of samples
uint32_t n_peaks; ///< Total number of peaks
uint32_t current_offset; ///< Current peak offset
bool sending; ///< True iff currently sending
} PeaksSender;
typedef struct {
PeaksURIs uris; ///< URIDs used in protocol
float* peaks; ///< Received peaks, or zeroes
uint32_t n_peaks; ///< Total number of peaks
} PeaksReceiver;
/**
Map URIs used in the peaks protocol.
*/
static inline void
peaks_map_uris(PeaksURIs* uris, LV2_URID_Map* map)
{
uris->atom_Float = map->map(map->handle, LV2_ATOM__Float);
uris->atom_Int = map->map(map->handle, LV2_ATOM__Int);
uris->atom_Vector = map->map(map->handle, LV2_ATOM__Vector);
uris->peaks_PeakUpdate = map->map(map->handle, PEAKS__PeakUpdate);
uris->peaks_magnitudes = map->map(map->handle, PEAKS__magnitudes);
uris->peaks_offset = map->map(map->handle, PEAKS__offset);
uris->peaks_total = map->map(map->handle, PEAKS__total);
}
/**
Initialise peaks sender. The new sender is inactive and will do nothing
when `peaks_sender_send()` is called, until a transmission is started with
`peaks_sender_start()`.
*/
static inline PeaksSender*
peaks_sender_init(PeaksSender* sender, LV2_URID_Map* map)
{
memset(sender, 0, sizeof(*sender));
peaks_map_uris(&sender->uris, map);
return sender;
}
/**
Prepare to start a new peaks transmission. After this is called, the peaks
can be sent with successive calls to `peaks_sender_send()`.
*/
static inline void
peaks_sender_start(PeaksSender* sender,
const float* samples,
uint32_t n_samples,
uint32_t n_peaks)
{
sender->samples = samples;
sender->n_samples = n_samples;
sender->n_peaks = n_peaks;
sender->current_offset = 0;
sender->sending = true;
}
/**
Forge a message which sends a range of peaks. Writes a peaks:PeakUpdate
object to `forge`, like:
[source,n3]
----
[]
a peaks:PeakUpdate ;
peaks:offset 256 ;
peaks:total 1024 ;
peaks:magnitudes [ 0.2f, 0.3f, ... ] .
----
*/
static inline bool
peaks_sender_send(PeaksSender* sender,
LV2_Atom_Forge* forge,
uint32_t n_frames,
uint32_t offset)
{
const PeaksURIs* uris = &sender->uris;
if (!sender->sending || sender->current_offset >= sender->n_peaks) {
return sender->sending = false;
}
// Start PeakUpdate object
lv2_atom_forge_frame_time(forge, offset);
LV2_Atom_Forge_Frame frame;
lv2_atom_forge_object(forge, &frame, 0, uris->peaks_PeakUpdate);
// eg:offset = OFFSET
lv2_atom_forge_key(forge, uris->peaks_offset);
lv2_atom_forge_int(forge, sender->current_offset);
// eg:total = TOTAL
lv2_atom_forge_key(forge, uris->peaks_total);
lv2_atom_forge_int(forge, sender->n_peaks);
// eg:magnitudes = Vector<Float>(PEAK, PEAK, ...)
lv2_atom_forge_key(forge, uris->peaks_magnitudes);
LV2_Atom_Forge_Frame vec_frame;
lv2_atom_forge_vector_head(
forge, &vec_frame, sizeof(float), uris->atom_Float);
// Calculate how many peaks to send this update
const int chunk_size = MAX(1, sender->n_samples / sender->n_peaks);
const uint32_t space = forge->size - forge->offset;
const uint32_t remaining = sender->n_peaks - sender->current_offset;
const int n_update = MIN(remaining,
MIN(n_frames / 4, space / sizeof(float)));
// Calculate peak (maximum magnitude) for each chunk
for (int i = 0; i < n_update; ++i) {
const int start = (sender->current_offset + i) * chunk_size;
float peak = 0.0f;
for (int j = 0; j < chunk_size; ++j) {
peak = fmaxf(peak, fabsf(sender->samples[start + j]));
}
lv2_atom_forge_float(forge, peak);
}
// Finish message
lv2_atom_forge_pop(forge, &vec_frame);
lv2_atom_forge_pop(forge, &frame);
sender->current_offset += n_update;
return true;
}
/**
Initialise a peaks receiver. The receiver stores an array of all peaks,
which is updated incrementally with peaks_receiver_receive().
*/
static inline PeaksReceiver*
peaks_receiver_init(PeaksReceiver* receiver, LV2_URID_Map* map)
{
memset(receiver, 0, sizeof(*receiver));
peaks_map_uris(&receiver->uris, map);
return receiver;
}
/**
Clear stored peaks and free all memory. This should be called when the
peaks are to be updated with a different audio source.
*/
static inline void
peaks_receiver_clear(PeaksReceiver* receiver)
{
free(receiver->peaks);
receiver->peaks = NULL;
receiver->n_peaks = 0;
}
/**
Handle PeakUpdate message.
The stored peaks array is updated with the slice of peaks in `update`,
resizing if necessary while preserving contents.
Returns 0 if peaks have been updated, negative on error.
*/
static inline int
peaks_receiver_receive(PeaksReceiver* receiver, const LV2_Atom_Object* update)
{
const PeaksURIs* uris = &receiver->uris;
// Get properties of interest from update
const LV2_Atom_Int* offset = NULL;
const LV2_Atom_Int* total = NULL;
const LV2_Atom_Vector* peaks = NULL;
lv2_atom_object_get_typed(update,
uris->peaks_offset, &offset, uris->atom_Int,
uris->peaks_total, &total, uris->atom_Int,
uris->peaks_magnitudes, &peaks, uris->atom_Vector,
0);
if (!offset || !total || !peaks ||
peaks->body.child_type != uris->atom_Float) {
return -1; // Invalid update
}
const uint32_t n = (uint32_t)total->body;
if (receiver->n_peaks != n) {
// Update is for a different total number of peaks, resize
receiver->peaks = (float*)realloc(receiver->peaks, n * sizeof(float));
if (receiver->n_peaks > 0 && receiver->n_peaks < n) {
/* The peaks array is being expanded. Copy the old peaks,
duplicating each as necessary to fill the new peaks buffer.
This preserves the current peaks so that the peaks array can be
reasonably drawn at any time, but the resolution will increase
as new updates arrive. */
const int n_per = n / receiver->n_peaks;
for (int i = n - 1; i >= 0; --i) {
receiver->peaks[i] = receiver->peaks[i / n_per];
}
} else if (receiver->n_peaks > 0) {
/* The peak array is being shrunk. Similar to the above. */
const int n_per = receiver->n_peaks / n;
for (int i = n - 1; i >= 0; --i) {
receiver->peaks[i] = receiver->peaks[i * n_per];
}
}
receiver->n_peaks = n;
}
// Copy vector contents to corresponding range in peaks array
memcpy(receiver->peaks + offset->body,
peaks + 1,
peaks->atom.size - sizeof(LV2_Atom_Vector_Body));
return 0;
}
#endif // PEAKS_H_INCLUDED
|