Debian Codegraph @

src/milkyplay/drivers/generic/portaudio/portaudio.h

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