annotate audio/EffectWrapper.h @ 739:ddfac001b543 audio-source-refactor

Introduce EffectWrapper for the auditioning effect
author Chris Cannam
date Thu, 19 Mar 2020 16:14:02 +0000
parents
children 54393ed09d65
rev   line source
Chris@739 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@739 2
Chris@739 3 /*
Chris@739 4 Sonic Visualiser
Chris@739 5 An audio file viewer and annotation editor.
Chris@739 6 Centre for Digital Music, Queen Mary, University of London.
Chris@739 7
Chris@739 8 This program is free software; you can redistribute it and/or
Chris@739 9 modify it under the terms of the GNU General Public License as
Chris@739 10 published by the Free Software Foundation; either version 2 of the
Chris@739 11 License, or (at your option) any later version. See the file
Chris@739 12 COPYING included with this distribution for more information.
Chris@739 13 */
Chris@739 14
Chris@739 15 #ifndef SV_EFFECT_WRAPPER_H
Chris@739 16 #define SV_EFFECT_WRAPPER_H
Chris@739 17
Chris@739 18 #include "bqaudioio/ApplicationPlaybackSource.h"
Chris@739 19
Chris@739 20 #include "base/BaseTypes.h"
Chris@739 21 #include "base/RingBuffer.h"
Chris@739 22
Chris@739 23 #include "plugin/RealTimePluginInstance.h"
Chris@739 24
Chris@739 25 #include <vector>
Chris@739 26 #include <mutex>
Chris@739 27 #include <memory>
Chris@739 28
Chris@739 29 /**
Chris@739 30 * A breakfastquay::ApplicationPlaybackSource wrapper that applies a
Chris@739 31 * real-time effect plugin.
Chris@739 32 */
Chris@739 33 class EffectWrapper : public breakfastquay::ApplicationPlaybackSource
Chris@739 34 {
Chris@739 35 public:
Chris@739 36 /**
Chris@739 37 * Create a wrapper around the given ApplicationPlaybackSource,
Chris@739 38 * implementing another ApplicationPlaybackSource interface that
Chris@739 39 * draws from the same source data but with an effect optionally
Chris@739 40 * applied.
Chris@739 41 *
Chris@739 42 * The wrapper does not take ownership of the wrapped
Chris@739 43 * ApplicationPlaybackSource, whose lifespan must exceed that of
Chris@739 44 * this object.
Chris@739 45 */
Chris@739 46 EffectWrapper(ApplicationPlaybackSource *source);
Chris@739 47 ~EffectWrapper();
Chris@739 48
Chris@739 49 /**
Chris@739 50 * Set the effect to apply. The effect instance is shared with the
Chris@739 51 * caller: the expectation is that the caller may continue to
Chris@739 52 * modify its parameters etc during auditioning. Replaces any
Chris@739 53 * instance previously set.
Chris@739 54 */
Chris@739 55 void setEffect(std::weak_ptr<RealTimePluginInstance>);
Chris@739 56
Chris@739 57 /**
Chris@739 58 * Remove any applied effect without setting another one.
Chris@739 59 */
Chris@739 60 void clearEffect();
Chris@739 61
Chris@739 62 /**
Chris@739 63 * Bypass or un-bypass the effect.
Chris@739 64 */
Chris@739 65 void setBypassed(bool bypassed);
Chris@739 66
Chris@739 67 /**
Chris@739 68 * Return true if the effect is bypassed.
Chris@739 69 */
Chris@739 70 bool isBypassed() const;
Chris@739 71
Chris@739 72 /**
Chris@739 73 * Clear any buffered data.
Chris@739 74 */
Chris@739 75 void reset();
Chris@739 76
Chris@739 77 // These functions are passed through to the wrapped
Chris@739 78 // ApplicationPlaybackSource
Chris@739 79
Chris@739 80 std::string getClientName() const override;
Chris@739 81 int getApplicationSampleRate() const override;
Chris@739 82 int getApplicationChannelCount() const override;
Chris@739 83
Chris@739 84 void setSystemPlaybackBlockSize(int) override;
Chris@739 85 void setSystemPlaybackSampleRate(int) override;
Chris@739 86 void setSystemPlaybackChannelCount(int) override;
Chris@739 87 void setSystemPlaybackLatency(int) override;
Chris@739 88
Chris@739 89 void setOutputLevels(float peakLeft, float peakRight) override;
Chris@739 90 void audioProcessingOverload() override;
Chris@739 91
Chris@739 92 /**
Chris@739 93 * Request some samples from the wrapped
Chris@739 94 * ApplicationPlaybackSource, apply effect if set, and return them
Chris@739 95 * to the target
Chris@739 96 */
Chris@739 97 int getSourceSamples(float *const *samples, int nchannels, int nframes)
Chris@739 98 override;
Chris@739 99
Chris@739 100 private:
Chris@739 101 ApplicationPlaybackSource *m_source;
Chris@739 102 std::weak_ptr<RealTimePluginInstance> m_effect;
Chris@739 103 bool m_bypassed;
Chris@739 104 bool m_failed;
Chris@739 105 int m_channelCount;
Chris@739 106 std::vector<RingBuffer<float>> m_effectOutputBuffers;
Chris@739 107 mutable std::mutex m_mutex;
Chris@739 108
Chris@739 109 EffectWrapper(const EffectWrapper &)=delete;
Chris@739 110 EffectWrapper &operator=(const EffectWrapper &)=delete;
Chris@739 111 };
Chris@739 112
Chris@739 113 #endif
Chris@739 114
Chris@739 115