2
0

portaudio.h 45 KB

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