Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Interop;
- namespace PortAudio
- {
- public static class PortAudio
- {
- #if BF_PLATFORM_WINDOWS
- #if BF_64_BIT
- const String dllName = "libportaudio64bit.dll";
- #else
- const String dllName = "libportaudio32bit.dll";
- #endif
- #elif BF_PLATFORM_LINUX
- const String dllName = "jportaudio.dll";
- #endif
- public typealias Time = double;
- public typealias Stream = void;
- public typealias StreamCallbackFlags = c_uint;
- public typealias DeviceIndex = c_int;
- public typealias HostApiIndex = c_int;
- [Import(dllName), LinkName("Pa_Initialize")] public static extern Error Initialize();
- [Import(dllName), LinkName("Pa_Terminate")] public static extern Error Terminate();
- [Import(dllName), LinkName("Pa_GetVersion")] public static extern int GetVersion(Stream* stream);
- [Import(dllName), LinkName("Pa_GetVersionText")] public static extern char8* GetVersionText();
- [CRepr] public struct StreamCallbackTimeInfo : this(Time inputBufferAdcTime, Time currentTime, Time outputBufferDacTime);
- public typealias StreamCallback = function [CallingConvention(.Stdcall)] int(void* input, void* output, uint32 frameCount, StreamCallbackTimeInfo* timeInfo, StreamCallbackFlags statusFlags, void* userData);
- [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);
- [Import(dllName), LinkName("Pa_StartStream")] public static extern Error StartStream(Stream* stream);
- [Import(dllName), LinkName("Pa_StopStream")] public static extern Error StopStream(Stream* stream);
- [Import(dllName), LinkName("Pa_AbortStream")] public static extern Error AbortStream(Stream* stream);
- [Import(dllName), LinkName("Pa_CloseStream")] public static extern Error CloseStream(Stream* stream);
- [Import(dllName), LinkName("Pa_Sleep")] public static extern void Sleep(c_uint msec);
- [Import(dllName), LinkName("Pa_WriteStream")] public static extern Error WriteStream(Stream* stream, void* buffer, int numFrames);
- [Import(dllName), LinkName("Pa_ReadStream")] public static extern Error ReadStream(Stream* stream, void* buffer, int numFrames);
- [Import(dllName), LinkName("Pa_IsStreamStopped")] public static extern Error IsStreamStopped(Stream* stream);
- [Import(dllName), LinkName("Pa_IsStreamActive")] public static extern Error IsStreamActive(Stream* stream);
- [Import(dllName), LinkName("Pa_GetStreamTime")] public static extern Error GetStreamTime(Stream* stream);
- [Import(dllName), LinkName("Pa_GetStreamInfo")] public static extern Error GetStreamInfo(Stream* stream);
- [Import(dllName), LinkName("Pa_GetStreamReadAvailable")] public static extern int GetStreamReadAvailable(Stream* stream);
- [Import(dllName), LinkName("Pa_GetStreamWriteAvailable")] public static extern int GetStreamWriteAvailable(Stream* stream);
- [Import(dllName), LinkName("Pa_GetHostApiCount")] public static extern int GetHostApiCount();
- [Import(dllName), LinkName("Pa_GetHostApiInfo")] public static extern HostApiInfo* GetHostApiInfo(int index);
- [Import(dllName), LinkName("Pa_HostApiTypeIdToHostApiIndex")] public static extern HostApiIndex HostApiTypeIdToHostApiIndex(HostApiTypeId hostApiType);
- [Import(dllName), LinkName("Pa_HostApiDeviceIndexToDeviceIndex")] public static extern DeviceIndex HostApiDeviceIndexToDeviceIndex(HostApiIndex hostApiIndex, int apiDeviceIndex);
- [Import(dllName), LinkName("Pa_GetDeviceCount")] public static extern int GetDeviceCount();
- [Import(dllName), LinkName("Pa_GetDeviceInfo")] public static extern DeviceInfo* GetDeviceInfo(int index);
- [Import(dllName), LinkName("Pa_GetDefaultInputDevice")] public static extern int GetDefaultInputDevice();
- [Import(dllName), LinkName("Pa_GetDefaultOutputDevice")] public static extern int GetDefaultOutputDevice();
- [Import(dllName), LinkName("Pa_GetDefaultHostApi")] public static extern int GetDefaultHostApi();
- [Import(dllName), LinkName("Pa_GetLastHostErrorInfo")] public static extern HostErrorInfo* GetLastHostErrorInfo();
- [Import(dllName), LinkName("Pa_IsFormatSupported")] public static extern Error IsFormatSupported(StreamParameters* inputStreamParameters, StreamParameters* outputStreamParameters, double sampleRate);
- [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);
- public static c_int UseHostApiSpecificDeviceSpecification = -2;
- [CRepr] public struct HostErrorInfo
- {
- public HostApiTypeId hostApiType;
- public c_long errorCode;
- public c_char* errorText;
- public override void ToString(String s)
- {
- var text = scope String(errorText);
- s.Append(scope $"Error {errorCode} on host({hostApiType}) :: {text}");
- }
- }
- [CRepr] public struct StreamParameters
- {
- public DeviceIndex device = 0;
- public c_int channelCount = 2;
- public SampleFormat sampleFormat = .Float32;
- public Time suggestedLatency = 0.050;
- public void* hostSpecificStreamInfo;
- public override void ToString(String s)
- {
- s.Append(scope $"Device[{device}] (channelCount={channelCount}, sampleFormat = {sampleFormat}, suggestedLatency = {suggestedLatency})");
- }
- }
- [CRepr] public struct DeviceInfo
- {
- public c_int version;
- public c_char* name;
- public c_int hostApi;
- public c_int maxInputChannels;
- public c_int maxOutputChannels;
- public double defaultLowInputLatency;
- public double defaultHighInputLatency;
- public double defaultLowOutputLatency;
- public double defaultHighOutputLatency;
- public double defaultSampleRate;
- }
- [CRepr] public struct HostApiInfo
- {
- public c_int version;
- public HostApiTypeId type;
- public c_char* name;
- public c_int deviceCount;
- public DeviceIndex defaultInputDevice;
- public DeviceIndex defaultOutputDevice;
- }
- [CRepr] public struct StreamInfo
- {
- public c_int structVersion;
- public double outputLatency;
- public double inputLatency;
- public double sampleRate;
- }
- public enum Error : c_int
- {
- NoError = 0,
- NotInitialized = -10000,
- UnanticipatedHostError,
- InvalidChannelCount,
- InvalidSampleRate,
- InvalidDevice,
- InvalidFlag,
- SampleFormatNotSupported,
- BadIODeviceCombination,
- InsufficientMemory,
- BufferTooBig,
- BufferTooSmall,
- NullCallback,
- BadStreamPtr,
- TimedOut,
- InternalError,
- DeviceUnavailable,
- IncompatibleHostApiSpecificStreamInfo,
- StreamIsStopped,
- StreamIsNotStopped,
- InputOverflowed,
- OutputUnderflowed,
- HostApiNotFound,
- InvalidHostApi,
- CanNotReadFromACallbackStream,
- CanNotWriteToACallbackStream,
- CanNotReadFromAnOutputOnlyStream,
- CanNotWriteToAnInputOnlyStream,
- IncompatibleStreamHostApi,
- BadBufferPtr
- }
- public enum StreamFlags : c_ulong
- {
- NoFlag = 0,
- ClipOff = 1,
- DitherOff = 2,
- NeverDropInput = 4,
- PrimeOutputBuffersUsingStreamCallback = 8,
- PlatformSpecificFlags = 0xFFFF0000
- }
- public enum SampleFormat : c_ulong
- {
- Float32 = 1,
- Int32 = 2,
- Int24 = 4,
- Int16 = 8,
- Int8 = 16,
- UInt8 = 32,
- CustomFormat = 0x00010000,
- NonInterleaved = 0x80000000,
- }
- public enum HostApiTypeId : c_int
- {
- InDevelopment = 0, //use while developing support for a new host API
- DirectSound = 1,
- MME = 2,
- ASIO = 3,
- SoundManager = 4,
- CoreAudio = 5,
- OSS = 7,
- ALSA = 8,
- AL = 9,
- BeOS = 10,
- WDMKS = 11,
- JACK = 12,
- WASAPI = 13,
- AudioScienceHPI = 14
- }
- public enum StreamCallbackFlag : c_uint
- {
- InputUnderflow = 1,
- InputOverflow = 2,
- OutputUnderflow = 4,
- OutputOverflow = 8,
- PrimingOutput = 16,
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement