jack2 codebase
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.

1128 lines
45KB

  1. #ifndef PORTAUDIO_H
  2. #define PORTAUDIO_H
  3. /*
  4. * $Id: portaudio.h,v 1.1.2.2 2006/06/20 14:44:48 letz Exp $
  5. * PortAudio Portable Real-Time Audio Library
  6. * PortAudio API Header File
  7. * Latest version available at: http://www.portaudio.com/
  8. *
  9. * Copyright (c) 1999-2002 Ross Bencina and Phil Burk
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining
  12. * a copy of this software and associated documentation files
  13. * (the "Software"), to deal in the Software without restriction,
  14. * including without limitation the rights to use, copy, modify, merge,
  15. * publish, distribute, sublicense, and/or sell copies of the Software,
  16. * and to permit persons to whom the Software is furnished to do so,
  17. * subject to the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be
  20. * included in all copies or substantial portions of the Software.
  21. *
  22. * Any person wishing to distribute modifications to the Software is
  23. * requested to send the modifications to the original developer so that
  24. * they can be incorporated into the canonical version.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  29. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  30. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  31. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  32. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. */
  34. /** @file
  35. @brief The PortAudio API.
  36. */
  37. #ifdef __cplusplus
  38. extern "C"
  39. {
  40. #endif /* __cplusplus */
  41. /** Retrieve the release number of the currently running PortAudio build,
  42. eg 1900.
  43. */
  44. int Pa_GetVersion( void );
  45. /** Retrieve a textual description of the current PortAudio build,
  46. eg "PortAudio V19-devel 13 October 2002".
  47. */
  48. const char* Pa_GetVersionText( void );
  49. /** Error codes returned by PortAudio functions.
  50. Note that with the exception of paNoError, all PaErrorCodes are negative.
  51. */
  52. typedef int PaError;
  53. typedef enum PaErrorCode {
  54. paNoError = 0,
  55. paNotInitialized = -10000,
  56. paUnanticipatedHostError,
  57. paInvalidChannelCount,
  58. paInvalidSampleRate,
  59. paInvalidDevice,
  60. paInvalidFlag,
  61. paSampleFormatNotSupported,
  62. paBadIODeviceCombination,
  63. paInsufficientMemory,
  64. paBufferTooBig,
  65. paBufferTooSmall,
  66. paNullCallback,
  67. paBadStreamPtr,
  68. paTimedOut,
  69. paInternalError,
  70. paDeviceUnavailable,
  71. paIncompatibleHostApiSpecificStreamInfo,
  72. paStreamIsStopped,
  73. paStreamIsNotStopped,
  74. paInputOverflowed,
  75. paOutputUnderflowed,
  76. paHostApiNotFound,
  77. paInvalidHostApi,
  78. paCanNotReadFromACallbackStream, /**< @todo review error code name */
  79. paCanNotWriteToACallbackStream, /**< @todo review error code name */
  80. paCanNotReadFromAnOutputOnlyStream, /**< @todo review error code name */
  81. paCanNotWriteToAnInputOnlyStream, /**< @todo review error code name */
  82. paIncompatibleStreamHostApi,
  83. paBadBufferPtr
  84. }
  85. PaErrorCode;
  86. /** Translate the supplied PortAudio error code into a human readable
  87. message.
  88. */
  89. const char *Pa_GetErrorText( PaError errorCode );
  90. /** Library initialization function - call this before using PortAudio.
  91. This function initialises internal data structures and prepares underlying
  92. host APIs for use. This function MUST be called before using any other
  93. PortAudio API functions.
  94. If Pa_Initialize() is called multiple times, each successful
  95. call must be matched with a corresponding call to Pa_Terminate().
  96. Pairs of calls to Pa_Initialize()/Pa_Terminate() may overlap, and are not
  97. required to be fully nested.
  98. Note that if Pa_Initialize() returns an error code, Pa_Terminate() should
  99. NOT be called.
  100. @return paNoError if successful, otherwise an error code indicating the cause
  101. of failure.
  102. @see Pa_Terminate
  103. */
  104. PaError Pa_Initialize( void );
  105. /** Library termination function - call this when finished using PortAudio.
  106. This function deallocates all resources allocated by PortAudio since it was
  107. initializied by a call to Pa_Initialize(). In cases where Pa_Initialise() has
  108. been called multiple times, each call must be matched with a corresponding call
  109. to Pa_Terminate(). The final matching call to Pa_Terminate() will automatically
  110. close any PortAudio streams that are still open.
  111. Pa_Terminate() MUST be called before exiting a program which uses PortAudio.
  112. Failure to do so may result in serious resource leaks, such as audio devices
  113. not being available until the next reboot.
  114. @return paNoError if successful, otherwise an error code indicating the cause
  115. of failure.
  116. @see Pa_Initialize
  117. */
  118. PaError Pa_Terminate( void );
  119. /** The type used to refer to audio devices. Values of this type usually
  120. range from 0 to (Pa_DeviceCount-1), and may also take on the PaNoDevice
  121. and paUseHostApiSpecificDeviceSpecification values.
  122. @see Pa_DeviceCount, paNoDevice, paUseHostApiSpecificDeviceSpecification
  123. */
  124. typedef int PaDeviceIndex;
  125. /** A special PaDeviceIndex value indicating that no device is available,
  126. or should be used.
  127. @see PaDeviceIndex
  128. */
  129. #define paNoDevice ((PaDeviceIndex)-1)
  130. /** A special PaDeviceIndex value indicating that the device(s) to be used
  131. are specified in the host api specific stream info structure.
  132. @see PaDeviceIndex
  133. */
  134. #define paUseHostApiSpecificDeviceSpecification ((PaDeviceIndex)-2)
  135. /* Host API enumeration mechanism */
  136. /** The type used to enumerate to host APIs at runtime. Values of this type
  137. range from 0 to (Pa_GetHostApiCount()-1).
  138. @see Pa_GetHostApiCount
  139. */
  140. typedef int PaHostApiIndex;
  141. /** Retrieve the number of available host APIs. Even if a host API is
  142. available it may have no devices available.
  143. @return A non-negative value indicating the number of available host APIs
  144. or, a PaErrorCode (which are always negative) if PortAudio is not initialized
  145. or an error is encountered.
  146. @see PaHostApiIndex
  147. */
  148. PaHostApiIndex Pa_GetHostApiCount( void );
  149. /** Retrieve the index of the default host API. The default host API will be
  150. the lowest common denominator host API on the current platform and is
  151. unlikely to provide the best performance.
  152. @return A non-negative value ranging from 0 to (Pa_GetHostApiCount()-1)
  153. indicating the default host API index or, a PaErrorCode (which are always
  154. negative) if PortAudio is not initialized or an error is encountered.
  155. */
  156. PaHostApiIndex Pa_GetDefaultHostApi( void );
  157. /** Unchanging unique identifiers for each supported host API. This type
  158. is used in the PaHostApiInfo structure. The values are guaranteed to be
  159. unique and to never change, thus allowing code to be written that
  160. conditionally uses host API specific extensions.
  161. New type ids will be allocated when support for a host API reaches
  162. "public alpha" status, prior to that developers should use the
  163. paInDevelopment type id.
  164. @see PaHostApiInfo
  165. */
  166. typedef enum PaHostApiTypeId {
  167. paInDevelopment = 0, /* use while developing support for a new host API */
  168. paDirectSound = 1,
  169. paMME = 2,
  170. paASIO = 3,
  171. paSoundManager = 4,
  172. paCoreAudio = 5,
  173. paOSS = 7,
  174. paALSA = 8,
  175. paAL = 9,
  176. paBeOS = 10,
  177. paWDMKS = 11,
  178. paJACK = 12,
  179. paWASAPI = 13
  180. }
  181. PaHostApiTypeId;
  182. /** A structure containing information about a particular host API. */
  183. typedef struct PaHostApiInfo {
  184. /** this is struct version 1 */
  185. int structVersion;
  186. /** The well known unique identifier of this host API @see PaHostApiTypeId */
  187. PaHostApiTypeId type;
  188. /** A textual description of the host API for display on user interfaces. */
  189. const char *name;
  190. /** The number of devices belonging to this host API. This field may be
  191. used in conjunction with Pa_HostApiDeviceIndexToDeviceIndex() to enumerate
  192. all devices for this host API.
  193. @see Pa_HostApiDeviceIndexToDeviceIndex
  194. */
  195. int deviceCount;
  196. /** The default input device for this host API. The value will be a
  197. device index ranging from 0 to (Pa_GetDeviceCount()-1), or paNoDevice
  198. if no default input device is available.
  199. */
  200. PaDeviceIndex defaultInputDevice;
  201. /** The default output device for this host API. The value will be a
  202. device index ranging from 0 to (Pa_GetDeviceCount()-1), or paNoDevice
  203. if no default output device is available.
  204. */
  205. PaDeviceIndex defaultOutputDevice;
  206. }
  207. PaHostApiInfo;
  208. /** Retrieve a pointer to a structure containing information about a specific
  209. host Api.
  210. @param hostApi A valid host API index ranging from 0 to (Pa_GetHostApiCount()-1)
  211. @return A pointer to an immutable PaHostApiInfo structure describing
  212. a specific host API. If the hostApi parameter is out of range or an error
  213. is encountered, the function returns NULL.
  214. The returned structure is owned by the PortAudio implementation and must not
  215. be manipulated or freed. The pointer is only guaranteed to be valid between
  216. calls to Pa_Initialize() and Pa_Terminate().
  217. */
  218. const PaHostApiInfo * Pa_GetHostApiInfo( PaHostApiIndex hostApi );
  219. /** Convert a static host API unique identifier, into a runtime
  220. host API index.
  221. @param type A unique host API identifier belonging to the PaHostApiTypeId
  222. enumeration.
  223. @return A valid PaHostApiIndex ranging from 0 to (Pa_GetHostApiCount()-1) or,
  224. a PaErrorCode (which are always negative) if PortAudio is not initialized
  225. or an error is encountered.
  226. The paHostApiNotFound error code indicates that the host API specified by the
  227. type parameter is not available.
  228. @see PaHostApiTypeId
  229. */
  230. PaHostApiIndex Pa_HostApiTypeIdToHostApiIndex( PaHostApiTypeId type );
  231. /** Convert a host-API-specific device index to standard PortAudio device index.
  232. This function may be used in conjunction with the deviceCount field of
  233. PaHostApiInfo to enumerate all devices for the specified host API.
  234. @param hostApi A valid host API index ranging from 0 to (Pa_GetHostApiCount()-1)
  235. @param hostApiDeviceIndex A valid per-host device index in the range
  236. 0 to (Pa_GetHostApiInfo(hostApi)->deviceCount-1)
  237. @return A non-negative PaDeviceIndex ranging from 0 to (Pa_GetDeviceCount()-1)
  238. or, a PaErrorCode (which are always negative) if PortAudio is not initialized
  239. or an error is encountered.
  240. A paInvalidHostApi error code indicates that the host API index specified by
  241. the hostApi parameter is out of range.
  242. A paInvalidDevice error code indicates that the hostApiDeviceIndex parameter
  243. is out of range.
  244. @see PaHostApiInfo
  245. */
  246. PaDeviceIndex Pa_HostApiDeviceIndexToDeviceIndex( PaHostApiIndex hostApi,
  247. int hostApiDeviceIndex );
  248. /** Structure used to return information about a host error condition.
  249. */
  250. typedef struct PaHostErrorInfo {
  251. PaHostApiTypeId hostApiType; /**< the host API which returned the error code */
  252. long errorCode; /**< the error code returned */
  253. const char *errorText; /**< a textual description of the error if available, otherwise a zero-length string */
  254. }
  255. PaHostErrorInfo;
  256. /** Return information about the last host error encountered. The error
  257. information returned by Pa_GetLastHostErrorInfo() will never be modified
  258. asyncronously by errors occurring in other PortAudio owned threads
  259. (such as the thread that manages the stream callback.)
  260. This function is provided as a last resort, primarily to enhance debugging
  261. by providing clients with access to all available error information.
  262. @return A pointer to an immutable structure constaining information about
  263. the host error. The values in this structure will only be valid if a
  264. PortAudio function has previously returned the paUnanticipatedHostError
  265. error code.
  266. */
  267. const PaHostErrorInfo* Pa_GetLastHostErrorInfo( void );
  268. /* Device enumeration and capabilities */
  269. /** Retrieve the number of available devices. The number of available devices
  270. may be zero.
  271. @return A non-negative value indicating the number of available devices or,
  272. a PaErrorCode (which are always negative) if PortAudio is not initialized
  273. or an error is encountered.
  274. */
  275. PaDeviceIndex Pa_GetDeviceCount( void );
  276. /** Retrieve the index of the default input device. The result can be
  277. used in the inputDevice parameter to Pa_OpenStream().
  278. @return The default input device index for the default host API, or paNoDevice
  279. if no default input device is available or an error was encountered.
  280. */
  281. PaDeviceIndex Pa_GetDefaultInputDevice( void );
  282. /** Retrieve the index of the default output device. The result can be
  283. used in the outputDevice parameter to Pa_OpenStream().
  284. @return The default output device index for the defualt host API, or paNoDevice
  285. if no default output device is available or an error was encountered.
  286. @note
  287. On the PC, the user can specify a default device by
  288. setting an environment variable. For example, to use device #1.
  289. <pre>
  290. set PA_RECOMMENDED_OUTPUT_DEVICE=1
  291. </pre>
  292. The user should first determine the available device ids by using
  293. the supplied application "pa_devs".
  294. */
  295. PaDeviceIndex Pa_GetDefaultOutputDevice( void );
  296. /** The type used to represent monotonic time in seconds that can be used
  297. for syncronisation. The type is used for the outTime argument to the
  298. PaStreamCallback and as the result of Pa_GetStreamTime().
  299. @see PaStreamCallback, Pa_GetStreamTime
  300. */
  301. typedef double PaTime;
  302. /** A type used to specify one or more sample formats. Each value indicates
  303. a possible format for sound data passed to and from the stream callback,
  304. Pa_ReadStream and Pa_WriteStream.
  305. The standard formats paFloat32, paInt16, paInt32, paInt24, paInt8
  306. and aUInt8 are usually implemented by all implementations.
  307. The floating point representation (paFloat32) uses +1.0 and -1.0 as the
  308. maximum and minimum respectively.
  309. paUInt8 is an unsigned 8 bit format where 128 is considered "ground"
  310. The paNonInterleaved flag indicates that a multichannel buffer is passed
  311. as a set of non-interleaved pointers.
  312. @see Pa_OpenStream, Pa_OpenDefaultStream, PaDeviceInfo
  313. @see paFloat32, paInt16, paInt32, paInt24, paInt8
  314. @see paUInt8, paCustomFormat, paNonInterleaved
  315. */
  316. typedef unsigned long PaSampleFormat;
  317. #define paFloat32 ((PaSampleFormat) 0x00000001) /**< @see PaSampleFormat */
  318. #define paInt32 ((PaSampleFormat) 0x00000002) /**< @see PaSampleFormat */
  319. #define paInt24 ((PaSampleFormat) 0x00000004) /**< Packed 24 bit format. @see PaSampleFormat */
  320. #define paInt16 ((PaSampleFormat) 0x00000008) /**< @see PaSampleFormat */
  321. #define paInt8 ((PaSampleFormat) 0x00000010) /**< @see PaSampleFormat */
  322. #define paUInt8 ((PaSampleFormat) 0x00000020) /**< @see PaSampleFormat */
  323. #define paCustomFormat ((PaSampleFormat) 0x00010000)/**< @see PaSampleFormat */
  324. #define paNonInterleaved ((PaSampleFormat) 0x80000000)
  325. /** A structure providing information and capabilities of PortAudio devices.
  326. Devices may support input, output or both input and output.
  327. */
  328. typedef struct PaDeviceInfo {
  329. int structVersion; /* this is struct version 2 */
  330. const char *name;
  331. PaHostApiIndex hostApi; /* note this is a host API index, not a type id*/
  332. int maxInputChannels;
  333. int maxOutputChannels;
  334. /* Default latency values for interactive performance. */
  335. PaTime defaultLowInputLatency;
  336. PaTime defaultLowOutputLatency;
  337. /* Default latency values for robust non-interactive applications (eg. playing sound files). */
  338. PaTime defaultHighInputLatency;
  339. PaTime defaultHighOutputLatency;
  340. double defaultSampleRate;
  341. }
  342. PaDeviceInfo;
  343. /** Retrieve a pointer to a PaDeviceInfo structure containing information
  344. about the specified device.
  345. @return A pointer to an immutable PaDeviceInfo structure. If the device
  346. parameter is out of range the function returns NULL.
  347. @param device A valid device index in the range 0 to (Pa_GetDeviceCount()-1)
  348. @note PortAudio manages the memory referenced by the returned pointer,
  349. the client must not manipulate or free the memory. The pointer is only
  350. guaranteed to be valid between calls to Pa_Initialize() and Pa_Terminate().
  351. @see PaDeviceInfo, PaDeviceIndex
  352. */
  353. const PaDeviceInfo* Pa_GetDeviceInfo( PaDeviceIndex device );
  354. /** Parameters for one direction (input or output) of a stream.
  355. */
  356. typedef struct PaStreamParameters {
  357. /** A valid device index in the range 0 to (Pa_GetDeviceCount()-1)
  358. specifying the device to be used or the special constant
  359. paUseHostApiSpecificDeviceSpecification which indicates that the actual
  360. device(s) to use are specified in hostApiSpecificStreamInfo.
  361. This field must not be set to paNoDevice.
  362. */
  363. PaDeviceIndex device;
  364. /** The number of channels of sound to be delivered to the
  365. stream callback or accessed by Pa_ReadStream() or Pa_WriteStream().
  366. It can range from 1 to the value of maxInputChannels in the
  367. PaDeviceInfo record for the device specified by the device parameter.
  368. */
  369. int channelCount;
  370. /** The sample format of the buffer provided to the stream callback,
  371. a_ReadStream() or Pa_WriteStream(). It may be any of the formats described
  372. by the PaSampleFormat enumeration.
  373. */
  374. PaSampleFormat sampleFormat;
  375. /** The desired latency in seconds. Where practical, implementations should
  376. configure their latency based on these parameters, otherwise they may
  377. choose the closest viable latency instead. Unless the suggested latency
  378. is greater than the absolute upper limit for the device implementations
  379. should round the suggestedLatency up to the next practial value - ie to
  380. provide an equal or higher latency than suggestedLatency wherever possibe.
  381. Actual latency values for an open stream may be retrieved using the
  382. inputLatency and outputLatency fields of the PaStreamInfo structure
  383. returned by Pa_GetStreamInfo().
  384. @see default*Latency in PaDeviceInfo, *Latency in PaStreamInfo
  385. */
  386. PaTime suggestedLatency;
  387. /** An optional pointer to a host api specific data structure
  388. containing additional information for device setup and/or stream processing.
  389. hostApiSpecificStreamInfo is never required for correct operation,
  390. if not used it should be set to NULL.
  391. */
  392. void *hostApiSpecificStreamInfo;
  393. }
  394. PaStreamParameters;
  395. /** Return code for Pa_IsFormatSupported indicating success. */
  396. #define paFormatIsSupported (0)
  397. /** Determine whether it would be possible to open a stream with the specified
  398. parameters.
  399. @param inputParameters A structure that describes the input parameters used to
  400. open a stream. The suggestedLatency field is ignored. See PaStreamParameters
  401. for a description of these parameters. inputParameters must be NULL for
  402. output-only streams.
  403. @param outputParameters A structure that describes the output parameters used
  404. to open a stream. The suggestedLatency field is ignored. See PaStreamParameters
  405. for a description of these parameters. outputParameters must be NULL for
  406. input-only streams.
  407. @param sampleRate The required sampleRate. For full-duplex streams it is the
  408. sample rate for both input and output
  409. @return Returns 0 if the format is supported, and an error code indicating why
  410. the format is not supported otherwise. The constant paFormatIsSupported is
  411. provided to compare with the return value for success.
  412. @see paFormatIsSupported, PaStreamParameters
  413. */
  414. PaError Pa_IsFormatSupported( const PaStreamParameters *inputParameters,
  415. const PaStreamParameters *outputParameters,
  416. double sampleRate );
  417. /* Streaming types and functions */
  418. /**
  419. A single PaStream can provide multiple channels of real-time
  420. streaming audio input and output to a client application. A stream
  421. provides access to audio hardware represented by one or more
  422. PaDevices. Depending on the underlying Host API, it may be possible
  423. to open multiple streams using the same device, however this behavior
  424. is implementation defined. Portable applications should assume that
  425. a PaDevice may be simultaneously used by at most one PaStream.
  426. Pointers to PaStream objects are passed between PortAudio functions that
  427. operate on streams.
  428. @see Pa_OpenStream, Pa_OpenDefaultStream, Pa_OpenDefaultStream, Pa_CloseStream,
  429. Pa_StartStream, Pa_StopStream, Pa_AbortStream, Pa_IsStreamActive,
  430. Pa_GetStreamTime, Pa_GetStreamCpuLoad
  431. */
  432. typedef void PaStream;
  433. /** Can be passed as the framesPerBuffer parameter to Pa_OpenStream()
  434. or Pa_OpenDefaultStream() to indicate that the stream callback will
  435. accept buffers of any size.
  436. */
  437. #define paFramesPerBufferUnspecified (0)
  438. /** Flags used to control the behavior of a stream. They are passed as
  439. parameters to Pa_OpenStream or Pa_OpenDefaultStream. Multiple flags may be
  440. ORed together.
  441. @see Pa_OpenStream, Pa_OpenDefaultStream
  442. @see paNoFlag, paClipOff, paDitherOff, paNeverDropInput,
  443. paPrimeOutputBuffersUsingStreamCallback, paPlatformSpecificFlags
  444. */
  445. typedef unsigned long PaStreamFlags;
  446. /** @see PaStreamFlags */
  447. #define paNoFlag ((PaStreamFlags) 0)
  448. /** Disable default clipping of out of range samples.
  449. @see PaStreamFlags
  450. */
  451. #define paClipOff ((PaStreamFlags) 0x00000001)
  452. /** Disable default dithering.
  453. @see PaStreamFlags
  454. */
  455. #define paDitherOff ((PaStreamFlags) 0x00000002)
  456. /** Flag requests that where possible a full duplex stream will not discard
  457. overflowed input samples without calling the stream callback. This flag is
  458. only valid for full duplex callback streams and only when used in combination
  459. with the paFramesPerBufferUnspecified (0) framesPerBuffer parameter. Using
  460. this flag incorrectly results in a paInvalidFlag error being returned from
  461. Pa_OpenStream and Pa_OpenDefaultStream.
  462. @see PaStreamFlags, paFramesPerBufferUnspecified
  463. */
  464. #define paNeverDropInput ((PaStreamFlags) 0x00000004)
  465. /** Call the stream callback to fill initial output buffers, rather than the
  466. default behavior of priming the buffers with zeros (silence). This flag has
  467. no effect for input-only and blocking read/write streams.
  468. @see PaStreamFlags
  469. */
  470. #define paPrimeOutputBuffersUsingStreamCallback ((PaStreamFlags) 0x00000008)
  471. /** A mask specifying the platform specific bits.
  472. @see PaStreamFlags
  473. */
  474. #define paPlatformSpecificFlags ((PaStreamFlags)0xFFFF0000)
  475. /**
  476. Timing information for the buffers passed to the stream callback.
  477. */
  478. typedef struct PaStreamCallbackTimeInfo {
  479. PaTime inputBufferAdcTime;
  480. PaTime currentTime;
  481. PaTime outputBufferDacTime;
  482. }
  483. PaStreamCallbackTimeInfo;
  484. /**
  485. Flag bit constants for the statusFlags to PaStreamCallback.
  486. @see paInputUnderflow, paInputOverflow, paOutputUnderflow, paOutputOverflow,
  487. paPrimingOutput
  488. */
  489. typedef unsigned long PaStreamCallbackFlags;
  490. /** In a stream opened with paFramesPerBufferUnspecified, indicates that
  491. input data is all silence (zeros) because no real data is available. In a
  492. stream opened without paFramesPerBufferUnspecified, it indicates that one or
  493. more zero samples have been inserted into the input buffer to compensate
  494. for an input underflow.
  495. @see PaStreamCallbackFlags
  496. */
  497. #define paInputUnderflow ((PaStreamCallbackFlags) 0x00000001)
  498. /** In a stream opened with paFramesPerBufferUnspecified, indicates that data
  499. prior to the first sample of the input buffer was discarded due to an
  500. overflow, possibly because the stream callback is using too much CPU time.
  501. Otherwise indicates that data prior to one or more samples in the
  502. input buffer was discarded.
  503. @see PaStreamCallbackFlags
  504. */
  505. #define paInputOverflow ((PaStreamCallbackFlags) 0x00000002)
  506. /** Indicates that output data (or a gap) was inserted, possibly because the
  507. stream callback is using too much CPU time.
  508. @see PaStreamCallbackFlags
  509. */
  510. #define paOutputUnderflow ((PaStreamCallbackFlags) 0x00000004)
  511. /** Indicates that output data will be discarded because no room is available.
  512. @see PaStreamCallbackFlags
  513. */
  514. #define paOutputOverflow ((PaStreamCallbackFlags) 0x00000008)
  515. /** Some of all of the output data will be used to prime the stream, input
  516. data may be zero.
  517. @see PaStreamCallbackFlags
  518. */
  519. #define paPrimingOutput ((PaStreamCallbackFlags) 0x00000010)
  520. /**
  521. Allowable return values for the PaStreamCallback.
  522. @see PaStreamCallback
  523. */
  524. typedef enum PaStreamCallbackResult
  525. {
  526. paContinue = 0,
  527. paComplete = 1,
  528. paAbort = 2
  529. } PaStreamCallbackResult;
  530. /**
  531. Functions of type PaStreamCallback are implemented by PortAudio clients.
  532. They consume, process or generate audio in response to requests from an
  533. active PortAudio stream.
  534. @param input and @param output are arrays of interleaved samples,
  535. the format, packing and number of channels used by the buffers are
  536. determined by parameters to Pa_OpenStream().
  537. @param frameCount The number of sample frames to be processed by
  538. the stream callback.
  539. @param timeInfo The time in seconds when the first sample of the input
  540. buffer was received at the audio input, the time in seconds when the first
  541. sample of the output buffer will begin being played at the audio output, and
  542. the time in seconds when the stream callback was called.
  543. See also Pa_GetStreamTime()
  544. @param statusFlags Flags indicating whether input and/or output buffers
  545. have been inserted or will be dropped to overcome underflow or overflow
  546. conditions.
  547. @param userData The value of a user supplied pointer passed to
  548. Pa_OpenStream() intended for storing synthesis data etc.
  549. @return
  550. The stream callback should return one of the values in the
  551. PaStreamCallbackResult enumeration. To ensure that the callback continues
  552. to be called, it should return paContinue (0). Either paComplete or paAbort
  553. can be returned to finish stream processing, after either of these values is
  554. returned the callback will not be called again. If paAbort is returned the
  555. stream will finish as soon as possible. If paComplete is returned, the stream
  556. will continue until all buffers generated by the callback have been played.
  557. This may be useful in applications such as soundfile players where a specific
  558. duration of output is required. However, it is not necessary to utilise this
  559. mechanism as Pa_StopStream(), Pa_AbortStream() or Pa_CloseStream() can also
  560. be used to stop the stream. The callback must always fill the entire output
  561. buffer irrespective of its return value.
  562. @see Pa_OpenStream, Pa_OpenDefaultStream
  563. @note With the exception of Pa_GetStreamCpuLoad() it is not permissable to call
  564. PortAudio API functions from within the stream callback.
  565. */
  566. typedef int PaStreamCallback(
  567. const void *input, void *output,
  568. unsigned long frameCount,
  569. const PaStreamCallbackTimeInfo* timeInfo,
  570. PaStreamCallbackFlags statusFlags,
  571. void *userData );
  572. /** Opens a stream for either input, output or both.
  573. @param stream The address of a PaStream pointer which will receive
  574. a pointer to the newly opened stream.
  575. @param inputParameters A structure that describes the input parameters used by
  576. the opened stream. See PaStreamParameters for a description of these parameters.
  577. inputParameters must be NULL for output-only streams.
  578. @param outputParameters A structure that describes the output parameters used by
  579. the opened stream. See PaStreamParameters for a description of these parameters.
  580. outputParameters must be NULL for input-only streams.
  581. @param sampleRate The desired sampleRate. For full-duplex streams it is the
  582. sample rate for both input and output
  583. @param framesPerBuffer The number of frames passed to the stream callback
  584. function, or the preferred block granularity for a blocking read/write stream.
  585. The special value paFramesPerBufferUnspecified (0) may be used to request that
  586. the stream callback will recieve an optimal (and possibly varying) number of
  587. frames based on host requirements and the requested latency settings.
  588. Note: With some host APIs, the use of non-zero framesPerBuffer for a callback
  589. stream may introduce an additional layer of buffering which could introduce
  590. additional latency. PortAudio guarantees that the additional latency
  591. will be kept to the theoretical minimum however, it is strongly recommended
  592. that a non-zero framesPerBuffer value only be used when your algorithm
  593. requires a fixed number of frames per stream callback.
  594. @param streamFlags Flags which modify the behaviour of the streaming process.
  595. This parameter may contain a combination of flags ORed together. Some flags may
  596. only be relevant to certain buffer formats.
  597. @param streamCallback A pointer to a client supplied function that is responsible
  598. for processing and filling input and output buffers. If this parameter is NULL
  599. the stream will be opened in 'blocking read/write' mode. In blocking mode,
  600. the client can receive sample data using Pa_ReadStream and write sample data
  601. using Pa_WriteStream, the number of samples that may be read or written
  602. without blocking is returned by Pa_GetStreamReadAvailable and
  603. Pa_GetStreamWriteAvailable respectively.
  604. @param userData A client supplied pointer which is passed to the stream callback
  605. function. It could for example, contain a pointer to instance data necessary
  606. for processing the audio buffers. This parameter is ignored if streamCallback
  607. is NULL.
  608. @return
  609. Upon success Pa_OpenStream() returns paNoError and places a pointer to a
  610. valid PaStream in the stream argument. The stream is inactive (stopped).
  611. If a call to Pa_OpenStream() fails, a non-zero error code is returned (see
  612. PaError for possible error codes) and the value of stream is invalid.
  613. @see PaStreamParameters, PaStreamCallback, Pa_ReadStream, Pa_WriteStream,
  614. Pa_GetStreamReadAvailable, Pa_GetStreamWriteAvailable
  615. */
  616. PaError Pa_OpenStream( PaStream** stream,
  617. const PaStreamParameters *inputParameters,
  618. const PaStreamParameters *outputParameters,
  619. double sampleRate,
  620. unsigned long framesPerBuffer,
  621. PaStreamFlags streamFlags,
  622. PaStreamCallback *streamCallback,
  623. void *userData );
  624. /** A simplified version of Pa_OpenStream() that opens the default input
  625. and/or output devices.
  626. @param stream The address of a PaStream pointer which will receive
  627. a pointer to the newly opened stream.
  628. @param numInputChannels The number of channels of sound that will be supplied
  629. to the stream callback or returned by Pa_ReadStream. It can range from 1 to
  630. the value of maxInputChannels in the PaDeviceInfo record for the default input
  631. device. If 0 the stream is opened as an output-only stream.
  632. @param numOutputChannels The number of channels of sound to be delivered to the
  633. stream callback or passed to Pa_WriteStream. It can range from 1 to the value
  634. of maxOutputChannels in the PaDeviceInfo record for the default output dvice.
  635. If 0 the stream is opened as an output-only stream.
  636. @param sampleFormat The sample format of both the input and output buffers
  637. provided to the callback or passed to and from Pa_ReadStream and Pa_WriteStream.
  638. sampleFormat may be any of the formats described by the PaSampleFormat
  639. enumeration.
  640. @param sampleRate Same as Pa_OpenStream parameter of the same name.
  641. @param framesPerBuffer Same as Pa_OpenStream parameter of the same name.
  642. @param streamCallback Same as Pa_OpenStream parameter of the same name.
  643. @param userData Same as Pa_OpenStream parameter of the same name.
  644. @return As for Pa_OpenStream
  645. @see Pa_OpenStream, PaStreamCallback
  646. */
  647. PaError Pa_OpenDefaultStream( PaStream** stream,
  648. int numInputChannels,
  649. int numOutputChannels,
  650. PaSampleFormat sampleFormat,
  651. double sampleRate,
  652. unsigned long framesPerBuffer,
  653. PaStreamCallback *streamCallback,
  654. void *userData );
  655. /** Closes an audio stream. If the audio stream is active it
  656. discards any pending buffers as if Pa_AbortStream() had been called.
  657. */
  658. PaError Pa_CloseStream( PaStream *stream );
  659. /** Functions of type PaStreamFinishedCallback are implemented by PortAudio
  660. clients. They can be registered with a stream using the Pa_SetStreamFinishedCallback
  661. function. Once registered they are called when the stream becomes inactive
  662. (ie once a call to Pa_StopStream() will not block).
  663. A stream will become inactive after the stream callback returns non-zero,
  664. or when Pa_StopStream or Pa_AbortStream is called. For a stream providing audio
  665. output, if the stream callback returns paComplete, or Pa_StopStream is called,
  666. the stream finished callback will not be called until all generated sample data
  667. has been played.
  668. @param userData The userData parameter supplied to Pa_OpenStream()
  669. @see Pa_SetStreamFinishedCallback
  670. */
  671. typedef void PaStreamFinishedCallback( void *userData );
  672. /** Register a stream finished callback function which will be called when the
  673. stream becomes inactive. See the description of PaStreamFinishedCallback for
  674. further details about when the callback will be called.
  675. @param stream a pointer to a PaStream that is in the stopped state - if the
  676. stream is not stopped, the stream's finished callback will remain unchanged
  677. and an error code will be returned.
  678. @param streamFinishedCallback a pointer to a function with the same signature
  679. as PaStreamFinishedCallback, that will be called when the stream becomes
  680. inactive. Passing NULL for this parameter will un-register a previously
  681. registered stream finished callback function.
  682. @return on success returns paNoError, otherwise an error code indicating the cause
  683. of the error.
  684. @see PaStreamFinishedCallback
  685. */
  686. PaError Pa_SetStreamFinishedCallback( PaStream *stream, PaStreamFinishedCallback* streamFinishedCallback );
  687. /** Commences audio processing.
  688. */
  689. PaError Pa_StartStream( PaStream *stream );
  690. /** Terminates audio processing. It waits until all pending
  691. audio buffers have been played before it returns.
  692. */
  693. PaError Pa_StopStream( PaStream *stream );
  694. /** Terminates audio processing immediately without waiting for pending
  695. buffers to complete.
  696. */
  697. PaError Pa_AbortStream( PaStream *stream );
  698. /** Determine whether the stream is stopped.
  699. A stream is considered to be stopped prior to a successful call to
  700. Pa_StartStream and after a successful call to Pa_StopStream or Pa_AbortStream.
  701. If a stream callback returns a value other than paContinue the stream is NOT
  702. considered to be stopped.
  703. @return Returns one (1) when the stream is stopped, zero (0) when
  704. the stream is running or, a PaErrorCode (which are always negative) if
  705. PortAudio is not initialized or an error is encountered.
  706. @see Pa_StopStream, Pa_AbortStream, Pa_IsStreamActive
  707. */
  708. PaError Pa_IsStreamStopped( PaStream *stream );
  709. /** Determine whether the stream is active.
  710. A stream is active after a successful call to Pa_StartStream(), until it
  711. becomes inactive either as a result of a call to Pa_StopStream() or
  712. Pa_AbortStream(), or as a result of a return value other than paContinue from
  713. the stream callback. In the latter case, the stream is considered inactive
  714. after the last buffer has finished playing.
  715. @return Returns one (1) when the stream is active (ie playing or recording
  716. audio), zero (0) when not playing or, a PaErrorCode (which are always negative)
  717. if PortAudio is not initialized or an error is encountered.
  718. @see Pa_StopStream, Pa_AbortStream, Pa_IsStreamStopped
  719. */
  720. PaError Pa_IsStreamActive( PaStream *stream );
  721. /** A structure containing unchanging information about an open stream.
  722. @see Pa_GetStreamInfo
  723. */
  724. typedef struct PaStreamInfo {
  725. /** this is struct version 1 */
  726. int structVersion;
  727. /** The input latency of the stream in seconds. This value provides the most
  728. accurate estimate of input latency available to the implementation. It may
  729. differ significantly from the suggestedLatency value passed to Pa_OpenStream().
  730. The value of this field will be zero (0.) for output-only streams.
  731. @see PaTime
  732. */
  733. PaTime inputLatency;
  734. /** The output latency of the stream in seconds. This value provides the most
  735. accurate estimate of output latency available to the implementation. It may
  736. differ significantly from the suggestedLatency value passed to Pa_OpenStream().
  737. The value of this field will be zero (0.) for input-only streams.
  738. @see PaTime
  739. */
  740. PaTime outputLatency;
  741. /** The sample rate of the stream in Hertz (samples per second). In cases
  742. where the hardware sample rate is inaccurate and PortAudio is aware of it,
  743. the value of this field may be different from the sampleRate parameter
  744. passed to Pa_OpenStream(). If information about the actual hardware sample
  745. rate is not available, this field will have the same value as the sampleRate
  746. parameter passed to Pa_OpenStream().
  747. */
  748. double sampleRate;
  749. }
  750. PaStreamInfo;
  751. /** Retrieve a pointer to a PaStreamInfo structure containing information
  752. about the specified stream.
  753. @return A pointer to an immutable PaStreamInfo structure. If the stream
  754. parameter invalid, or an error is encountered, the function returns NULL.
  755. @param stream A pointer to an open stream previously created with Pa_OpenStream.
  756. @note PortAudio manages the memory referenced by the returned pointer,
  757. the client must not manipulate or free the memory. The pointer is only
  758. guaranteed to be valid until the specified stream is closed.
  759. @see PaStreamInfo
  760. */
  761. const PaStreamInfo* Pa_GetStreamInfo( PaStream *stream );
  762. /** Determine the current time for the stream according to the same clock used
  763. to generate buffer timestamps. This time may be used for syncronising other
  764. events to the audio stream, for example synchronizing audio to MIDI.
  765. @return The stream's current time in seconds, or 0 if an error occurred.
  766. @see PaTime, PaStreamCallback
  767. */
  768. PaTime Pa_GetStreamTime( PaStream *stream );
  769. /** Retrieve CPU usage information for the specified stream.
  770. The "CPU Load" is a fraction of total CPU time consumed by a callback stream's
  771. audio processing routines including, but not limited to the client supplied
  772. stream callback. This function does not work with blocking read/write streams.
  773. This function may be called from the stream callback function or the
  774. application.
  775. @return
  776. A floating point value, typically between 0.0 and 1.0, where 1.0 indicates
  777. that the stream callback is consuming the maximum number of CPU cycles possible
  778. to maintain real-time operation. A value of 0.5 would imply that PortAudio and
  779. the stream callback was consuming roughly 50% of the available CPU time. The
  780. return value may exceed 1.0. A value of 0.0 will always be returned for a
  781. blocking read/write stream, or if an error occurrs.
  782. */
  783. double Pa_GetStreamCpuLoad( PaStream* stream );
  784. /** Read samples from an input stream. The function doesn't return until
  785. the entire buffer has been filled - this may involve waiting for the operating
  786. system to supply the data.
  787. @param stream A pointer to an open stream previously created with Pa_OpenStream.
  788. @param buffer A pointer to a buffer of sample frames. The buffer contains
  789. samples in the format specified by the inputParameters->sampleFormat field
  790. used to open the stream, and the number of channels specified by
  791. inputParameters->numChannels. If non-interleaved samples were requested,
  792. buffer is a pointer to the first element of an array of non-interleaved
  793. buffer pointers, one for each channel.
  794. @param frames The number of frames to be read into buffer. This parameter
  795. is not constrained to a specific range, however high performance applications
  796. will want to match this parameter to the framesPerBuffer parameter used
  797. when opening the stream.
  798. @return On success PaNoError will be returned, or PaInputOverflowed if input
  799. data was discarded by PortAudio after the previous call and before this call.
  800. */
  801. PaError Pa_ReadStream( PaStream* stream,
  802. void *buffer,
  803. unsigned long frames );
  804. /** Write samples to an output stream. This function doesn't return until the
  805. entire buffer has been consumed - this may involve waiting for the operating
  806. system to consume the data.
  807. @param stream A pointer to an open stream previously created with Pa_OpenStream.
  808. @param buffer A pointer to a buffer of sample frames. The buffer contains
  809. samples in the format specified by the outputParameters->sampleFormat field
  810. used to open the stream, and the number of channels specified by
  811. outputParameters->numChannels. If non-interleaved samples were requested,
  812. buffer is a pointer to the first element of an array of non-interleaved
  813. buffer pointers, one for each channel.
  814. @param frames The number of frames to be written from buffer. This parameter
  815. is not constrained to a specific range, however high performance applications
  816. will want to match this parameter to the framesPerBuffer parameter used
  817. when opening the stream.
  818. @return On success PaNoError will be returned, or paOutputUnderflowed if
  819. additional output data was inserted after the previous call and before this
  820. call.
  821. */
  822. PaError Pa_WriteStream( PaStream* stream,
  823. const void *buffer,
  824. unsigned long frames );
  825. /** Retrieve the number of frames that can be read from the stream without
  826. waiting.
  827. @return Returns a non-negative value representing the maximum number of frames
  828. that can be read from the stream without blocking or busy waiting or, a
  829. PaErrorCode (which are always negative) if PortAudio is not initialized or an
  830. error is encountered.
  831. */
  832. signed long Pa_GetStreamReadAvailable( PaStream* stream );
  833. /** Retrieve the number of frames that can be written to the stream without
  834. waiting.
  835. @return Returns a non-negative value representing the maximum number of frames
  836. that can be written to the stream without blocking or busy waiting or, a
  837. PaErrorCode (which are always negative) if PortAudio is not initialized or an
  838. error is encountered.
  839. */
  840. signed long Pa_GetStreamWriteAvailable( PaStream* stream );
  841. /* Miscellaneous utilities */
  842. /** Retrieve the size of a given sample format in bytes.
  843. @return The size in bytes of a single sample in the specified format,
  844. or paSampleFormatNotSupported if the format is not supported.
  845. */
  846. PaError Pa_GetSampleSize( PaSampleFormat format );
  847. /** Put the caller to sleep for at least 'msec' milliseconds. This function is
  848. provided only as a convenience for authors of portable code (such as the tests
  849. and examples in the PortAudio distribution.)
  850. The function may sleep longer than requested so don't rely on this for accurate
  851. musical timing.
  852. */
  853. void Pa_Sleep( long msec );
  854. #ifdef __cplusplus
  855. }
  856. #endif /* __cplusplus */
  857. #endif /* PORTAUDIO_H */