├── .gitignore ├── meson_options.txt ├── plugin.h ├── README.md ├── plugin.cpp ├── gstvstaudioprocessor.h ├── meson.build ├── COPYING └── gstvstaudioprocessor.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | -------------------------------------------------------------------------------- /meson_options.txt: -------------------------------------------------------------------------------- 1 | option('vst-libdir', type : 'string', value : '/opt/vst3/lib', 2 | description : 'Directory with VST SDK3 libraries (e.g. libsdk.a, libbase.a)') 3 | option('vst-sdkdir', type : 'string', value : '/opt/vst3', 4 | description : 'Directory with VST SDK3 headers (e.g. public.sdk/source/vst/hosting/module.h)') 5 | -------------------------------------------------------------------------------- /plugin.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017 Sebastian Dröge 3 | * 4 | * This library is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU Lesser General Public 6 | * License as published by the Free Software Foundation; either 7 | * version 2.1 of the License, or (at your option) any later version. 8 | * 9 | * This library is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public 15 | * License along with this library; if not, write to the Free Software 16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | * 18 | */ 19 | 20 | #include 21 | #include 22 | 23 | #ifndef __GST_VST_PLUGIN_H__ 24 | #define __GST_VST_PLUGIN_H__ 25 | 26 | namespace Steinberg { 27 | extern FUnknown* gStandardPluginContext; 28 | }; 29 | 30 | #endif /* __GST_VST_PLUGIN_H__ */ 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gstreamer-vst3 2 | 3 | [GStreamer](https://gstreamer.freedesktop.org/) plugin for 4 | [Steinberg VST3](https://www.steinberg.net/en/company/technologies/vst3.html) audio plugins. 5 | 6 | Currently only mono and stereo plugins that implement the `IAudioProcessor` 7 | interface are supported. The plugin should work fine on Linux, Windows and 8 | macOS. 9 | 10 | To compile this, the [VST3 SDK](https://www.steinberg.net/en/company/developers.html) has to 11 | be downloaded and compiled. The paths of the SDK and the build directory must 12 | be provided to meson via `-Dvst-sdkdir=...` and `-Dvst-libdir=...` 13 | 14 | Some sample VST plugins are included in the SDK. 15 | 16 | ## Environment variables 17 | 18 | This plugin will parse two environment variables for the purpose of VST3 19 | plugin discovery: 20 | 21 | * `GST_VST3_PLUGIN_PATH`: A colon-separated list of paths to look for plugins 22 | into, eg `/home/foo:/home/bar` 23 | 24 | * `GST_VST3_SEARCH_DEFAULT_PATHS`: If set to (case-insensitive) `no`, plugins 25 | will only be looked for in the paths listed in the `GST_VST3_PLUGIN_PATH` 26 | environment variable. The default behaviour is to search in the default paths. 27 | 28 | * `GST_VST3_BLACKLIST`: A semicolon-separated of vendor::name pairs to blacklist, 29 | eg `"mda::mda Overdrive;mda::mda Bandisto"` 30 | 31 | ## LICENSE 32 | 33 | GStreamer and gstreamer-vst3 is licensed under the [Lesser General Public 34 | License version 2.1](COPYING) or (at your option) any later version. 35 | 36 | The Steinberg VST3 SDK is licensed under the [3-clause BSD 37 | license](https://opensource.org/licenses/BSD-3-Clause). 38 | -------------------------------------------------------------------------------- /plugin.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017 Sebastian Dröge 3 | * 4 | * This library is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU Lesser General Public 6 | * License as published by the Free Software Foundation; either 7 | * version 2.1 of the License, or (at your option) any later version. 8 | * 9 | * This library is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public 15 | * License along with this library; if not, write to the Free Software 16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | * 18 | */ 19 | 20 | #include "plugin.h" 21 | #include 22 | #include 23 | #include 24 | 25 | #include 26 | 27 | #include "gstvstaudioprocessor.h" 28 | 29 | using namespace Steinberg; 30 | 31 | namespace Steinberg { 32 | DEF_CLASS_IID(Vst::IPlugProvider) 33 | 34 | FUnknown* gStandardPluginContext = nullptr; 35 | }; 36 | 37 | class GStreamerHostApplication: public Vst::HostApplication { 38 | public: 39 | GStreamerHostApplication() { } 40 | 41 | tresult PLUGIN_API getName(Vst::String128 name) override 42 | { 43 | String str ("GStreamer VST Plugin"); 44 | str.copyTo16 (name, 0, 127); 45 | return kResultTrue; 46 | } 47 | }; 48 | 49 | DECLARE_CLASS_IID(GStreamerHostApplication, 0x696c109c, 0x40dd4aed, 0xb272bebe, 0xc27b75d8) 50 | 51 | static gboolean 52 | plugin_init(GstPlugin * plugin) 53 | { 54 | gStandardPluginContext = new GStreamerHostApplication(); 55 | 56 | gst_vst_audio_processor_register(plugin); 57 | 58 | return TRUE; 59 | } 60 | 61 | GST_PLUGIN_DEFINE(GST_VERSION_MAJOR, 62 | GST_VERSION_MINOR, 63 | vst3, 64 | "Steinberg VST3 plugin", 65 | plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN) 66 | 67 | -------------------------------------------------------------------------------- /gstvstaudioprocessor.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017 Sebastian Dröge 3 | * 4 | * This library is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU Lesser General Public 6 | * License as published by the Free Software Foundation; either 7 | * version 2.1 of the License, or (at your option) any later version. 8 | * 9 | * This library is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public 15 | * License along with this library; if not, write to the Free Software 16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | * 18 | */ 19 | 20 | #include 21 | 22 | #ifndef __GST_VST_AUDIO_PROCESSOR_H__ 23 | #define __GST_VST_AUDIO_PROCESSOR_H__ 24 | 25 | /* C declarations here */ 26 | 27 | G_BEGIN_DECLS 28 | 29 | #define GST_TYPE_VST_AUDIO_PROCESSOR \ 30 | (gst_vst_audio_processor_get_type()) 31 | #define GST_VST_AUDIO_PROCESSOR(obj) \ 32 | (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_VST_AUDIO_PROCESSOR, GstVstAudioProcessor)) 33 | #define GST_VST_AUDIO_PROCESSOR_CLASS(klass) \ 34 | (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_VST_AUDIO_PROCESSOR, GstVstAudioProcessorClass)) 35 | #define GST_VST_AUDIO_PROCESSOR_GET_CLASS(obj) \ 36 | (G_TYPE_INSTANCE_GET_CLASS((obj),GST_TYPE_VST_AUDIO_PROCESSOR,GstVstAudioProcessorClass)) 37 | #define GST_IS_VST_AUDIO_PROCESSOR(obj) \ 38 | (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_VST_AUDIO_PROCESSOR)) 39 | #define GST_IS_VST_AUDIO_PROCESSOR_CLASS(klass) \ 40 | (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_VST_AUDIO_PROCESSOR)) 41 | 42 | typedef struct _GstVstAudioProcessor GstVstAudioProcessor; 43 | typedef struct _GstVstAudioProcessorClass GstVstAudioProcessorClass; 44 | typedef struct _GstVstAudioProcessorInfo GstVstAudioProcessorInfo; 45 | 46 | GType gst_vst_audio_processor_get_type(void); 47 | 48 | void gst_vst_audio_processor_register(GstPlugin * plugin); 49 | 50 | G_END_DECLS 51 | 52 | #endif /* __GST_VST_AUDIO_PROCESSOR_H__ */ 53 | -------------------------------------------------------------------------------- /meson.build: -------------------------------------------------------------------------------- 1 | project('gstreamer-vst3', 'cpp', 2 | version : '0.1.0', 3 | meson_version : '>= 0.36.0', 4 | default_options : [ 'warning_level=1', 5 | 'buildtype=debugoptimized', 6 | 'cpp_std=c++11']) 7 | 8 | plugins_install_dir = '@0@/gstreamer-1.0'.format(get_option('libdir')) 9 | 10 | cxx = meson.get_compiler('cpp') 11 | 12 | # FIXME: Requires GStreamer >= 1.12.3 13 | #if cxx.has_argument('-fvisibility=hidden') 14 | # add_project_arguments('-fvisibility=hidden', language: 'cpp') 15 | #endif 16 | 17 | if cxx.get_id() == 'msvc' 18 | # Ignore several spurious warnings for things gstreamer does very commonly 19 | # If a warning is completely useless and spammy, use '/wdXXXX' to suppress it 20 | # If a warning is harmless but hard to fix, use '/woXXXX' so it's shown once 21 | # NOTE: Only add warnings here if you are sure they're spurious 22 | test_cppflags = [] 23 | msvc_args = [ 24 | '/wd4018', # implicit signed/unsigned conversion 25 | '/wd4146', # unary minus on unsigned (beware INT_MIN) 26 | '/wd4244', # lossy type conversion (e.g. double -> int) 27 | '/wd4305', # truncating type conversion (e.g. double -> float) 28 | ] 29 | add_project_arguments(msvc_args, language : 'cpp') 30 | # Disable SAFESEH with MSVC for plugins and libs that use external deps that 31 | # are built with MinGW 32 | noseh_link_args = ['/SAFESEH:NO'] 33 | else 34 | test_cppflags = ['-Wno-non-virtual-dtor'] 35 | noseh_link_args = [] 36 | endif 37 | 38 | common_flags = [] 39 | foreach cxxflag: test_cppflags 40 | if cxx.has_argument(cxxflag) 41 | common_flags += [ cxxflag ] 42 | endif 43 | endforeach 44 | 45 | gst_dep = dependency('gstreamer-1.0', version : '>= 1.8', required : true) 46 | gstbase_dep = dependency('gstreamer-base-1.0', version : '>= 1.8', required : true) 47 | gstaudio_dep = dependency('gstreamer-audio-1.0', version : '>= 1.8', required : true) 48 | 49 | vst_sdkdir = get_option('vst-sdkdir') 50 | message('Looking for VST3 SDK in directory ' + vst_sdkdir) 51 | if not cxx.has_header('vst/hosting/module.h', 52 | args : ['-I@0@/public.sdk/source'.format(vst_sdkdir), 53 | '-I' + vst_sdkdir]) 54 | error('Cannot find VST3 SDK') 55 | endif 56 | if not cxx.has_header('pluginterfaces/vst/ivstaudioprocessor.h', args : '-I@0@'.format(vst_sdkdir)) 57 | error('Cannot find VST3 SDK plug interfaces') 58 | endif 59 | 60 | vst_includedir = '@0@/public.sdk/source'.format(vst_sdkdir) 61 | vst_sourcedir = '@0@/public.sdk/source/'.format(vst_sdkdir) 62 | vst_pluginterfaces_includedir = '@0@'.format(vst_sdkdir) 63 | 64 | vst_sources = [ 65 | vst_sourcedir + 'vst/vstinitiids.cpp', 66 | vst_sourcedir + 'vst/hosting/hostclasses.cpp', 67 | vst_sourcedir + 'vst/hosting/module.cpp', 68 | vst_sourcedir + 'vst/hosting/parameterchanges.cpp', 69 | vst_sourcedir + 'vst/hosting/stringconvert.cpp', 70 | vst_sourcedir + 'common/memorystream.cpp', 71 | ] 72 | 73 | if host_machine.system() == 'windows' 74 | vst_platform_sources = [ 75 | vst_sourcedir + 'vst/hosting/module_win32.cpp', 76 | ] 77 | platform_deps = [] 78 | elif host_machine.system() == 'linux' 79 | vst_platform_sources = [ 80 | vst_sourcedir + 'vst/hosting/module_linux.cpp', 81 | ] 82 | threads_dep = dependency('threads', required : true) 83 | dl_dep = cxx.find_library('dl', required : true) 84 | 85 | # Need to do something different for clang and MSVC here 86 | libcpp_fs_dep = cxx.find_library('libstdc++fs', required : true) 87 | 88 | platform_deps = [dl_dep, threads_dep, libcpp_fs_dep] 89 | elif host_machine.system() == 'osx' 90 | vst_platform_sources = [ 91 | vst_sourcedir + 'vst/hosting/module_mac.mm', 92 | ] 93 | platform_deps = [] 94 | else 95 | error('Only Windows, Linux and macOS are supported currently') 96 | endif 97 | 98 | if get_option('buildtype') == 'debug' 99 | vst_cpp_args = ['-DDEVELOPMENT'] 100 | elif get_option('buildtype') == 'release' 101 | vst_cpp_args = ['-DRELEASE'] 102 | else 103 | vst_cpp_args = ['-DDEVELOPMENT'] 104 | endif 105 | 106 | if cxx.has_function('gst_get_main_executable_path', 107 | prefix: '#include ', 108 | dependencies: gst_dep) 109 | vst_cpp_args += ['-DHAVE_GST_EXE_PATH'] 110 | endif 111 | 112 | libbase_dep = cxx.find_library('base', required : true, 113 | dirs : [get_option('vst-libdir')]) 114 | libsdk_dep = cxx.find_library('sdk', required : true, 115 | dirs : [get_option('vst-libdir')]) 116 | 117 | gstvst3 = library('gstvst3', 118 | ['plugin.cpp', 'gstvstaudioprocessor.cpp'] + vst_sources + vst_platform_sources, 119 | cpp_args : [ 120 | '-I@0@'.format(vst_includedir), 121 | '-I@0@'.format(vst_pluginterfaces_includedir), 122 | '-DPACKAGE="gstreamer-vst3"', 123 | '-DGST_PACKAGE_NAME="gstreamer-vst3"', 124 | '-DGST_PACKAGE_ORIGIN="https://github.com/centricular/gstreamer-vst3"', 125 | '-DVERSION="@0@"'.format(meson.project_version())] + common_flags + vst_cpp_args, 126 | link_args : noseh_link_args, 127 | dependencies : [gstaudio_dep, gstbase_dep, gst_dep, libbase_dep, libsdk_dep] + platform_deps, 128 | install : true, 129 | install_dir : plugins_install_dir, 130 | ) 131 | 132 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 2.1, February 1999 3 | 4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | [This is the first released version of the Lesser GPL. It also counts 10 | as the successor of the GNU Library Public License, version 2, hence 11 | the version number 2.1.] 12 | 13 | Preamble 14 | 15 | The licenses for most software are designed to take away your 16 | freedom to share and change it. By contrast, the GNU General Public 17 | Licenses are intended to guarantee your freedom to share and change 18 | free software--to make sure the software is free for all its users. 19 | 20 | This license, the Lesser General Public License, applies to some 21 | specially designated software packages--typically libraries--of the 22 | Free Software Foundation and other authors who decide to use it. You 23 | can use it too, but we suggest you first think carefully about whether 24 | this license or the ordinary General Public License is the better 25 | strategy to use in any particular case, based on the explanations below. 26 | 27 | When we speak of free software, we are referring to freedom of use, 28 | not price. Our General Public Licenses are designed to make sure that 29 | you have the freedom to distribute copies of free software (and charge 30 | for this service if you wish); that you receive source code or can get 31 | it if you want it; that you can change the software and use pieces of 32 | it in new free programs; and that you are informed that you can do 33 | these things. 34 | 35 | To protect your rights, we need to make restrictions that forbid 36 | distributors to deny you these rights or to ask you to surrender these 37 | rights. These restrictions translate to certain responsibilities for 38 | you if you distribute copies of the library or if you modify it. 39 | 40 | For example, if you distribute copies of the library, whether gratis 41 | or for a fee, you must give the recipients all the rights that we gave 42 | you. You must make sure that they, too, receive or can get the source 43 | code. If you link other code with the library, you must provide 44 | complete object files to the recipients, so that they can relink them 45 | with the library after making changes to the library and recompiling 46 | it. And you must show them these terms so they know their rights. 47 | 48 | We protect your rights with a two-step method: (1) we copyright the 49 | library, and (2) we offer you this license, which gives you legal 50 | permission to copy, distribute and/or modify the library. 51 | 52 | To protect each distributor, we want to make it very clear that 53 | there is no warranty for the free library. Also, if the library is 54 | modified by someone else and passed on, the recipients should know 55 | that what they have is not the original version, so that the original 56 | author's reputation will not be affected by problems that might be 57 | introduced by others. 58 | 59 | Finally, software patents pose a constant threat to the existence of 60 | any free program. We wish to make sure that a company cannot 61 | effectively restrict the users of a free program by obtaining a 62 | restrictive license from a patent holder. Therefore, we insist that 63 | any patent license obtained for a version of the library must be 64 | consistent with the full freedom of use specified in this license. 65 | 66 | Most GNU software, including some libraries, is covered by the 67 | ordinary GNU General Public License. This license, the GNU Lesser 68 | General Public License, applies to certain designated libraries, and 69 | is quite different from the ordinary General Public License. We use 70 | this license for certain libraries in order to permit linking those 71 | libraries into non-free programs. 72 | 73 | When a program is linked with a library, whether statically or using 74 | a shared library, the combination of the two is legally speaking a 75 | combined work, a derivative of the original library. The ordinary 76 | General Public License therefore permits such linking only if the 77 | entire combination fits its criteria of freedom. The Lesser General 78 | Public License permits more lax criteria for linking other code with 79 | the library. 80 | 81 | We call this license the "Lesser" General Public License because it 82 | does Less to protect the user's freedom than the ordinary General 83 | Public License. It also provides other free software developers Less 84 | of an advantage over competing non-free programs. These disadvantages 85 | are the reason we use the ordinary General Public License for many 86 | libraries. However, the Lesser license provides advantages in certain 87 | special circumstances. 88 | 89 | For example, on rare occasions, there may be a special need to 90 | encourage the widest possible use of a certain library, so that it becomes 91 | a de-facto standard. To achieve this, non-free programs must be 92 | allowed to use the library. A more frequent case is that a free 93 | library does the same job as widely used non-free libraries. In this 94 | case, there is little to gain by limiting the free library to free 95 | software only, so we use the Lesser General Public License. 96 | 97 | In other cases, permission to use a particular library in non-free 98 | programs enables a greater number of people to use a large body of 99 | free software. For example, permission to use the GNU C Library in 100 | non-free programs enables many more people to use the whole GNU 101 | operating system, as well as its variant, the GNU/Linux operating 102 | system. 103 | 104 | Although the Lesser General Public License is Less protective of the 105 | users' freedom, it does ensure that the user of a program that is 106 | linked with the Library has the freedom and the wherewithal to run 107 | that program using a modified version of the Library. 108 | 109 | The precise terms and conditions for copying, distribution and 110 | modification follow. Pay close attention to the difference between a 111 | "work based on the library" and a "work that uses the library". The 112 | former contains code derived from the library, whereas the latter must 113 | be combined with the library in order to run. 114 | 115 | GNU LESSER GENERAL PUBLIC LICENSE 116 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 117 | 118 | 0. This License Agreement applies to any software library or other 119 | program which contains a notice placed by the copyright holder or 120 | other authorized party saying it may be distributed under the terms of 121 | this Lesser General Public License (also called "this License"). 122 | Each licensee is addressed as "you". 123 | 124 | A "library" means a collection of software functions and/or data 125 | prepared so as to be conveniently linked with application programs 126 | (which use some of those functions and data) to form executables. 127 | 128 | The "Library", below, refers to any such software library or work 129 | which has been distributed under these terms. A "work based on the 130 | Library" means either the Library or any derivative work under 131 | copyright law: that is to say, a work containing the Library or a 132 | portion of it, either verbatim or with modifications and/or translated 133 | straightforwardly into another language. (Hereinafter, translation is 134 | included without limitation in the term "modification".) 135 | 136 | "Source code" for a work means the preferred form of the work for 137 | making modifications to it. For a library, complete source code means 138 | all the source code for all modules it contains, plus any associated 139 | interface definition files, plus the scripts used to control compilation 140 | and installation of the library. 141 | 142 | Activities other than copying, distribution and modification are not 143 | covered by this License; they are outside its scope. The act of 144 | running a program using the Library is not restricted, and output from 145 | such a program is covered only if its contents constitute a work based 146 | on the Library (independent of the use of the Library in a tool for 147 | writing it). Whether that is true depends on what the Library does 148 | and what the program that uses the Library does. 149 | 150 | 1. You may copy and distribute verbatim copies of the Library's 151 | complete source code as you receive it, in any medium, provided that 152 | you conspicuously and appropriately publish on each copy an 153 | appropriate copyright notice and disclaimer of warranty; keep intact 154 | all the notices that refer to this License and to the absence of any 155 | warranty; and distribute a copy of this License along with the 156 | Library. 157 | 158 | You may charge a fee for the physical act of transferring a copy, 159 | and you may at your option offer warranty protection in exchange for a 160 | fee. 161 | 162 | 2. You may modify your copy or copies of the Library or any portion 163 | of it, thus forming a work based on the Library, and copy and 164 | distribute such modifications or work under the terms of Section 1 165 | above, provided that you also meet all of these conditions: 166 | 167 | a) The modified work must itself be a software library. 168 | 169 | b) You must cause the files modified to carry prominent notices 170 | stating that you changed the files and the date of any change. 171 | 172 | c) You must cause the whole of the work to be licensed at no 173 | charge to all third parties under the terms of this License. 174 | 175 | d) If a facility in the modified Library refers to a function or a 176 | table of data to be supplied by an application program that uses 177 | the facility, other than as an argument passed when the facility 178 | is invoked, then you must make a good faith effort to ensure that, 179 | in the event an application does not supply such function or 180 | table, the facility still operates, and performs whatever part of 181 | its purpose remains meaningful. 182 | 183 | (For example, a function in a library to compute square roots has 184 | a purpose that is entirely well-defined independent of the 185 | application. Therefore, Subsection 2d requires that any 186 | application-supplied function or table used by this function must 187 | be optional: if the application does not supply it, the square 188 | root function must still compute square roots.) 189 | 190 | These requirements apply to the modified work as a whole. If 191 | identifiable sections of that work are not derived from the Library, 192 | and can be reasonably considered independent and separate works in 193 | themselves, then this License, and its terms, do not apply to those 194 | sections when you distribute them as separate works. But when you 195 | distribute the same sections as part of a whole which is a work based 196 | on the Library, the distribution of the whole must be on the terms of 197 | this License, whose permissions for other licensees extend to the 198 | entire whole, and thus to each and every part regardless of who wrote 199 | it. 200 | 201 | Thus, it is not the intent of this section to claim rights or contest 202 | your rights to work written entirely by you; rather, the intent is to 203 | exercise the right to control the distribution of derivative or 204 | collective works based on the Library. 205 | 206 | In addition, mere aggregation of another work not based on the Library 207 | with the Library (or with a work based on the Library) on a volume of 208 | a storage or distribution medium does not bring the other work under 209 | the scope of this License. 210 | 211 | 3. You may opt to apply the terms of the ordinary GNU General Public 212 | License instead of this License to a given copy of the Library. To do 213 | this, you must alter all the notices that refer to this License, so 214 | that they refer to the ordinary GNU General Public License, version 2, 215 | instead of to this License. (If a newer version than version 2 of the 216 | ordinary GNU General Public License has appeared, then you can specify 217 | that version instead if you wish.) Do not make any other change in 218 | these notices. 219 | 220 | Once this change is made in a given copy, it is irreversible for 221 | that copy, so the ordinary GNU General Public License applies to all 222 | subsequent copies and derivative works made from that copy. 223 | 224 | This option is useful when you wish to copy part of the code of 225 | the Library into a program that is not a library. 226 | 227 | 4. You may copy and distribute the Library (or a portion or 228 | derivative of it, under Section 2) in object code or executable form 229 | under the terms of Sections 1 and 2 above provided that you accompany 230 | it with the complete corresponding machine-readable source code, which 231 | must be distributed under the terms of Sections 1 and 2 above on a 232 | medium customarily used for software interchange. 233 | 234 | If distribution of object code is made by offering access to copy 235 | from a designated place, then offering equivalent access to copy the 236 | source code from the same place satisfies the requirement to 237 | distribute the source code, even though third parties are not 238 | compelled to copy the source along with the object code. 239 | 240 | 5. A program that contains no derivative of any portion of the 241 | Library, but is designed to work with the Library by being compiled or 242 | linked with it, is called a "work that uses the Library". Such a 243 | work, in isolation, is not a derivative work of the Library, and 244 | therefore falls outside the scope of this License. 245 | 246 | However, linking a "work that uses the Library" with the Library 247 | creates an executable that is a derivative of the Library (because it 248 | contains portions of the Library), rather than a "work that uses the 249 | library". The executable is therefore covered by this License. 250 | Section 6 states terms for distribution of such executables. 251 | 252 | When a "work that uses the Library" uses material from a header file 253 | that is part of the Library, the object code for the work may be a 254 | derivative work of the Library even though the source code is not. 255 | Whether this is true is especially significant if the work can be 256 | linked without the Library, or if the work is itself a library. The 257 | threshold for this to be true is not precisely defined by law. 258 | 259 | If such an object file uses only numerical parameters, data 260 | structure layouts and accessors, and small macros and small inline 261 | functions (ten lines or less in length), then the use of the object 262 | file is unrestricted, regardless of whether it is legally a derivative 263 | work. (Executables containing this object code plus portions of the 264 | Library will still fall under Section 6.) 265 | 266 | Otherwise, if the work is a derivative of the Library, you may 267 | distribute the object code for the work under the terms of Section 6. 268 | Any executables containing that work also fall under Section 6, 269 | whether or not they are linked directly with the Library itself. 270 | 271 | 6. As an exception to the Sections above, you may also combine or 272 | link a "work that uses the Library" with the Library to produce a 273 | work containing portions of the Library, and distribute that work 274 | under terms of your choice, provided that the terms permit 275 | modification of the work for the customer's own use and reverse 276 | engineering for debugging such modifications. 277 | 278 | You must give prominent notice with each copy of the work that the 279 | Library is used in it and that the Library and its use are covered by 280 | this License. You must supply a copy of this License. If the work 281 | during execution displays copyright notices, you must include the 282 | copyright notice for the Library among them, as well as a reference 283 | directing the user to the copy of this License. Also, you must do one 284 | of these things: 285 | 286 | a) Accompany the work with the complete corresponding 287 | machine-readable source code for the Library including whatever 288 | changes were used in the work (which must be distributed under 289 | Sections 1 and 2 above); and, if the work is an executable linked 290 | with the Library, with the complete machine-readable "work that 291 | uses the Library", as object code and/or source code, so that the 292 | user can modify the Library and then relink to produce a modified 293 | executable containing the modified Library. (It is understood 294 | that the user who changes the contents of definitions files in the 295 | Library will not necessarily be able to recompile the application 296 | to use the modified definitions.) 297 | 298 | b) Use a suitable shared library mechanism for linking with the 299 | Library. A suitable mechanism is one that (1) uses at run time a 300 | copy of the library already present on the user's computer system, 301 | rather than copying library functions into the executable, and (2) 302 | will operate properly with a modified version of the library, if 303 | the user installs one, as long as the modified version is 304 | interface-compatible with the version that the work was made with. 305 | 306 | c) Accompany the work with a written offer, valid for at 307 | least three years, to give the same user the materials 308 | specified in Subsection 6a, above, for a charge no more 309 | than the cost of performing this distribution. 310 | 311 | d) If distribution of the work is made by offering access to copy 312 | from a designated place, offer equivalent access to copy the above 313 | specified materials from the same place. 314 | 315 | e) Verify that the user has already received a copy of these 316 | materials or that you have already sent this user a copy. 317 | 318 | For an executable, the required form of the "work that uses the 319 | Library" must include any data and utility programs needed for 320 | reproducing the executable from it. However, as a special exception, 321 | the materials to be distributed need not include anything that is 322 | normally distributed (in either source or binary form) with the major 323 | components (compiler, kernel, and so on) of the operating system on 324 | which the executable runs, unless that component itself accompanies 325 | the executable. 326 | 327 | It may happen that this requirement contradicts the license 328 | restrictions of other proprietary libraries that do not normally 329 | accompany the operating system. Such a contradiction means you cannot 330 | use both them and the Library together in an executable that you 331 | distribute. 332 | 333 | 7. You may place library facilities that are a work based on the 334 | Library side-by-side in a single library together with other library 335 | facilities not covered by this License, and distribute such a combined 336 | library, provided that the separate distribution of the work based on 337 | the Library and of the other library facilities is otherwise 338 | permitted, and provided that you do these two things: 339 | 340 | a) Accompany the combined library with a copy of the same work 341 | based on the Library, uncombined with any other library 342 | facilities. This must be distributed under the terms of the 343 | Sections above. 344 | 345 | b) Give prominent notice with the combined library of the fact 346 | that part of it is a work based on the Library, and explaining 347 | where to find the accompanying uncombined form of the same work. 348 | 349 | 8. You may not copy, modify, sublicense, link with, or distribute 350 | the Library except as expressly provided under this License. Any 351 | attempt otherwise to copy, modify, sublicense, link with, or 352 | distribute the Library is void, and will automatically terminate your 353 | rights under this License. However, parties who have received copies, 354 | or rights, from you under this License will not have their licenses 355 | terminated so long as such parties remain in full compliance. 356 | 357 | 9. You are not required to accept this License, since you have not 358 | signed it. However, nothing else grants you permission to modify or 359 | distribute the Library or its derivative works. These actions are 360 | prohibited by law if you do not accept this License. Therefore, by 361 | modifying or distributing the Library (or any work based on the 362 | Library), you indicate your acceptance of this License to do so, and 363 | all its terms and conditions for copying, distributing or modifying 364 | the Library or works based on it. 365 | 366 | 10. Each time you redistribute the Library (or any work based on the 367 | Library), the recipient automatically receives a license from the 368 | original licensor to copy, distribute, link with or modify the Library 369 | subject to these terms and conditions. You may not impose any further 370 | restrictions on the recipients' exercise of the rights granted herein. 371 | You are not responsible for enforcing compliance by third parties with 372 | this License. 373 | 374 | 11. If, as a consequence of a court judgment or allegation of patent 375 | infringement or for any other reason (not limited to patent issues), 376 | conditions are imposed on you (whether by court order, agreement or 377 | otherwise) that contradict the conditions of this License, they do not 378 | excuse you from the conditions of this License. If you cannot 379 | distribute so as to satisfy simultaneously your obligations under this 380 | License and any other pertinent obligations, then as a consequence you 381 | may not distribute the Library at all. For example, if a patent 382 | license would not permit royalty-free redistribution of the Library by 383 | all those who receive copies directly or indirectly through you, then 384 | the only way you could satisfy both it and this License would be to 385 | refrain entirely from distribution of the Library. 386 | 387 | If any portion of this section is held invalid or unenforceable under any 388 | particular circumstance, the balance of the section is intended to apply, 389 | and the section as a whole is intended to apply in other circumstances. 390 | 391 | It is not the purpose of this section to induce you to infringe any 392 | patents or other property right claims or to contest validity of any 393 | such claims; this section has the sole purpose of protecting the 394 | integrity of the free software distribution system which is 395 | implemented by public license practices. Many people have made 396 | generous contributions to the wide range of software distributed 397 | through that system in reliance on consistent application of that 398 | system; it is up to the author/donor to decide if he or she is willing 399 | to distribute software through any other system and a licensee cannot 400 | impose that choice. 401 | 402 | This section is intended to make thoroughly clear what is believed to 403 | be a consequence of the rest of this License. 404 | 405 | 12. If the distribution and/or use of the Library is restricted in 406 | certain countries either by patents or by copyrighted interfaces, the 407 | original copyright holder who places the Library under this License may add 408 | an explicit geographical distribution limitation excluding those countries, 409 | so that distribution is permitted only in or among countries not thus 410 | excluded. In such case, this License incorporates the limitation as if 411 | written in the body of this License. 412 | 413 | 13. The Free Software Foundation may publish revised and/or new 414 | versions of the Lesser General Public License from time to time. 415 | Such new versions will be similar in spirit to the present version, 416 | but may differ in detail to address new problems or concerns. 417 | 418 | Each version is given a distinguishing version number. If the Library 419 | specifies a version number of this License which applies to it and 420 | "any later version", you have the option of following the terms and 421 | conditions either of that version or of any later version published by 422 | the Free Software Foundation. If the Library does not specify a 423 | license version number, you may choose any version ever published by 424 | the Free Software Foundation. 425 | 426 | 14. If you wish to incorporate parts of the Library into other free 427 | programs whose distribution conditions are incompatible with these, 428 | write to the author to ask for permission. For software which is 429 | copyrighted by the Free Software Foundation, write to the Free 430 | Software Foundation; we sometimes make exceptions for this. Our 431 | decision will be guided by the two goals of preserving the free status 432 | of all derivatives of our free software and of promoting the sharing 433 | and reuse of software generally. 434 | 435 | NO WARRANTY 436 | 437 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 438 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 439 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 440 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 441 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 442 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 443 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 444 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 445 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 446 | 447 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 448 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 449 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 450 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 451 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 452 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 453 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 454 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 455 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 456 | DAMAGES. 457 | 458 | END OF TERMS AND CONDITIONS 459 | 460 | How to Apply These Terms to Your New Libraries 461 | 462 | If you develop a new library, and you want it to be of the greatest 463 | possible use to the public, we recommend making it free software that 464 | everyone can redistribute and change. You can do so by permitting 465 | redistribution under these terms (or, alternatively, under the terms of the 466 | ordinary General Public License). 467 | 468 | To apply these terms, attach the following notices to the library. It is 469 | safest to attach them to the start of each source file to most effectively 470 | convey the exclusion of warranty; and each file should have at least the 471 | "copyright" line and a pointer to where the full notice is found. 472 | 473 | 474 | Copyright (C) 475 | 476 | This library is free software; you can redistribute it and/or 477 | modify it under the terms of the GNU Lesser General Public 478 | License as published by the Free Software Foundation; either 479 | version 2.1 of the License, or (at your option) any later version. 480 | 481 | This library is distributed in the hope that it will be useful, 482 | but WITHOUT ANY WARRANTY; without even the implied warranty of 483 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 484 | Lesser General Public License for more details. 485 | 486 | You should have received a copy of the GNU Lesser General Public 487 | License along with this library; if not, write to the Free Software 488 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 489 | 490 | Also add information on how to contact you by electronic and paper mail. 491 | 492 | You should also get your employer (if you work as a programmer) or your 493 | school, if any, to sign a "copyright disclaimer" for the library, if 494 | necessary. Here is a sample; alter the names: 495 | 496 | Yoyodyne, Inc., hereby disclaims all copyright interest in the 497 | library `Frob' (a library for tweaking knobs) written by James Random Hacker. 498 | 499 | , 1 April 1990 500 | Ty Coon, President of Vice 501 | 502 | That's all there is to it! 503 | -------------------------------------------------------------------------------- /gstvstaudioprocessor.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017 Sebastian Dröge 3 | * 4 | * This library is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU Lesser General Public 6 | * License as published by the Free Software Foundation; either 7 | * version 2.1 of the License, or (at your option) any later version. 8 | * 9 | * This library is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public 15 | * License along with this library; if not, write to the Free Software 16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | * 18 | */ 19 | 20 | #include 21 | #include 22 | /* For g_stat () */ 23 | #include 24 | 25 | #include "plugin.h" 26 | #include "gstvstaudioprocessor.h" 27 | 28 | #include 29 | #include 30 | 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | 40 | #if defined(G_OS_WIN32) 41 | #include 42 | #include 43 | #pragma comment(lib, "Shell32") 44 | #endif 45 | 46 | GST_DEBUG_CATEGORY_STATIC(gst_vst_audio_processor_debug); 47 | #define GST_CAT_DEFAULT gst_vst_audio_processor_debug 48 | 49 | using namespace Steinberg; 50 | 51 | static GstElementClass *parent_class = nullptr; 52 | static GQuark audio_processor_info_quark; 53 | 54 | static void gst_vst_audio_processor_class_init(GstVstAudioProcessorClass * klass); 55 | static void gst_vst_audio_processor_sub_class_init(GstVstAudioProcessorClass * klass); 56 | static void gst_vst_audio_processor_init(GstVstAudioProcessor * self, GstVstAudioProcessorClass * klass); 57 | 58 | static GstFlowReturn gst_vst_audio_processor_sink_chain(GstPad * pad, 59 | GstObject * parent, GstBuffer * buffer); 60 | static gboolean gst_vst_audio_processor_sink_event(GstPad * pad, 61 | GstObject * parent, GstEvent * event); 62 | static gboolean gst_vst_audio_processor_src_query(GstPad * pad, 63 | GstObject * parent, GstQuery * query); 64 | 65 | static void gst_vst_audio_processor_finalize(GObject * object); 66 | static void gst_vst_audio_processor_get_property(GObject * object, 67 | guint property_id, GValue * value, GParamSpec * pspec); 68 | static void gst_vst_audio_processor_set_property(GObject * object, 69 | guint property_id, const GValue * value, GParamSpec * pspec); 70 | 71 | static void gst_vst_audio_processor_sub_get_property(GObject * object, 72 | guint property_id, GValue * value, GParamSpec * pspec); 73 | static void gst_vst_audio_processor_sub_set_property(GObject * object, 74 | guint property_id, const GValue * value, GParamSpec * pspec); 75 | 76 | static GstStateChangeReturn gst_vst_audio_processor_change_state(GstElement * 77 | element, GstStateChange transition); 78 | 79 | // The different states the audio processor can be in 80 | typedef enum { 81 | STATE_NONE = 0, 82 | STATE_CREATED, 83 | STATE_INITIALIZED, 84 | STATE_SETUP, 85 | STATE_ACTIVE, 86 | STATE_PROCESSING, 87 | } State; 88 | 89 | enum { 90 | PROP_0 = 0, 91 | PROP_MAX_SAMPLES_PER_CHUNK, 92 | }; 93 | 94 | #define DEFAULT_MAX_SAMPLES_PER_CHUNK (1024) 95 | 96 | // Communication between edit controller and component happens over this. We 97 | // don't really use this as we don't want to use the GUI provided by the 98 | // controller. 99 | class GstVstAudioProcessorComponentHandler: public Vst::IComponentHandler { 100 | public: 101 | GstVstAudioProcessorComponentHandler(GstVstAudioProcessor * processor): processor(processor) { FUNKNOWN_CTOR } 102 | 103 | virtual ~GstVstAudioProcessorComponentHandler() { FUNKNOWN_DTOR } 104 | 105 | DECLARE_FUNKNOWN_METHODS 106 | 107 | tresult PLUGIN_API beginEdit(Vst::ParamID id) override { 108 | GST_FIXME_OBJECT(processor, "beginEdit not implemented"); 109 | return kNotImplemented; 110 | } 111 | tresult PLUGIN_API performEdit(Vst::ParamID id, Vst::ParamValue valueNormalized) override { 112 | GST_FIXME_OBJECT(processor, "performEdit not implemented"); 113 | return kNotImplemented; 114 | } 115 | tresult PLUGIN_API endEdit(Vst::ParamID id) override { 116 | GST_FIXME_OBJECT(processor, "endEdit not implemented"); 117 | return kNotImplemented; 118 | } 119 | tresult PLUGIN_API restartComponent(int32 flags) override { 120 | // TODO: Maybe want to do something with the other ones? 121 | GST_DEBUG_OBJECT(processor, "restartComponent(0x%08x)", flags); 122 | 123 | if (flags & Vst::kLatencyChanged) { 124 | gst_element_post_message(GST_ELEMENT_CAST(processor), 125 | gst_message_new_latency(GST_OBJECT_CAST(processor))); 126 | } 127 | 128 | return kResultOk; 129 | } 130 | 131 | GstVstAudioProcessor * processor; 132 | }; 133 | 134 | IMPLEMENT_REFCOUNT(GstVstAudioProcessorComponentHandler); 135 | DECLARE_CLASS_IID(GstVstAudioProcessorComponentHandler, 0x8f2a46d5, 0x148a4e40, 0xab996b56, 0xc2c615cf); 136 | 137 | tresult GstVstAudioProcessorComponentHandler::queryInterface(const char * iid, void ** obj) { 138 | QUERY_INTERFACE(iid, obj, FUnknown::iid, FUnknown); 139 | QUERY_INTERFACE(iid, obj, Vst::IComponentHandler::iid, Vst::IComponentHandler); 140 | *obj = nullptr; 141 | return kNoInterface; 142 | } 143 | 144 | struct _GstVstAudioProcessor { 145 | GstElement element; 146 | 147 | GstPad *srcpad, *sinkpad; 148 | 149 | // Properties 150 | gint max_samples_per_chunk; 151 | 152 | // Protected by object lock 153 | Vst::ParameterChanges *parameter_changes; 154 | gdouble *parameter_values; 155 | 156 | // State 157 | // Protected by stream lock 158 | GstSegment segment; 159 | GstAudioInfo info; 160 | GstClockTime latency; 161 | 162 | State state; 163 | std::shared_ptr module; 164 | IPtr component; 165 | IPtr edit_controller; 166 | IPtr audio_processor; 167 | IPtr component_handler; 168 | 169 | // Temporary buffer space used for deinterleaving 170 | gpointer in_data[2]; 171 | gpointer out_data[2]; 172 | guint data_len; 173 | }; 174 | 175 | struct _GstVstAudioProcessorClass { 176 | GstElementClass parent_class; 177 | 178 | const GstVstAudioProcessorInfo *processor_info; 179 | }; 180 | 181 | // Property definition 182 | typedef struct { 183 | Vst::ParamID param_id; 184 | const gchar *name; 185 | const gchar *nick; 186 | const gchar *description; 187 | GType type; // float, bool, int 188 | gint32 max_value; // for int 189 | gdouble default_value; 190 | gboolean read_only; 191 | 192 | GParamSpec *pspec; 193 | } GstVstAudioProcessorProperty; 194 | 195 | // Class information extracted from component 196 | struct _GstVstAudioProcessorInfo { 197 | const gchar *name; 198 | GstCaps *caps; 199 | const gchar *path; 200 | VST3::UID class_id; 201 | 202 | // properties[0] -> GObject property ID 1 203 | GstVstAudioProcessorProperty *properties; 204 | guint n_properties; 205 | }; 206 | 207 | GType 208 | gst_vst_audio_processor_get_type(void) 209 | { 210 | static volatile gsize type = 0; 211 | 212 | if (g_once_init_enter(&type)) { 213 | GType _type; 214 | static const GTypeInfo info = { 215 | sizeof (GstVstAudioProcessorClass), 216 | nullptr, 217 | nullptr, 218 | (GClassInitFunc) gst_vst_audio_processor_class_init, 219 | nullptr, 220 | nullptr, 221 | sizeof (GstVstAudioProcessor), 222 | 0, 223 | (GInstanceInitFunc) gst_vst_audio_processor_init, 224 | nullptr 225 | }; 226 | 227 | _type = g_type_register_static(GST_TYPE_ELEMENT, "GstVstAudioProcessor", 228 | &info, (GTypeFlags) 0); 229 | 230 | g_once_init_leave(&type, _type); 231 | } 232 | return type; 233 | } 234 | 235 | static void 236 | gst_vst_audio_processor_sub_class_init(GstVstAudioProcessorClass * klass) 237 | { 238 | auto gobject_class = G_OBJECT_CLASS(klass); 239 | auto element_class = GST_ELEMENT_CLASS(klass); 240 | auto audio_processor_klass = GST_VST_AUDIO_PROCESSOR_CLASS(klass); 241 | 242 | auto processor_info = (const GstVstAudioProcessorInfo *) 243 | g_type_get_qdata(G_TYPE_FROM_CLASS(klass), audio_processor_info_quark); 244 | // This happens for the base class and abstract subclasses 245 | if (!processor_info) 246 | return; 247 | 248 | audio_processor_klass->processor_info = processor_info; 249 | 250 | // Add pad templates 251 | auto templ = gst_pad_template_new("sink", GST_PAD_SINK, GST_PAD_ALWAYS, processor_info->caps); 252 | gst_element_class_add_pad_template(element_class, templ); 253 | 254 | templ = gst_pad_template_new("src", GST_PAD_SRC, GST_PAD_ALWAYS, processor_info->caps); 255 | gst_element_class_add_pad_template(element_class, templ); 256 | 257 | auto longname = g_strdup_printf("VST3 Audio processor - %s", processor_info->name); 258 | gst_element_class_set_metadata(element_class, 259 | processor_info->name, 260 | "Audio/Filter", 261 | longname, "Sebastian Dröge "); 262 | g_free(longname); 263 | 264 | // Register all our properties, if any 265 | if (processor_info->n_properties > 0) { 266 | gobject_class->set_property = gst_vst_audio_processor_sub_set_property; 267 | gobject_class->get_property = gst_vst_audio_processor_sub_get_property; 268 | 269 | for (auto i = 0U; i < processor_info->n_properties; i++) { 270 | auto property = &processor_info->properties[i]; 271 | GParamSpec *param_spec; 272 | 273 | if (property->type == G_TYPE_DOUBLE) { 274 | param_spec = g_param_spec_double(property->name, property->nick, property->description, 275 | -G_MAXDOUBLE, G_MAXDOUBLE, 276 | property->default_value, 277 | (GParamFlags) ((property->read_only ? G_PARAM_READABLE : G_PARAM_READWRITE) | GST_PARAM_CONTROLLABLE)); 278 | } else if (property->type == G_TYPE_BOOLEAN) { 279 | param_spec = g_param_spec_boolean(property->name, property->nick, property->description, 280 | property->default_value > 0.5, 281 | (GParamFlags) ((property->read_only ? G_PARAM_READABLE : G_PARAM_READWRITE) | GST_PARAM_CONTROLLABLE)); 282 | } else { 283 | param_spec = g_param_spec_int(property->name, property->nick, property->description, 284 | 0, property->max_value, 285 | property->default_value, 286 | (GParamFlags) ((property->read_only ? G_PARAM_READABLE : G_PARAM_READWRITE) | GST_PARAM_CONTROLLABLE)); 287 | } 288 | property->pspec = param_spec; 289 | 290 | g_object_class_install_property(gobject_class, i + 1, param_spec); 291 | } 292 | } 293 | } 294 | 295 | static void 296 | gst_vst_audio_processor_class_init(GstVstAudioProcessorClass * klass) 297 | { 298 | auto gobject_class = G_OBJECT_CLASS(klass); 299 | auto gstelement_class = GST_ELEMENT_CLASS(klass); 300 | 301 | parent_class = GST_ELEMENT_CLASS(g_type_class_peek_parent(klass)); 302 | 303 | gobject_class->set_property = gst_vst_audio_processor_set_property; 304 | gobject_class->get_property = gst_vst_audio_processor_get_property; 305 | gobject_class->finalize = gst_vst_audio_processor_finalize; 306 | 307 | g_object_class_install_property (gobject_class, PROP_MAX_SAMPLES_PER_CHUNK, 308 | g_param_spec_int ("max-samples-per-chunk", "Max Samples per Chunk", 309 | "Maximum number of samples to process per chunk", 1, 310 | G_MAXINT, DEFAULT_MAX_SAMPLES_PER_CHUNK, 311 | (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | 312 | GST_PARAM_MUTABLE_READY))); 313 | 314 | gstelement_class->change_state = gst_vst_audio_processor_change_state; 315 | } 316 | 317 | static void 318 | gst_vst_audio_processor_init(GstVstAudioProcessor * self, GstVstAudioProcessorClass * klass) 319 | { 320 | auto sink_templ = gst_element_class_get_pad_template(GST_ELEMENT_CLASS(klass), "sink"); 321 | self->sinkpad = gst_pad_new_from_template (sink_templ, "sink"); 322 | gst_pad_set_chain_function (self->sinkpad, 323 | GST_DEBUG_FUNCPTR (gst_vst_audio_processor_sink_chain)); 324 | gst_pad_set_event_function (self->sinkpad, 325 | GST_DEBUG_FUNCPTR (gst_vst_audio_processor_sink_event)); 326 | GST_PAD_SET_PROXY_CAPS (self->sinkpad); 327 | gst_element_add_pad (GST_ELEMENT (self), self->sinkpad); 328 | 329 | auto src_templ = gst_element_class_get_pad_template(GST_ELEMENT_CLASS(klass), "src"); 330 | self->srcpad = gst_pad_new_from_template (src_templ, "src"); 331 | gst_pad_set_query_function (self->srcpad, 332 | GST_DEBUG_FUNCPTR (gst_vst_audio_processor_src_query)); 333 | GST_PAD_SET_PROXY_CAPS (self->srcpad); 334 | gst_pad_use_fixed_caps (self->srcpad); 335 | gst_element_add_pad (GST_ELEMENT (self), self->srcpad); 336 | 337 | self->state = STATE_NONE; 338 | self->module = nullptr; 339 | self->component = nullptr; 340 | self->audio_processor = nullptr; 341 | self->edit_controller = nullptr; 342 | self->component_handler = nullptr; 343 | 344 | self->max_samples_per_chunk = DEFAULT_MAX_SAMPLES_PER_CHUNK; 345 | 346 | // Initialize all properties as stored here with their default values 347 | self->parameter_values = g_new0(gdouble, klass->processor_info->n_properties); 348 | for (auto i = 0U; i < klass->processor_info->n_properties; i++) 349 | self->parameter_values[i] = klass->processor_info->properties[i].default_value; 350 | } 351 | 352 | static void 353 | gst_vst_audio_processor_finalize(GObject * object) 354 | { 355 | auto self = GST_VST_AUDIO_PROCESSOR(object); 356 | 357 | if (self->parameter_changes) 358 | delete self->parameter_changes; 359 | g_free(self->parameter_values); 360 | 361 | G_OBJECT_CLASS(parent_class)->finalize(object); 362 | } 363 | 364 | static void 365 | gst_vst_audio_processor_get_property (GObject * object, 366 | guint property_id, GValue * value, GParamSpec * pspec) 367 | { 368 | auto self = GST_VST_AUDIO_PROCESSOR(object); 369 | 370 | switch (property_id) { 371 | case PROP_MAX_SAMPLES_PER_CHUNK: 372 | g_value_set_int (value, self->max_samples_per_chunk); 373 | break; 374 | default: 375 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); 376 | break; 377 | } 378 | } 379 | 380 | static void 381 | gst_vst_audio_processor_set_property (GObject * object, 382 | guint property_id, const GValue * value, GParamSpec * pspec) 383 | { 384 | auto self = GST_VST_AUDIO_PROCESSOR(object); 385 | 386 | switch (property_id) { 387 | case PROP_MAX_SAMPLES_PER_CHUNK: 388 | self->max_samples_per_chunk = g_value_get_int (value); 389 | break; 390 | default: 391 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); 392 | break; 393 | } 394 | } 395 | 396 | static void 397 | gst_vst_audio_processor_sub_get_property (GObject * object, 398 | guint property_id, GValue * value, GParamSpec * pspec) 399 | { 400 | auto self = GST_VST_AUDIO_PROCESSOR(object); 401 | auto klass = GST_VST_AUDIO_PROCESSOR_GET_CLASS(self); 402 | 403 | if (property_id > klass->processor_info->n_properties + 1 || property_id == 0) { 404 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); 405 | return; 406 | } 407 | 408 | GST_OBJECT_LOCK(self); 409 | auto property = &klass->processor_info->properties[property_id - 1]; 410 | if (property->type == G_TYPE_DOUBLE) 411 | g_value_set_double(value, self->parameter_values[property_id - 1]); 412 | else if (property->type == G_TYPE_BOOLEAN) 413 | g_value_set_boolean(value, self->parameter_values[property_id - 1] > 0.5); 414 | else 415 | g_value_set_int(value, self->parameter_values[property_id - 1]); 416 | GST_OBJECT_UNLOCK(self); 417 | } 418 | 419 | static void 420 | gst_vst_audio_processor_sub_set_property (GObject * object, 421 | guint property_id, const GValue * value, GParamSpec * pspec) 422 | { 423 | auto self = GST_VST_AUDIO_PROCESSOR(object); 424 | auto klass = GST_VST_AUDIO_PROCESSOR_GET_CLASS(self); 425 | 426 | if (property_id > klass->processor_info->n_properties + 1 || property_id == 0) { 427 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); 428 | return; 429 | } 430 | 431 | GST_OBJECT_LOCK(self); 432 | auto property = &klass->processor_info->properties[property_id - 1]; 433 | 434 | // Store value in our cache 435 | if (property->type == G_TYPE_DOUBLE) { 436 | self->parameter_values[property_id - 1] = g_value_get_double(value); 437 | } else if (property->type == G_TYPE_BOOLEAN) { 438 | self->parameter_values[property_id - 1] = g_value_get_boolean(value); 439 | } else { 440 | self->parameter_values[property_id - 1] = g_value_get_int(value); 441 | } 442 | 443 | // If we have an edit controller, convert our plain values to normalized 444 | // values and store the parameter changes and let the edit controller know 445 | // about it 446 | // 447 | // We always use plain values, but controller and component use normalized 448 | // values between 0.0 and 1.0 449 | if (self->edit_controller) { 450 | if (!self->parameter_changes) 451 | self->parameter_changes = new Vst::ParameterChanges(); 452 | 453 | Steinberg::int32 idx = 0; 454 | auto queue = self->parameter_changes->addParameterData(property->param_id, idx); 455 | auto value = self->edit_controller->plainParamToNormalized(property->param_id, 456 | self->parameter_values[property_id - 1]); 457 | 458 | queue->addPoint(0, value, idx); 459 | self->edit_controller->setParamNormalized(property->param_id, value); 460 | } 461 | GST_OBJECT_UNLOCK(self); 462 | } 463 | 464 | static gboolean 465 | gst_vst_audio_processor_open(GstVstAudioProcessor *self) 466 | { 467 | auto klass = GST_VST_AUDIO_PROCESSOR_GET_CLASS(self); 468 | std::string err; 469 | std::string path(klass->processor_info->path); 470 | 471 | self->state = STATE_NONE; 472 | 473 | auto mod = VST3::Hosting::Module::create(path, err); 474 | if (!mod) { 475 | GST_ERROR_OBJECT(self, "Failed to load module '%s': %s", path.c_str(), err.c_str()); 476 | return FALSE; 477 | } 478 | 479 | auto factory = mod->getFactory(); 480 | 481 | auto component = factory.createInstance(klass->processor_info->class_id); 482 | if (!component) { 483 | GST_ERROR_OBJECT(self, "Failed to create instance for '%s'", klass->processor_info->name); 484 | return FALSE; 485 | } 486 | 487 | auto res = component->initialize(gStandardPluginContext); 488 | if (res != kResultOk) { 489 | GST_ERROR_OBJECT(self, "Component can't be initialized: 0x%08x", res); 490 | return FALSE; 491 | } 492 | 493 | // Check if this supports the IAudioProcessor interface 494 | IPtr audio_processor; 495 | Vst::IAudioProcessor *audio_processor_ptr = nullptr; 496 | if (component->queryInterface(Vst::IAudioProcessor::iid, (void **) &audio_processor_ptr) != kResultOk 497 | || !audio_processor_ptr) { 498 | GST_ERROR_OBJECT(self, "Component does not implement IAudioProcessor interface"); 499 | return FALSE; 500 | } 501 | audio_processor = shared(audio_processor_ptr); 502 | 503 | // Get the controller 504 | IPtr edit_controller; 505 | Vst::IEditController *edit_controller_ptr = nullptr; 506 | if (component->queryInterface(Vst::IEditController::iid, (void**) &edit_controller_ptr) != kResultOk 507 | || !edit_controller_ptr) { 508 | FUID controller_cid; 509 | 510 | // ask for the associated controller class ID 511 | if (component->getControllerClassId(controller_cid) == kResultOk && controller_cid.isValid ()) { 512 | // create its controller part created from the factory 513 | edit_controller = factory.createInstance(controller_cid.toTUID()); 514 | if (edit_controller) { 515 | // initialize the component with our context 516 | res = edit_controller->initialize(gStandardPluginContext); 517 | if (res != kResultOk) { 518 | GST_ERROR_OBJECT(self, "Can't initialize edit controller: 0x%08x", res); 519 | return FALSE; 520 | } 521 | } 522 | } 523 | } else { 524 | edit_controller = shared(edit_controller_ptr); 525 | } 526 | 527 | if (!edit_controller) { 528 | GST_ERROR_OBJECT(self, "No edit controller found"); 529 | return FALSE; 530 | } 531 | 532 | // activate busses, just in case 533 | res = component->activateBus(Vst::MediaTypes::kAudio, Vst::BusDirections::kInput, 0, TRUE); 534 | if (res != kResultOk) { 535 | GST_ERROR_OBJECT(self, "Failed to activate input bus: 0x%08x", res); 536 | return FALSE; 537 | } 538 | res = component->activateBus(Vst::MediaTypes::kAudio, Vst::BusDirections::kOutput, 0, TRUE); 539 | if (res != kResultOk) { 540 | GST_ERROR_OBJECT(self, "Failed to activate output bus: 0x%08x", res); 541 | return FALSE; 542 | } 543 | 544 | self->component_handler = owned(new GstVstAudioProcessorComponentHandler(self)); 545 | 546 | // the host set its handler to the controller 547 | edit_controller->setComponentHandler(self->component_handler); 548 | 549 | // connect the 2 components 550 | Vst::IConnectionPoint* iConnectionPointComponent = nullptr; 551 | Vst::IConnectionPoint* iConnectionPointController = nullptr; 552 | component->queryInterface(Vst::IConnectionPoint::iid, (void**)&iConnectionPointComponent); 553 | edit_controller->queryInterface(Vst::IConnectionPoint::iid, (void**)&iConnectionPointController); 554 | if (iConnectionPointComponent && iConnectionPointController) { 555 | iConnectionPointComponent->connect(iConnectionPointController); 556 | iConnectionPointController->connect(iConnectionPointComponent); 557 | } 558 | 559 | // synchronize controller to component by using setComponentState 560 | MemoryStream stream; 561 | if (component->getState(&stream) == kResultOk) { 562 | stream.truncate(); 563 | edit_controller->setComponentState(&stream); 564 | } 565 | 566 | // synchronize our cached property values with the component and controller 567 | self->parameter_changes = new Vst::ParameterChanges(); 568 | for (auto i = 0U; i < klass->processor_info->n_properties; i++) { 569 | auto property = &klass->processor_info->properties[i]; 570 | 571 | if (property->read_only) 572 | continue; 573 | 574 | Steinberg::int32 idx = 0; 575 | auto queue = self->parameter_changes->addParameterData(property->param_id, idx); 576 | auto value = edit_controller->plainParamToNormalized(property->param_id, 577 | self->parameter_values[i]); 578 | 579 | queue->addPoint(0, value, idx); 580 | edit_controller->setParamNormalized(property->param_id, value); 581 | } 582 | 583 | self->state = STATE_INITIALIZED; 584 | self->module = mod; 585 | self->component = component; 586 | self->audio_processor = audio_processor; 587 | self->edit_controller = edit_controller; 588 | 589 | return TRUE; 590 | } 591 | 592 | static GstStateChangeReturn 593 | gst_vst_audio_processor_change_state(GstElement * element, 594 | GstStateChange transition) 595 | { 596 | auto self = GST_VST_AUDIO_PROCESSOR(element); 597 | auto state_ret = GST_STATE_CHANGE_SUCCESS; 598 | 599 | switch (transition) { 600 | case GST_STATE_CHANGE_NULL_TO_READY: 601 | gst_audio_info_init(&self->info); 602 | gst_segment_init(&self->segment, GST_FORMAT_TIME); 603 | if (!gst_vst_audio_processor_open(self)) 604 | state_ret = GST_STATE_CHANGE_FAILURE; 605 | break; 606 | default: 607 | break; 608 | } 609 | 610 | state_ret = GST_ELEMENT_CLASS(parent_class)->change_state(element, transition); 611 | if (state_ret == GST_STATE_CHANGE_FAILURE) 612 | return state_ret; 613 | 614 | switch (transition) { 615 | case GST_STATE_CHANGE_PAUSED_TO_READY: 616 | if (self->state >= STATE_PROCESSING) 617 | self->audio_processor->setProcessing(false); 618 | if (self->state >= STATE_ACTIVE) 619 | self->component->setActive(false); 620 | self->state = STATE_SETUP; 621 | break; 622 | case GST_STATE_CHANGE_READY_TO_NULL: 623 | if (self->state >= STATE_SETUP) { 624 | self->component->terminate(); 625 | self->edit_controller->terminate(); 626 | } 627 | self->state = STATE_NONE; 628 | self->audio_processor = nullptr; 629 | self->component = nullptr; 630 | self->edit_controller = nullptr; 631 | self->module = nullptr; 632 | self->component_handler = nullptr; 633 | 634 | g_free(self->in_data[0]); 635 | self->in_data[0] = nullptr; 636 | g_free(self->in_data[1]); 637 | self->in_data[1] = nullptr; 638 | g_free(self->out_data[0]); 639 | self->out_data[0] = nullptr; 640 | g_free(self->out_data[1]); 641 | self->out_data[1] = nullptr; 642 | self->data_len = 0; 643 | break; 644 | default: 645 | break; 646 | } 647 | 648 | return state_ret; 649 | } 650 | 651 | static void 652 | deinterleave_data(GstVstAudioProcessor *self, gconstpointer in_data, guint len) 653 | { 654 | if (self->info.finfo->format == GST_AUDIO_FORMAT_F32) { 655 | if (self->info.channels == 1) { 656 | memcpy (self->in_data[0], in_data, len * self->info.bpf); 657 | } else { 658 | const float *f_in_data = (const float *) in_data; 659 | 660 | for (auto i = 0; i < 2; i++) { 661 | float *f_out_data = (float *) self->in_data[i]; 662 | 663 | for (auto j = 0U; j < len; j++) { 664 | f_out_data[j] = f_in_data[i + j * 2]; 665 | } 666 | } 667 | } 668 | } else { 669 | if (self->info.channels == 1) { 670 | memcpy (self->in_data[0], in_data, len * self->info.bpf); 671 | } else { 672 | const double *f_in_data = (const double *) in_data; 673 | 674 | for (auto i = 0; i < 2; i++) { 675 | double *f_out_data = (double *) self->in_data[i]; 676 | 677 | for (auto j = 0U; j < len; j++) { 678 | f_out_data[j] = f_in_data[i + j * 2]; 679 | } 680 | } 681 | } 682 | } 683 | } 684 | 685 | static void 686 | interleave_data(GstVstAudioProcessor *self, gpointer out_data, guint len) 687 | { 688 | if (self->info.finfo->format == GST_AUDIO_FORMAT_F32) { 689 | if (self->info.channels == 1) { 690 | memcpy (out_data, self->out_data[0], len * self->info.bpf); 691 | } else { 692 | float *f_out_data = (float *) out_data; 693 | 694 | for (auto i = 0; i < 2; i++) { 695 | const float *f_in_data = (const float *) self->out_data[i]; 696 | 697 | for (auto j = 0U; j < len; j++) { 698 | f_out_data[i + j * 2] = f_in_data[j]; 699 | } 700 | } 701 | } 702 | } else { 703 | if (self->info.channels == 1) { 704 | memcpy (out_data, self->out_data[0], len * self->info.bpf); 705 | } else { 706 | double *f_out_data = (double *) out_data; 707 | 708 | for (auto i = 0; i < 2; i++) { 709 | const double *f_in_data = (const double *) self->out_data[i]; 710 | 711 | for (auto j = 0U; j < len; j++) { 712 | f_out_data[i + j * 2] = f_in_data[j]; 713 | } 714 | } 715 | } 716 | } 717 | } 718 | 719 | static GstFlowReturn 720 | gst_vst_audio_processor_sink_chain(GstPad * pad, GstObject * parent, 721 | GstBuffer * in_buffer) 722 | { 723 | auto self = GST_VST_AUDIO_PROCESSOR(parent); 724 | auto klass = GST_VST_AUDIO_PROCESSOR_GET_CLASS(self); 725 | auto ret = GST_FLOW_OK; 726 | 727 | if (self->state < STATE_SETUP) { 728 | gst_buffer_unref(in_buffer); 729 | GST_ERROR_OBJECT(self, "Not negotiated yet"); 730 | return GST_FLOW_NOT_NEGOTIATED; 731 | } 732 | 733 | if (!GST_BUFFER_PTS_IS_VALID (in_buffer)) { 734 | GST_ERROR_OBJECT(self, "Need buffers with valid timestamps"); 735 | gst_buffer_unref(in_buffer); 736 | return GST_FLOW_ERROR; 737 | } 738 | 739 | // FIXME: Can we drain somehow? We should on disconts 740 | if (GST_BUFFER_IS_DISCONT (in_buffer)) { 741 | GST_DEBUG_OBJECT(self, "Discontinuity, restarting component"); 742 | if (self->state >= STATE_PROCESSING) 743 | self->audio_processor->setProcessing(false); 744 | if (self->state >= STATE_ACTIVE) 745 | self->component->setActive(false); 746 | } 747 | 748 | if (self->state < STATE_ACTIVE) { 749 | GST_DEBUG_OBJECT(self, "Activating component"); 750 | auto res = self->component->setActive(true); 751 | if (res != kResultOk) { 752 | GST_ERROR_OBJECT(self, "Failed to set active: %08x", res); 753 | gst_buffer_unref(in_buffer); 754 | return GST_FLOW_ERROR; 755 | } 756 | 757 | self->state = STATE_ACTIVE; 758 | } 759 | 760 | if (self->state < STATE_PROCESSING) { 761 | GST_DEBUG_OBJECT(self, "Set component to processing"); 762 | auto res = self->audio_processor->setProcessing(true); 763 | if (res != kResultOk && res != kNotImplemented) { 764 | GST_ERROR_OBJECT(self, "Failed to set processing: %08x", res); 765 | gst_buffer_unref(in_buffer); 766 | return GST_FLOW_ERROR; 767 | } 768 | 769 | self->state = STATE_PROCESSING; 770 | } 771 | 772 | // We process the input buffer in chunks of at most the configured 773 | // max-samples-per-chunk, and while doing so keep track of our current 774 | // timestamp, stream time and sample position 775 | GstMapInfo in_map; 776 | gst_buffer_map(in_buffer, &in_map, GST_MAP_READ); 777 | 778 | auto in_data = in_map.data; 779 | auto num_samples = in_map.size / self->info.bpf; 780 | 781 | auto stream_time = gst_segment_to_stream_time(&self->segment, GST_FORMAT_TIME, GST_BUFFER_PTS(in_buffer)); 782 | auto sample_position = (gint64) gst_util_uint64_scale(GST_BUFFER_PTS(in_buffer), self->info.rate, GST_SECOND); 783 | auto sample_start_position = sample_position; 784 | 785 | do { 786 | gst_object_sync_values(GST_OBJECT_CAST(self), stream_time + 787 | gst_util_uint64_scale(sample_position - sample_start_position, GST_SECOND, self->info.rate)); 788 | 789 | auto chunk_size = MIN(self->data_len, num_samples); 790 | 791 | // Fill input buffers and metadata 792 | deinterleave_data(self, (gconstpointer) in_data, chunk_size); 793 | Vst::AudioBusBuffers input; 794 | input.numChannels = self->info.channels; 795 | input.silenceFlags = GST_BUFFER_FLAG_IS_SET(in_buffer, GST_BUFFER_FLAG_GAP) ? G_MAXUINT64 : 0; 796 | if (self->info.finfo->format == GST_AUDIO_FORMAT_F32) 797 | input.channelBuffers32 = (Vst::Sample32 **) self->in_data; 798 | else 799 | input.channelBuffers64 = (Vst::Sample64 **) self->in_data; 800 | 801 | // Fill output buffer metadata 802 | Vst::AudioBusBuffers output; 803 | output.numChannels = self->info.channels; 804 | output.silenceFlags = 0; 805 | if (self->info.finfo->format == GST_AUDIO_FORMAT_F32) 806 | output.channelBuffers32 = (Vst::Sample32 **) self->out_data; 807 | else 808 | output.channelBuffers64 = (Vst::Sample64 **) self->out_data; 809 | 810 | // Set up process context with information about the system state 811 | Vst::ProcessContext process_context; 812 | process_context.state = Vst::ProcessContext::kPlaying | 813 | Vst::ProcessContext::kRecording | 814 | Vst::ProcessContext::kSystemTimeValid; 815 | process_context.sampleRate = self->info.rate; 816 | process_context.projectTimeSamples = sample_position; 817 | // FIXME: Should we pretend real-time processing here? 818 | process_context.systemTime = gst_util_get_timestamp(); 819 | 820 | // Set up process data 821 | Vst::ProcessData data; 822 | data.processMode = Vst::kPrefetch; 823 | data.symbolicSampleSize = self->info.finfo->format == GST_AUDIO_FORMAT_F32 ? Vst::kSample32 : Vst::kSample64; 824 | data.numSamples = chunk_size; 825 | data.numInputs = 1; 826 | data.numOutputs = 1; 827 | data.inputs = &input; 828 | data.outputs = &output; 829 | data.processContext = &process_context; 830 | 831 | // Check if we have any pending input parameter changes 832 | GST_OBJECT_LOCK(self); 833 | auto parameter_changes = self->parameter_changes; 834 | self->parameter_changes = nullptr; 835 | GST_OBJECT_UNLOCK(self); 836 | data.inputParameterChanges = parameter_changes; 837 | 838 | // Allocate space for any output parameter changes 839 | Vst::ParameterChanges out_parameter_changes; 840 | data.outputParameterChanges = &out_parameter_changes; 841 | 842 | // And finally do the actual processing of this chunk 843 | auto res = self->audio_processor->process(data); 844 | 845 | // We have to delete the pointer here, the processor does not do that 846 | if (parameter_changes) { 847 | delete parameter_changes; 848 | data.inputParameterChanges = nullptr; 849 | } 850 | 851 | // Update out parameter changes 852 | auto out_changes_count = out_parameter_changes.getParameterCount(); 853 | for (auto i = 0; i < out_changes_count; i++) { 854 | auto queue = out_parameter_changes.getParameterData(i); 855 | auto point_count = queue->getPointCount(); 856 | if (point_count > 0) { 857 | Vst::ParamValue value; 858 | Steinberg::int32 sample_offset = 0; 859 | 860 | if (queue->getPoint(point_count - 1, sample_offset, value) == kResultOk) { 861 | GstVstAudioProcessorProperty *prop = nullptr; 862 | auto param_id = queue->getParameterId(); 863 | auto plain_value = self->edit_controller->normalizedParamToPlain(param_id, value); 864 | 865 | guint k; 866 | for (k = 0; k < klass->processor_info->n_properties; k++) { 867 | if (klass->processor_info->properties[k].param_id == param_id) { 868 | prop = &klass->processor_info->properties[k]; 869 | break; 870 | } 871 | } 872 | 873 | // Cache the new value and notify anybody interested 874 | if (prop) { 875 | self->parameter_values[k] = plain_value; 876 | g_object_notify_by_pspec(G_OBJECT(self), prop->pspec); 877 | 878 | } 879 | 880 | // And let the edit controller know about this change too 881 | self->edit_controller->setParamNormalized(param_id, value); 882 | } 883 | } 884 | } 885 | 886 | if (res != kResultOk) { 887 | ret = GST_FLOW_ERROR; 888 | break; 889 | } 890 | 891 | // If there's output, allocate a new buffer and fill it 892 | // FIXME: We assume that input length == output length currently 893 | // for the timestamp calculation. This is not necessarily true: 894 | // there could be latency involved. But none of the plugins this was 895 | // tested with makes use of that 896 | if (data.numSamples != (gint) chunk_size) { 897 | GST_FIXME_OBJECT(self, "Output number of samples different than input: %d != %lu", data.numSamples, chunk_size); 898 | } 899 | 900 | if (data.numSamples > 0) { 901 | auto out_buffer = gst_buffer_new_and_alloc(data.numSamples * self->info.bpf); 902 | GstMapInfo out_map; 903 | 904 | gst_buffer_map(out_buffer, &out_map, GST_MAP_WRITE); 905 | auto out_data = out_map.data; 906 | interleave_data(self, (gpointer) out_data, data.numSamples); 907 | gst_buffer_unmap(out_buffer, &out_map); 908 | 909 | GST_BUFFER_PTS(out_buffer) = GST_BUFFER_PTS(in_buffer) + 910 | gst_util_uint64_scale(sample_position - sample_start_position, GST_SECOND, self->info.rate); 911 | GST_BUFFER_DURATION(out_buffer) = gst_util_uint64_scale(chunk_size, GST_SECOND, self->info.rate); 912 | 913 | ret = gst_pad_push(self->srcpad, out_buffer); 914 | } 915 | 916 | num_samples -= chunk_size; 917 | in_data += chunk_size * self->info.bpf; 918 | sample_position += chunk_size; 919 | } while (ret == GST_FLOW_OK && num_samples > 0); 920 | 921 | gst_buffer_unmap(in_buffer, &in_map); 922 | gst_buffer_unref(in_buffer); 923 | 924 | return ret; 925 | } 926 | 927 | static gboolean 928 | gst_vst_audio_processor_sink_event(GstPad * pad, GstObject * parent, 929 | GstEvent * event) 930 | { 931 | auto self = GST_VST_AUDIO_PROCESSOR(parent); 932 | gboolean ret = FALSE; 933 | 934 | switch (GST_EVENT_TYPE(event)) { 935 | case GST_EVENT_CAPS:{ 936 | GstCaps *caps; 937 | GstAudioInfo info; 938 | gboolean changed; 939 | 940 | gst_event_parse_caps(event, &caps); 941 | 942 | ret = gst_audio_info_from_caps(&info, caps); 943 | changed = ret && !gst_audio_info_is_equal(&info, &self->info); 944 | if (ret && changed) { 945 | GST_DEBUG_OBJECT(self, "Got caps %" GST_PTR_FORMAT, caps); 946 | 947 | self->info = info; 948 | 949 | // Need to shut down the component to be able to configure any new 950 | // sample rate, channel configuration or sample format 951 | // FIXME: Can we drain somehow? 952 | if (self->state >= STATE_PROCESSING) 953 | self->audio_processor->setProcessing(false); 954 | if (self->state >= STATE_ACTIVE) 955 | self->component->setActive(false); 956 | self->state = STATE_SETUP; 957 | 958 | Vst::SpeakerArrangement arrangement[1] = { info.channels == 1 ? Vst::SpeakerArr::kMono : Vst::SpeakerArr::kStereo }; 959 | auto res = self->audio_processor->setBusArrangements(arrangement, 1, arrangement, 1); 960 | if (res != kResultOk) { 961 | GST_ERROR_OBJECT(self, "Failed to set bus arrangments: 0x%08x", res); 962 | self->state = STATE_INITIALIZED; 963 | ret = FALSE; 964 | gst_event_unref(event); 965 | break; 966 | } 967 | 968 | Vst::ProcessSetup setup = { 969 | Vst::kPrefetch, 970 | info.finfo->format == GST_AUDIO_FORMAT_F32 ? Vst::kSample32 : Vst::kSample64, 971 | self->max_samples_per_chunk, 972 | (double) info.rate 973 | }; 974 | 975 | res = self->audio_processor->setupProcessing(setup); 976 | if (res != kResultOk) { 977 | GST_ERROR_OBJECT(self, "Failed to setup processing: %08x", res); 978 | self->state = STATE_INITIALIZED; 979 | ret = FALSE; 980 | gst_event_unref(event); 981 | break; 982 | } 983 | 984 | // Reallocate our buffers for both channel 985 | auto bps = info.bpf / info.channels; 986 | g_free(self->in_data[0]); 987 | self->in_data[0] = g_malloc0(bps * self->max_samples_per_chunk); 988 | g_free(self->in_data[1]); 989 | self->in_data[1] = info.channels == 2 ? g_malloc0(bps * self->max_samples_per_chunk) : nullptr; 990 | g_free(self->out_data[0]); 991 | self->out_data[0] = g_malloc0(bps * self->max_samples_per_chunk); 992 | g_free(self->out_data[1]); 993 | self->out_data[1] = info.channels == 2 ? g_malloc0(bps * self->max_samples_per_chunk) : nullptr; 994 | self->data_len = self->max_samples_per_chunk; 995 | 996 | // Update latency 997 | auto latency_samples = self->audio_processor->getLatencySamples(); 998 | auto latency = gst_util_uint64_scale_int(latency_samples, GST_SECOND, info.rate); 999 | if (latency != self->latency) { 1000 | self->latency = latency; 1001 | GST_DEBUG_OBJECT(self, "Latency changed to %" GST_TIME_FORMAT, GST_TIME_ARGS(latency)); 1002 | gst_element_post_message(GST_ELEMENT_CAST(self), gst_message_new_latency(GST_OBJECT_CAST(self))); 1003 | } 1004 | 1005 | GST_DEBUG_OBJECT(self, "Finished setup for new caps"); 1006 | 1007 | self->state = STATE_SETUP; 1008 | } else if (!ret) { 1009 | GST_ERROR_OBJECT(self, "Invalid caps"); 1010 | ret = FALSE; 1011 | } else { 1012 | // Nothing changed 1013 | ret = TRUE; 1014 | } 1015 | 1016 | if (ret) 1017 | ret = gst_pad_event_default(pad, parent, event); 1018 | else 1019 | gst_event_unref (event); 1020 | 1021 | break; 1022 | } 1023 | case GST_EVENT_FLUSH_STOP: 1024 | gst_segment_init(&self->segment, GST_FORMAT_TIME); 1025 | // Shut down component, it will be started again on next buffer 1026 | // FIXME: Is there a better way of flushing? 1027 | if (self->state >= STATE_PROCESSING) 1028 | self->audio_processor->setProcessing(false); 1029 | if (self->state >= STATE_ACTIVE) 1030 | self->component->setActive(false); 1031 | self->state = STATE_SETUP; 1032 | ret = gst_pad_event_default(pad, parent, event); 1033 | break; 1034 | case GST_EVENT_SEGMENT: 1035 | gst_event_copy_segment(event, &self->segment); 1036 | if (self->segment.format != GST_FORMAT_TIME) { 1037 | gst_event_unref(event); 1038 | ret = FALSE; 1039 | } else { 1040 | ret = gst_pad_event_default(pad, parent, event); 1041 | } 1042 | break; 1043 | case GST_EVENT_EOS: 1044 | // FIXME: Can we drain somehow? 1045 | ret = gst_pad_event_default(pad, parent, event); 1046 | break; 1047 | default: 1048 | ret = gst_pad_event_default(pad, parent, event); 1049 | break; 1050 | } 1051 | 1052 | return ret; 1053 | } 1054 | 1055 | static gboolean 1056 | gst_vst_audio_processor_src_query(GstPad * pad, 1057 | GstObject * parent, GstQuery * query) 1058 | { 1059 | auto self = GST_VST_AUDIO_PROCESSOR(parent); 1060 | gboolean ret = FALSE; 1061 | 1062 | switch (GST_QUERY_TYPE(query)) { 1063 | case GST_QUERY_LATENCY:{ 1064 | if ((ret = gst_pad_peer_query(self->sinkpad, query))) { 1065 | GstClockTime latency; 1066 | GstClockTime min, max; 1067 | gboolean live; 1068 | 1069 | gst_query_parse_latency(query, &live, &min, &max); 1070 | 1071 | GST_DEBUG_OBJECT(self, "Peer latency: min %" 1072 | GST_TIME_FORMAT " max %" GST_TIME_FORMAT, 1073 | GST_TIME_ARGS(min), GST_TIME_ARGS(max)); 1074 | 1075 | latency = self->latency; 1076 | 1077 | GST_DEBUG_OBJECT(self, "Our latency: min %" GST_TIME_FORMAT 1078 | ", max %" GST_TIME_FORMAT, 1079 | GST_TIME_ARGS(latency), GST_TIME_ARGS(latency)); 1080 | 1081 | min += latency; 1082 | if (max != GST_CLOCK_TIME_NONE) 1083 | max += latency; 1084 | 1085 | GST_DEBUG_OBJECT(self, "Calculated total latency : min %" 1086 | GST_TIME_FORMAT " max %" GST_TIME_FORMAT, 1087 | GST_TIME_ARGS(min), GST_TIME_ARGS(max)); 1088 | 1089 | gst_query_set_latency(query, live, min, max); 1090 | } 1091 | 1092 | break; 1093 | } 1094 | default: 1095 | ret = gst_pad_query_default(pad, parent, query); 1096 | break; 1097 | } 1098 | 1099 | return ret; 1100 | } 1101 | 1102 | static std::string 1103 | create_type_name(const gchar * parent_name, const gchar * class_name) 1104 | { 1105 | std::string res; 1106 | auto parent_name_len = strlen(parent_name); 1107 | auto class_name_len = strlen(class_name); 1108 | auto upper = true; 1109 | 1110 | auto typified_name = g_new0(gchar, parent_name_len + 1 + class_name_len + 1); 1111 | memcpy(typified_name, parent_name, parent_name_len); 1112 | typified_name[parent_name_len] = '-'; 1113 | 1114 | for (auto i = 0U, k = 0U; i < class_name_len; i++) { 1115 | if (g_ascii_isalnum(class_name[i])) { 1116 | if (upper) 1117 | typified_name[parent_name_len + 1 + k++] = 1118 | g_ascii_toupper(class_name[i]); 1119 | else 1120 | typified_name[parent_name_len + 1 + k++] = 1121 | g_ascii_tolower(class_name[i]); 1122 | 1123 | upper = false; 1124 | } else { 1125 | /* Skip all non-alnum chars and start a new upper case word */ 1126 | upper = true; 1127 | } 1128 | } 1129 | 1130 | res = std::string(typified_name); 1131 | g_free(typified_name); 1132 | return res; 1133 | } 1134 | 1135 | static std::string 1136 | create_element_name(const gchar * prefix, const gchar * class_name) 1137 | { 1138 | std::string res; 1139 | auto prefix_len = strlen(prefix); 1140 | auto class_name_len = strlen(class_name); 1141 | 1142 | auto element_name = g_new0(gchar, prefix_len + class_name_len + 1); 1143 | memcpy(element_name, prefix, prefix_len); 1144 | 1145 | for (auto i = 0U, k = 0U; i < class_name_len; i++) { 1146 | if (g_ascii_isalnum(class_name[i])) { 1147 | element_name[prefix_len + k++] = g_ascii_tolower(class_name[i]); 1148 | } 1149 | /* Skip all non-alnum chars */ 1150 | } 1151 | 1152 | res = std::string(element_name); 1153 | g_free(element_name); 1154 | return res; 1155 | } 1156 | 1157 | #if defined(__linux__) && !defined(__BIONIC__) 1158 | static void 1159 | register_system_dependencies(GstPlugin *plugin) 1160 | { 1161 | gst_plugin_add_dependency_simple (plugin, NULL, "/usr/lib/vst3:/usr/local/lib/vst3", ".vst3", 1162 | (GstPluginDependencyFlags) (GST_PLUGIN_DEPENDENCY_FLAG_RECURSE | 1163 | GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_SUFFIX)); 1164 | gst_plugin_add_dependency_simple (plugin, "HOME/.vst3", NULL, ".vst3", 1165 | (GstPluginDependencyFlags) (GST_PLUGIN_DEPENDENCY_FLAG_RECURSE | 1166 | GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_SUFFIX)); 1167 | } 1168 | #elif defined(G_OS_WIN32) 1169 | static void 1170 | register_system_dependencies(GstPlugin *plugin) 1171 | { 1172 | PWSTR wideStr {}; 1173 | char commonpath[MAX_PATH]; 1174 | gchar *path; 1175 | if (FAILED (SHGetKnownFolderPath (FOLDERID_ProgramFilesCommon, 0, NULL, &wideStr))) 1176 | return; 1177 | 1178 | if (!WideCharToMultiByte(CP_ACP, 0, wideStr, -1, commonpath, MAX_PATH, NULL, NULL)) 1179 | return; 1180 | 1181 | path = g_build_filename (commonpath, "VST3", NULL); 1182 | gst_plugin_add_dependency_simple (plugin, NULL, path, ".vst3", 1183 | (GstPluginDependencyFlags) (GST_PLUGIN_DEPENDENCY_FLAG_RECURSE | 1184 | GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_SUFFIX)); 1185 | g_free (path); 1186 | } 1187 | #else 1188 | static void 1189 | register_system_dependencies(GstPlugin *plugin) 1190 | { 1191 | GST_FIXME_OBJECT (plugin, "Implement plugin dependencies support for this platform"); 1192 | } 1193 | #endif 1194 | 1195 | static void 1196 | list_paths_with_vst3_extension (VST3::Hosting::Module::PathList &paths, 1197 | const gchar *path, gboolean recurse) 1198 | { 1199 | GDir *dir; 1200 | const gchar *dirent; 1201 | gchar *filename; 1202 | 1203 | dir = g_dir_open (path, 0, NULL); 1204 | if (!dir) 1205 | return; 1206 | 1207 | while ((dirent = g_dir_read_name (dir))) { 1208 | GStatBuf file_status; 1209 | 1210 | filename = g_build_filename (path, dirent, NULL); 1211 | if (g_stat (filename, &file_status) < 0) { 1212 | g_free (filename); 1213 | continue; 1214 | } 1215 | 1216 | if (g_str_has_suffix (dirent, ".vst3")) 1217 | paths.push_back (filename); 1218 | 1219 | if ((file_status.st_mode & S_IFDIR) && recurse) 1220 | list_paths_with_vst3_extension (paths, filename, recurse); 1221 | 1222 | g_free (filename); 1223 | } 1224 | 1225 | g_dir_close (dir); 1226 | } 1227 | 1228 | static gboolean 1229 | compare_versions (const gchar *version_str, const gchar *version_req_str) 1230 | { 1231 | gchar op[3]; 1232 | gint major = 0; 1233 | gint minor = 0; 1234 | gint subminor = 0; 1235 | gint subsubminor = 0; 1236 | gint major_req = 0; 1237 | gint minor_req = 0; 1238 | gint subminor_req = 0; 1239 | gint subsubminor_req = 0; 1240 | gint version = 0; 1241 | gint version_req = 0; 1242 | gint n_parts = sscanf(version_str, "%d.%d.%d.%d", &major, &minor, &subminor, &subsubminor); 1243 | gint n_parts_req = sscanf(version_req_str, "%2[<>=]%d.%d.%d.%d", op, &major_req, &minor_req, &subminor_req, &subsubminor_req); 1244 | 1245 | if (n_parts > 0) 1246 | version += (major & 0xff) << 24; 1247 | if (n_parts > 1) 1248 | version += (minor & 0xff) << 16; 1249 | if (n_parts > 2) 1250 | version += (subminor & 0xff) << 8; 1251 | if (n_parts > 3) 1252 | version += (subsubminor & 0xff); 1253 | 1254 | if (n_parts_req > 1) 1255 | version_req += (major_req & 0xff) << 24; 1256 | if (n_parts_req > 2) 1257 | version_req += (minor_req & 0xff) << 16; 1258 | if (n_parts_req > 3) 1259 | version_req += (subminor_req & 0xff) << 8; 1260 | if (n_parts_req > 4) 1261 | version_req += (subsubminor_req & 0xff); 1262 | 1263 | if (!g_strcmp0 (op, "==")) 1264 | return version == version_req; 1265 | else if (!g_strcmp0 (op, "<=")) 1266 | return version <= version_req; 1267 | else if (!g_strcmp0 (op, "<")) 1268 | return version < version_req; 1269 | else if (!g_strcmp0 (op, ">=")) 1270 | return version >= version_req; 1271 | else if (!g_strcmp0 (op, ">")) 1272 | return version > version_req; 1273 | else 1274 | GST_WARNING ("Invalid comparison: %s", op); 1275 | 1276 | return FALSE; 1277 | } 1278 | 1279 | static void 1280 | fill_default_blacklist (std::map &blacklist) 1281 | { 1282 | blacklist["MeldaProduction::MRecorder"] = ""; 1283 | } 1284 | 1285 | void 1286 | gst_vst_audio_processor_register(GstPlugin * plugin) 1287 | { 1288 | const gchar *main_exe_path; 1289 | const gchar *paths_env_var; 1290 | const gchar *search_default_paths_env_var; 1291 | const gchar *blacklist_env_var; 1292 | gboolean search_default_paths = TRUE; 1293 | VST3::Hosting::Module::PathList paths; 1294 | std::map blacklist; 1295 | 1296 | GST_DEBUG_CATEGORY_INIT (gst_vst_audio_processor_debug, "vst-audio-processor", 0, 1297 | "VST Audio Processor"); 1298 | 1299 | blacklist_env_var = g_getenv ("GST_VST3_BLACKLIST"); 1300 | fill_default_blacklist (blacklist); 1301 | if (blacklist_env_var) { 1302 | GStrv blacklisted = g_strsplit (blacklist_env_var, ";", 0); 1303 | int i; 1304 | 1305 | for (i = 0; blacklisted[i]; i++) { 1306 | blacklist[blacklisted[i]] = ""; 1307 | } 1308 | g_strfreev (blacklisted); 1309 | } 1310 | gst_plugin_add_dependency_simple (plugin, "GST_VST3_BLACKLIST", NULL, NULL, 1311 | GST_PLUGIN_DEPENDENCY_FLAG_NONE); 1312 | 1313 | search_default_paths_env_var = g_getenv("GST_VST3_SEARCH_DEFAULT_PATHS"); 1314 | if (search_default_paths_env_var) 1315 | search_default_paths = g_ascii_strcasecmp(search_default_paths_env_var, "no"); 1316 | gst_plugin_add_dependency_simple (plugin, "GST_VST3_SEARCH_DEFAULT_PATHS", NULL, NULL, 1317 | GST_PLUGIN_DEPENDENCY_FLAG_NONE); 1318 | 1319 | GST_INFO ("Search default paths: %d", search_default_paths); 1320 | 1321 | register_system_dependencies(plugin); 1322 | 1323 | audio_processor_info_quark = g_quark_from_static_string ("gst-vst-audio-processor-info"); 1324 | 1325 | if (search_default_paths) 1326 | paths = VST3::Hosting::Module::getModulePaths(); 1327 | 1328 | #ifdef HAVE_GST_EXE_PATH 1329 | main_exe_path = gst_get_main_executable_path (); 1330 | if (main_exe_path && search_default_paths) { 1331 | gchar *appdir = g_path_get_dirname (main_exe_path); 1332 | gchar *vst3_exe_path; 1333 | #if defined(__linux__) && !defined(__BIONIC__) 1334 | const gchar vst3_path[] = "vst3"; 1335 | #else 1336 | const gchar vst3_path[] = "VST3"; 1337 | #endif 1338 | 1339 | vst3_exe_path = g_build_filename (appdir, vst3_path, NULL); 1340 | 1341 | GST_INFO_OBJECT (plugin, "Looking up plugins in executable path %s", vst3_exe_path); 1342 | list_paths_with_vst3_extension (paths, vst3_exe_path, TRUE); 1343 | gst_plugin_add_dependency_simple (plugin, NULL, vst3_path, ".vst3", 1344 | (GstPluginDependencyFlags) (GST_PLUGIN_DEPENDENCY_FLAG_RECURSE | 1345 | GST_PLUGIN_DEPENDENCY_FLAG_PATHS_ARE_RELATIVE_TO_EXE | 1346 | GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_SUFFIX)); 1347 | g_free (vst3_exe_path); 1348 | } 1349 | #endif 1350 | 1351 | paths_env_var = g_getenv ("GST_VST3_PLUGIN_PATH"); 1352 | if (paths_env_var) { 1353 | GStrv path_list = g_strsplit (paths_env_var, G_SEARCHPATH_SEPARATOR_S, 0); 1354 | int i; 1355 | 1356 | for (i = 0; path_list[i]; i++) { 1357 | GST_INFO_OBJECT (plugin, "Looking up plugins in env path %s", path_list[i]); 1358 | list_paths_with_vst3_extension (paths, path_list[i], TRUE); 1359 | } 1360 | g_strfreev (path_list); 1361 | } 1362 | gst_plugin_add_dependency_simple (plugin, "GST_VST3_PLUGIN_PATH", NULL, ".vst3", 1363 | (GstPluginDependencyFlags) (GST_PLUGIN_DEPENDENCY_FLAG_RECURSE | 1364 | GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_SUFFIX)); 1365 | 1366 | for (auto& path: paths) { 1367 | std::string err; 1368 | auto mod = VST3::Hosting::Module::create(path, err); 1369 | if (!mod) { 1370 | GST_ERROR("Failed to load module '%s': %s", path.c_str(), err.c_str()); 1371 | continue; 1372 | } 1373 | 1374 | GST_DEBUG("Loaded module '%s' with name '%s'", path.c_str(), mod->getName().c_str()); 1375 | 1376 | auto factory = mod->getFactory(); 1377 | 1378 | auto factory_info = factory.info(); 1379 | GST_DEBUG("Vendor: %s, URL: %s, e-mail: %s", 1380 | factory_info.vendor().c_str(), 1381 | factory_info.url().c_str(), 1382 | factory_info.email().c_str()); 1383 | 1384 | for (auto& class_info: factory.classInfos()) { 1385 | GST_DEBUG("\t Class: %s, category: %s, version: %s", class_info.name().c_str(), 1386 | class_info.category().c_str(), class_info.version().c_str()); 1387 | GTypeQuery type_query; 1388 | g_type_query(gst_vst_audio_processor_get_type(), &type_query); 1389 | auto type_name = create_type_name(type_query.type_name, class_info.name().c_str()); 1390 | if (g_type_from_name (type_name.c_str())) { 1391 | GST_DEBUG("\t Skipping already registered %s", type_name.c_str()); 1392 | continue; 1393 | } 1394 | 1395 | auto blacklist_search = blacklist.find(factory_info.vendor() + "::" + class_info.name()); 1396 | if (blacklist_search != blacklist.end()) { 1397 | if (blacklist_search->second.empty() || 1398 | compare_versions (class_info.version().c_str(), blacklist_search->second.c_str())) { 1399 | GST_DEBUG ("\t Skipping blacklisted %s", type_name.c_str()); 1400 | continue; 1401 | } 1402 | } 1403 | 1404 | auto component = factory.createInstance(class_info.ID()); 1405 | if (!component) { 1406 | GST_DEBUG("\t Failed to create instance for '%s'", class_info.name().c_str()); 1407 | continue; 1408 | } 1409 | 1410 | auto res = component->initialize(gStandardPluginContext); 1411 | if (res != kResultOk) { 1412 | GST_DEBUG("\t Component can't be initialized: 0x%08x", res); 1413 | continue; 1414 | } 1415 | 1416 | // Check if this supports the IAudioProcessor interface 1417 | IPtr audio_processor; 1418 | Vst::IAudioProcessor *audio_processor_ptr = nullptr; 1419 | if (component->queryInterface(Vst::IAudioProcessor::iid, (void **) &audio_processor_ptr) != kResultOk 1420 | || !audio_processor_ptr) { 1421 | GST_DEBUG("\t Component does not implement IAudioProcessor interface"); 1422 | continue; 1423 | } 1424 | audio_processor = shared(audio_processor_ptr); 1425 | 1426 | // Get the controller 1427 | IPtr edit_controller; 1428 | Vst::IEditController *edit_controller_ptr = nullptr; 1429 | if (component->queryInterface(Vst::IEditController::iid, (void**) &edit_controller_ptr) != kResultOk 1430 | || !edit_controller_ptr) { 1431 | FUID controller_cid; 1432 | 1433 | // ask for the associated controller class ID (could be called before processorComponent->initialize ()) 1434 | if (component->getControllerClassId(controller_cid) == kResultOk && controller_cid.isValid ()) { 1435 | // create its controller part created from the factory 1436 | edit_controller = factory.createInstance(controller_cid.toTUID()); 1437 | if (edit_controller) { 1438 | // initialize the component with our context 1439 | res = edit_controller->initialize(gStandardPluginContext); 1440 | if (res != kResultOk) { 1441 | GST_DEBUG("\t Can't initialize edit controller: 0x%08x", res); 1442 | continue; 1443 | } 1444 | } 1445 | } 1446 | } else { 1447 | edit_controller = shared(edit_controller_ptr); 1448 | } 1449 | 1450 | if (!edit_controller) { 1451 | GST_DEBUG("\t No edit controller found"); 1452 | continue; 1453 | } 1454 | 1455 | // Get input audio bus. We only support components with a single audio 1456 | // input and no event inputs 1457 | auto count = component->getBusCount(Vst::MediaTypes::kAudio, Vst::BusDirections::kInput); 1458 | if (count != 1) { 1459 | GST_DEBUG("\t Unsupported number of audio input busses %d", count); 1460 | continue; 1461 | } 1462 | 1463 | count = component->getBusCount(Vst::MediaTypes::kEvent, Vst::BusDirections::kInput); 1464 | if (count != 0) { 1465 | GST_DEBUG("\t Unsupported number of event input busses %d", count); 1466 | continue; 1467 | } 1468 | 1469 | Vst::BusInfo bus_info; 1470 | res = component->getBusInfo(Vst::MediaTypes::kAudio, Vst::BusDirections::kInput, 0, bus_info); 1471 | if (res != kResultOk) { 1472 | GST_DEBUG("\t Failed to get audio input bus info: 0x%08x", res); 1473 | continue; 1474 | } 1475 | 1476 | // TODO: Anything we can do with the bus info? 1477 | 1478 | // Get output audio bus. We only support components with a single audio 1479 | // output and no event outputs 1480 | count = component->getBusCount(Vst::MediaTypes::kAudio, Vst::BusDirections::kOutput); 1481 | if (count != 1) { 1482 | GST_DEBUG("\t Unsupported number of audio output busses %d", count); 1483 | continue; 1484 | } 1485 | 1486 | count = component->getBusCount(Vst::MediaTypes::kEvent, Vst::BusDirections::kOutput); 1487 | if (count != 0) { 1488 | GST_DEBUG("\t Unsupported number of event output busses %d", count); 1489 | continue; 1490 | } 1491 | 1492 | res = component->getBusInfo(Vst::MediaTypes::kAudio, Vst::BusDirections::kOutput, 0, bus_info); 1493 | if (res != kResultOk) { 1494 | GST_DEBUG("\t Failed to get audio output bus info: 0x%08x", res); 1495 | continue; 1496 | } 1497 | 1498 | // TODO: Anything we can do with the bus info? 1499 | 1500 | // Check which sample sizes the component supports 1501 | auto caps = gst_caps_new_empty(); 1502 | 1503 | if (audio_processor->canProcessSampleSize(Vst::kSample32) == kResultOk) { 1504 | gst_caps_append(caps, gst_caps_from_string( 1505 | "audio/x-raw, " 1506 | "format=(string) " GST_AUDIO_NE (F32) ", " 1507 | "layout=(string) interleaved, " 1508 | "rate=(int) [0, MAX]")); 1509 | } 1510 | if (audio_processor->canProcessSampleSize(Vst::kSample64) == kResultOk) { 1511 | gst_caps_append(caps, gst_caps_from_string( 1512 | "audio/x-raw, " 1513 | "format=(string) " GST_AUDIO_NE (F64) ", " 1514 | "layout=(string) interleaved, " 1515 | "rate=(int) [0, MAX]")); 1516 | } 1517 | 1518 | // Check if the component can do mono-mono and/or stereo-stereo 1519 | Vst::SpeakerArrangement inputs[1]; 1520 | Vst::SpeakerArrangement outputs[1]; 1521 | 1522 | GValue channels = G_VALUE_INIT; 1523 | GValue tmp = G_VALUE_INIT; 1524 | g_value_init(&channels, GST_TYPE_LIST); 1525 | g_value_init(&tmp, G_TYPE_INT); 1526 | 1527 | inputs[0] = outputs[0] = Vst::SpeakerArr::kMono; 1528 | if (audio_processor->setBusArrangements(inputs, 1, outputs, 1) == kResultOk) { 1529 | g_value_set_int(&tmp, 1); 1530 | gst_value_list_append_value(&channels, &tmp); 1531 | } 1532 | 1533 | inputs[0] = outputs[0] = Vst::SpeakerArr::kStereo; 1534 | if (audio_processor->setBusArrangements(inputs, 1, outputs, 1) == kResultOk) { 1535 | g_value_set_int(&tmp, 2); 1536 | gst_value_list_append_value(&channels, &tmp); 1537 | } 1538 | 1539 | if (gst_value_list_get_size(&channels) == 1) { 1540 | gst_caps_set_simple(caps, "channels", G_TYPE_INT, g_value_get_int(&tmp), nullptr); 1541 | } else { 1542 | gst_caps_set_value(caps, "channels", &channels); 1543 | } 1544 | 1545 | g_value_unset(&channels); 1546 | g_value_unset(&tmp); 1547 | 1548 | // Get properties 1549 | auto n_properties = edit_controller->getParameterCount(); 1550 | auto properties = g_new0(GstVstAudioProcessorProperty, n_properties); 1551 | for (auto i = 0, k = 0; i < n_properties; i++) { 1552 | Vst::ParameterInfo parameter_info; 1553 | 1554 | if (edit_controller->getParameterInfo(i, parameter_info) != kResultOk) { 1555 | n_properties--; 1556 | continue; 1557 | } 1558 | 1559 | auto title = VST3::StringConvert::convert(parameter_info.title); 1560 | auto short_title = VST3::StringConvert::convert(parameter_info.shortTitle); 1561 | auto units = VST3::StringConvert::convert(parameter_info.units); 1562 | 1563 | properties[k].param_id = parameter_info.id; 1564 | 1565 | auto prop_name = g_ascii_strdown(short_title.length() != 0 ? short_title.c_str() : title.c_str(), -1); 1566 | g_strcanon(prop_name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-+", '-'); 1567 | /* satisfy glib2 (argname[0] must be [A-Za-z]) */ 1568 | if (!((prop_name[0] >= 'a' && prop_name[0] <= 'z') || 1569 | (prop_name[0] >= 'A' && prop_name[0] <= 'Z'))) { 1570 | auto tempstr = prop_name; 1571 | prop_name = g_strconcat("param-", prop_name, nullptr); 1572 | g_free (tempstr); 1573 | } 1574 | 1575 | auto duplicates = 0; 1576 | for (auto j = 0; j < k; j++) { 1577 | if (strcmp(properties[j].name, prop_name) == 0) { 1578 | duplicates++; 1579 | } 1580 | } 1581 | if (duplicates > 0) { 1582 | auto tempstr = prop_name; 1583 | prop_name = g_strdup_printf("%s-%d", prop_name, duplicates); 1584 | g_free(tempstr); 1585 | } 1586 | 1587 | properties[k].name = prop_name; 1588 | properties[k].nick = g_strdup(short_title.length() != 0 ? short_title.c_str() : title.c_str()); 1589 | properties[k].description = units.length() != 0 ? 1590 | g_strdup_printf("%s (%s)", title.c_str(), units.c_str()) : 1591 | g_strdup(title.c_str()); 1592 | 1593 | if (parameter_info.stepCount == 0) { 1594 | properties[k].type = G_TYPE_DOUBLE; 1595 | } else if (parameter_info.stepCount == 1) { 1596 | properties[k].type = G_TYPE_BOOLEAN; 1597 | } else { 1598 | properties[k].type = G_TYPE_INT; 1599 | properties[k].max_value = parameter_info.stepCount; 1600 | } 1601 | 1602 | properties[k].default_value = edit_controller->normalizedParamToPlain(parameter_info.id, 1603 | parameter_info.defaultNormalizedValue); 1604 | properties[k].read_only = parameter_info.flags & Vst::ParameterInfo::kIsReadOnly; 1605 | 1606 | k++; 1607 | } 1608 | 1609 | // And finally register our sub-type 1610 | GTypeInfo type_info = { 0, }; 1611 | type_info.class_size = type_query.class_size; 1612 | type_info.instance_size = type_query.instance_size; 1613 | type_info.class_init = (GClassInitFunc) gst_vst_audio_processor_sub_class_init; 1614 | 1615 | auto type = g_type_register_static(gst_vst_audio_processor_get_type(), type_name.c_str(), &type_info, (GTypeFlags) 0); 1616 | 1617 | auto processor_info = g_new0(GstVstAudioProcessorInfo, 1); 1618 | processor_info->name = g_strdup(class_info.name().c_str()); 1619 | processor_info->caps = caps; 1620 | processor_info->path = g_strdup(path.c_str()); 1621 | processor_info->class_id = class_info.ID(); 1622 | processor_info->properties = properties; 1623 | processor_info->n_properties = n_properties; 1624 | 1625 | g_type_set_qdata(type, audio_processor_info_quark, processor_info); 1626 | 1627 | auto element_name = 1628 | create_element_name("vstaudioprocessor-", processor_info->name); 1629 | 1630 | gst_element_register(plugin, element_name.c_str(), GST_RANK_NONE, type); 1631 | 1632 | edit_controller->terminate(); 1633 | component->terminate(); 1634 | } 1635 | } 1636 | } 1637 | 1638 | --------------------------------------------------------------------------------