]> git.alsa-project.org Git - alsa-lib.git/blob - src/pcm/pcm.c
11b15100cd796612e2c482aa3d897e24f49c66ee
[alsa-lib.git] / src / pcm / pcm.c
1 /**
2  * \file pcm/pcm.c
3  * \ingroup PCM
4  * \brief PCM Interface
5  * \author Jaroslav Kysela <perex@perex.cz>
6  * \author Abramo Bagnara <abramo@alsa-project.org>
7  * \date 2000-2001
8  *
9  * PCM Interface is designed to write or read digital audio frames. A
10  * frame is the data unit converted into/from sound in one time unit
11  * (1/rate seconds), by example if you set your playback PCM rate to
12  * 44100 you'll hear 44100 frames per second. The size in bytes of a
13  * frame may be obtained from bits needed to store a sample and
14  * channels count.
15  *
16  * See the \ref pcm page for more details.
17  */
18 /*
19  *  PCM Interface - main file
20  *  Copyright (c) 1998 by Jaroslav Kysela <perex@perex.cz>
21  *  Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
22  *
23  *   This library is free software; you can redistribute it and/or modify
24  *   it under the terms of the GNU Lesser General Public License as
25  *   published by the Free Software Foundation; either version 2.1 of
26  *   the License, or (at your option) any later version.
27  *
28  *   This program is distributed in the hope that it will be useful,
29  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
30  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31  *   GNU Lesser General Public License for more details.
32  *
33  *   You should have received a copy of the GNU Lesser General Public
34  *   License along with this library; if not, write to the Free Software
35  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
36  *
37  */
38
39 /*! \page pcm PCM (digital audio) interface
40
41 <P>Although abbreviation PCM stands for Pulse Code Modulation, we are
42 understanding it as general digital audio processing with volume samples
43 generated in continuous time periods.</P>
44
45 <P>The analog signal is recorded via analog to digital converters (ADC).
46 The digital value (de-facto a volume at a specific time) obtained
47 from ADC can be further processed. The following picture shows a perfect
48 sinus waveform:</P>
49
50 <BR>
51 \image html wave1.gif
52
53 <P>Next image shows digitized representation:</P>
54
55 <BR>
56 \image html wave2.gif
57
58 <P>As you may see, the quality of digital audio signal depends on the time
59 (recording rate) and voltage resolution (usually in an linear integer
60 representation with basic unit one bit).</P>
61
62 <P>The stored digital signal can be converted back to voltage (analog)
63 representation via digital to analog converters (DAC).</P>
64
65 <P>One digital value is called sample. More samples are collected to frames
66 (frame is terminology for ALSA) depending on count of converters used at one
67 specific time. One frame might contain one sample (when only one converter is
68 used - mono) or more samples (for example: stereo has signals from two converters
69 recorded at same time). Digital audio stream contains collection of frames
70 recorded at boundaries of continuous time periods.</P>
71
72 \section pcm_general_overview General overview
73
74 ALSA uses the ring buffer to store outgoing (playback) and incoming (capture,
75 record) samples. There are two pointers being maintained to allow
76 a precise communication between application and device pointing to current
77 processed sample by hardware and last processed sample by application.
78 The modern audio chips allow to program the transfer time periods.
79 It means that the stream of samples is divided to small chunks. Device
80 acknowledges to application when the transfer of a chunk is complete.
81
82 \section pcm_transfer Transfer methods in UNIX environments
83
84 In the UNIX environment, data chunk acknowledges are received via standard I/O
85 calls or event waiting routines (poll or select function). To accomplish
86 this list, the asynchronous notification of acknowledges should be listed
87 here. The ALSA implementation for these methods is described in
88 the \ref alsa_transfers section.
89
90 \subsection pcm_transfer_io Standard I/O transfers
91
92 The standard I/O transfers are using the read (see 'man 2 read') and write
93 (see 'man 2 write') C functions. There are two basic behaviours of these
94 functions - blocked and non-blocked (see the O_NONBLOCK flag for the
95 standard C open function - see 'man 2 open'). In non-blocked behaviour,
96 these I/O functions never stops, they return -EAGAIN error code, when no
97 data can be transferred (the ring buffer is full in our case). In blocked
98 behaviour, these I/O functions stop and wait until there is a room in the
99 ring buffer (playback) or until there are new samples (capture). The ALSA
100 implementation can be found in the \ref alsa_pcm_rw section.
101
102 \subsection pcm_transfer_event Event waiting routines
103
104 The poll or select functions (see 'man 2 poll' or 'man 2 select' for further
105 details) allows to receive requests/events from the device while
106 an application is waiting on events from other sources (like keyboard, screen,
107 network etc.), too. \ref snd_pcm_poll_descriptors can be used to get file
108 descriptors to poll or select on (note that wait direction might be different
109 than expected - do not use only returned file descriptors, but handle
110 events member as well - see \ref snd_pcm_poll_descriptors function
111 description for more details and \ref snd_pcm_poll_descriptors_revents for
112 events demangling). The implemented transfer routines can be found in
113 the \ref alsa_transfers section.
114
115 \subsection pcm_transfer_async Asynchronous notification
116
117 ALSA driver and library knows to handle the asynchronous notifications over
118 the SIGIO signal. This signal allows to interrupt application and transfer
119 data in the signal handler. For further details see the sigaction function
120 ('man 2 sigaction'). The section \ref pcm_async describes the ALSA API for
121 this extension. The implemented transfer routines can be found in the
122 \ref alsa_transfers section.
123
124 \section pcm_open_behaviour Blocked and non-blocked open
125
126 The ALSA PCM API uses a different behaviour when the device is opened
127 with blocked or non-blocked mode. The mode can be specified with
128 \a mode argument in #snd_pcm_open() function.
129 The blocked mode is the default (without #SND_PCM_NONBLOCK mode).
130 In this mode, the behaviour is that if the resources have already used
131 with another application, then it blocks the caller, until resources are
132 free. The non-blocked behaviour (with #SND_PCM_NONBLOCK)
133 doesn't block the caller in any way and returns -EBUSY error when the
134 resources are not available. Note that the mode also determines the
135 behaviour of standard I/O calls, returning -EAGAIN when non-blocked mode is
136 used and the ring buffer is full (playback) or empty (capture).
137 The operation mode for I/O calls can be changed later with
138 the #snd_pcm_nonblock() function.
139
140 \section pcm_async Asynchronous mode
141
142 There is also possibility to receive asynchronous notification after
143 specified time periods. You may see the #SND_PCM_ASYNC
144 mode for #snd_pcm_open() function and
145 #snd_async_add_pcm_handler() function for further details.
146
147 \section pcm_handshake Handshake between application and library
148
149 The ALSA PCM API design uses the states to determine the communication
150 phase between application and library. The actual state can be determined
151 using #snd_pcm_state() call. There are these states:
152
153 \par SND_PCM_STATE_OPEN
154 The PCM device is in the open state. After the #snd_pcm_open() open call,
155 the device is in this state. Also, when #snd_pcm_hw_params() call fails,
156 then this state is entered to force application calling
157 #snd_pcm_hw_params() function to set right communication
158 parameters.
159
160 \par SND_PCM_STATE_SETUP
161 The PCM device has accepted communication parameters and it is waiting
162 for #snd_pcm_prepare() call to prepare the hardware for
163 selected operation (playback or capture).
164
165 \par SND_PCM_STATE_PREPARED
166 The PCM device is prepared for operation. Application can use
167 #snd_pcm_start() call, write or read data to start
168 the operation.
169
170 \par SND_PCM_STATE_RUNNING
171 The PCM device has been started and is running. It processes the samples. The stream can
172 be stopped using the #snd_pcm_drop() or
173 #snd_pcm_drain() calls.
174
175 \par SND_PCM_STATE_XRUN
176 The PCM device reached overrun (capture) or underrun (playback).
177 You can use the -EPIPE return code from I/O functions
178 (#snd_pcm_writei(), #snd_pcm_writen(), #snd_pcm_readi(), #snd_pcm_readn())
179 to determine this state without checking
180 the actual state via #snd_pcm_state() call. It is recommended to use
181 the helper function #snd_pcm_recover() to recover from this state, but you can also use #snd_pcm_prepare(),
182 #snd_pcm_drop() or #snd_pcm_drain() calls.
183
184 \par SND_PCM_STATE_DRAINING
185 The device is in this state when application using the capture mode
186 called #snd_pcm_drain() function. Until all data are
187 read from the internal ring buffer using I/O routines
188 (#snd_pcm_readi(), #snd_pcm_readn()),
189 then the device stays in this state.
190
191 \par SND_PCM_STATE_PAUSED
192 The device is in this state when application called
193 the #snd_pcm_pause() function until the pause is released.
194 Not all hardware supports this feature. Application should check the
195 capability with the #snd_pcm_hw_params_can_pause().
196
197 \par SND_PCM_STATE_SUSPENDED
198 The device is in the suspend state provoked with the power management
199 system. The stream can be resumed using #snd_pcm_resume()
200 call, but not all hardware supports this feature. Application should check
201 the capability with the #snd_pcm_hw_params_can_resume().
202 In other case, the calls #snd_pcm_prepare(),
203 #snd_pcm_drop(), #snd_pcm_drain() can be used
204 to leave this state.
205
206 \par SND_PCM_STATE_DISCONNECTED
207 The device is physicaly disconnected. It does not accept any I/O calls in this state.
208
209 \section pcm_formats PCM formats
210
211 The full list of formats present the #snd_pcm_format_t type.
212 The 24-bit linear samples use 32-bit physical space, but the sample is
213 stored in the lower three bytes. Some hardware does not support processing of full
214 range, thus you may get the significant bits for linear samples via
215 #snd_pcm_hw_params_get_sbits() function. The example: ICE1712
216 chips support 32-bit sample processing, but low byte is ignored (playback)
217 or zero (capture). The function snd_pcm_hw_params_get_sbits()
218 returns 24 in this case. The significant bits are related to the usable
219 sample bits (width) not the physical sample space.
220
221 \section alsa_transfers ALSA transfers
222
223 There are two methods to transfer samples in application. The first method
224 is the standard read / write one. The second method, uses the direct audio
225 buffer to communicate with the device while ALSA library manages this space
226 itself. You can find examples of all communication schemes for playback
227 in \link example_test_pcm Sine-wave generator example \endlink. To complete the
228 list, we should note that #snd_pcm_wait() function contains
229 embedded poll waiting implementation.
230
231 \subsection alsa_pcm_rw Read / Write transfer
232
233 There are two versions of read / write routines. The first expects the
234 interleaved samples at input (#SND_PCM_ACCESS_RW_INTERLEAVED access method),
235 and the second one expects non-interleaved (samples in separated buffers -
236 #SND_PCM_ACCESS_RW_NONINTERLEAVED access method) at input. There are these
237 functions for interleaved transfers: #snd_pcm_writei()
238 #snd_pcm_readi(). For non-interleaved transfers, there are
239 these functions: #snd_pcm_writen() and #snd_pcm_readn().
240
241 \subsection alsa_mmap_rw Direct Read / Write transfer (via mmap'ed areas)
242
243 Three kinds of organization of ring buffer memory areas exist in ALSA API.
244 Access #SND_PCM_ACCESS_MMAP_INTERLEAVED has interleaved samples. Access
245 #SND_PCM_ACCESS_MMAP_NONINTERLEAVED expects continous sample areas for
246 one channel. Access #SND_PCM_ACCESS_MMAP_COMPLEX does not fit to interleaved
247 and non-interleaved ring buffer organization.
248
249 There are two functions for this kind of transfer. Application can get an
250 access to memory areas via #snd_pcm_mmap_begin() function.
251 This function returns the areas (single area is equal to a channel)
252 containing the direct pointers to memory and sample position description
253 in #snd_pcm_channel_area_t structure. After application
254 transfers the data in the memory areas, then it must be acknowledged
255 the end of transfer via #snd_pcm_mmap_commit() function
256 to allow the ALSA library update the pointers to ring buffer. This kind of
257 communication is also called "zero-copy", because the device does not require
258 to copy the samples from application to another place in system memory.
259
260 If you like to use the compatibility functions in mmap mode, there are
261 read / write routines equaling to standard read / write transfers. Using
262 these functions discards the benefits of direct access to memory region.
263 See the #snd_pcm_mmap_readi(),
264 #snd_pcm_mmap_writei(), #snd_pcm_mmap_readn()
265 and #snd_pcm_mmap_writen() functions. These functions use
266 #snd_pcm_areas_copy() internally.
267
268 \section pcm_errors Error codes
269
270 \par -EPIPE
271
272 This error means xrun (underrun for playback or overrun for capture).
273 The underrun can happen when an application does not feed new samples
274 in time to alsa-lib (due CPU usage). The overrun can happen when
275 an application does not take new captured samples in time from alsa-lib.
276
277 \par -ESTRPIPE
278
279 This error means that system has suspended drivers. The application
280 should wait in loop when snd_pcm_resume() != -EAGAIN and then
281 call snd_pcm_prepare() when snd_pcm_resume() return an error code.
282 If snd_pcm_resume() does not fail (a zero value is returned), driver
283 supports resume and the snd_pcm_prepare() call can be ommited.
284
285 \par -EBADFD
286
287 This error means that the device is in a bad state. It means that
288 the handshake between application and alsa-lib is corrupted.
289
290 \par -ENOTTY, -ENODEV
291
292 This error can happen when device is physically removed (for example
293 some hotplug devices like USB or PCMCIA, CardBus or ExpressCard
294 can be removed on the fly).
295
296 \par -ENODATA
297
298 This error can happen if the device data transfer is dependent on
299 an external condition and that condition is not met. For example,
300 PCM device for echo reference as described by SND_USE_CASE_MOD_ECHO_REF
301 UCM token, may return -ENODATA if the linked playback stream has not been
302 started.
303
304 There is no defined recovery or event mechanism to signal the data / link
305 availability at the moment. The PCM must be completely restarted until
306 the mechanism is designed. The #snd_pcm_recover() function cannot be
307 used for this.
308
309 \section pcm_params Managing parameters
310
311 The ALSA PCM device uses two groups of PCM related parameters. The hardware
312 parameters contains the stream description like format, rate, count of
313 channels, ring buffer size etc. The software parameters contains the
314 software (driver) related parameters. The communication behaviour can be
315 controlled via these parameters, like automatic start, automatic stop,
316 interrupting (chunk acknowledge) etc. The software parameters can be
317 modified at any time (when valid hardware parameters are set). It includes
318 the running state as well.
319
320 \subsection pcm_hw_params Hardware related parameters
321
322 The ALSA PCM devices use the parameter refining system for hardware
323 parameters - #snd_pcm_hw_params_t. It means, that
324 application choose the full-range of configurations at first and then
325 application sets single parameters until all parameters are elementary
326 (definite).
327
328 \par Access modes
329
330 ALSA knows about five access modes. The first three can be used for direct
331 communication. The access mode #SND_PCM_ACCESS_MMAP_INTERLEAVED
332 determines the direct memory area and interleaved sample organization.
333 Interleaved organization means, that samples from channels are mixed together.
334 The access mode #SND_PCM_ACCESS_MMAP_NONINTERLEAVED
335 determines the direct memory area and non-interleaved sample organization.
336 Each channel has a separate buffer in the case. The complex direct memory
337 organization represents the #SND_PCM_ACCESS_MMAP_COMPLEX
338 access mode. The sample organization does not fit the interleaved or
339 non-interleaved access modes in the case. The last two access modes
340 describes the read / write access methods.
341 The #SND_PCM_ACCESS_RW_INTERLEAVED access represents the read /
342 write interleaved access and the #SND_PCM_ACCESS_RW_NONINTERLEAVED
343 represents the non-interleaved access.
344
345 \par Formats
346
347 The full list of formats is available in #snd_pcm_format_t
348 enumeration.
349
350 \subsection pcm_sw_params Software related parameters
351
352 These parameters - #snd_pcm_sw_params_t can be modified at
353 any time including the running state.
354
355 \par Minimum available count of frames
356
357 This parameter controls the wakeup point. If the count of available frames
358 is equal or greater than this value, then application will be activated.
359
360 \par Timestamp mode
361
362 The timestamp mode specifies, if timestamps are activated. Currently, only
363 #SND_PCM_TSTAMP_NONE and #SND_PCM_TSTAMP_MMAP
364 modes are known. The mmap mode means that timestamp is taken
365 on every period time boundary. Corresponding position in the ring buffer
366 assigned to timestamp can be obtained using #snd_pcm_htimestamp() function.
367
368 \par Transfer align
369
370 The read / write transfers can be aligned to this sample count. The modulo
371 is ignored by device. Usually, this value is set to one (no align).
372
373 \par Start threshold
374
375 The start threshold parameter is used to determine the start point in
376 stream. For playback, if the frame count in the ring buffer is equal or greater
377 than the start threshold parameter and the stream is not running, the stream
378 will be started automatically from the device. For capture, if the application
379 wants to read count of frames equal or greater then the stream will be started.
380 If you want to use explicit start (#snd_pcm_start), you can set this value
381 greater than the ring buffer size (in frames). For that simply using a large
382 constant such as LONG_MAX or the boundary value is not a bad idea.
383
384 \par Stop threshold
385
386 Similarly, the stop threshold parameter is used to automatically stop
387 the running stream, when the available frames crosses this boundary.
388 It means, for playback, the empty samples in ring buffer and for capture,
389 the filled (used) samples in ring buffer.
390
391 \par Silence threshold
392
393 The silence threshold specifies the count of frames before an underrun when the
394 buffer gets filled with frames of silence according to the silence size parameter
395 ahead of the current application pointer for playback. It is usable for applications
396 when an underrun is possible (like tasks depending on network I/O etc.). If
397 application wants to manage the ahead samples itself, the #snd_pcm_rewind() function
398 allows to forget the last samples in the stream.
399
400 \section pcm_status Obtaining stream status
401
402 The stream status is stored in #snd_pcm_status_t structure.
403 These parameters can be obtained: the current stream state -
404 #snd_pcm_status_get_state(), timestamp of trigger -
405 #snd_pcm_status_get_trigger_tstamp(), timestamp of last
406 pointer update #snd_pcm_status_get_tstamp(), delay in frames -
407 #snd_pcm_status_get_delay(), available count in frames -
408 #snd_pcm_status_get_avail(), maximum available frames -
409 #snd_pcm_status_get_avail_max(), ADC over-range count in
410 frames - #snd_pcm_status_get_overrange(). The last two
411 parameters - avail_max and overrange are reset to zero after the status
412 call.
413
414 \subsection pcm_status_fast Obtaining stream state fast and update r/w pointer
415
416 <p>
417 The function #snd_pcm_avail_update() updates the current
418 available count of frames for writing (playback) or filled frames for
419 reading (capture). This call is mandatory for updating actual r/w pointer.
420 Using standalone, it is a light method to obtain current stream position,
421 because it does not require the user <-> kernel context switch, but the value
422 is less accurate, because ring buffer pointers are updated in kernel drivers
423 only when an interrupt occurs. If you want to get accurate stream state,
424 use functions #snd_pcm_avail(), #snd_pcm_delay() or #snd_pcm_avail_delay().
425 </p>
426 <p>
427 The function #snd_pcm_avail() reads the current hardware pointer
428 in the ring buffer from hardware and calls #snd_pcm_avail_update() then.
429 </p>
430 <p>
431 The function #snd_pcm_delay() returns the delay in frames.
432 For playback, it means count of frames in the ring buffer before
433 the next frames will be sent to DAC. For capture, it means count of frames
434 in the ring buffer before the next frames will be captured from ADC. It works
435 only when the stream is in the running or draining (playback only) state.
436 Note that this function does not update the current r/w pointer for applications,
437 so the function #snd_pcm_avail_update() must be called afterwards
438 before any read/write begin+commit operations.
439 </p>
440 <p>
441 The function #snd_pcm_avail_delay() combines #snd_pcm_avail() and
442 #snd_pcm_delay() and returns both values in sync.
443 </p>
444
445 \section pcm_action Managing the stream state
446
447 The following functions directly and indirectly affect the stream state:
448
449 \par snd_pcm_hw_params
450 The #snd_pcm_hw_params() function brings the stream state
451 to #SND_PCM_STATE_SETUP
452 if successfully finishes, otherwise the state #SND_PCM_STATE_OPEN
453 is entered.
454 When it is brought to SETUP state, this function automatically
455 calls #snd_pcm_prepare() function to bring to the PREPARED state
456 as below.
457
458 \par snd_pcm_prepare
459 The #snd_pcm_prepare() function enters from #SND_PCM_STATE_SETUP
460 to the #SND_PCM_STATE_PREPARED after a successful finish.
461
462 \par snd_pcm_start
463 The #snd_pcm_start() function enters
464 the #SND_PCM_STATE_RUNNING after a successful finish.
465
466 \par snd_pcm_drop
467 The #snd_pcm_drop() function enters the
468 #SND_PCM_STATE_SETUP state.
469
470 \par snd_pcm_drain
471 The #snd_pcm_drain() function enters the
472 #SND_PCM_STATE_DRAINING, if
473 the capture device has some samples in the ring buffer otherwise
474 #SND_PCM_STATE_SETUP state is entered.
475
476 \par snd_pcm_pause
477 The #snd_pcm_pause() function enters the
478 #SND_PCM_STATE_PAUSED or #SND_PCM_STATE_RUNNING.
479
480 \par snd_pcm_writei, snd_pcm_writen
481 The #snd_pcm_writei() and #snd_pcm_writen()
482 functions can conditionally start the stream -
483 #SND_PCM_STATE_RUNNING. They depend on the start threshold
484 software parameter.
485
486 \par snd_pcm_readi, snd_pcm_readn
487 The #snd_pcm_readi() and #snd_pcm_readn()
488 functions can conditionally start the stream -
489 #SND_PCM_STATE_RUNNING. They depend on the start threshold
490 software parameter.
491
492 \section pcm_sync Streams synchronization
493
494 There are two functions allowing link multiple streams together. In the
495 case, the linking means that all operations are synchronized. Because the
496 drivers cannot guarantee the synchronization (sample resolution) on hardware
497 lacking this feature, the #snd_pcm_hw_params_get_sync() function
498 returns 16-byte synchronization ID, which is equal
499 for hardware synchronized streams. When the #snd_pcm_link()
500 function is called, all operations managing the stream state for these two
501 streams are joined. The opposite function is #snd_pcm_unlink().
502
503 \section pcm_thread_safety Thread-safety
504
505 When the library is configured with the proper option, some PCM functions
506 (e.g. #snd_pcm_avail_update()) are thread-safe and can be called concurrently
507 from multiple threads.  Meanwhile, some functions (e.g. #snd_pcm_hw_params())
508 aren't thread-safe, and application needs to call them carefully when they
509 are called from multiple threads.  In general, all the functions that are
510 often called during streaming are covered as thread-safe.
511
512 This thread-safe behavior can be disabled also by passing 0 to the environment
513 variable LIBASOUND_THREAD_SAFE, e.g.
514 \code
515 LIBASOUND_THREAD_SAFE=0 aplay foo.wav
516 \endcode
517 for making the debugging easier.
518
519 \section pcm_dev_names PCM naming conventions
520
521 The ALSA library uses a generic string representation for names of devices.
522 The devices might be virtual, physical or a mix of both. The generic string
523 is passed to #snd_pcm_open() or #snd_pcm_open_lconf().
524 It contains two parts: device name and arguments. Devices and arguments are described
525 in configuration files. The usual place for default definitions is at /usr/share/alsa/alsa.conf.
526 For detailed descriptions about integrated PCM plugins look to \ref pcm_plugins.
527
528 \subsection pcm_dev_names_default Default device
529
530 The default device is equal to plug plugin with hw plugin as slave. The defaults are
531 used:
532
533 \code
534 defaults.pcm.card 0
535 defaults.pcm.device 0
536 defaults.pcm.subdevice -1
537 \endcode
538
539 These defaults can be freely overwritten in local configuration files.
540
541 Example:
542
543 \code
544 default
545 \endcode
546
547 \subsection pcm_dev_names_hw HW device
548
549 The hw device description uses the hw plugin. The three arguments (in order: CARD,DEV,SUBDEV)
550 specify card number or identifier, device number and subdevice number (-1 means any).
551
552 Example:
553
554 \code
555 hw
556 hw:0
557 hw:0,0
558 hw:supersonic,1
559 hw:soundwave,1,2
560 hw:DEV=1,CARD=soundwave,SUBDEV=2
561 \endcode
562
563 \subsection pcm_dev_names_plughw Plug->HW device
564
565 The plughw device description uses the plug plugin and hw plugin as slave. The arguments
566 are same as for hw device.
567
568 Example:
569
570 \code
571 plughw
572 plughw:0
573 plughw:0,0
574 plughw:supersonic,1
575 plughw:soundwave,1,2
576 plughw:DEV=1,CARD=soundwave,SUBDEV=2
577 \endcode
578
579 \subsection pcm_dev_names_plug Plug device
580
581 The plug device uses the plug plugin. The one SLAVE argument specifies the slave plugin.
582
583 Example:
584
585 \code
586 plug:mypcmdef
587 plug:hw
588 plug:'hw:0,0'
589 plug:SLAVE=hw
590 \endcode
591
592 \subsection pcm_dev_names_shm Shared memory device
593
594 The shm device uses the shm plugin. The two arguments (in order: SOCKET,PCM) specify
595 UNIX socket name (for example /tmp/alsa.socket) for server communication and server's PCM name.
596
597 Example:
598
599 \code
600 shm:'/tmp/alsa.sock',default
601 shm:SOCKET='/tmp/alsa.sock',PCM=default
602 \endcode
603
604 \subsection pcm_dev_names_tee Tee device
605
606 The tee device stores contents of a stream to given file plus transfers it to given slave plugin.
607 The three arguments (in order: SLAVE,FILE,FORMAT) specify slave plugin, filename and file format.
608
609 Example:
610
611 \code
612 tee:hw,'/tmp/out.raw',raw
613 \endcode
614
615 \subsection pcm_dev_names_file File device
616
617 The file device is file plugin with null plugin as slave. The arguments (in order: FILE,FORMAT)
618 specify filename and file format.
619
620 Example:
621
622 \code
623 file:'/tmp/out.raw',raw
624 \endcode
625
626 \subsection pcm_dev_names_null Null device
627
628 The null device is null plugin. This device has not any arguments.
629
630
631 \section pcm_examples Examples
632
633 The full featured examples with cross-links can be found in Examples section
634 (see top of page):
635
636 \par Sine-wave generator
637 \par
638 \link example_test_pcm alsa-lib/test/pcm.c \endlink
639 example shows various transfer methods for the playback direction.
640
641 \par Minimalistic PCM playback code
642 \par
643 \link example_test_minimal alsa-lib/test/pcm_min.c \endlink
644 example shows the minimal code to produce a sound.
645
646 \par Latency measuring tool
647 \par
648 \link example_test_latency alsa-lib/test/latency.c \endlink
649 example shows the measuring of minimal latency between capture and
650 playback devices.
651
652 */
653
654 /**
655 \example ../../test/pcm.c
656 \anchor example_test_pcm
657 Shows various transfer methods for the playback direction.
658 */
659 /**
660 \example ../../test/pcm_min.c
661 \anchor example_test_minimal
662 Shows the minimal code to produce a sound.
663 */
664 /**
665 \example ../../test/latency.c
666 \anchor example_test_latency
667 Shows the measuring of minimal latency between capture and
668 playback devices.
669 */
670
671 #include "pcm_local.h"
672 #include <stdio.h>
673 #include <string.h>
674 #if HAVE_MALLOC_H
675 #include <malloc.h>
676 #endif
677 #include <stdarg.h>
678 #include <signal.h>
679 #include <ctype.h>
680 #include <poll.h>
681 #include <sys/mman.h>
682 #include <limits.h>
683
684 #ifndef DOC_HIDDEN
685 /* return specific error codes for known bad PCM states */
686 static int pcm_state_to_error(snd_pcm_state_t state)
687 {
688         switch (state) {
689         case SND_PCM_STATE_XRUN:
690                 return -EPIPE;
691         case SND_PCM_STATE_SUSPENDED:
692                 return -ESTRPIPE;
693         case SND_PCM_STATE_DISCONNECTED:
694                 return -ENODEV;
695         default:
696                 return 0;
697         }
698 }
699
700 #define P_STATE(x)      (1U << SND_PCM_STATE_ ## x)
701 #define P_STATE_RUNNABLE (P_STATE(PREPARED) | \
702                           P_STATE(RUNNING) | \
703                           P_STATE(XRUN) | \
704                           P_STATE(PAUSED) | \
705                           P_STATE(DRAINING))
706
707 /* check whether the PCM is in the unexpected state */
708 static int bad_pcm_state(snd_pcm_t *pcm, unsigned int supported_states,
709                          unsigned int noop_states)
710 {
711         snd_pcm_state_t state;
712         int err;
713
714         if (pcm->own_state_check)
715                 return 0; /* don't care, the plugin checks by itself */
716         state = snd_pcm_state(pcm);
717         if (noop_states & (1U << state))
718                 return 1; /* OK, return immediately */
719         if (supported_states & (1U << state))
720                 return 0; /* OK */
721         err = pcm_state_to_error(state);
722         if (err < 0)
723                 return err;
724         return -EBADFD;
725 }
726 #endif
727
728 /**
729  * \brief get identifier of PCM handle
730  * \param pcm PCM handle
731  * \return ascii identifier of PCM handle
732  *
733  * Returns the ASCII identifier of given PCM handle. It's the same
734  * identifier specified in snd_pcm_open().
735  */
736 const char *snd_pcm_name(snd_pcm_t *pcm)
737 {
738         assert(pcm);
739         return pcm->name;
740 }
741
742 /**
743  * \brief get type of PCM handle
744  * \param pcm PCM handle
745  * \return type of PCM handle
746  *
747  * Returns the type #snd_pcm_type_t of given PCM handle.
748  */
749 snd_pcm_type_t snd_pcm_type(snd_pcm_t *pcm)
750 {
751         assert(pcm);
752         return pcm->type;
753 }
754
755 /**
756  * \brief get stream for a PCM handle
757  * \param pcm PCM handle
758  * \return stream of PCM handle
759  *
760  * Returns the type #snd_pcm_stream_t of given PCM handle.
761  */
762 snd_pcm_stream_t snd_pcm_stream(snd_pcm_t *pcm)
763 {
764         assert(pcm);
765         return pcm->stream;
766 }
767
768 /**
769  * \brief close PCM handle
770  * \param pcm PCM handle
771  * \return 0 on success otherwise a negative error code
772  *
773  * Closes the specified PCM handle and frees all associated
774  * resources.
775  */
776 int snd_pcm_close(snd_pcm_t *pcm)
777 {
778         int res = 0, err;
779         assert(pcm);
780         while (!list_empty(&pcm->async_handlers)) {
781                 snd_async_handler_t *h = list_entry(pcm->async_handlers.next, snd_async_handler_t, hlist);
782                 snd_async_del_handler(h);
783         }
784         if (pcm->setup && !pcm->donot_close) {
785                 snd_pcm_drop(pcm);
786                 err = snd_pcm_hw_free(pcm);
787                 if (err < 0)
788                         res = err;
789         }
790         if (pcm->mmap_channels)
791                 snd_pcm_munmap(pcm);
792         if (pcm->ops->close)
793                 err = pcm->ops->close(pcm->op_arg);
794         else
795                 err = -ENOSYS;
796         if (err < 0)
797                 res = err;
798         err = snd_pcm_free(pcm);
799         if (err < 0)
800                 res = err;
801         return res;
802 }
803
804 /**
805  * \brief set nonblock mode
806  * \param pcm PCM handle
807  * \param nonblock 0 = block, 1 = nonblock mode, 2 = abort
808  * \return 0 on success otherwise a negative error code
809  *
810  * The function is thread-safe when built with the proper option.
811  */
812 int snd_pcm_nonblock(snd_pcm_t *pcm, int nonblock)
813 {
814         int err = 0;
815
816         assert(pcm);
817         /* FIXME: __snd_pcm_lock() call below is commented out because of the
818          * the possible deadlock in signal handler calling snd_pcm_abort()
819          */
820         /* __snd_pcm_lock(pcm->op_arg); */ /* forced lock due to pcm field change */
821         if (pcm->ops->nonblock)
822                 err = pcm->ops->nonblock(pcm->op_arg, nonblock);
823         else
824                 err = -ENOSYS;
825         if (err < 0)
826                 goto unlock;
827         if (nonblock == 2) {
828                 pcm->mode |= SND_PCM_ABORT;
829                 goto unlock;
830         }
831         if (nonblock)
832                 pcm->mode |= SND_PCM_NONBLOCK;
833         else {
834                 if (pcm->hw_flags & SND_PCM_HW_PARAMS_NO_PERIOD_WAKEUP)
835                         err = -EINVAL;
836                 else
837                         pcm->mode &= ~SND_PCM_NONBLOCK;
838         }
839  unlock:
840         /* __snd_pcm_unlock(pcm->op_arg); */ /* FIXME: see above */
841         return err;
842 }
843
844 #ifndef DOC_HIDDEN
845 /**
846  * \brief set async mode
847  * \param pcm PCM handle
848  * \param sig Signal to raise: < 0 disable, 0 default (SIGIO)
849  * \param pid Process ID to signal: 0 current
850  * \return 0 on success otherwise a negative error code
851  *
852  * A signal is raised every period.
853  */
854 int snd_pcm_async(snd_pcm_t *pcm, int sig, pid_t pid)
855 {
856         int err = 0;
857
858         assert(pcm);
859         if (sig == 0)
860                 sig = SIGIO;
861         if (pid == 0)
862                 pid = getpid();
863
864 #ifdef THREAD_SAFE_API
865         /* async handler may lead to a deadlock; suppose no multi thread */
866         pcm->lock_enabled = 0;
867 #endif
868         if (pcm->ops->async)
869                 err = pcm->ops->async(pcm->op_arg, sig, pid);
870         else
871                 err = -ENOSYS;
872         return err;
873 }
874 #endif
875
876 /**
877  * \brief Obtain general (static) information for PCM handle
878  * \param pcm PCM handle
879  * \param info Information container
880  * \return 0 on success otherwise a negative error code
881  */
882 int snd_pcm_info(snd_pcm_t *pcm, snd_pcm_info_t *info)
883 {
884         int err = 0;
885
886         assert(pcm && info);
887         if (pcm->ops->info)
888                 err = pcm->ops->info(pcm->op_arg, info);
889         else
890                 err = -ENOSYS;
891         return err;
892 }
893
894 /** \brief Retreive current PCM hardware configuration chosen with #snd_pcm_hw_params
895  * \param pcm PCM handle
896  * \param params Configuration space definition container
897  * \return 0 on success otherwise a negative error code
898  * \retval -EBADFD no hardware configuration is set
899  */
900 int snd_pcm_hw_params_current(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
901 {
902         unsigned int frame_bits;
903
904         assert(pcm && params);
905         if (!pcm->setup)
906                 return -EBADFD;
907         memset(params, 0, snd_pcm_hw_params_sizeof());
908         params->flags = pcm->hw_flags;
909         snd_mask_set(&params->masks[SND_PCM_HW_PARAM_ACCESS - SND_PCM_HW_PARAM_FIRST_MASK], pcm->access);
910         snd_mask_set(&params->masks[SND_PCM_HW_PARAM_FORMAT - SND_PCM_HW_PARAM_FIRST_MASK], pcm->format);
911         snd_mask_set(&params->masks[SND_PCM_HW_PARAM_SUBFORMAT - SND_PCM_HW_PARAM_FIRST_MASK], pcm->subformat);
912         frame_bits = snd_pcm_format_physical_width(pcm->format) * pcm->channels;
913         snd_interval_set_value(&params->intervals[SND_PCM_HW_PARAM_FRAME_BITS - SND_PCM_HW_PARAM_FIRST_INTERVAL], frame_bits);
914         snd_interval_set_value(&params->intervals[SND_PCM_HW_PARAM_CHANNELS - SND_PCM_HW_PARAM_FIRST_INTERVAL], pcm->channels);
915         snd_interval_set_value(&params->intervals[SND_PCM_HW_PARAM_RATE - SND_PCM_HW_PARAM_FIRST_INTERVAL], pcm->rate);
916         snd_interval_set_value(&params->intervals[SND_PCM_HW_PARAM_PERIOD_TIME - SND_PCM_HW_PARAM_FIRST_INTERVAL], pcm->period_time);
917         snd_interval_set_value(&params->intervals[SND_PCM_HW_PARAM_PERIOD_SIZE - SND_PCM_HW_PARAM_FIRST_INTERVAL], pcm->period_size);
918         snd_interval_copy(&params->intervals[SND_PCM_HW_PARAM_PERIODS - SND_PCM_HW_PARAM_FIRST_INTERVAL], &pcm->periods);
919         snd_interval_copy(&params->intervals[SND_PCM_HW_PARAM_BUFFER_TIME - SND_PCM_HW_PARAM_FIRST_INTERVAL], &pcm->buffer_time);
920         snd_interval_set_value(&params->intervals[SND_PCM_HW_PARAM_BUFFER_SIZE - SND_PCM_HW_PARAM_FIRST_INTERVAL], pcm->buffer_size);
921         snd_interval_set_value(&params->intervals[SND_PCM_HW_PARAM_BUFFER_BYTES - SND_PCM_HW_PARAM_FIRST_INTERVAL], (pcm->buffer_size * frame_bits) / 8);
922         params->info = pcm->info;
923         params->msbits = pcm->msbits;
924         params->rate_num = pcm->rate_num;
925         params->rate_den = pcm->rate_den;
926         params->fifo_size = pcm->fifo_size;
927         return 0;
928 }
929
930 /** \brief Install one PCM hardware configuration chosen from a configuration space and #snd_pcm_prepare it
931  * \param pcm PCM handle
932  * \param params Configuration space definition container
933  * \return 0 on success otherwise a negative error code
934  *
935  * The configuration is chosen fixing single parameters in this order:
936  * first access, first format, first subformat, min channels, min rate,
937  * min period time, max buffer size, min tick time. If no mutually
938  * compatible set of parameters can be chosen, a negative error code
939  * will be returned.
940  *
941  * After this call, #snd_pcm_prepare() is called automatically and
942  * the stream is brought to \c #SND_PCM_STATE_PREPARED state.
943  *
944  * The hardware parameters cannot be changed when the stream is
945  * running (active). The software parameters can be changed
946  * at any time.
947  *
948  * The configuration space will be updated to reflect the chosen
949  * parameters.
950  */
951 int snd_pcm_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
952 {
953         int err;
954         assert(pcm && params);
955         err = _snd_pcm_hw_params_internal(pcm, params);
956         if (err < 0)
957                 return err;
958         err = snd_pcm_prepare(pcm);
959         return err;
960 }
961
962 /** \brief Remove PCM hardware configuration and free associated resources
963  * \param pcm PCM handle
964  * \return 0 on success otherwise a negative error code
965  *
966  * The function will also report success if no configuration is set.
967  */
968 int snd_pcm_hw_free(snd_pcm_t *pcm)
969 {
970         int err;
971         if (! pcm->setup)
972                 return 0;
973         if (pcm->mmap_channels) {
974                 err = snd_pcm_munmap(pcm);
975                 if (err < 0)
976                         return err;
977         }
978         // assert(snd_pcm_state(pcm) == SND_PCM_STATE_SETUP ||
979         //        snd_pcm_state(pcm) == SND_PCM_STATE_PREPARED);
980         if (pcm->ops->hw_free)
981                 err = pcm->ops->hw_free(pcm->op_arg);
982         else
983                 err = -ENOSYS;
984         pcm->setup = 0;
985         if (err < 0)
986                 return err;
987         return 0;
988 }
989
990 /** \brief Install PCM software configuration defined by params
991  * \param pcm PCM handle
992  * \param params Configuration container
993  * \return 0 on success otherwise a negative error code
994  *
995  * The software parameters can be changed at any time.
996  * The hardware parameters cannot be changed when the stream is
997  * running (active).
998  *
999  * The function is thread-safe when built with the proper option.
1000  */
1001 int snd_pcm_sw_params(snd_pcm_t *pcm, snd_pcm_sw_params_t *params)
1002 {
1003         int err;
1004         /* the hw_params must be set at first!!! */
1005         if (CHECK_SANITY(! pcm->setup)) {
1006                 snd_check(PCM, "PCM not set up");
1007                 return -EIO;
1008         }
1009         if (! params->avail_min) {
1010                 snd_check(PCM, "params->avail_min is 0");
1011                 return -EINVAL;
1012         }
1013 #if 0
1014         /* disable the check below - it looks too restrictive
1015          * (start_threshold is basically independent from avail_min)
1016          */
1017         if (params->start_threshold <= pcm->buffer_size &&
1018             params->start_threshold > (pcm->buffer_size / params->avail_min) * params->avail_min) {
1019                 snd_check(PCM, "params->avail_min problem for start_threshold");
1020                 return -EINVAL;
1021         }
1022 #endif
1023         __snd_pcm_lock(pcm->op_arg); /* forced lock due to pcm field change */
1024         if (pcm->ops->sw_params)
1025                 err = pcm->ops->sw_params(pcm->op_arg, params);
1026         else
1027                 err = -ENOSYS;
1028         if (err < 0) {
1029                 __snd_pcm_unlock(pcm->op_arg);
1030                 return err;
1031         }
1032         pcm->tstamp_mode = params->tstamp_mode;
1033         pcm->tstamp_type = params->tstamp_type;
1034         pcm->period_step = params->period_step;
1035         pcm->avail_min = params->avail_min;
1036         pcm->period_event = sw_get_period_event(params);
1037         pcm->start_threshold = params->start_threshold;
1038         pcm->stop_threshold = params->stop_threshold;
1039         pcm->silence_threshold = params->silence_threshold;
1040         pcm->silence_size = params->silence_size;
1041         pcm->boundary = params->boundary;
1042         __snd_pcm_unlock(pcm->op_arg);
1043         return 0;
1044 }
1045
1046 /**
1047  * \brief Obtain status (runtime) information for PCM handle
1048  * \param pcm PCM handle
1049  * \param status Status container
1050  * \return 0 on success otherwise a negative error code
1051  *
1052  * The function is thread-safe when built with the proper option.
1053  */
1054 int snd_pcm_status(snd_pcm_t *pcm, snd_pcm_status_t *status)
1055 {
1056         int err;
1057
1058         assert(pcm && status);
1059         snd_pcm_lock(pcm->fast_op_arg);
1060         if (pcm->fast_ops->status)
1061                 err = pcm->fast_ops->status(pcm->fast_op_arg, status);
1062         else
1063                 err = -ENOSYS;
1064         snd_pcm_unlock(pcm->fast_op_arg);
1065
1066         return err;
1067 }
1068
1069 /**
1070  * \brief Return PCM state
1071  * \param pcm PCM handle
1072  * \return PCM state #snd_pcm_state_t of given PCM handle
1073  *
1074  * This is a faster way to obtain only the PCM state without calling
1075  * \link ::snd_pcm_status() \endlink.
1076  *
1077  * Note that this function always returns one of the
1078  * #snd_pcm_state_t enum variants.
1079  * It will never return a negative error code.
1080  *
1081  * The function is thread-safe when built with the proper option.
1082  */
1083 snd_pcm_state_t snd_pcm_state(snd_pcm_t *pcm)
1084 {
1085         snd_pcm_state_t state;
1086
1087         assert(pcm);
1088         snd_pcm_lock(pcm->fast_op_arg);
1089         state = __snd_pcm_state(pcm);
1090         snd_pcm_unlock(pcm->fast_op_arg);
1091         return state;
1092 }
1093
1094 /**
1095  * \brief (DEPRECATED) Synchronize stream position with hardware
1096  * \param pcm PCM handle
1097  * \return 0 on success otherwise a negative error code
1098  *
1099  * Note this function does not update the actual r/w pointer
1100  * for applications. The function #snd_pcm_avail_update()
1101  * have to be called before any mmap begin+commit operation.
1102  *
1103  * The function is thread-safe when built with the proper option.
1104  *
1105  * This function is deprecated. Use #snd_pcm_avail_update() instead.
1106  */
1107 int snd_pcm_hwsync(snd_pcm_t *pcm)
1108 {
1109         int err;
1110
1111         assert(pcm);
1112         if (CHECK_SANITY(! pcm->setup)) {
1113                 snd_check(PCM, "PCM not set up");
1114                 return -EIO;
1115         }
1116         snd_pcm_lock(pcm->fast_op_arg);
1117         err = __snd_pcm_hwsync(pcm);
1118         snd_pcm_unlock(pcm->fast_op_arg);
1119         return err;
1120 }
1121
1122 /**
1123  * \brief Obtain delay for a running PCM handle
1124  * \param pcm PCM handle
1125  * \param delayp Returned delay in frames
1126  * \return 0 on success otherwise a negative error code
1127  *
1128  * For playback the delay is defined as the time that a frame that is written
1129  * to the PCM stream shortly after this call will take to be actually
1130  * audible. It is as such the overall latency from the write call to the final
1131  * DAC.
1132  *
1133  * For capture the delay is defined as the time that a frame that was
1134  * digitized by the audio device takes until it can be read from the PCM
1135  * stream shortly after this call returns. It is as such the overall latency
1136  * from the initial ADC to the read call.
1137  *
1138  * Please note that hence in case of a playback underrun this value will not
1139  * necessarily got down to 0.
1140  *
1141  * If the application is interested in the fill level of the playback buffer
1142  * of the device, it should use #snd_pcm_avail*() functions. The
1143  * value returned by that call is not directly related to the delay, since the
1144  * latter might include some additional, fixed latencies the former does not.
1145  *
1146  * Note this function does not update the actual r/w pointer
1147  * for applications. The function #snd_pcm_avail_update()
1148  * have to be called before any begin+commit operation.
1149  *
1150  * The function is thread-safe when built with the proper option.
1151  */
1152 int snd_pcm_delay(snd_pcm_t *pcm, snd_pcm_sframes_t *delayp)
1153 {
1154         int err;
1155
1156         assert(pcm);
1157         if (CHECK_SANITY(! pcm->setup)) {
1158                 snd_check(PCM, "PCM not set up");
1159                 return -EIO;
1160         }
1161         snd_pcm_lock(pcm->fast_op_arg);
1162         err = __snd_pcm_delay(pcm, delayp);
1163         snd_pcm_unlock(pcm->fast_op_arg);
1164         return err;
1165 }
1166
1167 /**
1168  * \brief Resume from suspend, no samples are lost
1169  * \param pcm PCM handle
1170  * \return 0 on success otherwise a negative error code
1171  * \retval -EAGAIN resume can't be proceed immediately (audio hardware is probably still suspended)
1172  * \retval -ENOSYS hardware doesn't support this feature
1173  *
1174  * This function can be used when the stream is in the suspend state
1175  * to do the fine resume from this state. Not all hardware supports
1176  * this feature, when an -ENOSYS error is returned, use the \link ::snd_pcm_prepare() \endlink
1177  * function to recovery.
1178  *
1179  * The function is thread-safe when built with the proper option.
1180  */
1181 int snd_pcm_resume(snd_pcm_t *pcm)
1182 {
1183         int err = 0;
1184
1185         assert(pcm);
1186         if (CHECK_SANITY(! pcm->setup)) {
1187                 snd_check(PCM, "PCM not set up");
1188                 return -EIO;
1189         }
1190         /* lock handled in the callback */
1191         if (pcm->fast_ops->resume)
1192                 err = pcm->fast_ops->resume(pcm->fast_op_arg);
1193         else
1194                 err = -ENOSYS;
1195         return err;
1196 }
1197
1198 /**
1199  * \brief Obtain last position update hi-res timestamp
1200  * \param pcm PCM handle
1201  * \param avail Number of available frames when timestamp was grabbed
1202  * \param tstamp Hi-res timestamp
1203  * \return 0 on success otherwise a negative error code
1204  *
1205  * Note this function does not update the actual r/w pointer
1206  * for applications.
1207  *
1208  * The function is thread-safe when built with the proper option.
1209  */
1210 int snd_pcm_htimestamp(snd_pcm_t *pcm, snd_pcm_uframes_t *avail, snd_htimestamp_t *tstamp)
1211 {
1212         int err;
1213
1214         assert(pcm);
1215         if (CHECK_SANITY(! pcm->setup)) {
1216                 snd_check(PCM, "PCM not set up");
1217                 return -EIO;
1218         }
1219         snd_pcm_lock(pcm->fast_op_arg);
1220         if (pcm->fast_ops->htimestamp)
1221                 err = pcm->fast_ops->htimestamp(pcm->fast_op_arg, avail, tstamp);
1222         else
1223                 err = -ENOSYS;
1224         snd_pcm_unlock(pcm->fast_op_arg);
1225         return err;
1226 }
1227
1228 /**
1229  * \brief Prepare PCM for use
1230  * \param pcm PCM handle
1231  * \return 0 on success otherwise a negative error code
1232  *
1233  * The function is thread-safe when built with the proper option.
1234  */
1235 int snd_pcm_prepare(snd_pcm_t *pcm)
1236 {
1237         int err;
1238
1239         assert(pcm);
1240         if (CHECK_SANITY(! pcm->setup)) {
1241                 snd_check(PCM, "PCM not set up");
1242                 return -EIO;
1243         }
1244         err = bad_pcm_state(pcm, ~P_STATE(DISCONNECTED), 0);
1245         if (err < 0)
1246                 return err;
1247         snd_pcm_lock(pcm->fast_op_arg);
1248         if (pcm->fast_ops->prepare)
1249                 err = pcm->fast_ops->prepare(pcm->fast_op_arg);
1250         else
1251                 err = -ENOSYS;
1252         snd_pcm_unlock(pcm->fast_op_arg);
1253         return err;
1254 }
1255
1256 /**
1257  * \brief Reset PCM position
1258  * \param pcm PCM handle
1259  * \return 0 on success otherwise a negative error code
1260  *
1261  * Reduce PCM delay to 0.
1262  *
1263  * The function is thread-safe when built with the proper option.
1264  */
1265 int snd_pcm_reset(snd_pcm_t *pcm)
1266 {
1267         int err;
1268
1269         assert(pcm);
1270         if (CHECK_SANITY(! pcm->setup)) {
1271                 snd_check(PCM, "PCM not set up");
1272                 return -EIO;
1273         }
1274         snd_pcm_lock(pcm->fast_op_arg);
1275         if (pcm->fast_ops->reset)
1276                 err = pcm->fast_ops->reset(pcm->fast_op_arg);
1277         else
1278                 err = -ENOSYS;
1279         snd_pcm_unlock(pcm->fast_op_arg);
1280         return err;
1281 }
1282
1283 /**
1284  * \brief Start a PCM
1285  * \param pcm PCM handle
1286  * \return 0 on success otherwise a negative error code
1287  *
1288  * The function is thread-safe when built with the proper option.
1289  */
1290 int snd_pcm_start(snd_pcm_t *pcm)
1291 {
1292         int err;
1293
1294         assert(pcm);
1295         if (CHECK_SANITY(! pcm->setup)) {
1296                 snd_check(PCM, "PCM not set up");
1297                 return -EIO;
1298         }
1299         err = bad_pcm_state(pcm, P_STATE(PREPARED), 0);
1300         if (err < 0)
1301                 return err;
1302         snd_pcm_lock(pcm->fast_op_arg);
1303         err = __snd_pcm_start(pcm);
1304         snd_pcm_unlock(pcm->fast_op_arg);
1305         return err;
1306 }
1307
1308 /**
1309  * \brief Stop a PCM dropping pending frames
1310  * \param pcm PCM handle
1311  * \return 0 on success otherwise a negative error code
1312  *
1313  * This function stops the PCM <i>immediately</i>.
1314  * The pending samples on the buffer are ignored.
1315  *
1316  * For processing all pending samples, use \link ::snd_pcm_drain() \endlink
1317  * instead.
1318  *
1319  * The function is thread-safe when built with the proper option.
1320  */
1321 int snd_pcm_drop(snd_pcm_t *pcm)
1322 {
1323         int err;
1324
1325         assert(pcm);
1326         if (CHECK_SANITY(! pcm->setup)) {
1327                 snd_check(PCM, "PCM not set up");
1328                 return -EIO;
1329         }
1330         err = bad_pcm_state(pcm, P_STATE_RUNNABLE | P_STATE(SETUP) |
1331                             P_STATE(SUSPENDED), 0);
1332         if (err < 0)
1333                 return err;
1334         snd_pcm_lock(pcm->fast_op_arg);
1335         if (pcm->fast_ops->drop)
1336                 err = pcm->fast_ops->drop(pcm->fast_op_arg);
1337         else
1338                 err = -ENOSYS;
1339         snd_pcm_unlock(pcm->fast_op_arg);
1340         return err;
1341 }
1342
1343 /**
1344  * \brief Stop a PCM preserving pending frames
1345  * \param pcm PCM handle
1346  * \return 0 on success otherwise a negative error code
1347  * \retval -ESTRPIPE a suspend event occurred
1348  *
1349  * For playback wait for all pending frames to be played and then stop
1350  * the PCM.
1351  * For capture stop PCM permitting to retrieve residual frames.
1352  *
1353  * For stopping the PCM stream immediately, use \link ::snd_pcm_drop() \endlink
1354  * instead.
1355  *
1356  * The function is thread-safe when built with the proper option.
1357  */
1358 int snd_pcm_drain(snd_pcm_t *pcm)
1359 {
1360         int err;
1361
1362         assert(pcm);
1363         if (CHECK_SANITY(! pcm->setup)) {
1364                 snd_check(PCM, "PCM not set up");
1365                 return -EIO;
1366         }
1367         err = bad_pcm_state(pcm, P_STATE_RUNNABLE | P_STATE(SETUP), P_STATE(SETUP));
1368         if (err < 0)
1369                 return err;
1370         if (err == 1)
1371                 return 0;
1372         /* lock handled in the callback */
1373         if (pcm->fast_ops->drain)
1374                 err = pcm->fast_ops->drain(pcm->fast_op_arg);
1375         else
1376                 err = -ENOSYS;
1377         return err;
1378 }
1379
1380 /**
1381  * \brief Pause/resume PCM
1382  * \param pcm PCM handle
1383  * \param enable 0 = resume, 1 = pause
1384  * \return 0 on success otherwise a negative error code
1385  *
1386  * Note that this function works only on the hardware which supports
1387  * pause feature.  You can check it via \link ::snd_pcm_hw_params_can_pause() \endlink
1388  * function.
1389  *
1390  * The function is thread-safe when built with the proper option.
1391  */
1392 int snd_pcm_pause(snd_pcm_t *pcm, int enable)
1393 {
1394         int err;
1395
1396         assert(pcm);
1397         if (CHECK_SANITY(! pcm->setup)) {
1398                 snd_check(PCM, "PCM not set up");
1399                 return -EIO;
1400         }
1401         err = bad_pcm_state(pcm, P_STATE_RUNNABLE, 0);
1402         if (err < 0)
1403                 return err;
1404         snd_pcm_lock(pcm->fast_op_arg);
1405         if (pcm->fast_ops->pause)
1406                 err = pcm->fast_ops->pause(pcm->fast_op_arg, enable);
1407         else
1408                 err = -ENOSYS;
1409         snd_pcm_unlock(pcm->fast_op_arg);
1410         return err;
1411 }
1412
1413 /**
1414  * \brief Get safe count of frames which can be rewinded
1415  * \param pcm PCM handle
1416  * \return a positive number of frames or negative error code
1417  *
1418  * Note: The snd_pcm_rewind() can accept bigger value than returned
1419  * by this function. But it is not guaranteed that output stream
1420  * will be consistent with bigger value.
1421  *
1422  * The function is thread-safe when built with the proper option.
1423  */
1424 snd_pcm_sframes_t snd_pcm_rewindable(snd_pcm_t *pcm)
1425 {
1426         snd_pcm_sframes_t result;
1427         int err;
1428
1429         assert(pcm);
1430         if (CHECK_SANITY(! pcm->setup)) {
1431                 snd_check(PCM, "PCM not set up");
1432                 return -EIO;
1433         }
1434         err = bad_pcm_state(pcm, P_STATE_RUNNABLE, 0);
1435         if (err < 0)
1436                 return err;
1437         snd_pcm_lock(pcm->fast_op_arg);
1438         if (pcm->fast_ops->rewindable)
1439                 result = pcm->fast_ops->rewindable(pcm->fast_op_arg);
1440         else
1441                 result = -ENOSYS;
1442         snd_pcm_unlock(pcm->fast_op_arg);
1443         return result;
1444 }
1445
1446 /**
1447  * \brief Move application frame position backward
1448  * \param pcm PCM handle
1449  * \param frames wanted displacement in frames
1450  * \return a positive number for actual displacement otherwise a
1451  * negative error code
1452  *
1453  * The function is thread-safe when built with the proper option.
1454  */
1455 snd_pcm_sframes_t snd_pcm_rewind(snd_pcm_t *pcm, snd_pcm_uframes_t frames)
1456 {
1457         snd_pcm_sframes_t result;
1458         int err;
1459
1460         assert(pcm);
1461         if (CHECK_SANITY(! pcm->setup)) {
1462                 snd_check(PCM, "PCM not set up");
1463                 return -EIO;
1464         }
1465         if (frames == 0)
1466                 return 0;
1467         err = bad_pcm_state(pcm, P_STATE_RUNNABLE, 0);
1468         if (err < 0)
1469                 return err;
1470         snd_pcm_lock(pcm->fast_op_arg);
1471         if (pcm->fast_ops->rewind)
1472                 result = pcm->fast_ops->rewind(pcm->fast_op_arg, frames);
1473         else
1474                 result = -ENOSYS;
1475         snd_pcm_unlock(pcm->fast_op_arg);
1476         return result;
1477 }
1478
1479 /**
1480  * \brief Get safe count of frames which can be forwarded
1481  * \param pcm PCM handle
1482  * \return a positive number of frames or negative error code
1483  *
1484  * Note: The snd_pcm_forward() can accept bigger value than returned
1485  * by this function. But it is not guaranteed that output stream
1486  * will be consistent with bigger value.
1487  *
1488  * The function is thread-safe when built with the proper option.
1489  */
1490 snd_pcm_sframes_t snd_pcm_forwardable(snd_pcm_t *pcm)
1491 {
1492         snd_pcm_sframes_t result;
1493         int err;
1494
1495         assert(pcm);
1496         if (CHECK_SANITY(! pcm->setup)) {
1497                 snd_check(PCM, "PCM not set up");
1498                 return -EIO;
1499         }
1500         err = bad_pcm_state(pcm, P_STATE_RUNNABLE, 0);
1501         if (err < 0)
1502                 return err;
1503         snd_pcm_lock(pcm->fast_op_arg);
1504         if (pcm->fast_ops->forwardable)
1505                 result = pcm->fast_ops->forwardable(pcm->fast_op_arg);
1506         else
1507                 result = -ENOSYS;
1508         snd_pcm_unlock(pcm->fast_op_arg);
1509         return result;
1510 }
1511
1512 /**
1513  * \brief Move application frame position forward
1514  * \param pcm PCM handle
1515  * \param frames wanted skip in frames
1516  * \return a positive number for actual skip otherwise a negative error code
1517  * \retval 0 means no action
1518  *
1519  * The function is thread-safe when built with the proper option.
1520  */
1521 #ifndef DOXYGEN
1522 EXPORT_SYMBOL snd_pcm_sframes_t INTERNAL(snd_pcm_forward)(snd_pcm_t *pcm, snd_pcm_uframes_t frames)
1523 #else
1524 snd_pcm_sframes_t snd_pcm_forward(snd_pcm_t *pcm, snd_pcm_uframes_t frames)
1525 #endif
1526 {
1527         snd_pcm_sframes_t result;
1528         int err;
1529
1530         assert(pcm);
1531         if (CHECK_SANITY(! pcm->setup)) {
1532                 snd_check(PCM, "PCM not set up");
1533                 return -EIO;
1534         }
1535         if (frames == 0)
1536                 return 0;
1537         err = bad_pcm_state(pcm, P_STATE_RUNNABLE, 0);
1538         if (err < 0)
1539                 return err;
1540         snd_pcm_lock(pcm->fast_op_arg);
1541         if (pcm->fast_ops->forward)
1542                 result = pcm->fast_ops->forward(pcm->fast_op_arg, frames);
1543         else
1544                 result = -ENOSYS;
1545         snd_pcm_unlock(pcm->fast_op_arg);
1546         return result;
1547 }
1548 use_default_symbol_version(__snd_pcm_forward, snd_pcm_forward, ALSA_0.9.0rc8);
1549
1550 /**
1551  * \brief Write interleaved frames to a PCM
1552  * \param pcm PCM handle
1553  * \param buffer frames containing buffer
1554  * \param size frames to be written
1555  * \return a positive number of frames actually written otherwise a
1556  * negative error code
1557  * \retval -EBADFD PCM is not in the right state (#SND_PCM_STATE_PREPARED or #SND_PCM_STATE_RUNNING)
1558  * \retval -EPIPE an underrun occurred
1559  * \retval -ESTRPIPE a suspend event occurred (stream is suspended and waiting for an application recovery)
1560  *
1561  * If the blocking behaviour is selected and it is running, then routine waits until
1562  * all requested frames are played or put to the playback ring buffer.
1563  * The returned number of frames can be less only if a signal or underrun occurred.
1564  *
1565  * If the non-blocking behaviour is selected, then routine doesn't wait at all.
1566  *
1567  * The function is thread-safe when built with the proper option.
1568  */
1569 snd_pcm_sframes_t snd_pcm_writei(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size)
1570 {
1571         int err;
1572
1573         assert(pcm);
1574         assert(size == 0 || buffer);
1575         if (CHECK_SANITY(! pcm->setup)) {
1576                 snd_check(PCM, "PCM not set up");
1577                 return -EIO;
1578         }
1579         if (pcm->access != SND_PCM_ACCESS_RW_INTERLEAVED) {
1580                 snd_check(PCM, "invalid access type %s", snd_pcm_access_name(pcm->access));
1581                 return -EINVAL;
1582         }
1583         err = bad_pcm_state(pcm, P_STATE_RUNNABLE, 0);
1584         if (err < 0)
1585                 return err;
1586         return _snd_pcm_writei(pcm, buffer, size);
1587 }
1588
1589 /**
1590  * \brief Write non interleaved frames to a PCM
1591  * \param pcm PCM handle
1592  * \param bufs frames containing buffers (one for each channel)
1593  * \param size frames to be written
1594  * \return a positive number of frames actually written otherwise a
1595  * negative error code
1596  * \retval -EBADFD PCM is not in the right state (#SND_PCM_STATE_PREPARED or #SND_PCM_STATE_RUNNING)
1597  * \retval -EPIPE an underrun occurred
1598  * \retval -ESTRPIPE a suspend event occurred (stream is suspended and waiting for an application recovery)
1599  *
1600  * If the blocking behaviour is selected and it is running, then routine waits until
1601  * all requested frames are played or put to the playback ring buffer.
1602  * The returned number of frames can be less only if a signal or underrun occurred.
1603  *
1604  * If the non-blocking behaviour is selected, then routine doesn't wait at all.
1605  *
1606  * The function is thread-safe when built with the proper option.
1607  */
1608 snd_pcm_sframes_t snd_pcm_writen(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size)
1609 {
1610         int err;
1611
1612         assert(pcm);
1613         assert(size == 0 || bufs);
1614         if (CHECK_SANITY(! pcm->setup)) {
1615                 snd_check(PCM, "PCM not set up");
1616                 return -EIO;
1617         }
1618         if (pcm->access != SND_PCM_ACCESS_RW_NONINTERLEAVED) {
1619                 snd_check(PCM, "invalid access type %s", snd_pcm_access_name(pcm->access));
1620                 return -EINVAL;
1621         }
1622         err = bad_pcm_state(pcm, P_STATE_RUNNABLE, 0);
1623         if (err < 0)
1624                 return err;
1625         return _snd_pcm_writen(pcm, bufs, size);
1626 }
1627
1628 /**
1629  * \brief Read interleaved frames from a PCM
1630  * \param pcm PCM handle
1631  * \param buffer frames containing buffer
1632  * \param size frames to be read
1633  * \return a positive number of frames actually read otherwise a
1634  * negative error code
1635  * \retval -EBADFD PCM is not in the right state (#SND_PCM_STATE_PREPARED or #SND_PCM_STATE_RUNNING)
1636  * \retval -EPIPE an overrun occurred
1637  * \retval -ESTRPIPE a suspend event occurred (stream is suspended and waiting for an application recovery)
1638  *
1639  * If the blocking behaviour was selected and it is running, then routine waits until
1640  * all requested frames are filled. The returned number of frames can be less only
1641  * if a signal or underrun occurred.
1642  *
1643  * If the non-blocking behaviour is selected, then routine doesn't wait at all.
1644  *
1645  * The function is thread-safe when built with the proper option.
1646  */
1647 snd_pcm_sframes_t snd_pcm_readi(snd_pcm_t *pcm, void *buffer, snd_pcm_uframes_t size)
1648 {
1649         int err;
1650
1651         assert(pcm);
1652         assert(size == 0 || buffer);
1653         if (CHECK_SANITY(! pcm->setup)) {
1654                 snd_check(PCM, "PCM not set up");
1655                 return -EIO;
1656         }
1657         if (pcm->access != SND_PCM_ACCESS_RW_INTERLEAVED) {
1658                 snd_check(PCM, "invalid access type %s", snd_pcm_access_name(pcm->access));
1659                 return -EINVAL;
1660         }
1661         err = bad_pcm_state(pcm, P_STATE_RUNNABLE, 0);
1662         if (err < 0)
1663                 return err;
1664         return _snd_pcm_readi(pcm, buffer, size);
1665 }
1666
1667 /**
1668  * \brief Read non interleaved frames to a PCM
1669  * \param pcm PCM handle
1670  * \param bufs frames containing buffers (one for each channel)
1671  * \param size frames to be read
1672  * \return a positive number of frames actually read otherwise a
1673  * negative error code
1674  * \retval -EBADFD PCM is not in the right state (#SND_PCM_STATE_PREPARED or #SND_PCM_STATE_RUNNING)
1675  * \retval -EPIPE an overrun occurred
1676  * \retval -ESTRPIPE a suspend event occurred (stream is suspended and waiting for an application recovery)
1677  *
1678  * If the blocking behaviour was selected and it is running, then routine waits until
1679  * all requested frames are filled. The returned number of frames can be less only
1680  * if a signal or underrun occurred.
1681  *
1682  * If the non-blocking behaviour is selected, then routine doesn't wait at all.
1683  *
1684  * The function is thread-safe when built with the proper option.
1685  */
1686 snd_pcm_sframes_t snd_pcm_readn(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size)
1687 {
1688         int err;
1689
1690         assert(pcm);
1691         assert(size == 0 || bufs);
1692         if (CHECK_SANITY(! pcm->setup)) {
1693                 snd_check(PCM, "PCM not set up");
1694                 return -EIO;
1695         }
1696         if (pcm->access != SND_PCM_ACCESS_RW_NONINTERLEAVED) {
1697                 snd_check(PCM, "invalid access type %s", snd_pcm_access_name(pcm->access));
1698                 return -EINVAL;
1699         }
1700         err = bad_pcm_state(pcm, P_STATE_RUNNABLE, 0);
1701         if (err < 0)
1702                 return err;
1703         return _snd_pcm_readn(pcm, bufs, size);
1704 }
1705
1706 /**
1707  * \brief Link two PCMs
1708  * \param pcm1 first PCM handle
1709  * \param pcm2 first PCM handle
1710  * \return 0 on success otherwise a negative error code
1711  *
1712  * The two PCMs will start/stop/prepare in sync.
1713  */
1714 int snd_pcm_link(snd_pcm_t *pcm1, snd_pcm_t *pcm2)
1715 {
1716         int err = 0;
1717
1718         assert(pcm1);
1719         assert(pcm2);
1720         if (pcm1->fast_ops->link)
1721                 err = pcm1->fast_ops->link(pcm1->fast_op_arg, pcm2);
1722         else
1723                 err = -ENOSYS;
1724         return err;
1725 }
1726
1727 /**
1728  * \brief Remove a PCM from a linked group
1729  * \param pcm PCM handle
1730  * \return 0 on success otherwise a negative error code
1731  */
1732 int snd_pcm_unlink(snd_pcm_t *pcm)
1733 {
1734         int err = 0;
1735
1736         assert(pcm);
1737         if (pcm->fast_ops->unlink)
1738                 err = pcm->fast_ops->unlink(pcm->fast_op_arg);
1739         else
1740                 err = -ENOSYS;
1741         return err;
1742 }
1743
1744 /* locked version */
1745 static int __snd_pcm_poll_descriptors_count(snd_pcm_t *pcm)
1746 {
1747         if (pcm->fast_ops->poll_descriptors_count)
1748                 return pcm->fast_ops->poll_descriptors_count(pcm->fast_op_arg);
1749         return pcm->poll_fd_count;
1750 }
1751
1752 /**
1753  * \brief get count of poll descriptors for PCM handle
1754  * \param pcm PCM handle
1755  * \return count of poll descriptors
1756  *
1757  * The function is thread-safe when built with the proper option.
1758  */
1759 int snd_pcm_poll_descriptors_count(snd_pcm_t *pcm)
1760 {
1761         int count;
1762
1763         assert(pcm);
1764         snd_pcm_lock(pcm->fast_op_arg);
1765         count = __snd_pcm_poll_descriptors_count(pcm);
1766         snd_pcm_unlock(pcm->fast_op_arg);
1767         return count;
1768 }
1769
1770 /* locked version */
1771 static int __snd_pcm_poll_descriptors(snd_pcm_t *pcm, struct pollfd *pfds,
1772                                       unsigned int space)
1773 {
1774         if (pcm->fast_ops->poll_descriptors)
1775                 return pcm->fast_ops->poll_descriptors(pcm->fast_op_arg, pfds, space);
1776         if (pcm->poll_fd < 0) {
1777                 snd_check(PCM, "poll_fd < 0");
1778                 return -EIO;
1779         }
1780         if (space >= 1 && pfds) {
1781                 pfds->fd = pcm->poll_fd;
1782                 pfds->events = pcm->poll_events | POLLERR | POLLNVAL;
1783         } else {
1784                 return 0;
1785         }
1786         return 1;
1787 }
1788
1789 /**
1790  * \brief get poll descriptors
1791  * \param pcm PCM handle
1792  * \param pfds array of poll descriptors
1793  * \param space space in the poll descriptor array
1794  * \return count of filled descriptors
1795  *
1796  * This function fills the given poll descriptor structs for the specified
1797  * PCM handle.  The poll desctiptor array should have the size returned by
1798  * \link ::snd_pcm_poll_descriptors_count() \endlink function.
1799  *
1800  * The result is intended for direct use with the poll() syscall.
1801  *
1802  * For reading the returned events of poll descriptor after poll() system
1803  * call, use \link ::snd_pcm_poll_descriptors_revents() \endlink function.
1804  * The field values in pollfd structs may be bogus regarding the stream
1805  * direction from the application perspective (POLLIN might not imply read
1806  * direction and POLLOUT might not imply write), but
1807  * the \link ::snd_pcm_poll_descriptors_revents() \endlink function
1808  * does the right "demangling".
1809  *
1810  * You can use output from this function as arguments for the select()
1811  * syscall, too. Do not forget to translate POLLIN and POLLOUT events to
1812  * corresponding FD_SET arrays and demangle events using
1813  * \link ::snd_pcm_poll_descriptors_revents() \endlink .
1814  *
1815  * It is guaranteed that for the given PCM handle, the output poll
1816  * descriptor structs (and their count) will not change after
1817  * hardware and software parameters setup. Thus it is valid to call
1818  * the function once when all parameters are set and reuse its output
1819  * for the lifetime of the stream parameters.
1820  *
1821  * The function is thread-safe when built with the proper option.
1822  */
1823 int snd_pcm_poll_descriptors(snd_pcm_t *pcm, struct pollfd *pfds, unsigned int space)
1824 {
1825         int err;
1826
1827         assert(pcm && pfds);
1828         snd_pcm_lock(pcm->fast_op_arg);
1829         err = __snd_pcm_poll_descriptors(pcm, pfds, space);
1830         snd_pcm_unlock(pcm->fast_op_arg);
1831         return err;
1832 }
1833
1834 static int __snd_pcm_poll_revents(snd_pcm_t *pcm, struct pollfd *pfds,
1835                                   unsigned int nfds, unsigned short *revents);
1836
1837 /**
1838  * \brief get returned events from poll descriptors
1839  * \param pcm PCM handle
1840  * \param pfds array of poll descriptors
1841  * \param nfds count of poll descriptors
1842  * \param revents pointer to the returned (single) event
1843  * \return zero if success, otherwise a negative error code
1844  *
1845  * This function does "demangling" of the revents mask returned from
1846  * the poll() syscall to correct semantics (POLLIN = read, POLLOUT = write).
1847  *
1848  * Note: The null event also exists. Even if poll() or select()
1849  * syscall returned that some events are waiting, this function might
1850  * return empty set of events. In this case, application should
1851  * do next event waiting using poll() or select().
1852  *
1853  * Note: Even if multiple poll descriptors are used (i.e. pfds > 1),
1854  * this function returns only a single event.
1855  *
1856  * The passed in count of poll descriptors must be equal to
1857  * \link ::snd_pcm_poll_descriptors_count() \endlink and the passed in array
1858  * must match the array returned by \link ::snd_pcm_poll_descriptors() \endlink
1859  * (in its full length and original order) with the revent fields updated
1860  * according to the poll() result. This function will not modify the file
1861  * descriptor or event field of any element of the given poll descriptor array.
1862  *
1863  * The function is thread-safe when built with the proper option.
1864  */
1865 int snd_pcm_poll_descriptors_revents(snd_pcm_t *pcm, struct pollfd *pfds, unsigned int nfds, unsigned short *revents)
1866 {
1867         int err;
1868
1869         assert(pcm && pfds && revents);
1870         snd_pcm_lock(pcm->fast_op_arg);
1871         err = __snd_pcm_poll_revents(pcm, pfds, nfds, revents);
1872         snd_pcm_unlock(pcm->fast_op_arg);
1873         return err;
1874 }
1875
1876 static int __snd_pcm_poll_revents(snd_pcm_t *pcm, struct pollfd *pfds,
1877                                   unsigned int nfds, unsigned short *revents)
1878 {
1879         if (pcm->fast_ops->poll_revents)
1880                 return pcm->fast_ops->poll_revents(pcm->fast_op_arg, pfds, nfds, revents);
1881         if (nfds == 1) {
1882                 *revents = pfds->revents;
1883                 return 0;
1884         }
1885         return -EINVAL;
1886 }
1887
1888 #ifndef DOC_HIDDEN
1889 #define PCMTYPE(v) [SND_PCM_TYPE_##v] = #v
1890 #define STATE(v) [SND_PCM_STATE_##v] = #v
1891 #define STREAM(v) [SND_PCM_STREAM_##v] = #v
1892 #define READY(v) [SND_PCM_READY_##v] = #v
1893 #define XRUN(v) [SND_PCM_XRUN_##v] = #v
1894 #define SILENCE(v) [SND_PCM_SILENCE_##v] = #v
1895 #define TSTAMP(v) [SND_PCM_TSTAMP_##v] = #v
1896 #define TSTAMP_TYPE(v) [SND_PCM_TSTAMP_TYPE_##v] = #v
1897 #define ACCESS(v) [SND_PCM_ACCESS_##v] = #v
1898 #define START(v) [SND_PCM_START_##v] = #v
1899 #define HW_PARAM(v) [SND_PCM_HW_PARAM_##v] = #v
1900 #define SW_PARAM(v) [SND_PCM_SW_PARAM_##v] = #v
1901 #define FORMAT(v) [SND_PCM_FORMAT_##v] = #v
1902 #define SUBFORMAT(v) [SND_PCM_SUBFORMAT_##v] = #v
1903
1904 #define FORMATD(v, d) [SND_PCM_FORMAT_##v] = d
1905 #define SUBFORMATD(v, d) [SND_PCM_SUBFORMAT_##v] = d
1906
1907
1908 static const char *const snd_pcm_stream_names[] = {
1909         STREAM(PLAYBACK),
1910         STREAM(CAPTURE),
1911 };
1912
1913 static const char *const snd_pcm_state_names[] = {
1914         STATE(OPEN),
1915         STATE(SETUP),
1916         STATE(PREPARED),
1917         STATE(RUNNING),
1918         STATE(XRUN),
1919         STATE(DRAINING),
1920         STATE(PAUSED),
1921         STATE(SUSPENDED),
1922         STATE(DISCONNECTED),
1923 };
1924
1925 static const char *const snd_pcm_access_names[] = {
1926         ACCESS(MMAP_INTERLEAVED),
1927         ACCESS(MMAP_NONINTERLEAVED),
1928         ACCESS(MMAP_COMPLEX),
1929         ACCESS(RW_INTERLEAVED),
1930         ACCESS(RW_NONINTERLEAVED),
1931 };
1932
1933 static const char *const snd_pcm_format_names[] = {
1934         FORMAT(S8),
1935         FORMAT(U8),
1936         FORMAT(S16_LE),
1937         FORMAT(S16_BE),
1938         FORMAT(U16_LE),
1939         FORMAT(U16_BE),
1940         FORMAT(S24_LE),
1941         FORMAT(S24_BE),
1942         FORMAT(U24_LE),
1943         FORMAT(U24_BE),
1944         FORMAT(S32_LE),
1945         FORMAT(S32_BE),
1946         FORMAT(U32_LE),
1947         FORMAT(U32_BE),
1948         FORMAT(FLOAT_LE),
1949         FORMAT(FLOAT_BE),
1950         FORMAT(FLOAT64_LE),
1951         FORMAT(FLOAT64_BE),
1952         FORMAT(IEC958_SUBFRAME_LE),
1953         FORMAT(IEC958_SUBFRAME_BE),
1954         FORMAT(MU_LAW),
1955         FORMAT(A_LAW),
1956         FORMAT(IMA_ADPCM),
1957         FORMAT(MPEG),
1958         FORMAT(GSM),
1959         FORMAT(S20_LE),
1960         FORMAT(S20_BE),
1961         FORMAT(U20_LE),
1962         FORMAT(U20_BE),
1963         FORMAT(SPECIAL),
1964         FORMAT(S24_3LE),
1965         FORMAT(S24_3BE),
1966         FORMAT(U24_3LE),
1967         FORMAT(U24_3BE),
1968         FORMAT(S20_3LE),
1969         FORMAT(S20_3BE),
1970         FORMAT(U20_3LE),
1971         FORMAT(U20_3BE),
1972         FORMAT(S18_3LE),
1973         FORMAT(S18_3BE),
1974         FORMAT(U18_3LE),
1975         FORMAT(U18_3BE),
1976         FORMAT(G723_24),
1977         FORMAT(G723_24_1B),
1978         FORMAT(G723_40),
1979         FORMAT(G723_40_1B),
1980         FORMAT(DSD_U8),
1981         FORMAT(DSD_U16_LE),
1982         FORMAT(DSD_U32_LE),
1983         FORMAT(DSD_U16_BE),
1984         FORMAT(DSD_U32_BE),
1985 };
1986
1987 static const char *const snd_pcm_format_aliases[SND_PCM_FORMAT_LAST+1] = {
1988         FORMAT(S16),
1989         FORMAT(U16),
1990         FORMAT(S24),
1991         FORMAT(U24),
1992         FORMAT(S32),
1993         FORMAT(U32),
1994         FORMAT(FLOAT),
1995         FORMAT(FLOAT64),
1996         FORMAT(IEC958_SUBFRAME),
1997         FORMAT(S20),
1998         FORMAT(U20),
1999 };
2000
2001 static const char *const snd_pcm_format_descriptions[] = {
2002         FORMATD(S8, "Signed 8 bit"),
2003         FORMATD(U8, "Unsigned 8 bit"),
2004         FORMATD(S16_LE, "Signed 16 bit Little Endian"),
2005         FORMATD(S16_BE, "Signed 16 bit Big Endian"),
2006         FORMATD(U16_LE, "Unsigned 16 bit Little Endian"),
2007         FORMATD(U16_BE, "Unsigned 16 bit Big Endian"),
2008         FORMATD(S24_LE, "Signed 24 bit Little Endian"),
2009         FORMATD(S24_BE, "Signed 24 bit Big Endian"),
2010         FORMATD(U24_LE, "Unsigned 24 bit Little Endian"),
2011         FORMATD(U24_BE, "Unsigned 24 bit Big Endian"),
2012         FORMATD(S32_LE, "Signed 32 bit Little Endian"),
2013         FORMATD(S32_BE, "Signed 32 bit Big Endian"),
2014         FORMATD(U32_LE, "Unsigned 32 bit Little Endian"),
2015         FORMATD(U32_BE, "Unsigned 32 bit Big Endian"),
2016         FORMATD(FLOAT_LE, "Float 32 bit Little Endian"),
2017         FORMATD(FLOAT_BE, "Float 32 bit Big Endian"),
2018         FORMATD(FLOAT64_LE, "Float 64 bit Little Endian"),
2019         FORMATD(FLOAT64_BE, "Float 64 bit Big Endian"),
2020         FORMATD(IEC958_SUBFRAME_LE, "IEC-958 Little Endian"),
2021         FORMATD(IEC958_SUBFRAME_BE, "IEC-958 Big Endian"),
2022         FORMATD(MU_LAW, "Mu-Law"),
2023         FORMATD(A_LAW, "A-Law"),
2024         FORMATD(IMA_ADPCM, "Ima-ADPCM"),
2025         FORMATD(MPEG, "MPEG"),
2026         FORMATD(GSM, "GSM"),
2027         FORMATD(S20_LE, "Signed 20 bit Little Endian in 4 bytes, LSB justified"),
2028         FORMATD(S20_BE, "Signed 20 bit Big Endian in 4 bytes, LSB justified"),
2029         FORMATD(U20_LE, "Unsigned 20 bit Little Endian in 4 bytes, LSB justified"),
2030         FORMATD(U20_BE, "Unsigned 20 bit Big Endian in 4 bytes, LSB justified"),
2031         FORMATD(SPECIAL, "Special"),
2032         FORMATD(S24_3LE, "Signed 24 bit Little Endian in 3bytes"),
2033         FORMATD(S24_3BE, "Signed 24 bit Big Endian in 3bytes"),
2034         FORMATD(U24_3LE, "Unsigned 24 bit Little Endian in 3bytes"),
2035         FORMATD(U24_3BE, "Unsigned 24 bit Big Endian in 3bytes"),
2036         FORMATD(S20_3LE, "Signed 20 bit Little Endian in 3bytes"),
2037         FORMATD(S20_3BE, "Signed 20 bit Big Endian in 3bytes"),
2038         FORMATD(U20_3LE, "Unsigned 20 bit Little Endian in 3bytes"),
2039         FORMATD(U20_3BE, "Unsigned 20 bit Big Endian in 3bytes"),
2040         FORMATD(S18_3LE, "Signed 18 bit Little Endian in 3bytes"),
2041         FORMATD(S18_3BE, "Signed 18 bit Big Endian in 3bytes"),
2042         FORMATD(U18_3LE, "Unsigned 18 bit Little Endian in 3bytes"),
2043         FORMATD(U18_3BE, "Unsigned 18 bit Big Endian in 3bytes"),
2044         FORMATD(G723_24, "G.723 (ADPCM) 24 kbit/s, 8 samples in 3 bytes"),
2045         FORMATD(G723_24_1B, "G.723 (ADPCM) 24 kbit/s, 1 sample in 1 byte"),
2046         FORMATD(G723_40, "G.723 (ADPCM) 40 kbit/s, 8 samples in 3 bytes"),
2047         FORMATD(G723_40_1B, "G.723 (ADPCM) 40 kbit/s, 1 sample in 1 byte"),
2048         FORMATD(DSD_U8,  "Direct Stream Digital, 1-byte (x8), oldest bit in MSB"),
2049         FORMATD(DSD_U16_LE, "Direct Stream Digital, 2-byte (x16), little endian, oldest bits in MSB"),
2050         FORMATD(DSD_U32_LE, "Direct Stream Digital, 4-byte (x32), little endian, oldest bits in MSB"),
2051         FORMATD(DSD_U16_BE, "Direct Stream Digital, 2-byte (x16), big endian, oldest bits in MSB"),
2052         FORMATD(DSD_U32_BE, "Direct Stream Digital, 4-byte (x32), big endian, oldest bits in MSB"),
2053 };
2054
2055 static const char *const snd_pcm_type_names[] = {
2056         PCMTYPE(HW),
2057         PCMTYPE(HOOKS),
2058         PCMTYPE(MULTI),
2059         PCMTYPE(FILE),
2060         PCMTYPE(NULL),
2061         PCMTYPE(SHM),
2062         PCMTYPE(INET),
2063         PCMTYPE(COPY),
2064         PCMTYPE(LINEAR),
2065         PCMTYPE(ALAW),
2066         PCMTYPE(MULAW),
2067         PCMTYPE(ADPCM),
2068         PCMTYPE(RATE),
2069         PCMTYPE(ROUTE),
2070         PCMTYPE(PLUG),
2071         PCMTYPE(SHARE),
2072         PCMTYPE(METER),
2073         PCMTYPE(MIX),
2074         PCMTYPE(DROUTE),
2075         PCMTYPE(LBSERVER),
2076         PCMTYPE(LINEAR_FLOAT),
2077         PCMTYPE(LADSPA),
2078         PCMTYPE(DMIX),
2079         PCMTYPE(JACK),
2080         PCMTYPE(DSNOOP),
2081         PCMTYPE(IEC958),
2082         PCMTYPE(SOFTVOL),
2083         PCMTYPE(IOPLUG),
2084         PCMTYPE(EXTPLUG),
2085         PCMTYPE(MMAP_EMUL),
2086 };
2087
2088 static const char *const snd_pcm_subformat_names[] = {
2089         SUBFORMAT(STD),
2090         SUBFORMAT(MSBITS_MAX),
2091         SUBFORMAT(MSBITS_20),
2092         SUBFORMAT(MSBITS_24),
2093 };
2094
2095 static const char *const snd_pcm_subformat_descriptions[] = {
2096         SUBFORMATD(STD, "Standard"),
2097         SUBFORMATD(MSBITS_MAX, "Maximum based on PCM format"),
2098         SUBFORMATD(MSBITS_20, "20 most significant bits"),
2099         SUBFORMATD(MSBITS_24, "24 most significant bits"),
2100 };
2101
2102 static const char *const snd_pcm_start_mode_names[] = {
2103         START(EXPLICIT),
2104         START(DATA),
2105 };
2106
2107 static const char *const snd_pcm_xrun_mode_names[] = {
2108         XRUN(NONE),
2109         XRUN(STOP),
2110 };
2111
2112 static const char *const snd_pcm_tstamp_mode_names[] = {
2113         TSTAMP(NONE),
2114         TSTAMP(ENABLE),
2115 };
2116
2117 static const char *const snd_pcm_tstamp_type_names[] = {
2118         TSTAMP_TYPE(GETTIMEOFDAY),
2119         TSTAMP_TYPE(MONOTONIC),
2120         TSTAMP_TYPE(MONOTONIC_RAW),
2121 };
2122 #endif
2123
2124 /**
2125  * \brief get name of PCM stream type
2126  * \param stream PCM stream type
2127  * \return ascii name of PCM stream type
2128  */
2129 const char *snd_pcm_stream_name(const snd_pcm_stream_t stream)
2130 {
2131         if (stream > SND_PCM_STREAM_LAST)
2132                 return NULL;
2133         return snd_pcm_stream_names[stream];
2134 }
2135
2136 /**
2137  * \brief get name of PCM access type
2138  * \param acc PCM access type
2139  * \return ascii name of PCM access type
2140  */
2141 const char *snd_pcm_access_name(const snd_pcm_access_t acc)
2142 {
2143         if (acc > SND_PCM_ACCESS_LAST)
2144                 return NULL;
2145         return snd_pcm_access_names[acc];
2146 }
2147
2148 /**
2149  * \brief get name of PCM sample format
2150  * \param format PCM sample format
2151  * \return ascii name of PCM sample format
2152  */
2153 const char *snd_pcm_format_name(const snd_pcm_format_t format)
2154 {
2155         if (format > SND_PCM_FORMAT_LAST)
2156                 return NULL;
2157         return snd_pcm_format_names[format];
2158 }
2159
2160 /**
2161  * \brief get description of PCM sample format
2162  * \param format PCM sample format
2163  * \return ascii description of PCM sample format
2164  */
2165 const char *snd_pcm_format_description(const snd_pcm_format_t format)
2166 {
2167         if (format > SND_PCM_FORMAT_LAST)
2168                 return NULL;
2169         return snd_pcm_format_descriptions[format];
2170 }
2171
2172 /**
2173  * \brief get PCM sample format from name
2174  * \param name PCM sample format name (case insensitive)
2175  * \return PCM sample format
2176  */
2177 snd_pcm_format_t snd_pcm_format_value(const char* name)
2178 {
2179         snd_pcm_format_t format;
2180         for (format = 0; format <= SND_PCM_FORMAT_LAST; format++) {
2181                 if (snd_pcm_format_names[format] &&
2182                     strcasecmp(name, snd_pcm_format_names[format]) == 0) {
2183                         return format;
2184                 }
2185                 if (snd_pcm_format_aliases[format] &&
2186                     strcasecmp(name, snd_pcm_format_aliases[format]) == 0) {
2187                         return format;
2188                 }
2189         }
2190         for (format = 0; format <= SND_PCM_FORMAT_LAST; format++) {
2191                 if (snd_pcm_format_descriptions[format] &&
2192                     strcasecmp(name, snd_pcm_format_descriptions[format]) == 0) {
2193                         return format;
2194                 }
2195         }
2196         return SND_PCM_FORMAT_UNKNOWN;
2197 }
2198
2199 /**
2200  * \brief get name of PCM sample subformat
2201  * \param subformat PCM sample subformat
2202  * \return ascii name of PCM sample subformat
2203  */
2204 const char *snd_pcm_subformat_name(const snd_pcm_subformat_t subformat)
2205 {
2206         if (subformat > SND_PCM_SUBFORMAT_LAST)
2207                 return NULL;
2208         return snd_pcm_subformat_names[subformat];
2209 }
2210
2211 /**
2212  * \brief get description of PCM sample subformat
2213  * \param subformat PCM sample subformat
2214  * \return ascii description of PCM sample subformat
2215  */
2216 const char *snd_pcm_subformat_description(const snd_pcm_subformat_t subformat)
2217 {
2218         if (subformat > SND_PCM_SUBFORMAT_LAST)
2219                 return NULL;
2220         return snd_pcm_subformat_descriptions[subformat];
2221 }
2222
2223 /**
2224  * \brief get PCM sample subformat from name
2225  * \param name PCM sample subformat name (case insensitive)
2226  * \return PCM sample subformat
2227  */
2228 snd_pcm_subformat_t snd_pcm_subformat_value(const char* name)
2229 {
2230         snd_pcm_subformat_t subformat;
2231
2232         for (subformat = 0; subformat <= SND_PCM_SUBFORMAT_LAST; subformat++) {
2233                 if (snd_pcm_subformat_names[subformat] &&
2234                     !strcasecmp(name, snd_pcm_subformat_names[subformat]))
2235                         return subformat;
2236         }
2237
2238         for (subformat = 0; subformat <= SND_PCM_SUBFORMAT_LAST; subformat++) {
2239                 if (snd_pcm_subformat_descriptions[subformat] &&
2240                     !strcasecmp(name, snd_pcm_subformat_descriptions[subformat]))
2241                         return subformat;
2242         }
2243
2244         return SND_PCM_SUBFORMAT_UNKNOWN;
2245 }
2246
2247 /**
2248  * \brief (DEPRECATED) get name of PCM start mode setting
2249  * \param mode PCM start mode
2250  * \return ascii name of PCM start mode setting
2251  */
2252 const char *snd_pcm_start_mode_name(snd_pcm_start_t mode)
2253 {
2254         if (mode > SND_PCM_START_LAST)
2255                 return NULL;
2256         return snd_pcm_start_mode_names[mode];
2257 }
2258
2259 #ifndef DOC_HIDDEN
2260 link_warning(snd_pcm_start_mode_name, "Warning: start_mode is deprecated, consider to use start_threshold");
2261 #endif
2262
2263 /**
2264  * \brief (DEPRECATED) get name of PCM xrun mode setting
2265  * \param mode PCM xrun mode
2266  * \return ascii name of PCM xrun mode setting
2267  */
2268 const char *snd_pcm_xrun_mode_name(snd_pcm_xrun_t mode)
2269 {
2270         if (mode > SND_PCM_XRUN_LAST)
2271                 return NULL;
2272         return snd_pcm_xrun_mode_names[mode];
2273 }
2274
2275 #ifndef DOC_HIDDEN
2276 link_warning(snd_pcm_xrun_mode_name, "Warning: xrun_mode is deprecated, consider to use stop_threshold");
2277 #endif
2278
2279 /**
2280  * \brief get name of PCM tstamp mode setting
2281  * \param mode PCM tstamp mode
2282  * \return ascii name of PCM tstamp mode setting
2283  */
2284 const char *snd_pcm_tstamp_mode_name(const snd_pcm_tstamp_t mode)
2285 {
2286         if (mode > SND_PCM_TSTAMP_LAST)
2287                 return NULL;
2288         return snd_pcm_tstamp_mode_names[mode];
2289 }
2290
2291 /**
2292  * \brief get name of PCM tstamp type setting
2293  * \param type PCM tstamp type
2294  * \return ascii name of PCM tstamp type setting
2295  */
2296 const char *snd_pcm_tstamp_type_name(snd_pcm_tstamp_type_t type)
2297 {
2298         if (type > SND_PCM_TSTAMP_TYPE_LAST)
2299                 return NULL;
2300         return snd_pcm_tstamp_type_names[type];
2301 }
2302
2303 /**
2304  * \brief get name of PCM state
2305  * \param state PCM state
2306  * \return ascii name of PCM state
2307  */
2308 const char *snd_pcm_state_name(const snd_pcm_state_t state)
2309 {
2310         if (state > SND_PCM_STATE_LAST)
2311                 return NULL;
2312         return snd_pcm_state_names[state];
2313 }
2314
2315 /**
2316  * \brief get name of PCM type
2317  * \param type PCM type
2318  * \return ascii name of PCM type
2319  */
2320 #ifndef DOXYGEN
2321 EXPORT_SYMBOL const char *INTERNAL(snd_pcm_type_name)(snd_pcm_type_t type)
2322 #else
2323 const char *snd_pcm_type_name(snd_pcm_type_t type)
2324 #endif
2325 {
2326         if (type > SND_PCM_TYPE_LAST)
2327                 return NULL;
2328         return snd_pcm_type_names[type];
2329 }
2330 use_default_symbol_version(__snd_pcm_type_name, snd_pcm_type_name, ALSA_0.9.0);
2331
2332 /**
2333  * \brief Dump current hardware setup for PCM
2334  * \param pcm PCM handle
2335  * \param out Output handle
2336  * \return 0 on success otherwise a negative error code
2337  */
2338 int snd_pcm_dump_hw_setup(snd_pcm_t *pcm, snd_output_t *out)
2339 {
2340         assert(pcm);
2341         assert(out);
2342         if (CHECK_SANITY(! pcm->setup)) {
2343                 snd_check(PCM, "PCM not set up");
2344                 return -EIO;
2345         }
2346         snd_output_printf(out, "  stream       : %s\n", snd_pcm_stream_name(pcm->stream));
2347         snd_output_printf(out, "  access       : %s\n", snd_pcm_access_name(pcm->access));
2348         snd_output_printf(out, "  format       : %s\n", snd_pcm_format_name(pcm->format));
2349         snd_output_printf(out, "  subformat    : %s\n", snd_pcm_subformat_name(pcm->subformat));
2350         snd_output_printf(out, "  channels     : %u\n", pcm->channels);
2351         snd_output_printf(out, "  rate         : %u\n", pcm->rate);
2352         snd_output_printf(out, "  exact rate   : %g (%u/%u)\n",
2353                           (pcm->rate_den ? ((double) pcm->rate_num / pcm->rate_den) : 0.0),
2354                           pcm->rate_num, pcm->rate_den);
2355         snd_output_printf(out, "  msbits       : %u\n", pcm->msbits);
2356         snd_output_printf(out, "  buffer_size  : %lu\n", pcm->buffer_size);
2357         snd_output_printf(out, "  period_size  : %lu\n", pcm->period_size);
2358         snd_output_printf(out, "  period_time  : %u\n", pcm->period_time);
2359         return 0;
2360 }
2361
2362 /**
2363  * \brief Dump current software setup for PCM
2364  * \param pcm PCM handle
2365  * \param out Output handle
2366  * \return 0 on success otherwise a negative error code
2367  */
2368 int snd_pcm_dump_sw_setup(snd_pcm_t *pcm, snd_output_t *out)
2369 {
2370         assert(pcm);
2371         assert(out);
2372         if (CHECK_SANITY(! pcm->setup)) {
2373                 snd_check(PCM, "PCM not set up");
2374                 return -EIO;
2375         }
2376         snd_output_printf(out, "  tstamp_mode  : %s\n", snd_pcm_tstamp_mode_name(pcm->tstamp_mode));
2377         snd_output_printf(out, "  tstamp_type  : %s\n", snd_pcm_tstamp_type_name(pcm->tstamp_type));
2378         snd_output_printf(out, "  period_step  : %d\n", pcm->period_step);
2379         snd_output_printf(out, "  avail_min    : %ld\n", pcm->avail_min);
2380         snd_output_printf(out, "  period_event : %i\n", pcm->period_event);
2381         snd_output_printf(out, "  start_threshold  : %ld\n", pcm->start_threshold);
2382         snd_output_printf(out, "  stop_threshold   : %ld\n", pcm->stop_threshold);
2383         snd_output_printf(out, "  silence_threshold: %ld\n", pcm->silence_threshold);
2384         snd_output_printf(out, "  silence_size : %ld\n", pcm->silence_size);
2385         snd_output_printf(out, "  boundary     : %ld\n", pcm->boundary);
2386         return 0;
2387 }
2388
2389 /**
2390  * \brief Dump current setup (hardware and software) for PCM
2391  * \param pcm PCM handle
2392  * \param out Output handle
2393  * \return 0 on success otherwise a negative error code
2394  */
2395 int snd_pcm_dump_setup(snd_pcm_t *pcm, snd_output_t *out)
2396 {
2397         snd_pcm_dump_hw_setup(pcm, out);
2398         snd_pcm_dump_sw_setup(pcm, out);
2399         return 0;
2400 }
2401
2402 /**
2403  * \brief Dump status
2404  * \param status Status container
2405  * \param out Output handle
2406  * \return 0 on success otherwise a negative error code
2407  */
2408 int snd_pcm_status_dump(snd_pcm_status_t *status, snd_output_t *out)
2409 {
2410         assert(status);
2411         snd_output_printf(out, "  state       : %s\n", snd_pcm_state_name((snd_pcm_state_t) status->state));
2412         snd_output_printf(out, "  trigger_time: %ld.%06ld\n",
2413                           status->trigger_tstamp.tv_sec,
2414                           status->trigger_tstamp.tv_nsec / 1000);
2415         snd_output_printf(out, "  tstamp      : %ld.%06ld\n",
2416                 status->tstamp.tv_sec, status->tstamp.tv_nsec / 1000);
2417         snd_output_printf(out, "  delay       : %ld\n", (long)status->delay);
2418         snd_output_printf(out, "  avail       : %ld\n", (long)status->avail);
2419         snd_output_printf(out, "  avail_max   : %ld\n", (long)status->avail_max);
2420         return 0;
2421 }
2422
2423 /**
2424  * \brief Dump PCM info
2425  * \param pcm PCM handle
2426  * \param out Output handle
2427  * \return 0 on success otherwise a negative error code
2428  */
2429 int snd_pcm_dump(snd_pcm_t *pcm, snd_output_t *out)
2430 {
2431         int err = 0;
2432
2433         assert(pcm);
2434         assert(out);
2435         if (pcm->ops->dump)
2436                 pcm->ops->dump(pcm->op_arg, out);
2437         else
2438                 err = -ENOSYS;
2439         return err;
2440 }
2441
2442 /**
2443  * \brief Convert bytes in frames for a PCM
2444  * \param pcm PCM handle
2445  * \param bytes quantity in bytes
2446  * \return quantity expressed in frames
2447  */
2448 snd_pcm_sframes_t snd_pcm_bytes_to_frames(snd_pcm_t *pcm, ssize_t bytes)
2449 {
2450         assert(pcm);
2451         if (CHECK_SANITY(! pcm->setup)) {
2452                 snd_check(PCM, "PCM not set up");
2453                 return -EIO;
2454         }
2455         return bytes * 8 / pcm->frame_bits;
2456 }
2457
2458 /**
2459  * \brief Convert frames in bytes for a PCM
2460  * \param pcm PCM handle
2461  * \param frames quantity in frames
2462  * \return quantity expressed in bytes
2463  */
2464 ssize_t snd_pcm_frames_to_bytes(snd_pcm_t *pcm, snd_pcm_sframes_t frames)
2465 {
2466         assert(pcm);
2467         if (CHECK_SANITY(! pcm->setup)) {
2468                 snd_check(PCM, "PCM not set up");
2469                 return -EIO;
2470         }
2471         return frames * pcm->frame_bits / 8;
2472 }
2473
2474 /**
2475  * \brief Convert bytes in samples for a PCM
2476  * \param pcm PCM handle
2477  * \param bytes quantity in bytes
2478  * \return quantity expressed in samples
2479  */
2480 long snd_pcm_bytes_to_samples(snd_pcm_t *pcm, ssize_t bytes)
2481 {
2482         assert(pcm);
2483         if (CHECK_SANITY(! pcm->setup)) {
2484                 snd_check(PCM, "PCM not set up");
2485                 return -EIO;
2486         }
2487         return bytes * 8 / pcm->sample_bits;
2488 }
2489
2490 /**
2491  * \brief Convert samples in bytes for a PCM
2492  * \param pcm PCM handle
2493  * \param samples quantity in samples
2494  * \return quantity expressed in bytes
2495  */
2496 ssize_t snd_pcm_samples_to_bytes(snd_pcm_t *pcm, long samples)
2497 {
2498         assert(pcm);
2499         if (CHECK_SANITY(! pcm->setup)) {
2500                 snd_check(PCM, "PCM not set up");
2501                 return -EIO;
2502         }
2503         return samples * pcm->sample_bits / 8;
2504 }
2505
2506 /**
2507  * \brief Add an async handler for a PCM
2508  * \param handler Returned handler handle
2509  * \param pcm PCM handle
2510  * \param callback Callback function
2511  * \param private_data Callback private data
2512  * \return 0 otherwise a negative error code on failure
2513  *
2514  * The asynchronous callback is called when period boundary elapses.
2515  */
2516 int snd_async_add_pcm_handler(snd_async_handler_t **handler, snd_pcm_t *pcm,
2517                               snd_async_callback_t callback, void *private_data)
2518 {
2519         int err;
2520         int was_empty;
2521         snd_async_handler_t *h;
2522         err = snd_async_add_handler(&h, _snd_pcm_async_descriptor(pcm),
2523                                     callback, private_data);
2524         if (err < 0)
2525                 return err;
2526         h->type = SND_ASYNC_HANDLER_PCM;
2527         h->u.pcm = pcm;
2528         was_empty = list_empty(&pcm->async_handlers);
2529         list_add_tail(&h->hlist, &pcm->async_handlers);
2530         if (was_empty) {
2531                 err = snd_pcm_async(pcm, snd_async_handler_get_signo(h), getpid());
2532                 if (err < 0) {
2533                         snd_async_del_handler(h);
2534                         return err;
2535                 }
2536         }
2537         *handler = h;
2538         return 0;
2539 }
2540
2541 /**
2542  * \brief Return PCM handle related to an async handler
2543  * \param handler Async handler handle
2544  * \return PCM handle
2545  */
2546 snd_pcm_t *snd_async_handler_get_pcm(snd_async_handler_t *handler)
2547 {
2548         if (handler->type != SND_ASYNC_HANDLER_PCM) {
2549                 snd_check(PCM, "invalid handler type %d", handler->type);
2550                 return NULL;
2551         }
2552         return handler->u.pcm;
2553 }
2554
2555 static const char *const build_in_pcms[] = {
2556         "adpcm", "alaw", "copy", "dmix", "file", "hooks", "hw", "ladspa", "lfloat",
2557         "linear", "meter", "mulaw", "multi", "null", "empty", "plug", "rate", "route", "share",
2558         "shm", "dsnoop", "dshare", "asym", "iec958", "softvol", "mmap_emul",
2559         NULL
2560 };
2561
2562 static int snd_pcm_open_conf(snd_pcm_t **pcmp, const char *name,
2563                              snd_config_t *pcm_root, snd_config_t *pcm_conf,
2564                              snd_pcm_stream_t stream, int mode)
2565 {
2566         const char *str;
2567         char *buf = NULL, *buf1 = NULL;
2568         int err;
2569         snd_config_t *conf, *type_conf = NULL, *tmp;
2570         snd_config_iterator_t i, next;
2571         const char *id;
2572         const char *lib = NULL, *open_name = NULL;
2573         int (*open_func)(snd_pcm_t **, const char *,
2574                          snd_config_t *, snd_config_t *,
2575                          snd_pcm_stream_t, int) = NULL;
2576 #ifndef PIC
2577         extern void *snd_pcm_open_symbols(void);
2578 #endif
2579         if (snd_config_get_type(pcm_conf) != SND_CONFIG_TYPE_COMPOUND) {
2580                 char *val;
2581                 if (snd_config_get_id(pcm_conf, &id) < 0)
2582                         id = NULL;
2583                 if (snd_config_get_ascii(pcm_conf, &val) < 0)
2584                         val = NULL;
2585                 snd_error(PCM, "Invalid type for PCM %s%sdefinition (id: %s, value: %s)", name ? name : "", name ? " " : "", id, val);
2586                 free(val);
2587                 return -EINVAL;
2588         }
2589         err = snd_config_search(pcm_conf, "type", &conf);
2590         if (err < 0) {
2591                 snd_error(PCM, "type is not defined");
2592                 return err;
2593         }
2594         err = snd_config_get_id(conf, &id);
2595         if (err < 0) {
2596                 snd_error(PCM, "unable to get id");
2597                 return err;
2598         }
2599         err = snd_config_get_string(conf, &str);
2600         if (err < 0) {
2601                 snd_error(PCM, "Invalid type for %s", id);
2602                 return err;
2603         }
2604         err = snd_config_search_definition(pcm_root, "pcm_type", str, &type_conf);
2605         if (err >= 0) {
2606                 if (snd_config_get_type(type_conf) != SND_CONFIG_TYPE_COMPOUND) {
2607                         snd_error(PCM, "Invalid type for PCM type %s definition", str);
2608                         err = -EINVAL;
2609                         goto _err;
2610                 }
2611                 snd_config_for_each(i, next, type_conf) {
2612                         snd_config_t *n = snd_config_iterator_entry(i);
2613                         const char *id;
2614                         if (snd_config_get_id(n, &id) < 0)
2615                                 continue;
2616                         if (strcmp(id, "comment") == 0)
2617                                 continue;
2618                         if (strcmp(id, "lib") == 0) {
2619                                 err = snd_config_get_string(n, &lib);
2620                                 if (err < 0) {
2621                                         snd_error(PCM, "Invalid type for %s", id);
2622                                         goto _err;
2623                                 }
2624                                 continue;
2625                         }
2626                         if (strcmp(id, "open") == 0) {
2627                                 err = snd_config_get_string(n, &open_name);
2628                                 if (err < 0) {
2629                                         snd_error(PCM, "Invalid type for %s", id);
2630                                         goto _err;
2631                                 }
2632                                 continue;
2633                         }
2634                         snd_error(PCM, "Unknown field %s", id);
2635                         err = -EINVAL;
2636                         goto _err;
2637                 }
2638         }
2639         if (!open_name) {
2640                 buf = malloc(strlen(str) + 32);
2641                 if (buf == NULL) {
2642                         err = -ENOMEM;
2643                         goto _err;
2644                 }
2645                 open_name = buf;
2646                 sprintf(buf, "_snd_pcm_%s_open", str);
2647         }
2648         if (!lib) {
2649                 const char *const *build_in = build_in_pcms;
2650                 while (*build_in) {
2651                         if (!strcmp(*build_in, str))
2652                                 break;
2653                         build_in++;
2654                 }
2655                 if (*build_in == NULL) {
2656                         buf1 = malloc(strlen(str) + 32);
2657                         if (buf1 == NULL) {
2658                                 err = -ENOMEM;
2659                                 goto _err;
2660                         }
2661                         lib = buf1;
2662                         sprintf(buf1, "libasound_module_pcm_%s.so", str);
2663                 }
2664         }
2665 #ifndef PIC
2666         snd_pcm_open_symbols(); /* this call is for static linking only */
2667 #endif
2668         open_func = snd_dlobj_cache_get(lib, open_name,
2669                         SND_DLSYM_VERSION(SND_PCM_DLSYM_VERSION), 1);
2670         if (open_func) {
2671                 err = open_func(pcmp, name, pcm_root, pcm_conf, stream, mode);
2672                 if (err >= 0) {
2673                         if ((*pcmp)->open_func) {
2674                                 /* only init plugin (like empty, asym) */
2675                                 snd_dlobj_cache_put(open_func);
2676                         } else {
2677                                 (*pcmp)->open_func = open_func;
2678                         }
2679                         err = 0;
2680                 } else {
2681                         snd_dlobj_cache_put(open_func);
2682                 }
2683         } else {
2684                 err = -ENXIO;
2685         }
2686         if (err >= 0) {
2687                 err = snd_config_search(pcm_root, "defaults.pcm.compat", &tmp);
2688                 if (err >= 0) {
2689                         long i;
2690                         if (snd_config_get_integer(tmp, &i) >= 0) {
2691                                 if (i > 0)
2692                                         (*pcmp)->compat = 1;
2693                         }
2694                 } else {
2695                         char *str = getenv("LIBASOUND_COMPAT");
2696                         if (str && *str)
2697                                 (*pcmp)->compat = 1;
2698                 }
2699                 err = snd_config_search(pcm_root, "defaults.pcm.minperiodtime", &tmp);
2700                 if (err >= 0)
2701                         snd_config_get_integer(tmp, &(*pcmp)->minperiodtime);
2702                 err = 0;
2703         }
2704        _err:
2705         if (type_conf)
2706                 snd_config_delete(type_conf);
2707         free(buf);
2708         free(buf1);
2709         return err;
2710 }
2711
2712 static int snd_pcm_open_noupdate(snd_pcm_t **pcmp, snd_config_t *root,
2713                                  const char *name, snd_pcm_stream_t stream,
2714                                  int mode, int hop)
2715 {
2716         int err;
2717         snd_config_t *pcm_conf;
2718         const char *str;
2719
2720         err = snd_config_search_definition(root, "pcm", name, &pcm_conf);
2721         if (err < 0) {
2722                 snd_error(PCM, "Unknown PCM %s", name);
2723                 return err;
2724         }
2725         if (snd_config_get_string(pcm_conf, &str) >= 0)
2726                 err = snd_pcm_open_noupdate(pcmp, root, str, stream, mode,
2727                                             hop + 1);
2728         else {
2729                 snd_config_set_hop(pcm_conf, hop);
2730                 err = snd_pcm_open_conf(pcmp, name, root, pcm_conf, stream, mode);
2731         }
2732         snd_config_delete(pcm_conf);
2733         return err;
2734 }
2735
2736 /**
2737  * \brief Opens a PCM
2738  * \param pcmp Returned PCM handle
2739  * \param name ASCII identifier of the PCM handle
2740  * \param stream Wanted stream
2741  * \param mode Open mode (see #SND_PCM_NONBLOCK, #SND_PCM_ASYNC)
2742  * \return 0 on success otherwise a negative error code
2743  */
2744 int snd_pcm_open(snd_pcm_t **pcmp, const char *name,
2745                  snd_pcm_stream_t stream, int mode)
2746 {
2747         snd_config_t *top;
2748         int err;
2749
2750         assert(pcmp && name);
2751         if (_snd_is_ucm_device(name)) {
2752                 name = uc_mgr_alibcfg_by_device(&top, name);
2753                 if (name == NULL)
2754                         return -ENODEV;
2755         } else {
2756                 err = snd_config_update_ref(&top);
2757                 if (err < 0)
2758                         return err;
2759         }
2760         err = snd_pcm_open_noupdate(pcmp, top, name, stream, mode, 0);
2761         snd_config_unref(top);
2762         return err;
2763 }
2764
2765 /**
2766  * \brief Opens a PCM using local configuration
2767  * \param pcmp Returned PCM handle
2768  * \param name ASCII identifier of the PCM handle
2769  * \param stream Wanted stream
2770  * \param mode Open mode (see #SND_PCM_NONBLOCK, #SND_PCM_ASYNC)
2771  * \param lconf Local configuration
2772  * \return 0 on success otherwise a negative error code
2773  */
2774 int snd_pcm_open_lconf(snd_pcm_t **pcmp, const char *name,
2775                        snd_pcm_stream_t stream, int mode,
2776                        snd_config_t *lconf)
2777 {
2778         assert(pcmp && name && lconf);
2779         return snd_pcm_open_noupdate(pcmp, lconf, name, stream, mode, 0);
2780 }
2781
2782 /**
2783  * \brief Opens a fallback PCM
2784  * \param pcmp Returned PCM handle
2785  * \param root Configuration root
2786  * \param name ASCII identifier of the PCM handle
2787  * \param orig_name The original ASCII name
2788  * \param stream Wanted stream
2789  * \param mode Open mode (see #SND_PCM_NONBLOCK, #SND_PCM_ASYNC)
2790  * \return 0 on success otherwise a negative error code
2791  */
2792 int snd_pcm_open_fallback(snd_pcm_t **pcmp, snd_config_t *root,
2793                           const char *name, const char *orig_name,
2794                           snd_pcm_stream_t stream, int mode)
2795 {
2796         int err;
2797         assert(pcmp && name && root);
2798         err = snd_pcm_open_noupdate(pcmp, root, name, stream, mode, 0);
2799         if (err >= 0) {
2800                 free((*pcmp)->name);
2801                 (*pcmp)->name = orig_name ? strdup(orig_name) : NULL;
2802         }
2803         return err;
2804 }
2805
2806 #ifndef DOC_HIDDEN
2807 int snd_pcm_new(snd_pcm_t **pcmp, snd_pcm_type_t type, const char *name,
2808                 snd_pcm_stream_t stream, int mode)
2809 {
2810         snd_pcm_t *pcm;
2811 #ifdef THREAD_SAFE_API
2812         pthread_mutexattr_t attr;
2813 #endif
2814
2815         pcm = calloc(1, sizeof(*pcm));
2816         if (!pcm)
2817                 return -ENOMEM;
2818         pcm->type = type;
2819         if (name)
2820                 pcm->name = strdup(name);
2821         pcm->stream = stream;
2822         pcm->mode = mode;
2823         pcm->poll_fd_count = 1;
2824         pcm->poll_fd = -1;
2825         pcm->op_arg = pcm;
2826         pcm->fast_op_arg = pcm;
2827         INIT_LIST_HEAD(&pcm->async_handlers);
2828 #ifdef THREAD_SAFE_API
2829         pthread_mutexattr_init(&attr);
2830 #ifdef HAVE_PTHREAD_MUTEX_RECURSIVE
2831         pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
2832 #endif
2833         pthread_mutex_init(&pcm->lock, &attr);
2834         pthread_mutexattr_destroy(&attr);
2835         /* use locking as default;
2836          * each plugin may suppress this in its open call
2837          */
2838         pcm->need_lock = 1;
2839         if (mode & SND_PCM_ASYNC) {
2840                 /* async handler may lead to a deadlock; suppose no MT */
2841                 pcm->lock_enabled = 0;
2842         } else {
2843                 /* set lock_enabled field depending on $LIBASOUND_THREAD_SAFE */
2844                 static int do_lock_enable = -1; /* uninitialized */
2845
2846                 /* evaluate env var only once at the first open for consistency */
2847                 if (do_lock_enable == -1) {
2848                         char *p = getenv("LIBASOUND_THREAD_SAFE");
2849                         do_lock_enable = !p || *p != '0';
2850                 }
2851                 pcm->lock_enabled = do_lock_enable;
2852         }
2853 #endif
2854         *pcmp = pcm;
2855         return 0;
2856 }
2857
2858 int snd_pcm_free(snd_pcm_t *pcm)
2859 {
2860         assert(pcm);
2861         free(pcm->name);
2862         free(pcm->hw.link_dst);
2863         free(pcm->appl.link_dst);
2864         snd_dlobj_cache_put(pcm->open_func);
2865 #ifdef THREAD_SAFE_API
2866         pthread_mutex_destroy(&pcm->lock);
2867 #endif
2868         free(pcm);
2869         return 0;
2870 }
2871
2872 int snd_pcm_open_named_slave(snd_pcm_t **pcmp, const char *name,
2873                              snd_config_t *root,
2874                              snd_config_t *conf, snd_pcm_stream_t stream,
2875                              int mode, snd_config_t *parent_conf)
2876 {
2877         const char *str;
2878         int hop;
2879
2880         if ((hop = snd_config_check_hop(parent_conf)) < 0)
2881                 return hop;
2882         if (snd_config_get_string(conf, &str) >= 0)
2883                 return snd_pcm_open_noupdate(pcmp, root, str, stream, mode,
2884                                              hop + 1);
2885         return snd_pcm_open_conf(pcmp, name, root, conf, stream, mode);
2886 }
2887 #endif
2888
2889 /**
2890  * \brief Wait for a PCM to become ready
2891  * \param pcm PCM handle
2892  * \param timeout maximum time in milliseconds to wait,
2893  *        a -1 value means infinity (SND_PCM_WAIT_INFINITE),
2894  *             see also SND_PCM_WAIT_IO and SND_PCM_WAIT_DRAIN
2895  * \return a positive value on success otherwise a negative error code
2896  *         (-EPIPE for the xrun and -ESTRPIPE for the suspended status,
2897  *          others for general errors)
2898  * \retval 0 timeout occurred
2899  * \retval 1 PCM stream is ready for I/O
2900  *
2901  * The function is thread-safe when built with the proper option.
2902  */
2903 int snd_pcm_wait(snd_pcm_t *pcm, int timeout)
2904 {
2905         int err;
2906
2907         __snd_pcm_lock(pcm->fast_op_arg); /* forced lock */
2908         err = __snd_pcm_wait_in_lock(pcm, timeout);
2909         __snd_pcm_unlock(pcm->fast_op_arg);
2910         return err;
2911 }
2912
2913 #ifndef DOC_HIDDEN
2914 /* locked version */
2915 int __snd_pcm_wait_in_lock(snd_pcm_t *pcm, int timeout)
2916 {
2917         int err;
2918
2919         /* NOTE: avail_min check can be skipped during draining */
2920         if (__snd_pcm_state(pcm) != SND_PCM_STATE_DRAINING &&
2921             !snd_pcm_may_wait_for_avail_min(pcm, snd_pcm_mmap_avail(pcm))) {
2922                 /* check more precisely */
2923                 err = pcm_state_to_error(__snd_pcm_state(pcm));
2924                 return err < 0 ? err : 1;
2925         }
2926         return snd_pcm_wait_nocheck(pcm, timeout);
2927 }
2928
2929 static int __snd_pcm_wait_io_timeout(snd_pcm_t *pcm)
2930 {
2931         int timeout;
2932
2933         /* period size is the time boundary */
2934         timeout = (pcm->period_size * 1000ULL) / pcm->rate;
2935         /* should not happen */
2936         if (timeout < 0)
2937                 timeout = 0;
2938         /* add extra time of 200 milliseconds */
2939         timeout += 200;
2940         return timeout;
2941 }
2942
2943 static int __snd_pcm_wait_drain_timeout(snd_pcm_t *pcm)
2944 {
2945         int timeout;
2946
2947         /* for capture, there's no reason to wait, just one iteration */
2948         if (snd_pcm_stream(pcm) == SND_PCM_STREAM_CAPTURE)
2949                 return 0;
2950         /* result is in milliseconds */
2951         timeout = (snd_pcm_mmap_playback_delay(pcm) * 1000LL) / pcm->rate;
2952         /* should not happen */
2953         if (timeout < 0)
2954                 timeout = 0;
2955         /* add extra time of 200 milliseconds */
2956         timeout += 200;
2957         return timeout;
2958 }
2959
2960 /*
2961  * like snd_pcm_wait() but doesn't check mmap_avail before calling poll()
2962  *
2963  * used in drain code in some plugins
2964  *
2965  * This function is called inside pcm lock.
2966  */
2967 int snd_pcm_wait_nocheck(snd_pcm_t *pcm, int timeout)
2968 {
2969         struct pollfd *pfd;
2970         unsigned short revents = 0;
2971         int npfds, err, err_poll;
2972
2973         npfds = __snd_pcm_poll_descriptors_count(pcm);
2974         if (npfds <= 0 || npfds >= 16) {
2975                 snd_error(PCM, "Invalid poll_fds %d", npfds);
2976                 return -EIO;
2977         }
2978         pfd = alloca(sizeof(*pfd) * npfds);
2979         err = __snd_pcm_poll_descriptors(pcm, pfd, npfds);
2980         if (err < 0)
2981                 return err;
2982         if (err != npfds) {
2983                 snd_check(PCM, "invalid poll descriptors %d", err);
2984                 return -EIO;
2985         }
2986         if (timeout == SND_PCM_WAIT_IO)
2987                 timeout = __snd_pcm_wait_io_timeout(pcm);
2988         else if (timeout == SND_PCM_WAIT_DRAIN)
2989                 timeout = __snd_pcm_wait_drain_timeout(pcm);
2990         else if (timeout < -1)
2991                 snd_check(PCM, "invalid snd_pcm_wait timeout argument %d", timeout);
2992         do {
2993                 __snd_pcm_unlock(pcm->fast_op_arg);
2994                 err_poll = poll(pfd, npfds, timeout);
2995                 __snd_pcm_lock(pcm->fast_op_arg);
2996                 if (err_poll < 0) {
2997                         if (errno == EINTR && !PCMINABORT(pcm) && !(pcm->mode & SND_PCM_EINTR))
2998                                 continue;
2999                         return -errno;
3000                 }
3001                 if (! err_poll)
3002                         break;
3003                 err = __snd_pcm_poll_revents(pcm, pfd, npfds, &revents);
3004                 if (err < 0)
3005                         return err;
3006                 if (revents & (POLLERR | POLLNVAL)) {
3007                         /* check more precisely */
3008                         err = pcm_state_to_error(__snd_pcm_state(pcm));
3009                         return err < 0 ? err : -EIO;
3010                 }
3011         } while (!(revents & (POLLIN | POLLOUT)));
3012 #if 0 /* very useful code to test poll related problems */
3013         {
3014                 snd_pcm_sframes_t avail_update;
3015                 __snd_pcm_hwsync(pcm);
3016                 avail_update = __snd_pcm_avail_update(pcm);
3017                 if (avail_update < (snd_pcm_sframes_t)pcm->avail_min) {
3018                         printf("*** snd_pcm_wait() FATAL ERROR!!!\n");
3019                         printf("avail_min = %li, avail_update = %li\n", pcm->avail_min, avail_update);
3020                 }
3021         }
3022 #endif
3023         return err_poll > 0 ? 1 : 0;
3024 }
3025 #endif
3026
3027 /**
3028  * \brief Return number of frames ready to be read (capture) / written (playback)
3029  * \param pcm PCM handle
3030  * \return a positive number of frames ready otherwise a negative
3031  * error code
3032  *
3033  * On capture does all the actions needed to transport to application
3034  * level all the ready frames across underlying layers.
3035  *
3036  * The position is not synced with hardware (driver) position in the sound
3037  * ring buffer in this function. This function is a light version of
3038  * #snd_pcm_avail() .
3039  *
3040  * Using this function is ideal after poll() or select() when audio
3041  * file descriptor made the event and when application expects just period
3042  * timing.
3043  *
3044  * Also this function might be called after #snd_pcm_delay() or
3045  * #snd_pcm_hwsync() functions to move private ring buffer pointers
3046  * in alsa-lib (the internal plugin chain).
3047  *
3048  * The function is thread-safe when built with the proper option.
3049  */
3050 snd_pcm_sframes_t snd_pcm_avail_update(snd_pcm_t *pcm)
3051 {
3052         snd_pcm_sframes_t result;
3053
3054         snd_pcm_lock(pcm->fast_op_arg);
3055         result = __snd_pcm_avail_update(pcm);
3056         snd_pcm_unlock(pcm->fast_op_arg);
3057         return result;
3058 }
3059
3060 /**
3061  * \brief Return number of frames ready to be read (capture) / written (playback)
3062  * \param pcm PCM handle
3063  * \return a positive number of frames ready otherwise a negative
3064  * error code
3065  *
3066  * On capture does all the actions needed to transport to application
3067  * level all the ready frames across underlying layers.
3068  *
3069  * The position is synced with hardware (driver) position in the sound
3070  * ring buffer in this functions.
3071  *
3072  * The function is thread-safe when built with the proper option.
3073  */
3074 snd_pcm_sframes_t snd_pcm_avail(snd_pcm_t *pcm)
3075 {
3076         int err;
3077         snd_pcm_sframes_t result;
3078
3079         assert(pcm);
3080         if (CHECK_SANITY(! pcm->setup)) {
3081                 snd_check(PCM, "PCM not set up");
3082                 return -EIO;
3083         }
3084         snd_pcm_lock(pcm->fast_op_arg);
3085         err = __snd_pcm_hwsync(pcm);
3086         if (err < 0)
3087                 result = err;
3088         else
3089                 result = __snd_pcm_avail_update(pcm);
3090         snd_pcm_unlock(pcm->fast_op_arg);
3091         return result;
3092 }
3093
3094 /**
3095  * \brief Combine snd_pcm_avail and snd_pcm_delay functions
3096  * \param pcm PCM handle
3097  * \param availp Number of available frames in the ring buffer
3098  * \param delayp Total I/O latency in frames
3099  * \return zero on success otherwise a negative error code
3100  *
3101  * The avail and delay values returned are in sync.
3102  *
3103  * The function is thread-safe when built with the proper option.
3104  */
3105 int snd_pcm_avail_delay(snd_pcm_t *pcm,
3106                         snd_pcm_sframes_t *availp,
3107                         snd_pcm_sframes_t *delayp)
3108 {
3109         snd_pcm_sframes_t sf;
3110         int err, ok = 0;
3111
3112         assert(pcm && availp && delayp);
3113         if (CHECK_SANITY(! pcm->setup)) {
3114                 snd_check(PCM, "PCM not set up");
3115                 return -EIO;
3116         }
3117         snd_pcm_lock(pcm->fast_op_arg);
3118         err = __snd_pcm_hwsync(pcm);
3119         if (err < 0)
3120                 goto unlock;
3121
3122         /*
3123          * Delay value is relative to avail, so we have to
3124          * loop to avoid reporting stale delay data.
3125          */
3126         while (1) {
3127                 sf = __snd_pcm_avail_update(pcm);
3128                 if (sf < 0) {
3129                         err = sf < INT_MIN ? -EOVERFLOW : (int)sf;
3130                         goto unlock;
3131                 }
3132                 if (ok && sf == *availp)
3133                         break;
3134                 *availp = sf;
3135                 err = __snd_pcm_delay(pcm, delayp);
3136                 if (err < 0)
3137                         goto unlock;
3138                 ok = 1;
3139         }
3140         err = 0;
3141  unlock:
3142         snd_pcm_unlock(pcm->fast_op_arg);
3143         return err;
3144 }
3145
3146 /**
3147  * \brief Silence an area
3148  * \param dst_area area specification
3149  * \param dst_offset offset in frames inside area
3150  * \param samples samples to silence
3151  * \param format PCM sample format
3152  * \return 0 on success otherwise a negative error code
3153  */
3154 int snd_pcm_area_silence(const snd_pcm_channel_area_t *dst_area, snd_pcm_uframes_t dst_offset,
3155                          unsigned int samples, snd_pcm_format_t format)
3156 {
3157         /* FIXME: sub byte resolution and odd dst_offset */
3158         char *dst;
3159         unsigned int dst_step;
3160         int width;
3161         uint64_t silence;
3162         if (!dst_area->addr)
3163                 return 0;
3164         dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
3165         width = snd_pcm_format_physical_width(format);
3166         silence = snd_pcm_format_silence_64(format);
3167         /*
3168          * Iterate copying silent sample for sample data aligned to 64 bit.
3169          * This is a fast path.
3170          */
3171         if (dst_area->step == (unsigned int) width &&
3172             width != 24 &&
3173             ((intptr_t)dst & 7) == 0) {
3174                 unsigned int dwords = samples * width / 64;
3175                 uint64_t *dstp = (uint64_t *)dst;
3176                 samples -= dwords * 64 / width;
3177                 while (dwords-- > 0)
3178                         *dstp++ = silence;
3179                 if (samples == 0)
3180                         return 0;
3181                 dst = (char *)dstp;
3182         }
3183         dst_step = dst_area->step / 8;
3184         switch (width) {
3185         case 4: {
3186                 uint8_t s0 = silence & 0xf0;
3187                 uint8_t s1 = silence & 0x0f;
3188                 int dstbit = dst_area->first % 8;
3189                 int dstbit_step = dst_area->step % 8;
3190                 while (samples-- > 0) {
3191                         if (dstbit) {
3192                                 *dst &= 0xf0;
3193                                 *dst |= s1;
3194                         } else {
3195                                 *dst &= 0x0f;
3196                                 *dst |= s0;
3197                         }
3198                         dst += dst_step;
3199                         dstbit += dstbit_step;
3200                         if (dstbit == 8) {
3201                                 dst++;
3202                                 dstbit = 0;
3203                         }
3204                 }
3205                 break;
3206         }
3207         case 8: {
3208                 uint8_t sil = silence;
3209                 while (samples-- > 0) {
3210                         *dst = sil;
3211                         dst += dst_step;
3212                 }
3213                 break;
3214         }
3215         case 16: {
3216                 uint16_t sil = silence;
3217                 while (samples-- > 0) {
3218                         *(uint16_t*)dst = sil;
3219                         dst += dst_step;
3220                 }
3221                 break;
3222         }
3223         case 24: {
3224                 while (samples-- > 0) {
3225 #ifdef SNDRV_LITTLE_ENDIAN
3226                         *(dst + 0) = silence >> 0;
3227                         *(dst + 1) = silence >> 8;
3228                         *(dst + 2) = silence >> 16;
3229 #else
3230                         *(dst + 2) = silence >> 0;
3231                         *(dst + 1) = silence >> 8;
3232                         *(dst + 0) = silence >> 16;
3233 #endif
3234                         dst += dst_step;
3235                 }
3236         }
3237                 break;
3238         case 32: {
3239                 uint32_t sil = silence;
3240                 while (samples-- > 0) {
3241                         *(uint32_t*)dst = sil;
3242                         dst += dst_step;
3243                 }
3244                 break;
3245         }
3246         case 64: {
3247                 while (samples-- > 0) {
3248                         *(uint64_t*)dst = silence;
3249                         dst += dst_step;
3250                 }
3251                 break;
3252         }
3253         default:
3254                 snd_check(PCM, "invalid format width %d", width);
3255                 return -EINVAL;
3256         }
3257         return 0;
3258 }
3259
3260 /**
3261  * \brief Silence one or more areas
3262  * \param dst_areas areas specification (one for each channel)
3263  * \param dst_offset offset in frames inside area
3264  * \param channels channels count
3265  * \param frames frames to silence
3266  * \param format PCM sample format
3267  * \return 0 on success otherwise a negative error code
3268  */
3269 int snd_pcm_areas_silence(const snd_pcm_channel_area_t *dst_areas, snd_pcm_uframes_t dst_offset,
3270                           unsigned int channels, snd_pcm_uframes_t frames, snd_pcm_format_t format)
3271 {
3272         int width = snd_pcm_format_physical_width(format);
3273         while (channels > 0) {
3274                 void *addr = dst_areas->addr;
3275                 unsigned int step = dst_areas->step;
3276                 const snd_pcm_channel_area_t *begin = dst_areas;
3277                 int channels1 = channels;
3278                 unsigned int chns = 0;
3279                 int err;
3280                 while (1) {
3281                         channels1--;
3282                         chns++;
3283                         dst_areas++;
3284                         if (channels1 == 0 ||
3285                             dst_areas->addr != addr ||
3286                             dst_areas->step != step ||
3287                             dst_areas->first != dst_areas[-1].first + width)
3288                                 break;
3289                 }
3290                 if (chns > 1 && chns * width == step) {
3291                         /* Collapse the areas */
3292                         snd_pcm_channel_area_t d;
3293                         d.addr = begin->addr;
3294                         d.first = begin->first;
3295                         d.step = width;
3296                         err = snd_pcm_area_silence(&d, dst_offset * chns, frames * chns, format);
3297                         channels -= chns;
3298                 } else {
3299                         err = snd_pcm_area_silence(begin, dst_offset, frames, format);
3300                         dst_areas = begin + 1;
3301                         channels--;
3302                 }
3303                 if (err < 0)
3304                         return err;
3305         }
3306         return 0;
3307 }
3308
3309
3310 /**
3311  * \brief Copy an area
3312  * \param dst_area destination area specification
3313  * \param dst_offset offset in frames inside destination area
3314  * \param src_area source area specification
3315  * \param src_offset offset in frames inside source area
3316  * \param samples samples to copy
3317  * \param format PCM sample format
3318  * \return 0 on success otherwise a negative error code
3319  */
3320 int snd_pcm_area_copy(const snd_pcm_channel_area_t *dst_area, snd_pcm_uframes_t dst_offset,
3321                       const snd_pcm_channel_area_t *src_area, snd_pcm_uframes_t src_offset,
3322                       unsigned int samples, snd_pcm_format_t format)
3323 {
3324         /* FIXME: sub byte resolution and odd dst_offset */
3325         const char *src;
3326         char *dst;
3327         int width;
3328         int src_step, dst_step;
3329         if (dst_area == src_area && dst_offset == src_offset)
3330                 return 0;
3331         if (!src_area->addr)
3332                 return snd_pcm_area_silence(dst_area, dst_offset, samples, format);
3333         src = snd_pcm_channel_area_addr(src_area, src_offset);
3334         if (!dst_area->addr)
3335                 return 0;
3336         dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
3337         width = snd_pcm_format_physical_width(format);
3338         if (width < 0)
3339                 return width;
3340         if (src_area->step == (unsigned int) width &&
3341             dst_area->step == (unsigned int) width) {
3342                 size_t bytes = samples * width / 8;
3343                 samples -= bytes * 8 / width;
3344                 assert(src < dst || src >= dst + bytes);
3345                 assert(dst < src || dst >= src + bytes);
3346                 memcpy(dst, src, bytes);
3347                 if (samples == 0)
3348                         return 0;
3349         }
3350         src_step = src_area->step / 8;
3351         dst_step = dst_area->step / 8;
3352         switch (width) {
3353         case 4: {
3354                 int srcbit = src_area->first % 8;
3355                 int srcbit_step = src_area->step % 8;
3356                 int dstbit = dst_area->first % 8;
3357                 int dstbit_step = dst_area->step % 8;
3358                 while (samples-- > 0) {
3359                         unsigned char srcval;
3360                         if (srcbit)
3361                                 srcval = *src & 0x0f;
3362                         else
3363                                 srcval = *src & 0xf0;
3364                         if (dstbit)
3365                                 *dst &= 0xf0;
3366                         else
3367                                 *dst &= 0x0f;
3368                         *dst |= srcval;
3369                         src += src_step;
3370                         srcbit += srcbit_step;
3371                         if (srcbit == 8) {
3372                                 src++;
3373                                 srcbit = 0;
3374                         }
3375                         dst += dst_step;
3376                         dstbit += dstbit_step;
3377                         if (dstbit == 8) {
3378                                 dst++;
3379                                 dstbit = 0;
3380                         }
3381                 }
3382                 break;
3383         }
3384         case 8: {
3385                 while (samples-- > 0) {
3386                         *dst = *src;
3387                         src += src_step;
3388                         dst += dst_step;
3389                 }
3390                 break;
3391         }
3392         case 16: {
3393                 while (samples-- > 0) {
3394                         *(uint16_t*)dst = *(const uint16_t*)src;
3395                         src += src_step;
3396                         dst += dst_step;
3397                 }
3398                 break;
3399         }
3400         case 24:
3401                 while (samples-- > 0) {
3402                         *(dst + 0) = *(src + 0);
3403                         *(dst + 1) = *(src + 1);
3404                         *(dst + 2) = *(src + 2);
3405                         src += src_step;
3406                         dst += dst_step;
3407                 }
3408                 break;
3409         case 32: {
3410                 while (samples-- > 0) {
3411                         *(uint32_t*)dst = *(const uint32_t*)src;
3412                         src += src_step;
3413                         dst += dst_step;
3414                 }
3415                 break;
3416         }
3417         case 64: {
3418                 while (samples-- > 0) {
3419                         *(uint64_t*)dst = *(const uint64_t*)src;
3420                         src += src_step;
3421                         dst += dst_step;
3422                 }
3423                 break;
3424         }
3425         default:
3426                 snd_check(PCM, "invalid format width %d", width);
3427                 return -EINVAL;
3428         }
3429         return 0;
3430 }
3431
3432 /**
3433  * \brief Copy one or more areas
3434  * \param dst_areas destination areas specification (one for each channel)
3435  * \param dst_offset offset in frames inside destination area
3436  * \param src_areas source areas specification (one for each channel)
3437  * \param src_offset offset in frames inside source area
3438  * \param channels channels count
3439  * \param frames frames to copy
3440  * \param format PCM sample format
3441  * \return 0 on success otherwise a negative error code
3442  */
3443 int snd_pcm_areas_copy(const snd_pcm_channel_area_t *dst_areas, snd_pcm_uframes_t dst_offset,
3444                        const snd_pcm_channel_area_t *src_areas, snd_pcm_uframes_t src_offset,
3445                        unsigned int channels, snd_pcm_uframes_t frames, snd_pcm_format_t format)
3446 {
3447         int width = snd_pcm_format_physical_width(format);
3448         assert(dst_areas);
3449         assert(src_areas);
3450         if (! channels) {
3451                 snd_check(PCM, "invalid channels %d", channels);
3452                 return -EINVAL;
3453         }
3454         if (! frames) {
3455                 snd_check(PCM, "invalid frames %ld", frames);
3456                 return -EINVAL;
3457         }
3458         while (channels > 0) {
3459                 unsigned int step = src_areas->step;
3460                 void *src_addr = src_areas->addr;
3461                 const snd_pcm_channel_area_t *src_start = src_areas;
3462                 void *dst_addr = dst_areas->addr;
3463                 const snd_pcm_channel_area_t *dst_start = dst_areas;
3464                 int channels1 = channels;
3465                 unsigned int chns = 0;
3466                 while (dst_areas->step == step) {
3467                         channels1--;
3468                         chns++;
3469                         src_areas++;
3470                         dst_areas++;
3471                         if (channels1 == 0 ||
3472                             src_areas->step != step ||
3473                             src_areas->addr != src_addr ||
3474                             dst_areas->addr != dst_addr ||
3475                             src_areas->first != src_areas[-1].first + width ||
3476                             dst_areas->first != dst_areas[-1].first + width)
3477                                 break;
3478                 }
3479                 if (chns > 1 && chns * width == step) {
3480                         if (src_offset != dst_offset ||
3481                             src_start->addr != dst_start->addr ||
3482                             src_start->first != dst_start->first) {
3483                                 /* Collapse the areas */
3484                                 snd_pcm_channel_area_t s, d;
3485                                 s.addr = src_start->addr;
3486                                 s.first = src_start->first;
3487                                 s.step = width;
3488                                 d.addr = dst_start->addr;
3489                                 d.first = dst_start->first;
3490                                 d.step = width;
3491                                 snd_pcm_area_copy(&d, dst_offset * chns,
3492                                                   &s, src_offset * chns,
3493                                                   frames * chns, format);
3494                         }
3495                         channels -= chns;
3496                 } else {
3497                         snd_pcm_area_copy(dst_start, dst_offset,
3498                                           src_start, src_offset,
3499                                           frames, format);
3500                         src_areas = src_start + 1;
3501                         dst_areas = dst_start + 1;
3502                         channels--;
3503                 }
3504         }
3505         return 0;
3506 }
3507
3508 /**
3509  * \brief Copy one or more areas
3510  * \param dst_channels destination areas specification (one for each channel)
3511  * \param dst_offset offset in frames inside destination area
3512  * \param dst_size size in frames of the destination buffer
3513  * \param src_channels source areas specification (one for each channel)
3514  * \param src_offset offset in frames inside source area
3515  * \param src_size size in frames of the source buffer
3516  * \param channels channels count
3517  * \param frames frames to copy
3518  * \param format PCM sample format
3519  * \return 0 on success otherwise a negative error code
3520  */
3521 int snd_pcm_areas_copy_wrap(const snd_pcm_channel_area_t *dst_channels,
3522                             snd_pcm_uframes_t dst_offset,
3523                             const snd_pcm_uframes_t dst_size,
3524                             const snd_pcm_channel_area_t *src_channels,
3525                             snd_pcm_uframes_t src_offset,
3526                             const snd_pcm_uframes_t src_size,
3527                             const unsigned int channels,
3528                             snd_pcm_uframes_t frames,
3529                             const snd_pcm_format_t format)
3530 {
3531         while (frames > 0) {
3532                 int err;
3533                 snd_pcm_uframes_t xfer = frames;
3534                 /* do not write above the destination buffer */
3535                 if ((dst_offset + xfer) > dst_size)
3536                         xfer = dst_size - dst_offset;
3537                 /* do not read from above the source buffer */
3538                 if ((src_offset + xfer) > src_size)
3539                         xfer = src_size - src_offset;
3540                 err = snd_pcm_areas_copy(dst_channels, dst_offset, src_channels,
3541                                          src_offset, channels, xfer, format);
3542                 if (err < 0)
3543                         return err;
3544
3545                 dst_offset += xfer;
3546                 if (dst_offset >= dst_size)
3547                         dst_offset = 0;
3548                 src_offset += xfer;
3549                 if (src_offset >= src_size)
3550                         src_offset = 0;
3551                 frames -= xfer;
3552         }
3553
3554         return 0;
3555 }
3556
3557 static void dump_one_param(snd_pcm_hw_params_t *params, unsigned int k, snd_output_t *out)
3558 {
3559         snd_output_printf(out, "%s: ", snd_pcm_hw_param_name(k));
3560         snd_pcm_hw_param_dump(params, k, out);
3561         snd_output_putc(out, '\n');
3562 }
3563
3564 /**
3565  * \brief Dump a PCM hardware configuration space
3566  * \param params Configuration space
3567  * \param out Output handle
3568  * \return 0 on success otherwise a negative error code
3569  */
3570 int snd_pcm_hw_params_dump(snd_pcm_hw_params_t *params, snd_output_t *out)
3571 {
3572         unsigned int k;
3573         for (k = SND_PCM_HW_PARAM_FIRST_MASK; k <= SND_PCM_HW_PARAM_LAST_MASK; k++)
3574                 dump_one_param(params, k, out);
3575         for (k = SND_PCM_HW_PARAM_FIRST_INTERVAL; k <= SND_PCM_HW_PARAM_LAST_INTERVAL; k++)
3576                 dump_one_param(params, k, out);
3577         return 0;
3578 }
3579
3580 /**
3581  * \brief Check if hardware supports sample-resolution mmap for given configuration
3582  * \param params Configuration space
3583  * \retval 0 Hardware doesn't support sample-resolution mmap
3584  * \retval 1 Hardware supports sample-resolution mmap
3585  *
3586  * This function should only be called when the configuration space
3587  * contains a single configuration. Call #snd_pcm_hw_params to choose
3588  * a single configuration from the configuration space.
3589  */
3590 int snd_pcm_hw_params_can_mmap_sample_resolution(const snd_pcm_hw_params_t *params)
3591 {
3592         assert(params);
3593         if (CHECK_SANITY(params->info == ~0U)) {
3594                 snd_check(PCM, "invalid PCM info field");
3595                 return 0; /* FIXME: should be a negative error? */
3596         }
3597         return !!(params->info & SNDRV_PCM_INFO_MMAP_VALID);
3598 }
3599
3600 /**
3601  * \brief Check if hardware does double buffering for start/stop for given configuration
3602  * \param params Configuration space
3603  * \retval 0 Hardware doesn't do double buffering for start/stop
3604  * \retval 1 Hardware does double buffering for start/stop
3605  *
3606  * This function should only be called when the configuration space
3607  * contains a single configuration. Call #snd_pcm_hw_params to choose
3608  * a single configuration from the configuration space.
3609  */
3610 int snd_pcm_hw_params_is_double(const snd_pcm_hw_params_t *params)
3611 {
3612         assert(params);
3613         if (CHECK_SANITY(params->info == ~0U)) {
3614                 snd_check(PCM, "invalid PCM info field");
3615                 return 0; /* FIXME: should be a negative error? */
3616         }
3617         return !!(params->info & SNDRV_PCM_INFO_DOUBLE);
3618 }
3619
3620 /**
3621  * \brief Check if hardware does double buffering for data transfers for given configuration
3622  * \param params Configuration space
3623  * \retval 0 Hardware doesn't do double buffering for data transfers
3624  * \retval 1 Hardware does double buffering for data transfers
3625  *
3626  * This function should only be called when the configuration space
3627  * contains a single configuration. Call #snd_pcm_hw_params to choose
3628  * a single configuration from the configuration space.
3629  */
3630 int snd_pcm_hw_params_is_batch(const snd_pcm_hw_params_t *params)
3631 {
3632         assert(params);
3633         if (CHECK_SANITY(params->info == ~0U)) {
3634                 snd_check(PCM, "invalid PCM info field");
3635                 return 0; /* FIXME: should be a negative error? */
3636         }
3637         return !!(params->info & SNDRV_PCM_INFO_BATCH);
3638 }
3639
3640 /**
3641  * \brief Check if hardware does block transfers for samples for given configuration
3642  * \param params Configuration space
3643  * \retval 0 Hardware doesn't block transfers
3644  * \retval 1 Hardware does block transfers
3645  *
3646  * This function should only be called when the configuration space
3647  * contains a single configuration. Call #snd_pcm_hw_params to choose
3648  * a single configuration from the configuration space.
3649  */
3650 int snd_pcm_hw_params_is_block_transfer(const snd_pcm_hw_params_t *params)
3651 {
3652         assert(params);
3653         if (CHECK_SANITY(params->info == ~0U)) {
3654                 snd_check(PCM, "invalid PCM info field");
3655                 return 0; /* FIXME: should be a negative error? */
3656         }
3657         return !!(params->info & SNDRV_PCM_INFO_BLOCK_TRANSFER);
3658 }
3659
3660 /**
3661  * \brief Check if timestamps are monotonic for given configuration
3662  * \param params Configuration space
3663  * \retval 0 Device doesn't do monotomic timestamps
3664  * \retval 1 Device does monotonic timestamps
3665  *
3666  * This function should only be called when the configuration space
3667  * contains a single configuration. Call #snd_pcm_hw_params to choose
3668  * a single configuration from the configuration space.
3669  */
3670 int snd_pcm_hw_params_is_monotonic(const snd_pcm_hw_params_t *params)
3671 {
3672         assert(params);
3673         if (CHECK_SANITY(params->info == ~0U)) {
3674                 snd_check(PCM, "invalid PCM info field");
3675                 return 0; /* FIXME: should be a negative error? */
3676         }
3677         return !!(params->info & SND_PCM_INFO_MONOTONIC);
3678 }
3679
3680 /**
3681  * \brief Check if hardware supports overrange detection
3682  * \param params Configuration space
3683  * \retval 0 Hardware doesn't support overrange detection
3684  * \retval 1 Hardware supports overrange detection
3685  *
3686  * This function should only be called when the configuration space
3687  * contains a single configuration. Call #snd_pcm_hw_params to choose
3688  * a single configuration from the configuration space.
3689  */
3690 int snd_pcm_hw_params_can_overrange(const snd_pcm_hw_params_t *params)
3691 {
3692         assert(params);
3693         if (CHECK_SANITY(params->info == ~0U)) {
3694                 snd_check(PCM, "invalid PCM info field");
3695                 return 0; /* FIXME: should be a negative error? */
3696         }
3697         return !!(params->info & SNDRV_PCM_INFO_OVERRANGE);
3698 }
3699
3700 /**
3701  * \brief Check if hardware supports pause
3702  * \param params Configuration space
3703  * \retval 0 Hardware doesn't support pause
3704  * \retval 1 Hardware supports pause
3705  *
3706  * This function should only be called when the configuration space
3707  * contains a single configuration. Call #snd_pcm_hw_params to choose
3708  * a single configuration from the configuration space.
3709  */
3710 int snd_pcm_hw_params_can_pause(const snd_pcm_hw_params_t *params)
3711 {
3712         assert(params);
3713         if (CHECK_SANITY(params->info == ~0U)) {
3714                 snd_check(PCM, "invalid PCM info field");
3715                 return 0; /* FIXME: should be a negative error? */
3716         }
3717         return !!(params->info & SNDRV_PCM_INFO_PAUSE);
3718 }
3719
3720 /**
3721  * \brief Check if hardware supports resume
3722  * \param params Configuration space
3723  * \retval 0 Hardware doesn't support resume
3724  * \retval 1 Hardware supports resume
3725  *
3726  * This function should only be called when the configuration space
3727  * contains a single configuration. Call #snd_pcm_hw_params to choose
3728  * a single configuration from the configuration space.
3729  */
3730 int snd_pcm_hw_params_can_resume(const snd_pcm_hw_params_t *params)
3731 {
3732         assert(params);
3733         if (CHECK_SANITY(params->info == ~0U)) {
3734                 snd_check(PCM, "invalid PCM info field");
3735                 return 0; /* FIXME: should be a negative error? */
3736         }
3737         return !!(params->info & SNDRV_PCM_INFO_RESUME);
3738 }
3739
3740 /**
3741  * \brief Check if hardware does half-duplex only
3742  * \param params Configuration space
3743  * \retval 0 Hardware doesn't do half-duplex
3744  * \retval 1 Hardware does half-duplex
3745  *
3746  * This function should only be called when the configuration space
3747  * contains a single configuration. Call #snd_pcm_hw_params to choose
3748  * a single configuration from the configuration space.
3749  */
3750 int snd_pcm_hw_params_is_half_duplex(const snd_pcm_hw_params_t *params)
3751 {
3752         assert(params);
3753         if (CHECK_SANITY(params->info == ~0U)) {
3754                 snd_check(PCM, "invalid PCM info field");
3755                 return 0; /* FIXME: should be a negative error? */
3756         }
3757         return !!(params->info & SNDRV_PCM_INFO_HALF_DUPLEX);
3758 }
3759
3760 /**
3761  * \brief Check if hardware does joint-duplex (playback and capture are somewhat correlated)
3762  * \param params Configuration space
3763  * \retval 0 Hardware doesn't do joint-duplex
3764  * \retval 1 Hardware does joint-duplex
3765  *
3766  * This function should only be called when the configuration space
3767  * contains a single configuration. Call #snd_pcm_hw_params to choose
3768  * a single configuration from the configuration space.
3769  */
3770 int snd_pcm_hw_params_is_joint_duplex(const snd_pcm_hw_params_t *params)
3771 {
3772         assert(params);
3773         if (CHECK_SANITY(params->info == ~0U)) {
3774                 snd_check(PCM, "invalid PCM info field");
3775                 return 0; /* FIXME: should be a negative error? */
3776         }
3777         return !!(params->info & SNDRV_PCM_INFO_JOINT_DUPLEX);
3778 }
3779
3780 /**
3781  * \brief Check if hardware supports synchronized start with sample resolution
3782  * \param params Configuration space
3783  * \retval 0 Hardware doesn't support synchronized start
3784  * \retval 1 Hardware supports synchronized start
3785  *
3786  * This function should only be called when the configuration space
3787  * contains a single configuration. Call #snd_pcm_hw_params to choose
3788  * a single configuration from the configuration space.
3789  */
3790 int snd_pcm_hw_params_can_sync_start(const snd_pcm_hw_params_t *params)
3791 {
3792         assert(params);
3793         if (CHECK_SANITY(params->info == ~0U)) {
3794                 snd_check(PCM, "invalid PCM info field");
3795                 return 0; /* FIXME: should be a negative error? */
3796         }
3797         return !!(params->info & SNDRV_PCM_INFO_SYNC_START);
3798 }
3799
3800 /**
3801  * \brief Check if hardware can disable period wakeups
3802  * \param params Configuration space
3803  * \retval 0 Hardware cannot disable period wakeups
3804  * \retval 1 Hardware can disable period wakeups
3805  */
3806 int snd_pcm_hw_params_can_disable_period_wakeup(const snd_pcm_hw_params_t *params)
3807 {
3808         assert(params);
3809         if (CHECK_SANITY(params->info == ~0U)) {
3810                 snd_check(PCM, "invalid PCM info field");
3811                 return 0; /* FIXME: should be a negative error? */
3812         }
3813         return !!(params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP);
3814 }
3815
3816 /**
3817  * \brief Check if hardware is capable of perfect drain
3818  * \param params Configuration space
3819  * \retval 0 Hardware doesn't do perfect drain
3820  * \retval 1 Hardware does perfect drain
3821  *
3822  * This function should only be called when the configuration space
3823  * contains a single configuration. Call #snd_pcm_hw_params to choose
3824  * a single configuration from the configuration space.
3825  *
3826  * Perfect drain means that the hardware does not use samples
3827  * beyond the stream application pointer.
3828  */
3829 int snd_pcm_hw_params_is_perfect_drain(const snd_pcm_hw_params_t *params)
3830 {
3831         assert(params);
3832         if (CHECK_SANITY(params->info == ~0U)) {
3833                 snd_check(PCM, "invalid PCM info field");
3834                 return 0; /* FIXME: should be a negative error? */
3835         }
3836         return !!(params->info & SNDRV_PCM_INFO_PERFECT_DRAIN);
3837 }
3838
3839 /**
3840  * \brief Check if hardware supports audio wallclock timestamps
3841  * \param params Configuration space
3842  * \retval 0 Hardware doesn't support audio wallclock timestamps
3843  * \retval 1 Hardware supports audio wallclock timestamps
3844  *
3845  * This function should only be called when the configuration space
3846  * contains a single configuration. Call #snd_pcm_hw_params to choose
3847  * a single configuration from the configuration space.
3848  */
3849 int snd_pcm_hw_params_supports_audio_wallclock_ts(const snd_pcm_hw_params_t *params)
3850 {
3851         /* deprecated */
3852         return snd_pcm_hw_params_supports_audio_ts_type(params,
3853                                                         SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT);
3854 }
3855
3856 /**
3857  * \brief Check if hardware supports type of audio timestamps
3858  * \param params Configuration space
3859  * \param type   Audio timestamp type
3860  * \retval 0 Hardware doesn't support type of audio timestamps
3861  * \retval 1 Hardware supports type of audio timestamps
3862  *
3863  * This function should only be called when the configuration space
3864  * contains a single configuration. Call #snd_pcm_hw_params to choose
3865  * a single configuration from the configuration space.
3866  */
3867 int snd_pcm_hw_params_supports_audio_ts_type(const snd_pcm_hw_params_t *params, int type)
3868 {
3869         assert(params);
3870         if (CHECK_SANITY(params->info == ~0U)) {
3871                 snd_check(PCM, "invalid PCM info field");
3872                 return 0; /* FIXME: should be a negative error? */
3873         }
3874         switch (type) {
3875         case SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT:
3876                 return !!(params->info & SNDRV_PCM_INFO_HAS_WALL_CLOCK); /* deprecated */
3877         case SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT:
3878                 return 1; /* always supported, based on hw_ptr */
3879         case SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK:
3880                 return !!(params->info & SNDRV_PCM_INFO_HAS_LINK_ATIME);
3881         case SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK_ABSOLUTE:
3882                 return !!(params->info & SNDRV_PCM_INFO_HAS_LINK_ABSOLUTE_ATIME);
3883         case SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK_ESTIMATED:
3884                 return !!(params->info & SNDRV_PCM_INFO_HAS_LINK_ESTIMATED_ATIME);
3885         case SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK_SYNCHRONIZED:
3886                 return !!(params->info & SNDRV_PCM_INFO_HAS_LINK_SYNCHRONIZED_ATIME);
3887         default:
3888                 return 0;
3889         }
3890 }
3891
3892 /**
3893  * \brief Get rate exact info from a configuration space
3894  * \param params Configuration space
3895  * \param rate_num Pointer to returned rate numerator
3896  * \param rate_den Pointer to returned rate denominator
3897  * \return 0 otherwise a negative error code if the info is not available
3898  *
3899  * This function should only be called when the configuration space
3900  * contains a single configuration. Call #snd_pcm_hw_params to choose
3901  * a single configuration from the configuration space.
3902  */
3903 int snd_pcm_hw_params_get_rate_numden(const snd_pcm_hw_params_t *params,
3904                                       unsigned int *rate_num, unsigned int *rate_den)
3905 {
3906         assert(params);
3907         if (CHECK_SANITY(params->rate_den == 0)) {
3908                 snd_check(PCM, "invalid rate_den value");
3909                 return -EINVAL;
3910         }
3911         *rate_num = params->rate_num;
3912         *rate_den = params->rate_den;
3913         return 0;
3914 }
3915
3916 /**
3917  * \brief Get sample resolution info from a configuration space
3918  * \param params Configuration space
3919  * \return sample resolution (in bits) otherwise a negative error code if the info is not available
3920  *
3921  * For linear formats, this function returns sample resolution -
3922  * used bits starting from the first usable significant bit defined by
3923  * the format (e.g. bit 31 for S32_LE format or bit 23 for S24_LE format -
3924  * starting from bit zero). Application may use full sample bit range defined
3925  * by the format, but additional bits (outside this sample resolution) are
3926  * stripped (not processed).
3927  *
3928  * For non-linear formats, this value may have a special meaning which may be defined in future.
3929  *
3930  * This function should only be called when the configuration space
3931  * contains a single configuration. Call #snd_pcm_hw_params to choose
3932  * a single configuration from the configuration space.
3933  */
3934 int snd_pcm_hw_params_get_sbits(const snd_pcm_hw_params_t *params)
3935 {
3936         assert(params);
3937         if (CHECK_SANITY(params->msbits == 0)) {
3938                 snd_check(PCM, "invalid msbits value");
3939                 return -EINVAL;
3940         }
3941         return params->msbits;
3942 }
3943
3944 /**
3945  * \brief Get hardware FIFO size info from a configuration space
3946  * \param params Configuration space
3947  * \return FIFO size in frames otherwise a negative error code if the info is not available
3948  *
3949  * This function should only be called when the configuration space
3950  * contains a single configuration. Call #snd_pcm_hw_params to choose
3951  * a single configuration from the configuration space.
3952  */
3953 int snd_pcm_hw_params_get_fifo_size(const snd_pcm_hw_params_t *params)
3954 {
3955         assert(params);
3956         if (CHECK_SANITY(params->info == ~0U)) {
3957                 snd_check(PCM, "invalid PCM info field");
3958                 return -EINVAL;
3959         }
3960         return params->fifo_size;
3961 }
3962
3963 /**
3964  * \brief Get hardware synchronization ID from a PCM info container
3965  * \param params Configuration space
3966  * \return 16-byte synchronization ID (use #SND_PCM_HW_PARAMS_SYNC_SIZE)
3967  *
3968  * This synchronization ID determines the similar clocks for the
3969  * PCM stream between multiple devices (including different cards).
3970  * "All zeros" means "not set". The contents of the ID can be used
3971  * only for a comparison with the contents of another ID returned
3972  * from this function. Applications should not do a comparison with
3973  * hard-coded values, because the implementation generating such
3974  * synchronization IDs may be changed in future.
3975  */
3976 const unsigned char *snd_pcm_hw_params_get_sync(const snd_pcm_hw_params_t *params)
3977 {
3978         assert(params);
3979         return params->sync;
3980 }
3981
3982 /**
3983  * \brief Fill params with a full configuration space for a PCM
3984  * \param pcm PCM handle
3985  * \param params Configuration space
3986  *
3987  * The configuration space will be filled with all possible ranges
3988  * for the PCM device.
3989  *
3990  * Note that the configuration space may be constrained by the
3991  * currently installed configuration on the PCM device. To remove
3992  * any constrains, free the configuration with #snd_pcm_hw_free
3993  * first.
3994  */
3995 int snd_pcm_hw_params_any(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
3996 {
3997         _snd_pcm_hw_params_any(params);
3998         return snd_pcm_hw_refine(pcm, params);
3999 }
4000
4001 /**
4002  * \brief get size of #snd_pcm_access_mask_t
4003  * \return size in bytes
4004  */
4005 size_t snd_pcm_access_mask_sizeof()
4006 {
4007         return sizeof(snd_pcm_access_mask_t);
4008 }
4009
4010 /**
4011  * \brief allocate an empty #snd_pcm_access_mask_t using standard malloc
4012  * \param ptr returned pointer
4013  * \return 0 on success otherwise negative error code
4014  */
4015 int snd_pcm_access_mask_malloc(snd_pcm_access_mask_t **ptr)
4016 {
4017         assert(ptr);
4018         *ptr = calloc(1, sizeof(snd_pcm_access_mask_t));
4019         if (!*ptr)
4020                 return -ENOMEM;
4021         return 0;
4022 }
4023
4024 /**
4025  * \brief frees a previously allocated #snd_pcm_access_mask_t
4026  * \param obj pointer to object to free
4027  */
4028 void snd_pcm_access_mask_free(snd_pcm_access_mask_t *obj)
4029 {
4030         free(obj);
4031 }
4032
4033 /**
4034  * \brief copy one #snd_pcm_access_mask_t to another
4035  * \param dst pointer to destination
4036  * \param src pointer to source
4037  */
4038 void snd_pcm_access_mask_copy(snd_pcm_access_mask_t *dst, const snd_pcm_access_mask_t *src)
4039 {
4040         assert(dst && src);
4041         *dst = *src;
4042 }
4043
4044 /**
4045  * \brief reset all bits in a #snd_pcm_access_mask_t
4046  * \param mask pointer to mask
4047  */
4048 void snd_pcm_access_mask_none(snd_pcm_access_mask_t *mask)
4049 {
4050         snd_mask_none((snd_mask_t *) mask);
4051 }
4052
4053 /**
4054  * \brief set all bits in a #snd_pcm_access_mask_t
4055  * \param mask pointer to mask
4056  */
4057 void snd_pcm_access_mask_any(snd_pcm_access_mask_t *mask)
4058 {
4059         snd_mask_any((snd_mask_t *) mask);
4060 }
4061
4062 /**
4063  * \brief test the presence of an access type in a #snd_pcm_access_mask_t
4064  * \param mask pointer to mask
4065  * \param val access type
4066  */
4067 int snd_pcm_access_mask_test(const snd_pcm_access_mask_t *mask, snd_pcm_access_t val)
4068 {
4069         return snd_mask_test((const snd_mask_t *) mask, (unsigned long) val);
4070 }
4071
4072 /**
4073  * \brief test, if given a #snd_pcm_access_mask_t is empty
4074  * \param mask pointer to mask
4075  * \retval 0 not empty
4076  * \retval 1 empty
4077  */
4078 int snd_pcm_access_mask_empty(const snd_pcm_access_mask_t *mask)
4079 {
4080         return snd_mask_empty((const snd_mask_t *) mask);
4081 }
4082
4083 /**
4084  * \brief make an access type present in a #snd_pcm_access_mask_t
4085  * \param mask pointer to mask
4086  * \param val access type
4087  */
4088 void snd_pcm_access_mask_set(snd_pcm_access_mask_t *mask, snd_pcm_access_t val)
4089 {
4090         snd_mask_set((snd_mask_t *) mask, (unsigned long) val);
4091 }
4092
4093 /**
4094  * \brief make an access type missing from a #snd_pcm_access_mask_t
4095  * \param mask pointer to mask
4096  * \param val access type
4097  */
4098 void snd_pcm_access_mask_reset(snd_pcm_access_mask_t *mask, snd_pcm_access_t val)
4099 {
4100         snd_mask_reset((snd_mask_t *) mask, (unsigned long) val);
4101 }
4102
4103 /**
4104  * \brief get size of #snd_pcm_format_mask_t
4105  * \return size in bytes
4106  */
4107 size_t snd_pcm_format_mask_sizeof()
4108 {
4109         return sizeof(snd_pcm_format_mask_t);
4110 }
4111
4112 /**
4113  * \brief allocate an empty #snd_pcm_format_mask_t using standard malloc
4114  * \param ptr returned pointer
4115  * \return 0 on success otherwise negative error code
4116  */
4117 int snd_pcm_format_mask_malloc(snd_pcm_format_mask_t **ptr)
4118 {
4119         assert(ptr);
4120         *ptr = calloc(1, sizeof(snd_pcm_format_mask_t));
4121         if (!*ptr)
4122                 return -ENOMEM;
4123         return 0;
4124 }
4125
4126 /**
4127  * \brief frees a previously allocated #snd_pcm_format_mask_t
4128  * \param obj pointer to object to free
4129  */
4130 void snd_pcm_format_mask_free(snd_pcm_format_mask_t *obj)
4131 {
4132         free(obj);
4133 }
4134
4135 /**
4136  * \brief copy one #snd_pcm_format_mask_t to another
4137  * \param dst pointer to destination
4138  * \param src pointer to source
4139  */
4140 void snd_pcm_format_mask_copy(snd_pcm_format_mask_t *dst, const snd_pcm_format_mask_t *src)
4141 {
4142         assert(dst && src);
4143         *dst = *src;
4144 }
4145
4146 /**
4147  * \brief reset all bits in a #snd_pcm_format_mask_t
4148  * \param mask pointer to mask
4149  */
4150 void snd_pcm_format_mask_none(snd_pcm_format_mask_t *mask)
4151 {
4152         snd_mask_none((snd_mask_t *) mask);
4153 }
4154
4155 /**
4156  * \brief set all bits in a #snd_pcm_format_mask_t
4157  * \param mask pointer to mask
4158  */
4159 void snd_pcm_format_mask_any(snd_pcm_format_mask_t *mask)
4160 {
4161         snd_mask_any((snd_mask_t *) mask);
4162 }
4163
4164 /**
4165  * \brief test the presence of a format in a #snd_pcm_format_mask_t
4166  * \param mask pointer to mask
4167  * \param val format
4168  */
4169 int snd_pcm_format_mask_test(const snd_pcm_format_mask_t *mask, snd_pcm_format_t val)
4170 {
4171         return snd_mask_test((const snd_mask_t *) mask, (unsigned long) val);
4172 }
4173
4174 /**
4175  * \brief test, if given a #snd_pcm_format_mask_t is empty
4176  * \param mask pointer to mask
4177  * \retval 0 not empty
4178  * \retval 1 empty
4179  */
4180 int snd_pcm_format_mask_empty(const snd_pcm_format_mask_t *mask)
4181 {
4182         return snd_mask_empty((const snd_mask_t *) mask);
4183 }
4184
4185 /**
4186  * \brief make a format present in a #snd_pcm_format_mask_t
4187  * \param mask pointer to mask
4188  * \param val format
4189  */
4190 void snd_pcm_format_mask_set(snd_pcm_format_mask_t *mask, snd_pcm_format_t val)
4191 {
4192         snd_mask_set((snd_mask_t *) mask, (unsigned long) val);
4193 }
4194
4195 /**
4196  * \brief make a format missing from a #snd_pcm_format_mask_t
4197  * \param mask pointer to mask
4198  * \param val format
4199  */
4200 void snd_pcm_format_mask_reset(snd_pcm_format_mask_t *mask, snd_pcm_format_t val)
4201 {
4202         snd_mask_reset((snd_mask_t *) mask, (unsigned long) val);
4203 }
4204
4205
4206 /**
4207  * \brief get size of #snd_pcm_subformat_mask_t
4208  * \return size in bytes
4209  */
4210 size_t snd_pcm_subformat_mask_sizeof()
4211 {
4212         return sizeof(snd_pcm_subformat_mask_t);
4213 }
4214
4215 /**
4216  * \brief allocate an empty #snd_pcm_subformat_mask_t using standard malloc
4217  * \param ptr returned pointer
4218  * \return 0 on success otherwise negative error code
4219  */
4220 int snd_pcm_subformat_mask_malloc(snd_pcm_subformat_mask_t **ptr)
4221 {
4222         assert(ptr);
4223         *ptr = calloc(1, sizeof(snd_pcm_subformat_mask_t));
4224         if (!*ptr)
4225                 return -ENOMEM;
4226         return 0;
4227 }
4228
4229 /**
4230  * \brief frees a previously allocated #snd_pcm_subformat_mask_t
4231  * \param obj pointer to object to free
4232  */
4233 void snd_pcm_subformat_mask_free(snd_pcm_subformat_mask_t *obj)
4234 {
4235         free(obj);
4236 }
4237
4238 /**
4239  * \brief copy one #snd_pcm_subformat_mask_t to another
4240  * \param dst pointer to destination
4241  * \param src pointer to source
4242  */
4243 void snd_pcm_subformat_mask_copy(snd_pcm_subformat_mask_t *dst, const snd_pcm_subformat_mask_t *src)
4244 {
4245         assert(dst && src);
4246         *dst = *src;
4247 }
4248
4249 /**
4250  * \brief reset all bits in a #snd_pcm_subformat_mask_t
4251  * \param mask pointer to mask
4252  */
4253 void snd_pcm_subformat_mask_none(snd_pcm_subformat_mask_t *mask)
4254 {
4255         snd_mask_none((snd_mask_t *) mask);
4256 }
4257
4258 /**
4259  * \brief set all bits in a #snd_pcm_subformat_mask_t
4260  * \param mask pointer to mask
4261  */
4262 void snd_pcm_subformat_mask_any(snd_pcm_subformat_mask_t *mask)
4263 {
4264         snd_mask_any((snd_mask_t *) mask);
4265 }
4266
4267 /**
4268  * \brief test the presence of a subformat in a #snd_pcm_subformat_mask_t
4269  * \param mask pointer to mask
4270  * \param val subformat
4271  */
4272 int snd_pcm_subformat_mask_test(const snd_pcm_subformat_mask_t *mask, snd_pcm_subformat_t val)
4273 {
4274         return snd_mask_test((const snd_mask_t *) mask, (unsigned long) val);
4275 }
4276
4277 /**
4278  * \brief test, if given a #snd_pcm_subformat_mask_t is empty
4279  * \param mask pointer to mask
4280  * \retval 0 not empty
4281  * \retval 1 empty
4282  */
4283 int snd_pcm_subformat_mask_empty(const snd_pcm_subformat_mask_t *mask)
4284 {
4285         return snd_mask_empty((const snd_mask_t *) mask);
4286 }
4287
4288 /**
4289  * \brief make a subformat present in a #snd_pcm_subformat_mask_t
4290  * \param mask pointer to mask
4291  * \param val subformat
4292  */
4293 void snd_pcm_subformat_mask_set(snd_pcm_subformat_mask_t *mask, snd_pcm_subformat_t val)
4294 {
4295         snd_mask_set((snd_mask_t *) mask, (unsigned long) val);
4296 }
4297
4298 /**
4299  * \brief make a subformat missing from a #snd_pcm_subformat_mask_t
4300  * \param mask pointer to mask
4301  * \param val subformat
4302  */
4303 void snd_pcm_subformat_mask_reset(snd_pcm_subformat_mask_t *mask, snd_pcm_subformat_t val)
4304 {
4305         snd_mask_reset((snd_mask_t *) mask, (unsigned long) val);
4306 }
4307
4308
4309 /**
4310  * \brief get size of #snd_pcm_hw_params_t
4311  * \return size in bytes
4312  */
4313 size_t snd_pcm_hw_params_sizeof()
4314 {
4315         return sizeof(snd_pcm_hw_params_t);
4316 }
4317
4318 /**
4319  * \brief allocate an invalid #snd_pcm_hw_params_t using standard malloc
4320  * \param ptr returned pointer
4321  * \return 0 on success otherwise negative error code
4322  */
4323 int snd_pcm_hw_params_malloc(snd_pcm_hw_params_t **ptr)
4324 {
4325         assert(ptr);
4326         *ptr = calloc(1, sizeof(snd_pcm_hw_params_t));
4327         if (!*ptr)
4328                 return -ENOMEM;
4329         return 0;
4330 }
4331
4332 /**
4333  * \brief frees a previously allocated #snd_pcm_hw_params_t
4334  * \param obj pointer to object to free
4335  */
4336 void snd_pcm_hw_params_free(snd_pcm_hw_params_t *obj)
4337 {
4338         free(obj);
4339 }
4340
4341 /**
4342  * \brief copy one #snd_pcm_hw_params_t to another
4343  * \param dst pointer to destination
4344  * \param src pointer to source
4345  */
4346 void snd_pcm_hw_params_copy(snd_pcm_hw_params_t *dst, const snd_pcm_hw_params_t *src)
4347 {
4348         assert(dst && src);
4349         *dst = *src;
4350 }
4351
4352
4353 /**
4354  * \brief Extract access type from a configuration space
4355  * \param params Configuration space
4356  * \param access Returned value
4357  * \return access type otherwise a negative error code if the configuration space does not contain a single value
4358  */
4359 #ifndef DOXYGEN
4360 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_access)(const snd_pcm_hw_params_t *params, snd_pcm_access_t *access)
4361 #else
4362 int snd_pcm_hw_params_get_access(const snd_pcm_hw_params_t *params, snd_pcm_access_t *access)
4363 #endif
4364 {
4365         unsigned int _val;
4366         int err = snd_pcm_hw_param_get(params, SND_PCM_HW_PARAM_ACCESS, &_val, NULL);
4367         if (err >= 0)
4368                 *access = _val;
4369         return err;
4370 }
4371
4372 /**
4373  * \brief Verify if an access type is available inside a configuration space for a PCM
4374  * \param pcm PCM handle
4375  * \param params Configuration space
4376  * \param access access type
4377  * \return 0 if available a negative error code otherwise
4378  */
4379 int snd_pcm_hw_params_test_access(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t access)
4380 {
4381         return snd_pcm_hw_param_set(pcm, params, SND_TEST, SND_PCM_HW_PARAM_ACCESS, access, 0);
4382 }
4383
4384 /**
4385  * \brief Restrict a configuration space to contain only one access type
4386  * \param pcm PCM handle
4387  * \param params Configuration space
4388  * \param access access type
4389  * \return 0 otherwise a negative error code if configuration space would become empty
4390  */
4391 int snd_pcm_hw_params_set_access(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t access)
4392 {
4393         return snd_pcm_hw_param_set(pcm, params, SND_TRY, SND_PCM_HW_PARAM_ACCESS, access, 0);
4394 }
4395
4396 /**
4397  * \brief Restrict a configuration space to contain only its first access type
4398  * \param pcm PCM handle
4399  * \param params Configuration space
4400  * \param access Returned first access type
4401  * \return 0 otherwise a negative error code
4402  */
4403 #ifndef DOXYGEN
4404 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_access_first)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t *access)
4405 #else
4406 int snd_pcm_hw_params_set_access_first(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t *access)
4407 #endif
4408 {
4409         return snd_pcm_hw_param_set_first(pcm, params, SND_PCM_HW_PARAM_ACCESS, access, NULL);
4410 }
4411
4412 /**
4413  * \brief Restrict a configuration space to contain only its last access type
4414  * \param pcm PCM handle
4415  * \param params Configuration space
4416  * \param access Returned last access type
4417  * \return 0 otherwise a negative error code
4418  */
4419 #ifndef DOXYGEN
4420 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_access_last)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t *access)
4421 #else
4422 int snd_pcm_hw_params_set_access_last(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t *access)
4423 #endif
4424 {
4425         return snd_pcm_hw_param_set_last(pcm, params, SND_PCM_HW_PARAM_ACCESS, access, NULL);
4426 }
4427
4428 /**
4429  * \brief Restrict a configuration space to contain only a set of access types
4430  * \param pcm PCM handle
4431  * \param params Configuration space
4432  * \param mask Access mask
4433  * \return 0 otherwise a negative error code
4434  */
4435 int snd_pcm_hw_params_set_access_mask(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_mask_t *mask)
4436 {
4437         return snd_pcm_hw_param_set_mask(pcm, params, SND_TRY, SND_PCM_HW_PARAM_ACCESS, (snd_mask_t *) mask);
4438 }
4439
4440 /**
4441  * \brief Get access mask from a configuration space
4442  * \param params Configuration space
4443  * \param mask Returned Access mask
4444  */
4445 int snd_pcm_hw_params_get_access_mask(snd_pcm_hw_params_t *params, snd_pcm_access_mask_t *mask)
4446 {
4447         if (params == NULL || mask == NULL)
4448                 return -EINVAL;
4449         snd_pcm_access_mask_copy(mask, snd_pcm_hw_param_get_mask(params, SND_PCM_HW_PARAM_ACCESS));
4450         return 0;
4451 }
4452
4453
4454 /**
4455  * \brief Extract format from a configuration space
4456  * \param params Configuration space
4457  * \param format returned format
4458  * \return format otherwise a negative error code if the configuration space does not contain a single value
4459  */
4460 #ifndef DOXYGEN
4461 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_format)(const snd_pcm_hw_params_t *params, snd_pcm_format_t *format)
4462 #else
4463 int snd_pcm_hw_params_get_format(const snd_pcm_hw_params_t *params, snd_pcm_format_t *format)
4464 #endif
4465 {
4466         return snd_pcm_hw_param_get(params, SND_PCM_HW_PARAM_FORMAT, (unsigned int *)format, NULL);
4467 }
4468
4469 /**
4470  * \brief Verify if a format is available inside a configuration space for a PCM
4471  * \param pcm PCM handle
4472  * \param params Configuration space
4473  * \param format format
4474  * \return 0 if available a negative error code otherwise
4475  */
4476 int snd_pcm_hw_params_test_format(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t format)
4477 {
4478         return snd_pcm_hw_param_set(pcm, params, SND_TEST, SND_PCM_HW_PARAM_FORMAT, format, 0);
4479 }
4480
4481 /**
4482  * \brief Restrict a configuration space to contain only one format
4483  * \param pcm PCM handle
4484  * \param params Configuration space
4485  * \param format format
4486  * \return 0 otherwise a negative error code
4487  */
4488 int snd_pcm_hw_params_set_format(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t format)
4489 {
4490         return snd_pcm_hw_param_set(pcm, params, SND_TRY, SND_PCM_HW_PARAM_FORMAT, format, 0);
4491 }
4492
4493 /**
4494  * \brief Restrict a configuration space to contain only its first format
4495  * \param pcm PCM handle
4496  * \param params Configuration space
4497  * \param format Returned first format
4498  * \return 0 otherwise a negative error code
4499  */
4500 #ifndef DOXYGEN
4501 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_format_first)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t *format)
4502 #else
4503 int snd_pcm_hw_params_set_format_first(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t *format)
4504 #endif
4505 {
4506         return snd_pcm_hw_param_set_first(pcm, params, SND_PCM_HW_PARAM_FORMAT, (unsigned int *)format, NULL);
4507 }
4508
4509 /**
4510  * \brief Restrict a configuration space to contain only its last format
4511  * \param pcm PCM handle
4512  * \param params Configuration space
4513  * \param format Returned last format
4514  * \return 0 otherwise a negative error code
4515  */
4516 #ifndef DOXYGEN
4517 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_format_last)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t *format)
4518 #else
4519 int snd_pcm_hw_params_set_format_last(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t *format)
4520 #endif
4521 {
4522         return snd_pcm_hw_param_set_last(pcm, params, SND_PCM_HW_PARAM_FORMAT, (unsigned int *)format, NULL);
4523 }
4524
4525 /**
4526  * \brief Restrict a configuration space to contain only a set of formats
4527  * \param pcm PCM handle
4528  * \param params Configuration space
4529  * \param mask Format mask
4530  * \return 0 otherwise a negative error code
4531  */
4532 int snd_pcm_hw_params_set_format_mask(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_mask_t *mask)
4533 {
4534         return snd_pcm_hw_param_set_mask(pcm, params, SND_TRY, SND_PCM_HW_PARAM_FORMAT, (snd_mask_t *) mask);
4535 }
4536
4537 /**
4538  * \brief Get format mask from a configuration space
4539  * \param params Configuration space
4540  * \param mask Returned Format mask
4541  */
4542 void snd_pcm_hw_params_get_format_mask(snd_pcm_hw_params_t *params, snd_pcm_format_mask_t *mask)
4543 {
4544         snd_pcm_format_mask_copy(mask, snd_pcm_hw_param_get_mask(params, SND_PCM_HW_PARAM_FORMAT));
4545 }
4546
4547
4548 /**
4549  * \brief Extract subformat from a configuration space
4550  * \param params Configuration space
4551  * \param subformat Returned subformat value
4552  * \return subformat otherwise a negative error code if the configuration space does not contain a single value
4553  */
4554 #ifndef DOXYGEN
4555 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_subformat)(const snd_pcm_hw_params_t *params, snd_pcm_subformat_t *subformat)
4556 #else
4557 int snd_pcm_hw_params_get_subformat(const snd_pcm_hw_params_t *params, snd_pcm_subformat_t *subformat)
4558 #endif
4559 {
4560         return snd_pcm_hw_param_get(params, SND_PCM_HW_PARAM_SUBFORMAT, (unsigned int *)subformat, NULL);
4561 }
4562
4563 /**
4564  * \brief Verify if a subformat is available inside a configuration space for a PCM
4565  * \param pcm PCM handle
4566  * \param params Configuration space
4567  * \param subformat subformat value
4568  * \return 0 if available a negative error code otherwise
4569  */
4570 int snd_pcm_hw_params_test_subformat(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_subformat_t subformat)
4571 {
4572         return snd_pcm_hw_param_set(pcm, params, SND_TEST, SND_PCM_HW_PARAM_SUBFORMAT, subformat, 0);
4573 }
4574
4575 /**
4576  * \brief Restrict a configuration space to contain only one subformat
4577  * \param pcm PCM handle
4578  * \param params Configuration space
4579  * \param subformat subformat value
4580  * \return 0 otherwise a negative error code if configuration space would become empty
4581  */
4582 int snd_pcm_hw_params_set_subformat(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_subformat_t subformat)
4583 {
4584         return snd_pcm_hw_param_set(pcm, params, SND_TRY, SND_PCM_HW_PARAM_SUBFORMAT, subformat, 0);
4585 }
4586
4587 /**
4588  * \brief Restrict a configuration space to contain only its first subformat
4589  * \param pcm PCM handle
4590  * \param params Configuration space
4591  * \param subformat Returned subformat
4592  * \return 0 otherwise a negative error code
4593  */
4594 #ifndef DOXYGEN
4595 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_subformat_first)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_subformat_t *subformat)
4596 #else
4597 int snd_pcm_hw_params_set_subformat_first(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_subformat_t *subformat)
4598 #endif
4599 {
4600         return snd_pcm_hw_param_set_first(pcm, params, SND_PCM_HW_PARAM_SUBFORMAT, (unsigned int *)subformat, NULL);
4601 }
4602
4603 /**
4604  * \brief Restrict a configuration space to contain only its last subformat
4605  * \param pcm PCM handle
4606  * \param params Configuration space
4607  * \param subformat Returned subformat
4608  * \return 0 otherwise a negative error code
4609  */
4610 #ifndef DOXYGEN
4611 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_subformat_last)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_subformat_t *subformat)
4612 #else
4613 int snd_pcm_hw_params_set_subformat_last(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_subformat_t *subformat)
4614 #endif
4615 {
4616         return snd_pcm_hw_param_set_last(pcm, params, SND_PCM_HW_PARAM_SUBFORMAT, (unsigned int *)subformat, NULL);
4617 }
4618
4619 /**
4620  * \brief Restrict a configuration space to contain only a set of subformats
4621  * \param pcm PCM handle
4622  * \param params Configuration space
4623  * \param mask Subformat mask
4624  * \return 0 otherwise a negative error code
4625  */
4626 int snd_pcm_hw_params_set_subformat_mask(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_subformat_mask_t *mask)
4627 {
4628         return snd_pcm_hw_param_set_mask(pcm, params, SND_TRY, SND_PCM_HW_PARAM_SUBFORMAT, (snd_mask_t *) mask);
4629 }
4630
4631 /**
4632  * \brief Get subformat mask from a configuration space
4633  * \param params Configuration space
4634  * \param mask Returned Subformat mask
4635  */
4636 void snd_pcm_hw_params_get_subformat_mask(snd_pcm_hw_params_t *params, snd_pcm_subformat_mask_t *mask)
4637 {
4638         snd_pcm_subformat_mask_copy(mask, snd_pcm_hw_param_get_mask(params, SND_PCM_HW_PARAM_SUBFORMAT));
4639 }
4640
4641
4642 /**
4643  * \brief Extract channels from a configuration space
4644  * \param params Configuration space
4645  * \param val Returned channels count
4646  * \return 0 otherwise a negative error code if the configuration space does not contain a single value
4647  */
4648 #ifndef DOXYGEN
4649 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_channels)(const snd_pcm_hw_params_t *params, unsigned int *val)
4650 #else
4651 int snd_pcm_hw_params_get_channels(const snd_pcm_hw_params_t *params, unsigned int *val)
4652 #endif
4653 {
4654         return snd_pcm_hw_param_get(params, SND_PCM_HW_PARAM_CHANNELS, val, NULL);
4655 }
4656
4657 /**
4658  * \brief Extract minimum channels count from a configuration space
4659  * \param params Configuration space
4660  * \param val minimum channels count
4661  * \return 0 otherwise a negative error code
4662  */
4663 #ifndef DOXYGEN
4664 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_channels_min)(const snd_pcm_hw_params_t *params, unsigned int *val)
4665 #else
4666 int snd_pcm_hw_params_get_channels_min(const snd_pcm_hw_params_t *params, unsigned int *val)
4667 #endif
4668 {
4669         return snd_pcm_hw_param_get_min(params, SND_PCM_HW_PARAM_CHANNELS, val, NULL);
4670 }
4671
4672 /**
4673  * \brief Extract maximum channels count from a configuration space
4674  * \param params Configuration space
4675  * \param val maximum channels count
4676  * \return 0 otherwise a negative error code
4677  */
4678 #ifndef DOXYGEN
4679 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_channels_max)(const snd_pcm_hw_params_t *params, unsigned int *val)
4680 #else
4681 int snd_pcm_hw_params_get_channels_max(const snd_pcm_hw_params_t *params, unsigned int *val)
4682 #endif
4683 {
4684         return snd_pcm_hw_param_get_max(params, SND_PCM_HW_PARAM_CHANNELS, val, NULL);
4685 }
4686
4687 /**
4688  * \brief Verify if a channels count is available inside a configuration space for a PCM
4689  * \param pcm PCM handle
4690  * \param params Configuration space
4691  * \param val channels count
4692  * \return 0 if available a negative error code otherwise
4693  */
4694 int snd_pcm_hw_params_test_channels(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val)
4695 {
4696         return snd_pcm_hw_param_set(pcm, params, SND_TEST, SND_PCM_HW_PARAM_CHANNELS, val, 0);
4697 }
4698
4699 /**
4700  * \brief Restrict a configuration space to contain only one channels count
4701  * \param pcm PCM handle
4702  * \param params Configuration space
4703  * \param val channels count
4704  * \return 0 otherwise a negative error code if configuration space would become empty
4705  */
4706 int snd_pcm_hw_params_set_channels(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val)
4707 {
4708         return snd_pcm_hw_param_set(pcm, params, SND_TRY, SND_PCM_HW_PARAM_CHANNELS, val, 0);
4709 }
4710
4711 /**
4712  * \brief Restrict a configuration space with a minimum channels count
4713  * \param pcm PCM handle
4714  * \param params Configuration space
4715  * \param val minimum channels count (on return filled with actual minimum)
4716  * \return 0 otherwise a negative error code if configuration space would become empty
4717  */
4718 int snd_pcm_hw_params_set_channels_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val)
4719 {
4720         return snd_pcm_hw_param_set_min(pcm, params, SND_TRY, SND_PCM_HW_PARAM_CHANNELS, val, NULL);
4721 }
4722
4723 /**
4724  * \brief Restrict a configuration space with a maximum channels count
4725  * \param pcm PCM handle
4726  * \param params Configuration space
4727  * \param val maximum channels count (on return filled with actual maximum)
4728  * \return 0 otherwise a negative error code if configuration space would become empty
4729  */
4730 int snd_pcm_hw_params_set_channels_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val)
4731 {
4732         return snd_pcm_hw_param_set_max(pcm, params, SND_TRY, SND_PCM_HW_PARAM_CHANNELS, val, NULL);
4733 }
4734
4735 /**
4736  * \brief Restrict a configuration space to have channels counts in a given range
4737  * \param pcm PCM handle
4738  * \param params Configuration space
4739  * \param min minimum channels count (on return filled with actual minimum)
4740  * \param max maximum channels count (on return filled with actual maximum)
4741  * \return 0 otherwise a negative error code if configuration space would become empty
4742  */
4743 int snd_pcm_hw_params_set_channels_minmax(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *min, unsigned int *max)
4744 {
4745         return snd_pcm_hw_param_set_minmax(pcm, params, SND_TRY, SND_PCM_HW_PARAM_CHANNELS, min, NULL, max, NULL);
4746 }
4747
4748 /**
4749  * \brief Restrict a configuration space to have channels count nearest to a target
4750  * \param pcm PCM handle
4751  * \param params Configuration space
4752  * \param val target channels count, returned chosen channels count
4753  * \return 0 otherwise a negative error code if configuration space is empty
4754  */
4755 #ifndef DOXYGEN
4756 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_channels_near)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val)
4757 #else
4758 int snd_pcm_hw_params_set_channels_near(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val)
4759 #endif
4760 {
4761         return snd_pcm_hw_param_set_near(pcm, params, SND_PCM_HW_PARAM_CHANNELS, val, NULL);
4762 }
4763
4764 /**
4765  * \brief Restrict a configuration space to contain only its minimum channels count
4766  * \param pcm PCM handle
4767  * \param params Configuration space
4768  * \param val minimum channels count
4769  * \return 0 otherwise a negative error code
4770  */
4771 #ifndef DOXYGEN
4772 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_channels_first)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val)
4773 #else
4774 int snd_pcm_hw_params_set_channels_first(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val)
4775 #endif
4776 {
4777         return snd_pcm_hw_param_set_first(pcm, params, SND_PCM_HW_PARAM_CHANNELS, val, NULL);
4778 }
4779
4780 /**
4781  * \brief Restrict a configuration space to contain only its maximum channels count
4782  * \param pcm PCM handle
4783  * \param params Configuration space
4784  * \param val maximum channels count
4785  * \return 0 otherwise a negative error code
4786  */
4787 #ifndef DOXYGEN
4788 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_channels_last)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val)
4789 #else
4790 int snd_pcm_hw_params_set_channels_last(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val)
4791 #endif
4792 {
4793         return snd_pcm_hw_param_set_last(pcm, params, SND_PCM_HW_PARAM_CHANNELS, val, NULL);
4794 }
4795
4796
4797 /**
4798  * \brief Extract rate from a configuration space
4799  * \param params Configuration space
4800  * \param val Returned approximate rate
4801  * \param dir Sub unit direction
4802  * \return 0 otherwise a negative error code if the configuration space does not contain a single value
4803  *
4804  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
4805  */
4806 #ifndef DOXYGEN
4807 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_rate)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
4808 #else
4809 int snd_pcm_hw_params_get_rate(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
4810 #endif
4811 {
4812         return snd_pcm_hw_param_get(params, SND_PCM_HW_PARAM_RATE, val, dir);
4813 }
4814
4815 /**
4816  * \brief Extract minimum rate from a configuration space
4817  * \param params Configuration space
4818  * \param val Returned approximate minimum rate
4819  * \param dir Sub unit direction
4820  * \return 0 otherwise a negative error code
4821  *
4822  * Exact value is <,=,> the returned one following dir (-1,0,1)
4823  */
4824 #ifndef DOXYGEN
4825 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_rate_min)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
4826 #else
4827 int snd_pcm_hw_params_get_rate_min(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
4828 #endif
4829 {
4830         return snd_pcm_hw_param_get_min(params, SND_PCM_HW_PARAM_RATE, val, dir);
4831 }
4832
4833 /**
4834  * \brief Extract maximum rate from a configuration space
4835  * \param params Configuration space
4836  * \param val Returned approximate maximum rate
4837  * \param dir Sub unit direction
4838  * \return 0 otherwise a negative error code
4839  *
4840  * Exact value is <,=,> the returned one following dir (-1,0,1)
4841  */
4842 #ifndef DOXYGEN
4843 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_rate_max)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
4844 #else
4845 int snd_pcm_hw_params_get_rate_max(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
4846 #endif
4847 {
4848         return snd_pcm_hw_param_get_max(params, SND_PCM_HW_PARAM_RATE, val, dir);
4849 }
4850
4851 /**
4852  * \brief Verify if a rate is available inside a configuration space for a PCM
4853  * \param pcm PCM handle
4854  * \param params Configuration space
4855  * \param val approximate rate
4856  * \param dir Sub unit direction
4857  * \return 0 if available a negative error code otherwise
4858  *
4859  * Wanted exact value is <,=,> val following dir (-1,0,1)
4860  */
4861 int snd_pcm_hw_params_test_rate(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir)
4862 {
4863         return snd_pcm_hw_param_set(pcm, params, SND_TEST, SND_PCM_HW_PARAM_RATE, val, dir);
4864 }
4865
4866 /**
4867  * \brief Restrict a configuration space to contain only one rate
4868  * \param pcm PCM handle
4869  * \param params Configuration space
4870  * \param val approximate rate
4871  * \param dir Sub unit direction
4872  * \return 0 otherwise a negative error code if configuration space would become empty
4873  *
4874  * Wanted exact value is <,=,> val following dir (-1,0,1)
4875  */
4876 int snd_pcm_hw_params_set_rate(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir)
4877 {
4878         return snd_pcm_hw_param_set(pcm, params, SND_TRY, SND_PCM_HW_PARAM_RATE, val, dir);
4879 }
4880
4881 /**
4882  * \brief Restrict a configuration space with a minimum rate
4883  * \param pcm PCM handle
4884  * \param params Configuration space
4885  * \param val approximate minimum rate (on return filled with actual minimum)
4886  * \param dir Sub unit direction (on return filled with actual direction)
4887  * \return 0 otherwise a negative error code if configuration space would become empty
4888  *
4889  * Wanted/actual exact minimum is <,=,> val following dir (-1,0,1)
4890  */
4891 int snd_pcm_hw_params_set_rate_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
4892 {
4893         return snd_pcm_hw_param_set_min(pcm, params, SND_TRY, SND_PCM_HW_PARAM_RATE, val, dir);
4894 }
4895
4896 /**
4897  * \brief Restrict a configuration space with a maximum rate
4898  * \param pcm PCM handle
4899  * \param params Configuration space
4900  * \param val approximate maximum rate (on return filled with actual maximum)
4901  * \param dir Sub unit direction (on return filled with actual direction)
4902  * \return 0 otherwise a negative error code if configuration space would become empty
4903  *
4904  * Wanted/actual exact maximum is <,=,> val following dir (-1,0,1)
4905  */
4906 int snd_pcm_hw_params_set_rate_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
4907 {
4908         return snd_pcm_hw_param_set_max(pcm, params, SND_TRY, SND_PCM_HW_PARAM_RATE, val, dir);
4909 }
4910
4911 /**
4912  * \brief Restrict a configuration space to have rates in a given range
4913  * \param pcm PCM handle
4914  * \param params Configuration space
4915  * \param min approximate minimum rate (on return filled with actual minimum)
4916  * \param mindir Sub unit direction for minimum (on return filled with actual direction)
4917  * \param max approximate maximum rate (on return filled with actual maximum)
4918  * \param maxdir Sub unit direction for maximum (on return filled with actual direction)
4919  * \return 0 otherwise a negative error code if configuration space would become empty
4920  *
4921  * Wanted/actual exact min/max is <,=,> val following dir (-1,0,1)
4922  */
4923 int snd_pcm_hw_params_set_rate_minmax(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *min, int *mindir, unsigned int *max, int *maxdir)
4924 {
4925         return snd_pcm_hw_param_set_minmax(pcm, params, SND_TRY, SND_PCM_HW_PARAM_RATE, min, mindir, max, maxdir);
4926 }
4927
4928 /**
4929  * \brief Restrict a configuration space to have rate nearest to a target
4930  * \param pcm PCM handle
4931  * \param params Configuration space
4932  * \param val approximate target rate / returned approximate set rate
4933  * \param dir Sub unit direction
4934  * \return 0 otherwise a negative error code if configuration space is empty
4935  *
4936  * target/chosen exact value is <,=,> val following dir (-1,0,1)
4937  */
4938 #ifndef DOXYGEN
4939 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_rate_near)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
4940 #else
4941 int snd_pcm_hw_params_set_rate_near(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
4942 #endif
4943 {
4944         return snd_pcm_hw_param_set_near(pcm, params, SND_PCM_HW_PARAM_RATE, val, dir);
4945 }
4946
4947 /**
4948  * \brief Restrict a configuration space to contain only its minimum rate
4949  * \param pcm PCM handle
4950  * \param params Configuration space
4951  * \param val Returned minimum approximate rate
4952  * \param dir Sub unit direction
4953  * \return 0 otherwise a negative error code
4954  *
4955  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
4956  */
4957 #ifndef DOXYGEN
4958 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_rate_first)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
4959 #else
4960 int snd_pcm_hw_params_set_rate_first(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
4961 #endif
4962 {
4963         return snd_pcm_hw_param_set_first(pcm, params, SND_PCM_HW_PARAM_RATE, val, dir);
4964 }
4965
4966 /**
4967  * \brief Restrict a configuration space to contain only its maximum rate
4968  * \param pcm PCM handle
4969  * \param params Configuration space
4970  * \param val Returned maximum approximate rate
4971  * \param dir Sub unit direction
4972  * \return 0 otherwise a negative error code
4973  *
4974  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
4975  */
4976 #ifndef DOXYGEN
4977 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_rate_last)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
4978 #else
4979 int snd_pcm_hw_params_set_rate_last(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
4980 #endif
4981 {
4982         return snd_pcm_hw_param_set_last(pcm, params, SND_PCM_HW_PARAM_RATE, val, dir);
4983 }
4984
4985 /**
4986  * \brief Restrict a configuration space to contain only real hardware rates
4987  * \param pcm PCM handle
4988  * \param params Configuration space
4989  * \param val 0 = disable, 1 = enable (default) rate resampling
4990  * \return 0 otherwise a negative error code
4991  */
4992 int snd_pcm_hw_params_set_rate_resample(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val)
4993 {
4994         assert(pcm && params);
4995         if (!val)
4996                 params->flags |= SND_PCM_HW_PARAMS_NORESAMPLE;
4997         else
4998                 params->flags &= ~SND_PCM_HW_PARAMS_NORESAMPLE;
4999         params->rmask = ~0;
5000         return snd_pcm_hw_refine(pcm, params);
5001 }
5002
5003 /**
5004  * \brief Extract resample state from a configuration space
5005  * \param pcm PCM handle
5006  * \param params Configuration space
5007  * \param val 0 = disable, 1 = enable rate resampling
5008  * \return 0 otherwise a negative error code
5009  */
5010 int snd_pcm_hw_params_get_rate_resample(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val)
5011 {
5012         assert(pcm && params && val);
5013         *val = params->flags & SND_PCM_HW_PARAMS_NORESAMPLE ? 0 : 1;
5014         return 0;
5015 }
5016
5017 /**
5018  * \brief Restrict a configuration space to allow the buffer to be accessible from outside
5019  * \param pcm PCM handle
5020  * \param params Configuration space
5021  * \param val 0 = disable, 1 = enable (default) exporting buffer
5022  * \return 0 otherwise a negative error code
5023  */
5024 int snd_pcm_hw_params_set_export_buffer(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val)
5025 {
5026         assert(pcm && params);
5027         if (val)
5028                 params->flags |= SND_PCM_HW_PARAMS_EXPORT_BUFFER;
5029         else
5030                 params->flags &= ~SND_PCM_HW_PARAMS_EXPORT_BUFFER;
5031         params->rmask = ~0;
5032         return snd_pcm_hw_refine(pcm, params);
5033 }
5034
5035 /**
5036  * \brief Extract buffer accessibility from a configuration space
5037  * \param pcm PCM handle
5038  * \param params Configuration space
5039  * \param val 0 = disable, 1 = enable exporting buffer
5040  * \return 0 otherwise a negative error code
5041  */
5042 int snd_pcm_hw_params_get_export_buffer(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val)
5043 {
5044         assert(pcm && params && val);
5045         *val = params->flags & SND_PCM_HW_PARAMS_EXPORT_BUFFER ? 1 : 0;
5046         return 0;
5047 }
5048
5049 /**
5050  * \brief Restrict a configuration space to settings without period wakeups
5051  * \param pcm PCM handle
5052  * \param params Configuration space
5053  * \param val 0 = disable, 1 = enable (default) period wakeup
5054  * \return Zero on success, otherwise a negative error code.
5055  *
5056  * This function must be called only on devices where non-blocking mode is
5057  * enabled.
5058  *
5059  * To check whether the hardware does support disabling period wakeups, call
5060  * #snd_pcm_hw_params_can_disable_period_wakeup(). If the hardware does not
5061  * support this mode, standard period wakeups will be generated.
5062  *
5063  * Even with disabled period wakeups, the period size/time/count parameters
5064  * are valid; it is suggested to use #snd_pcm_hw_params_set_period_size_last().
5065  *
5066  * When period wakeups are disabled, the application must not use any functions
5067  * that could block on this device. The use of poll should be limited to error
5068  * cases. The application needs to use an external event or a timer to
5069  * check the state of the ring buffer and refill it apropriately.
5070  */
5071 int snd_pcm_hw_params_set_period_wakeup(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val)
5072 {
5073         assert(pcm && params);
5074
5075         if (!val) {
5076                 if (!(pcm->mode & SND_PCM_NONBLOCK))
5077                         return -EINVAL;
5078                 params->flags |= SND_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
5079         } else
5080                 params->flags &= ~SND_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
5081         params->rmask = ~0;
5082
5083         return snd_pcm_hw_refine(pcm, params);
5084 }
5085
5086 /**
5087  * \brief Extract period wakeup flag from a configuration space
5088  * \param pcm PCM handle
5089  * \param params Configuration space
5090  * \param val 0 = disabled, 1 = enabled period wakeups
5091  * \return Zero on success, otherwise a negative error code.
5092  */
5093 int snd_pcm_hw_params_get_period_wakeup(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val)
5094 {
5095         assert(pcm && params && val);
5096         *val = params->flags & SND_PCM_HW_PARAMS_NO_PERIOD_WAKEUP ? 0 : 1;
5097         return 0;
5098 }
5099
5100 /**
5101  * \brief Restrict a configuration space to fill the end of playback stream with silence when drain() is invoked
5102  * \param pcm PCM handle
5103  * \param params Configuration space
5104  * \param val 0 = disabled, 1 = enabled (default) fill the end of the playback stream with silence when drain() is invoked
5105  * \return Zero on success, otherwise a negative error code.
5106  *
5107  * When disabled, the application should handle the end of stream gracefully
5108  * (fill the silent samples to align to the period size plus some extra
5109  * samples for hardware / driver without perfect drain). Note that the rewind
5110  * may be used for this purpose or the sw_params silencing mechanism.
5111  */
5112 int snd_pcm_hw_params_set_drain_silence(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val)
5113 {
5114         assert(pcm && params);
5115         if (val)
5116                 params->flags &= ~SND_PCM_HW_PARAMS_NO_DRAIN_SILENCE;
5117         else
5118                 params->flags |= SND_PCM_HW_PARAMS_NO_DRAIN_SILENCE;
5119         params->rmask = ~0;
5120         return snd_pcm_hw_refine(pcm, params);
5121 }
5122
5123 /**
5124  * \brief Extract drain with the filling of silence samples from a configuration space
5125  * \param pcm PCM handle
5126  * \param params Configuration space
5127  * \param val 0 = disabled, 1 = enabled
5128  * \return 0 otherwise a negative error code
5129  */
5130 int snd_pcm_hw_params_get_drain_silence(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val)
5131 {
5132         assert(pcm && params && val);
5133         *val = params->flags & SND_PCM_HW_PARAMS_NO_DRAIN_SILENCE ? 0 : 1;
5134         return 0;
5135 }
5136
5137 /**
5138  * \brief Extract period time from a configuration space
5139  * \param params Configuration space
5140  * \param val Returned approximate period duration in us
5141  * \param dir Sub unit direction
5142  * \return 0 otherwise a negative error code if the configuration space does not contain a single value
5143  *
5144  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
5145  */
5146 #ifndef DOXYGEN
5147 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_period_time)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5148 #else
5149 int snd_pcm_hw_params_get_period_time(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5150 #endif
5151 {
5152         return snd_pcm_hw_param_get(params, SND_PCM_HW_PARAM_PERIOD_TIME, val, dir);
5153 }
5154
5155 /**
5156  * \brief Extract minimum period time from a configuration space
5157  * \param params Configuration space
5158  * \param val approximate minimum period duration in us
5159  * \param dir Sub unit direction
5160  * \return 0 otherwise a negative error code
5161  *
5162  * Exact value is <,=,> the returned one following dir (-1,0,1)
5163  */
5164 #ifndef DOXYGEN
5165 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_period_time_min)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5166 #else
5167 int snd_pcm_hw_params_get_period_time_min(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5168 #endif
5169 {
5170         return snd_pcm_hw_param_get_min(params, SND_PCM_HW_PARAM_PERIOD_TIME, val, dir);
5171 }
5172
5173 /**
5174  * \brief Extract maximum period time from a configuration space
5175  * \param params Configuration space
5176  * \param val approximate maximum period duration in us
5177  * \param dir Sub unit direction
5178  * \return 0 otherwise a negative error code
5179  *
5180  * Exact value is <,=,> the returned one following dir (-1,0,1)
5181  */
5182 #ifndef DOXYGEN
5183 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_period_time_max)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5184 #else
5185 int snd_pcm_hw_params_get_period_time_max(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5186 #endif
5187 {
5188         return snd_pcm_hw_param_get_max(params, SND_PCM_HW_PARAM_PERIOD_TIME, val, dir);
5189 }
5190
5191 /**
5192  * \brief Verify if a period time is available inside a configuration space for a PCM
5193  * \param pcm PCM handle
5194  * \param params Configuration space
5195  * \param val approximate period duration in us
5196  * \param dir Sub unit direction
5197  * \return 0 if available a negative error code otherwise
5198  *
5199  * Wanted exact value is <,=,> val following dir (-1,0,1)
5200  */
5201 int snd_pcm_hw_params_test_period_time(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir)
5202 {
5203         return snd_pcm_hw_param_set(pcm, params, SND_TEST, SND_PCM_HW_PARAM_PERIOD_TIME, val, dir);
5204 }
5205
5206 /**
5207  * \brief Restrict a configuration space to contain only one period time
5208  * \param pcm PCM handle
5209  * \param params Configuration space
5210  * \param val approximate period duration in us
5211  * \param dir Sub unit direction
5212  * \return 0 otherwise a negative error code if configuration space would become empty
5213  *
5214  * Wanted exact value is <,=,> val following dir (-1,0,1)
5215  */
5216 int snd_pcm_hw_params_set_period_time(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir)
5217 {
5218         return snd_pcm_hw_param_set(pcm, params, SND_TRY, SND_PCM_HW_PARAM_PERIOD_TIME, val, dir);
5219 }
5220
5221
5222 /**
5223  * \brief Restrict a configuration space with a minimum period time
5224  * \param pcm PCM handle
5225  * \param params Configuration space
5226  * \param val approximate minimum period duration in us (on return filled with actual minimum)
5227  * \param dir Sub unit direction (on return filled with actual direction)
5228  * \return 0 otherwise a negative error code if configuration space would become empty
5229  *
5230  * Wanted/actual exact minimum is <,=,> val following dir (-1,0,1)
5231  */
5232 int snd_pcm_hw_params_set_period_time_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5233 {
5234         return snd_pcm_hw_param_set_min(pcm, params, SND_TRY, SND_PCM_HW_PARAM_PERIOD_TIME, val, dir);
5235 }
5236
5237 /**
5238  * \brief Restrict a configuration space with a maximum period time
5239  * \param pcm PCM handle
5240  * \param params Configuration space
5241  * \param val approximate maximum period duration in us (on return filled with actual maximum)
5242  * \param dir Sub unit direction (on return filled with actual direction)
5243  * \return 0 otherwise a negative error code if configuration space would become empty
5244  *
5245  * Wanted/actual exact maximum is <,=,> val following dir (-1,0,1)
5246  */
5247 int snd_pcm_hw_params_set_period_time_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5248 {
5249         return snd_pcm_hw_param_set_max(pcm, params, SND_TRY, SND_PCM_HW_PARAM_PERIOD_TIME, val, dir);
5250 }
5251
5252 /**
5253  * \brief Restrict a configuration space to have period times in a given range
5254  * \param pcm PCM handle
5255  * \param params Configuration space
5256  * \param min approximate minimum period duration in us (on return filled with actual minimum)
5257  * \param mindir Sub unit direction for minimum (on return filled with actual direction)
5258  * \param max approximate maximum period duration in us (on return filled with actual maximum)
5259  * \param maxdir Sub unit direction for maximum (on return filled with actual direction)
5260  * \return 0 otherwise a negative error code if configuration space would become empty
5261  *
5262  * Wanted/actual exact min/max is <,=,> val following dir (-1,0,1)
5263  */
5264 int snd_pcm_hw_params_set_period_time_minmax(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *min, int *mindir, unsigned int *max, int *maxdir)
5265 {
5266         return snd_pcm_hw_param_set_minmax(pcm, params, SND_TRY, SND_PCM_HW_PARAM_PERIOD_TIME, min, mindir, max, maxdir);
5267 }
5268
5269 /**
5270  * \brief Restrict a configuration space to have period time nearest to a target
5271  * \param pcm PCM handle
5272  * \param params Configuration space
5273  * \param val approximate target period duration in us / returned chosen approximate target period duration
5274  * \param dir Sub unit direction
5275  * \return 0 otherwise a negative error code if configuration space is empty
5276  *
5277  * target/chosen exact value is <,=,> val following dir (-1,0,1)
5278  */
5279 #ifndef DOXYGEN
5280 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_period_time_near)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5281 #else
5282 int snd_pcm_hw_params_set_period_time_near(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5283 #endif
5284 {
5285         return snd_pcm_hw_param_set_near(pcm, params, SND_PCM_HW_PARAM_PERIOD_TIME, val, dir);
5286 }
5287
5288 /**
5289  * \brief Restrict a configuration space to contain only its minimum period time
5290  * \param pcm PCM handle
5291  * \param params Configuration space
5292  * \param val Returned approximate period duration in us
5293  * \param dir Sub unit direction
5294  * \return 0 otherwise a negative error code
5295  *
5296  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
5297  */
5298 #ifndef DOXYGEN
5299 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_period_time_first)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5300 #else
5301 int snd_pcm_hw_params_set_period_time_first(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5302 #endif
5303 {
5304         return snd_pcm_hw_param_set_first(pcm, params, SND_PCM_HW_PARAM_PERIOD_TIME, val, dir);
5305 }
5306
5307 /**
5308  * \brief Restrict a configuration space to contain only its maximum period time
5309  * \param pcm PCM handle
5310  * \param params Configuration space
5311  * \param val Returned maximum approximate period time
5312  * \param dir Sub unit direction
5313  * \return approximate period duration in us
5314  */
5315 #ifndef DOXYGEN
5316 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_period_time_last)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5317 #else
5318 int snd_pcm_hw_params_set_period_time_last(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5319 #endif
5320 {
5321         return snd_pcm_hw_param_set_last(pcm, params, SND_PCM_HW_PARAM_PERIOD_TIME, val, dir);
5322 }
5323
5324
5325 /**
5326  * \brief Extract period size from a configuration space
5327  * \param params Configuration space
5328  * \param val Returned approximate period size in frames
5329  * \param dir Sub unit direction
5330  * \return 0 otherwise a negative error code if the configuration space does not contain a single value
5331  *
5332  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
5333  */
5334 #ifndef DOXYGEN
5335 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_period_size)(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir)
5336 #else
5337 int snd_pcm_hw_params_get_period_size(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir)
5338 #endif
5339 {
5340         unsigned int _val;
5341         int err = snd_pcm_hw_param_get(params, SND_PCM_HW_PARAM_PERIOD_SIZE, &_val, dir);
5342         if (err >= 0)
5343                 *val = _val;
5344         return err;
5345 }
5346
5347 /**
5348  * \brief Extract minimum period size from a configuration space
5349  * \param params Configuration space
5350  * \param val approximate minimum period size in frames
5351  * \param dir Sub unit direction
5352  * \return 0 otherwise a negative error code
5353  *
5354  * Exact value is <,=,> the returned one following dir (-1,0,1)
5355  */
5356 #ifndef DOXYGEN
5357 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_period_size_min)(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir)
5358 #else
5359 int snd_pcm_hw_params_get_period_size_min(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir)
5360 #endif
5361 {
5362         unsigned int _val = *val;
5363         int err = snd_pcm_hw_param_get_min(params, SND_PCM_HW_PARAM_PERIOD_SIZE, &_val, dir);
5364         if (err >= 0)
5365                 *val = _val;
5366         return err;
5367 }
5368
5369 /**
5370  * \brief Extract maximum period size from a configuration space
5371  * \param params Configuration space
5372  * \param val approximate minimum period size in frames
5373  * \param dir Sub unit direction
5374  * \return 0 otherwise a negative error code
5375  *
5376  * Exact value is <,=,> the returned one following dir (-1,0,1)
5377  */
5378 #ifndef DOXYGEN
5379 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_period_size_max)(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir)
5380 #else
5381 int snd_pcm_hw_params_get_period_size_max(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir)
5382 #endif
5383 {
5384         unsigned int _val = *val;
5385         int err = snd_pcm_hw_param_get_max(params, SND_PCM_HW_PARAM_PERIOD_SIZE, &_val, dir);
5386         if (err >= 0)
5387                 *val = _val;
5388         return err;
5389 }
5390
5391 /**
5392  * \brief Verify if a period size is available inside a configuration space for a PCM
5393  * \param pcm PCM handle
5394  * \param params Configuration space
5395  * \param val approximate period size in frames
5396  * \param dir Sub unit direction
5397  * \return 0 if available a negative error code otherwise
5398  *
5399  * Wanted exact value is <,=,> val following dir (-1,0,1)
5400  */
5401 int snd_pcm_hw_params_test_period_size(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t val, int dir)
5402 {
5403         return snd_pcm_hw_param_set(pcm, params, SND_TEST, SND_PCM_HW_PARAM_PERIOD_SIZE, val, dir);
5404 }
5405
5406 /**
5407  * \brief Restrict a configuration space to contain only one period size
5408  * \param pcm PCM handle
5409  * \param params Configuration space
5410  * \param val approximate period size in frames
5411  * \param dir Sub unit direction
5412  * \return 0 otherwise a negative error code if configuration space would become empty
5413  *
5414  * Wanted exact value is <,=,> val following dir (-1,0,1)
5415  */
5416 int snd_pcm_hw_params_set_period_size(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t val, int dir)
5417 {
5418         return snd_pcm_hw_param_set(pcm, params, SND_TRY, SND_PCM_HW_PARAM_PERIOD_SIZE, val, dir);
5419 }
5420
5421 /**
5422  * \brief Restrict a configuration space with a minimum period size
5423  * \param pcm PCM handle
5424  * \param params Configuration space
5425  * \param val approximate minimum period size in frames (on return filled with actual minimum)
5426  * \param dir Sub unit direction (on return filled with actual direction)
5427  * \return 0 otherwise a negative error code if configuration space would become empty
5428  *
5429  * Wanted/actual exact minimum is <,=,> val following dir (-1,0,1)
5430  */
5431 int snd_pcm_hw_params_set_period_size_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir)
5432 {
5433         unsigned int _val = *val;
5434         int err = snd_pcm_hw_param_set_min(pcm, params, SND_TRY, SND_PCM_HW_PARAM_PERIOD_SIZE, &_val, dir);
5435         if (err >= 0)
5436                 *val = _val;
5437         return err;
5438 }
5439
5440 /**
5441  * \brief Restrict a configuration space with a maximum period size
5442  * \param pcm PCM handle
5443  * \param params Configuration space
5444  * \param val approximate maximum period size in frames (on return filled with actual maximum)
5445  * \param dir Sub unit direction (on return filled with actual direction)
5446  * \return 0 otherwise a negative error code if configuration space would become empty
5447  *
5448  * Wanted/actual exact minimum is <,=,> val following dir (-1,0,1)
5449  */
5450 int snd_pcm_hw_params_set_period_size_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir)
5451 {
5452         unsigned int _val = *val;
5453         int err = snd_pcm_hw_param_set_max(pcm, params, SND_TRY, SND_PCM_HW_PARAM_PERIOD_SIZE, &_val, dir);
5454         if (err >= 0)
5455                 *val = _val;
5456         return err;
5457 }
5458
5459 /**
5460  * \brief Restrict a configuration space to have period sizes in a given range
5461  * \param pcm PCM handle
5462  * \param params Configuration space
5463  * \param min approximate minimum period size in frames (on return filled with actual minimum)
5464  * \param mindir Sub unit direction for minimum (on return filled with actual direction)
5465  * \param max approximate maximum period size in frames (on return filled with actual maximum)
5466  * \param maxdir Sub unit direction for maximum (on return filled with actual direction)
5467  * \return 0 otherwise a negative error code if configuration space would become empty
5468  *
5469  * Wanted/actual exact min/max is <,=,> val following dir (-1,0,1)
5470  */
5471 int snd_pcm_hw_params_set_period_size_minmax(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *min, int *mindir, snd_pcm_uframes_t *max, int *maxdir)
5472 {
5473         unsigned int _min = *min;
5474         unsigned int _max = *max;
5475         int err = snd_pcm_hw_param_set_minmax(pcm, params, SND_TRY, SND_PCM_HW_PARAM_PERIOD_SIZE, &_min, mindir, &_max, maxdir);
5476         *min = _min;
5477         *max = _max;
5478         return err;
5479 }
5480
5481 /**
5482  * \brief Restrict a configuration space to have period size nearest to a target
5483  * \param pcm PCM handle
5484  * \param params Configuration space
5485  * \param val approximate target period size in frames / returned chosen approximate target period size
5486  * \param dir Sub unit direction
5487  * \return 0 otherwise a negative error code if configuration space is empty
5488  *
5489  * target/chosen exact value is <,=,> val following dir (-1,0,1)
5490  */
5491 #ifndef DOXYGEN
5492 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_period_size_near)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir)
5493 #else
5494 int snd_pcm_hw_params_set_period_size_near(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir)
5495 #endif
5496 {
5497         unsigned int _val = *val;
5498         int err = snd_pcm_hw_param_set_near(pcm, params, SND_PCM_HW_PARAM_PERIOD_SIZE, &_val, dir);
5499         if (err >= 0)
5500                 *val = _val;
5501         return err;
5502 }
5503
5504 /**
5505  * \brief Restrict a configuration space to contain only its minimum period size
5506  * \param pcm PCM handle
5507  * \param params Configuration space
5508  * \param val Returned maximum approximate period size in frames
5509  * \param dir Sub unit direction
5510  * \return 0 otherwise a negative error code
5511  *
5512  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
5513  */
5514 #ifndef DOXYGEN
5515 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_period_size_first)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir)
5516 #else
5517 int snd_pcm_hw_params_set_period_size_first(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir)
5518 #endif
5519 {
5520         unsigned int _val;
5521         int err = snd_pcm_hw_param_set_first(pcm, params, SND_PCM_HW_PARAM_PERIOD_SIZE, &_val, dir);
5522         if (err >= 0)
5523                 *val = _val;
5524         return err;
5525 }
5526
5527 /**
5528  * \brief Restrict a configuration space to contain only its maximum period size
5529  * \param pcm PCM handle
5530  * \param params Configuration space
5531  * \param val Returned maximum approximate period size in frames
5532  * \param dir Sub unit direction
5533  * \return 0 otherwise a negative error code
5534  *
5535  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
5536  */
5537 #ifndef DOXYGEN
5538 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_period_size_last)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir)
5539 #else
5540 int snd_pcm_hw_params_set_period_size_last(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir)
5541 #endif
5542 {
5543         unsigned int _val;
5544         int err = snd_pcm_hw_param_set_last(pcm, params, SND_PCM_HW_PARAM_PERIOD_SIZE, &_val, dir);
5545         if (err >= 0)
5546                 *val = _val;
5547         return err;
5548 }
5549
5550 /**
5551  * \brief Restrict a configuration space to contain only integer period sizes
5552  * \param pcm PCM handle
5553  * \param params Configuration space
5554  * \return 0 otherwise a negative error code if configuration space would become empty
5555  */
5556 int snd_pcm_hw_params_set_period_size_integer(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
5557 {
5558         return snd_pcm_hw_param_set_integer(pcm, params, SND_TRY, SND_PCM_HW_PARAM_PERIOD_SIZE);
5559 }
5560
5561
5562 /**
5563  * \brief Extract periods from a configuration space
5564  * \param params Configuration space
5565  * \param val approximate periods per buffer
5566  * \param dir Sub unit direction
5567  * \return 0 otherwise a negative error code if the configuration space does not contain a single value
5568  *
5569  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
5570  */
5571 #ifndef DOXYGEN
5572 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_periods)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5573 #else
5574 int snd_pcm_hw_params_get_periods(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5575 #endif
5576 {
5577         return snd_pcm_hw_param_get(params, SND_PCM_HW_PARAM_PERIODS, val, dir);
5578 }
5579
5580 /**
5581  * \brief Extract minimum periods count from a configuration space
5582  * \param params Configuration space
5583  * \param val approximate minimum periods per buffer
5584  * \param dir Sub unit direction
5585  * \return 0 otherwise a negative error code
5586  *
5587  * Exact value is <,=,> the returned one following dir (-1,0,1)
5588  */
5589 #ifndef DOXYGEN
5590 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_periods_min)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5591 #else
5592 int snd_pcm_hw_params_get_periods_min(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5593 #endif
5594 {
5595         return snd_pcm_hw_param_get_min(params, SND_PCM_HW_PARAM_PERIODS, val, dir);
5596 }
5597
5598 /**
5599  * \brief Extract maximum periods count from a configuration space
5600  * \param params Configuration space
5601  * \param val approximate maximum periods per buffer
5602  * \param dir Sub unit direction
5603  * \return 0 otherwise a negative error code
5604  *
5605  * Exact value is <,=,> the returned one following dir (-1,0,1)
5606  */
5607 #ifndef DOXYGEN
5608 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_periods_max)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5609 #else
5610 int snd_pcm_hw_params_get_periods_max(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5611 #endif
5612 {
5613         return snd_pcm_hw_param_get_max(params, SND_PCM_HW_PARAM_PERIODS, val, dir);
5614 }
5615
5616 /**
5617  * \brief Verify if a periods count is available inside a configuration space for a PCM
5618  * \param pcm PCM handle
5619  * \param params Configuration space
5620  * \param val approximate periods per buffer
5621  * \param dir Sub unit direction
5622  * \return 0 if available a negative error code otherwise
5623  *
5624  * Wanted exact value is <,=,> val following dir (-1,0,1)
5625  */
5626 int snd_pcm_hw_params_test_periods(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir)
5627 {
5628         return snd_pcm_hw_param_set(pcm, params, SND_TEST, SND_PCM_HW_PARAM_PERIODS, val, dir);
5629 }
5630
5631 /**
5632  * \brief Restrict a configuration space to contain only one periods count
5633  * \param pcm PCM handle
5634  * \param params Configuration space
5635  * \param val approximate periods per buffer
5636  * \param dir Sub unit direction
5637  * \return 0 otherwise a negative error code if configuration space would become empty
5638  *
5639  * Wanted exact value is <,=,> val following dir (-1,0,1)
5640  */
5641 int snd_pcm_hw_params_set_periods(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir)
5642 {
5643         return snd_pcm_hw_param_set(pcm, params, SND_TRY, SND_PCM_HW_PARAM_PERIODS, val, dir);
5644 }
5645
5646 /**
5647  * \brief Restrict a configuration space with a minimum periods count
5648  * \param pcm PCM handle
5649  * \param params Configuration space
5650  * \param val approximate minimum periods per buffer (on return filled with actual minimum)
5651  * \param dir Sub unit direction (on return filled with actual direction)
5652  * \return 0 otherwise a negative error code if configuration space would become empty
5653  *
5654  * Wanted/actual exact minimum is <,=,> val following dir (-1,0,1)
5655  */
5656 int snd_pcm_hw_params_set_periods_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5657 {
5658         return snd_pcm_hw_param_set_min(pcm, params, SND_TRY, SND_PCM_HW_PARAM_PERIODS, val, dir);
5659 }
5660
5661 /**
5662  * \brief Restrict a configuration space with a maximum periods count
5663  * \param pcm PCM handle
5664  * \param params Configuration space
5665  * \param val approximate maximum periods per buffer (on return filled with actual maximum)
5666  * \param dir Sub unit direction (on return filled with actual direction)
5667  * \return 0 otherwise a negative error code if configuration space would become empty
5668  *
5669  * Wanted/actual exact maximum is <,=,> val following dir (-1,0,1)
5670  */
5671 int snd_pcm_hw_params_set_periods_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5672 {
5673         return snd_pcm_hw_param_set_max(pcm, params, SND_TRY, SND_PCM_HW_PARAM_PERIODS, val, dir);
5674 }
5675
5676 /**
5677  * \brief Restrict a configuration space to have periods counts in a given range
5678  * \param pcm PCM handle
5679  * \param params Configuration space
5680  * \param min approximate minimum periods per buffer (on return filled with actual minimum)
5681  * \param mindir Sub unit direction for minimum (on return filled with actual direction)
5682  * \param max approximate maximum periods per buffer (on return filled with actual maximum)
5683  * \param maxdir Sub unit direction for maximum (on return filled with actual direction)
5684  * \return 0 otherwise a negative error code if configuration space would become empty
5685  *
5686  * Wanted/actual exact min/max is <,=,> val following dir (-1,0,1)
5687  */
5688 int snd_pcm_hw_params_set_periods_minmax(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *min, int *mindir, unsigned int *max, int *maxdir)
5689 {
5690         return snd_pcm_hw_param_set_minmax(pcm, params, SND_TRY, SND_PCM_HW_PARAM_PERIODS, min, mindir, max, maxdir);
5691 }
5692
5693 /**
5694  * \brief Restrict a configuration space to have periods count nearest to a target
5695  * \param pcm PCM handle
5696  * \param params Configuration space
5697  * \param val approximate target periods per buffer / returned chosen approximate target periods per buffer
5698  * \param dir Sub unit direction
5699  * \return 0 otherwise a negative error code if configuration space is empty
5700  *
5701  * target/chosen exact value is <,=,> val following dir (-1,0,1)
5702  */
5703 #ifndef DOXYGEN
5704 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_periods_near)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5705 #else
5706 int snd_pcm_hw_params_set_periods_near(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5707 #endif
5708 {
5709         return snd_pcm_hw_param_set_near(pcm, params, SND_PCM_HW_PARAM_PERIODS, val, dir);
5710 }
5711
5712 /**
5713  * \brief Restrict a configuration space to contain only its minimum periods count
5714  * \param pcm PCM handle
5715  * \param params Configuration space
5716  * \param val Returned approximate minimum periods per buffer
5717  * \param dir Sub unit direction
5718  * \return 0 otherwise a negative error code
5719  *
5720  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
5721  */
5722 #ifndef DOXYGEN
5723 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_periods_first)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5724 #else
5725 int snd_pcm_hw_params_set_periods_first(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5726 #endif
5727 {
5728         return snd_pcm_hw_param_set_first(pcm, params, SND_PCM_HW_PARAM_PERIODS, val, dir);
5729 }
5730
5731 /**
5732  * \brief Restrict a configuration space to contain only its maximum periods count
5733  * \param pcm PCM handle
5734  * \param params Configuration space
5735  * \param val Returned approximate maximum periods per buffer
5736  * \param dir Sub unit direction
5737  * \return 0 otherwise a negative error code
5738  *
5739  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
5740  */
5741 #ifndef DOXYGEN
5742 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_periods_last)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5743 #else
5744 int snd_pcm_hw_params_set_periods_last(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5745 #endif
5746 {
5747         return snd_pcm_hw_param_set_last(pcm, params, SND_PCM_HW_PARAM_PERIODS, val, dir);
5748 }
5749
5750 /**
5751  * \brief Restrict a configuration space to contain only integer periods counts
5752  * \param pcm PCM handle
5753  * \param params Configuration space
5754  * \return 0 otherwise a negative error code if configuration space would become empty
5755  */
5756 int snd_pcm_hw_params_set_periods_integer(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
5757 {
5758         return snd_pcm_hw_param_set_integer(pcm, params, SND_TRY, SND_PCM_HW_PARAM_PERIODS);
5759 }
5760
5761
5762 /**
5763  * \brief Extract buffer time from a configuration space
5764  * \param params Configuration space
5765  * \param val Returned buffer time in us
5766  * \param dir Sub unit direction
5767  * \return 0 otherwise a negative error code if the configuration space does not contain a single value
5768  *
5769  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
5770  */
5771 #ifndef DOXYGEN
5772 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_buffer_time)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5773 #else
5774 int snd_pcm_hw_params_get_buffer_time(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5775 #endif
5776 {
5777         return snd_pcm_hw_param_get(params, SND_PCM_HW_PARAM_BUFFER_TIME, val, dir);
5778 }
5779
5780 /**
5781  * \brief Extract minimum buffer time from a configuration space
5782  * \param params Configuration space
5783  * \param val approximate minimum buffer duration in us
5784  * \param dir Sub unit direction
5785  * \return 0 otherwise a negative error code
5786  *
5787  * Exact value is <,=,> the returned one following dir (-1,0,1)
5788  */
5789 #ifndef DOXYGEN
5790 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_buffer_time_min)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5791 #else
5792 int snd_pcm_hw_params_get_buffer_time_min(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5793 #endif
5794 {
5795         return snd_pcm_hw_param_get_min(params, SND_PCM_HW_PARAM_BUFFER_TIME, val, dir);
5796 }
5797
5798 /**
5799  * \brief Extract maximum buffer time from a configuration space
5800  * \param params Configuration space
5801  * \param val approximate maximum buffer duration in us
5802  * \param dir Sub unit direction
5803  * \return 0 otherwise a negative error code
5804  *
5805  * Exact value is <,=,> the returned one following dir (-1,0,1)
5806  */
5807 #ifndef DOXYGEN
5808 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_buffer_time_max)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5809 #else
5810 int snd_pcm_hw_params_get_buffer_time_max(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5811 #endif
5812 {
5813         return snd_pcm_hw_param_get_max(params, SND_PCM_HW_PARAM_BUFFER_TIME, val, dir);
5814 }
5815
5816 /**
5817  * \brief Verify if a buffer time is available inside a configuration space for a PCM
5818  * \param pcm PCM handle
5819  * \param params Configuration space
5820  * \param val approximate buffer duration in us
5821  * \param dir Sub unit direction
5822  * \return 0 if available a negative error code otherwise
5823  *
5824  * Wanted exact value is <,=,> val following dir (-1,0,1)
5825  */
5826 int snd_pcm_hw_params_test_buffer_time(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir)
5827 {
5828         return snd_pcm_hw_param_set(pcm, params, SND_TEST, SND_PCM_HW_PARAM_BUFFER_TIME, val, dir);
5829 }
5830
5831 /**
5832  * \brief Restrict a configuration space to contain only one buffer time
5833  * \param pcm PCM handle
5834  * \param params Configuration space
5835  * \param val approximate buffer duration in us
5836  * \param dir Sub unit direction
5837  * \return 0 otherwise a negative error code if configuration space would become empty
5838  *
5839  * Wanted exact value is <,=,> val following dir (-1,0,1)
5840  */
5841 int snd_pcm_hw_params_set_buffer_time(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir)
5842 {
5843         return snd_pcm_hw_param_set(pcm, params, SND_TRY, SND_PCM_HW_PARAM_BUFFER_TIME, val, dir);
5844 }
5845
5846 /**
5847  * \brief Restrict a configuration space with a minimum buffer time
5848  * \param pcm PCM handle
5849  * \param params Configuration space
5850  * \param val approximate minimum buffer duration in us (on return filled with actual minimum)
5851  * \param dir Sub unit direction (on return filled with actual direction)
5852  * \return 0 otherwise a negative error code if configuration space would become empty
5853  *
5854  * Wanted/actual exact minimum is <,=,> val following dir (-1,0,1)
5855  */
5856 int snd_pcm_hw_params_set_buffer_time_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5857 {
5858         return snd_pcm_hw_param_set_min(pcm, params, SND_TRY, SND_PCM_HW_PARAM_BUFFER_TIME, val, dir);
5859 }
5860
5861 /**
5862  * \brief Restrict a configuration space with a maximum buffer time
5863  * \param pcm PCM handle
5864  * \param params Configuration space
5865  * \param val approximate maximum buffer duration in us (on return filled with actual maximum)
5866  * \param dir Sub unit direction (on return filled with actual direction)
5867  * \return 0 otherwise a negative error code if configuration space would become empty
5868  *
5869  * Wanted/actual exact maximum is <,=,> val following dir (-1,0,1)
5870  */
5871 int snd_pcm_hw_params_set_buffer_time_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5872 {
5873         return snd_pcm_hw_param_set_max(pcm, params, SND_TRY, SND_PCM_HW_PARAM_BUFFER_TIME, val, dir);
5874 }
5875
5876 /**
5877  * \brief Restrict a configuration space to have buffer times in a given range
5878  * \param pcm PCM handle
5879  * \param params Configuration space
5880  * \param min approximate minimum buffer duration in us (on return filled with actual minimum)
5881  * \param mindir Sub unit direction for minimum (on return filled with actual direction)
5882  * \param max approximate maximum buffer duration in us (on return filled with actual maximum)
5883  * \param maxdir Sub unit direction for maximum (on return filled with actual direction)
5884  * \return 0 otherwise a negative error code if configuration space would become empty
5885  *
5886  * Wanted/actual exact min/max is <,=,> val following dir (-1,0,1)
5887  */
5888 int snd_pcm_hw_params_set_buffer_time_minmax(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *min, int *mindir, unsigned int *max, int *maxdir)
5889 {
5890         return snd_pcm_hw_param_set_minmax(pcm, params, SND_TRY, SND_PCM_HW_PARAM_BUFFER_TIME, min, mindir, max, maxdir);
5891 }
5892
5893 /**
5894  * \brief Restrict a configuration space to have buffer time nearest to a target
5895  * \param pcm PCM handle
5896  * \param params Configuration space
5897  * \param val approximate target buffer duration in us / returned chosen approximate target buffer duration
5898  * \param dir Sub unit direction
5899  * \return 0 otherwise a negative error code if configuration space is empty
5900  *
5901  * target/chosen exact value is <,=,> val following dir (-1,0,1)
5902  */
5903 #ifndef DOXYGEN
5904 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_buffer_time_near)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5905 #else
5906 int snd_pcm_hw_params_set_buffer_time_near(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5907 #endif
5908 {
5909         return snd_pcm_hw_param_set_near(pcm, params, SND_PCM_HW_PARAM_BUFFER_TIME, val, dir);
5910 }
5911
5912 /**
5913  * \brief Restrict a configuration space to contain only its minimum buffer time
5914  * \param pcm PCM handle
5915  * \param params Configuration space
5916  * \param val Returned approximate minimum buffer duration in us
5917  * \param dir Sub unit direction
5918  * \return 0 otherwise a negative error code
5919  *
5920  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
5921  */
5922 #ifndef DOXYGEN
5923 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_buffer_time_first)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5924 #else
5925 int snd_pcm_hw_params_set_buffer_time_first(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5926 #endif
5927 {
5928         return snd_pcm_hw_param_set_first(pcm, params, SND_PCM_HW_PARAM_BUFFER_TIME, val, dir);
5929 }
5930
5931 /**
5932  * \brief Restrict a configuration space to contain only its maximum buffered time
5933  * \param pcm PCM handle
5934  * \param params Configuration space
5935  * \param val Returned approximate maximum buffer duration in us
5936  * \param dir Sub unit direction
5937  * \return 0 otherwise a negative error code
5938  *
5939  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
5940  */
5941 #ifndef DOXYGEN
5942 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_buffer_time_last)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5943 #else
5944 int snd_pcm_hw_params_set_buffer_time_last(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5945 #endif
5946 {
5947         return snd_pcm_hw_param_set_last(pcm, params, SND_PCM_HW_PARAM_BUFFER_TIME, val, dir);
5948 }
5949
5950
5951 /**
5952  * \brief Extract buffer size from a configuration space
5953  * \param params Configuration space
5954  * \param val Returned buffer size in frames
5955  * \return 0 otherwise a negative error code if the configuration space does not contain a single value
5956  */
5957 #ifndef DOXYGEN
5958 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_buffer_size)(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)
5959 #else
5960 int snd_pcm_hw_params_get_buffer_size(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)
5961 #endif
5962 {
5963         unsigned int _val;
5964         int err = snd_pcm_hw_param_get(params, SND_PCM_HW_PARAM_BUFFER_SIZE, &_val, NULL);
5965         if (err >= 0)
5966                 *val = _val;
5967         return err;
5968 }
5969
5970 /**
5971  * \brief Extract minimum buffer size from a configuration space
5972  * \param params Configuration space
5973  * \param val Returned approximate minimum buffer size in frames
5974  * \return 0 otherwise a negative error code
5975  */
5976 #ifndef DOXYGEN
5977 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_buffer_size_min)(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)
5978 #else
5979 int snd_pcm_hw_params_get_buffer_size_min(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)
5980 #endif
5981 {
5982         unsigned int _val;
5983         int err = snd_pcm_hw_param_get_min(params, SND_PCM_HW_PARAM_BUFFER_SIZE, &_val, NULL);
5984         if (err >= 0)
5985                 *val = _val;
5986         return err;
5987 }
5988
5989 /**
5990  * \brief Extract maximum buffer size from a configuration space
5991  * \param params Configuration space
5992  * \param val Returned approximate maximum buffer size in frames
5993  * \return 0 otherwise a negative error code
5994  *
5995  * Exact value is <,=,> the returned one following dir (-1,0,1)
5996  */
5997 #ifndef DOXYGEN
5998 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_buffer_size_max)(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)
5999 #else
6000 int snd_pcm_hw_params_get_buffer_size_max(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)
6001 #endif
6002 {
6003         unsigned int _val;
6004         int err = snd_pcm_hw_param_get_max(params, SND_PCM_HW_PARAM_BUFFER_SIZE, &_val, NULL);
6005         if (err >= 0)
6006                 *val = _val;
6007         return err;
6008 }
6009
6010 /**
6011  * \brief Verify if a buffer size is available inside a configuration space for a PCM
6012  * \param pcm PCM handle
6013  * \param params Configuration space
6014  * \param val buffer size in frames
6015  * \return 0 if available a negative error code otherwise
6016  *
6017  * Wanted exact value is <,=,> val following dir (-1,0,1)
6018  */
6019 int snd_pcm_hw_params_test_buffer_size(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t val)
6020 {
6021         return snd_pcm_hw_param_set(pcm, params, SND_TEST, SND_PCM_HW_PARAM_BUFFER_SIZE, val, 0);
6022 }
6023
6024 /**
6025  * \brief Restrict a configuration space to contain only one buffer size
6026  * \param pcm PCM handle
6027  * \param params Configuration space
6028  * \param val buffer size in frames
6029  * \return 0 otherwise a negative error code if configuration space would become empty
6030  *
6031  * Wanted exact value is <,=,> val following dir (-1,0,1)
6032  */
6033 int snd_pcm_hw_params_set_buffer_size(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t val)
6034 {
6035         return snd_pcm_hw_param_set(pcm, params, SND_TRY, SND_PCM_HW_PARAM_BUFFER_SIZE, val, 0);
6036 }
6037
6038 /**
6039  * \brief Restrict a configuration space with a minimum buffer size
6040  * \param pcm PCM handle
6041  * \param params Configuration space
6042  * \param val approximate minimum buffer size in frames (on return filled with actual minimum)
6043  * \return 0 otherwise a negative error code if configuration space would become empty
6044  */
6045 int snd_pcm_hw_params_set_buffer_size_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)
6046 {
6047         unsigned int _val = *val;
6048         int err = snd_pcm_hw_param_set_min(pcm, params, SND_TRY, SND_PCM_HW_PARAM_BUFFER_SIZE, &_val, NULL);
6049         if (err >= 0)
6050                 *val = _val;
6051         return err;
6052 }
6053
6054 /**
6055  * \brief Restrict a configuration space with a maximum buffer size
6056  * \param pcm PCM handle
6057  * \param params Configuration space
6058  * \param val approximate maximum buffer size in frames (on return filled with actual maximum)
6059  * \return 0 otherwise a negative error code if configuration space would become empty
6060  */
6061 int snd_pcm_hw_params_set_buffer_size_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)
6062 {
6063         unsigned int _val = *val;
6064         int err = snd_pcm_hw_param_set_max(pcm, params, SND_TRY, SND_PCM_HW_PARAM_BUFFER_SIZE, &_val, NULL);
6065         if (err >= 0)
6066                 *val = _val;
6067         return err;
6068 }
6069
6070 /**
6071  * \brief Restrict a configuration space to have buffer sizes in a given range
6072  * \param pcm PCM handle
6073  * \param params Configuration space
6074  * \param min approximate minimum buffer size in frames (on return filled with actual minimum)
6075  * \param max approximate maximum buffer size in frames (on return filled with actual maximum)
6076  * \return 0 otherwise a negative error code if configuration space would become empty
6077  */
6078 int snd_pcm_hw_params_set_buffer_size_minmax(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *min, snd_pcm_uframes_t *max)
6079 {
6080         unsigned int _min = *min;
6081         unsigned int _max = *max;
6082         int err = snd_pcm_hw_param_set_minmax(pcm, params, SND_TRY, SND_PCM_HW_PARAM_BUFFER_SIZE, &_min, NULL, &_max, NULL);
6083         *min = _min;
6084         *max = _max;
6085         return err;
6086 }
6087
6088 /**
6089  * \brief Restrict a configuration space to have buffer size nearest to a target
6090  * \param pcm PCM handle
6091  * \param params Configuration space
6092  * \param val approximate target buffer size in frames / returned chosen approximate target buffer size in frames
6093  * \return 0 otherwise a negative error code if configuration space is empty
6094  */
6095 #ifndef DOXYGEN
6096 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_buffer_size_near)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)
6097 #else
6098 int snd_pcm_hw_params_set_buffer_size_near(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)
6099 #endif
6100 {
6101         unsigned int _val = *val;
6102         int err = snd_pcm_hw_param_set_near(pcm, params, SND_PCM_HW_PARAM_BUFFER_SIZE, &_val, NULL);
6103         if (err >= 0)
6104                 *val = _val;
6105         return err;
6106 }
6107
6108 /**
6109  * \brief Restrict a configuration space to contain only its minimum buffer size
6110  * \param pcm PCM handle
6111  * \param params Configuration space
6112  * \param val Returned minimum buffer size in frames
6113  * \return buffer size in frames
6114  */
6115 #ifndef DOXYGEN
6116 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_buffer_size_first)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)
6117 #else
6118 int snd_pcm_hw_params_set_buffer_size_first(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)
6119 #endif
6120 {
6121         unsigned int _val;
6122         int err = snd_pcm_hw_param_set_first(pcm, params, SND_PCM_HW_PARAM_BUFFER_SIZE, &_val, NULL);
6123         if (err >= 0)
6124                 *val = _val;
6125         return err;
6126 }
6127
6128 /**
6129  * \brief Restrict a configuration space to contain only its maximum buffer size
6130  * \param pcm PCM handle
6131  * \param params Configuration space
6132  * \param val Returned maximum buffer size in frames
6133  * \return 0 otherwise a negative error code
6134  */
6135 #ifndef DOXYGEN
6136 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_buffer_size_last)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)
6137 #else
6138 int snd_pcm_hw_params_set_buffer_size_last(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)
6139 #endif
6140 {
6141         unsigned int _val;
6142         int err = snd_pcm_hw_param_set_last(pcm, params, SND_PCM_HW_PARAM_BUFFER_SIZE, &_val, NULL);
6143         if (err >= 0)
6144                 *val = _val;
6145         return err;
6146 }
6147
6148
6149 /**
6150  * \brief (DEPRECATED) Extract tick time from a configuration space
6151  * \param params Configuration space
6152  * \param val Returned approximate tick duration in us
6153  * \param dir Sub unit direction
6154  * \return 0 otherwise a negative error code if the configuration space does not contain a single value
6155  *
6156  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
6157  */
6158 #ifndef DOXYGEN
6159 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_tick_time)(const snd_pcm_hw_params_t *params ATTRIBUTE_UNUSED, unsigned int *val, int *dir ATTRIBUTE_UNUSED)
6160 #else
6161 int snd_pcm_hw_params_get_tick_time(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
6162 #endif
6163 {
6164         *val = 0;
6165         return 0;
6166 }
6167
6168 /**
6169  * \brief (DEPRECATED) Extract minimum tick time from a configuration space
6170  * \param params Configuration space
6171  * \param val Returned approximate minimum tick duration in us
6172  * \param dir Sub unit direction
6173  * \return 0 otherwise a negative error code
6174  *
6175  * Exact value is <,=,> the returned one following dir (-1,0,1)
6176  */
6177 #ifndef DOXYGEN
6178 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_tick_time_min)(const snd_pcm_hw_params_t *params ATTRIBUTE_UNUSED, unsigned int *val, int *dir ATTRIBUTE_UNUSED)
6179 #else
6180 int snd_pcm_hw_params_get_tick_time_min(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
6181 #endif
6182 {
6183         *val = 0;
6184         return 0;
6185 }
6186
6187 /**
6188  * \brief (DEPRECATED) Extract maximum tick time from a configuration space
6189  * \param params Configuration space
6190  * \param val Returned approximate maximum tick duration in us
6191  * \param dir Sub unit direction
6192  * \return 0 otherwise a negative error code
6193  *
6194  * Exact value is <,=,> the returned one following dir (-1,0,1)
6195  */
6196 #ifndef DOXYGEN
6197 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_tick_time_max)(const snd_pcm_hw_params_t *params ATTRIBUTE_UNUSED, unsigned int *val, int *dir ATTRIBUTE_UNUSED)
6198 #else
6199 int snd_pcm_hw_params_get_tick_time_max(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
6200 #endif
6201 {
6202         *val = 0;
6203         return 0;
6204 }
6205
6206 /**
6207  * \brief (DEPRECATED) Verify if a tick time is available inside a configuration space for a PCM
6208  * \param pcm PCM handle
6209  * \param params Configuration space
6210  * \param val approximate tick duration in us
6211  * \param dir Sub unit direction
6212  * \return 0 if available a negative error code otherwise
6213  *
6214  * Wanted exact value is <,=,> val following dir (-1,0,1)
6215  */
6216 int snd_pcm_hw_params_test_tick_time(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params ATTRIBUTE_UNUSED, unsigned int val, int dir ATTRIBUTE_UNUSED)
6217 {
6218         return val ? -EINVAL : 0;
6219 }
6220
6221 /**
6222  * \brief (DEPRECATED) Restrict a configuration space to contain only one tick time
6223  * \param pcm PCM handle
6224  * \param params Configuration space
6225  * \param val approximate tick duration in us
6226  * \param dir Sub unit direction
6227  * \return 0 otherwise a negative error code if configuration space would become empty
6228  *
6229  * Wanted exact value is <,=,> val following dir (-1,0,1)
6230  */
6231 int snd_pcm_hw_params_set_tick_time(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params ATTRIBUTE_UNUSED, unsigned int val ATTRIBUTE_UNUSED, int dir ATTRIBUTE_UNUSED)
6232 {
6233         return 0;
6234 }
6235
6236 /**
6237  * \brief (DEPRECATED) Restrict a configuration space with a minimum tick time
6238  * \param pcm PCM handle
6239  * \param params Configuration space
6240  * \param val approximate minimum tick duration in us (on return filled with actual minimum)
6241  * \param dir Sub unit direction (on return filled with actual direction)
6242  * \return 0 otherwise a negative error code if configuration space would become empty
6243  *
6244  * Wanted/actual exact minimum is <,=,> val following dir (-1,0,1)
6245  */
6246 int snd_pcm_hw_params_set_tick_time_min(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params ATTRIBUTE_UNUSED, unsigned int *val ATTRIBUTE_UNUSED, int *dir ATTRIBUTE_UNUSED)
6247 {
6248         return 0;
6249 }
6250
6251 /**
6252  * \brief (DEPRECATED) Restrict a configuration space with a maximum tick time
6253  * \param pcm PCM handle
6254  * \param params Configuration space
6255  * \param val approximate maximum tick duration in us (on return filled with actual maximum)
6256  * \param dir Sub unit direction (on return filled with actual direction)
6257  * \return 0 otherwise a negative error code if configuration space would become empty
6258  *
6259  * Wanted/actual exact maximum is <,=,> val following dir (-1,0,1)
6260  */
6261 int snd_pcm_hw_params_set_tick_time_max(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params ATTRIBUTE_UNUSED, unsigned int *val ATTRIBUTE_UNUSED, int *dir ATTRIBUTE_UNUSED)
6262 {
6263         return 0;
6264 }
6265
6266 /**
6267  * \brief (DEPRECATED) Restrict a configuration space to have tick times in a given range
6268  * \param pcm PCM handle
6269  * \param params Configuration space
6270  * \param min approximate minimum tick duration in us (on return filled with actual minimum)
6271  * \param mindir Sub unit direction for minimum (on return filled with actual direction)
6272  * \param max approximate maximum tick duration in us (on return filled with actual maximum)
6273  * \param maxdir Sub unit direction for maximum (on return filled with actual direction)
6274  * \return 0 otherwise a negative error code if configuration space would become empty
6275  *
6276  * Wanted/actual exact min/max is <,=,> val following dir (-1,0,1)
6277  */
6278 int snd_pcm_hw_params_set_tick_time_minmax(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params ATTRIBUTE_UNUSED, unsigned int *min ATTRIBUTE_UNUSED, int *mindir ATTRIBUTE_UNUSED, unsigned int *max ATTRIBUTE_UNUSED, int *maxdir ATTRIBUTE_UNUSED)
6279 {
6280         return 0;
6281 }
6282
6283 /**
6284  * \brief (DEPRECATED) Restrict a configuration space to have tick time nearest to a target
6285  * \param pcm PCM handle
6286  * \param params Configuration space
6287  * \param val approximate target tick duration in us / returned chosen approximate target tick duration in us
6288  * \param dir Sub unit direction
6289  * \return 0 otherwise a negative error code if configuration space is empty
6290  *
6291  * target/chosen exact value is <,=,> val following dir (-1,0,1)
6292  */
6293 #ifndef DOXYGEN
6294 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_tick_time_near)(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params ATTRIBUTE_UNUSED, unsigned int *val ATTRIBUTE_UNUSED, int *dir ATTRIBUTE_UNUSED)
6295 #else
6296 int snd_pcm_hw_params_set_tick_time_near(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
6297 #endif
6298 {
6299         return 0;
6300 }
6301
6302 /**
6303  * \brief (DEPRECATED) Restrict a configuration space to contain only its minimum tick time
6304  * \param pcm PCM handle
6305  * \param params Configuration space
6306  * \param val Returned approximate minimum tick duration in us
6307  * \param dir Sub unit direction
6308  * \return 0 otherwise a negative error code
6309  *
6310  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
6311  */
6312 #ifndef DOXYGEN
6313 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_tick_time_first)(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params ATTRIBUTE_UNUSED, unsigned int *val ATTRIBUTE_UNUSED, int *dir ATTRIBUTE_UNUSED)
6314 #else
6315 int snd_pcm_hw_params_set_tick_time_first(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
6316 #endif
6317 {
6318         return 0;
6319 }
6320
6321 /**
6322  * \brief (DEPRECATED) Restrict a configuration space to contain only its maximum tick time
6323  * \param pcm PCM handle
6324  * \param params Configuration space
6325  * \param val Returned approximate maximum tick duration in us
6326  * \param dir Sub unit direction
6327  * \return 0 otherwise a negative error code
6328  *
6329  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
6330  */
6331 #ifndef DOXYGEN
6332 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_tick_time_last)(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params ATTRIBUTE_UNUSED, unsigned int *val ATTRIBUTE_UNUSED, int *dir ATTRIBUTE_UNUSED)
6333 #else
6334 int snd_pcm_hw_params_set_tick_time_last(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
6335 #endif
6336 {
6337         return 0;
6338 }
6339
6340 /**
6341  * \brief Get the minimum transfer align value in samples
6342  * \param params Configuration space
6343  * \param val Returned minimum align value
6344  * \return 0 otherwise a negative error code if the configuration space does not contain a single value
6345  */
6346 int snd_pcm_hw_params_get_min_align(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)
6347 {
6348         unsigned int format, channels, fb, min_align;
6349         int err;
6350
6351         err = snd_pcm_hw_param_get(params, SND_PCM_HW_PARAM_FORMAT, &format, NULL);
6352         if (err < 0)
6353                 return err;
6354         err = snd_pcm_hw_param_get(params, SND_PCM_HW_PARAM_CHANNELS, &channels, NULL);
6355         if (err < 0)
6356                 return err;
6357         // compute frame bits
6358         fb = snd_pcm_format_physical_width((snd_pcm_format_t)format) * channels;
6359         min_align = 1;
6360         while (fb % 8) {
6361                 fb *= 2;
6362                 min_align *= 2;
6363         }
6364         if (val)
6365                 *val = min_align;
6366         return 0;
6367 }
6368
6369 #ifndef DOXYGEN
6370 void snd_pcm_sw_params_current_no_lock(snd_pcm_t *pcm, snd_pcm_sw_params_t *params)
6371 {
6372         params->proto = SNDRV_PCM_VERSION;
6373         params->tstamp_mode = pcm->tstamp_mode;
6374         params->tstamp_type = pcm->tstamp_type;
6375         params->period_step = pcm->period_step;
6376         params->sleep_min = 0;
6377         params->avail_min = pcm->avail_min;
6378         sw_set_period_event(params, pcm->period_event);
6379         params->xfer_align = 1;
6380         params->start_threshold = pcm->start_threshold;
6381         params->stop_threshold = pcm->stop_threshold;
6382         params->silence_threshold = pcm->silence_threshold;
6383         params->silence_size = pcm->silence_size;
6384         params->boundary = pcm->boundary;
6385 }
6386 #endif
6387
6388 /**
6389  * \brief Return current software configuration for a PCM
6390  * \param pcm PCM handle
6391  * \param params Software configuration container
6392  * \return 0 on success otherwise a negative error code
6393  *
6394  * The function is thread-safe when built with the proper option.
6395  */
6396 int snd_pcm_sw_params_current(snd_pcm_t *pcm, snd_pcm_sw_params_t *params)
6397 {
6398         assert(pcm && params);
6399         if (CHECK_SANITY(! pcm->setup)) {
6400                 snd_check(PCM, "PCM not set up");
6401                 return -EIO;
6402         }
6403         __snd_pcm_lock(pcm); /* forced lock due to pcm field changes */
6404         snd_pcm_sw_params_current_no_lock(pcm, params);
6405         __snd_pcm_unlock(pcm);
6406         return 0;
6407 }
6408
6409 /**
6410  * \brief Dump a software configuration
6411  * \param params Software configuration container
6412  * \param out Output handle
6413  * \return 0 on success otherwise a negative error code
6414  */
6415 int snd_pcm_sw_params_dump(snd_pcm_sw_params_t *params, snd_output_t *out)
6416 {
6417         snd_output_printf(out, "tstamp_mode: %s\n", snd_pcm_tstamp_mode_name(params->tstamp_mode));
6418         snd_output_printf(out, "tstamp_type: %s\n", snd_pcm_tstamp_type_name(params->tstamp_type));
6419         snd_output_printf(out, "period_step: %u\n", params->period_step);
6420         snd_output_printf(out, "avail_min: %lu\n", params->avail_min);
6421         snd_output_printf(out, "start_threshold: %ld\n", params->start_threshold);
6422         snd_output_printf(out, "stop_threshold: %ld\n", params->stop_threshold);
6423         snd_output_printf(out, "silence_threshold: %lu\n", params->silence_threshold);
6424         snd_output_printf(out, "silence_size: %lu\n", params->silence_size);
6425         snd_output_printf(out, "boundary: %lu\n", params->boundary);
6426         return 0;
6427 }
6428
6429 /**
6430  * \brief get size of #snd_pcm_sw_params_t
6431  * \return size in bytes
6432  */
6433 size_t snd_pcm_sw_params_sizeof()
6434 {
6435         return sizeof(snd_pcm_sw_params_t);
6436 }
6437
6438 /**
6439  * \brief allocate an invalid #snd_pcm_sw_params_t using standard malloc
6440  * \param ptr returned pointer
6441  * \return 0 on success otherwise negative error code
6442  */
6443 int snd_pcm_sw_params_malloc(snd_pcm_sw_params_t **ptr)
6444 {
6445         assert(ptr);
6446         *ptr = calloc(1, sizeof(snd_pcm_sw_params_t));
6447         if (!*ptr)
6448                 return -ENOMEM;
6449         return 0;
6450 }
6451
6452 /**
6453  * \brief frees a previously allocated #snd_pcm_sw_params_t
6454  * \param obj pointer to object to free
6455  */
6456 void snd_pcm_sw_params_free(snd_pcm_sw_params_t *obj)
6457 {
6458         free(obj);
6459 }
6460
6461 /**
6462  * \brief copy one #snd_pcm_sw_params_t to another
6463  * \param dst pointer to destination
6464  * \param src pointer to source
6465  */
6466 void snd_pcm_sw_params_copy(snd_pcm_sw_params_t *dst, const snd_pcm_sw_params_t *src)
6467 {
6468         assert(dst && src);
6469         *dst = *src;
6470 }
6471
6472 /**
6473  * \brief Get boundary for ring pointers from a software configuration container
6474  * \param params Software configuration container
6475  * \param val Returned boundary in frames
6476  * \return 0 otherwise a negative error code
6477  */
6478 int snd_pcm_sw_params_get_boundary(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val)
6479 {
6480         assert(params);
6481         *val = params->boundary;
6482         return 0;
6483 }
6484
6485 /**
6486  * \brief (DEPRECATED) Set start mode inside a software configuration container
6487  * \param pcm PCM handle
6488  * \param params Software configuration container
6489  * \param val Start mode
6490  * \return 0 otherwise a negative error code
6491  */
6492 int snd_pcm_sw_params_set_start_mode(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_start_t val)
6493 {
6494         assert(pcm && params);
6495         switch (val) {
6496         case SND_PCM_START_DATA:
6497                 params->start_threshold = 1;
6498                 break;
6499         case SND_PCM_START_EXPLICIT:
6500                 params->start_threshold = pcm->boundary;
6501                 break;
6502         default:
6503                 snd_check(PCM, "invalid start mode value %d", val);
6504                 return -EINVAL;
6505         }
6506         return 0;
6507 }
6508
6509 #ifndef DOC_HIDDEN
6510 link_warning(snd_pcm_sw_params_set_start_mode, "Warning: start_mode is deprecated, consider to use start_threshold");
6511 #endif
6512
6513 /**
6514  * \brief (DEPRECATED) Get start mode from a software configuration container
6515  * \param params Software configuration container
6516  * \return start mode
6517  */
6518 snd_pcm_start_t snd_pcm_sw_params_get_start_mode(const snd_pcm_sw_params_t *params)
6519 {
6520         assert(params);
6521         /* FIXME: Ugly */
6522         return params->start_threshold > 1024 * 1024 ? SND_PCM_START_EXPLICIT : SND_PCM_START_DATA;
6523 }
6524
6525 #ifndef DOC_HIDDEN
6526 link_warning(snd_pcm_sw_params_get_start_mode, "Warning: start_mode is deprecated, consider to use start_threshold");
6527 #endif
6528
6529 /**
6530  * \brief (DEPRECATED) Set xrun mode inside a software configuration container
6531  * \param pcm PCM handle
6532  * \param params Software configuration container
6533  * \param val Xrun mode
6534  * \return 0 otherwise a negative error code
6535  */
6536 #ifndef DOXYGEN
6537 int snd_pcm_sw_params_set_xrun_mode(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_sw_params_t *params, snd_pcm_xrun_t val)
6538 #else
6539 int snd_pcm_sw_params_set_xrun_mode(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_xrun_t val)
6540 #endif
6541 {
6542         assert(pcm && params);
6543         switch (val) {
6544         case SND_PCM_XRUN_STOP:
6545                 params->stop_threshold = pcm->buffer_size;
6546                 break;
6547         case SND_PCM_XRUN_NONE:
6548                 params->stop_threshold = pcm->boundary;
6549                 break;
6550         default:
6551                 snd_check(PCM, "invalid xrun mode value %d", val);
6552                 return -EINVAL;
6553         }
6554         return 0;
6555 }
6556
6557 #ifndef DOC_HIDDEN
6558 link_warning(snd_pcm_sw_params_set_xrun_mode, "Warning: xrun_mode is deprecated, consider to use stop_threshold");
6559 #endif
6560
6561 /**
6562  * \brief (DEPRECATED) Get xrun mode from a software configuration container
6563  * \param params Software configuration container
6564  * \return xrun mode
6565  */
6566 snd_pcm_xrun_t snd_pcm_sw_params_get_xrun_mode(const snd_pcm_sw_params_t *params)
6567 {
6568         assert(params);
6569         /* FIXME: Ugly */
6570         return params->stop_threshold > 1024 * 1024 ? SND_PCM_XRUN_NONE : SND_PCM_XRUN_STOP;
6571 }
6572
6573 #ifndef DOC_HIDDEN
6574 link_warning(snd_pcm_sw_params_get_xrun_mode, "Warning: xrun_mode is deprecated, consider to use stop_threshold");
6575 #endif
6576
6577 /**
6578  * \brief Set timestamp mode inside a software configuration container
6579  * \param pcm PCM handle
6580  * \param params Software configuration container
6581  * \param val Timestamp mode
6582  * \return 0 otherwise a negative error code
6583  */
6584 #ifndef DOXYGEN
6585 int snd_pcm_sw_params_set_tstamp_mode(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_sw_params_t *params, snd_pcm_tstamp_t val)
6586 #else
6587 int snd_pcm_sw_params_set_tstamp_mode(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_tstamp_t val)
6588 #endif
6589 {
6590         assert(pcm && params);
6591         if (CHECK_SANITY(val > SND_PCM_TSTAMP_LAST)) {
6592                 snd_check(PCM, "invalid tstamp_mode value %d", val);
6593                 return -EINVAL;
6594         }
6595         params->tstamp_mode = val;
6596         return 0;
6597 }
6598
6599 /**
6600  * \brief Get timestamp mode from a software configuration container
6601  * \param params Software configuration container
6602  * \param val Returned timestamp
6603  * \return 0 otherwise a negative error code
6604  */
6605 #ifndef DOXYGEN
6606 EXPORT_SYMBOL int INTERNAL(snd_pcm_sw_params_get_tstamp_mode)(const snd_pcm_sw_params_t *params, snd_pcm_tstamp_t *val)
6607 #else
6608 int snd_pcm_sw_params_get_tstamp_mode(const snd_pcm_sw_params_t *params, snd_pcm_tstamp_t *val)
6609 #endif
6610 {
6611         assert(params && val);
6612         *val = params->tstamp_mode;
6613         return 0;
6614 }
6615
6616 /**
6617  * \brief Set timestamp type inside a software configuration container
6618  * \param pcm PCM handle
6619  * \param params Software configuration container
6620  * \param val Timestamp type
6621  * \return 0 otherwise a negative error code
6622  */
6623 int snd_pcm_sw_params_set_tstamp_type(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_tstamp_type_t val)
6624 {
6625         assert(pcm && params);
6626         if (CHECK_SANITY(val > SND_PCM_TSTAMP_TYPE_LAST)) {
6627                 snd_check(PCM, "invalid tstamp_type value %d", val);
6628                 return -EINVAL;
6629         }
6630         params->tstamp_type = val;
6631         return 0;
6632 }
6633
6634 /**
6635  * \brief Get timestamp type from a software configuration container
6636  * \param params Software configuration container
6637  * \param val Returned timestamp type
6638  * \return 0 otherwise a negative error code
6639  */
6640 int snd_pcm_sw_params_get_tstamp_type(const snd_pcm_sw_params_t *params, snd_pcm_tstamp_type_t *val)
6641 {
6642         assert(params && val);
6643         *val = params->tstamp_type;
6644         return 0;
6645 }
6646
6647 /**
6648  * \brief (DEPRECATED) Set minimum number of ticks to sleep inside a software configuration container
6649  * \param pcm PCM handle
6650  * \param params Software configuration container
6651  * \param val Minimum ticks to sleep or 0 to disable the use of tick timer
6652  * \return 0 otherwise a negative error code
6653  */
6654 #ifndef DOXYGEN
6655 int snd_pcm_sw_params_set_sleep_min(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_sw_params_t *params ATTRIBUTE_UNUSED, unsigned int val ATTRIBUTE_UNUSED)
6656 #else
6657 int snd_pcm_sw_params_set_sleep_min(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, unsigned int val)
6658 #endif
6659 {
6660         return 0;
6661 }
6662
6663 /**
6664  * \brief (DEPRECATED) Get minimum numbers of ticks to sleep from a software configuration container
6665  * \param params Software configuration container
6666  * \param val returned minimum number of ticks to sleep or 0 if tick timer is disabled
6667  * \return 0 otherwise a negative error code
6668  */
6669 #ifndef DOXYGEN
6670 EXPORT_SYMBOL int INTERNAL(snd_pcm_sw_params_get_sleep_min)(const snd_pcm_sw_params_t *params ATTRIBUTE_UNUSED, unsigned int *val)
6671 #else
6672 int snd_pcm_sw_params_get_sleep_min(const snd_pcm_sw_params_t *params, unsigned int *val)
6673 #endif
6674 {
6675         *val = 0;
6676         return 0;
6677 }
6678
6679 /**
6680  * \brief Set avail min inside a software configuration container
6681  * \param pcm PCM handle
6682  * \param params Software configuration container
6683  * \param val Minimum avail frames to consider PCM ready
6684  * \return 0 otherwise a negative error code
6685  *
6686  * Note: This is similar to setting an OSS wakeup point.  The valid
6687  * values for 'val' are determined by the specific hardware.  Most PC
6688  * sound cards can only accept power of 2 frame counts (i.e. 512,
6689  * 1024, 2048).  You cannot use this as a high resolution timer - it
6690  * is limited to how often the sound card hardware raises an
6691  * interrupt.
6692  */
6693 #ifndef DOXYGEN
6694 EXPORT_SYMBOL int snd_pcm_sw_params_set_avail_min(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val)
6695 #else
6696 int snd_pcm_sw_params_set_avail_min(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val)
6697 #endif
6698 {
6699         assert(pcm && params);
6700         /* Fix avail_min if it's below period size.  The period_size
6701          * defines the minimal wake-up timing accuracy, so it doesn't
6702          * make sense to set below that.
6703          */
6704         if (val < pcm->period_size)
6705                 val = pcm->period_size;
6706         params->avail_min = val;
6707         return 0;
6708 }
6709
6710 /**
6711  * \brief Get avail min from a software configuration container
6712  * \param params Software configuration container
6713  * \param val returned minimum available frames to consider PCM ready
6714  * \return 0 otherwise a negative error code
6715  *
6716  * This is a threshold value when the PCM stream is considered as ready for
6717  * another read/write operation or poll event.
6718  */
6719 #ifndef DOXYGEN
6720 EXPORT_SYMBOL int INTERNAL(snd_pcm_sw_params_get_avail_min)(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val)
6721 #else
6722 int snd_pcm_sw_params_get_avail_min(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val)
6723 #endif
6724 {
6725         assert(params && val);
6726         *val = params->avail_min;
6727         return 0;
6728 }
6729
6730 /**
6731  * \brief Set period event inside a software configuration container
6732  * \param pcm PCM handle
6733  * \param params Software configuration container
6734  * \param val 0 = disable period event, 1 = enable period event
6735  * \return 0 otherwise a negative error code
6736  *
6737  * An poll (select) wakeup event is raised if enabled.
6738  */
6739 int snd_pcm_sw_params_set_period_event(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, int val)
6740 {
6741         assert(pcm && params);
6742         sw_set_period_event(params, val);
6743         return 0;
6744 }
6745
6746 /**
6747  * \brief Get period event from a software configuration container
6748  * \param params Software configuration container
6749  * \param val returned period event state
6750  * \return 0 otherwise a negative error code
6751  */
6752 int snd_pcm_sw_params_get_period_event(const snd_pcm_sw_params_t *params, int *val)
6753 {
6754         assert(params && val);
6755         *val = sw_get_period_event(params);
6756         return 0;
6757 }
6758
6759 /**
6760  * \brief (DEPRECATED) Set xfer align inside a software configuration container
6761  * \param pcm PCM handle
6762  * \param params Software configuration container
6763  * \param val Chunk size (frames are attempted to be transferred in chunks)
6764  * \return 0 otherwise a negative error code
6765  */
6766 #ifndef DOXYGEN
6767 EXPORT_SYMBOL int snd_pcm_sw_params_set_xfer_align(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_sw_params_t *params ATTRIBUTE_UNUSED, snd_pcm_uframes_t val ATTRIBUTE_UNUSED)
6768 #else
6769 int snd_pcm_sw_params_set_xfer_align(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val)
6770 #endif
6771 {
6772         return 0;
6773 }
6774
6775 /**
6776  * \brief (DEPRECATED) Get xfer align from a software configuration container
6777  * \param params Software configuration container
6778  * \param val returned chunk size (frames are attempted to be transferred in chunks)
6779  * \return 0 otherwise a negative error code
6780  */
6781 #ifndef DOXYGEN
6782 EXPORT_SYMBOL int INTERNAL(snd_pcm_sw_params_get_xfer_align)(const snd_pcm_sw_params_t *params ATTRIBUTE_UNUSED, snd_pcm_uframes_t *val)
6783 #else
6784 int snd_pcm_sw_params_get_xfer_align(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val)
6785 #endif
6786 {
6787         *val = 1;
6788         return 0;
6789 }
6790
6791 /**
6792  * \brief Set start threshold inside a software configuration container
6793  * \param pcm PCM handle
6794  * \param params Software configuration container
6795  * \param val Start threshold in frames
6796  * \return 0 otherwise a negative error code
6797  *
6798  * PCM is automatically started when playback frames available to PCM
6799  * are >= threshold or when requested capture frames are >= threshold
6800  */
6801 #ifndef DOXYGEN
6802 EXPORT_SYMBOL int snd_pcm_sw_params_set_start_threshold(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val)
6803 #else
6804 int snd_pcm_sw_params_set_start_threshold(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val)
6805 #endif
6806 {
6807         assert(pcm && params);
6808         params->start_threshold = val;
6809         return 0;
6810 }
6811
6812 /**
6813  * \brief Get start threshold from a software configuration container
6814  * \param params Software configuration container
6815  * \param val Returned start threshold in frames
6816  * \return 0 otherwise a negative error code
6817  *
6818  * PCM is automatically started when playback frames available to PCM
6819  * are >= threshold or when requested capture frames are >= threshold
6820  */
6821 #ifndef DOXYGEN
6822 EXPORT_SYMBOL int INTERNAL(snd_pcm_sw_params_get_start_threshold)(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val)
6823 #else
6824 int snd_pcm_sw_params_get_start_threshold(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val)
6825 #endif
6826 {
6827         assert(params);
6828         *val = params->start_threshold;
6829         return 0;
6830 }
6831
6832
6833 /**
6834  * \brief Set stop threshold inside a software configuration container
6835  * \param pcm PCM handle
6836  * \param params Software configuration container
6837  * \param val Stop threshold in frames
6838  * \return 0 otherwise a negative error code
6839  *
6840  * PCM is automatically stopped in #SND_PCM_STATE_XRUN state when available
6841  * frames is >= threshold. If the stop threshold is equal to boundary (also
6842  * software parameter - sw_param) then automatic stop will be disabled
6843  * (thus device will do the endless loop in the ring buffer).
6844  */
6845 #ifndef DOXYGEN
6846 EXPORT_SYMBOL int snd_pcm_sw_params_set_stop_threshold(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val)
6847 #else
6848 int snd_pcm_sw_params_set_stop_threshold(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val)
6849 #endif
6850 {
6851         assert(pcm && params);
6852         params->stop_threshold = val;
6853         return 0;
6854 }
6855
6856 /**
6857  * \brief Get stop threshold from a software configuration container
6858  * \param params Software configuration container
6859  * \param val Returned stop threshold in frames
6860  * \return 0 otherwise a negative error code
6861  *
6862  * PCM is automatically stopped in #SND_PCM_STATE_XRUN state when available
6863  * frames is >= threshold. If the stop threshold is equal to boundary (also
6864  * software parameter - sw_param) then automatic stop will be disabled
6865  * (thus device will do the endless loop in the ring buffer).
6866  */
6867 #ifndef DOXYGEN
6868 EXPORT_SYMBOL int INTERNAL(snd_pcm_sw_params_get_stop_threshold)(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val)
6869 #else
6870 int snd_pcm_sw_params_get_stop_threshold(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val)
6871 #endif
6872 {
6873         assert(params);
6874         *val = params->stop_threshold;
6875         return 0;
6876 }
6877
6878
6879 /**
6880  * \brief Set silence threshold inside a software configuration container
6881  * \param pcm PCM handle
6882  * \param params Software configuration container
6883  * \param val Silence threshold in frames
6884  * \return 0 otherwise a negative error code
6885  *
6886  * A portion of playback buffer is overwritten with silence (see
6887  * #snd_pcm_sw_params_set_silence_size) when playback underrun is nearer
6888  * than silence threshold.
6889  */
6890 #ifndef DOXYGEN
6891 EXPORT_SYMBOL int snd_pcm_sw_params_set_silence_threshold(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val)
6892 #else
6893 int snd_pcm_sw_params_set_silence_threshold(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val)
6894 #endif
6895 {
6896         assert(pcm && params);
6897         if (CHECK_SANITY(val >= pcm->buffer_size)) {
6898                 snd_check(PCM, "invalid silent_threshold value %ld (buffer_size = %ld)",
6899                                val, pcm->buffer_size);
6900
6901                 return -EINVAL;
6902         }
6903         params->silence_threshold = val;
6904         return 0;
6905 }
6906
6907 /**
6908  * \brief Get silence threshold from a software configuration container
6909  * \param params Software configuration container
6910  * \param val Returned silence threshold in frames
6911  * \return 0 otherwise a negative error value
6912  *
6913  * A portion of playback buffer is overwritten with silence (see
6914  * #snd_pcm_sw_params_set_silence_size) when playback underrun is nearer
6915  * than silence threshold.
6916  */
6917 #ifndef DOXYGEN
6918 EXPORT_SYMBOL int INTERNAL(snd_pcm_sw_params_get_silence_threshold)(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val)
6919 #else
6920 int snd_pcm_sw_params_get_silence_threshold(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val)
6921 #endif
6922 {
6923         assert(params && val);
6924         *val = params->silence_threshold;
6925         return 0;
6926 }
6927
6928
6929 /**
6930  * \brief Set silence size inside a software configuration container
6931  * \param pcm PCM handle
6932  * \param params Software configuration container
6933  * \param val Silence size in frames (0 for disabled)
6934  * \return 0 otherwise a negative error code
6935  *
6936  * A portion of playback buffer is overwritten with silence when playback
6937  * underrun is nearer than silence threshold (see
6938  * #snd_pcm_sw_params_set_silence_threshold)
6939  *
6940  * When drain silence (see #snd_pcm_hw_params_get_drain_silence) is disabled,
6941  * this will also apply for draining, i.e. silence is written also when the
6942  * drain end is nearer than the silence threshold.
6943  *
6944  * The special case is when silence size value is equal or greater than
6945  * boundary. The unused portion of the ring buffer (initial written samples
6946  * are untouched) is filled with silence at start. Later, only just processed
6947  * sample area is filled with silence. Note: silence_threshold must be set to zero.
6948  */
6949 #ifndef DOXYGEN
6950 EXPORT_SYMBOL int snd_pcm_sw_params_set_silence_size(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val)
6951 #else
6952 int snd_pcm_sw_params_set_silence_size(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val)
6953 #endif
6954 {
6955         assert(pcm && params);
6956         if (CHECK_SANITY(val < pcm->boundary && val > pcm->buffer_size)) {
6957                 snd_check(PCM, "invalid silence_size %ld (boundary %ld, buffer_size %ld)",
6958                                val, pcm->boundary, pcm->buffer_size);
6959
6960                 return -EINVAL;
6961         }
6962         params->silence_size = val;
6963         return 0;
6964 }
6965
6966 /**
6967  * \brief Get silence size from a software configuration container
6968  * \param params Software configuration container
6969  * \param val Returned silence size in frames (0 for disabled)
6970  * \return 0 otherwise a negative error code
6971  *
6972  * A portion of playback buffer is overwritten with silence when playback
6973  * underrun is nearer than silence threshold (see
6974  * #snd_pcm_sw_params_set_silence_threshold)
6975  */
6976 #ifndef DOXYGEN
6977 EXPORT_SYMBOL int INTERNAL(snd_pcm_sw_params_get_silence_size)(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val)
6978 #else
6979 int snd_pcm_sw_params_get_silence_size(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val)
6980 #endif
6981 {
6982         assert(params);
6983         *val = params->silence_size;
6984         return 0;
6985 }
6986
6987
6988 /**
6989  * \brief get size of #snd_pcm_status_t
6990  * \return size in bytes
6991  */
6992 size_t snd_pcm_status_sizeof()
6993 {
6994         return sizeof(snd_pcm_status_t);
6995 }
6996
6997 /**
6998  * \brief allocate an invalid #snd_pcm_status_t using standard malloc
6999  * \param ptr returned pointer
7000  * \return 0 on success otherwise negative error code
7001  */
7002 int snd_pcm_status_malloc(snd_pcm_status_t **ptr)
7003 {
7004         assert(ptr);
7005         *ptr = calloc(1, sizeof(snd_pcm_status_t));
7006         if (!*ptr)
7007                 return -ENOMEM;
7008         return 0;
7009 }
7010
7011 /**
7012  * \brief frees a previously allocated #snd_pcm_status_t
7013  * \param obj pointer to object to free
7014  */
7015 void snd_pcm_status_free(snd_pcm_status_t *obj)
7016 {
7017         free(obj);
7018 }
7019
7020 /**
7021  * \brief copy one #snd_pcm_status_t to another
7022  * \param dst pointer to destination
7023  * \param src pointer to source
7024  */
7025 void snd_pcm_status_copy(snd_pcm_status_t *dst, const snd_pcm_status_t *src)
7026 {
7027         assert(dst && src);
7028         *dst = *src;
7029 }
7030
7031 /**
7032  * \brief Get state from a PCM status container (see #snd_pcm_state)
7033  * \param obj #snd_pcm_status_t pointer
7034  * \return PCM state
7035  */
7036 snd_pcm_state_t snd_pcm_status_get_state(const snd_pcm_status_t *obj)
7037 {
7038         assert(obj);
7039         return obj->state;
7040 }
7041
7042 /**
7043  * \brief Get trigger timestamp from a PCM status container
7044  * \param obj #snd_pcm_status_t pointer
7045  * \param ptr Pointer to returned timestamp
7046  *
7047  * Trigger means a PCM state transition (from stopped to running or
7048  * versa vice). It applies also to pause and suspend. In other words,
7049  * timestamp contains time when stream started or when it was stopped.
7050  */
7051 void snd_pcm_status_get_trigger_tstamp(const snd_pcm_status_t *obj, snd_timestamp_t *ptr)
7052 {
7053         assert(obj && ptr);
7054         ptr->tv_sec = obj->trigger_tstamp.tv_sec;
7055         ptr->tv_usec = obj->trigger_tstamp.tv_nsec / 1000L;
7056 }
7057
7058 /**
7059  * \brief Get trigger hi-res timestamp from a PCM status container
7060  * \param obj #snd_pcm_status_t pointer
7061  * \param ptr Pointer to returned timestamp
7062  *
7063  * Trigger means a PCM state transition (from stopped to running or
7064  * versa vice). It applies also to pause and suspend. In other words,
7065  * timestamp contains time when stream started or when it was stopped.
7066  */
7067 #ifndef DOXYGEN
7068 EXPORT_SYMBOL void INTERNAL(snd_pcm_status_get_trigger_htstamp)(const snd_pcm_status_t *obj, snd_htimestamp_t *ptr)
7069 #else
7070 void snd_pcm_status_get_trigger_htstamp(const snd_pcm_status_t *obj, snd_htimestamp_t *ptr)
7071 #endif
7072 {
7073         assert(obj && ptr);
7074         *ptr = obj->trigger_tstamp;
7075 }
7076 use_default_symbol_version(__snd_pcm_status_get_trigger_htstamp, snd_pcm_status_get_trigger_htstamp, ALSA_0.9.0rc8);
7077
7078 /**
7079  * \brief Get "now" timestamp from a PCM status container
7080  * \param obj #snd_pcm_status_t pointer
7081  * \param ptr Pointer to returned timestamp
7082  */
7083 void snd_pcm_status_get_tstamp(const snd_pcm_status_t *obj, snd_timestamp_t *ptr)
7084 {
7085         assert(obj && ptr);
7086         ptr->tv_sec = obj->tstamp.tv_sec;
7087         ptr->tv_usec = obj->tstamp.tv_nsec / 1000L;
7088 }
7089
7090 /**
7091  * \brief Get "now" hi-res timestamp from a PCM status container
7092  * \param obj pointer to #snd_pcm_status_t
7093  * \param ptr Pointer to returned timestamp
7094  */
7095 #ifndef DOXYGEN
7096 EXPORT_SYMBOL void INTERNAL(snd_pcm_status_get_htstamp)(const snd_pcm_status_t *obj, snd_htimestamp_t *ptr)
7097 #else
7098 void snd_pcm_status_get_htstamp(const snd_pcm_status_t *obj, snd_htimestamp_t *ptr)
7099 #endif
7100 {
7101         assert(obj && ptr);
7102         *ptr = obj->tstamp;
7103 }
7104 use_default_symbol_version(__snd_pcm_status_get_htstamp, snd_pcm_status_get_htstamp, ALSA_0.9.0rc8);
7105
7106 /**
7107  * \brief Get "now" hi-res audio timestamp from a PCM status container
7108  * \param obj pointer to #snd_pcm_status_t
7109  * \param ptr Pointer to returned timestamp
7110  */
7111 void snd_pcm_status_get_audio_htstamp(const snd_pcm_status_t *obj, snd_htimestamp_t *ptr)
7112 {
7113         assert(obj && ptr);
7114         *ptr = obj->audio_tstamp;
7115 }
7116
7117 /**
7118  * \brief Get "now" hi-res driver timestamp from a PCM status container. Defines when the status
7119  * was generated by driver, may differ from normal timestamp.
7120  * \param obj pointer to #snd_pcm_status_t
7121  * \param ptr Pointer to returned timestamp
7122  */
7123 void snd_pcm_status_get_driver_htstamp(const snd_pcm_status_t *obj, snd_htimestamp_t *ptr)
7124 {
7125         assert(obj && ptr);
7126         *ptr = obj->driver_tstamp;
7127 }
7128
7129 /**
7130  * \brief Get audio_tstamp_report from a PCM status container
7131  * \param obj pointer to #snd_pcm_status_t
7132  * \param audio_tstamp_report Pointer to returned report
7133  */
7134 void snd_pcm_status_get_audio_htstamp_report(const snd_pcm_status_t *obj,
7135                                              snd_pcm_audio_tstamp_report_t *audio_tstamp_report)
7136 {
7137         assert(obj && audio_tstamp_report);
7138         snd_pcm_unpack_audio_tstamp_report(obj->audio_tstamp_data,
7139                                         obj->audio_tstamp_accuracy,
7140                                         audio_tstamp_report);
7141 }
7142
7143 /**
7144  * \brief set audio_tstamp_config from a PCM status container
7145  * \param obj pointer to #snd_pcm_status_t
7146  * \param audio_tstamp_config Pointer to config (valid fields are type_requested and report_delay)
7147  */
7148 void snd_pcm_status_set_audio_htstamp_config(snd_pcm_status_t *obj,
7149                                              snd_pcm_audio_tstamp_config_t *audio_tstamp_config)
7150 {
7151         assert(obj && audio_tstamp_config);
7152         snd_pcm_pack_audio_tstamp_config(&obj->audio_tstamp_data, audio_tstamp_config);
7153 }
7154
7155 /**
7156  * \brief Get delay from a PCM status container (see #snd_pcm_delay)
7157  * \return Delay in frames
7158  *
7159  * Delay is distance between current application frame position and
7160  * sound frame position.
7161  * It's positive and less than buffer size in normal situation,
7162  * negative on playback underrun and greater than buffer size on
7163  * capture overrun.
7164  */
7165 snd_pcm_sframes_t snd_pcm_status_get_delay(const snd_pcm_status_t *obj)
7166 {
7167         assert(obj);
7168         return obj->delay;
7169 }
7170
7171 /**
7172  * \brief Get number of frames available from a PCM status container (see #snd_pcm_avail_update)
7173  * \return Number of frames ready to be read/written
7174  */
7175 snd_pcm_uframes_t snd_pcm_status_get_avail(const snd_pcm_status_t *obj)
7176 {
7177         assert(obj);
7178         return obj->avail;
7179 }
7180
7181 /**
7182  * \brief Get maximum number of frames available from a PCM status container after last #snd_pcm_status call
7183  * \return Maximum number of frames ready to be read/written
7184  *
7185  * This value returns the peak for the available frames between #snd_pcm_status calls.
7186  */
7187 snd_pcm_uframes_t snd_pcm_status_get_avail_max(const snd_pcm_status_t *obj)
7188 {
7189         assert(obj);
7190         return obj->avail_max;
7191 }
7192
7193 /**
7194  * \brief Get count of ADC overrange detections since last call
7195  * \return Count of ADC overrange detections
7196  */
7197 snd_pcm_uframes_t snd_pcm_status_get_overrange(const snd_pcm_status_t *obj)
7198 {
7199         assert(obj);
7200         return obj->overrange;
7201 }
7202
7203 /**
7204  * \brief get size of #snd_pcm_info_t
7205  * \return size in bytes
7206  */
7207 size_t snd_pcm_info_sizeof()
7208 {
7209         return sizeof(snd_pcm_info_t);
7210 }
7211
7212 /**
7213  * \brief allocate an invalid #snd_pcm_info_t using standard malloc
7214  * \param ptr returned pointer
7215  * \return 0 on success otherwise negative error code
7216  */
7217 int snd_pcm_info_malloc(snd_pcm_info_t **ptr)
7218 {
7219         assert(ptr);
7220         *ptr = calloc(1, sizeof(snd_pcm_info_t));
7221         if (!*ptr)
7222                 return -ENOMEM;
7223         return 0;
7224 }
7225
7226 /**
7227  * \brief frees a previously allocated #snd_pcm_info_t
7228  * \param obj pointer to object to free
7229  */
7230 void snd_pcm_info_free(snd_pcm_info_t *obj)
7231 {
7232         free(obj);
7233 }
7234
7235 /**
7236  * \brief copy one #snd_pcm_info_t to another
7237  * \param dst pointer to destination
7238  * \param src pointer to source
7239  */
7240 void snd_pcm_info_copy(snd_pcm_info_t *dst, const snd_pcm_info_t *src)
7241 {
7242         assert(dst && src);
7243         *dst = *src;
7244 }
7245
7246 /**
7247  * \brief Get device from a PCM info container
7248  * \param obj PCM info container
7249  * \return device number
7250  */
7251 unsigned int snd_pcm_info_get_device(const snd_pcm_info_t *obj)
7252 {
7253         assert(obj);
7254         return obj->device;
7255 }
7256
7257 /**
7258  * \brief Get subdevice from a PCM info container
7259  * \param obj PCM info container
7260  * \return subdevice number
7261  */
7262 unsigned int snd_pcm_info_get_subdevice(const snd_pcm_info_t *obj)
7263 {
7264         assert(obj);
7265         return obj->subdevice;
7266 }
7267
7268 /**
7269  * \brief Get stream (direction) from a PCM info container
7270  * \param obj PCM info container
7271  * \return stream
7272  */
7273 snd_pcm_stream_t snd_pcm_info_get_stream(const snd_pcm_info_t *obj)
7274 {
7275         assert(obj);
7276         return obj->stream;
7277 }
7278
7279 /**
7280  * \brief Get card from a PCM info container
7281  * \param obj PCM info container
7282  * \return card number otherwise a negative error code if not associable to a card
7283  */
7284 int snd_pcm_info_get_card(const snd_pcm_info_t *obj)
7285 {
7286         assert(obj);
7287         return obj->card;
7288 }
7289
7290 /**
7291  * \brief Get id from a PCM info container
7292  * \param obj PCM info container
7293  * \return short id of PCM
7294  */
7295 const char *snd_pcm_info_get_id(const snd_pcm_info_t *obj)
7296 {
7297         assert(obj);
7298         return (const char *)obj->id;
7299 }
7300
7301 /**
7302  * \brief Get name from a PCM info container
7303  * \param obj PCM info container
7304  * \return name of PCM
7305  */
7306 const char *snd_pcm_info_get_name(const snd_pcm_info_t *obj)
7307 {
7308         assert(obj);
7309         return (const char *)obj->name;
7310 }
7311
7312 /**
7313  * \brief Get subdevice name from a PCM info container
7314  * \param obj PCM info container
7315  * \return name of used PCM subdevice
7316  */
7317 const char *snd_pcm_info_get_subdevice_name(const snd_pcm_info_t *obj)
7318 {
7319         assert(obj);
7320         return (const char *)obj->subname;
7321 }
7322
7323 /**
7324  * \brief Get class from a PCM info container
7325  * \param obj PCM info container
7326  * \return class of PCM
7327  */
7328 snd_pcm_class_t snd_pcm_info_get_class(const snd_pcm_info_t *obj)
7329 {
7330         assert(obj);
7331         return obj->dev_class;
7332 }
7333
7334 /**
7335  * \brief Get subclass from a PCM info container
7336  * \param obj PCM info container
7337  * \return subclass of PCM
7338  */
7339 snd_pcm_subclass_t snd_pcm_info_get_subclass(const snd_pcm_info_t *obj)
7340 {
7341         assert(obj);
7342         return obj->dev_subclass;
7343 }
7344
7345 /**
7346  * \brief Get subdevices count from a PCM info container
7347  * \param obj PCM info container
7348  * \return subdevices total count of PCM
7349  */
7350 unsigned int snd_pcm_info_get_subdevices_count(const snd_pcm_info_t *obj)
7351 {
7352         assert(obj);
7353         return obj->subdevices_count;
7354 }
7355
7356 /**
7357  * \brief Get available subdevices count from a PCM info container
7358  * \param obj PCM info container
7359  * \return available subdevices count of PCM
7360  */
7361 unsigned int snd_pcm_info_get_subdevices_avail(const snd_pcm_info_t *obj)
7362 {
7363         assert(obj);
7364         return obj->subdevices_avail;
7365 }
7366
7367 /**
7368  * \brief (DEPRECATED) Get hardware synchronization ID from a PCM info container
7369  * \param obj PCM info container
7370  * \return hardware synchronization ID
7371  */
7372 snd_pcm_sync_id_t snd_pcm_info_get_sync(const snd_pcm_info_t *obj)
7373 {
7374         snd_pcm_sync_id_t res;
7375         assert(obj);
7376         bzero(&res, sizeof(res));
7377         return res;
7378 }
7379 #ifndef DOC_HIDDEN
7380 link_warning(snd_pcm_info_get_sync, "Warning: snd_pcm_info_get_sync is deprecated, consider to use snd_pcm_hw_params_get_sync");
7381 #endif
7382
7383 /**
7384  * \brief Set wanted device inside a PCM info container (see #snd_ctl_pcm_info)
7385  * \param obj PCM info container
7386  * \param val Device number
7387  */
7388 void snd_pcm_info_set_device(snd_pcm_info_t *obj, unsigned int val)
7389 {
7390         assert(obj);
7391         obj->device = val;
7392 }
7393
7394 /**
7395  * \brief Set wanted subdevice inside a PCM info container (see #snd_ctl_pcm_info)
7396  * \param obj PCM info container
7397  * \param val Subdevice number
7398  */
7399 void snd_pcm_info_set_subdevice(snd_pcm_info_t *obj, unsigned int val)
7400 {
7401         assert(obj);
7402         obj->subdevice = val;
7403 }
7404
7405 /**
7406  * \brief Set wanted stream inside a PCM info container (see #snd_ctl_pcm_info)
7407  * \param obj PCM info container
7408  * \param val Stream
7409  */
7410 void snd_pcm_info_set_stream(snd_pcm_info_t *obj, snd_pcm_stream_t val)
7411 {
7412         assert(obj);
7413         obj->stream = val;
7414 }
7415
7416 /**
7417  * \brief Application request to access a portion of direct (mmap) area
7418  * \param pcm PCM handle
7419  * \param areas Returned mmap channel areas
7420  * \param offset Returned mmap area offset in area steps (== frames)
7421  * \param frames mmap area portion size in frames (wanted on entry, contiguous available on exit)
7422  * \return 0 on success otherwise a negative error code
7423  *
7424  * It is necessary to call the snd_pcm_avail_update() function directly before
7425  * this call. Otherwise, this function can return a wrong count of available frames.
7426  *
7427  * The function should be called before a sample-direct area can be accessed.
7428  * The resulting size parameter is always less or equal to the input count of frames
7429  * and can be zero, if no frames can be processed (the ring buffer is full).
7430  *
7431  * See the snd_pcm_mmap_commit() function to finish the frame processing in
7432  * the direct areas.
7433  *
7434  * The function is thread-safe when built with the proper option.
7435  */
7436 int snd_pcm_mmap_begin(snd_pcm_t *pcm,
7437                        const snd_pcm_channel_area_t **areas,
7438                        snd_pcm_uframes_t *offset,
7439                        snd_pcm_uframes_t *frames)
7440 {
7441         int err;
7442
7443         err = bad_pcm_state(pcm, P_STATE_RUNNABLE, 0);
7444         if (err < 0)
7445                 return err;
7446         snd_pcm_lock(pcm->fast_op_arg);
7447         err = __snd_pcm_mmap_begin(pcm, areas, offset, frames);
7448         snd_pcm_unlock(pcm->fast_op_arg);
7449         return err;
7450 }
7451
7452 #ifndef DOC_HIDDEN
7453 int __snd_pcm_mmap_begin_generic(snd_pcm_t *pcm, const snd_pcm_channel_area_t **areas,
7454                                  snd_pcm_uframes_t *offset, snd_pcm_uframes_t *frames)
7455 {
7456         snd_pcm_uframes_t cont;
7457         snd_pcm_uframes_t f;
7458         snd_pcm_uframes_t avail;
7459         const snd_pcm_channel_area_t *xareas;
7460
7461         assert(pcm && areas && offset && frames);
7462
7463         /* fallback for plugins that do not specify new callback */
7464         xareas = snd_pcm_mmap_areas(pcm);
7465         if (xareas == NULL)
7466                 return -EBADFD;
7467         *areas = xareas;
7468         *offset = *pcm->appl.ptr % pcm->buffer_size;
7469         avail = snd_pcm_mmap_avail(pcm);
7470         if (avail > pcm->buffer_size)
7471                 avail = pcm->buffer_size;
7472         cont = pcm->buffer_size - *offset;
7473         f = *frames;
7474         if (f > avail)
7475                 f = avail;
7476         if (f > cont)
7477                 f = cont;
7478         *frames = f;
7479         return 0;
7480 }
7481
7482 /* locked version */
7483 int __snd_pcm_mmap_begin(snd_pcm_t *pcm, const snd_pcm_channel_area_t **areas,
7484                          snd_pcm_uframes_t *offset, snd_pcm_uframes_t *frames)
7485 {
7486         assert(pcm && areas && offset && frames);
7487
7488         if (pcm->fast_ops->mmap_begin)
7489                 return pcm->fast_ops->mmap_begin(pcm->fast_op_arg, areas, offset, frames);
7490
7491         return __snd_pcm_mmap_begin_generic(pcm, areas, offset, frames);
7492 }
7493 #endif
7494
7495 /**
7496  * \brief Application has completed the access to area requested with #snd_pcm_mmap_begin
7497  * \param pcm PCM handle
7498  * \param offset area offset in area steps (== frames)
7499  * \param frames area portion size in frames
7500  * \return count of transferred frames otherwise a negative error code
7501  *
7502  * You should pass this function the offset value that
7503  * snd_pcm_mmap_begin() returned. The frames parameter should hold the
7504  * number of frames you have written or read to/from the audio
7505  * buffer. The frames parameter must never exceed the contiguous frames
7506  * count that snd_pcm_mmap_begin() returned. Each call to snd_pcm_mmap_begin()
7507  * must be followed by a call to snd_pcm_mmap_commit().
7508  *
7509  * Example:
7510 \code
7511   double phase = 0;
7512   const snd_pcm_area_t *areas;
7513   snd_pcm_sframes_t avail, size, commitres;
7514   snd_pcm_uframes_t offset, frames;
7515   int err;
7516
7517   avail = snd_pcm_avail_update(pcm);
7518   if (avail < 0)
7519     error(avail);
7520   // at this point, we can transfer at least 'avail' frames
7521
7522   // we want to process frames in chunks (period_size)
7523   if (avail < period_size)
7524     goto _skip;
7525   size = period_size;
7526   // it is possible that contiguous areas are smaller, thus we use a loop
7527   while (size > 0) {
7528     frames = size;
7529
7530     err = snd_pcm_mmap_begin(pcm_handle, &areas, &offset, &frames);
7531     if (err < 0)
7532       error(err);
7533     // this function fills the areas from offset with count of frames
7534     generate_sine(areas, offset, frames, &phase);
7535     commitres = snd_pcm_mmap_commit(pcm_handle, offset, frames);
7536     if (commitres < 0 || commitres != frames)
7537       error(commitres >= 0 ? -EPIPE : commitres);
7538
7539     size -= frames;
7540   }
7541  _skip:
7542 \endcode
7543  *
7544  * Look to the \link example_test_pcm Sine-wave generator \endlink example
7545  * for more details about the generate_sine function.
7546  *
7547  * The function is thread-safe when built with the proper option.
7548  */
7549 snd_pcm_sframes_t snd_pcm_mmap_commit(snd_pcm_t *pcm,
7550                                       snd_pcm_uframes_t offset,
7551                                       snd_pcm_uframes_t frames)
7552 {
7553         snd_pcm_sframes_t result;
7554         int err;
7555
7556         err = bad_pcm_state(pcm, P_STATE_RUNNABLE, 0);
7557         if (err < 0)
7558                 return err;
7559         snd_pcm_lock(pcm->fast_op_arg);
7560         result = __snd_pcm_mmap_commit(pcm, offset, frames);
7561         snd_pcm_unlock(pcm->fast_op_arg);
7562         return result;
7563 }
7564
7565 #ifndef DOC_HIDDEN
7566 /* locked version*/
7567 snd_pcm_sframes_t __snd_pcm_mmap_commit(snd_pcm_t *pcm,
7568                                         snd_pcm_uframes_t offset,
7569                                         snd_pcm_uframes_t frames)
7570 {
7571         assert(pcm);
7572         if (CHECK_SANITY(offset != *pcm->appl.ptr % pcm->buffer_size)) {
7573                 snd_check(PCM, "commit offset (%ld) doesn't match with appl_ptr (%ld) %% buf_size (%ld)",
7574                                offset, *pcm->appl.ptr, pcm->buffer_size);
7575
7576                 return -EPIPE;
7577         }
7578         if (CHECK_SANITY(frames > snd_pcm_mmap_avail(pcm))) {
7579                 snd_check(PCM, "commit frames (%ld) overflow (avail = %ld)", frames,
7580                                snd_pcm_mmap_avail(pcm));
7581
7582                 return -EPIPE;
7583         }
7584         if (pcm->fast_ops->mmap_commit)
7585                 return pcm->fast_ops->mmap_commit(pcm->fast_op_arg, offset, frames);
7586         else
7587                 return -ENOSYS;
7588 }
7589
7590 int _snd_pcm_poll_descriptor(snd_pcm_t *pcm)
7591 {
7592         assert(pcm);
7593         return pcm->poll_fd;
7594 }
7595
7596 void snd_pcm_areas_from_buf(snd_pcm_t *pcm, snd_pcm_channel_area_t *areas,
7597                             void *buf)
7598 {
7599         unsigned int channel;
7600         unsigned int channels;
7601
7602         snd_pcm_lock(pcm);
7603         channels = pcm->channels;
7604         for (channel = 0; channel < channels; ++channel, ++areas) {
7605                 areas->addr = buf;
7606                 areas->first = channel * pcm->sample_bits;
7607                 areas->step = pcm->frame_bits;
7608         }
7609         snd_pcm_unlock(pcm);
7610 }
7611
7612 void snd_pcm_areas_from_bufs(snd_pcm_t *pcm, snd_pcm_channel_area_t *areas,
7613                              void **bufs)
7614 {
7615         unsigned int channel;
7616         unsigned int channels;
7617
7618         snd_pcm_lock(pcm);
7619         channels = pcm->channels;
7620         for (channel = 0; channel < channels; ++channel, ++areas, ++bufs) {
7621                 areas->addr = *bufs;
7622                 areas->first = 0;
7623                 areas->step = pcm->sample_bits;
7624         }
7625         snd_pcm_unlock(pcm);
7626 }
7627
7628 snd_pcm_sframes_t snd_pcm_read_areas(snd_pcm_t *pcm, const snd_pcm_channel_area_t *areas,
7629                                      snd_pcm_uframes_t offset, snd_pcm_uframes_t size,
7630                                      snd_pcm_xfer_areas_func_t func)
7631 {
7632         snd_pcm_uframes_t xfer = 0;
7633         snd_pcm_sframes_t err = 0;
7634         snd_pcm_state_t state;
7635
7636         if (size == 0)
7637                 return 0;
7638
7639         __snd_pcm_lock(pcm->fast_op_arg); /* forced lock */
7640         while (size > 0) {
7641                 snd_pcm_uframes_t frames;
7642                 snd_pcm_sframes_t avail;
7643         _again:
7644                 state = __snd_pcm_state(pcm);
7645                 switch (state) {
7646                 case SND_PCM_STATE_PREPARED:
7647                         err = __snd_pcm_start(pcm);
7648                         if (err < 0)
7649                                 goto _end;
7650                         break;
7651                 case SND_PCM_STATE_RUNNING:
7652                         err = __snd_pcm_hwsync(pcm);
7653                         if (err < 0)
7654                                 goto _end;
7655                         break;
7656                 case SND_PCM_STATE_DRAINING:
7657                 case SND_PCM_STATE_PAUSED:
7658                         break;
7659                 default:
7660                         err = pcm_state_to_error(state);
7661                         if (!err)
7662                                 err = -EBADFD;
7663                         goto _end;
7664                 }
7665                 avail = __snd_pcm_avail_update(pcm);
7666                 if (avail < 0) {
7667                         err = avail;
7668                         goto _end;
7669                 }
7670                 if (avail == 0) {
7671                         if (state == SND_PCM_STATE_DRAINING)
7672                                 goto _end;
7673                         if (pcm->mode & SND_PCM_NONBLOCK) {
7674                                 err = -EAGAIN;
7675                                 goto _end;
7676                         }
7677
7678                         err = __snd_pcm_wait_in_lock(pcm, SND_PCM_WAIT_IO);
7679                         if (err < 0)
7680                                 break;
7681                         goto _again;
7682
7683                 }
7684                 frames = size;
7685                 if (frames > (snd_pcm_uframes_t) avail)
7686                         frames = avail;
7687                 /* frames must be at least 1 here (see while condition) */
7688                 err = func(pcm, areas, offset, frames);
7689                 if (err < 0)
7690                         break;
7691                 frames = err;
7692                 offset += frames;
7693                 size -= frames;
7694                 xfer += frames;
7695         }
7696  _end:
7697         __snd_pcm_unlock(pcm->fast_op_arg);
7698         return xfer > 0 ? (snd_pcm_sframes_t) xfer : snd_pcm_check_error(pcm, err);
7699 }
7700
7701 snd_pcm_sframes_t snd_pcm_write_areas(snd_pcm_t *pcm, const snd_pcm_channel_area_t *areas,
7702                                       snd_pcm_uframes_t offset, snd_pcm_uframes_t size,
7703                                       snd_pcm_xfer_areas_func_t func)
7704 {
7705         snd_pcm_uframes_t xfer = 0;
7706         snd_pcm_sframes_t err = 0;
7707         snd_pcm_state_t state;
7708
7709         if (size == 0)
7710                 return 0;
7711
7712         __snd_pcm_lock(pcm->fast_op_arg); /* forced lock */
7713         while (size > 0) {
7714                 snd_pcm_uframes_t frames;
7715                 snd_pcm_sframes_t avail;
7716         _again:
7717                 state = __snd_pcm_state(pcm);
7718                 switch (state) {
7719                 case SND_PCM_STATE_PREPARED:
7720                 case SND_PCM_STATE_PAUSED:
7721                         break;
7722                 case SND_PCM_STATE_RUNNING:
7723                         err = __snd_pcm_hwsync(pcm);
7724                         if (err < 0)
7725                                 goto _end;
7726                         break;
7727                 default:
7728                         err = pcm_state_to_error(state);
7729                         if (!err)
7730                                 err = -EBADFD;
7731                         goto _end;
7732                 }
7733                 avail = __snd_pcm_avail_update(pcm);
7734                 if (avail < 0) {
7735                         err = avail;
7736                         goto _end;
7737                 }
7738                 if (state == SND_PCM_STATE_RUNNING &&
7739                     size > (snd_pcm_uframes_t)avail) {
7740                         if (snd_pcm_may_wait_for_avail_min(pcm, avail)) {
7741                                 if (pcm->mode & SND_PCM_NONBLOCK) {
7742                                         err = -EAGAIN;
7743                                         goto _end;
7744                                 }
7745
7746                                 err = snd_pcm_wait_nocheck(pcm, SND_PCM_WAIT_IO);
7747                                 if (err < 0)
7748                                         break;
7749                                 goto _again;
7750                         }
7751                         /* the snd_pcm_may_wait_for_avail_min may check against the
7752                          * updated hw.ptr (slaves), get the avail again here
7753                          */
7754                         avail = __snd_pcm_avail_update(pcm);
7755                         if (avail < 0) {
7756                                 err = avail;
7757                                 goto _end;
7758                         }
7759                 }
7760                 frames = size;
7761                 if (frames > (snd_pcm_uframes_t) avail)
7762                         frames = avail;
7763                 if (! frames)
7764                         break;
7765                 err = func(pcm, areas, offset, frames);
7766                 if (err < 0)
7767                         break;
7768                 frames = err;
7769                 if (state == SND_PCM_STATE_PREPARED) {
7770                         snd_pcm_sframes_t hw_avail = pcm->buffer_size - avail;
7771                         hw_avail += frames;
7772                         /* some plugins might automatically start the stream */
7773                         state = __snd_pcm_state(pcm);
7774                         if (state == SND_PCM_STATE_PREPARED &&
7775                             hw_avail >= 0 &&
7776                             (snd_pcm_uframes_t) hw_avail >= pcm->start_threshold) {
7777                                 err = __snd_pcm_start(pcm);
7778                                 if (err < 0)
7779                                         goto _end;
7780                         }
7781                 }
7782                 offset += frames;
7783                 size -= frames;
7784                 xfer += frames;
7785         }
7786  _end:
7787         __snd_pcm_unlock(pcm->fast_op_arg);
7788         return xfer > 0 ? (snd_pcm_sframes_t) xfer : snd_pcm_check_error(pcm, err);
7789 }
7790
7791 snd_pcm_uframes_t _snd_pcm_mmap_hw_ptr(snd_pcm_t *pcm)
7792 {
7793         return *pcm->hw.ptr;
7794 }
7795
7796 snd_pcm_uframes_t _snd_pcm_boundary(snd_pcm_t *pcm)
7797 {
7798         return pcm->boundary;
7799 }
7800
7801 #ifndef DOC_HIDDEN
7802 link_warning(_snd_pcm_mmap_hw_ptr, "Warning: _snd_pcm_mmap_hw_ptr() is deprecated, consider to not use this function");
7803 link_warning(_snd_pcm_boundary, "Warning: _snd_pcm_boundary() is deprecated, consider to use snd_pcm_sw_params_current()");
7804 #endif
7805
7806 static const char *const names[SND_PCM_HW_PARAM_LAST_INTERVAL + 1] = {
7807         [SND_PCM_HW_PARAM_FORMAT] = "format",
7808         [SND_PCM_HW_PARAM_CHANNELS] = "channels",
7809         [SND_PCM_HW_PARAM_RATE] = "rate",
7810         [SND_PCM_HW_PARAM_PERIOD_TIME] = "period_time",
7811         [SND_PCM_HW_PARAM_PERIOD_SIZE] = "period_size",
7812         [SND_PCM_HW_PARAM_BUFFER_TIME] = "buffer_time",
7813         [SND_PCM_HW_PARAM_BUFFER_SIZE] = "buffer_size",
7814         [SND_PCM_HW_PARAM_PERIODS] = "periods"
7815 };
7816
7817 int snd_pcm_slave_conf(snd_config_t *root, snd_config_t *conf,
7818                        snd_config_t **_pcm_conf, unsigned int count, ...)
7819 {
7820         snd_config_iterator_t i, next;
7821         const char *str;
7822         struct {
7823                 unsigned int index;
7824                 int flags;
7825                 void *ptr;
7826                 int present;
7827         } fields[count];
7828         unsigned int k;
7829         snd_config_t *pcm_conf = NULL;
7830         int err;
7831         int to_free = 0;
7832         va_list args;
7833         assert(root);
7834         assert(conf);
7835         assert(_pcm_conf);
7836         if (snd_config_get_string(conf, &str) >= 0) {
7837                 err = snd_config_search_definition(root, "pcm_slave", str, &conf);
7838                 if (err < 0) {
7839                         snd_error(PCM, "Invalid slave definition");
7840                         return -EINVAL;
7841                 }
7842                 to_free = 1;
7843         }
7844         if (snd_config_get_type(conf) != SND_CONFIG_TYPE_COMPOUND) {
7845                 snd_error(PCM, "Invalid slave definition");
7846                 err = -EINVAL;
7847                 goto _err;
7848         }
7849         va_start(args, count);
7850         for (k = 0; k < count; ++k) {
7851                 fields[k].index = va_arg(args, int);
7852                 fields[k].flags = va_arg(args, int);
7853                 fields[k].ptr = va_arg(args, void *);
7854                 fields[k].present = 0;
7855         }
7856         va_end(args);
7857         snd_config_for_each(i, next, conf) {
7858                 snd_config_t *n = snd_config_iterator_entry(i);
7859                 const char *id;
7860                 if (snd_config_get_id(n, &id) < 0)
7861                         continue;
7862                 if (strcmp(id, "comment") == 0)
7863                         continue;
7864                 if (strcmp(id, "pcm") == 0) {
7865                         if (pcm_conf != NULL)
7866                                 snd_config_delete(pcm_conf);
7867                         if ((err = snd_config_copy(&pcm_conf, n)) < 0)
7868                                 goto _err;
7869                         continue;
7870                 }
7871                 for (k = 0; k < count; ++k) {
7872                         unsigned int idx = fields[k].index;
7873                         long v;
7874                         assert(idx < SND_PCM_HW_PARAM_LAST_INTERVAL);
7875                         assert(names[idx]);
7876                         if (strcmp(id, names[idx]) != 0)
7877                                 continue;
7878                         switch (idx) {
7879                         case SND_PCM_HW_PARAM_FORMAT:
7880                         {
7881                                 snd_pcm_format_t f;
7882                                 err = snd_config_get_string(n, &str);
7883                                 if (err < 0) {
7884                                 _invalid:
7885                                         snd_error(PCM, "invalid type for %s", id);
7886                                         goto _err;
7887                                 }
7888                                 if ((fields[k].flags & SCONF_UNCHANGED) &&
7889                                     strcasecmp(str, "unchanged") == 0) {
7890                                         *(snd_pcm_format_t*)fields[k].ptr = (snd_pcm_format_t) -2;
7891                                         break;
7892                                 }
7893                                 f = snd_pcm_format_value(str);
7894                                 if (f == SND_PCM_FORMAT_UNKNOWN) {
7895                                         snd_error(PCM, "unknown format %s", str);
7896                                         err = -EINVAL;
7897                                         goto _err;
7898                                 }
7899                                 *(snd_pcm_format_t*)fields[k].ptr = f;
7900                                 break;
7901                         }
7902                         default:
7903                                 if ((fields[k].flags & SCONF_UNCHANGED)) {
7904                                         err = snd_config_get_string(n, &str);
7905                                         if (err >= 0 &&
7906                                             strcasecmp(str, "unchanged") == 0) {
7907                                                 *(int*)fields[k].ptr = -2;
7908                                                 break;
7909                                         }
7910                                 }
7911                                 err = snd_config_get_integer(n, &v);
7912                                 if (err < 0)
7913                                         goto _invalid;
7914                                 *(int*)fields[k].ptr = v;
7915                                 break;
7916                         }
7917                         fields[k].present = 1;
7918                         break;
7919                 }
7920                 if (k < count)
7921                         continue;
7922                 snd_error(PCM, "Unknown field %s", id);
7923                 err = -EINVAL;
7924                 goto _err;
7925         }
7926         if (!pcm_conf) {
7927                 snd_error(PCM, "missing field pcm");
7928                 err = -EINVAL;
7929                 goto _err;
7930         }
7931         for (k = 0; k < count; ++k) {
7932                 if ((fields[k].flags & SCONF_MANDATORY) && !fields[k].present) {
7933                         snd_error(PCM, "missing field %s", names[fields[k].index]);
7934                         err = -EINVAL;
7935                         goto _err;
7936                 }
7937         }
7938         *_pcm_conf = pcm_conf;
7939         pcm_conf = NULL;
7940         err = 0;
7941  _err:
7942         if (pcm_conf)
7943                 snd_config_delete(pcm_conf);
7944         if (to_free)
7945                 snd_config_delete(conf);
7946         return err;
7947 }
7948
7949 static void snd_pcm_set_ptr(snd_pcm_t *pcm, snd_pcm_rbptr_t *rbptr,
7950                             volatile snd_pcm_uframes_t *hw_ptr, int fd, off_t offset)
7951 {
7952         rbptr->master = NULL;   /* I'm master */
7953         rbptr->ptr = hw_ptr;
7954         rbptr->fd = fd;
7955         rbptr->offset = offset;
7956         if (rbptr->changed)
7957                 rbptr->changed(pcm, NULL);
7958 }
7959
7960 void snd_pcm_set_hw_ptr(snd_pcm_t *pcm, volatile snd_pcm_uframes_t *hw_ptr, int fd, off_t offset)
7961 {
7962         assert(pcm);
7963         assert(hw_ptr);
7964         snd_pcm_set_ptr(pcm, &pcm->hw, hw_ptr, fd, offset);
7965 }
7966
7967 void snd_pcm_set_appl_ptr(snd_pcm_t *pcm, volatile snd_pcm_uframes_t *appl_ptr, int fd, off_t offset)
7968 {
7969         assert(pcm);
7970         assert(appl_ptr);
7971         snd_pcm_set_ptr(pcm, &pcm->appl, appl_ptr, fd, offset);
7972 }
7973
7974 static void snd_pcm_link_ptr(snd_pcm_t *pcm, snd_pcm_rbptr_t *pcm_rbptr,
7975                              snd_pcm_t *slave, snd_pcm_rbptr_t *slave_rbptr)
7976 {
7977         snd_pcm_t **a;
7978         int idx;
7979
7980         a = slave_rbptr->link_dst;
7981         for (idx = 0; idx < slave_rbptr->link_dst_count; idx++)
7982                 if (a[idx] == NULL) {
7983                         a[idx] = pcm;
7984                         goto __found_free_place;
7985                 }
7986         a = realloc(a, sizeof(snd_pcm_t *) * (slave_rbptr->link_dst_count + 1));
7987         if (a == NULL) {
7988                 pcm_rbptr->ptr = NULL;
7989                 pcm_rbptr->fd = -1;
7990                 pcm_rbptr->offset = 0UL;
7991                 return;
7992         }
7993         a[slave_rbptr->link_dst_count++] = pcm;
7994       __found_free_place:
7995         pcm_rbptr->master = slave_rbptr->master ? slave_rbptr->master : slave;
7996         pcm_rbptr->ptr = slave_rbptr->ptr;
7997         pcm_rbptr->fd = slave_rbptr->fd;
7998         pcm_rbptr->offset = slave_rbptr->offset;
7999         slave_rbptr->link_dst = a;
8000         if (pcm_rbptr->changed)
8001                 pcm_rbptr->changed(pcm, slave);
8002 }
8003
8004 static void snd_pcm_unlink_ptr(snd_pcm_t *pcm, snd_pcm_rbptr_t *pcm_rbptr,
8005                                snd_pcm_t *slave, snd_pcm_rbptr_t *slave_rbptr)
8006 {
8007         snd_pcm_t **a;
8008         int idx;
8009
8010         a = slave_rbptr->link_dst;
8011         for (idx = 0; idx < slave_rbptr->link_dst_count; idx++) {
8012                 if (a[idx] == pcm) {
8013                         a[idx] = NULL;
8014                         goto __found;
8015                 }
8016         }
8017         /* assert(0); */
8018         return;
8019
8020       __found:
8021         pcm_rbptr->master = NULL;
8022         pcm_rbptr->ptr = NULL;
8023         pcm_rbptr->fd = -1;
8024         pcm_rbptr->offset = 0UL;
8025         if (pcm_rbptr->changed)
8026                 pcm_rbptr->changed(pcm, slave);
8027 }
8028
8029 void snd_pcm_link_hw_ptr(snd_pcm_t *pcm, snd_pcm_t *slave)
8030 {
8031         assert(pcm);
8032         assert(slave);
8033         snd_pcm_link_ptr(pcm, &pcm->hw, slave, &slave->hw);
8034 }
8035
8036 void snd_pcm_link_appl_ptr(snd_pcm_t *pcm, snd_pcm_t *slave)
8037 {
8038         assert(pcm);
8039         assert(slave);
8040         snd_pcm_link_ptr(pcm, &pcm->appl, slave, &slave->appl);
8041 }
8042
8043 void snd_pcm_unlink_hw_ptr(snd_pcm_t *pcm, snd_pcm_t *slave)
8044 {
8045         assert(pcm);
8046         assert(slave);
8047         snd_pcm_unlink_ptr(pcm, &pcm->hw, slave, &slave->hw);
8048 }
8049
8050 void snd_pcm_unlink_appl_ptr(snd_pcm_t *pcm, snd_pcm_t *slave)
8051 {
8052         assert(pcm);
8053         assert(slave);
8054         snd_pcm_unlink_ptr(pcm, &pcm->appl, slave, &slave->appl);
8055 }
8056
8057 #endif /* DOC_HIDDEN */
8058
8059 /*
8060  *
8061  */
8062
8063 #if !defined(DOC_HIDDEN) && !defined(__COVERITY__)
8064
8065 #ifdef USE_VERSIONED_SYMBOLS
8066
8067 #define OBSOLETE1(name, what, new) \
8068   default_symbol_version(__##name, name, new); \
8069   symbol_version(__old_##name, name, what);
8070
8071 #else
8072
8073 #define OBSOLETE1(name, what, new) \
8074   use_default_symbol_version(__##name, name, new);
8075
8076 #endif /* USE_VERSIONED_SYMBOLS */
8077
8078 #define __P_OLD_GET(pfx, name, val_type, ret_type) \
8079 EXPORT_SYMBOL ret_type pfx##name(const snd_pcm_hw_params_t *params) \
8080 { \
8081         val_type val; \
8082         if (INTERNAL(name)(params, &val) < 0) \
8083                 return 0; \
8084         return (ret_type)val; \
8085 }
8086
8087 #define __P_OLD_GET1(pfx, name, val_type, ret_type) \
8088 EXPORT_SYMBOL ret_type pfx##name(const snd_pcm_hw_params_t *params, int *dir) \
8089 { \
8090         val_type val; \
8091         if (INTERNAL(name)(params, &val, dir) < 0) \
8092                 return 0; \
8093         return (ret_type)val; \
8094 }
8095
8096 #define __OLD_GET(name, val_type, ret_type) __P_OLD_GET(__old_, name, val_type, ret_type)
8097 #define __OLD_GET1(name, val_type, ret_type) __P_OLD_GET1(__old_, name, val_type, ret_type)
8098
8099 __OLD_GET(snd_pcm_hw_params_get_access, snd_pcm_access_t, int);
8100 __OLD_GET(snd_pcm_hw_params_get_format, snd_pcm_format_t, int);
8101 __OLD_GET(snd_pcm_hw_params_get_subformat, snd_pcm_subformat_t, int);
8102 __OLD_GET(snd_pcm_hw_params_get_channels, unsigned int, int);
8103 __OLD_GET1(snd_pcm_hw_params_get_rate, unsigned int, int);
8104 __OLD_GET1(snd_pcm_hw_params_get_period_time, unsigned int, int);
8105 __OLD_GET1(snd_pcm_hw_params_get_period_size, snd_pcm_uframes_t, snd_pcm_sframes_t);
8106 __OLD_GET1(snd_pcm_hw_params_get_periods, unsigned int, int);
8107 __OLD_GET1(snd_pcm_hw_params_get_buffer_time, unsigned int, int);
8108 __OLD_GET(snd_pcm_hw_params_get_buffer_size, snd_pcm_uframes_t, snd_pcm_sframes_t);
8109 __OLD_GET1(snd_pcm_hw_params_get_tick_time, unsigned int, int);
8110
8111 __OLD_GET(snd_pcm_hw_params_get_channels_min, unsigned int, unsigned int);
8112 __OLD_GET1(snd_pcm_hw_params_get_rate_min, unsigned int, unsigned int);
8113 __OLD_GET1(snd_pcm_hw_params_get_period_time_min, unsigned int, unsigned int);
8114 __OLD_GET1(snd_pcm_hw_params_get_period_size_min, snd_pcm_uframes_t, snd_pcm_uframes_t);
8115 __OLD_GET1(snd_pcm_hw_params_get_periods_min, unsigned int, unsigned int);
8116 __OLD_GET1(snd_pcm_hw_params_get_buffer_time_min, unsigned int, unsigned int);
8117 __OLD_GET(snd_pcm_hw_params_get_buffer_size_min, snd_pcm_uframes_t, snd_pcm_uframes_t);
8118 __OLD_GET1(snd_pcm_hw_params_get_tick_time_min, unsigned int, unsigned int);
8119
8120 __OLD_GET(snd_pcm_hw_params_get_channels_max, unsigned int, unsigned int);
8121 __OLD_GET1(snd_pcm_hw_params_get_rate_max, unsigned int, unsigned int);
8122 __OLD_GET1(snd_pcm_hw_params_get_period_time_max, unsigned int, unsigned int);
8123 __OLD_GET1(snd_pcm_hw_params_get_period_size_max, snd_pcm_uframes_t, snd_pcm_uframes_t);
8124 __OLD_GET1(snd_pcm_hw_params_get_periods_max, unsigned int, unsigned int);
8125 __OLD_GET1(snd_pcm_hw_params_get_buffer_time_max, unsigned int, unsigned int);
8126 __OLD_GET(snd_pcm_hw_params_get_buffer_size_max, snd_pcm_uframes_t, snd_pcm_uframes_t);
8127 __OLD_GET1(snd_pcm_hw_params_get_tick_time_max, unsigned int, unsigned int);
8128
8129 #define __P_OLD_NEAR(pfx, name, ret_type) \
8130 EXPORT_SYMBOL ret_type pfx##name(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, ret_type val) \
8131 { \
8132         if (INTERNAL(name)(pcm, params, &val) < 0) \
8133                 return 0; \
8134         return (ret_type)val; \
8135 }
8136
8137 #define __P_OLD_NEAR1(pfx, name, ret_type) \
8138 EXPORT_SYMBOL ret_type pfx##name(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, ret_type val, int *dir) \
8139 { \
8140         if (INTERNAL(name)(pcm, params, &val, dir) < 0) \
8141                 return 0; \
8142         return (ret_type)val; \
8143 }
8144
8145 #define __OLD_NEAR(name, ret_type) __P_OLD_NEAR(__old_, name, ret_type)
8146 #define __OLD_NEAR1(name, ret_type) __P_OLD_NEAR1(__old_, name, ret_type)
8147
8148 __OLD_NEAR(snd_pcm_hw_params_set_channels_near, unsigned int);
8149 __OLD_NEAR1(snd_pcm_hw_params_set_rate_near, unsigned int);
8150 __OLD_NEAR1(snd_pcm_hw_params_set_period_time_near, unsigned int);
8151 __OLD_NEAR1(snd_pcm_hw_params_set_period_size_near, snd_pcm_uframes_t);
8152 __OLD_NEAR1(snd_pcm_hw_params_set_periods_near, unsigned int);
8153 __OLD_NEAR1(snd_pcm_hw_params_set_buffer_time_near, unsigned int);
8154 __OLD_NEAR(snd_pcm_hw_params_set_buffer_size_near, snd_pcm_uframes_t);
8155 __OLD_NEAR1(snd_pcm_hw_params_set_tick_time_near, unsigned int);
8156
8157 #define __P_OLD_SET_FL(pfx, name, ret_type) \
8158 EXPORT_SYMBOL ret_type pfx##name(snd_pcm_t *pcm, snd_pcm_hw_params_t *params) \
8159 { \
8160         ret_type val; \
8161         if (INTERNAL(name)(pcm, params, &val) < 0) \
8162                 return 0; \
8163         return (ret_type)val; \
8164 }
8165
8166 #define __P_OLD_SET_FL1(pfx, name, ret_type) \
8167 EXPORT_SYMBOL ret_type pfx##name(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, int *dir) \
8168 { \
8169         ret_type val; \
8170         if (INTERNAL(name)(pcm, params, &val, dir) < 0) \
8171                 return 0; \
8172         return (ret_type)val; \
8173 }
8174
8175 #define __OLD_SET_FL(name, ret_type) __P_OLD_SET_FL(__old_, name, ret_type)
8176 #define __OLD_SET_FL1(name, ret_type) __P_OLD_SET_FL1(__old_, name, ret_type)
8177
8178 __OLD_SET_FL(snd_pcm_hw_params_set_access_first, snd_pcm_access_t);
8179 __OLD_SET_FL(snd_pcm_hw_params_set_format_first, snd_pcm_format_t);
8180 __OLD_SET_FL(snd_pcm_hw_params_set_subformat_first, snd_pcm_subformat_t);
8181 __OLD_SET_FL(snd_pcm_hw_params_set_channels_first, unsigned int);
8182 __OLD_SET_FL1(snd_pcm_hw_params_set_rate_first, unsigned int);
8183 __OLD_SET_FL1(snd_pcm_hw_params_set_period_time_first, unsigned int);
8184 __OLD_SET_FL1(snd_pcm_hw_params_set_period_size_first, snd_pcm_uframes_t);
8185 __OLD_SET_FL1(snd_pcm_hw_params_set_periods_first, unsigned int);
8186 __OLD_SET_FL1(snd_pcm_hw_params_set_buffer_time_first, unsigned int);
8187 __OLD_SET_FL(snd_pcm_hw_params_set_buffer_size_first, snd_pcm_uframes_t);
8188 __OLD_SET_FL1(snd_pcm_hw_params_set_tick_time_first, unsigned int);
8189
8190 __OLD_SET_FL(snd_pcm_hw_params_set_access_last, snd_pcm_access_t);
8191 __OLD_SET_FL(snd_pcm_hw_params_set_format_last, snd_pcm_format_t);
8192 __OLD_SET_FL(snd_pcm_hw_params_set_subformat_last, snd_pcm_subformat_t);
8193 __OLD_SET_FL(snd_pcm_hw_params_set_channels_last, unsigned int);
8194 __OLD_SET_FL1(snd_pcm_hw_params_set_rate_last, unsigned int);
8195 __OLD_SET_FL1(snd_pcm_hw_params_set_period_time_last, unsigned int);
8196 __OLD_SET_FL1(snd_pcm_hw_params_set_period_size_last, snd_pcm_uframes_t);
8197 __OLD_SET_FL1(snd_pcm_hw_params_set_periods_last, unsigned int);
8198 __OLD_SET_FL1(snd_pcm_hw_params_set_buffer_time_last, unsigned int);
8199 __OLD_SET_FL(snd_pcm_hw_params_set_buffer_size_last, snd_pcm_uframes_t);
8200 __OLD_SET_FL1(snd_pcm_hw_params_set_tick_time_last, unsigned int);
8201
8202 #define __P_OLD_GET_SW(pfx, name, ret_type) \
8203 EXPORT_SYMBOL ret_type pfx##name(snd_pcm_sw_params_t *params) \
8204 { \
8205         ret_type val; \
8206         if (INTERNAL(name)(params, &val) < 0) \
8207                 return 0; \
8208         return (ret_type)val; \
8209 }
8210
8211 #define __OLD_GET_SW(name, ret_type) __P_OLD_GET_SW(__old_, name, ret_type)
8212
8213 __OLD_GET_SW(snd_pcm_sw_params_get_tstamp_mode, snd_pcm_tstamp_t);
8214 __OLD_GET_SW(snd_pcm_sw_params_get_sleep_min, unsigned int);
8215 __OLD_GET_SW(snd_pcm_sw_params_get_avail_min, snd_pcm_uframes_t);
8216 __OLD_GET_SW(snd_pcm_sw_params_get_xfer_align, snd_pcm_uframes_t);
8217 __OLD_GET_SW(snd_pcm_sw_params_get_start_threshold, snd_pcm_uframes_t);
8218 __OLD_GET_SW(snd_pcm_sw_params_get_stop_threshold, snd_pcm_uframes_t);
8219 __OLD_GET_SW(snd_pcm_sw_params_get_silence_threshold, snd_pcm_uframes_t);
8220 __OLD_GET_SW(snd_pcm_sw_params_get_silence_size, snd_pcm_uframes_t);
8221
8222 OBSOLETE1(snd_pcm_hw_params_get_access, ALSA_0.9, ALSA_0.9.0rc4);
8223 OBSOLETE1(snd_pcm_hw_params_set_access_first, ALSA_0.9, ALSA_0.9.0rc4);
8224 OBSOLETE1(snd_pcm_hw_params_set_access_last, ALSA_0.9, ALSA_0.9.0rc4);
8225
8226 OBSOLETE1(snd_pcm_hw_params_get_format, ALSA_0.9, ALSA_0.9.0rc4);
8227 OBSOLETE1(snd_pcm_hw_params_set_format_first, ALSA_0.9, ALSA_0.9.0rc4);
8228 OBSOLETE1(snd_pcm_hw_params_set_format_last, ALSA_0.9, ALSA_0.9.0rc4);
8229
8230 OBSOLETE1(snd_pcm_hw_params_get_subformat, ALSA_0.9, ALSA_0.9.0rc4);
8231 OBSOLETE1(snd_pcm_hw_params_set_subformat_first, ALSA_0.9, ALSA_0.9.0rc4);
8232 OBSOLETE1(snd_pcm_hw_params_set_subformat_last, ALSA_0.9, ALSA_0.9.0rc4);
8233
8234 OBSOLETE1(snd_pcm_hw_params_get_channels, ALSA_0.9, ALSA_0.9.0rc4);
8235 OBSOLETE1(snd_pcm_hw_params_get_channels_min, ALSA_0.9, ALSA_0.9.0rc4);
8236 OBSOLETE1(snd_pcm_hw_params_get_channels_max, ALSA_0.9, ALSA_0.9.0rc4);
8237 OBSOLETE1(snd_pcm_hw_params_set_channels_near, ALSA_0.9, ALSA_0.9.0rc4);
8238 OBSOLETE1(snd_pcm_hw_params_set_channels_first, ALSA_0.9, ALSA_0.9.0rc4);
8239 OBSOLETE1(snd_pcm_hw_params_set_channels_last, ALSA_0.9, ALSA_0.9.0rc4);
8240
8241 OBSOLETE1(snd_pcm_hw_params_get_rate, ALSA_0.9, ALSA_0.9.0rc4);
8242 OBSOLETE1(snd_pcm_hw_params_get_rate_min, ALSA_0.9, ALSA_0.9.0rc4);
8243 OBSOLETE1(snd_pcm_hw_params_get_rate_max, ALSA_0.9, ALSA_0.9.0rc4);
8244 OBSOLETE1(snd_pcm_hw_params_set_rate_near, ALSA_0.9, ALSA_0.9.0rc4);
8245 OBSOLETE1(snd_pcm_hw_params_set_rate_first, ALSA_0.9, ALSA_0.9.0rc4);
8246 OBSOLETE1(snd_pcm_hw_params_set_rate_last, ALSA_0.9, ALSA_0.9.0rc4);
8247
8248 OBSOLETE1(snd_pcm_hw_params_get_period_time, ALSA_0.9, ALSA_0.9.0rc4);
8249 OBSOLETE1(snd_pcm_hw_params_get_period_time_min, ALSA_0.9, ALSA_0.9.0rc4);
8250 OBSOLETE1(snd_pcm_hw_params_get_period_time_max, ALSA_0.9, ALSA_0.9.0rc4);
8251 OBSOLETE1(snd_pcm_hw_params_set_period_time_near, ALSA_0.9, ALSA_0.9.0rc4);
8252 OBSOLETE1(snd_pcm_hw_params_set_period_time_first, ALSA_0.9, ALSA_0.9.0rc4);
8253 OBSOLETE1(snd_pcm_hw_params_set_period_time_last, ALSA_0.9, ALSA_0.9.0rc4);
8254
8255 OBSOLETE1(snd_pcm_hw_params_get_period_size, ALSA_0.9, ALSA_0.9.0rc4);
8256 OBSOLETE1(snd_pcm_hw_params_get_period_size_min, ALSA_0.9, ALSA_0.9.0rc4);
8257 OBSOLETE1(snd_pcm_hw_params_get_period_size_max, ALSA_0.9, ALSA_0.9.0rc4);
8258 OBSOLETE1(snd_pcm_hw_params_set_period_size_near, ALSA_0.9, ALSA_0.9.0rc4);
8259 OBSOLETE1(snd_pcm_hw_params_set_period_size_first, ALSA_0.9, ALSA_0.9.0rc4);
8260 OBSOLETE1(snd_pcm_hw_params_set_period_size_last, ALSA_0.9, ALSA_0.9.0rc4);
8261
8262 OBSOLETE1(snd_pcm_hw_params_get_periods, ALSA_0.9, ALSA_0.9.0rc4);
8263 OBSOLETE1(snd_pcm_hw_params_get_periods_min, ALSA_0.9, ALSA_0.9.0rc4);
8264 OBSOLETE1(snd_pcm_hw_params_get_periods_max, ALSA_0.9, ALSA_0.9.0rc4);
8265 OBSOLETE1(snd_pcm_hw_params_set_periods_near, ALSA_0.9, ALSA_0.9.0rc4);
8266 OBSOLETE1(snd_pcm_hw_params_set_periods_first, ALSA_0.9, ALSA_0.9.0rc4);
8267 OBSOLETE1(snd_pcm_hw_params_set_periods_last, ALSA_0.9, ALSA_0.9.0rc4);
8268
8269 OBSOLETE1(snd_pcm_hw_params_get_buffer_time, ALSA_0.9, ALSA_0.9.0rc4);
8270 OBSOLETE1(snd_pcm_hw_params_get_buffer_time_min, ALSA_0.9, ALSA_0.9.0rc4);
8271 OBSOLETE1(snd_pcm_hw_params_get_buffer_time_max, ALSA_0.9, ALSA_0.9.0rc4);
8272 OBSOLETE1(snd_pcm_hw_params_set_buffer_time_near, ALSA_0.9, ALSA_0.9.0rc4);
8273 OBSOLETE1(snd_pcm_hw_params_set_buffer_time_first, ALSA_0.9, ALSA_0.9.0rc4);
8274 OBSOLETE1(snd_pcm_hw_params_set_buffer_time_last, ALSA_0.9, ALSA_0.9.0rc4);
8275
8276 OBSOLETE1(snd_pcm_hw_params_get_buffer_size, ALSA_0.9, ALSA_0.9.0rc4);
8277 OBSOLETE1(snd_pcm_hw_params_get_buffer_size_min, ALSA_0.9, ALSA_0.9.0rc4);
8278 OBSOLETE1(snd_pcm_hw_params_get_buffer_size_max, ALSA_0.9, ALSA_0.9.0rc4);
8279 OBSOLETE1(snd_pcm_hw_params_set_buffer_size_near, ALSA_0.9, ALSA_0.9.0rc4);
8280 OBSOLETE1(snd_pcm_hw_params_set_buffer_size_first, ALSA_0.9, ALSA_0.9.0rc4);
8281 OBSOLETE1(snd_pcm_hw_params_set_buffer_size_last, ALSA_0.9, ALSA_0.9.0rc4);
8282
8283 OBSOLETE1(snd_pcm_hw_params_get_tick_time, ALSA_0.9, ALSA_0.9.0rc4);
8284 OBSOLETE1(snd_pcm_hw_params_get_tick_time_min, ALSA_0.9, ALSA_0.9.0rc4);
8285 OBSOLETE1(snd_pcm_hw_params_get_tick_time_max, ALSA_0.9, ALSA_0.9.0rc4);
8286 OBSOLETE1(snd_pcm_hw_params_set_tick_time_near, ALSA_0.9, ALSA_0.9.0rc4);
8287 OBSOLETE1(snd_pcm_hw_params_set_tick_time_first, ALSA_0.9, ALSA_0.9.0rc4);
8288 OBSOLETE1(snd_pcm_hw_params_set_tick_time_last, ALSA_0.9, ALSA_0.9.0rc4);
8289
8290 OBSOLETE1(snd_pcm_sw_params_get_tstamp_mode, ALSA_0.9, ALSA_0.9.0rc4);
8291 OBSOLETE1(snd_pcm_sw_params_get_sleep_min, ALSA_0.9, ALSA_0.9.0rc4);
8292 OBSOLETE1(snd_pcm_sw_params_get_avail_min, ALSA_0.9, ALSA_0.9.0rc4);
8293 OBSOLETE1(snd_pcm_sw_params_get_xfer_align, ALSA_0.9, ALSA_0.9.0rc4);
8294 OBSOLETE1(snd_pcm_sw_params_get_start_threshold, ALSA_0.9, ALSA_0.9.0rc4);
8295 OBSOLETE1(snd_pcm_sw_params_get_stop_threshold, ALSA_0.9, ALSA_0.9.0rc4);
8296 OBSOLETE1(snd_pcm_sw_params_get_silence_threshold, ALSA_0.9, ALSA_0.9.0rc4);
8297 OBSOLETE1(snd_pcm_sw_params_get_silence_size, ALSA_0.9, ALSA_0.9.0rc4);
8298
8299 #endif /* DOC_HIDDEN/COVERITY */
8300
8301 static int chmap_equal(const snd_pcm_chmap_t *a, const snd_pcm_chmap_t *b)
8302 {
8303         if (a->channels != b->channels)
8304                 return 0;
8305         return !memcmp(a->pos, b->pos, a->channels * sizeof(a->pos[0]));
8306 }
8307
8308 /**
8309  * \!brief Query the available channel maps
8310  * \param pcm PCM handle to query
8311  * \return the NULL-terminated array of integer pointers, each of
8312  * which contains the channel map. A channel map is represented by an
8313  * integer array, beginning with the channel map type, followed by the
8314  * number of channels, and the position of each channel. Return NULL
8315  * in case of an error.
8316  *
8317  * Note: the caller is requested to release the returned value via
8318  * snd_pcm_free_chmaps().
8319  */
8320 snd_pcm_chmap_query_t **snd_pcm_query_chmaps(snd_pcm_t *pcm)
8321 {
8322         if (!pcm->ops->query_chmaps)
8323                 return NULL;
8324         return pcm->ops->query_chmaps(pcm);
8325 }
8326
8327 /**
8328  * \!brief Release the channel map array allocated via #snd_pcm_query_chmaps
8329  * \param maps the array pointer to release
8330  */
8331 void snd_pcm_free_chmaps(snd_pcm_chmap_query_t **maps)
8332 {
8333         snd_pcm_chmap_query_t **p;
8334         if (!maps)
8335                 return;
8336         for (p = maps; *p; p++)
8337                 free(*p);
8338         free(maps);
8339 }
8340
8341 /**
8342  * \!brief Get the current channel map
8343  * \param pcm PCM instance
8344  * \return the current channel map, or NULL if error
8345  *
8346  * Note: the caller is requested to release the returned value via free()
8347  */
8348 snd_pcm_chmap_t *snd_pcm_get_chmap(snd_pcm_t *pcm)
8349 {
8350         if (!pcm->ops->get_chmap)
8351                 return NULL;
8352         return pcm->ops->get_chmap(pcm);
8353 }
8354
8355 /**
8356  * \!brief Configure the current channel map
8357  * \param pcm PCM instance
8358  * \param map the channel map to write
8359  * \return zero if succeeded, or a negative error code
8360  */
8361 int snd_pcm_set_chmap(snd_pcm_t *pcm, const snd_pcm_chmap_t *map)
8362 {
8363         const snd_pcm_chmap_t *oldmap;
8364         int nochange;
8365
8366         oldmap = snd_pcm_get_chmap(pcm);
8367         nochange = (oldmap && chmap_equal(oldmap, map));
8368         free((void *)oldmap);
8369         if (nochange)
8370                 return 0;
8371
8372         if (!pcm->ops->set_chmap)
8373                 return -ENXIO;
8374         return pcm->ops->set_chmap(pcm, map);
8375 }
8376
8377 /*
8378  */
8379 #ifndef DOC_HIDDEN
8380 #define _NAME(n) [SND_CHMAP_TYPE_##n] = #n
8381 static const char *chmap_type_names[SND_CHMAP_TYPE_LAST + 1] = {
8382         _NAME(NONE), _NAME(FIXED), _NAME(VAR), _NAME(PAIRED),
8383 };
8384 #undef _NAME
8385 #endif
8386
8387 /**
8388  * \!brief Get a name string for a channel map type as query results
8389  * \param val Channel position
8390  * \return The string corresponding to the given type, or NULL
8391  */
8392 const char *snd_pcm_chmap_type_name(enum snd_pcm_chmap_type val)
8393 {
8394         if (val <= SND_CHMAP_TYPE_LAST)
8395                 return chmap_type_names[val];
8396         else
8397                 return NULL;
8398 }
8399
8400 #ifndef DOC_HIDDEN
8401 #define _NAME(n) [SND_CHMAP_##n] = #n
8402 static const char *chmap_names[SND_CHMAP_LAST + 1] = {
8403         _NAME(UNKNOWN), _NAME(NA), _NAME(MONO),
8404         _NAME(FL), _NAME(FR),
8405         _NAME(RL), _NAME(RR),
8406         _NAME(FC), _NAME(LFE),
8407         _NAME(SL), _NAME(SR),
8408         _NAME(RC), _NAME(FLC), _NAME(FRC), _NAME(RLC), _NAME(RRC),
8409         _NAME(FLW), _NAME(FRW),
8410         _NAME(FLH), _NAME(FCH), _NAME(FRH), _NAME(TC),
8411         _NAME(TFL), _NAME(TFR), _NAME(TFC),
8412         _NAME(TRL), _NAME(TRR), _NAME(TRC),
8413         _NAME(TFLC), _NAME(TFRC), _NAME(TSL), _NAME(TSR),
8414         _NAME(LLFE), _NAME(RLFE),
8415         _NAME(BC), _NAME(BLC), _NAME(BRC),
8416 };
8417 #undef _NAME
8418 #endif
8419
8420 /**
8421  * \!brief Get a name string for a standard channel map position
8422  * \param val Channel position
8423  * \return The string corresponding to the given position, or NULL
8424  */
8425 const char *snd_pcm_chmap_name(enum snd_pcm_chmap_position val)
8426 {
8427         if (val <= SND_CHMAP_LAST)
8428                 return chmap_names[val];
8429         else
8430                 return NULL;
8431 }
8432
8433 static const char *chmap_long_names[SND_CHMAP_LAST + 1] = {
8434         [SND_CHMAP_UNKNOWN] = "Unknown",
8435         [SND_CHMAP_NA] = "Unused",
8436         [SND_CHMAP_MONO] = "Mono",
8437         [SND_CHMAP_FL] = "Front Left",
8438         [SND_CHMAP_FR] = "Front Right",
8439         [SND_CHMAP_RL] = "Rear Left",
8440         [SND_CHMAP_RR] = "Rear Right",
8441         [SND_CHMAP_FC] = "Front Center",
8442         [SND_CHMAP_LFE] = "LFE",
8443         [SND_CHMAP_SL] = "Side Left",
8444         [SND_CHMAP_SR] = "Side Right",
8445         [SND_CHMAP_RC] = "Rear Center",
8446         [SND_CHMAP_FLC] = "Front Left Center",
8447         [SND_CHMAP_FRC] = "Front Right Center",
8448         [SND_CHMAP_RLC] = "Rear Left Center",
8449         [SND_CHMAP_RRC] = "Rear Right Center",
8450         [SND_CHMAP_FLW] = "Front Left Wide",
8451         [SND_CHMAP_FRW] = "Front Right Wide",
8452         [SND_CHMAP_FLH] = "Front Left High",
8453         [SND_CHMAP_FCH] = "Front Center High",
8454         [SND_CHMAP_FRH] = "Front Right High",
8455         [SND_CHMAP_TC] = "Top Center",
8456         [SND_CHMAP_TFL] = "Top Front Left",
8457         [SND_CHMAP_TFR] = "Top Front Right",
8458         [SND_CHMAP_TFC] = "Top Front Center",
8459         [SND_CHMAP_TRL] = "Top Rear Left",
8460         [SND_CHMAP_TRR] = "Top Rear Right",
8461         [SND_CHMAP_TRC] = "Top Rear Center",
8462         [SND_CHMAP_TFLC] = "Top Front Left Center",
8463         [SND_CHMAP_TFRC] = "Top Front Right Center",
8464         [SND_CHMAP_TSL] = "Top Side Left",
8465         [SND_CHMAP_TSR] = "Top Side Right",
8466         [SND_CHMAP_LLFE] = "Left LFE",
8467         [SND_CHMAP_RLFE] = "Right LFE",
8468         [SND_CHMAP_BC] = "Bottom Center",
8469         [SND_CHMAP_BLC] = "Bottom Left Center",
8470         [SND_CHMAP_BRC] = "Bottom Right Center",
8471 };
8472
8473 /**
8474  * \!brief Get a longer name string for a standard channel map position
8475  * \param val Channel position
8476  * \return The string corresponding to the given position, or NULL
8477  */
8478 const char *snd_pcm_chmap_long_name(enum snd_pcm_chmap_position val)
8479 {
8480         if (val <= SND_CHMAP_LAST)
8481                 return chmap_long_names[val];
8482         else
8483                 return NULL;
8484 }
8485
8486 /**
8487  * \!brief Print the channels in chmap on the buffer
8488  * \param map The channel map to print
8489  * \param maxlen The maximal length to write (including NUL letter)
8490  * \param buf The buffer to write
8491  * \return The actual string length or a negative error code
8492  */
8493 int snd_pcm_chmap_print(const snd_pcm_chmap_t *map, size_t maxlen, char *buf)
8494 {
8495         unsigned int i, len = 0;
8496
8497         for (i = 0; i < map->channels; i++) {
8498                 unsigned int p = map->pos[i] & SND_CHMAP_POSITION_MASK;
8499                 if (i > 0) {
8500                         len += snprintf(buf + len, maxlen - len, " ");
8501                         if (len >= maxlen)
8502                                 return -ENOMEM;
8503                 }
8504                 if (map->pos[i] & SND_CHMAP_DRIVER_SPEC)
8505                         len += snprintf(buf + len, maxlen - len, "%d", p);
8506                 else {
8507                         const char *name = chmap_names[p];
8508                         if (name)
8509                                 len += snprintf(buf + len, maxlen - len,
8510                                                 "%s", name);
8511                         else
8512                                 len += snprintf(buf + len, maxlen - len,
8513                                                 "Ch%d", p);
8514                 }
8515                 if (len >= maxlen)
8516                         return -ENOMEM;
8517                 if (map->pos[i] & SND_CHMAP_PHASE_INVERSE) {
8518                         len += snprintf(buf + len, maxlen - len, "[INV]");
8519                         if (len >= maxlen)
8520                                 return -ENOMEM;
8521                 }
8522         }
8523         return len;
8524 }
8525
8526 static int str_to_chmap(const char *str, int len)
8527 {
8528         int val;
8529         unsigned long v;
8530         char *p;
8531
8532         if (isdigit(*str)) {
8533                 v = strtoul(str, &p, 0);
8534                 if (v == ULONG_MAX)
8535                         return -1;
8536                 val = v;
8537                 val |= SND_CHMAP_DRIVER_SPEC;
8538                 str = p;
8539         } else if (!strncasecmp(str, "ch", 2)) {
8540                 v = strtoul(str + 2, &p, 0);
8541                 if (v == ULONG_MAX)
8542                         return -1;
8543                 val = v;
8544                 str = p;
8545         } else {
8546                 for (val = 0; val <= SND_CHMAP_LAST; val++) {
8547                         int slen;
8548                         assert(chmap_names[val]);
8549                         slen = strlen(chmap_names[val]);
8550                         if (slen > len)
8551                                 continue;
8552                         if (!strncasecmp(str, chmap_names[val], slen) &&
8553                             !isalpha(str[slen])) {
8554                                 str += slen;
8555                                 break;
8556                         }
8557                 }
8558                 if (val > SND_CHMAP_LAST)
8559                         return -1;
8560         }
8561         if (str && !strncasecmp(str, "[INV]", 5))
8562                 val |= SND_CHMAP_PHASE_INVERSE;
8563         return val;
8564 }
8565
8566 /**
8567  * \!brief Convert from string to channel position
8568  * \param str The string to parse
8569  * \return The channel position value or -1 as an error
8570  */
8571 unsigned int snd_pcm_chmap_from_string(const char *str)
8572 {
8573         return str_to_chmap(str, strlen(str));
8574 }
8575
8576 /**
8577  * \!brief Convert from string to channel map
8578  * \param str The string to parse
8579  * \return The channel map
8580  *
8581  * Note: the caller is requested to release the returned value via free()
8582  */
8583 snd_pcm_chmap_t *snd_pcm_chmap_parse_string(const char *str)
8584 {
8585         int i, ch = 0;
8586         int tmp_map[64];
8587         snd_pcm_chmap_t *map;
8588
8589         for (;;) {
8590                 const char *p;
8591                 int len, val;
8592
8593                 if (ch >= (int)(sizeof(tmp_map) / sizeof(tmp_map[0])))
8594                         return NULL;
8595                 for (p = str; *p && isalnum(*p); p++)
8596                         ;
8597                 len = p - str;
8598                 if (!len)
8599                         return NULL;
8600                 val = str_to_chmap(str, len);
8601                 if (val < 0)
8602                         return NULL;
8603                 str += len;
8604                 if (*str == '[') {
8605                         if (!strncmp(str, "[INV]", 5)) {
8606                                 val |= SND_CHMAP_PHASE_INVERSE;
8607                                 str += 5;
8608                         }
8609                 }
8610                 tmp_map[ch] = val;
8611                 ch++;
8612                 for (; *str && !isalnum(*str); str++)
8613                         ;
8614                 if (!*str)
8615                         break;
8616         }
8617         map = malloc(sizeof(*map) + ch * sizeof(int));
8618         if (!map)
8619                 return NULL;
8620         map->channels = ch;
8621         for (i = 0; i < ch; i++)
8622                 map->pos[i] = tmp_map[i];
8623         return map;
8624 }
8625
8626 /* copy a single channel map with the fixed type to chmap_query pointer */
8627 static int _copy_to_fixed_query_map(snd_pcm_chmap_query_t **dst,
8628                                     const snd_pcm_chmap_t *src)
8629 {
8630         *dst = malloc((src->channels + 2) * sizeof(int));
8631         if (!*dst)
8632                 return -ENOMEM;
8633         (*dst)->type = SND_CHMAP_TYPE_FIXED;
8634         memcpy(&(*dst)->map, src, (src->channels + 1) * sizeof(int));
8635         return 0;
8636 }
8637
8638 #ifndef DOC_HIDDEN
8639 /* make a chmap_query array from a single channel map */
8640 snd_pcm_chmap_query_t **
8641 _snd_pcm_make_single_query_chmaps(const snd_pcm_chmap_t *src)
8642 {
8643         snd_pcm_chmap_query_t **maps;
8644
8645         maps = calloc(2, sizeof(*maps));
8646         if (!maps)
8647                 return NULL;
8648         if (_copy_to_fixed_query_map(maps, src)) {
8649                 free(maps);
8650                 return NULL;
8651         }
8652         return maps;
8653 }
8654
8655 /* make a copy of chmap */
8656 snd_pcm_chmap_t *_snd_pcm_copy_chmap(const snd_pcm_chmap_t *src)
8657 {
8658         snd_pcm_chmap_t *map;
8659
8660         map = malloc((src->channels + 1) * sizeof(int));
8661         if (!map)
8662                 return NULL;
8663         memcpy(map, src, (src->channels + 1) * sizeof(int));
8664         return map;
8665 }
8666
8667 /* make a copy of channel maps */
8668 snd_pcm_chmap_query_t **
8669 _snd_pcm_copy_chmap_query(snd_pcm_chmap_query_t * const *src)
8670 {
8671         snd_pcm_chmap_query_t * const *p;
8672         snd_pcm_chmap_query_t **maps;
8673         int i, nums;
8674
8675         for (nums = 0, p = src; *p; p++)
8676                 nums++;
8677
8678         maps = calloc(nums + 1, sizeof(*maps));
8679         if (!maps)
8680                 return NULL;
8681         for (i = 0; i < nums; i++) {
8682                 maps[i] = malloc((src[i]->map.channels + 2) * sizeof(int));
8683                 if (!maps[i]) {
8684                         snd_pcm_free_chmaps(maps);
8685                         return NULL;
8686                 }
8687                 memcpy(maps[i], src[i], (src[i]->map.channels + 2) * sizeof(int));
8688         }
8689         return maps;
8690 }
8691
8692 /* select the channel map with the current PCM channels and make a copy */
8693 snd_pcm_chmap_t *
8694 _snd_pcm_choose_fixed_chmap(snd_pcm_t *pcm, snd_pcm_chmap_query_t * const *maps)
8695 {
8696         snd_pcm_chmap_query_t * const *p;
8697
8698         for (p = maps; *p; p++) {
8699                 if ((*p)->map.channels == pcm->channels)
8700                         return _snd_pcm_copy_chmap(&(*p)->map);
8701         }
8702         return NULL;
8703 }
8704
8705 /* make chmap_query array from the config tree;
8706  * conf must be a compound (array)
8707  */
8708 snd_pcm_chmap_query_t **
8709 _snd_pcm_parse_config_chmaps(snd_config_t *conf)
8710 {
8711         snd_pcm_chmap_t *chmap;
8712         snd_pcm_chmap_query_t **maps;
8713         snd_config_iterator_t i, next;
8714         const char *str;
8715         int nums, err;
8716
8717         if (snd_config_get_type(conf) != SND_CONFIG_TYPE_COMPOUND)
8718                 return NULL;
8719
8720         nums = 0;
8721         snd_config_for_each(i, next, conf) {
8722                 nums++;
8723         }
8724
8725         maps = calloc(nums + 1, sizeof(*maps));
8726         if (!maps)
8727                 return NULL;
8728
8729         nums = 0;
8730         snd_config_for_each(i, next, conf) {
8731                 snd_config_t *n = snd_config_iterator_entry(i);
8732                 err = snd_config_get_string(n, &str);
8733                 if (err < 0)
8734                         goto error;
8735                 chmap = snd_pcm_chmap_parse_string(str);
8736                 if (!chmap)
8737                         goto error;
8738                 if (_copy_to_fixed_query_map(maps + nums, chmap)) {
8739                         free(chmap);
8740                         goto error;
8741                 }
8742                 free(chmap);
8743                 nums++;
8744         }
8745         return maps;
8746
8747  error:
8748         snd_pcm_free_chmaps(maps);
8749         return NULL;
8750 }
8751 #endif /* DOC_HIDDEN */
8752
8753 /*
8754  * basic helpers
8755  */
8756
8757
8758 /**
8759  * \brief Recover the stream state from an error or suspend
8760  * \param pcm PCM handle
8761  * \param err error number
8762  * \param silent do not print error reason
8763  * \return 0 when error code was handled successfuly, otherwise a negative error code
8764  *
8765  * This a high-level helper function building on other functions.
8766  *
8767  * This functions handles -EINTR (interrupted system call),
8768  * -EPIPE (overrun or underrun) and -ESTRPIPE (stream is suspended)
8769  * error codes trying to prepare given stream for next I/O.
8770  *
8771  * Note that this function returns the original error code when it is not
8772  * handled inside this function (for example -EAGAIN is returned back).
8773  */
8774 int snd_pcm_recover(snd_pcm_t *pcm, int err, int silent)
8775 {
8776         if (err > 0)
8777                 err = -err;
8778         if (err == -EINTR)      /* nothing to do, continue */
8779                 return 0;
8780         if (err == -EPIPE) {
8781                 const char *s;
8782                 if (snd_pcm_stream(pcm) == SND_PCM_STREAM_PLAYBACK)
8783                         s = "underrun";
8784                 else
8785                         s = "overrun";
8786                 if (!silent)
8787                         snd_error(PCM, "%s occurred", s);
8788                 err = snd_pcm_prepare(pcm);
8789                 if (err < 0) {
8790                         snd_error(PCM, "cannot recovery from %s, prepare failed: %s", s, snd_strerror(err));
8791                         return err;
8792                 }
8793                 return 0;
8794         }
8795         if (err == -ESTRPIPE) {
8796                 while ((err = snd_pcm_resume(pcm)) == -EAGAIN)
8797                         /* wait until suspend flag is released */
8798                         (void)poll(NULL, 0, 1000);
8799                 if (err < 0) {
8800                         err = snd_pcm_prepare(pcm);
8801                         if (err < 0) {
8802                                 snd_error(PCM, "cannot recovery from suspend, prepare failed: %s", snd_strerror(err));
8803                                 return err;
8804                         }
8805                 }
8806                 return 0;
8807         }
8808         return err;
8809 }
8810
8811 /**
8812  * \brief Set the hardware and software parameters in a simple way
8813  * \param pcm PCM handle
8814  * \param format required PCM format
8815  * \param access required PCM access
8816  * \param channels required PCM channels
8817  * \param rate required sample rate in Hz
8818  * \param soft_resample 0 = disallow alsa-lib resample stream, 1 = allow resampling
8819  * \param latency required overall latency in us
8820  * \return 0 on success otherwise a negative error code
8821  */
8822 int snd_pcm_set_params(snd_pcm_t *pcm,
8823                        snd_pcm_format_t format,
8824                        snd_pcm_access_t access,
8825                        unsigned int channels,
8826                        unsigned int rate,
8827                        int soft_resample,
8828                        unsigned int latency)
8829 {
8830         snd_pcm_hw_params_t params_saved, params = {0};
8831         snd_pcm_sw_params_t swparams = {0};
8832         const char *s = snd_pcm_stream_name(snd_pcm_stream(pcm));
8833         snd_pcm_uframes_t buffer_size, period_size;
8834         unsigned int rrate, period_time;
8835         int err;
8836
8837         assert(pcm);
8838         /* choose all parameters */
8839         err = snd_pcm_hw_params_any(pcm, &params);
8840         if (err < 0) {
8841                 snd_error(PCM, "Broken configuration for %s: no configurations available",
8842                                s);
8843
8844                 return err;
8845         }
8846         /* set software resampling */
8847         err = snd_pcm_hw_params_set_rate_resample(pcm, &params, soft_resample);
8848         if (err < 0) {
8849                 snd_error(PCM, "Resampling setup failed for %s: %s",
8850                                s, snd_strerror(err));
8851
8852                 return err;
8853         }
8854         /* set the selected read/write format */
8855         err = snd_pcm_hw_params_set_access(pcm, &params, access);
8856         if (err < 0) {
8857                 snd_error(PCM, "Access type not available for %s: %s",
8858                                s, snd_strerror(err));
8859
8860                 return err;
8861         }
8862         /* set the sample format */
8863         err = snd_pcm_hw_params_set_format(pcm, &params, format);
8864         if (err < 0) {
8865                 snd_error(PCM, "Sample format not available for %s: %s",
8866                                s, snd_strerror(err));
8867
8868                 return err;
8869         }
8870         /* set the count of channels */
8871         err = snd_pcm_hw_params_set_channels(pcm, &params, channels);
8872         if (err < 0) {
8873                 snd_error(PCM, "Channels count (%i) not available for %s: %s",
8874                                channels, s, snd_strerror(err));
8875
8876                 return err;
8877         }
8878         /* set the stream rate */
8879         rrate = rate;
8880         err = INTERNAL(snd_pcm_hw_params_set_rate_near)(pcm, &params, &rrate,
8881                                                         0);
8882         if (err < 0) {
8883                 snd_error(PCM, "Rate %iHz not available for playback: %s",
8884                                rate, snd_strerror(err));
8885
8886                 return err;
8887         }
8888         if (rrate != rate) {
8889                 snd_error(PCM, "Rate doesn't match (requested %iHz, get %iHz)",
8890                                rate, rrate);
8891
8892                 return -EINVAL;
8893         }
8894         /* set the buffer time */
8895         params_saved = params;
8896         err = INTERNAL(snd_pcm_hw_params_set_buffer_time_near)(pcm, &params,
8897                                                         &latency, NULL);
8898         if (err < 0) {
8899                 /* error path -> set period size as first */
8900                 params = params_saved;
8901                 /* set the period time */
8902                 period_time = latency / 4;
8903                 err = INTERNAL(snd_pcm_hw_params_set_period_time_near)(pcm,
8904                                                 &params, &period_time, NULL);
8905                 if (err < 0) {
8906                         snd_error(PCM, "Unable to set period time %i for %s: %s",
8907                                        period_time, s, snd_strerror(err));
8908
8909                         return err;
8910                 }
8911                 err = INTERNAL(snd_pcm_hw_params_get_period_size)(&params,
8912                                                         &period_size, NULL);
8913                 if (err < 0) {
8914                         snd_error(PCM, "Unable to get period size for %s: %s",
8915                                                                 s, snd_strerror(err));
8916
8917                         return err;
8918                 }
8919                 buffer_size = period_size * 4;
8920                 err = INTERNAL(snd_pcm_hw_params_set_buffer_size_near)(pcm,
8921                                                         &params, &buffer_size);
8922                 if (err < 0) {
8923                         snd_error(PCM, "Unable to set buffer size %lu %s: %s",
8924                                                 buffer_size, s, snd_strerror(err));
8925
8926                         return err;
8927                 }
8928                 err = INTERNAL(snd_pcm_hw_params_get_buffer_size)(&params,
8929                                                                 &buffer_size);
8930                 if (err < 0) {
8931                         snd_error(PCM, "Unable to get buffer size for %s: %s",
8932                                        s, snd_strerror(err));
8933
8934                         return err;
8935                 }
8936         } else {
8937                 /* standard configuration buffer_time -> periods */
8938                 err = INTERNAL(snd_pcm_hw_params_get_buffer_size)(&params,
8939                                                                 &buffer_size);
8940                 if (err < 0) {
8941                         snd_error(PCM, "Unable to get buffer size for %s: %s",
8942                                                                 s, snd_strerror(err));
8943
8944                         return err;
8945                 }
8946                 err = INTERNAL(snd_pcm_hw_params_get_buffer_time)(&params,
8947                                                         &latency, NULL);
8948                 if (err < 0) {
8949                         snd_error(PCM, "Unable to get buffer time (latency) for %s: %s",
8950                                        s, snd_strerror(err));
8951
8952                         return err;
8953                 }
8954                 /* set the period time */
8955                 period_time = latency / 4;
8956                 err = INTERNAL(snd_pcm_hw_params_set_period_time_near)(pcm,
8957                                                 &params, &period_time, NULL);
8958                 if (err < 0) {
8959                         snd_error(PCM, "Unable to set period time %i for %s: %s",
8960                                        period_time, s, snd_strerror(err));
8961
8962                         return err;
8963                 }
8964                 err = INTERNAL(snd_pcm_hw_params_get_period_size)(&params,
8965                                                         &period_size, NULL);
8966                 if (err < 0) {
8967                         snd_error(PCM, "Unable to get period size for %s: %s",
8968                                        s, snd_strerror(err));
8969
8970                         return err;
8971                 }
8972         }
8973         /* write the parameters to device */
8974         err = snd_pcm_hw_params(pcm, &params);
8975         if (err < 0) {
8976                 snd_error(PCM, "Unable to set hw params for %s: %s",
8977                                s, snd_strerror(err));
8978
8979                 return err;
8980         }
8981
8982         /* get the current swparams */
8983         err = snd_pcm_sw_params_current(pcm, &swparams);
8984         if (err < 0) {
8985                 snd_error(PCM, "Unable to determine current swparams for %s: %s",
8986                                s, snd_strerror(err));
8987
8988                 return err;
8989         }
8990         /*
8991          * start the transfer when the buffer is almost full:
8992          * (buffer_size / avail_min) * avail_min
8993          */
8994         err = snd_pcm_sw_params_set_start_threshold(pcm, &swparams,
8995                                 (buffer_size / period_size) * period_size);
8996         if (err < 0) {
8997                 snd_error(PCM, "Unable to set start threshold mode for %s: %s",
8998                                s, snd_strerror(err));
8999
9000                 return err;
9001         }
9002         /*
9003          * allow the transfer when at least period_size samples can be
9004          * processed
9005          */
9006         err = snd_pcm_sw_params_set_avail_min(pcm, &swparams, period_size);
9007         if (err < 0) {
9008                 snd_error(PCM, "Unable to set avail min for %s: %s",
9009                                s, snd_strerror(err));
9010
9011                 return err;
9012         }
9013         /* write the parameters to the playback device */
9014         err = snd_pcm_sw_params(pcm, &swparams);
9015         if (err < 0) {
9016                 snd_error(PCM, "Unable to set sw params for %s: %s",
9017                                s, snd_strerror(err));
9018
9019                 return err;
9020         }
9021         return 0;
9022 }
9023
9024 /**
9025  * \brief Get the transfer size parameters in a simple way
9026  * \param pcm PCM handle
9027  * \param buffer_size PCM ring buffer size in frames
9028  * \param period_size PCM period size in frames
9029  * \return 0 on success otherwise a negative error code
9030  */
9031 int snd_pcm_get_params(snd_pcm_t *pcm,
9032                        snd_pcm_uframes_t *buffer_size,
9033                        snd_pcm_uframes_t *period_size)
9034 {
9035         snd_pcm_hw_params_t params = {0};
9036         int err;
9037
9038         assert(pcm);
9039         err = snd_pcm_hw_params_current(pcm, &params);
9040         if (err < 0)
9041                 return err;
9042         err = INTERNAL(snd_pcm_hw_params_get_buffer_size)(&params, buffer_size);
9043         if (err < 0)
9044                 return err;
9045         return INTERNAL(snd_pcm_hw_params_get_period_size)(&params, period_size,
9046                                                            NULL);
9047 }