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