1 /*
2     Copyright (c) 2019, Michael Fisher <mfisher@kushview.net>
3 
4     Permission to use, copy, modify, and/or distribute this software for any
5     purpose with or without fee is hereby granted, provided that the above
6     copyright notice and this permission notice appear in all copies.
7 
8     THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9     WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10     MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11     ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12     WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13     ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14     OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16 
17 /** @defgroup bufsize Buf Size
18     Access to buffer information
19 
20     The @ref BufSize extension scans for buffer information provided by the
21     host, and populates a @ref BufferDetails accordingly. To use it, add
22     BufSize to the template parameters of your Plugin and call buffer_details()
23     for buffer info
24 
25     @code
26     class MyPlug : public lvtk::Plugin<MyPlug, lvtk::BufSize> {
27     public:
28         MyPlug (lvtk::Args& args) : lvtk::Plugin (args) {
29             const auto& details = buffer_details();
30 
31             // setup plugin using details. members are optional
32             // and only set if provided by the host. e.g....
33 
34             if (details.min && details.max) {
35                 // got min/max do something with them
36             } else {
37                 // wasn't set...
38             }
39         }
40     };
41     @endcode
42 */
43 
44 #pragma once
45 
46 #include <lv2/lv2plug.in/ns/ext/buf-size/buf-size.h>
47 #include <lvtk/ext/options.hpp>
48 #include <lvtk/ext/urid.hpp>
49 #include <lvtk/optional.hpp>
50 
51 #ifndef LV2_BUF_SIZE__nominalBlockLength
52  #define LV2_BUF_SIZE__nominalBlockLength LV2_BUF_SIZE_PREFIX "nominalBlockLength"
53 #endif
54 
55 namespace lvtk {
56 /* @{ */
57 /** Description of buffer information.
58 
59     Used by the @ref BufSize extension to automatically scan for buffer details
60     during instantiation. @see @ref BufSize
61 
62     @headerfile lvtk/ext/bufsize.hpp
63  */
64 struct BufferDetails final {
65     Optional<uint32_t> min;             /**< <http://lv2plug.in/ns/ext/buf-size#minBlockLength> */
66     Optional<uint32_t> max;             /**< <http://lv2plug.in/ns/ext/buf-size#maxBlockLength> */
67     Optional<uint32_t> nominal;         /**< <http://lv2plug.in/ns/ext/buf-size#nominalBlockLength> */
68     Optional<uint32_t> sequence_size;   /**< <http://lv2plug.in/ns/ext/buf-size#sequenceSize> */
69 
70     /** Update with Options. Updates `min`, `max`, and `nominal`, and
71         `sequence_size` if found in the Option array
72 
73         @param map      Map for getting required LV2_URIDs
74         @param options  The Options array to scan. MUST be valid with a
75                         zeroed option at the end.
76      */
apply_optionslvtk::BufferDetails77     void apply_options (Map& map, const Option* options) {
78         uint32_t minkey = map (LV2_BUF_SIZE__minBlockLength);
79         uint32_t maxkey = map (LV2_BUF_SIZE__maxBlockLength);
80         uint32_t nomkey = map (LV2_BUF_SIZE__nominalBlockLength);
81         uint32_t seqkey = map (LV2_BUF_SIZE__sequenceSize);
82 
83         for (uint32_t i = 0;;++i)
84         {
85             const auto& opt = options [i];
86             if (opt.key == 0 || opt.value == nullptr)
87                 break;
88 
89             if (minkey == opt.key)
90                 min = *(uint32_t*) opt.value;
91             else if (maxkey == opt.key)
92                 max = *(uint32_t*) opt.value;
93             else if (seqkey == opt.key)
94                 sequence_size = *(uint32_t*) opt.value;
95             else if (nomkey == opt.key)
96                 nominal = *(uint32_t*) opt.value;
97         }
98     }
99 };
100 
101 /** LV2 Buf Size Extension
102     @headerfile lvtk/ext/bufsize.hpp
103  */
104 template<class I>
105 struct BufSize : NullExtension
106 {
107     /** @private */
BufSizelvtk::BufSize108     BufSize (const FeatureList& features)
109     {
110         memset (&details, 0, sizeof(BufferDetails));
111         Map map; OptionsData options;
112         for (const auto& f : features) {
113             if (! map) map.set (f);
114             if (! options) options.set (f);
115             if (map && options) {
116                 details.apply_options (map, options);
117                 break;
118             }
119         }
120     }
121 
122     /** Get the buffer details
123 
124         If the required features and options were available when instantiated,
125         This will be populated with host buffer information
126 
127         @returns The host provided buffer details
128      */
buffer_detailslvtk::BufSize129     const BufferDetails& buffer_details() const { return details; }
130 
131 private:
132     BufferDetails details;
133 };
134 /* @} */
135 }
136