Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
6.3KB

  1. //-----------------------------------------------------------------------------
  2. // Project : VST SDK
  3. //
  4. // Category : Helpers
  5. // Filename : public.sdk/source/vst/interappaudio/AudioIO.h
  6. // Created by : Steinberg, 08/2013.
  7. // Description : VST 3 InterAppAudio
  8. //
  9. //-----------------------------------------------------------------------------
  10. // LICENSE
  11. // (c) 2017, Steinberg Media Technologies GmbH, All Rights Reserved
  12. //-----------------------------------------------------------------------------
  13. // Redistribution and use in source and binary forms, with or without modification,
  14. // are permitted provided that the following conditions are met:
  15. //
  16. // * Redistributions of source code must retain the above copyright notice,
  17. // this list of conditions and the following disclaimer.
  18. // * Redistributions in binary form must reproduce the above copyright notice,
  19. // this list of conditions and the following disclaimer in the documentation
  20. // and/or other materials provided with the distribution.
  21. // * Neither the name of the Steinberg Media Technologies nor the names of its
  22. // contributors may be used to endorse or promote products derived from this
  23. // software without specific prior written permission.
  24. //
  25. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  26. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  27. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  28. // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  29. // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  33. // OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  34. // OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //-----------------------------------------------------------------------------
  36. #pragma once
  37. /// \cond ignore
  38. #include <AudioUnit/AUComponent.h>
  39. #include <AudioToolbox/AudioToolbox.h>
  40. #include "pluginterfaces/base/funknown.h"
  41. #include "pluginterfaces/vst/vsttypes.h"
  42. #include <vector>
  43. #ifndef __OBJC__
  44. struct UIImage;
  45. struct NSString;
  46. #endif
  47. namespace Steinberg {
  48. namespace Vst {
  49. namespace InterAppAudio {
  50. class AudioIO;
  51. //------------------------------------------------------------------------
  52. class IMidiProcessor
  53. {
  54. public:
  55. virtual void onMIDIEvent (UInt32 status, UInt32 data1, UInt32 data2, UInt32 sampleOffset, bool withinRealtimeThread) = 0;
  56. };
  57. //------------------------------------------------------------------------
  58. class IAudioIOProcessor : public IMidiProcessor
  59. {
  60. public:
  61. virtual void willStartAudio (AudioIO* audioIO) = 0;
  62. virtual void didStopAudio (AudioIO* audioIO) = 0;
  63. virtual void process (const AudioTimeStamp* timeStamp, UInt32 busNumber, UInt32 numFrames, AudioBufferList* ioData, bool& outputIsSilence, AudioIO* audioIO) = 0;
  64. };
  65. //------------------------------------------------------------------------
  66. class AudioIO
  67. {
  68. public:
  69. static AudioIO* instance ();
  70. tresult init (OSType type, OSType subType, OSType manufacturer, CFStringRef name);
  71. bool switchToHost ();
  72. bool sendRemoteControlEvent (AudioUnitRemoteControlEvent event);
  73. UIImage* getHostIcon ();
  74. tresult start ();
  75. tresult stop ();
  76. tresult addProcessor (IAudioIOProcessor* processor);
  77. tresult removeProcessor (IAudioIOProcessor* processor);
  78. // accessors
  79. AudioUnit getRemoteIO () const { return remoteIO; }
  80. SampleRate getSampleRate () const { return sampleRate; }
  81. bool getInterAppAudioConnected () const { return interAppAudioConnected; }
  82. // host context information
  83. bool getBeatAndTempo (Float64& beat, Float64& tempo);
  84. bool getMusicalTimeLocation (UInt32& deltaSampleOffset, Float32& timeSigNumerator, UInt32& timeSigDenominator, Float64& downBeat);
  85. bool getTransportState (Boolean& isPlaying, Boolean& isRecording, Boolean& transportStateChanged, Float64& sampleInTimeLine, Boolean& isCycling, Float64& cycleStartBeat, Float64& cycleEndBeat);
  86. void setStaticFallbackTempo (Float64 tempo) {staticTempo = tempo; }
  87. Float64 getStaticFallbackTempo () const { return staticTempo; }
  88. static NSString* kConnectionStateChange;
  89. //------------------------------------------------------------------------
  90. protected:
  91. AudioIO ();
  92. ~AudioIO ();
  93. static OSStatus inputCallbackStatic (void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData);
  94. static OSStatus renderCallbackStatic (void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData);
  95. static void propertyChangeStatic (void *inRefCon, AudioUnit inUnit, AudioUnitPropertyID inID, AudioUnitScope inScope, AudioUnitElement inElement);
  96. static void midiEventCallbackStatic (void *inRefCon, UInt32 inStatus, UInt32 inData1, UInt32 inData2, UInt32 inOffsetSampleFrame);
  97. OSStatus inputCallback (AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData);
  98. OSStatus renderCallback (AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData);
  99. void midiEventCallback (UInt32 inStatus, UInt32 inData1, UInt32 inData2, UInt32 inOffsetSampleFrame);
  100. void remoteIOPropertyChanged (AudioUnitPropertyID inID, AudioUnitScope inScope, AudioUnitElement inElement);
  101. void setAudioSessionActive (bool state);
  102. tresult setupRemoteIO (OSType type);
  103. tresult setupAUGraph (OSType type);
  104. void updateInterAppAudioConnectionState ();
  105. AudioUnit remoteIO;
  106. AUGraph graph;
  107. AudioBufferList* ioBufferList;
  108. HostCallbackInfo hostCallback;
  109. UInt32 maxFrames;
  110. Float64 staticTempo;
  111. SampleRate sampleRate;
  112. bool interAppAudioConnected;
  113. std::vector<IAudioIOProcessor*> audioProcessors;
  114. enum InternalState {
  115. kUninitialized,
  116. kInitialized,
  117. kStarted,
  118. };
  119. InternalState internalState;
  120. };
  121. //------------------------------------------------------------------------
  122. } // namespace InterAppAudio
  123. } // namespace Vst
  124. } // namespace Steinberg
  125. /// \endcond