Getting an const dsp::AudioBlock out of a const AudioBuffer


I don’t see a (non-ugly, involving const_casts etc.) way to get a const dsp::AudioBlock view of a const AudioBuffer. Is there one?

A nice way to do it would be to have a “const constructor” for dsp::AudioBlock which would accept a const AudioBuffer& and produce a const dsp::AudioBlock. Unfortunately, C++ doesn’t have such a thing as a “const constructor” :slight_smile:

My use case is that I have an existing method returning const AudioBuffer&, and I would like to get a view of a channel of that buffer, using dsp::AudioBlock. Of course, it’s possible to just change the existing method, but that would entail more changes to any other users of the existing interface. Being able to more gradually integrate dsp::AudioBlock with existing use of AudioBuffer would be nice.

Edit: Trying to modify my method to return a dsp::AudioBlock instead of an const AudioBuffer& doesn’t work either, since my method is const, and so the AudioBuffer member is considered const, so constructing a dsp::AudioBlock from it fails (it also makes sense logically that it does - returning a dsp::AudioBlock would allow any caller to later modify the data it refers to, that is the AudioBuffer member, which shouldn’t be possible since the method is const).

Edit2: On second thought, even if I could get a const dsp::AudioBlock, once I copy it to a non-const one I would be able to modify the underlying buffer. So I guess it just come down to needing a different class for non-modifiable slices of buffers (similar to std::string_view).

Thoughts / suggestions?


Bumping due to no reply. JUCE team, would love to hear your thoughts about how to support slicing const AudioBuffers with existing classes, or whether you think a new class is required.