Advertisement
Guest User

Beef bindings for PortAudio

a guest
May 6th, 2025
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.20 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Interop;
  4.  
  5. namespace PortAudio
  6. {
  7. public static class PortAudio
  8. {
  9. #if BF_PLATFORM_WINDOWS
  10. #if BF_64_BIT
  11. const String dllName = "libportaudio64bit.dll";
  12. #else
  13. const String dllName = "libportaudio32bit.dll";
  14. #endif
  15. #elif BF_PLATFORM_LINUX
  16. const String dllName = "jportaudio.dll";
  17. #endif
  18.  
  19. public typealias Time = double;
  20. public typealias Stream = void;
  21. public typealias StreamCallbackFlags = c_uint;
  22. public typealias DeviceIndex = c_int;
  23. public typealias HostApiIndex = c_int;
  24.  
  25. [Import(dllName), LinkName("Pa_Initialize")] public static extern Error Initialize();
  26. [Import(dllName), LinkName("Pa_Terminate")] public static extern Error Terminate();
  27.  
  28. [Import(dllName), LinkName("Pa_GetVersion")] public static extern int GetVersion(Stream* stream);
  29. [Import(dllName), LinkName("Pa_GetVersionText")] public static extern char8* GetVersionText();
  30.  
  31. [CRepr] public struct StreamCallbackTimeInfo : this(Time inputBufferAdcTime, Time currentTime, Time outputBufferDacTime);
  32. public typealias StreamCallback = function [CallingConvention(.Stdcall)] int(void* input, void* output, uint32 frameCount, StreamCallbackTimeInfo* timeInfo, StreamCallbackFlags statusFlags, void* userData);
  33.  
  34. [Import(dllName), LinkName("Pa_OpenStream")] public static extern Error OpenStream(Stream** s, StreamParameters* inputStreamParameters, StreamParameters* outputStreamParameters, double sampleRate, c_ulong framesPerBuffer, StreamFlags flags, StreamCallback callback, void* userData);
  35. [Import(dllName), LinkName("Pa_StartStream")] public static extern Error StartStream(Stream* stream);
  36. [Import(dllName), LinkName("Pa_StopStream")] public static extern Error StopStream(Stream* stream);
  37. [Import(dllName), LinkName("Pa_AbortStream")] public static extern Error AbortStream(Stream* stream);
  38. [Import(dllName), LinkName("Pa_CloseStream")] public static extern Error CloseStream(Stream* stream);
  39.  
  40. [Import(dllName), LinkName("Pa_Sleep")] public static extern void Sleep(c_uint msec);
  41.  
  42. [Import(dllName), LinkName("Pa_WriteStream")] public static extern Error WriteStream(Stream* stream, void* buffer, int numFrames);
  43. [Import(dllName), LinkName("Pa_ReadStream")] public static extern Error ReadStream(Stream* stream, void* buffer, int numFrames);
  44.  
  45. [Import(dllName), LinkName("Pa_IsStreamStopped")] public static extern Error IsStreamStopped(Stream* stream);
  46. [Import(dllName), LinkName("Pa_IsStreamActive")] public static extern Error IsStreamActive(Stream* stream);
  47. [Import(dllName), LinkName("Pa_GetStreamTime")] public static extern Error GetStreamTime(Stream* stream);
  48. [Import(dllName), LinkName("Pa_GetStreamInfo")] public static extern Error GetStreamInfo(Stream* stream);
  49. [Import(dllName), LinkName("Pa_GetStreamReadAvailable")] public static extern int GetStreamReadAvailable(Stream* stream);
  50. [Import(dllName), LinkName("Pa_GetStreamWriteAvailable")] public static extern int GetStreamWriteAvailable(Stream* stream);
  51.  
  52. [Import(dllName), LinkName("Pa_GetHostApiCount")] public static extern int GetHostApiCount();
  53. [Import(dllName), LinkName("Pa_GetHostApiInfo")] public static extern HostApiInfo* GetHostApiInfo(int index);
  54. [Import(dllName), LinkName("Pa_HostApiTypeIdToHostApiIndex")] public static extern HostApiIndex HostApiTypeIdToHostApiIndex(HostApiTypeId hostApiType);
  55. [Import(dllName), LinkName("Pa_HostApiDeviceIndexToDeviceIndex")] public static extern DeviceIndex HostApiDeviceIndexToDeviceIndex(HostApiIndex hostApiIndex, int apiDeviceIndex);
  56.  
  57. [Import(dllName), LinkName("Pa_GetDeviceCount")] public static extern int GetDeviceCount();
  58. [Import(dllName), LinkName("Pa_GetDeviceInfo")] public static extern DeviceInfo* GetDeviceInfo(int index);
  59.  
  60. [Import(dllName), LinkName("Pa_GetDefaultInputDevice")] public static extern int GetDefaultInputDevice();
  61. [Import(dllName), LinkName("Pa_GetDefaultOutputDevice")] public static extern int GetDefaultOutputDevice();
  62. [Import(dllName), LinkName("Pa_GetDefaultHostApi")] public static extern int GetDefaultHostApi();
  63.  
  64. [Import(dllName), LinkName("Pa_GetLastHostErrorInfo")] public static extern HostErrorInfo* GetLastHostErrorInfo();
  65.  
  66. [Import(dllName), LinkName("Pa_IsFormatSupported")] public static extern Error IsFormatSupported(StreamParameters* inputStreamParameters, StreamParameters* outputStreamParameters, double sampleRate);
  67.  
  68. [Import(dllName), LinkName("Pa_OpenDefaultStream")] public static extern Error OpenDefaultStream(Stream** stream, c_int numInputChannels, c_int numOutputChannels, SampleFormat sampleFormat, double sampleRate, uint32 framesPerBuffer, StreamCallback streamCallback, void* userData);
  69.  
  70. public static c_int UseHostApiSpecificDeviceSpecification = -2;
  71.  
  72. [CRepr] public struct HostErrorInfo
  73. {
  74. public HostApiTypeId hostApiType;
  75. public c_long errorCode;
  76. public c_char* errorText;
  77.  
  78. public override void ToString(String s)
  79. {
  80. var text = scope String(errorText);
  81. s.Append(scope $"Error {errorCode} on host({hostApiType}) :: {text}");
  82. }
  83. }
  84.  
  85. [CRepr] public struct StreamParameters
  86. {
  87. public DeviceIndex device = 0;
  88. public c_int channelCount = 2;
  89. public SampleFormat sampleFormat = .Float32;
  90. public Time suggestedLatency = 0.050;
  91. public void* hostSpecificStreamInfo;
  92.  
  93. public override void ToString(String s)
  94. {
  95.  
  96. s.Append(scope $"Device[{device}] (channelCount={channelCount}, sampleFormat = {sampleFormat}, suggestedLatency = {suggestedLatency})");
  97. }
  98. }
  99.  
  100. [CRepr] public struct DeviceInfo
  101. {
  102. public c_int version;
  103. public c_char* name;
  104. public c_int hostApi;
  105. public c_int maxInputChannels;
  106. public c_int maxOutputChannels;
  107. public double defaultLowInputLatency;
  108. public double defaultHighInputLatency;
  109. public double defaultLowOutputLatency;
  110. public double defaultHighOutputLatency;
  111. public double defaultSampleRate;
  112. }
  113.  
  114. [CRepr] public struct HostApiInfo
  115. {
  116. public c_int version;
  117. public HostApiTypeId type;
  118. public c_char* name;
  119.  
  120. public c_int deviceCount;
  121.  
  122. public DeviceIndex defaultInputDevice;
  123. public DeviceIndex defaultOutputDevice;
  124. }
  125.  
  126.  
  127. [CRepr] public struct StreamInfo
  128. {
  129. public c_int structVersion;
  130. public double outputLatency;
  131. public double inputLatency;
  132. public double sampleRate;
  133. }
  134.  
  135. public enum Error : c_int
  136. {
  137. NoError = 0,
  138.  
  139. NotInitialized = -10000,
  140. UnanticipatedHostError,
  141. InvalidChannelCount,
  142. InvalidSampleRate,
  143. InvalidDevice,
  144. InvalidFlag,
  145. SampleFormatNotSupported,
  146. BadIODeviceCombination,
  147. InsufficientMemory,
  148. BufferTooBig,
  149. BufferTooSmall,
  150. NullCallback,
  151. BadStreamPtr,
  152. TimedOut,
  153. InternalError,
  154. DeviceUnavailable,
  155. IncompatibleHostApiSpecificStreamInfo,
  156. StreamIsStopped,
  157. StreamIsNotStopped,
  158. InputOverflowed,
  159. OutputUnderflowed,
  160. HostApiNotFound,
  161. InvalidHostApi,
  162. CanNotReadFromACallbackStream,
  163. CanNotWriteToACallbackStream,
  164. CanNotReadFromAnOutputOnlyStream,
  165. CanNotWriteToAnInputOnlyStream,
  166. IncompatibleStreamHostApi,
  167. BadBufferPtr
  168. }
  169.  
  170. public enum StreamFlags : c_ulong
  171. {
  172. NoFlag = 0,
  173. ClipOff = 1,
  174. DitherOff = 2,
  175. NeverDropInput = 4,
  176. PrimeOutputBuffersUsingStreamCallback = 8,
  177. PlatformSpecificFlags = 0xFFFF0000
  178. }
  179.  
  180. public enum SampleFormat : c_ulong
  181. {
  182. Float32 = 1,
  183. Int32 = 2,
  184. Int24 = 4,
  185. Int16 = 8,
  186. Int8 = 16,
  187. UInt8 = 32,
  188. CustomFormat = 0x00010000,
  189. NonInterleaved = 0x80000000,
  190. }
  191.  
  192. public enum HostApiTypeId : c_int
  193. {
  194. InDevelopment = 0, //use while developing support for a new host API
  195. DirectSound = 1,
  196. MME = 2,
  197. ASIO = 3,
  198. SoundManager = 4,
  199. CoreAudio = 5,
  200. OSS = 7,
  201. ALSA = 8,
  202. AL = 9,
  203. BeOS = 10,
  204. WDMKS = 11,
  205. JACK = 12,
  206. WASAPI = 13,
  207. AudioScienceHPI = 14
  208. }
  209.  
  210. public enum StreamCallbackFlag : c_uint
  211. {
  212. InputUnderflow = 1,
  213. InputOverflow = 2,
  214. OutputUnderflow = 4,
  215. OutputOverflow = 8,
  216. PrimingOutput = 16,
  217. }
  218. }
  219. }
  220.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement