summaryrefslogtreecommitdiff
path: root/libs/vamp-sdk/vamp-sdk/hostext/PluginBufferingAdapter.cpp
blob: cbe179fb6b1b8fca504bebb8134746622a9dc0e2 (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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    Vamp

    An API for audio analysis and feature extraction plugins.

    Centre for Digital Music, Queen Mary, University of London.
    Copyright 2006-2007 Chris Cannam and QMUL.
    This file by Mark Levy and Chris Cannam.
  
    Permission is hereby granted, free of charge, to any person
    obtaining a copy of this software and associated documentation
    files (the "Software"), to deal in the Software without
    restriction, including without limitation the rights to use, copy,
    modify, merge, publish, distribute, sublicense, and/or sell copies
    of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
    ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
    CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

    Except as contained in this notice, the names of the Centre for
    Digital Music; Queen Mary, University of London; and Chris Cannam
    shall not be used in advertising or otherwise to promote the sale,
    use or other dealings in this Software without prior written
    authorization.
*/

#include <vector>
#include <map>

#include "PluginBufferingAdapter.h"

using std::vector;
using std::map;

namespace Vamp {
	
namespace HostExt {
		
class PluginBufferingAdapter::Impl
{
public:
    Impl(Plugin *plugin, float inputSampleRate);
    ~Impl();
		
    bool initialise(size_t channels, size_t stepSize, size_t blockSize);

    OutputList getOutputDescriptors() const;

    void reset();

    FeatureSet process(const float *const *inputBuffers, RealTime timestamp);
		
    FeatureSet getRemainingFeatures();
		
protected:
    class RingBuffer
    {
    public:
        RingBuffer(int n) :
            m_buffer(new float[n+1]), m_writer(0), m_reader(0), m_size(n+1) { }
        virtual ~RingBuffer() { delete[] m_buffer; }

        int getSize() const { return m_size-1; }
        void reset() { m_writer = 0; m_reader = 0; }

        int getReadSpace() const {
            int writer = m_writer, reader = m_reader, space;
            if (writer > reader) space = writer - reader;
            else if (writer < reader) space = (writer + m_size) - reader;
            else space = 0;
            return space;
        }

        int getWriteSpace() const {
            int writer = m_writer;
            int reader = m_reader;
            int space = (reader + m_size - writer - 1);
            if (space >= m_size) space -= m_size;
            return space;
        }
        
        int peek(float *destination, int n) const {

            int available = getReadSpace();

            if (n > available) {
                for (int i = available; i < n; ++i) {
                    destination[i] = 0.f;
                }
                n = available;
            }
            if (n == 0) return n;

            int reader = m_reader;
            int here = m_size - reader;
            const float *const bufbase = m_buffer + reader;

            if (here >= n) {
                for (int i = 0; i < n; ++i) {
                    destination[i] = bufbase[i];
                }
            } else {
                for (int i = 0; i < here; ++i) {
                    destination[i] = bufbase[i];
                }
                float *const destbase = destination + here;
                const int nh = n - here;
                for (int i = 0; i < nh; ++i) {
                    destbase[i] = m_buffer[i];
                }
            }

            return n;
        }

        int skip(int n) {
            
            int available = getReadSpace();
            if (n > available) {
                n = available;
            }
            if (n == 0) return n;

            int reader = m_reader;
            reader += n;
            while (reader >= m_size) reader -= m_size;
            m_reader = reader;
            return n;
        }
        
        int write(const float *source, int n) {

            int available = getWriteSpace();
            if (n > available) {
                n = available;
            }
            if (n == 0) return n;

            int writer = m_writer;
            int here = m_size - writer;
            float *const bufbase = m_buffer + writer;
            
            if (here >= n) {
                for (int i = 0; i < n; ++i) {
                    bufbase[i] = source[i];
                }
            } else {
                for (int i = 0; i < here; ++i) {
                    bufbase[i] = source[i];
                }
                const int nh = n - here;
                const float *const srcbase = source + here;
                float *const buf = m_buffer;
                for (int i = 0; i < nh; ++i) {
                    buf[i] = srcbase[i];
                }
            }

            writer += n;
            while (writer >= m_size) writer -= m_size;
            m_writer = writer;

            return n;
        }

        int zero(int n) {
            
            int available = getWriteSpace();
            if (n > available) {
                n = available;
            }
            if (n == 0) return n;

            int writer = m_writer;
            int here = m_size - writer;
            float *const bufbase = m_buffer + writer;

            if (here >= n) {
                for (int i = 0; i < n; ++i) {
                    bufbase[i] = 0.f;
                }
            } else {
                for (int i = 0; i < here; ++i) {
                    bufbase[i] = 0.f;
                }
                const int nh = n - here;
                for (int i = 0; i < nh; ++i) {
                    m_buffer[i] = 0.f;
                }
            }
            
            writer += n;
            while (writer >= m_size) writer -= m_size;
            m_writer = writer;

            return n;
        }

    protected:
        float *m_buffer;
        int    m_writer;
        int    m_reader;
        int    m_size;

    private:
        RingBuffer(const RingBuffer &); // not provided
        RingBuffer &operator=(const RingBuffer &); // not provided
    };

    Plugin *m_plugin;
    size_t m_inputStepSize;
    size_t m_inputBlockSize;
    size_t m_stepSize;
    size_t m_blockSize;
    size_t m_channels;
    vector<RingBuffer *> m_queue;
    float **m_buffers;
    float m_inputSampleRate;
    RealTime m_timestamp;
    bool m_unrun;
    OutputList m_outputs;
		
    void processBlock(FeatureSet& allFeatureSets, RealTime timestamp);
};
		
PluginBufferingAdapter::PluginBufferingAdapter(Plugin *plugin) :
    PluginWrapper(plugin)
{
    m_impl = new Impl(plugin, m_inputSampleRate);
}
		
PluginBufferingAdapter::~PluginBufferingAdapter()
{
    delete m_impl;
}
		
bool
PluginBufferingAdapter::initialise(size_t channels, size_t stepSize, size_t blockSize)
{
    return m_impl->initialise(channels, stepSize, blockSize);
}

PluginBufferingAdapter::OutputList
PluginBufferingAdapter::getOutputDescriptors() const
{
    return m_impl->getOutputDescriptors();
}

void
PluginBufferingAdapter::reset()
{
    m_impl->reset();
}
		
PluginBufferingAdapter::FeatureSet
PluginBufferingAdapter::process(const float *const *inputBuffers,
                                RealTime timestamp)
{
    return m_impl->process(inputBuffers, timestamp);
}
		
PluginBufferingAdapter::FeatureSet
PluginBufferingAdapter::getRemainingFeatures()
{
    return m_impl->getRemainingFeatures();
}
		
PluginBufferingAdapter::Impl::Impl(Plugin *plugin, float inputSampleRate) :
    m_plugin(plugin),
    m_inputStepSize(0),
    m_inputBlockSize(0),
    m_stepSize(0),
    m_blockSize(0),
    m_channels(0), 
    m_queue(0),
    m_buffers(0),
    m_inputSampleRate(inputSampleRate),
    m_timestamp(RealTime::zeroTime),
    m_unrun(true)
{
    m_outputs = plugin->getOutputDescriptors();
}
		
PluginBufferingAdapter::Impl::~Impl()
{
    // the adapter will delete the plugin

    for (size_t i = 0; i < m_channels; ++i) {
        delete m_queue[i];
        delete[] m_buffers[i];
    }
    delete[] m_buffers;
}

size_t
PluginBufferingAdapter::getPreferredStepSize() const
{
    return getPreferredBlockSize();
}
		
bool
PluginBufferingAdapter::Impl::initialise(size_t channels, size_t stepSize, size_t blockSize)
{
    if (stepSize != blockSize) {
        std::cerr << "PluginBufferingAdapter::initialise: input stepSize must be equal to blockSize for this adapter (stepSize = " << stepSize << ", blockSize = " << blockSize << ")" << std::endl;
        return false;
    }

    m_channels = channels;	
    m_inputStepSize = stepSize;
    m_inputBlockSize = blockSize;
    
    // use the step and block sizes which the plugin prefers
    m_stepSize = m_plugin->getPreferredStepSize();
    m_blockSize = m_plugin->getPreferredBlockSize();
    
    // or sensible defaults if it has no preference
    if (m_blockSize == 0) {
        m_blockSize = 1024;
    }
    if (m_stepSize == 0) {
        if (m_plugin->getInputDomain() == Vamp::Plugin::FrequencyDomain) {
            m_stepSize = m_blockSize/2;
        } else {
            m_stepSize = m_blockSize;
        }
    } else if (m_stepSize > m_blockSize) {
        if (m_plugin->getInputDomain() == Vamp::Plugin::FrequencyDomain) {
            m_blockSize = m_stepSize * 2;
        } else {
            m_blockSize = m_stepSize;
        }
    }
    
    // std::cerr << "PluginBufferingAdapter::initialise: stepSize " << m_inputStepSize << " -> " << m_stepSize 
    // << ", blockSize " << m_inputBlockSize << " -> " << m_blockSize << std::endl;			
    
    // current implementation breaks if step is greater than block
    if (m_stepSize > m_blockSize) {
        std::cerr << "PluginBufferingAdapter::initialise: plugin's preferred stepSize greater than blockSize, giving up!" << std::endl;
        return false;
    }

    m_buffers = new float *[m_channels];

    for (size_t i = 0; i < m_channels; ++i) {
        m_queue.push_back(new RingBuffer(m_blockSize + m_inputBlockSize));
        m_buffers[i] = new float[m_blockSize];
    }
    
    return m_plugin->initialise(m_channels, m_stepSize, m_blockSize);
}
		
PluginBufferingAdapter::OutputList
PluginBufferingAdapter::Impl::getOutputDescriptors() const
{
    OutputList outs = m_plugin->getOutputDescriptors();
    for (size_t i = 0; i < outs.size(); ++i) {
        if (outs[i].sampleType == OutputDescriptor::OneSamplePerStep) {
            outs[i].sampleRate = 1.f / m_stepSize;
        }
        outs[i].sampleType = OutputDescriptor::VariableSampleRate;
    }
    return outs;
}

void
PluginBufferingAdapter::Impl::reset()
{
    m_timestamp = RealTime::zeroTime;
    m_unrun = true;

    for (size_t i = 0; i < m_queue.size(); ++i) {
        m_queue[i]->reset();
    }
}

PluginBufferingAdapter::FeatureSet
PluginBufferingAdapter::Impl::process(const float *const *inputBuffers,
                                      RealTime timestamp)
{
    FeatureSet allFeatureSets;

    if (m_unrun) {
        m_timestamp = timestamp;
        m_unrun = false;
    }
			
    // queue the new input
    
    for (size_t i = 0; i < m_channels; ++i) {
        int written = m_queue[i]->write(inputBuffers[i], m_inputBlockSize);
        if (written < int(m_inputBlockSize) && i == 0) {
            std::cerr << "WARNING: PluginBufferingAdapter::Impl::process: "
                      << "Buffer overflow: wrote " << written 
                      << " of " << m_inputBlockSize 
                      << " input samples (for plugin step size "
                      << m_stepSize << ", block size " << m_blockSize << ")"
                      << std::endl;
        }
    }    
    
    // process as much as we can

    while (m_queue[0]->getReadSpace() >= int(m_blockSize)) {
        processBlock(allFeatureSets, timestamp);
    }	
    
    return allFeatureSets;
}
    
PluginBufferingAdapter::FeatureSet
PluginBufferingAdapter::Impl::getRemainingFeatures() 
{
    FeatureSet allFeatureSets;
    
    // process remaining samples in queue
    while (m_queue[0]->getReadSpace() >= int(m_blockSize)) {
        processBlock(allFeatureSets, m_timestamp);
    }
    
    // pad any last samples remaining and process
    if (m_queue[0]->getReadSpace() > 0) {
        for (size_t i = 0; i < m_channels; ++i) {
            m_queue[i]->zero(m_blockSize - m_queue[i]->getReadSpace());
        }
        processBlock(allFeatureSets, m_timestamp);
    }			
    
    // get remaining features			

    FeatureSet featureSet = m_plugin->getRemainingFeatures();

    for (map<int, FeatureList>::iterator iter = featureSet.begin();
         iter != featureSet.end(); ++iter) {
        FeatureList featureList = iter->second;
        for (size_t i = 0; i < featureList.size(); ++i) {
            allFeatureSets[iter->first].push_back(featureList[i]);
        }
    }
    
    return allFeatureSets;
}
    
void
PluginBufferingAdapter::Impl::processBlock(FeatureSet& allFeatureSets,
                                           RealTime timestamp)
{
    for (size_t i = 0; i < m_channels; ++i) {
        m_queue[i]->peek(m_buffers[i], m_blockSize);
    }

    FeatureSet featureSet = m_plugin->process(m_buffers, m_timestamp);
    
    for (map<int, FeatureList>::iterator iter = featureSet.begin();
         iter != featureSet.end(); ++iter) {
	
        FeatureList featureList = iter->second;
        int outputNo = iter->first;
	
        for (size_t i = 0; i < featureList.size(); ++i) {
            
            // make sure the timestamp is set
            switch (m_outputs[outputNo].sampleType) {

            case OutputDescriptor::OneSamplePerStep:
		// use our internal timestamp - OK????
                featureList[i].timestamp = m_timestamp;
                break;

            case OutputDescriptor::FixedSampleRate:
		// use our internal timestamp
                featureList[i].timestamp = m_timestamp;
                break;

            case OutputDescriptor::VariableSampleRate:
                break;		// plugin must set timestamp

            default:
                break;
            }
            
            allFeatureSets[outputNo].push_back(featureList[i]);		
        }
    }
    
    // step forward

    for (size_t i = 0; i < m_channels; ++i) {
        m_queue[i]->skip(m_stepSize);
    }
    
    // fake up the timestamp each time we step forward

    long frame = RealTime::realTime2Frame(m_timestamp,
                                          int(m_inputSampleRate + 0.5));
    m_timestamp = RealTime::frame2RealTime(frame + m_stepSize,
                                           int(m_inputSampleRate + 0.5));
}

}
	
}