]> git.alsa-project.org Git - alsa-lib.git/blob - src/pcm/pcm.c
c59ea3b686b22bc81b5fa6d9266e0c631bf1cb1d
[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                 SNDMSG("PCM not set up");
1007                 return -EIO;
1008         }
1009         if (! params->avail_min) {
1010                 SNDMSG("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                 SNDMSG("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                 SNDMSG("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                 SNDMSG("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                 SNDMSG("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                 SNDMSG("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                 SNDMSG("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                 SNDMSG("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                 SNDMSG("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                 SNDMSG("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                 SNDMSG("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                 SNDMSG("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                 SNDMSG("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                 SNDMSG("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                 SNDMSG("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                 SNDMSG("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                 SNDMSG("PCM not set up");
1577                 return -EIO;
1578         }
1579         if (pcm->access != SND_PCM_ACCESS_RW_INTERLEAVED) {
1580                 SNDMSG("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                 SNDMSG("PCM not set up");
1616                 return -EIO;
1617         }
1618         if (pcm->access != SND_PCM_ACCESS_RW_NONINTERLEAVED) {
1619                 SNDMSG("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                 SNDMSG("PCM not set up");
1655                 return -EIO;
1656         }
1657         if (pcm->access != SND_PCM_ACCESS_RW_INTERLEAVED) {
1658                 SNDMSG("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                 SNDMSG("PCM not set up");
1694                 return -EIO;
1695         }
1696         if (pcm->access != SND_PCM_ACCESS_RW_NONINTERLEAVED) {
1697                 SNDMSG("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                 SNDMSG("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                 SNDMSG("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                 SNDMSG("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                 SNDMSG("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                 SNDMSG("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                 SNDMSG("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                 SNDMSG("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                 SNDMSG("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                 id = NULL;
2582                 snd_config_get_id(pcm_conf, &id);
2583                 val = NULL;
2584                 snd_config_get_ascii(pcm_conf, &val);
2585                 SNDERR("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                 SNDERR("type is not defined");
2592                 return err;
2593         }
2594         err = snd_config_get_id(conf, &id);
2595         if (err < 0) {
2596                 SNDERR("unable to get id");
2597                 return err;
2598         }
2599         err = snd_config_get_string(conf, &str);
2600         if (err < 0) {
2601                 SNDERR("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                         SNDERR("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                                         SNDERR("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                                         SNDERR("Invalid type for %s", id);
2630                                         goto _err;
2631                                 }
2632                                 continue;
2633                         }
2634                         SNDERR("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                 SNDERR("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                 SNDERR("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                 SNDMSG("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                 SNDMSG("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                 SNDMSG("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                 SNDMSG("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 = (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                 SNDMSG("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 (src_area->step == (unsigned int) width &&
3339             dst_area->step == (unsigned int) width) {
3340                 size_t bytes = samples * width / 8;
3341                 samples -= bytes * 8 / width;
3342                 assert(src < dst || src >= dst + bytes);
3343                 assert(dst < src || dst >= src + bytes);
3344                 memcpy(dst, src, bytes);
3345                 if (samples == 0)
3346                         return 0;
3347         }
3348         src_step = src_area->step / 8;
3349         dst_step = dst_area->step / 8;
3350         switch (width) {
3351         case 4: {
3352                 int srcbit = src_area->first % 8;
3353                 int srcbit_step = src_area->step % 8;
3354                 int dstbit = dst_area->first % 8;
3355                 int dstbit_step = dst_area->step % 8;
3356                 while (samples-- > 0) {
3357                         unsigned char srcval;
3358                         if (srcbit)
3359                                 srcval = *src & 0x0f;
3360                         else
3361                                 srcval = *src & 0xf0;
3362                         if (dstbit)
3363                                 *dst &= 0xf0;
3364                         else
3365                                 *dst &= 0x0f;
3366                         *dst |= srcval;
3367                         src += src_step;
3368                         srcbit += srcbit_step;
3369                         if (srcbit == 8) {
3370                                 src++;
3371                                 srcbit = 0;
3372                         }
3373                         dst += dst_step;
3374                         dstbit += dstbit_step;
3375                         if (dstbit == 8) {
3376                                 dst++;
3377                                 dstbit = 0;
3378                         }
3379                 }
3380                 break;
3381         }
3382         case 8: {
3383                 while (samples-- > 0) {
3384                         *dst = *src;
3385                         src += src_step;
3386                         dst += dst_step;
3387                 }
3388                 break;
3389         }
3390         case 16: {
3391                 while (samples-- > 0) {
3392                         *(uint16_t*)dst = *(const uint16_t*)src;
3393                         src += src_step;
3394                         dst += dst_step;
3395                 }
3396                 break;
3397         }
3398         case 24:
3399                 while (samples-- > 0) {
3400                         *(dst + 0) = *(src + 0);
3401                         *(dst + 1) = *(src + 1);
3402                         *(dst + 2) = *(src + 2);
3403                         src += src_step;
3404                         dst += dst_step;
3405                 }
3406                 break;
3407         case 32: {
3408                 while (samples-- > 0) {
3409                         *(uint32_t*)dst = *(const uint32_t*)src;
3410                         src += src_step;
3411                         dst += dst_step;
3412                 }
3413                 break;
3414         }
3415         case 64: {
3416                 while (samples-- > 0) {
3417                         *(uint64_t*)dst = *(const uint64_t*)src;
3418                         src += src_step;
3419                         dst += dst_step;
3420                 }
3421                 break;
3422         }
3423         default:
3424                 SNDMSG("invalid format width %d", width);
3425                 return -EINVAL;
3426         }
3427         return 0;
3428 }
3429
3430 /**
3431  * \brief Copy one or more areas
3432  * \param dst_areas destination areas specification (one for each channel)
3433  * \param dst_offset offset in frames inside destination area
3434  * \param src_areas source areas specification (one for each channel)
3435  * \param src_offset offset in frames inside source area
3436  * \param channels channels count
3437  * \param frames frames to copy
3438  * \param format PCM sample format
3439  * \return 0 on success otherwise a negative error code
3440  */
3441 int snd_pcm_areas_copy(const snd_pcm_channel_area_t *dst_areas, snd_pcm_uframes_t dst_offset,
3442                        const snd_pcm_channel_area_t *src_areas, snd_pcm_uframes_t src_offset,
3443                        unsigned int channels, snd_pcm_uframes_t frames, snd_pcm_format_t format)
3444 {
3445         int width = snd_pcm_format_physical_width(format);
3446         assert(dst_areas);
3447         assert(src_areas);
3448         if (! channels) {
3449                 SNDMSG("invalid channels %d", channels);
3450                 return -EINVAL;
3451         }
3452         if (! frames) {
3453                 SNDMSG("invalid frames %ld", frames);
3454                 return -EINVAL;
3455         }
3456         while (channels > 0) {
3457                 unsigned int step = src_areas->step;
3458                 void *src_addr = src_areas->addr;
3459                 const snd_pcm_channel_area_t *src_start = src_areas;
3460                 void *dst_addr = dst_areas->addr;
3461                 const snd_pcm_channel_area_t *dst_start = dst_areas;
3462                 int channels1 = channels;
3463                 unsigned int chns = 0;
3464                 while (dst_areas->step == step) {
3465                         channels1--;
3466                         chns++;
3467                         src_areas++;
3468                         dst_areas++;
3469                         if (channels1 == 0 ||
3470                             src_areas->step != step ||
3471                             src_areas->addr != src_addr ||
3472                             dst_areas->addr != dst_addr ||
3473                             src_areas->first != src_areas[-1].first + width ||
3474                             dst_areas->first != dst_areas[-1].first + width)
3475                                 break;
3476                 }
3477                 if (chns > 1 && chns * width == step) {
3478                         if (src_offset != dst_offset ||
3479                             src_start->addr != dst_start->addr ||
3480                             src_start->first != dst_start->first) {
3481                                 /* Collapse the areas */
3482                                 snd_pcm_channel_area_t s, d;
3483                                 s.addr = src_start->addr;
3484                                 s.first = src_start->first;
3485                                 s.step = width;
3486                                 d.addr = dst_start->addr;
3487                                 d.first = dst_start->first;
3488                                 d.step = width;
3489                                 snd_pcm_area_copy(&d, dst_offset * chns,
3490                                                   &s, src_offset * chns, 
3491                                                   frames * chns, format);
3492                         }
3493                         channels -= chns;
3494                 } else {
3495                         snd_pcm_area_copy(dst_start, dst_offset,
3496                                           src_start, src_offset,
3497                                           frames, format);
3498                         src_areas = src_start + 1;
3499                         dst_areas = dst_start + 1;
3500                         channels--;
3501                 }
3502         }
3503         return 0;
3504 }
3505
3506 /**
3507  * \brief Copy one or more areas
3508  * \param dst_channels destination areas specification (one for each channel)
3509  * \param dst_offset offset in frames inside destination area
3510  * \param dst_size size in frames of the destination buffer
3511  * \param src_channels source areas specification (one for each channel)
3512  * \param src_offset offset in frames inside source area
3513  * \param src_size size in frames of the source buffer
3514  * \param channels channels count
3515  * \param frames frames to copy
3516  * \param format PCM sample format
3517  * \return 0 on success otherwise a negative error code
3518  */
3519 int snd_pcm_areas_copy_wrap(const snd_pcm_channel_area_t *dst_channels,
3520                             snd_pcm_uframes_t dst_offset,
3521                             const snd_pcm_uframes_t dst_size,
3522                             const snd_pcm_channel_area_t *src_channels,
3523                             snd_pcm_uframes_t src_offset,
3524                             const snd_pcm_uframes_t src_size,
3525                             const unsigned int channels,
3526                             snd_pcm_uframes_t frames,
3527                             const snd_pcm_format_t format)
3528 {
3529         while (frames > 0) {
3530                 int err;
3531                 snd_pcm_uframes_t xfer = frames;
3532                 /* do not write above the destination buffer */
3533                 if ((dst_offset + xfer) > dst_size)
3534                         xfer = dst_size - dst_offset;
3535                 /* do not read from above the source buffer */
3536                 if ((src_offset + xfer) > src_size)
3537                         xfer = src_size - src_offset;
3538                 err = snd_pcm_areas_copy(dst_channels, dst_offset, src_channels,
3539                                          src_offset, channels, xfer, format);
3540                 if (err < 0)
3541                         return err;
3542
3543                 dst_offset += xfer;
3544                 if (dst_offset >= dst_size)
3545                         dst_offset = 0;
3546                 src_offset += xfer;
3547                 if (src_offset >= src_size)
3548                         src_offset = 0;
3549                 frames -= xfer;
3550         }
3551
3552         return 0;
3553 }
3554
3555 static void dump_one_param(snd_pcm_hw_params_t *params, unsigned int k, snd_output_t *out)
3556 {
3557         snd_output_printf(out, "%s: ", snd_pcm_hw_param_name(k));
3558         snd_pcm_hw_param_dump(params, k, out);
3559         snd_output_putc(out, '\n');
3560 }
3561
3562 /**
3563  * \brief Dump a PCM hardware configuration space
3564  * \param params Configuration space
3565  * \param out Output handle
3566  * \return 0 on success otherwise a negative error code
3567  */
3568 int snd_pcm_hw_params_dump(snd_pcm_hw_params_t *params, snd_output_t *out)
3569 {
3570         unsigned int k;
3571         for (k = SND_PCM_HW_PARAM_FIRST_MASK; k <= SND_PCM_HW_PARAM_LAST_MASK; k++)
3572                 dump_one_param(params, k, out);
3573         for (k = SND_PCM_HW_PARAM_FIRST_INTERVAL; k <= SND_PCM_HW_PARAM_LAST_INTERVAL; k++)
3574                 dump_one_param(params, k, out);
3575         return 0;
3576 }
3577
3578 /**
3579  * \brief Check if hardware supports sample-resolution mmap for given configuration
3580  * \param params Configuration space
3581  * \retval 0 Hardware doesn't support sample-resolution mmap
3582  * \retval 1 Hardware supports sample-resolution mmap
3583  *
3584  * This function should only be called when the configuration space
3585  * contains a single configuration. Call #snd_pcm_hw_params to choose
3586  * a single configuration from the configuration space.
3587  */
3588 int snd_pcm_hw_params_can_mmap_sample_resolution(const snd_pcm_hw_params_t *params)
3589 {
3590         assert(params);
3591         if (CHECK_SANITY(params->info == ~0U)) {
3592                 SNDMSG("invalid PCM info field");
3593                 return 0; /* FIXME: should be a negative error? */
3594         }
3595         return !!(params->info & SNDRV_PCM_INFO_MMAP_VALID);
3596 }
3597
3598 /**
3599  * \brief Check if hardware does double buffering for start/stop for given configuration
3600  * \param params Configuration space
3601  * \retval 0 Hardware doesn't do double buffering for start/stop
3602  * \retval 1 Hardware does double buffering for start/stop
3603  *
3604  * This function should only be called when the configuration space
3605  * contains a single configuration. Call #snd_pcm_hw_params to choose
3606  * a single configuration from the configuration space.
3607  */
3608 int snd_pcm_hw_params_is_double(const snd_pcm_hw_params_t *params)
3609 {
3610         assert(params);
3611         if (CHECK_SANITY(params->info == ~0U)) {
3612                 SNDMSG("invalid PCM info field");
3613                 return 0; /* FIXME: should be a negative error? */
3614         }
3615         return !!(params->info & SNDRV_PCM_INFO_DOUBLE);
3616 }
3617
3618 /**
3619  * \brief Check if hardware does double buffering for data transfers for given configuration
3620  * \param params Configuration space
3621  * \retval 0 Hardware doesn't do double buffering for data transfers
3622  * \retval 1 Hardware does double buffering for data transfers
3623  *
3624  * This function should only be called when the configuration space
3625  * contains a single configuration. Call #snd_pcm_hw_params to choose
3626  * a single configuration from the configuration space.
3627  */
3628 int snd_pcm_hw_params_is_batch(const snd_pcm_hw_params_t *params)
3629 {
3630         assert(params);
3631         if (CHECK_SANITY(params->info == ~0U)) {
3632                 SNDMSG("invalid PCM info field");
3633                 return 0; /* FIXME: should be a negative error? */
3634         }
3635         return !!(params->info & SNDRV_PCM_INFO_BATCH);
3636 }
3637
3638 /**
3639  * \brief Check if hardware does block transfers for samples for given configuration
3640  * \param params Configuration space
3641  * \retval 0 Hardware doesn't block transfers
3642  * \retval 1 Hardware does block transfers
3643  *
3644  * This function should only be called when the configuration space
3645  * contains a single configuration. Call #snd_pcm_hw_params to choose
3646  * a single configuration from the configuration space.
3647  */
3648 int snd_pcm_hw_params_is_block_transfer(const snd_pcm_hw_params_t *params)
3649 {
3650         assert(params);
3651         if (CHECK_SANITY(params->info == ~0U)) {
3652                 SNDMSG("invalid PCM info field");
3653                 return 0; /* FIXME: should be a negative error? */
3654         }
3655         return !!(params->info & SNDRV_PCM_INFO_BLOCK_TRANSFER);
3656 }
3657
3658 /**
3659  * \brief Check if timestamps are monotonic for given configuration
3660  * \param params Configuration space
3661  * \retval 0 Device doesn't do monotomic timestamps
3662  * \retval 1 Device does monotonic timestamps
3663  *
3664  * This function should only be called when the configuration space
3665  * contains a single configuration. Call #snd_pcm_hw_params to choose
3666  * a single configuration from the configuration space.
3667  */
3668 int snd_pcm_hw_params_is_monotonic(const snd_pcm_hw_params_t *params)
3669 {
3670         assert(params);
3671         if (CHECK_SANITY(params->info == ~0U)) {
3672                 SNDMSG("invalid PCM info field");
3673                 return 0; /* FIXME: should be a negative error? */
3674         }
3675         return !!(params->info & SND_PCM_INFO_MONOTONIC);
3676 }
3677
3678 /**
3679  * \brief Check if hardware supports overrange detection
3680  * \param params Configuration space
3681  * \retval 0 Hardware doesn't support overrange detection
3682  * \retval 1 Hardware supports overrange detection
3683  *
3684  * This function should only be called when the configuration space
3685  * contains a single configuration. Call #snd_pcm_hw_params to choose
3686  * a single configuration from the configuration space.
3687  */
3688 int snd_pcm_hw_params_can_overrange(const snd_pcm_hw_params_t *params)
3689 {
3690         assert(params);
3691         if (CHECK_SANITY(params->info == ~0U)) {
3692                 SNDMSG("invalid PCM info field");
3693                 return 0; /* FIXME: should be a negative error? */
3694         }
3695         return !!(params->info & SNDRV_PCM_INFO_OVERRANGE);
3696 }
3697
3698 /**
3699  * \brief Check if hardware supports pause
3700  * \param params Configuration space
3701  * \retval 0 Hardware doesn't support pause
3702  * \retval 1 Hardware supports pause
3703  *
3704  * This function should only be called when the configuration space
3705  * contains a single configuration. Call #snd_pcm_hw_params to choose
3706  * a single configuration from the configuration space.
3707  */
3708 int snd_pcm_hw_params_can_pause(const snd_pcm_hw_params_t *params)
3709 {
3710         assert(params);
3711         if (CHECK_SANITY(params->info == ~0U)) {
3712                 SNDMSG("invalid PCM info field");
3713                 return 0; /* FIXME: should be a negative error? */
3714         }
3715         return !!(params->info & SNDRV_PCM_INFO_PAUSE);
3716 }
3717
3718 /**
3719  * \brief Check if hardware supports resume
3720  * \param params Configuration space
3721  * \retval 0 Hardware doesn't support resume
3722  * \retval 1 Hardware supports resume
3723  *
3724  * This function should only be called when the configuration space
3725  * contains a single configuration. Call #snd_pcm_hw_params to choose
3726  * a single configuration from the configuration space.
3727  */
3728 int snd_pcm_hw_params_can_resume(const snd_pcm_hw_params_t *params)
3729 {
3730         assert(params);
3731         if (CHECK_SANITY(params->info == ~0U)) {
3732                 SNDMSG("invalid PCM info field");
3733                 return 0; /* FIXME: should be a negative error? */
3734         }
3735         return !!(params->info & SNDRV_PCM_INFO_RESUME);
3736 }
3737
3738 /**
3739  * \brief Check if hardware does half-duplex only
3740  * \param params Configuration space
3741  * \retval 0 Hardware doesn't do half-duplex
3742  * \retval 1 Hardware does half-duplex
3743  *
3744  * This function should only be called when the configuration space
3745  * contains a single configuration. Call #snd_pcm_hw_params to choose
3746  * a single configuration from the configuration space.
3747  */
3748 int snd_pcm_hw_params_is_half_duplex(const snd_pcm_hw_params_t *params)
3749 {
3750         assert(params);
3751         if (CHECK_SANITY(params->info == ~0U)) {
3752                 SNDMSG("invalid PCM info field");
3753                 return 0; /* FIXME: should be a negative error? */
3754         }
3755         return !!(params->info & SNDRV_PCM_INFO_HALF_DUPLEX);
3756 }
3757
3758 /**
3759  * \brief Check if hardware does joint-duplex (playback and capture are somewhat correlated)
3760  * \param params Configuration space
3761  * \retval 0 Hardware doesn't do joint-duplex
3762  * \retval 1 Hardware does joint-duplex
3763  *
3764  * This function should only be called when the configuration space
3765  * contains a single configuration. Call #snd_pcm_hw_params to choose
3766  * a single configuration from the configuration space.
3767  */
3768 int snd_pcm_hw_params_is_joint_duplex(const snd_pcm_hw_params_t *params)
3769 {
3770         assert(params);
3771         if (CHECK_SANITY(params->info == ~0U)) {
3772                 SNDMSG("invalid PCM info field");
3773                 return 0; /* FIXME: should be a negative error? */
3774         }
3775         return !!(params->info & SNDRV_PCM_INFO_JOINT_DUPLEX);
3776 }
3777
3778 /**
3779  * \brief Check if hardware supports synchronized start with sample resolution
3780  * \param params Configuration space
3781  * \retval 0 Hardware doesn't support synchronized start
3782  * \retval 1 Hardware supports synchronized start
3783  *
3784  * This function should only be called when the configuration space
3785  * contains a single configuration. Call #snd_pcm_hw_params to choose
3786  * a single configuration from the configuration space.
3787  */
3788 int snd_pcm_hw_params_can_sync_start(const snd_pcm_hw_params_t *params)
3789 {
3790         assert(params);
3791         if (CHECK_SANITY(params->info == ~0U)) {
3792                 SNDMSG("invalid PCM info field");
3793                 return 0; /* FIXME: should be a negative error? */
3794         }
3795         return !!(params->info & SNDRV_PCM_INFO_SYNC_START);
3796 }
3797
3798 /**
3799  * \brief Check if hardware can disable period wakeups
3800  * \param params Configuration space
3801  * \retval 0 Hardware cannot disable period wakeups
3802  * \retval 1 Hardware can disable period wakeups
3803  */
3804 int snd_pcm_hw_params_can_disable_period_wakeup(const snd_pcm_hw_params_t *params)
3805 {
3806         assert(params);
3807         if (CHECK_SANITY(params->info == ~0U)) {
3808                 SNDMSG("invalid PCM info field");
3809                 return 0; /* FIXME: should be a negative error? */
3810         }
3811         return !!(params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP);
3812 }
3813
3814 /**
3815  * \brief Check if hardware is capable of perfect drain
3816  * \param params Configuration space
3817  * \retval 0 Hardware doesn't do perfect drain
3818  * \retval 1 Hardware does perfect drain
3819  *
3820  * This function should only be called when the configuration space
3821  * contains a single configuration. Call #snd_pcm_hw_params to choose
3822  * a single configuration from the configuration space.
3823  *
3824  * Perfect drain means that the hardware does not use samples
3825  * beyond the stream application pointer.
3826  */
3827 int snd_pcm_hw_params_is_perfect_drain(const snd_pcm_hw_params_t *params)
3828 {
3829         assert(params);
3830         if (CHECK_SANITY(params->info == ~0U)) {
3831                 SNDMSG("invalid PCM info field");
3832                 return 0; /* FIXME: should be a negative error? */
3833         }
3834         return !!(params->info & SNDRV_PCM_INFO_PERFECT_DRAIN);
3835 }
3836
3837 /**
3838  * \brief Check if hardware supports audio wallclock timestamps
3839  * \param params Configuration space
3840  * \retval 0 Hardware doesn't support audio wallclock timestamps
3841  * \retval 1 Hardware supports audio wallclock timestamps
3842  *
3843  * This function should only be called when the configuration space
3844  * contains a single configuration. Call #snd_pcm_hw_params to choose
3845  * a single configuration from the configuration space.
3846  */
3847 int snd_pcm_hw_params_supports_audio_wallclock_ts(const snd_pcm_hw_params_t *params)
3848 {
3849         /* deprecated */
3850         return snd_pcm_hw_params_supports_audio_ts_type(params,
3851                                                         SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT);
3852 }
3853
3854 /**
3855  * \brief Check if hardware supports type of audio timestamps
3856  * \param params Configuration space
3857  * \param type   Audio timestamp type
3858  * \retval 0 Hardware doesn't support type of audio timestamps
3859  * \retval 1 Hardware supports type of audio timestamps
3860  *
3861  * This function should only be called when the configuration space
3862  * contains a single configuration. Call #snd_pcm_hw_params to choose
3863  * a single configuration from the configuration space.
3864  */
3865 int snd_pcm_hw_params_supports_audio_ts_type(const snd_pcm_hw_params_t *params, int type)
3866 {
3867         assert(params);
3868         if (CHECK_SANITY(params->info == ~0U)) {
3869                 SNDMSG("invalid PCM info field");
3870                 return 0; /* FIXME: should be a negative error? */
3871         }
3872         switch (type) {
3873         case SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT:
3874                 return !!(params->info & SNDRV_PCM_INFO_HAS_WALL_CLOCK); /* deprecated */
3875         case SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT:
3876                 return 1; /* always supported, based on hw_ptr */
3877         case SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK:
3878                 return !!(params->info & SNDRV_PCM_INFO_HAS_LINK_ATIME);
3879         case SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK_ABSOLUTE:
3880                 return !!(params->info & SNDRV_PCM_INFO_HAS_LINK_ABSOLUTE_ATIME);
3881         case SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK_ESTIMATED:
3882                 return !!(params->info & SNDRV_PCM_INFO_HAS_LINK_ESTIMATED_ATIME);
3883         case SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK_SYNCHRONIZED:
3884                 return !!(params->info & SNDRV_PCM_INFO_HAS_LINK_SYNCHRONIZED_ATIME);
3885         default:
3886                 return 0;
3887         }
3888 }
3889
3890 /**
3891  * \brief Get rate exact info from a configuration space
3892  * \param params Configuration space
3893  * \param rate_num Pointer to returned rate numerator
3894  * \param rate_den Pointer to returned rate denominator
3895  * \return 0 otherwise a negative error code if the info is not available
3896  *
3897  * This function should only be called when the configuration space
3898  * contains a single configuration. Call #snd_pcm_hw_params to choose
3899  * a single configuration from the configuration space.
3900  */
3901 int snd_pcm_hw_params_get_rate_numden(const snd_pcm_hw_params_t *params,
3902                                       unsigned int *rate_num, unsigned int *rate_den)
3903 {
3904         assert(params);
3905         if (CHECK_SANITY(params->rate_den == 0)) {
3906                 SNDMSG("invalid rate_den value");
3907                 return -EINVAL;
3908         }
3909         *rate_num = params->rate_num;
3910         *rate_den = params->rate_den;
3911         return 0;
3912 }
3913
3914 /**
3915  * \brief Get sample resolution info from a configuration space
3916  * \param params Configuration space
3917  * \return sample resolution (in bits) otherwise a negative error code if the info is not available
3918  *
3919  * For linear formats, this function returns sample resolution -
3920  * used bits starting from the first usable significant bit defined by
3921  * the format (e.g. bit 31 for S32_LE format or bit 23 for S24_LE format -
3922  * starting from bit zero). Application may use full sample bit range defined
3923  * by the format, but additional bits (outside this sample resolution) are
3924  * stripped (not processed).
3925  *
3926  * For non-linear formats, this value may have a special meaning which may be defined in future.
3927  *
3928  * This function should only be called when the configuration space
3929  * contains a single configuration. Call #snd_pcm_hw_params to choose
3930  * a single configuration from the configuration space.
3931  */
3932 int snd_pcm_hw_params_get_sbits(const snd_pcm_hw_params_t *params)
3933 {
3934         assert(params);
3935         if (CHECK_SANITY(params->msbits == 0)) {
3936                 SNDMSG("invalid msbits value");
3937                 return -EINVAL;
3938         }
3939         return params->msbits;
3940 }
3941
3942 /**
3943  * \brief Get hardware FIFO size info from a configuration space
3944  * \param params Configuration space
3945  * \return FIFO size in frames otherwise a negative error code if the info is not available
3946  *
3947  * This function should only be called when the configuration space
3948  * contains a single configuration. Call #snd_pcm_hw_params to choose
3949  * a single configuration from the configuration space.
3950  */
3951 int snd_pcm_hw_params_get_fifo_size(const snd_pcm_hw_params_t *params)
3952 {
3953         assert(params);
3954         if (CHECK_SANITY(params->info == ~0U)) {
3955                 SNDMSG("invalid PCM info field");
3956                 return -EINVAL;
3957         }
3958         return params->fifo_size;
3959 }
3960
3961 /**
3962  * \brief Get hardware synchronization ID from a PCM info container
3963  * \param params Configuration space
3964  * \return 16-byte synchronization ID (use #SND_PCM_HW_PARAMS_SYNC_SIZE)
3965  *
3966  * This synchronization ID determines the similar clocks for the
3967  * PCM stream between multiple devices (including different cards).
3968  * "All zeros" means "not set". The contents of the ID can be used
3969  * only for a comparison with the contents of another ID returned
3970  * from this function. Applications should not do a comparison with
3971  * hard-coded values, because the implementation generating such
3972  * synchronization IDs may be changed in future.
3973  */
3974 const unsigned char *snd_pcm_hw_params_get_sync(const snd_pcm_hw_params_t *params)
3975 {
3976         assert(params);
3977         return params->sync;
3978 }
3979
3980 /**
3981  * \brief Fill params with a full configuration space for a PCM
3982  * \param pcm PCM handle
3983  * \param params Configuration space
3984  *
3985  * The configuration space will be filled with all possible ranges
3986  * for the PCM device.
3987  *
3988  * Note that the configuration space may be constrained by the
3989  * currently installed configuration on the PCM device. To remove
3990  * any constrains, free the configuration with #snd_pcm_hw_free
3991  * first.
3992  */
3993 int snd_pcm_hw_params_any(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
3994 {
3995         _snd_pcm_hw_params_any(params);
3996         return snd_pcm_hw_refine(pcm, params);
3997 }
3998
3999 /**
4000  * \brief get size of #snd_pcm_access_mask_t
4001  * \return size in bytes
4002  */
4003 size_t snd_pcm_access_mask_sizeof()
4004 {
4005         return sizeof(snd_pcm_access_mask_t);
4006 }
4007
4008 /**
4009  * \brief allocate an empty #snd_pcm_access_mask_t using standard malloc
4010  * \param ptr returned pointer
4011  * \return 0 on success otherwise negative error code
4012  */
4013 int snd_pcm_access_mask_malloc(snd_pcm_access_mask_t **ptr)
4014 {
4015         assert(ptr);
4016         *ptr = calloc(1, sizeof(snd_pcm_access_mask_t));
4017         if (!*ptr)
4018                 return -ENOMEM;
4019         return 0;
4020 }
4021
4022 /**
4023  * \brief frees a previously allocated #snd_pcm_access_mask_t
4024  * \param obj pointer to object to free
4025  */
4026 void snd_pcm_access_mask_free(snd_pcm_access_mask_t *obj)
4027 {
4028         free(obj);
4029 }
4030
4031 /**
4032  * \brief copy one #snd_pcm_access_mask_t to another
4033  * \param dst pointer to destination
4034  * \param src pointer to source
4035  */
4036 void snd_pcm_access_mask_copy(snd_pcm_access_mask_t *dst, const snd_pcm_access_mask_t *src)
4037 {
4038         assert(dst && src);
4039         *dst = *src;
4040 }
4041
4042 /**
4043  * \brief reset all bits in a #snd_pcm_access_mask_t
4044  * \param mask pointer to mask
4045  */
4046 void snd_pcm_access_mask_none(snd_pcm_access_mask_t *mask)
4047 {
4048         snd_mask_none((snd_mask_t *) mask);
4049 }
4050
4051 /**
4052  * \brief set all bits in a #snd_pcm_access_mask_t
4053  * \param mask pointer to mask
4054  */
4055 void snd_pcm_access_mask_any(snd_pcm_access_mask_t *mask)
4056 {
4057         snd_mask_any((snd_mask_t *) mask);
4058 }
4059
4060 /**
4061  * \brief test the presence of an access type in a #snd_pcm_access_mask_t
4062  * \param mask pointer to mask
4063  * \param val access type
4064  */
4065 int snd_pcm_access_mask_test(const snd_pcm_access_mask_t *mask, snd_pcm_access_t val)
4066 {
4067         return snd_mask_test((const snd_mask_t *) mask, (unsigned long) val);
4068 }
4069
4070 /**
4071  * \brief test, if given a #snd_pcm_access_mask_t is empty
4072  * \param mask pointer to mask
4073  * \retval 0 not empty
4074  * \retval 1 empty
4075  */
4076 int snd_pcm_access_mask_empty(const snd_pcm_access_mask_t *mask)
4077 {
4078         return snd_mask_empty((const snd_mask_t *) mask);
4079 }
4080
4081 /**
4082  * \brief make an access type present in a #snd_pcm_access_mask_t
4083  * \param mask pointer to mask
4084  * \param val access type
4085  */
4086 void snd_pcm_access_mask_set(snd_pcm_access_mask_t *mask, snd_pcm_access_t val)
4087 {
4088         snd_mask_set((snd_mask_t *) mask, (unsigned long) val);
4089 }
4090
4091 /**
4092  * \brief make an access type missing from a #snd_pcm_access_mask_t
4093  * \param mask pointer to mask
4094  * \param val access type
4095  */
4096 void snd_pcm_access_mask_reset(snd_pcm_access_mask_t *mask, snd_pcm_access_t val)
4097 {
4098         snd_mask_reset((snd_mask_t *) mask, (unsigned long) val);
4099 }
4100
4101 /**
4102  * \brief get size of #snd_pcm_format_mask_t
4103  * \return size in bytes
4104  */
4105 size_t snd_pcm_format_mask_sizeof()
4106 {
4107         return sizeof(snd_pcm_format_mask_t);
4108 }
4109
4110 /**
4111  * \brief allocate an empty #snd_pcm_format_mask_t using standard malloc
4112  * \param ptr returned pointer
4113  * \return 0 on success otherwise negative error code
4114  */
4115 int snd_pcm_format_mask_malloc(snd_pcm_format_mask_t **ptr)
4116 {
4117         assert(ptr);
4118         *ptr = calloc(1, sizeof(snd_pcm_format_mask_t));
4119         if (!*ptr)
4120                 return -ENOMEM;
4121         return 0;
4122 }
4123
4124 /**
4125  * \brief frees a previously allocated #snd_pcm_format_mask_t
4126  * \param obj pointer to object to free
4127  */
4128 void snd_pcm_format_mask_free(snd_pcm_format_mask_t *obj)
4129 {
4130         free(obj);
4131 }
4132
4133 /**
4134  * \brief copy one #snd_pcm_format_mask_t to another
4135  * \param dst pointer to destination
4136  * \param src pointer to source
4137  */
4138 void snd_pcm_format_mask_copy(snd_pcm_format_mask_t *dst, const snd_pcm_format_mask_t *src)
4139 {
4140         assert(dst && src);
4141         *dst = *src;
4142 }
4143
4144 /**
4145  * \brief reset all bits in a #snd_pcm_format_mask_t
4146  * \param mask pointer to mask
4147  */
4148 void snd_pcm_format_mask_none(snd_pcm_format_mask_t *mask)
4149 {
4150         snd_mask_none((snd_mask_t *) mask);
4151 }
4152
4153 /**
4154  * \brief set all bits in a #snd_pcm_format_mask_t
4155  * \param mask pointer to mask
4156  */
4157 void snd_pcm_format_mask_any(snd_pcm_format_mask_t *mask)
4158 {
4159         snd_mask_any((snd_mask_t *) mask);
4160 }
4161
4162 /**
4163  * \brief test the presence of a format in a #snd_pcm_format_mask_t
4164  * \param mask pointer to mask
4165  * \param val format
4166  */
4167 int snd_pcm_format_mask_test(const snd_pcm_format_mask_t *mask, snd_pcm_format_t val)
4168 {
4169         return snd_mask_test((const snd_mask_t *) mask, (unsigned long) val);
4170 }
4171
4172 /**
4173  * \brief test, if given a #snd_pcm_format_mask_t is empty
4174  * \param mask pointer to mask
4175  * \retval 0 not empty
4176  * \retval 1 empty
4177  */
4178 int snd_pcm_format_mask_empty(const snd_pcm_format_mask_t *mask)
4179 {
4180         return snd_mask_empty((const snd_mask_t *) mask);
4181 }
4182
4183 /**
4184  * \brief make a format present in a #snd_pcm_format_mask_t
4185  * \param mask pointer to mask
4186  * \param val format
4187  */
4188 void snd_pcm_format_mask_set(snd_pcm_format_mask_t *mask, snd_pcm_format_t val)
4189 {
4190         snd_mask_set((snd_mask_t *) mask, (unsigned long) val);
4191 }
4192
4193 /**
4194  * \brief make a format missing from a #snd_pcm_format_mask_t
4195  * \param mask pointer to mask
4196  * \param val format
4197  */
4198 void snd_pcm_format_mask_reset(snd_pcm_format_mask_t *mask, snd_pcm_format_t val)
4199 {
4200         snd_mask_reset((snd_mask_t *) mask, (unsigned long) val);
4201 }
4202
4203
4204 /**
4205  * \brief get size of #snd_pcm_subformat_mask_t
4206  * \return size in bytes
4207  */
4208 size_t snd_pcm_subformat_mask_sizeof()
4209 {
4210         return sizeof(snd_pcm_subformat_mask_t);
4211 }
4212
4213 /**
4214  * \brief allocate an empty #snd_pcm_subformat_mask_t using standard malloc
4215  * \param ptr returned pointer
4216  * \return 0 on success otherwise negative error code
4217  */
4218 int snd_pcm_subformat_mask_malloc(snd_pcm_subformat_mask_t **ptr)
4219 {
4220         assert(ptr);
4221         *ptr = calloc(1, sizeof(snd_pcm_subformat_mask_t));
4222         if (!*ptr)
4223                 return -ENOMEM;
4224         return 0;
4225 }
4226
4227 /**
4228  * \brief frees a previously allocated #snd_pcm_subformat_mask_t
4229  * \param obj pointer to object to free
4230  */
4231 void snd_pcm_subformat_mask_free(snd_pcm_subformat_mask_t *obj)
4232 {
4233         free(obj);
4234 }
4235
4236 /**
4237  * \brief copy one #snd_pcm_subformat_mask_t to another
4238  * \param dst pointer to destination
4239  * \param src pointer to source
4240  */
4241 void snd_pcm_subformat_mask_copy(snd_pcm_subformat_mask_t *dst, const snd_pcm_subformat_mask_t *src)
4242 {
4243         assert(dst && src);
4244         *dst = *src;
4245 }
4246
4247 /**
4248  * \brief reset all bits in a #snd_pcm_subformat_mask_t
4249  * \param mask pointer to mask
4250  */
4251 void snd_pcm_subformat_mask_none(snd_pcm_subformat_mask_t *mask)
4252 {
4253         snd_mask_none((snd_mask_t *) mask);
4254 }
4255
4256 /**
4257  * \brief set all bits in a #snd_pcm_subformat_mask_t
4258  * \param mask pointer to mask
4259  */
4260 void snd_pcm_subformat_mask_any(snd_pcm_subformat_mask_t *mask)
4261 {
4262         snd_mask_any((snd_mask_t *) mask);
4263 }
4264
4265 /**
4266  * \brief test the presence of a subformat in a #snd_pcm_subformat_mask_t
4267  * \param mask pointer to mask
4268  * \param val subformat
4269  */
4270 int snd_pcm_subformat_mask_test(const snd_pcm_subformat_mask_t *mask, snd_pcm_subformat_t val)
4271 {
4272         return snd_mask_test((const snd_mask_t *) mask, (unsigned long) val);
4273 }
4274
4275 /**
4276  * \brief test, if given a #snd_pcm_subformat_mask_t is empty
4277  * \param mask pointer to mask
4278  * \retval 0 not empty
4279  * \retval 1 empty
4280  */
4281 int snd_pcm_subformat_mask_empty(const snd_pcm_subformat_mask_t *mask)
4282 {
4283         return snd_mask_empty((const snd_mask_t *) mask);
4284 }
4285
4286 /**
4287  * \brief make a subformat present in a #snd_pcm_subformat_mask_t
4288  * \param mask pointer to mask
4289  * \param val subformat
4290  */
4291 void snd_pcm_subformat_mask_set(snd_pcm_subformat_mask_t *mask, snd_pcm_subformat_t val)
4292 {
4293         snd_mask_set((snd_mask_t *) mask, (unsigned long) val);
4294 }
4295
4296 /**
4297  * \brief make a subformat missing from a #snd_pcm_subformat_mask_t
4298  * \param mask pointer to mask
4299  * \param val subformat
4300  */
4301 void snd_pcm_subformat_mask_reset(snd_pcm_subformat_mask_t *mask, snd_pcm_subformat_t val)
4302 {
4303         snd_mask_reset((snd_mask_t *) mask, (unsigned long) val);
4304 }
4305
4306
4307 /**
4308  * \brief get size of #snd_pcm_hw_params_t
4309  * \return size in bytes
4310  */
4311 size_t snd_pcm_hw_params_sizeof()
4312 {
4313         return sizeof(snd_pcm_hw_params_t);
4314 }
4315
4316 /**
4317  * \brief allocate an invalid #snd_pcm_hw_params_t using standard malloc
4318  * \param ptr returned pointer
4319  * \return 0 on success otherwise negative error code
4320  */
4321 int snd_pcm_hw_params_malloc(snd_pcm_hw_params_t **ptr)
4322 {
4323         assert(ptr);
4324         *ptr = calloc(1, sizeof(snd_pcm_hw_params_t));
4325         if (!*ptr)
4326                 return -ENOMEM;
4327         return 0;
4328 }
4329
4330 /**
4331  * \brief frees a previously allocated #snd_pcm_hw_params_t
4332  * \param obj pointer to object to free
4333  */
4334 void snd_pcm_hw_params_free(snd_pcm_hw_params_t *obj)
4335 {
4336         free(obj);
4337 }
4338
4339 /**
4340  * \brief copy one #snd_pcm_hw_params_t to another
4341  * \param dst pointer to destination
4342  * \param src pointer to source
4343  */
4344 void snd_pcm_hw_params_copy(snd_pcm_hw_params_t *dst, const snd_pcm_hw_params_t *src)
4345 {
4346         assert(dst && src);
4347         *dst = *src;
4348 }
4349
4350
4351 /**
4352  * \brief Extract access type from a configuration space
4353  * \param params Configuration space
4354  * \param access Returned value
4355  * \return access type otherwise a negative error code if the configuration space does not contain a single value
4356  */
4357 #ifndef DOXYGEN
4358 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_access)(const snd_pcm_hw_params_t *params, snd_pcm_access_t *access)
4359 #else
4360 int snd_pcm_hw_params_get_access(const snd_pcm_hw_params_t *params, snd_pcm_access_t *access)
4361 #endif
4362 {
4363         unsigned int _val;
4364         int err = snd_pcm_hw_param_get(params, SND_PCM_HW_PARAM_ACCESS, &_val, NULL);
4365         if (err >= 0)
4366                 *access = _val;
4367         return err;
4368 }
4369
4370 /**
4371  * \brief Verify if an access type is available inside a configuration space for a PCM
4372  * \param pcm PCM handle
4373  * \param params Configuration space
4374  * \param access access type
4375  * \return 0 if available a negative error code otherwise
4376  */
4377 int snd_pcm_hw_params_test_access(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t access)
4378 {
4379         return snd_pcm_hw_param_set(pcm, params, SND_TEST, SND_PCM_HW_PARAM_ACCESS, access, 0);
4380 }
4381
4382 /**
4383  * \brief Restrict a configuration space to contain only one access type
4384  * \param pcm PCM handle
4385  * \param params Configuration space
4386  * \param access access type
4387  * \return 0 otherwise a negative error code if configuration space would become empty
4388  */
4389 int snd_pcm_hw_params_set_access(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t access)
4390 {
4391         return snd_pcm_hw_param_set(pcm, params, SND_TRY, SND_PCM_HW_PARAM_ACCESS, access, 0);
4392 }
4393
4394 /**
4395  * \brief Restrict a configuration space to contain only its first access type
4396  * \param pcm PCM handle
4397  * \param params Configuration space
4398  * \param access Returned first access type
4399  * \return 0 otherwise a negative error code
4400  */
4401 #ifndef DOXYGEN
4402 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)
4403 #else
4404 int snd_pcm_hw_params_set_access_first(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t *access)
4405 #endif
4406 {
4407         return snd_pcm_hw_param_set_first(pcm, params, SND_PCM_HW_PARAM_ACCESS, access, NULL);
4408 }
4409
4410 /**
4411  * \brief Restrict a configuration space to contain only its last access type
4412  * \param pcm PCM handle
4413  * \param params Configuration space
4414  * \param access Returned last access type
4415  * \return 0 otherwise a negative error code
4416  */
4417 #ifndef DOXYGEN
4418 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)
4419 #else
4420 int snd_pcm_hw_params_set_access_last(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t *access)
4421 #endif
4422 {
4423         return snd_pcm_hw_param_set_last(pcm, params, SND_PCM_HW_PARAM_ACCESS, access, NULL);
4424 }
4425
4426 /**
4427  * \brief Restrict a configuration space to contain only a set of access types
4428  * \param pcm PCM handle
4429  * \param params Configuration space
4430  * \param mask Access mask
4431  * \return 0 otherwise a negative error code
4432  */
4433 int snd_pcm_hw_params_set_access_mask(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_mask_t *mask)
4434 {
4435         return snd_pcm_hw_param_set_mask(pcm, params, SND_TRY, SND_PCM_HW_PARAM_ACCESS, (snd_mask_t *) mask);
4436 }
4437
4438 /**
4439  * \brief Get access mask from a configuration space
4440  * \param params Configuration space
4441  * \param mask Returned Access mask
4442  */
4443 int snd_pcm_hw_params_get_access_mask(snd_pcm_hw_params_t *params, snd_pcm_access_mask_t *mask)
4444 {
4445         if (params == NULL || mask == NULL)
4446                 return -EINVAL;
4447         snd_pcm_access_mask_copy(mask, snd_pcm_hw_param_get_mask(params, SND_PCM_HW_PARAM_ACCESS));
4448         return 0;
4449 }
4450
4451
4452 /**
4453  * \brief Extract format from a configuration space
4454  * \param params Configuration space
4455  * \param format returned format
4456  * \return format otherwise a negative error code if the configuration space does not contain a single value
4457  */
4458 #ifndef DOXYGEN
4459 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_format)(const snd_pcm_hw_params_t *params, snd_pcm_format_t *format)
4460 #else
4461 int snd_pcm_hw_params_get_format(const snd_pcm_hw_params_t *params, snd_pcm_format_t *format)
4462 #endif
4463 {
4464         return snd_pcm_hw_param_get(params, SND_PCM_HW_PARAM_FORMAT, (unsigned int *)format, NULL);
4465 }
4466
4467 /**
4468  * \brief Verify if a format is available inside a configuration space for a PCM
4469  * \param pcm PCM handle
4470  * \param params Configuration space
4471  * \param format format
4472  * \return 0 if available a negative error code otherwise
4473  */
4474 int snd_pcm_hw_params_test_format(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t format)
4475 {
4476         return snd_pcm_hw_param_set(pcm, params, SND_TEST, SND_PCM_HW_PARAM_FORMAT, format, 0);
4477 }
4478
4479 /**
4480  * \brief Restrict a configuration space to contain only one format
4481  * \param pcm PCM handle
4482  * \param params Configuration space
4483  * \param format format
4484  * \return 0 otherwise a negative error code
4485  */
4486 int snd_pcm_hw_params_set_format(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t format)
4487 {
4488         return snd_pcm_hw_param_set(pcm, params, SND_TRY, SND_PCM_HW_PARAM_FORMAT, format, 0);
4489 }
4490
4491 /**
4492  * \brief Restrict a configuration space to contain only its first format
4493  * \param pcm PCM handle
4494  * \param params Configuration space
4495  * \param format Returned first format
4496  * \return 0 otherwise a negative error code
4497  */
4498 #ifndef DOXYGEN
4499 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)
4500 #else
4501 int snd_pcm_hw_params_set_format_first(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t *format)
4502 #endif
4503 {
4504         return snd_pcm_hw_param_set_first(pcm, params, SND_PCM_HW_PARAM_FORMAT, (unsigned int *)format, NULL);
4505 }
4506
4507 /**
4508  * \brief Restrict a configuration space to contain only its last format
4509  * \param pcm PCM handle
4510  * \param params Configuration space
4511  * \param format Returned last format
4512  * \return 0 otherwise a negative error code
4513  */
4514 #ifndef DOXYGEN
4515 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)
4516 #else
4517 int snd_pcm_hw_params_set_format_last(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t *format)
4518 #endif
4519 {
4520         return snd_pcm_hw_param_set_last(pcm, params, SND_PCM_HW_PARAM_FORMAT, (unsigned int *)format, NULL);
4521 }
4522
4523 /**
4524  * \brief Restrict a configuration space to contain only a set of formats
4525  * \param pcm PCM handle
4526  * \param params Configuration space
4527  * \param mask Format mask
4528  * \return 0 otherwise a negative error code
4529  */
4530 int snd_pcm_hw_params_set_format_mask(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_mask_t *mask)
4531 {
4532         return snd_pcm_hw_param_set_mask(pcm, params, SND_TRY, SND_PCM_HW_PARAM_FORMAT, (snd_mask_t *) mask);
4533 }
4534
4535 /**
4536  * \brief Get format mask from a configuration space
4537  * \param params Configuration space
4538  * \param mask Returned Format mask
4539  */
4540 void snd_pcm_hw_params_get_format_mask(snd_pcm_hw_params_t *params, snd_pcm_format_mask_t *mask)
4541 {
4542         snd_pcm_format_mask_copy(mask, snd_pcm_hw_param_get_mask(params, SND_PCM_HW_PARAM_FORMAT));
4543 }
4544
4545
4546 /**
4547  * \brief Extract subformat from a configuration space
4548  * \param params Configuration space
4549  * \param subformat Returned subformat value
4550  * \return subformat otherwise a negative error code if the configuration space does not contain a single value
4551  */
4552 #ifndef DOXYGEN
4553 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_subformat)(const snd_pcm_hw_params_t *params, snd_pcm_subformat_t *subformat)
4554 #else
4555 int snd_pcm_hw_params_get_subformat(const snd_pcm_hw_params_t *params, snd_pcm_subformat_t *subformat)
4556 #endif
4557 {
4558         return snd_pcm_hw_param_get(params, SND_PCM_HW_PARAM_SUBFORMAT, (unsigned int *)subformat, NULL);
4559 }
4560
4561 /**
4562  * \brief Verify if a subformat is available inside a configuration space for a PCM
4563  * \param pcm PCM handle
4564  * \param params Configuration space
4565  * \param subformat subformat value
4566  * \return 0 if available a negative error code otherwise
4567  */
4568 int snd_pcm_hw_params_test_subformat(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_subformat_t subformat)
4569 {
4570         return snd_pcm_hw_param_set(pcm, params, SND_TEST, SND_PCM_HW_PARAM_SUBFORMAT, subformat, 0);
4571 }
4572
4573 /**
4574  * \brief Restrict a configuration space to contain only one subformat
4575  * \param pcm PCM handle
4576  * \param params Configuration space
4577  * \param subformat subformat value
4578  * \return 0 otherwise a negative error code if configuration space would become empty
4579  */
4580 int snd_pcm_hw_params_set_subformat(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_subformat_t subformat)
4581 {
4582         return snd_pcm_hw_param_set(pcm, params, SND_TRY, SND_PCM_HW_PARAM_SUBFORMAT, subformat, 0);
4583 }
4584
4585 /**
4586  * \brief Restrict a configuration space to contain only its first subformat
4587  * \param pcm PCM handle
4588  * \param params Configuration space
4589  * \param subformat Returned subformat
4590  * \return 0 otherwise a negative error code
4591  */
4592 #ifndef DOXYGEN
4593 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)
4594 #else
4595 int snd_pcm_hw_params_set_subformat_first(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_subformat_t *subformat)
4596 #endif
4597 {
4598         return snd_pcm_hw_param_set_first(pcm, params, SND_PCM_HW_PARAM_SUBFORMAT, (unsigned int *)subformat, NULL);
4599 }
4600
4601 /**
4602  * \brief Restrict a configuration space to contain only its last subformat
4603  * \param pcm PCM handle
4604  * \param params Configuration space
4605  * \param subformat Returned subformat
4606  * \return 0 otherwise a negative error code
4607  */
4608 #ifndef DOXYGEN
4609 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)
4610 #else
4611 int snd_pcm_hw_params_set_subformat_last(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_subformat_t *subformat)
4612 #endif
4613 {
4614         return snd_pcm_hw_param_set_last(pcm, params, SND_PCM_HW_PARAM_SUBFORMAT, (unsigned int *)subformat, NULL);
4615 }
4616
4617 /**
4618  * \brief Restrict a configuration space to contain only a set of subformats
4619  * \param pcm PCM handle
4620  * \param params Configuration space
4621  * \param mask Subformat mask
4622  * \return 0 otherwise a negative error code
4623  */
4624 int snd_pcm_hw_params_set_subformat_mask(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_subformat_mask_t *mask)
4625 {
4626         return snd_pcm_hw_param_set_mask(pcm, params, SND_TRY, SND_PCM_HW_PARAM_SUBFORMAT, (snd_mask_t *) mask);
4627 }
4628
4629 /**
4630  * \brief Get subformat mask from a configuration space
4631  * \param params Configuration space
4632  * \param mask Returned Subformat mask
4633  */
4634 void snd_pcm_hw_params_get_subformat_mask(snd_pcm_hw_params_t *params, snd_pcm_subformat_mask_t *mask)
4635 {
4636         snd_pcm_subformat_mask_copy(mask, snd_pcm_hw_param_get_mask(params, SND_PCM_HW_PARAM_SUBFORMAT));
4637 }
4638
4639
4640 /**
4641  * \brief Extract channels from a configuration space
4642  * \param params Configuration space
4643  * \param val Returned channels count
4644  * \return 0 otherwise a negative error code if the configuration space does not contain a single value
4645  */
4646 #ifndef DOXYGEN
4647 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_channels)(const snd_pcm_hw_params_t *params, unsigned int *val)
4648 #else
4649 int snd_pcm_hw_params_get_channels(const snd_pcm_hw_params_t *params, unsigned int *val)
4650 #endif
4651 {
4652         return snd_pcm_hw_param_get(params, SND_PCM_HW_PARAM_CHANNELS, val, NULL);
4653 }
4654
4655 /**
4656  * \brief Extract minimum channels count from a configuration space
4657  * \param params Configuration space
4658  * \param val minimum channels count
4659  * \return 0 otherwise a negative error code
4660  */
4661 #ifndef DOXYGEN
4662 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_channels_min)(const snd_pcm_hw_params_t *params, unsigned int *val)
4663 #else
4664 int snd_pcm_hw_params_get_channels_min(const snd_pcm_hw_params_t *params, unsigned int *val)
4665 #endif
4666 {
4667         return snd_pcm_hw_param_get_min(params, SND_PCM_HW_PARAM_CHANNELS, val, NULL);
4668 }
4669
4670 /**
4671  * \brief Extract maximum channels count from a configuration space
4672  * \param params Configuration space
4673  * \param val maximum channels count
4674  * \return 0 otherwise a negative error code
4675  */
4676 #ifndef DOXYGEN
4677 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_channels_max)(const snd_pcm_hw_params_t *params, unsigned int *val)
4678 #else
4679 int snd_pcm_hw_params_get_channels_max(const snd_pcm_hw_params_t *params, unsigned int *val)
4680 #endif
4681 {
4682         return snd_pcm_hw_param_get_max(params, SND_PCM_HW_PARAM_CHANNELS, val, NULL);
4683 }
4684
4685 /**
4686  * \brief Verify if a channels count is available inside a configuration space for a PCM
4687  * \param pcm PCM handle
4688  * \param params Configuration space
4689  * \param val channels count
4690  * \return 0 if available a negative error code otherwise
4691  */
4692 int snd_pcm_hw_params_test_channels(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val)
4693 {
4694         return snd_pcm_hw_param_set(pcm, params, SND_TEST, SND_PCM_HW_PARAM_CHANNELS, val, 0);
4695 }
4696
4697 /**
4698  * \brief Restrict a configuration space to contain only one channels count
4699  * \param pcm PCM handle
4700  * \param params Configuration space
4701  * \param val channels count
4702  * \return 0 otherwise a negative error code if configuration space would become empty
4703  */
4704 int snd_pcm_hw_params_set_channels(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val)
4705 {
4706         return snd_pcm_hw_param_set(pcm, params, SND_TRY, SND_PCM_HW_PARAM_CHANNELS, val, 0);
4707 }
4708
4709 /**
4710  * \brief Restrict a configuration space with a minimum channels count
4711  * \param pcm PCM handle
4712  * \param params Configuration space
4713  * \param val minimum channels count (on return filled with actual minimum)
4714  * \return 0 otherwise a negative error code if configuration space would become empty
4715  */
4716 int snd_pcm_hw_params_set_channels_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val)
4717 {
4718         return snd_pcm_hw_param_set_min(pcm, params, SND_TRY, SND_PCM_HW_PARAM_CHANNELS, val, NULL);
4719 }
4720
4721 /**
4722  * \brief Restrict a configuration space with a maximum channels count
4723  * \param pcm PCM handle
4724  * \param params Configuration space
4725  * \param val maximum channels count (on return filled with actual maximum)
4726  * \return 0 otherwise a negative error code if configuration space would become empty
4727  */
4728 int snd_pcm_hw_params_set_channels_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val)
4729 {
4730         return snd_pcm_hw_param_set_max(pcm, params, SND_TRY, SND_PCM_HW_PARAM_CHANNELS, val, NULL);
4731 }
4732
4733 /**
4734  * \brief Restrict a configuration space to have channels counts in a given range
4735  * \param pcm PCM handle
4736  * \param params Configuration space
4737  * \param min minimum channels count (on return filled with actual minimum)
4738  * \param max maximum channels count (on return filled with actual maximum)
4739  * \return 0 otherwise a negative error code if configuration space would become empty
4740  */
4741 int snd_pcm_hw_params_set_channels_minmax(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *min, unsigned int *max)
4742 {
4743         return snd_pcm_hw_param_set_minmax(pcm, params, SND_TRY, SND_PCM_HW_PARAM_CHANNELS, min, NULL, max, NULL);
4744 }
4745
4746 /**
4747  * \brief Restrict a configuration space to have channels count nearest to a target
4748  * \param pcm PCM handle
4749  * \param params Configuration space
4750  * \param val target channels count, returned chosen channels count
4751  * \return 0 otherwise a negative error code if configuration space is empty
4752  */
4753 #ifndef DOXYGEN
4754 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_channels_near)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val)
4755 #else
4756 int snd_pcm_hw_params_set_channels_near(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val)
4757 #endif
4758 {
4759         return snd_pcm_hw_param_set_near(pcm, params, SND_PCM_HW_PARAM_CHANNELS, val, NULL);
4760 }
4761
4762 /**
4763  * \brief Restrict a configuration space to contain only its minimum channels count
4764  * \param pcm PCM handle
4765  * \param params Configuration space
4766  * \param val minimum channels count
4767  * \return 0 otherwise a negative error code
4768  */
4769 #ifndef DOXYGEN
4770 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_channels_first)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val)
4771 #else
4772 int snd_pcm_hw_params_set_channels_first(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val)
4773 #endif
4774 {
4775         return snd_pcm_hw_param_set_first(pcm, params, SND_PCM_HW_PARAM_CHANNELS, val, NULL);
4776 }
4777
4778 /**
4779  * \brief Restrict a configuration space to contain only its maximum channels count
4780  * \param pcm PCM handle
4781  * \param params Configuration space
4782  * \param val maximum channels count
4783  * \return 0 otherwise a negative error code
4784  */
4785 #ifndef DOXYGEN
4786 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_set_channels_last)(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val)
4787 #else
4788 int snd_pcm_hw_params_set_channels_last(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val)
4789 #endif
4790 {
4791         return snd_pcm_hw_param_set_last(pcm, params, SND_PCM_HW_PARAM_CHANNELS, val, NULL);
4792 }
4793
4794
4795 /**
4796  * \brief Extract rate from a configuration space
4797  * \param params Configuration space
4798  * \param val Returned approximate rate
4799  * \param dir Sub unit direction
4800  * \return 0 otherwise a negative error code if the configuration space does not contain a single value
4801  *
4802  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
4803  */
4804 #ifndef DOXYGEN
4805 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_rate)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
4806 #else
4807 int snd_pcm_hw_params_get_rate(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
4808 #endif
4809 {
4810         return snd_pcm_hw_param_get(params, SND_PCM_HW_PARAM_RATE, val, dir);
4811 }
4812
4813 /**
4814  * \brief Extract minimum rate from a configuration space
4815  * \param params Configuration space
4816  * \param val Returned approximate minimum rate
4817  * \param dir Sub unit direction
4818  * \return 0 otherwise a negative error code
4819  *
4820  * Exact value is <,=,> the returned one following dir (-1,0,1)
4821  */
4822 #ifndef DOXYGEN
4823 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_rate_min)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
4824 #else
4825 int snd_pcm_hw_params_get_rate_min(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
4826 #endif
4827 {
4828         return snd_pcm_hw_param_get_min(params, SND_PCM_HW_PARAM_RATE, val, dir);
4829 }
4830
4831 /**
4832  * \brief Extract maximum rate from a configuration space
4833  * \param params Configuration space
4834  * \param val Returned approximate maximum rate
4835  * \param dir Sub unit direction
4836  * \return 0 otherwise a negative error code
4837  *
4838  * Exact value is <,=,> the returned one following dir (-1,0,1)
4839  */
4840 #ifndef DOXYGEN
4841 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_rate_max)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
4842 #else
4843 int snd_pcm_hw_params_get_rate_max(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
4844 #endif
4845 {
4846         return snd_pcm_hw_param_get_max(params, SND_PCM_HW_PARAM_RATE, val, dir);
4847 }
4848
4849 /**
4850  * \brief Verify if a rate is available inside a configuration space for a PCM
4851  * \param pcm PCM handle
4852  * \param params Configuration space
4853  * \param val approximate rate
4854  * \param dir Sub unit direction
4855  * \return 0 if available a negative error code otherwise
4856  *
4857  * Wanted exact value is <,=,> val following dir (-1,0,1)
4858  */
4859 int snd_pcm_hw_params_test_rate(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir)
4860 {
4861         return snd_pcm_hw_param_set(pcm, params, SND_TEST, SND_PCM_HW_PARAM_RATE, val, dir);
4862 }
4863
4864 /**
4865  * \brief Restrict a configuration space to contain only one rate
4866  * \param pcm PCM handle
4867  * \param params Configuration space
4868  * \param val approximate rate
4869  * \param dir Sub unit direction
4870  * \return 0 otherwise a negative error code if configuration space would become empty
4871  *
4872  * Wanted exact value is <,=,> val following dir (-1,0,1)
4873  */
4874 int snd_pcm_hw_params_set_rate(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir)
4875 {
4876         return snd_pcm_hw_param_set(pcm, params, SND_TRY, SND_PCM_HW_PARAM_RATE, val, dir);
4877 }
4878
4879 /**
4880  * \brief Restrict a configuration space with a minimum rate
4881  * \param pcm PCM handle
4882  * \param params Configuration space
4883  * \param val approximate minimum rate (on return filled with actual minimum)
4884  * \param dir Sub unit direction (on return filled with actual direction)
4885  * \return 0 otherwise a negative error code if configuration space would become empty
4886  *
4887  * Wanted/actual exact minimum is <,=,> val following dir (-1,0,1)
4888  */
4889 int snd_pcm_hw_params_set_rate_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
4890 {
4891         return snd_pcm_hw_param_set_min(pcm, params, SND_TRY, SND_PCM_HW_PARAM_RATE, val, dir);
4892 }
4893
4894 /**
4895  * \brief Restrict a configuration space with a maximum rate
4896  * \param pcm PCM handle
4897  * \param params Configuration space
4898  * \param val approximate maximum rate (on return filled with actual maximum)
4899  * \param dir Sub unit direction (on return filled with actual direction)
4900  * \return 0 otherwise a negative error code if configuration space would become empty
4901  *
4902  * Wanted/actual exact maximum is <,=,> val following dir (-1,0,1)
4903  */
4904 int snd_pcm_hw_params_set_rate_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
4905 {
4906         return snd_pcm_hw_param_set_max(pcm, params, SND_TRY, SND_PCM_HW_PARAM_RATE, val, dir);
4907 }
4908
4909 /**
4910  * \brief Restrict a configuration space to have rates in a given range
4911  * \param pcm PCM handle
4912  * \param params Configuration space
4913  * \param min approximate minimum rate (on return filled with actual minimum)
4914  * \param mindir Sub unit direction for minimum (on return filled with actual direction)
4915  * \param max approximate maximum rate (on return filled with actual maximum)
4916  * \param maxdir Sub unit direction for maximum (on return filled with actual direction)
4917  * \return 0 otherwise a negative error code if configuration space would become empty
4918  *
4919  * Wanted/actual exact min/max is <,=,> val following dir (-1,0,1)
4920  */
4921 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)
4922 {
4923         return snd_pcm_hw_param_set_minmax(pcm, params, SND_TRY, SND_PCM_HW_PARAM_RATE, min, mindir, max, maxdir);
4924 }
4925
4926 /**
4927  * \brief Restrict a configuration space to have rate nearest to a target
4928  * \param pcm PCM handle
4929  * \param params Configuration space
4930  * \param val approximate target rate / returned approximate set rate
4931  * \param dir Sub unit direction
4932  * \return 0 otherwise a negative error code if configuration space is empty
4933  *
4934  * target/chosen exact value is <,=,> val following dir (-1,0,1)
4935  */
4936 #ifndef DOXYGEN
4937 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)
4938 #else
4939 int snd_pcm_hw_params_set_rate_near(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
4940 #endif
4941 {
4942         return snd_pcm_hw_param_set_near(pcm, params, SND_PCM_HW_PARAM_RATE, val, dir);
4943 }
4944
4945 /**
4946  * \brief Restrict a configuration space to contain only its minimum rate
4947  * \param pcm PCM handle
4948  * \param params Configuration space
4949  * \param val Returned minimum approximate rate
4950  * \param dir Sub unit direction
4951  * \return 0 otherwise a negative error code
4952  *
4953  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
4954  */
4955 #ifndef DOXYGEN
4956 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)
4957 #else
4958 int snd_pcm_hw_params_set_rate_first(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
4959 #endif
4960 {
4961         return snd_pcm_hw_param_set_first(pcm, params, SND_PCM_HW_PARAM_RATE, val, dir);
4962 }
4963
4964 /**
4965  * \brief Restrict a configuration space to contain only its maximum rate
4966  * \param pcm PCM handle
4967  * \param params Configuration space
4968  * \param val Returned maximum approximate rate
4969  * \param dir Sub unit direction
4970  * \return 0 otherwise a negative error code
4971  *
4972  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
4973  */
4974 #ifndef DOXYGEN
4975 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)
4976 #else
4977 int snd_pcm_hw_params_set_rate_last(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
4978 #endif
4979 {
4980         return snd_pcm_hw_param_set_last(pcm, params, SND_PCM_HW_PARAM_RATE, val, dir);
4981 }
4982
4983 /**
4984  * \brief Restrict a configuration space to contain only real hardware rates
4985  * \param pcm PCM handle
4986  * \param params Configuration space
4987  * \param val 0 = disable, 1 = enable (default) rate resampling
4988  * \return 0 otherwise a negative error code
4989  */
4990 int snd_pcm_hw_params_set_rate_resample(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val)
4991 {
4992         assert(pcm && params);
4993         if (!val)
4994                 params->flags |= SND_PCM_HW_PARAMS_NORESAMPLE;
4995         else
4996                 params->flags &= ~SND_PCM_HW_PARAMS_NORESAMPLE;
4997         params->rmask = ~0;
4998         return snd_pcm_hw_refine(pcm, params);
4999 }
5000
5001 /**
5002  * \brief Extract resample state from a configuration space
5003  * \param pcm PCM handle
5004  * \param params Configuration space
5005  * \param val 0 = disable, 1 = enable rate resampling
5006  * \return 0 otherwise a negative error code
5007  */
5008 int snd_pcm_hw_params_get_rate_resample(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val)
5009 {
5010         assert(pcm && params && val);
5011         *val = params->flags & SND_PCM_HW_PARAMS_NORESAMPLE ? 0 : 1;
5012         return 0;
5013 }
5014
5015 /**
5016  * \brief Restrict a configuration space to allow the buffer to be accessible from outside
5017  * \param pcm PCM handle
5018  * \param params Configuration space
5019  * \param val 0 = disable, 1 = enable (default) exporting buffer
5020  * \return 0 otherwise a negative error code
5021  */
5022 int snd_pcm_hw_params_set_export_buffer(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val)
5023 {
5024         assert(pcm && params);
5025         if (val)
5026                 params->flags |= SND_PCM_HW_PARAMS_EXPORT_BUFFER;
5027         else
5028                 params->flags &= ~SND_PCM_HW_PARAMS_EXPORT_BUFFER;
5029         params->rmask = ~0;
5030         return snd_pcm_hw_refine(pcm, params);
5031 }
5032
5033 /**
5034  * \brief Extract buffer accessibility from a configuration space
5035  * \param pcm PCM handle
5036  * \param params Configuration space
5037  * \param val 0 = disable, 1 = enable exporting buffer
5038  * \return 0 otherwise a negative error code
5039  */
5040 int snd_pcm_hw_params_get_export_buffer(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val)
5041 {
5042         assert(pcm && params && val);
5043         *val = params->flags & SND_PCM_HW_PARAMS_EXPORT_BUFFER ? 1 : 0;
5044         return 0;
5045 }
5046
5047 /**
5048  * \brief Restrict a configuration space to settings without period wakeups
5049  * \param pcm PCM handle
5050  * \param params Configuration space
5051  * \param val 0 = disable, 1 = enable (default) period wakeup
5052  * \return Zero on success, otherwise a negative error code.
5053  *
5054  * This function must be called only on devices where non-blocking mode is
5055  * enabled.
5056  *
5057  * To check whether the hardware does support disabling period wakeups, call
5058  * #snd_pcm_hw_params_can_disable_period_wakeup(). If the hardware does not
5059  * support this mode, standard period wakeups will be generated.
5060  *
5061  * Even with disabled period wakeups, the period size/time/count parameters
5062  * are valid; it is suggested to use #snd_pcm_hw_params_set_period_size_last().
5063  *
5064  * When period wakeups are disabled, the application must not use any functions
5065  * that could block on this device. The use of poll should be limited to error
5066  * cases. The application needs to use an external event or a timer to
5067  * check the state of the ring buffer and refill it apropriately.
5068  */
5069 int snd_pcm_hw_params_set_period_wakeup(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val)
5070 {
5071         assert(pcm && params);
5072
5073         if (!val) {
5074                 if (!(pcm->mode & SND_PCM_NONBLOCK))
5075                         return -EINVAL;
5076                 params->flags |= SND_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
5077         } else
5078                 params->flags &= ~SND_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
5079         params->rmask = ~0;
5080
5081         return snd_pcm_hw_refine(pcm, params);
5082 }
5083
5084 /**
5085  * \brief Extract period wakeup flag from a configuration space
5086  * \param pcm PCM handle
5087  * \param params Configuration space
5088  * \param val 0 = disabled, 1 = enabled period wakeups
5089  * \return Zero on success, otherwise a negative error code.
5090  */
5091 int snd_pcm_hw_params_get_period_wakeup(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val)
5092 {
5093         assert(pcm && params && val);
5094         *val = params->flags & SND_PCM_HW_PARAMS_NO_PERIOD_WAKEUP ? 0 : 1;
5095         return 0;
5096 }
5097
5098 /**
5099  * \brief Restrict a configuration space to fill the end of playback stream with silence when drain() is invoked
5100  * \param pcm PCM handle
5101  * \param params Configuration space
5102  * \param val 0 = disabled, 1 = enabled (default) fill the end of the playback stream with silence when drain() is invoked
5103  * \return Zero on success, otherwise a negative error code.
5104  *
5105  * When disabled, the application should handle the end of stream gracefully
5106  * (fill the silent samples to align to the period size plus some extra
5107  * samples for hardware / driver without perfect drain). Note that the rewind
5108  * may be used for this purpose or the sw_params silencing mechanism.
5109  */
5110 int snd_pcm_hw_params_set_drain_silence(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val)
5111 {
5112         assert(pcm && params);
5113         if (val)
5114                 params->flags &= ~SND_PCM_HW_PARAMS_NO_DRAIN_SILENCE;
5115         else
5116                 params->flags |= SND_PCM_HW_PARAMS_NO_DRAIN_SILENCE;
5117         params->rmask = ~0;
5118         return snd_pcm_hw_refine(pcm, params);
5119 }
5120
5121 /**
5122  * \brief Extract drain with the filling of silence samples from a configuration space
5123  * \param pcm PCM handle
5124  * \param params Configuration space
5125  * \param val 0 = disabled, 1 = enabled
5126  * \return 0 otherwise a negative error code
5127  */
5128 int snd_pcm_hw_params_get_drain_silence(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val)
5129 {
5130         assert(pcm && params && val);
5131         *val = params->flags & SND_PCM_HW_PARAMS_NO_DRAIN_SILENCE ? 0 : 1;
5132         return 0;
5133 }
5134
5135 /**
5136  * \brief Extract period time from a configuration space
5137  * \param params Configuration space
5138  * \param val Returned approximate period duration in us
5139  * \param dir Sub unit direction
5140  * \return 0 otherwise a negative error code if the configuration space does not contain a single value
5141  *
5142  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
5143  */
5144 #ifndef DOXYGEN
5145 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_period_time)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5146 #else
5147 int snd_pcm_hw_params_get_period_time(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5148 #endif
5149 {
5150         return snd_pcm_hw_param_get(params, SND_PCM_HW_PARAM_PERIOD_TIME, val, dir);
5151 }
5152
5153 /**
5154  * \brief Extract minimum period time from a configuration space
5155  * \param params Configuration space
5156  * \param val approximate minimum period duration in us
5157  * \param dir Sub unit direction
5158  * \return 0 otherwise a negative error code
5159  *
5160  * Exact value is <,=,> the returned one following dir (-1,0,1)
5161  */
5162 #ifndef DOXYGEN
5163 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_period_time_min)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5164 #else
5165 int snd_pcm_hw_params_get_period_time_min(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5166 #endif
5167 {
5168         return snd_pcm_hw_param_get_min(params, SND_PCM_HW_PARAM_PERIOD_TIME, val, dir);
5169 }
5170
5171 /**
5172  * \brief Extract maximum period time from a configuration space
5173  * \param params Configuration space
5174  * \param val approximate maximum period duration in us
5175  * \param dir Sub unit direction
5176  * \return 0 otherwise a negative error code
5177  *
5178  * Exact value is <,=,> the returned one following dir (-1,0,1)
5179  */
5180 #ifndef DOXYGEN
5181 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_period_time_max)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5182 #else
5183 int snd_pcm_hw_params_get_period_time_max(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5184 #endif
5185 {
5186         return snd_pcm_hw_param_get_max(params, SND_PCM_HW_PARAM_PERIOD_TIME, val, dir);
5187 }
5188
5189 /**
5190  * \brief Verify if a period time is available inside a configuration space for a PCM
5191  * \param pcm PCM handle
5192  * \param params Configuration space
5193  * \param val approximate period duration in us
5194  * \param dir Sub unit direction
5195  * \return 0 if available a negative error code otherwise
5196  *
5197  * Wanted exact value is <,=,> val following dir (-1,0,1)
5198  */
5199 int snd_pcm_hw_params_test_period_time(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir)
5200 {
5201         return snd_pcm_hw_param_set(pcm, params, SND_TEST, SND_PCM_HW_PARAM_PERIOD_TIME, val, dir);
5202 }
5203
5204 /**
5205  * \brief Restrict a configuration space to contain only one period time
5206  * \param pcm PCM handle
5207  * \param params Configuration space
5208  * \param val approximate period duration in us
5209  * \param dir Sub unit direction
5210  * \return 0 otherwise a negative error code if configuration space would become empty
5211  *
5212  * Wanted exact value is <,=,> val following dir (-1,0,1)
5213  */
5214 int snd_pcm_hw_params_set_period_time(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir)
5215 {
5216         return snd_pcm_hw_param_set(pcm, params, SND_TRY, SND_PCM_HW_PARAM_PERIOD_TIME, val, dir);
5217 }
5218
5219
5220 /**
5221  * \brief Restrict a configuration space with a minimum period time
5222  * \param pcm PCM handle
5223  * \param params Configuration space
5224  * \param val approximate minimum period duration in us (on return filled with actual minimum)
5225  * \param dir Sub unit direction (on return filled with actual direction)
5226  * \return 0 otherwise a negative error code if configuration space would become empty
5227  *
5228  * Wanted/actual exact minimum is <,=,> val following dir (-1,0,1)
5229  */
5230 int snd_pcm_hw_params_set_period_time_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5231 {
5232         return snd_pcm_hw_param_set_min(pcm, params, SND_TRY, SND_PCM_HW_PARAM_PERIOD_TIME, val, dir);
5233 }
5234
5235 /**
5236  * \brief Restrict a configuration space with a maximum period time
5237  * \param pcm PCM handle
5238  * \param params Configuration space
5239  * \param val approximate maximum period duration in us (on return filled with actual maximum)
5240  * \param dir Sub unit direction (on return filled with actual direction)
5241  * \return 0 otherwise a negative error code if configuration space would become empty
5242  *
5243  * Wanted/actual exact maximum is <,=,> val following dir (-1,0,1)
5244  */
5245 int snd_pcm_hw_params_set_period_time_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5246 {
5247         return snd_pcm_hw_param_set_max(pcm, params, SND_TRY, SND_PCM_HW_PARAM_PERIOD_TIME, val, dir);
5248 }
5249
5250 /**
5251  * \brief Restrict a configuration space to have period times in a given range
5252  * \param pcm PCM handle
5253  * \param params Configuration space
5254  * \param min approximate minimum period duration in us (on return filled with actual minimum)
5255  * \param mindir Sub unit direction for minimum (on return filled with actual direction)
5256  * \param max approximate maximum period duration in us (on return filled with actual maximum)
5257  * \param maxdir Sub unit direction for maximum (on return filled with actual direction)
5258  * \return 0 otherwise a negative error code if configuration space would become empty
5259  *
5260  * Wanted/actual exact min/max is <,=,> val following dir (-1,0,1)
5261  */
5262 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)
5263 {
5264         return snd_pcm_hw_param_set_minmax(pcm, params, SND_TRY, SND_PCM_HW_PARAM_PERIOD_TIME, min, mindir, max, maxdir);
5265 }
5266
5267 /**
5268  * \brief Restrict a configuration space to have period time nearest to a target
5269  * \param pcm PCM handle
5270  * \param params Configuration space
5271  * \param val approximate target period duration in us / returned chosen approximate target period duration
5272  * \param dir Sub unit direction
5273  * \return 0 otherwise a negative error code if configuration space is empty
5274  *
5275  * target/chosen exact value is <,=,> val following dir (-1,0,1)
5276  */
5277 #ifndef DOXYGEN
5278 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)
5279 #else
5280 int snd_pcm_hw_params_set_period_time_near(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5281 #endif
5282 {
5283         return snd_pcm_hw_param_set_near(pcm, params, SND_PCM_HW_PARAM_PERIOD_TIME, val, dir);
5284 }
5285
5286 /**
5287  * \brief Restrict a configuration space to contain only its minimum period time
5288  * \param pcm PCM handle
5289  * \param params Configuration space
5290  * \param val Returned approximate period duration in us
5291  * \param dir Sub unit direction
5292  * \return 0 otherwise a negative error code
5293  *
5294  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
5295  */
5296 #ifndef DOXYGEN
5297 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)
5298 #else
5299 int snd_pcm_hw_params_set_period_time_first(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5300 #endif
5301 {
5302         return snd_pcm_hw_param_set_first(pcm, params, SND_PCM_HW_PARAM_PERIOD_TIME, val, dir);
5303 }
5304
5305 /**
5306  * \brief Restrict a configuration space to contain only its maximum period time
5307  * \param pcm PCM handle
5308  * \param params Configuration space
5309  * \param val Returned maximum approximate period time
5310  * \param dir Sub unit direction
5311  * \return approximate period duration in us
5312  */
5313 #ifndef DOXYGEN
5314 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)
5315 #else
5316 int snd_pcm_hw_params_set_period_time_last(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5317 #endif
5318 {
5319         return snd_pcm_hw_param_set_last(pcm, params, SND_PCM_HW_PARAM_PERIOD_TIME, val, dir);
5320 }
5321
5322
5323 /**
5324  * \brief Extract period size from a configuration space
5325  * \param params Configuration space
5326  * \param val Returned approximate period size in frames
5327  * \param dir Sub unit direction
5328  * \return 0 otherwise a negative error code if the configuration space does not contain a single value
5329  *
5330  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
5331  */
5332 #ifndef DOXYGEN
5333 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)
5334 #else
5335 int snd_pcm_hw_params_get_period_size(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir)
5336 #endif
5337 {
5338         unsigned int _val;
5339         int err = snd_pcm_hw_param_get(params, SND_PCM_HW_PARAM_PERIOD_SIZE, &_val, dir);
5340         if (err >= 0)
5341                 *val = _val;
5342         return err;
5343 }
5344
5345 /**
5346  * \brief Extract minimum period size from a configuration space
5347  * \param params Configuration space
5348  * \param val approximate minimum period size in frames
5349  * \param dir Sub unit direction
5350  * \return 0 otherwise a negative error code
5351  *
5352  * Exact value is <,=,> the returned one following dir (-1,0,1)
5353  */
5354 #ifndef DOXYGEN
5355 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)
5356 #else
5357 int snd_pcm_hw_params_get_period_size_min(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir)
5358 #endif
5359 {
5360         unsigned int _val = *val;
5361         int err = snd_pcm_hw_param_get_min(params, SND_PCM_HW_PARAM_PERIOD_SIZE, &_val, dir);
5362         if (err >= 0)
5363                 *val = _val;
5364         return err;
5365 }
5366
5367 /**
5368  * \brief Extract maximum period size from a configuration space
5369  * \param params Configuration space
5370  * \param val approximate minimum period size in frames
5371  * \param dir Sub unit direction
5372  * \return 0 otherwise a negative error code
5373  *
5374  * Exact value is <,=,> the returned one following dir (-1,0,1)
5375  */
5376 #ifndef DOXYGEN
5377 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)
5378 #else
5379 int snd_pcm_hw_params_get_period_size_max(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir)
5380 #endif
5381 {
5382         unsigned int _val = *val;
5383         int err = snd_pcm_hw_param_get_max(params, SND_PCM_HW_PARAM_PERIOD_SIZE, &_val, dir);
5384         if (err >= 0)
5385                 *val = _val;
5386         return err;
5387 }
5388
5389 /**
5390  * \brief Verify if a period size is available inside a configuration space for a PCM
5391  * \param pcm PCM handle
5392  * \param params Configuration space
5393  * \param val approximate period size in frames
5394  * \param dir Sub unit direction
5395  * \return 0 if available a negative error code otherwise
5396  *
5397  * Wanted exact value is <,=,> val following dir (-1,0,1)
5398  */
5399 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)
5400 {
5401         return snd_pcm_hw_param_set(pcm, params, SND_TEST, SND_PCM_HW_PARAM_PERIOD_SIZE, val, dir);
5402 }
5403
5404 /**
5405  * \brief Restrict a configuration space to contain only one period size
5406  * \param pcm PCM handle
5407  * \param params Configuration space
5408  * \param val approximate period size in frames
5409  * \param dir Sub unit direction
5410  * \return 0 otherwise a negative error code if configuration space would become empty
5411  *
5412  * Wanted exact value is <,=,> val following dir (-1,0,1)
5413  */
5414 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)
5415 {
5416         return snd_pcm_hw_param_set(pcm, params, SND_TRY, SND_PCM_HW_PARAM_PERIOD_SIZE, val, dir);
5417 }
5418
5419 /**
5420  * \brief Restrict a configuration space with a minimum period size
5421  * \param pcm PCM handle
5422  * \param params Configuration space
5423  * \param val approximate minimum period size in frames (on return filled with actual minimum)
5424  * \param dir Sub unit direction (on return filled with actual direction)
5425  * \return 0 otherwise a negative error code if configuration space would become empty
5426  *
5427  * Wanted/actual exact minimum is <,=,> val following dir (-1,0,1)
5428  */
5429 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)
5430 {
5431         unsigned int _val = *val;
5432         int err = snd_pcm_hw_param_set_min(pcm, params, SND_TRY, SND_PCM_HW_PARAM_PERIOD_SIZE, &_val, dir);
5433         if (err >= 0)
5434                 *val = _val;
5435         return err;
5436 }
5437
5438 /**
5439  * \brief Restrict a configuration space with a maximum period size
5440  * \param pcm PCM handle
5441  * \param params Configuration space
5442  * \param val approximate maximum period size in frames (on return filled with actual maximum)
5443  * \param dir Sub unit direction (on return filled with actual direction)
5444  * \return 0 otherwise a negative error code if configuration space would become empty
5445  *
5446  * Wanted/actual exact minimum is <,=,> val following dir (-1,0,1)
5447  */
5448 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)
5449 {
5450         unsigned int _val = *val;
5451         int err = snd_pcm_hw_param_set_max(pcm, params, SND_TRY, SND_PCM_HW_PARAM_PERIOD_SIZE, &_val, dir);
5452         if (err >= 0)
5453                 *val = _val;
5454         return err;
5455 }
5456
5457 /**
5458  * \brief Restrict a configuration space to have period sizes in a given range
5459  * \param pcm PCM handle
5460  * \param params Configuration space
5461  * \param min approximate minimum period size in frames (on return filled with actual minimum)
5462  * \param mindir Sub unit direction for minimum (on return filled with actual direction)
5463  * \param max approximate maximum period size in frames (on return filled with actual maximum)
5464  * \param maxdir Sub unit direction for maximum (on return filled with actual direction)
5465  * \return 0 otherwise a negative error code if configuration space would become empty
5466  *
5467  * Wanted/actual exact min/max is <,=,> val following dir (-1,0,1)
5468  */
5469 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)
5470 {
5471         unsigned int _min = *min;
5472         unsigned int _max = *max;
5473         int err = snd_pcm_hw_param_set_minmax(pcm, params, SND_TRY, SND_PCM_HW_PARAM_PERIOD_SIZE, &_min, mindir, &_max, maxdir);
5474         *min = _min;
5475         *max = _max;
5476         return err;
5477 }
5478
5479 /**
5480  * \brief Restrict a configuration space to have period size nearest to a target
5481  * \param pcm PCM handle
5482  * \param params Configuration space
5483  * \param val approximate target period size in frames / returned chosen approximate target period size
5484  * \param dir Sub unit direction
5485  * \return 0 otherwise a negative error code if configuration space is empty
5486  *
5487  * target/chosen exact value is <,=,> val following dir (-1,0,1)
5488  */
5489 #ifndef DOXYGEN
5490 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)
5491 #else
5492 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)
5493 #endif
5494 {
5495         unsigned int _val = *val;
5496         int err = snd_pcm_hw_param_set_near(pcm, params, SND_PCM_HW_PARAM_PERIOD_SIZE, &_val, dir);
5497         if (err >= 0)
5498                 *val = _val;
5499         return err;
5500 }
5501
5502 /**
5503  * \brief Restrict a configuration space to contain only its minimum period size
5504  * \param pcm PCM handle
5505  * \param params Configuration space
5506  * \param val Returned maximum approximate period size in frames
5507  * \param dir Sub unit direction
5508  * \return 0 otherwise a negative error code
5509  *
5510  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
5511  */
5512 #ifndef DOXYGEN
5513 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)
5514 #else
5515 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)
5516 #endif
5517 {
5518         unsigned int _val;
5519         int err = snd_pcm_hw_param_set_first(pcm, params, SND_PCM_HW_PARAM_PERIOD_SIZE, &_val, dir);
5520         if (err >= 0)
5521                 *val = _val;
5522         return err;
5523 }
5524
5525 /**
5526  * \brief Restrict a configuration space to contain only its maximum period size
5527  * \param pcm PCM handle
5528  * \param params Configuration space
5529  * \param val Returned maximum approximate period size in frames
5530  * \param dir Sub unit direction
5531  * \return 0 otherwise a negative error code
5532  *
5533  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
5534  */
5535 #ifndef DOXYGEN
5536 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)
5537 #else
5538 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)
5539 #endif
5540 {
5541         unsigned int _val;
5542         int err = snd_pcm_hw_param_set_last(pcm, params, SND_PCM_HW_PARAM_PERIOD_SIZE, &_val, dir);
5543         if (err >= 0)
5544                 *val = _val;
5545         return err;
5546 }
5547
5548 /**
5549  * \brief Restrict a configuration space to contain only integer period sizes
5550  * \param pcm PCM handle
5551  * \param params Configuration space
5552  * \return 0 otherwise a negative error code if configuration space would become empty
5553  */
5554 int snd_pcm_hw_params_set_period_size_integer(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
5555 {
5556         return snd_pcm_hw_param_set_integer(pcm, params, SND_TRY, SND_PCM_HW_PARAM_PERIOD_SIZE);
5557 }
5558
5559
5560 /**
5561  * \brief Extract periods from a configuration space
5562  * \param params Configuration space
5563  * \param val approximate periods per buffer
5564  * \param dir Sub unit direction
5565  * \return 0 otherwise a negative error code if the configuration space does not contain a single value
5566  *
5567  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
5568  */
5569 #ifndef DOXYGEN
5570 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_periods)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5571 #else
5572 int snd_pcm_hw_params_get_periods(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5573 #endif
5574 {
5575         return snd_pcm_hw_param_get(params, SND_PCM_HW_PARAM_PERIODS, val, dir);
5576 }
5577
5578 /**
5579  * \brief Extract minimum periods count from a configuration space
5580  * \param params Configuration space
5581  * \param val approximate minimum periods per buffer
5582  * \param dir Sub unit direction
5583  * \return 0 otherwise a negative error code
5584  *
5585  * Exact value is <,=,> the returned one following dir (-1,0,1)
5586  */
5587 #ifndef DOXYGEN
5588 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_periods_min)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5589 #else
5590 int snd_pcm_hw_params_get_periods_min(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5591 #endif
5592 {
5593         return snd_pcm_hw_param_get_min(params, SND_PCM_HW_PARAM_PERIODS, val, dir);
5594 }
5595
5596 /**
5597  * \brief Extract maximum periods count from a configuration space
5598  * \param params Configuration space
5599  * \param val approximate maximum periods per buffer
5600  * \param dir Sub unit direction
5601  * \return 0 otherwise a negative error code
5602  *
5603  * Exact value is <,=,> the returned one following dir (-1,0,1)
5604  */
5605 #ifndef DOXYGEN
5606 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_periods_max)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5607 #else
5608 int snd_pcm_hw_params_get_periods_max(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5609 #endif
5610 {
5611         return snd_pcm_hw_param_get_max(params, SND_PCM_HW_PARAM_PERIODS, val, dir);
5612 }
5613
5614 /**
5615  * \brief Verify if a periods count is available inside a configuration space for a PCM
5616  * \param pcm PCM handle
5617  * \param params Configuration space
5618  * \param val approximate periods per buffer
5619  * \param dir Sub unit direction
5620  * \return 0 if available a negative error code otherwise
5621  *
5622  * Wanted exact value is <,=,> val following dir (-1,0,1)
5623  */
5624 int snd_pcm_hw_params_test_periods(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir)
5625 {
5626         return snd_pcm_hw_param_set(pcm, params, SND_TEST, SND_PCM_HW_PARAM_PERIODS, val, dir);
5627 }
5628
5629 /**
5630  * \brief Restrict a configuration space to contain only one periods count
5631  * \param pcm PCM handle
5632  * \param params Configuration space
5633  * \param val approximate periods per buffer
5634  * \param dir Sub unit direction
5635  * \return 0 otherwise a negative error code if configuration space would become empty
5636  *
5637  * Wanted exact value is <,=,> val following dir (-1,0,1)
5638  */
5639 int snd_pcm_hw_params_set_periods(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir)
5640 {
5641         return snd_pcm_hw_param_set(pcm, params, SND_TRY, SND_PCM_HW_PARAM_PERIODS, val, dir);
5642 }
5643
5644 /**
5645  * \brief Restrict a configuration space with a minimum periods count
5646  * \param pcm PCM handle
5647  * \param params Configuration space
5648  * \param val approximate minimum periods per buffer (on return filled with actual minimum)
5649  * \param dir Sub unit direction (on return filled with actual direction)
5650  * \return 0 otherwise a negative error code if configuration space would become empty
5651  *
5652  * Wanted/actual exact minimum is <,=,> val following dir (-1,0,1)
5653  */
5654 int snd_pcm_hw_params_set_periods_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5655 {
5656         return snd_pcm_hw_param_set_min(pcm, params, SND_TRY, SND_PCM_HW_PARAM_PERIODS, val, dir);
5657 }
5658
5659 /**
5660  * \brief Restrict a configuration space with a maximum periods count
5661  * \param pcm PCM handle
5662  * \param params Configuration space
5663  * \param val approximate maximum periods per buffer (on return filled with actual maximum)
5664  * \param dir Sub unit direction (on return filled with actual direction)
5665  * \return 0 otherwise a negative error code if configuration space would become empty
5666  *
5667  * Wanted/actual exact maximum is <,=,> val following dir (-1,0,1)
5668  */
5669 int snd_pcm_hw_params_set_periods_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5670 {
5671         return snd_pcm_hw_param_set_max(pcm, params, SND_TRY, SND_PCM_HW_PARAM_PERIODS, val, dir);
5672 }
5673
5674 /**
5675  * \brief Restrict a configuration space to have periods counts in a given range
5676  * \param pcm PCM handle
5677  * \param params Configuration space
5678  * \param min approximate minimum periods per buffer (on return filled with actual minimum)
5679  * \param mindir Sub unit direction for minimum (on return filled with actual direction)
5680  * \param max approximate maximum periods per buffer (on return filled with actual maximum)
5681  * \param maxdir Sub unit direction for maximum (on return filled with actual direction)
5682  * \return 0 otherwise a negative error code if configuration space would become empty
5683  *
5684  * Wanted/actual exact min/max is <,=,> val following dir (-1,0,1)
5685  */
5686 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)
5687 {
5688         return snd_pcm_hw_param_set_minmax(pcm, params, SND_TRY, SND_PCM_HW_PARAM_PERIODS, min, mindir, max, maxdir);
5689 }
5690
5691 /**
5692  * \brief Restrict a configuration space to have periods count nearest to a target
5693  * \param pcm PCM handle
5694  * \param params Configuration space
5695  * \param val approximate target periods per buffer / returned chosen approximate target periods per buffer
5696  * \param dir Sub unit direction
5697  * \return 0 otherwise a negative error code if configuration space is empty
5698  *
5699  * target/chosen exact value is <,=,> val following dir (-1,0,1)
5700  */
5701 #ifndef DOXYGEN
5702 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)
5703 #else
5704 int snd_pcm_hw_params_set_periods_near(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5705 #endif
5706 {
5707         return snd_pcm_hw_param_set_near(pcm, params, SND_PCM_HW_PARAM_PERIODS, val, dir);
5708 }
5709
5710 /**
5711  * \brief Restrict a configuration space to contain only its minimum periods count
5712  * \param pcm PCM handle
5713  * \param params Configuration space
5714  * \param val Returned approximate minimum periods per buffer
5715  * \param dir Sub unit direction
5716  * \return 0 otherwise a negative error code
5717  *
5718  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
5719  */
5720 #ifndef DOXYGEN
5721 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)
5722 #else
5723 int snd_pcm_hw_params_set_periods_first(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5724 #endif
5725 {
5726         return snd_pcm_hw_param_set_first(pcm, params, SND_PCM_HW_PARAM_PERIODS, val, dir);
5727 }
5728
5729 /**
5730  * \brief Restrict a configuration space to contain only its maximum periods count
5731  * \param pcm PCM handle
5732  * \param params Configuration space
5733  * \param val Returned approximate maximum periods per buffer
5734  * \param dir Sub unit direction
5735  * \return 0 otherwise a negative error code
5736  *
5737  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
5738  */
5739 #ifndef DOXYGEN
5740 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)
5741 #else
5742 int snd_pcm_hw_params_set_periods_last(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5743 #endif
5744 {
5745         return snd_pcm_hw_param_set_last(pcm, params, SND_PCM_HW_PARAM_PERIODS, val, dir);
5746 }
5747
5748 /**
5749  * \brief Restrict a configuration space to contain only integer periods counts
5750  * \param pcm PCM handle
5751  * \param params Configuration space
5752  * \return 0 otherwise a negative error code if configuration space would become empty
5753  */
5754 int snd_pcm_hw_params_set_periods_integer(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
5755 {
5756         return snd_pcm_hw_param_set_integer(pcm, params, SND_TRY, SND_PCM_HW_PARAM_PERIODS);
5757 }
5758
5759
5760 /**
5761  * \brief Extract buffer time from a configuration space
5762  * \param params Configuration space
5763  * \param val Returned buffer time in us
5764  * \param dir Sub unit direction
5765  * \return 0 otherwise a negative error code if the configuration space does not contain a single value
5766  *
5767  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
5768  */
5769 #ifndef DOXYGEN
5770 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_buffer_time)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5771 #else
5772 int snd_pcm_hw_params_get_buffer_time(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5773 #endif
5774 {
5775         return snd_pcm_hw_param_get(params, SND_PCM_HW_PARAM_BUFFER_TIME, val, dir);
5776 }
5777
5778 /**
5779  * \brief Extract minimum buffer time from a configuration space
5780  * \param params Configuration space
5781  * \param val approximate minimum buffer duration in us
5782  * \param dir Sub unit direction
5783  * \return 0 otherwise a negative error code
5784  *
5785  * Exact value is <,=,> the returned one following dir (-1,0,1)
5786  */
5787 #ifndef DOXYGEN
5788 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_buffer_time_min)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5789 #else
5790 int snd_pcm_hw_params_get_buffer_time_min(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5791 #endif
5792 {
5793         return snd_pcm_hw_param_get_min(params, SND_PCM_HW_PARAM_BUFFER_TIME, val, dir);
5794 }
5795
5796 /**
5797  * \brief Extract maximum buffer time from a configuration space
5798  * \param params Configuration space
5799  * \param val approximate maximum buffer duration in us
5800  * \param dir Sub unit direction
5801  * \return 0 otherwise a negative error code
5802  *
5803  * Exact value is <,=,> the returned one following dir (-1,0,1)
5804  */
5805 #ifndef DOXYGEN
5806 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_buffer_time_max)(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5807 #else
5808 int snd_pcm_hw_params_get_buffer_time_max(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5809 #endif
5810 {
5811         return snd_pcm_hw_param_get_max(params, SND_PCM_HW_PARAM_BUFFER_TIME, val, dir);
5812 }
5813
5814 /**
5815  * \brief Verify if a buffer time is available inside a configuration space for a PCM
5816  * \param pcm PCM handle
5817  * \param params Configuration space
5818  * \param val approximate buffer duration in us
5819  * \param dir Sub unit direction
5820  * \return 0 if available a negative error code otherwise
5821  *
5822  * Wanted exact value is <,=,> val following dir (-1,0,1)
5823  */
5824 int snd_pcm_hw_params_test_buffer_time(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir)
5825 {
5826         return snd_pcm_hw_param_set(pcm, params, SND_TEST, SND_PCM_HW_PARAM_BUFFER_TIME, val, dir);
5827 }
5828
5829 /**
5830  * \brief Restrict a configuration space to contain only one buffer time
5831  * \param pcm PCM handle
5832  * \param params Configuration space
5833  * \param val approximate buffer duration in us
5834  * \param dir Sub unit direction
5835  * \return 0 otherwise a negative error code if configuration space would become empty
5836  *
5837  * Wanted exact value is <,=,> val following dir (-1,0,1)
5838  */
5839 int snd_pcm_hw_params_set_buffer_time(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir)
5840 {
5841         return snd_pcm_hw_param_set(pcm, params, SND_TRY, SND_PCM_HW_PARAM_BUFFER_TIME, val, dir);
5842 }
5843
5844 /**
5845  * \brief Restrict a configuration space with a minimum buffer time
5846  * \param pcm PCM handle
5847  * \param params Configuration space
5848  * \param val approximate minimum buffer duration in us (on return filled with actual minimum)
5849  * \param dir Sub unit direction (on return filled with actual direction)
5850  * \return 0 otherwise a negative error code if configuration space would become empty
5851  *
5852  * Wanted/actual exact minimum is <,=,> val following dir (-1,0,1)
5853  */
5854 int snd_pcm_hw_params_set_buffer_time_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5855 {
5856         return snd_pcm_hw_param_set_min(pcm, params, SND_TRY, SND_PCM_HW_PARAM_BUFFER_TIME, val, dir);
5857 }
5858
5859 /**
5860  * \brief Restrict a configuration space with a maximum buffer time
5861  * \param pcm PCM handle
5862  * \param params Configuration space
5863  * \param val approximate maximum buffer duration in us (on return filled with actual maximum)
5864  * \param dir Sub unit direction (on return filled with actual direction)
5865  * \return 0 otherwise a negative error code if configuration space would become empty
5866  *
5867  * Wanted/actual exact maximum is <,=,> val following dir (-1,0,1)
5868  */
5869 int snd_pcm_hw_params_set_buffer_time_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5870 {
5871         return snd_pcm_hw_param_set_max(pcm, params, SND_TRY, SND_PCM_HW_PARAM_BUFFER_TIME, val, dir);
5872 }
5873
5874 /**
5875  * \brief Restrict a configuration space to have buffer times in a given range
5876  * \param pcm PCM handle
5877  * \param params Configuration space
5878  * \param min approximate minimum buffer duration in us (on return filled with actual minimum)
5879  * \param mindir Sub unit direction for minimum (on return filled with actual direction)
5880  * \param max approximate maximum buffer duration in us (on return filled with actual maximum)
5881  * \param maxdir Sub unit direction for maximum (on return filled with actual direction)
5882  * \return 0 otherwise a negative error code if configuration space would become empty
5883  *
5884  * Wanted/actual exact min/max is <,=,> val following dir (-1,0,1)
5885  */
5886 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)
5887 {
5888         return snd_pcm_hw_param_set_minmax(pcm, params, SND_TRY, SND_PCM_HW_PARAM_BUFFER_TIME, min, mindir, max, maxdir);
5889 }
5890
5891 /**
5892  * \brief Restrict a configuration space to have buffer time nearest to a target
5893  * \param pcm PCM handle
5894  * \param params Configuration space
5895  * \param val approximate target buffer duration in us / returned chosen approximate target buffer duration
5896  * \param dir Sub unit direction
5897  * \return 0 otherwise a negative error code if configuration space is empty
5898  *
5899  * target/chosen exact value is <,=,> val following dir (-1,0,1)
5900  */
5901 #ifndef DOXYGEN
5902 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)
5903 #else
5904 int snd_pcm_hw_params_set_buffer_time_near(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5905 #endif
5906 {
5907         return snd_pcm_hw_param_set_near(pcm, params, SND_PCM_HW_PARAM_BUFFER_TIME, val, dir);
5908 }
5909
5910 /**
5911  * \brief Restrict a configuration space to contain only its minimum buffer time
5912  * \param pcm PCM handle
5913  * \param params Configuration space
5914  * \param val Returned approximate minimum buffer duration in us
5915  * \param dir Sub unit direction
5916  * \return 0 otherwise a negative error code
5917  *
5918  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
5919  */
5920 #ifndef DOXYGEN
5921 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)
5922 #else
5923 int snd_pcm_hw_params_set_buffer_time_first(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5924 #endif
5925 {
5926         return snd_pcm_hw_param_set_first(pcm, params, SND_PCM_HW_PARAM_BUFFER_TIME, val, dir);
5927 }
5928
5929 /**
5930  * \brief Restrict a configuration space to contain only its maximum buffered time
5931  * \param pcm PCM handle
5932  * \param params Configuration space
5933  * \param val Returned approximate maximum buffer duration in us
5934  * \param dir Sub unit direction
5935  * \return 0 otherwise a negative error code
5936  *
5937  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
5938  */
5939 #ifndef DOXYGEN
5940 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)
5941 #else
5942 int snd_pcm_hw_params_set_buffer_time_last(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
5943 #endif
5944 {
5945         return snd_pcm_hw_param_set_last(pcm, params, SND_PCM_HW_PARAM_BUFFER_TIME, val, dir);
5946 }
5947
5948
5949 /**
5950  * \brief Extract buffer size from a configuration space
5951  * \param params Configuration space
5952  * \param val Returned buffer size in frames
5953  * \return 0 otherwise a negative error code if the configuration space does not contain a single value
5954  */
5955 #ifndef DOXYGEN
5956 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_buffer_size)(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)
5957 #else
5958 int snd_pcm_hw_params_get_buffer_size(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)
5959 #endif
5960 {
5961         unsigned int _val;
5962         int err = snd_pcm_hw_param_get(params, SND_PCM_HW_PARAM_BUFFER_SIZE, &_val, NULL);
5963         if (err >= 0)
5964                 *val = _val;
5965         return err;
5966 }
5967
5968 /**
5969  * \brief Extract minimum buffer size from a configuration space
5970  * \param params Configuration space
5971  * \param val Returned approximate minimum buffer size in frames
5972  * \return 0 otherwise a negative error code
5973  */
5974 #ifndef DOXYGEN
5975 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_buffer_size_min)(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)
5976 #else
5977 int snd_pcm_hw_params_get_buffer_size_min(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)
5978 #endif
5979 {
5980         unsigned int _val;
5981         int err = snd_pcm_hw_param_get_min(params, SND_PCM_HW_PARAM_BUFFER_SIZE, &_val, NULL);
5982         if (err >= 0)
5983                 *val = _val;
5984         return err;
5985 }
5986
5987 /**
5988  * \brief Extract maximum buffer size from a configuration space
5989  * \param params Configuration space
5990  * \param val Returned approximate maximum buffer size in frames
5991  * \return 0 otherwise a negative error code
5992  *
5993  * Exact value is <,=,> the returned one following dir (-1,0,1)
5994  */
5995 #ifndef DOXYGEN
5996 EXPORT_SYMBOL int INTERNAL(snd_pcm_hw_params_get_buffer_size_max)(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)
5997 #else
5998 int snd_pcm_hw_params_get_buffer_size_max(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)
5999 #endif
6000 {
6001         unsigned int _val;
6002         int err = snd_pcm_hw_param_get_max(params, SND_PCM_HW_PARAM_BUFFER_SIZE, &_val, NULL);
6003         if (err >= 0)
6004                 *val = _val;
6005         return err;
6006 }
6007
6008 /**
6009  * \brief Verify if a buffer size is available inside a configuration space for a PCM
6010  * \param pcm PCM handle
6011  * \param params Configuration space
6012  * \param val buffer size in frames
6013  * \return 0 if available a negative error code otherwise
6014  *
6015  * Wanted exact value is <,=,> val following dir (-1,0,1)
6016  */
6017 int snd_pcm_hw_params_test_buffer_size(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t val)
6018 {
6019         return snd_pcm_hw_param_set(pcm, params, SND_TEST, SND_PCM_HW_PARAM_BUFFER_SIZE, val, 0);
6020 }
6021
6022 /**
6023  * \brief Restrict a configuration space to contain only one buffer size
6024  * \param pcm PCM handle
6025  * \param params Configuration space
6026  * \param val buffer size in frames
6027  * \return 0 otherwise a negative error code if configuration space would become empty
6028  *
6029  * Wanted exact value is <,=,> val following dir (-1,0,1)
6030  */
6031 int snd_pcm_hw_params_set_buffer_size(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t val)
6032 {
6033         return snd_pcm_hw_param_set(pcm, params, SND_TRY, SND_PCM_HW_PARAM_BUFFER_SIZE, val, 0);
6034 }
6035
6036 /**
6037  * \brief Restrict a configuration space with a minimum buffer size
6038  * \param pcm PCM handle
6039  * \param params Configuration space
6040  * \param val approximate minimum buffer size in frames (on return filled with actual minimum)
6041  * \return 0 otherwise a negative error code if configuration space would become empty
6042  */
6043 int snd_pcm_hw_params_set_buffer_size_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)
6044 {
6045         unsigned int _val = *val;
6046         int err = snd_pcm_hw_param_set_min(pcm, params, SND_TRY, SND_PCM_HW_PARAM_BUFFER_SIZE, &_val, NULL);
6047         if (err >= 0)
6048                 *val = _val;
6049         return err;
6050 }
6051
6052 /**
6053  * \brief Restrict a configuration space with a maximum buffer size
6054  * \param pcm PCM handle
6055  * \param params Configuration space
6056  * \param val approximate maximum buffer size in frames (on return filled with actual maximum)
6057  * \return 0 otherwise a negative error code if configuration space would become empty
6058  */
6059 int snd_pcm_hw_params_set_buffer_size_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)
6060 {
6061         unsigned int _val = *val;
6062         int err = snd_pcm_hw_param_set_max(pcm, params, SND_TRY, SND_PCM_HW_PARAM_BUFFER_SIZE, &_val, NULL);
6063         if (err >= 0)
6064                 *val = _val;
6065         return err;
6066 }
6067
6068 /**
6069  * \brief Restrict a configuration space to have buffer sizes in a given range
6070  * \param pcm PCM handle
6071  * \param params Configuration space
6072  * \param min approximate minimum buffer size in frames (on return filled with actual minimum)
6073  * \param max approximate maximum buffer size in frames (on return filled with actual maximum)
6074  * \return 0 otherwise a negative error code if configuration space would become empty
6075  */
6076 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)
6077 {
6078         unsigned int _min = *min;
6079         unsigned int _max = *max;
6080         int err = snd_pcm_hw_param_set_minmax(pcm, params, SND_TRY, SND_PCM_HW_PARAM_BUFFER_SIZE, &_min, NULL, &_max, NULL);
6081         *min = _min;
6082         *max = _max;
6083         return err;
6084 }
6085
6086 /**
6087  * \brief Restrict a configuration space to have buffer size nearest to a target
6088  * \param pcm PCM handle
6089  * \param params Configuration space
6090  * \param val approximate target buffer size in frames / returned chosen approximate target buffer size in frames
6091  * \return 0 otherwise a negative error code if configuration space is empty
6092  */
6093 #ifndef DOXYGEN
6094 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)
6095 #else
6096 int snd_pcm_hw_params_set_buffer_size_near(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)
6097 #endif
6098 {
6099         unsigned int _val = *val;
6100         int err = snd_pcm_hw_param_set_near(pcm, params, SND_PCM_HW_PARAM_BUFFER_SIZE, &_val, NULL);
6101         if (err >= 0)
6102                 *val = _val;
6103         return err;
6104 }
6105
6106 /**
6107  * \brief Restrict a configuration space to contain only its minimum buffer size
6108  * \param pcm PCM handle
6109  * \param params Configuration space
6110  * \param val Returned minimum buffer size in frames
6111  * \return buffer size in frames
6112  */
6113 #ifndef DOXYGEN
6114 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)
6115 #else
6116 int snd_pcm_hw_params_set_buffer_size_first(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)
6117 #endif
6118 {
6119         unsigned int _val;
6120         int err = snd_pcm_hw_param_set_first(pcm, params, SND_PCM_HW_PARAM_BUFFER_SIZE, &_val, NULL);
6121         if (err >= 0)
6122                 *val = _val;
6123         return err;
6124 }
6125
6126 /**
6127  * \brief Restrict a configuration space to contain only its maximum buffer size
6128  * \param pcm PCM handle
6129  * \param params Configuration space
6130  * \param val Returned maximum buffer size in frames
6131  * \return 0 otherwise a negative error code
6132  */
6133 #ifndef DOXYGEN
6134 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)
6135 #else
6136 int snd_pcm_hw_params_set_buffer_size_last(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)
6137 #endif
6138 {
6139         unsigned int _val;
6140         int err = snd_pcm_hw_param_set_last(pcm, params, SND_PCM_HW_PARAM_BUFFER_SIZE, &_val, NULL);
6141         if (err >= 0)
6142                 *val = _val;
6143         return err;
6144 }
6145
6146
6147 /**
6148  * \brief (DEPRECATED) Extract tick time from a configuration space
6149  * \param params Configuration space
6150  * \param val Returned approximate tick duration in us
6151  * \param dir Sub unit direction
6152  * \return 0 otherwise a negative error code if the configuration space does not contain a single value
6153  *
6154  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
6155  */
6156 #ifndef DOXYGEN
6157 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)
6158 #else
6159 int snd_pcm_hw_params_get_tick_time(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
6160 #endif
6161 {
6162         *val = 0;
6163         return 0;
6164 }
6165
6166 /**
6167  * \brief (DEPRECATED) Extract minimum tick time from a configuration space
6168  * \param params Configuration space
6169  * \param val Returned approximate minimum tick duration in us
6170  * \param dir Sub unit direction
6171  * \return 0 otherwise a negative error code
6172  *
6173  * Exact value is <,=,> the returned one following dir (-1,0,1)
6174  */
6175 #ifndef DOXYGEN
6176 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)
6177 #else
6178 int snd_pcm_hw_params_get_tick_time_min(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
6179 #endif
6180 {
6181         *val = 0;
6182         return 0;
6183 }
6184
6185 /**
6186  * \brief (DEPRECATED) Extract maximum tick time from a configuration space
6187  * \param params Configuration space
6188  * \param val Returned approximate maximum tick duration in us
6189  * \param dir Sub unit direction
6190  * \return 0 otherwise a negative error code
6191  *
6192  * Exact value is <,=,> the returned one following dir (-1,0,1)
6193  */
6194 #ifndef DOXYGEN
6195 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)
6196 #else
6197 int snd_pcm_hw_params_get_tick_time_max(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
6198 #endif
6199 {
6200         *val = 0;
6201         return 0;
6202 }
6203
6204 /**
6205  * \brief (DEPRECATED) Verify if a tick time is available inside a configuration space for a PCM
6206  * \param pcm PCM handle
6207  * \param params Configuration space
6208  * \param val approximate tick duration in us
6209  * \param dir Sub unit direction
6210  * \return 0 if available a negative error code otherwise
6211  *
6212  * Wanted exact value is <,=,> val following dir (-1,0,1)
6213  */
6214 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)
6215 {
6216         return val ? -EINVAL : 0;
6217 }
6218
6219 /**
6220  * \brief (DEPRECATED) Restrict a configuration space to contain only one tick time
6221  * \param pcm PCM handle
6222  * \param params Configuration space
6223  * \param val approximate tick duration in us
6224  * \param dir Sub unit direction
6225  * \return 0 otherwise a negative error code if configuration space would become empty
6226  *
6227  * Wanted exact value is <,=,> val following dir (-1,0,1)
6228  */
6229 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)
6230 {
6231         return 0;
6232 }
6233
6234 /**
6235  * \brief (DEPRECATED) Restrict a configuration space with a minimum tick time
6236  * \param pcm PCM handle
6237  * \param params Configuration space
6238  * \param val approximate minimum tick duration in us (on return filled with actual minimum)
6239  * \param dir Sub unit direction (on return filled with actual direction)
6240  * \return 0 otherwise a negative error code if configuration space would become empty
6241  *
6242  * Wanted/actual exact minimum is <,=,> val following dir (-1,0,1)
6243  */
6244 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)
6245 {
6246         return 0;
6247 }
6248
6249 /**
6250  * \brief (DEPRECATED) Restrict a configuration space with a maximum tick time
6251  * \param pcm PCM handle
6252  * \param params Configuration space
6253  * \param val approximate maximum tick duration in us (on return filled with actual maximum)
6254  * \param dir Sub unit direction (on return filled with actual direction)
6255  * \return 0 otherwise a negative error code if configuration space would become empty
6256  *
6257  * Wanted/actual exact maximum is <,=,> val following dir (-1,0,1)
6258  */
6259 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)
6260 {
6261         return 0;
6262 }
6263
6264 /**
6265  * \brief (DEPRECATED) Restrict a configuration space to have tick times in a given range
6266  * \param pcm PCM handle
6267  * \param params Configuration space
6268  * \param min approximate minimum tick duration in us (on return filled with actual minimum)
6269  * \param mindir Sub unit direction for minimum (on return filled with actual direction)
6270  * \param max approximate maximum tick duration in us (on return filled with actual maximum)
6271  * \param maxdir Sub unit direction for maximum (on return filled with actual direction)
6272  * \return 0 otherwise a negative error code if configuration space would become empty
6273  *
6274  * Wanted/actual exact min/max is <,=,> val following dir (-1,0,1)
6275  */
6276 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)
6277 {
6278         return 0;
6279 }
6280
6281 /**
6282  * \brief (DEPRECATED) Restrict a configuration space to have tick time nearest to a target
6283  * \param pcm PCM handle
6284  * \param params Configuration space
6285  * \param val approximate target tick duration in us / returned chosen approximate target tick duration in us
6286  * \param dir Sub unit direction
6287  * \return 0 otherwise a negative error code if configuration space is empty
6288  *
6289  * target/chosen exact value is <,=,> val following dir (-1,0,1)
6290  */
6291 #ifndef DOXYGEN
6292 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)
6293 #else
6294 int snd_pcm_hw_params_set_tick_time_near(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
6295 #endif
6296 {
6297         return 0;
6298 }
6299
6300 /**
6301  * \brief (DEPRECATED) Restrict a configuration space to contain only its minimum tick time
6302  * \param pcm PCM handle
6303  * \param params Configuration space
6304  * \param val Returned approximate minimum tick duration in us
6305  * \param dir Sub unit direction
6306  * \return 0 otherwise a negative error code
6307  *
6308  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
6309  */
6310 #ifndef DOXYGEN
6311 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)
6312 #else
6313 int snd_pcm_hw_params_set_tick_time_first(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
6314 #endif
6315 {
6316         return 0;
6317 }
6318
6319 /**
6320  * \brief (DEPRECATED) Restrict a configuration space to contain only its maximum tick time
6321  * \param pcm PCM handle
6322  * \param params Configuration space
6323  * \param val Returned approximate maximum tick duration in us
6324  * \param dir Sub unit direction
6325  * \return 0 otherwise a negative error code
6326  *
6327  * Actual exact value is <,=,> the approximate one following dir (-1, 0, 1)
6328  */
6329 #ifndef DOXYGEN
6330 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)
6331 #else
6332 int snd_pcm_hw_params_set_tick_time_last(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir)
6333 #endif
6334 {
6335         return 0;
6336 }
6337
6338 /**
6339  * \brief Get the minimum transfer align value in samples
6340  * \param params Configuration space
6341  * \param val Returned minimum align value
6342  * \return 0 otherwise a negative error code if the configuration space does not contain a single value
6343  */
6344 int snd_pcm_hw_params_get_min_align(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)
6345 {
6346         unsigned int format, channels, fb, min_align;
6347         int err;
6348
6349         err = snd_pcm_hw_param_get(params, SND_PCM_HW_PARAM_FORMAT, &format, NULL);
6350         if (err < 0)
6351                 return err;
6352         err = snd_pcm_hw_param_get(params, SND_PCM_HW_PARAM_CHANNELS, &channels, NULL);
6353         if (err < 0)
6354                 return err;
6355         // compute frame bits
6356         fb = snd_pcm_format_physical_width((snd_pcm_format_t)format) * channels;
6357         min_align = 1;
6358         while (fb % 8) {
6359                 fb *= 2;
6360                 min_align *= 2;
6361         }
6362         if (val)
6363                 *val = min_align;
6364         return 0;
6365 }
6366
6367 #ifndef DOXYGEN
6368 void snd_pcm_sw_params_current_no_lock(snd_pcm_t *pcm, snd_pcm_sw_params_t *params)
6369 {
6370         params->proto = SNDRV_PCM_VERSION;
6371         params->tstamp_mode = pcm->tstamp_mode;
6372         params->tstamp_type = pcm->tstamp_type;
6373         params->period_step = pcm->period_step;
6374         params->sleep_min = 0;
6375         params->avail_min = pcm->avail_min;
6376         sw_set_period_event(params, pcm->period_event);
6377         params->xfer_align = 1;
6378         params->start_threshold = pcm->start_threshold;
6379         params->stop_threshold = pcm->stop_threshold;
6380         params->silence_threshold = pcm->silence_threshold;
6381         params->silence_size = pcm->silence_size;
6382         params->boundary = pcm->boundary;
6383 }
6384 #endif
6385
6386 /**
6387  * \brief Return current software configuration for a PCM
6388  * \param pcm PCM handle
6389  * \param params Software configuration container
6390  * \return 0 on success otherwise a negative error code
6391  *
6392  * The function is thread-safe when built with the proper option.
6393  */
6394 int snd_pcm_sw_params_current(snd_pcm_t *pcm, snd_pcm_sw_params_t *params)
6395 {
6396         assert(pcm && params);
6397         if (CHECK_SANITY(! pcm->setup)) {
6398                 SNDMSG("PCM not set up");
6399                 return -EIO;
6400         }
6401         __snd_pcm_lock(pcm); /* forced lock due to pcm field changes */
6402         snd_pcm_sw_params_current_no_lock(pcm, params);
6403         __snd_pcm_unlock(pcm);
6404         return 0;
6405 }
6406
6407 /**
6408  * \brief Dump a software configuration
6409  * \param params Software configuration container
6410  * \param out Output handle
6411  * \return 0 on success otherwise a negative error code
6412  */
6413 int snd_pcm_sw_params_dump(snd_pcm_sw_params_t *params, snd_output_t *out)
6414 {
6415         snd_output_printf(out, "tstamp_mode: %s\n", snd_pcm_tstamp_mode_name(params->tstamp_mode));
6416         snd_output_printf(out, "tstamp_type: %s\n", snd_pcm_tstamp_type_name(params->tstamp_type));
6417         snd_output_printf(out, "period_step: %u\n", params->period_step);
6418         snd_output_printf(out, "avail_min: %lu\n", params->avail_min);
6419         snd_output_printf(out, "start_threshold: %ld\n", params->start_threshold);
6420         snd_output_printf(out, "stop_threshold: %ld\n", params->stop_threshold);
6421         snd_output_printf(out, "silence_threshold: %lu\n", params->silence_threshold);
6422         snd_output_printf(out, "silence_size: %lu\n", params->silence_size);
6423         snd_output_printf(out, "boundary: %lu\n", params->boundary);
6424         return 0;
6425 }
6426
6427 /**
6428  * \brief get size of #snd_pcm_sw_params_t
6429  * \return size in bytes
6430  */
6431 size_t snd_pcm_sw_params_sizeof()
6432 {
6433         return sizeof(snd_pcm_sw_params_t);
6434 }
6435
6436 /**
6437  * \brief allocate an invalid #snd_pcm_sw_params_t using standard malloc
6438  * \param ptr returned pointer
6439  * \return 0 on success otherwise negative error code
6440  */
6441 int snd_pcm_sw_params_malloc(snd_pcm_sw_params_t **ptr)
6442 {
6443         assert(ptr);
6444         *ptr = calloc(1, sizeof(snd_pcm_sw_params_t));
6445         if (!*ptr)
6446                 return -ENOMEM;
6447         return 0;
6448 }
6449
6450 /**
6451  * \brief frees a previously allocated #snd_pcm_sw_params_t
6452  * \param obj pointer to object to free
6453  */
6454 void snd_pcm_sw_params_free(snd_pcm_sw_params_t *obj)
6455 {
6456         free(obj);
6457 }
6458
6459 /**
6460  * \brief copy one #snd_pcm_sw_params_t to another
6461  * \param dst pointer to destination
6462  * \param src pointer to source
6463  */
6464 void snd_pcm_sw_params_copy(snd_pcm_sw_params_t *dst, const snd_pcm_sw_params_t *src)
6465 {
6466         assert(dst && src);
6467         *dst = *src;
6468 }
6469
6470 /**
6471  * \brief Get boundary for ring pointers from a software configuration container
6472  * \param params Software configuration container
6473  * \param val Returned boundary in frames
6474  * \return 0 otherwise a negative error code
6475  */
6476 int snd_pcm_sw_params_get_boundary(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val)
6477 {
6478         assert(params);
6479         *val = params->boundary;
6480         return 0;
6481 }
6482
6483 /**
6484  * \brief (DEPRECATED) Set start mode inside a software configuration container
6485  * \param pcm PCM handle
6486  * \param params Software configuration container
6487  * \param val Start mode
6488  * \return 0 otherwise a negative error code
6489  */
6490 int snd_pcm_sw_params_set_start_mode(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_start_t val)
6491 {
6492         assert(pcm && params);
6493         switch (val) {
6494         case SND_PCM_START_DATA:
6495                 params->start_threshold = 1;
6496                 break;
6497         case SND_PCM_START_EXPLICIT:
6498                 params->start_threshold = pcm->boundary;
6499                 break;
6500         default:
6501                 SNDMSG("invalid start mode value %d", val);
6502                 return -EINVAL;
6503         }
6504         return 0;
6505 }
6506
6507 #ifndef DOC_HIDDEN
6508 link_warning(snd_pcm_sw_params_set_start_mode, "Warning: start_mode is deprecated, consider to use start_threshold");
6509 #endif
6510
6511 /**
6512  * \brief (DEPRECATED) Get start mode from a software configuration container
6513  * \param params Software configuration container
6514  * \return start mode
6515  */
6516 snd_pcm_start_t snd_pcm_sw_params_get_start_mode(const snd_pcm_sw_params_t *params)
6517 {
6518         assert(params);
6519         /* FIXME: Ugly */
6520         return params->start_threshold > 1024 * 1024 ? SND_PCM_START_EXPLICIT : SND_PCM_START_DATA;
6521 }
6522
6523 #ifndef DOC_HIDDEN
6524 link_warning(snd_pcm_sw_params_get_start_mode, "Warning: start_mode is deprecated, consider to use start_threshold");
6525 #endif
6526
6527 /**
6528  * \brief (DEPRECATED) Set xrun mode inside a software configuration container
6529  * \param pcm PCM handle
6530  * \param params Software configuration container
6531  * \param val Xrun mode
6532  * \return 0 otherwise a negative error code
6533  */
6534 #ifndef DOXYGEN
6535 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)
6536 #else
6537 int snd_pcm_sw_params_set_xrun_mode(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_xrun_t val)
6538 #endif
6539 {
6540         assert(pcm && params);
6541         switch (val) {
6542         case SND_PCM_XRUN_STOP:
6543                 params->stop_threshold = pcm->buffer_size;
6544                 break;
6545         case SND_PCM_XRUN_NONE:
6546                 params->stop_threshold = pcm->boundary;
6547                 break;
6548         default:
6549                 SNDMSG("invalid xrun mode value %d", val);
6550                 return -EINVAL;
6551         }
6552         return 0;
6553 }
6554
6555 #ifndef DOC_HIDDEN
6556 link_warning(snd_pcm_sw_params_set_xrun_mode, "Warning: xrun_mode is deprecated, consider to use stop_threshold");
6557 #endif
6558
6559 /**
6560  * \brief (DEPRECATED) Get xrun mode from a software configuration container
6561  * \param params Software configuration container
6562  * \return xrun mode
6563  */
6564 snd_pcm_xrun_t snd_pcm_sw_params_get_xrun_mode(const snd_pcm_sw_params_t *params)
6565 {
6566         assert(params);
6567         /* FIXME: Ugly */
6568         return params->stop_threshold > 1024 * 1024 ? SND_PCM_XRUN_NONE : SND_PCM_XRUN_STOP;
6569 }
6570
6571 #ifndef DOC_HIDDEN
6572 link_warning(snd_pcm_sw_params_get_xrun_mode, "Warning: xrun_mode is deprecated, consider to use stop_threshold");
6573 #endif
6574
6575 /**
6576  * \brief Set timestamp mode inside a software configuration container
6577  * \param pcm PCM handle
6578  * \param params Software configuration container
6579  * \param val Timestamp mode
6580  * \return 0 otherwise a negative error code
6581  */
6582 #ifndef DOXYGEN
6583 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)
6584 #else
6585 int snd_pcm_sw_params_set_tstamp_mode(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_tstamp_t val)
6586 #endif
6587 {
6588         assert(pcm && params);
6589         if (CHECK_SANITY(val > SND_PCM_TSTAMP_LAST)) {
6590                 SNDMSG("invalid tstamp_mode value %d", val);
6591                 return -EINVAL;
6592         }
6593         params->tstamp_mode = val;
6594         return 0;
6595 }
6596
6597 /**
6598  * \brief Get timestamp mode from a software configuration container
6599  * \param params Software configuration container
6600  * \param val Returned timestamp
6601  * \return 0 otherwise a negative error code
6602  */
6603 #ifndef DOXYGEN
6604 EXPORT_SYMBOL int INTERNAL(snd_pcm_sw_params_get_tstamp_mode)(const snd_pcm_sw_params_t *params, snd_pcm_tstamp_t *val)
6605 #else
6606 int snd_pcm_sw_params_get_tstamp_mode(const snd_pcm_sw_params_t *params, snd_pcm_tstamp_t *val)
6607 #endif
6608 {
6609         assert(params && val);
6610         *val = params->tstamp_mode;
6611         return 0;
6612 }
6613
6614 /**
6615  * \brief Set timestamp type inside a software configuration container
6616  * \param pcm PCM handle
6617  * \param params Software configuration container
6618  * \param val Timestamp type
6619  * \return 0 otherwise a negative error code
6620  */
6621 int snd_pcm_sw_params_set_tstamp_type(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_tstamp_type_t val)
6622 {
6623         assert(pcm && params);
6624         if (CHECK_SANITY(val > SND_PCM_TSTAMP_TYPE_LAST)) {
6625                 SNDMSG("invalid tstamp_type value %d", val);
6626                 return -EINVAL;
6627         }
6628         params->tstamp_type = val;
6629         return 0;
6630 }
6631
6632 /**
6633  * \brief Get timestamp type from a software configuration container
6634  * \param params Software configuration container
6635  * \param val Returned timestamp type
6636  * \return 0 otherwise a negative error code
6637  */
6638 int snd_pcm_sw_params_get_tstamp_type(const snd_pcm_sw_params_t *params, snd_pcm_tstamp_type_t *val)
6639 {
6640         assert(params && val);
6641         *val = params->tstamp_type;
6642         return 0;
6643 }
6644
6645 /**
6646  * \brief (DEPRECATED) Set minimum number of ticks to sleep inside a software configuration container
6647  * \param pcm PCM handle
6648  * \param params Software configuration container
6649  * \param val Minimum ticks to sleep or 0 to disable the use of tick timer
6650  * \return 0 otherwise a negative error code
6651  */
6652 #ifndef DOXYGEN
6653 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)
6654 #else
6655 int snd_pcm_sw_params_set_sleep_min(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, unsigned int val)
6656 #endif
6657 {
6658         return 0;
6659 }
6660
6661 /**
6662  * \brief (DEPRECATED) Get minimum numbers of ticks to sleep from a software configuration container
6663  * \param params Software configuration container
6664  * \param val returned minimum number of ticks to sleep or 0 if tick timer is disabled
6665  * \return 0 otherwise a negative error code
6666  */
6667 #ifndef DOXYGEN
6668 EXPORT_SYMBOL int INTERNAL(snd_pcm_sw_params_get_sleep_min)(const snd_pcm_sw_params_t *params ATTRIBUTE_UNUSED, unsigned int *val)
6669 #else
6670 int snd_pcm_sw_params_get_sleep_min(const snd_pcm_sw_params_t *params, unsigned int *val)
6671 #endif
6672 {
6673         *val = 0;
6674         return 0;
6675 }
6676
6677 /**
6678  * \brief Set avail min inside a software configuration container
6679  * \param pcm PCM handle
6680  * \param params Software configuration container
6681  * \param val Minimum avail frames to consider PCM ready
6682  * \return 0 otherwise a negative error code
6683  *
6684  * Note: This is similar to setting an OSS wakeup point.  The valid
6685  * values for 'val' are determined by the specific hardware.  Most PC
6686  * sound cards can only accept power of 2 frame counts (i.e. 512,
6687  * 1024, 2048).  You cannot use this as a high resolution timer - it
6688  * is limited to how often the sound card hardware raises an
6689  * interrupt.
6690  */
6691 #ifndef DOXYGEN
6692 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)
6693 #else
6694 int snd_pcm_sw_params_set_avail_min(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val)
6695 #endif
6696 {
6697         assert(pcm && params);
6698         /* Fix avail_min if it's below period size.  The period_size
6699          * defines the minimal wake-up timing accuracy, so it doesn't
6700          * make sense to set below that.
6701          */
6702         if (val < pcm->period_size)
6703                 val = pcm->period_size;
6704         params->avail_min = val;
6705         return 0;
6706 }
6707
6708 /**
6709  * \brief Get avail min from a software configuration container
6710  * \param params Software configuration container
6711  * \param val returned minimum available frames to consider PCM ready
6712  * \return 0 otherwise a negative error code
6713  *
6714  * This is a threshold value when the PCM stream is considered as ready for
6715  * another read/write operation or poll event.
6716  */
6717 #ifndef DOXYGEN
6718 EXPORT_SYMBOL int INTERNAL(snd_pcm_sw_params_get_avail_min)(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val)
6719 #else
6720 int snd_pcm_sw_params_get_avail_min(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val)
6721 #endif
6722 {
6723         assert(params && val);
6724         *val = params->avail_min;
6725         return 0;
6726 }
6727
6728 /**
6729  * \brief Set period event inside a software configuration container
6730  * \param pcm PCM handle
6731  * \param params Software configuration container
6732  * \param val 0 = disable period event, 1 = enable period event
6733  * \return 0 otherwise a negative error code
6734  *
6735  * An poll (select) wakeup event is raised if enabled.
6736  */
6737 int snd_pcm_sw_params_set_period_event(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, int val)
6738 {
6739         assert(pcm && params);
6740         sw_set_period_event(params, val);
6741         return 0;
6742 }
6743
6744 /**
6745  * \brief Get period event from a software configuration container
6746  * \param params Software configuration container
6747  * \param val returned period event state
6748  * \return 0 otherwise a negative error code
6749  */
6750 int snd_pcm_sw_params_get_period_event(const snd_pcm_sw_params_t *params, int *val)
6751 {
6752         assert(params && val);
6753         *val = sw_get_period_event(params);
6754         return 0;
6755 }
6756
6757 /**
6758  * \brief (DEPRECATED) Set xfer align inside a software configuration container
6759  * \param pcm PCM handle
6760  * \param params Software configuration container
6761  * \param val Chunk size (frames are attempted to be transferred in chunks)
6762  * \return 0 otherwise a negative error code
6763  */
6764 #ifndef DOXYGEN
6765 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)
6766 #else
6767 int snd_pcm_sw_params_set_xfer_align(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val)
6768 #endif
6769 {
6770         return 0;
6771 }
6772
6773 /**
6774  * \brief (DEPRECATED) Get xfer align from a software configuration container
6775  * \param params Software configuration container
6776  * \param val returned chunk size (frames are attempted to be transferred in chunks)
6777  * \return 0 otherwise a negative error code
6778  */
6779 #ifndef DOXYGEN
6780 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)
6781 #else
6782 int snd_pcm_sw_params_get_xfer_align(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val)
6783 #endif
6784 {
6785         *val = 1;
6786         return 0;
6787 }
6788
6789 /**
6790  * \brief Set start threshold inside a software configuration container
6791  * \param pcm PCM handle
6792  * \param params Software configuration container
6793  * \param val Start threshold in frames
6794  * \return 0 otherwise a negative error code
6795  *
6796  * PCM is automatically started when playback frames available to PCM 
6797  * are >= threshold or when requested capture frames are >= threshold
6798  */
6799 #ifndef DOXYGEN
6800 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)
6801 #else
6802 int snd_pcm_sw_params_set_start_threshold(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val)
6803 #endif
6804 {
6805         assert(pcm && params);
6806         params->start_threshold = val;
6807         return 0;
6808 }
6809
6810 /**
6811  * \brief Get start threshold from a software configuration container
6812  * \param params Software configuration container
6813  * \param val Returned start threshold in frames
6814  * \return 0 otherwise a negative error code
6815  *
6816  * PCM is automatically started when playback frames available to PCM 
6817  * are >= threshold or when requested capture frames are >= threshold
6818  */
6819 #ifndef DOXYGEN
6820 EXPORT_SYMBOL int INTERNAL(snd_pcm_sw_params_get_start_threshold)(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val)
6821 #else
6822 int snd_pcm_sw_params_get_start_threshold(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val)
6823 #endif
6824 {
6825         assert(params);
6826         *val = params->start_threshold;
6827         return 0;
6828 }
6829
6830
6831 /**
6832  * \brief Set stop threshold inside a software configuration container
6833  * \param pcm PCM handle
6834  * \param params Software configuration container
6835  * \param val Stop threshold in frames
6836  * \return 0 otherwise a negative error code
6837  *
6838  * PCM is automatically stopped in #SND_PCM_STATE_XRUN state when available
6839  * frames is >= threshold. If the stop threshold is equal to boundary (also
6840  * software parameter - sw_param) then automatic stop will be disabled
6841  * (thus device will do the endless loop in the ring buffer).
6842  */
6843 #ifndef DOXYGEN
6844 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)
6845 #else
6846 int snd_pcm_sw_params_set_stop_threshold(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val)
6847 #endif
6848 {
6849         assert(pcm && params);
6850         params->stop_threshold = val;
6851         return 0;
6852 }
6853
6854 /**
6855  * \brief Get stop threshold from a software configuration container
6856  * \param params Software configuration container
6857  * \param val Returned stop threshold in frames
6858  * \return 0 otherwise a negative error code
6859  *
6860  * PCM is automatically stopped in #SND_PCM_STATE_XRUN state when available
6861  * frames is >= threshold. If the stop threshold is equal to boundary (also
6862  * software parameter - sw_param) then automatic stop will be disabled
6863  * (thus device will do the endless loop in the ring buffer).
6864  */
6865 #ifndef DOXYGEN
6866 EXPORT_SYMBOL int INTERNAL(snd_pcm_sw_params_get_stop_threshold)(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val)
6867 #else
6868 int snd_pcm_sw_params_get_stop_threshold(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val)
6869 #endif
6870 {
6871         assert(params);
6872         *val = params->stop_threshold;
6873         return 0;
6874 }
6875
6876
6877 /**
6878  * \brief Set silence threshold inside a software configuration container
6879  * \param pcm PCM handle
6880  * \param params Software configuration container
6881  * \param val Silence threshold in frames 
6882  * \return 0 otherwise a negative error code
6883  *
6884  * A portion of playback buffer is overwritten with silence (see 
6885  * #snd_pcm_sw_params_set_silence_size) when playback underrun is nearer
6886  * than silence threshold.
6887  */
6888 #ifndef DOXYGEN
6889 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)
6890 #else
6891 int snd_pcm_sw_params_set_silence_threshold(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val)
6892 #endif
6893 {
6894         assert(pcm && params);
6895         if (CHECK_SANITY(val >= pcm->buffer_size)) {
6896                 SNDMSG("invalid silent_threshold value %ld (buffer_size = %ld)",
6897                        val, pcm->buffer_size);
6898                 return -EINVAL;
6899         }
6900         params->silence_threshold = val;
6901         return 0;
6902 }
6903
6904 /**
6905  * \brief Get silence threshold from a software configuration container
6906  * \param params Software configuration container
6907  * \param val Returned silence threshold in frames
6908  * \return 0 otherwise a negative error value
6909  *
6910  * A portion of playback buffer is overwritten with silence (see 
6911  * #snd_pcm_sw_params_set_silence_size) when playback underrun is nearer
6912  * than silence threshold.
6913  */
6914 #ifndef DOXYGEN
6915 EXPORT_SYMBOL int INTERNAL(snd_pcm_sw_params_get_silence_threshold)(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val)
6916 #else
6917 int snd_pcm_sw_params_get_silence_threshold(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val)
6918 #endif
6919 {
6920         assert(params && val);
6921         *val = params->silence_threshold;
6922         return 0;
6923 }
6924
6925
6926 /**
6927  * \brief Set silence size inside a software configuration container
6928  * \param pcm PCM handle
6929  * \param params Software configuration container
6930  * \param val Silence size in frames (0 for disabled)
6931  * \return 0 otherwise a negative error code
6932  *
6933  * A portion of playback buffer is overwritten with silence when playback
6934  * underrun is nearer than silence threshold (see 
6935  * #snd_pcm_sw_params_set_silence_threshold)
6936  *
6937  * When drain silence (see #snd_pcm_hw_params_get_drain_silence) is disabled,
6938  * this will also apply for draining, i.e. silence is written also when the
6939  * drain end is nearer than the silence threshold.
6940  *
6941  * The special case is when silence size value is equal or greater than
6942  * boundary. The unused portion of the ring buffer (initial written samples
6943  * are untouched) is filled with silence at start. Later, only just processed
6944  * sample area is filled with silence. Note: silence_threshold must be set to zero.
6945  */
6946 #ifndef DOXYGEN
6947 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)
6948 #else
6949 int snd_pcm_sw_params_set_silence_size(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val)
6950 #endif
6951 {
6952         assert(pcm && params);
6953         if (CHECK_SANITY(val < pcm->boundary && val > pcm->buffer_size)) {
6954                 SNDMSG("invalid silence_size %ld (boundary %ld, buffer_size %ld)",
6955                        val, pcm->boundary, pcm->buffer_size);
6956                 return -EINVAL;
6957         }
6958         params->silence_size = val;
6959         return 0;
6960 }
6961
6962 /**
6963  * \brief Get silence size from a software configuration container
6964  * \param params Software configuration container
6965  * \param val Returned silence size in frames (0 for disabled)
6966  * \return 0 otherwise a negative error code
6967  *
6968  * A portion of playback buffer is overwritten with silence when playback
6969  * underrun is nearer than silence threshold (see 
6970  * #snd_pcm_sw_params_set_silence_threshold)
6971  */
6972 #ifndef DOXYGEN
6973 EXPORT_SYMBOL int INTERNAL(snd_pcm_sw_params_get_silence_size)(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val)
6974 #else
6975 int snd_pcm_sw_params_get_silence_size(const snd_pcm_sw_params_t *params, snd_pcm_uframes_t *val)
6976 #endif
6977 {
6978         assert(params);
6979         *val = params->silence_size;
6980         return 0;
6981 }
6982
6983
6984 /**
6985  * \brief get size of #snd_pcm_status_t
6986  * \return size in bytes
6987  */
6988 size_t snd_pcm_status_sizeof()
6989 {
6990         return sizeof(snd_pcm_status_t);
6991 }
6992
6993 /**
6994  * \brief allocate an invalid #snd_pcm_status_t using standard malloc
6995  * \param ptr returned pointer
6996  * \return 0 on success otherwise negative error code
6997  */
6998 int snd_pcm_status_malloc(snd_pcm_status_t **ptr)
6999 {
7000         assert(ptr);
7001         *ptr = calloc(1, sizeof(snd_pcm_status_t));
7002         if (!*ptr)
7003                 return -ENOMEM;
7004         return 0;
7005 }
7006
7007 /**
7008  * \brief frees a previously allocated #snd_pcm_status_t
7009  * \param obj pointer to object to free
7010  */
7011 void snd_pcm_status_free(snd_pcm_status_t *obj)
7012 {
7013         free(obj);
7014 }
7015
7016 /**
7017  * \brief copy one #snd_pcm_status_t to another
7018  * \param dst pointer to destination
7019  * \param src pointer to source
7020  */
7021 void snd_pcm_status_copy(snd_pcm_status_t *dst, const snd_pcm_status_t *src)
7022 {
7023         assert(dst && src);
7024         *dst = *src;
7025 }
7026
7027 /** 
7028  * \brief Get state from a PCM status container (see #snd_pcm_state)
7029  * \param obj #snd_pcm_status_t pointer
7030  * \return PCM state
7031  */
7032 snd_pcm_state_t snd_pcm_status_get_state(const snd_pcm_status_t *obj)
7033 {
7034         assert(obj);
7035         return obj->state;
7036 }
7037
7038 /** 
7039  * \brief Get trigger timestamp from a PCM status container
7040  * \param obj #snd_pcm_status_t pointer
7041  * \param ptr Pointer to returned timestamp
7042  *
7043  * Trigger means a PCM state transition (from stopped to running or
7044  * versa vice). It applies also to pause and suspend. In other words,
7045  * timestamp contains time when stream started or when it was stopped.
7046  */
7047 void snd_pcm_status_get_trigger_tstamp(const snd_pcm_status_t *obj, snd_timestamp_t *ptr)
7048 {
7049         assert(obj && ptr);
7050         ptr->tv_sec = obj->trigger_tstamp.tv_sec;
7051         ptr->tv_usec = obj->trigger_tstamp.tv_nsec / 1000L;
7052 }
7053
7054 /** 
7055  * \brief Get trigger hi-res timestamp from a PCM status container
7056  * \param obj #snd_pcm_status_t pointer
7057  * \param ptr Pointer to returned timestamp
7058  *
7059  * Trigger means a PCM state transition (from stopped to running or
7060  * versa vice). It applies also to pause and suspend. In other words,
7061  * timestamp contains time when stream started or when it was stopped.
7062  */
7063 #ifndef DOXYGEN
7064 EXPORT_SYMBOL void INTERNAL(snd_pcm_status_get_trigger_htstamp)(const snd_pcm_status_t *obj, snd_htimestamp_t *ptr)
7065 #else
7066 void snd_pcm_status_get_trigger_htstamp(const snd_pcm_status_t *obj, snd_htimestamp_t *ptr)
7067 #endif
7068 {
7069         assert(obj && ptr);
7070         *ptr = obj->trigger_tstamp;
7071 }
7072 use_default_symbol_version(__snd_pcm_status_get_trigger_htstamp, snd_pcm_status_get_trigger_htstamp, ALSA_0.9.0rc8);
7073
7074 /** 
7075  * \brief Get "now" timestamp from a PCM status container
7076  * \param obj #snd_pcm_status_t pointer
7077  * \param ptr Pointer to returned timestamp
7078  */
7079 void snd_pcm_status_get_tstamp(const snd_pcm_status_t *obj, snd_timestamp_t *ptr)
7080 {
7081         assert(obj && ptr);
7082         ptr->tv_sec = obj->tstamp.tv_sec;
7083         ptr->tv_usec = obj->tstamp.tv_nsec / 1000L;
7084 }
7085
7086 /** 
7087  * \brief Get "now" hi-res timestamp from a PCM status container
7088  * \param obj pointer to #snd_pcm_status_t
7089  * \param ptr Pointer to returned timestamp
7090  */
7091 #ifndef DOXYGEN
7092 EXPORT_SYMBOL void INTERNAL(snd_pcm_status_get_htstamp)(const snd_pcm_status_t *obj, snd_htimestamp_t *ptr)
7093 #else
7094 void snd_pcm_status_get_htstamp(const snd_pcm_status_t *obj, snd_htimestamp_t *ptr)
7095 #endif
7096 {
7097         assert(obj && ptr);
7098         *ptr = obj->tstamp;
7099 }
7100 use_default_symbol_version(__snd_pcm_status_get_htstamp, snd_pcm_status_get_htstamp, ALSA_0.9.0rc8);
7101
7102 /** 
7103  * \brief Get "now" hi-res audio timestamp from a PCM status container
7104  * \param obj pointer to #snd_pcm_status_t
7105  * \param ptr Pointer to returned timestamp
7106  */
7107 void snd_pcm_status_get_audio_htstamp(const snd_pcm_status_t *obj, snd_htimestamp_t *ptr)
7108 {
7109         assert(obj && ptr);
7110         *ptr = obj->audio_tstamp;
7111 }
7112
7113 /**
7114  * \brief Get "now" hi-res driver timestamp from a PCM status container. Defines when the status
7115  * was generated by driver, may differ from normal timestamp.
7116  * \param obj pointer to #snd_pcm_status_t
7117  * \param ptr Pointer to returned timestamp
7118  */
7119 void snd_pcm_status_get_driver_htstamp(const snd_pcm_status_t *obj, snd_htimestamp_t *ptr)
7120 {
7121         assert(obj && ptr);
7122         *ptr = obj->driver_tstamp;
7123 }
7124
7125 /**
7126  * \brief Get audio_tstamp_report from a PCM status container
7127  * \param obj pointer to #snd_pcm_status_t
7128  * \param audio_tstamp_report Pointer to returned report
7129  */
7130 void snd_pcm_status_get_audio_htstamp_report(const snd_pcm_status_t *obj,
7131                                              snd_pcm_audio_tstamp_report_t *audio_tstamp_report)
7132 {
7133         assert(obj && audio_tstamp_report);
7134         snd_pcm_unpack_audio_tstamp_report(obj->audio_tstamp_data,
7135                                         obj->audio_tstamp_accuracy,
7136                                         audio_tstamp_report);
7137 }
7138
7139 /**
7140  * \brief set audio_tstamp_config from a PCM status container
7141  * \param obj pointer to #snd_pcm_status_t
7142  * \param audio_tstamp_config Pointer to config (valid fields are type_requested and report_delay)
7143  */
7144 void snd_pcm_status_set_audio_htstamp_config(snd_pcm_status_t *obj,
7145                                              snd_pcm_audio_tstamp_config_t *audio_tstamp_config)
7146 {
7147         assert(obj && audio_tstamp_config);
7148         snd_pcm_pack_audio_tstamp_config(&obj->audio_tstamp_data, audio_tstamp_config);
7149 }
7150
7151 /**
7152  * \brief Get delay from a PCM status container (see #snd_pcm_delay)
7153  * \return Delay in frames
7154  *
7155  * Delay is distance between current application frame position and
7156  * sound frame position.
7157  * It's positive and less than buffer size in normal situation,
7158  * negative on playback underrun and greater than buffer size on
7159  * capture overrun.
7160  */
7161 snd_pcm_sframes_t snd_pcm_status_get_delay(const snd_pcm_status_t *obj)
7162 {
7163         assert(obj);
7164         return obj->delay;
7165 }
7166
7167 /** 
7168  * \brief Get number of frames available from a PCM status container (see #snd_pcm_avail_update)
7169  * \return Number of frames ready to be read/written
7170  */
7171 snd_pcm_uframes_t snd_pcm_status_get_avail(const snd_pcm_status_t *obj)
7172 {
7173         assert(obj);
7174         return obj->avail;
7175 }
7176
7177 /** 
7178  * \brief Get maximum number of frames available from a PCM status container after last #snd_pcm_status call
7179  * \return Maximum number of frames ready to be read/written
7180  *
7181  * This value returns the peak for the available frames between #snd_pcm_status calls.
7182  */
7183 snd_pcm_uframes_t snd_pcm_status_get_avail_max(const snd_pcm_status_t *obj)
7184 {
7185         assert(obj);
7186         return obj->avail_max;
7187 }
7188
7189 /** 
7190  * \brief Get count of ADC overrange detections since last call
7191  * \return Count of ADC overrange detections
7192  */
7193 snd_pcm_uframes_t snd_pcm_status_get_overrange(const snd_pcm_status_t *obj)
7194 {
7195         assert(obj);
7196         return obj->overrange;
7197 }
7198
7199 /**
7200  * \brief get size of #snd_pcm_info_t
7201  * \return size in bytes
7202  */
7203 size_t snd_pcm_info_sizeof()
7204 {
7205         return sizeof(snd_pcm_info_t);
7206 }
7207
7208 /**
7209  * \brief allocate an invalid #snd_pcm_info_t using standard malloc
7210  * \param ptr returned pointer
7211  * \return 0 on success otherwise negative error code
7212  */
7213 int snd_pcm_info_malloc(snd_pcm_info_t **ptr)
7214 {
7215         assert(ptr);
7216         *ptr = calloc(1, sizeof(snd_pcm_info_t));
7217         if (!*ptr)
7218                 return -ENOMEM;
7219         return 0;
7220 }
7221
7222 /**
7223  * \brief frees a previously allocated #snd_pcm_info_t
7224  * \param obj pointer to object to free
7225  */
7226 void snd_pcm_info_free(snd_pcm_info_t *obj)
7227 {
7228         free(obj);
7229 }
7230
7231 /**
7232  * \brief copy one #snd_pcm_info_t to another
7233  * \param dst pointer to destination
7234  * \param src pointer to source
7235  */
7236 void snd_pcm_info_copy(snd_pcm_info_t *dst, const snd_pcm_info_t *src)
7237 {
7238         assert(dst && src);
7239         *dst = *src;
7240 }
7241
7242 /**
7243  * \brief Get device from a PCM info container
7244  * \param obj PCM info container
7245  * \return device number
7246  */
7247 unsigned int snd_pcm_info_get_device(const snd_pcm_info_t *obj)
7248 {
7249         assert(obj);
7250         return obj->device;
7251 }
7252
7253 /**
7254  * \brief Get subdevice from a PCM info container
7255  * \param obj PCM info container
7256  * \return subdevice number
7257  */
7258 unsigned int snd_pcm_info_get_subdevice(const snd_pcm_info_t *obj)
7259 {
7260         assert(obj);
7261         return obj->subdevice;
7262 }
7263
7264 /**
7265  * \brief Get stream (direction) from a PCM info container
7266  * \param obj PCM info container
7267  * \return stream
7268  */
7269 snd_pcm_stream_t snd_pcm_info_get_stream(const snd_pcm_info_t *obj)
7270 {
7271         assert(obj);
7272         return obj->stream;
7273 }
7274
7275 /**
7276  * \brief Get card from a PCM info container
7277  * \param obj PCM info container
7278  * \return card number otherwise a negative error code if not associable to a card
7279  */
7280 int snd_pcm_info_get_card(const snd_pcm_info_t *obj)
7281 {
7282         assert(obj);
7283         return obj->card;
7284 }
7285
7286 /**
7287  * \brief Get id from a PCM info container
7288  * \param obj PCM info container
7289  * \return short id of PCM
7290  */
7291 const char *snd_pcm_info_get_id(const snd_pcm_info_t *obj)
7292 {
7293         assert(obj);
7294         return (const char *)obj->id;
7295 }
7296
7297 /**
7298  * \brief Get name from a PCM info container
7299  * \param obj PCM info container
7300  * \return name of PCM
7301  */
7302 const char *snd_pcm_info_get_name(const snd_pcm_info_t *obj)
7303 {
7304         assert(obj);
7305         return (const char *)obj->name;
7306 }
7307
7308 /**
7309  * \brief Get subdevice name from a PCM info container
7310  * \param obj PCM info container
7311  * \return name of used PCM subdevice
7312  */
7313 const char *snd_pcm_info_get_subdevice_name(const snd_pcm_info_t *obj)
7314 {
7315         assert(obj);
7316         return (const char *)obj->subname;
7317 }
7318
7319 /**
7320  * \brief Get class from a PCM info container
7321  * \param obj PCM info container
7322  * \return class of PCM
7323  */
7324 snd_pcm_class_t snd_pcm_info_get_class(const snd_pcm_info_t *obj)
7325 {
7326         assert(obj);
7327         return obj->dev_class;
7328 }
7329
7330 /**
7331  * \brief Get subclass from a PCM info container
7332  * \param obj PCM info container
7333  * \return subclass of PCM
7334  */
7335 snd_pcm_subclass_t snd_pcm_info_get_subclass(const snd_pcm_info_t *obj)
7336 {
7337         assert(obj);
7338         return obj->dev_subclass;
7339 }
7340
7341 /**
7342  * \brief Get subdevices count from a PCM info container
7343  * \param obj PCM info container
7344  * \return subdevices total count of PCM
7345  */
7346 unsigned int snd_pcm_info_get_subdevices_count(const snd_pcm_info_t *obj)
7347 {
7348         assert(obj);
7349         return obj->subdevices_count;
7350 }
7351
7352 /**
7353  * \brief Get available subdevices count from a PCM info container
7354  * \param obj PCM info container
7355  * \return available subdevices count of PCM
7356  */
7357 unsigned int snd_pcm_info_get_subdevices_avail(const snd_pcm_info_t *obj)
7358 {
7359         assert(obj);
7360         return obj->subdevices_avail;
7361 }
7362
7363 /**
7364  * \brief (DEPRECATED) Get hardware synchronization ID from a PCM info container
7365  * \param obj PCM info container
7366  * \return hardware synchronization ID
7367  */
7368 snd_pcm_sync_id_t snd_pcm_info_get_sync(const snd_pcm_info_t *obj)
7369 {
7370         snd_pcm_sync_id_t res;
7371         assert(obj);
7372         bzero(&res, sizeof(res));
7373         return res;
7374 }
7375 #ifndef DOC_HIDDEN
7376 link_warning(snd_pcm_info_get_sync, "Warning: snd_pcm_info_get_sync is deprecated, consider to use snd_pcm_hw_params_get_sync");
7377 #endif
7378
7379 /**
7380  * \brief Set wanted device inside a PCM info container (see #snd_ctl_pcm_info)
7381  * \param obj PCM info container
7382  * \param val Device number
7383  */
7384 void snd_pcm_info_set_device(snd_pcm_info_t *obj, unsigned int val)
7385 {
7386         assert(obj);
7387         obj->device = val;
7388 }
7389
7390 /**
7391  * \brief Set wanted subdevice inside a PCM info container (see #snd_ctl_pcm_info)
7392  * \param obj PCM info container
7393  * \param val Subdevice number
7394  */
7395 void snd_pcm_info_set_subdevice(snd_pcm_info_t *obj, unsigned int val)
7396 {
7397         assert(obj);
7398         obj->subdevice = val;
7399 }
7400
7401 /**
7402  * \brief Set wanted stream inside a PCM info container (see #snd_ctl_pcm_info)
7403  * \param obj PCM info container
7404  * \param val Stream
7405  */
7406 void snd_pcm_info_set_stream(snd_pcm_info_t *obj, snd_pcm_stream_t val)
7407 {
7408         assert(obj);
7409         obj->stream = val;
7410 }
7411
7412 /**
7413  * \brief Application request to access a portion of direct (mmap) area
7414  * \param pcm PCM handle 
7415  * \param areas Returned mmap channel areas
7416  * \param offset Returned mmap area offset in area steps (== frames)
7417  * \param frames mmap area portion size in frames (wanted on entry, contiguous available on exit)
7418  * \return 0 on success otherwise a negative error code
7419  *
7420  * It is necessary to call the snd_pcm_avail_update() function directly before
7421  * this call. Otherwise, this function can return a wrong count of available frames.
7422  *
7423  * The function should be called before a sample-direct area can be accessed.
7424  * The resulting size parameter is always less or equal to the input count of frames
7425  * and can be zero, if no frames can be processed (the ring buffer is full).
7426  *
7427  * See the snd_pcm_mmap_commit() function to finish the frame processing in
7428  * the direct areas.
7429  *
7430  * The function is thread-safe when built with the proper option.
7431  */
7432 int snd_pcm_mmap_begin(snd_pcm_t *pcm,
7433                        const snd_pcm_channel_area_t **areas,
7434                        snd_pcm_uframes_t *offset,
7435                        snd_pcm_uframes_t *frames)
7436 {
7437         int err;
7438
7439         err = bad_pcm_state(pcm, P_STATE_RUNNABLE, 0);
7440         if (err < 0)
7441                 return err;
7442         snd_pcm_lock(pcm->fast_op_arg);
7443         err = __snd_pcm_mmap_begin(pcm, areas, offset, frames);
7444         snd_pcm_unlock(pcm->fast_op_arg);
7445         return err;
7446 }
7447
7448 #ifndef DOC_HIDDEN
7449 int __snd_pcm_mmap_begin_generic(snd_pcm_t *pcm, const snd_pcm_channel_area_t **areas,
7450                                  snd_pcm_uframes_t *offset, snd_pcm_uframes_t *frames)
7451 {
7452         snd_pcm_uframes_t cont;
7453         snd_pcm_uframes_t f;
7454         snd_pcm_uframes_t avail;
7455         const snd_pcm_channel_area_t *xareas;
7456
7457         assert(pcm && areas && offset && frames);
7458
7459         /* fallback for plugins that do not specify new callback */
7460         xareas = snd_pcm_mmap_areas(pcm);
7461         if (xareas == NULL)
7462                 return -EBADFD;
7463         *areas = xareas;
7464         *offset = *pcm->appl.ptr % pcm->buffer_size;
7465         avail = snd_pcm_mmap_avail(pcm);
7466         if (avail > pcm->buffer_size)
7467                 avail = pcm->buffer_size;
7468         cont = pcm->buffer_size - *offset;
7469         f = *frames;
7470         if (f > avail)
7471                 f = avail;
7472         if (f > cont)
7473                 f = cont;
7474         *frames = f;
7475         return 0;
7476 }
7477
7478 /* locked version */
7479 int __snd_pcm_mmap_begin(snd_pcm_t *pcm, const snd_pcm_channel_area_t **areas,
7480                          snd_pcm_uframes_t *offset, snd_pcm_uframes_t *frames)
7481 {
7482         assert(pcm && areas && offset && frames);
7483
7484         if (pcm->fast_ops->mmap_begin)
7485                 return pcm->fast_ops->mmap_begin(pcm->fast_op_arg, areas, offset, frames);
7486
7487         return __snd_pcm_mmap_begin_generic(pcm, areas, offset, frames);
7488 }
7489 #endif
7490
7491 /**
7492  * \brief Application has completed the access to area requested with #snd_pcm_mmap_begin
7493  * \param pcm PCM handle
7494  * \param offset area offset in area steps (== frames)
7495  * \param frames area portion size in frames
7496  * \return count of transferred frames otherwise a negative error code
7497  *
7498  * You should pass this function the offset value that
7499  * snd_pcm_mmap_begin() returned. The frames parameter should hold the
7500  * number of frames you have written or read to/from the audio
7501  * buffer. The frames parameter must never exceed the contiguous frames
7502  * count that snd_pcm_mmap_begin() returned. Each call to snd_pcm_mmap_begin()
7503  * must be followed by a call to snd_pcm_mmap_commit().
7504  *
7505  * Example:
7506 \code
7507   double phase = 0;
7508   const snd_pcm_area_t *areas;
7509   snd_pcm_sframes_t avail, size, commitres;
7510   snd_pcm_uframes_t offset, frames;
7511   int err;
7512
7513   avail = snd_pcm_avail_update(pcm);
7514   if (avail < 0)
7515     error(avail);
7516   // at this point, we can transfer at least 'avail' frames
7517   
7518   // we want to process frames in chunks (period_size)
7519   if (avail < period_size)
7520     goto _skip;
7521   size = period_size;
7522   // it is possible that contiguous areas are smaller, thus we use a loop
7523   while (size > 0) {
7524     frames = size;
7525
7526     err = snd_pcm_mmap_begin(pcm_handle, &areas, &offset, &frames);
7527     if (err < 0)
7528       error(err);
7529     // this function fills the areas from offset with count of frames
7530     generate_sine(areas, offset, frames, &phase);
7531     commitres = snd_pcm_mmap_commit(pcm_handle, offset, frames);
7532     if (commitres < 0 || commitres != frames)
7533       error(commitres >= 0 ? -EPIPE : commitres);
7534       
7535     size -= frames;
7536   }
7537  _skip:
7538 \endcode
7539  *
7540  * Look to the \link example_test_pcm Sine-wave generator \endlink example
7541  * for more details about the generate_sine function.
7542  *
7543  * The function is thread-safe when built with the proper option.
7544  */
7545 snd_pcm_sframes_t snd_pcm_mmap_commit(snd_pcm_t *pcm,
7546                                       snd_pcm_uframes_t offset,
7547                                       snd_pcm_uframes_t frames)
7548 {
7549         snd_pcm_sframes_t result;
7550         int err;
7551
7552         err = bad_pcm_state(pcm, P_STATE_RUNNABLE, 0);
7553         if (err < 0)
7554                 return err;
7555         snd_pcm_lock(pcm->fast_op_arg);
7556         result = __snd_pcm_mmap_commit(pcm, offset, frames);
7557         snd_pcm_unlock(pcm->fast_op_arg);
7558         return result;
7559 }
7560
7561 #ifndef DOC_HIDDEN
7562 /* locked version*/
7563 snd_pcm_sframes_t __snd_pcm_mmap_commit(snd_pcm_t *pcm,
7564                                         snd_pcm_uframes_t offset,
7565                                         snd_pcm_uframes_t frames)
7566 {
7567         assert(pcm);
7568         if (CHECK_SANITY(offset != *pcm->appl.ptr % pcm->buffer_size)) {
7569                 SNDMSG("commit offset (%ld) doesn't match with appl_ptr (%ld) %% buf_size (%ld)",
7570                        offset, *pcm->appl.ptr, pcm->buffer_size);
7571                 return -EPIPE;
7572         }
7573         if (CHECK_SANITY(frames > snd_pcm_mmap_avail(pcm))) {
7574                 SNDMSG("commit frames (%ld) overflow (avail = %ld)", frames,
7575                        snd_pcm_mmap_avail(pcm));
7576                 return -EPIPE;
7577         }
7578         if (pcm->fast_ops->mmap_commit)
7579                 return pcm->fast_ops->mmap_commit(pcm->fast_op_arg, offset, frames);
7580         else
7581                 return -ENOSYS;
7582 }
7583
7584 int _snd_pcm_poll_descriptor(snd_pcm_t *pcm)
7585 {
7586         assert(pcm);
7587         return pcm->poll_fd;
7588 }
7589
7590 void snd_pcm_areas_from_buf(snd_pcm_t *pcm, snd_pcm_channel_area_t *areas, 
7591                             void *buf)
7592 {
7593         unsigned int channel;
7594         unsigned int channels;
7595
7596         snd_pcm_lock(pcm);
7597         channels = pcm->channels;
7598         for (channel = 0; channel < channels; ++channel, ++areas) {
7599                 areas->addr = buf;
7600                 areas->first = channel * pcm->sample_bits;
7601                 areas->step = pcm->frame_bits;
7602         }
7603         snd_pcm_unlock(pcm);
7604 }
7605
7606 void snd_pcm_areas_from_bufs(snd_pcm_t *pcm, snd_pcm_channel_area_t *areas, 
7607                              void **bufs)
7608 {
7609         unsigned int channel;
7610         unsigned int channels;
7611
7612         snd_pcm_lock(pcm);
7613         channels = pcm->channels;
7614         for (channel = 0; channel < channels; ++channel, ++areas, ++bufs) {
7615                 areas->addr = *bufs;
7616                 areas->first = 0;
7617                 areas->step = pcm->sample_bits;
7618         }
7619         snd_pcm_unlock(pcm);
7620 }
7621
7622 snd_pcm_sframes_t snd_pcm_read_areas(snd_pcm_t *pcm, const snd_pcm_channel_area_t *areas,
7623                                      snd_pcm_uframes_t offset, snd_pcm_uframes_t size,
7624                                      snd_pcm_xfer_areas_func_t func)
7625 {
7626         snd_pcm_uframes_t xfer = 0;
7627         snd_pcm_sframes_t err = 0;
7628         snd_pcm_state_t state;
7629
7630         if (size == 0)
7631                 return 0;
7632
7633         __snd_pcm_lock(pcm->fast_op_arg); /* forced lock */
7634         while (size > 0) {
7635                 snd_pcm_uframes_t frames;
7636                 snd_pcm_sframes_t avail;
7637         _again:
7638                 state = __snd_pcm_state(pcm);
7639                 switch (state) {
7640                 case SND_PCM_STATE_PREPARED:
7641                         err = __snd_pcm_start(pcm);
7642                         if (err < 0)
7643                                 goto _end;
7644                         break;
7645                 case SND_PCM_STATE_RUNNING:
7646                         err = __snd_pcm_hwsync(pcm);
7647                         if (err < 0)
7648                                 goto _end;
7649                         break;
7650                 case SND_PCM_STATE_DRAINING:
7651                 case SND_PCM_STATE_PAUSED:
7652                         break;
7653                 default:
7654                         err = pcm_state_to_error(state);
7655                         if (!err)
7656                                 err = -EBADFD;
7657                         goto _end;
7658                 }
7659                 avail = __snd_pcm_avail_update(pcm);
7660                 if (avail < 0) {
7661                         err = avail;
7662                         goto _end;
7663                 }
7664                 if (avail == 0) {
7665                         if (state == SND_PCM_STATE_DRAINING)
7666                                 goto _end;
7667                         if (pcm->mode & SND_PCM_NONBLOCK) {
7668                                 err = -EAGAIN;
7669                                 goto _end;
7670                         }
7671
7672                         err = __snd_pcm_wait_in_lock(pcm, SND_PCM_WAIT_IO);
7673                         if (err < 0)
7674                                 break;
7675                         goto _again;
7676                         
7677                 }
7678                 frames = size;
7679                 if (frames > (snd_pcm_uframes_t) avail)
7680                         frames = avail;
7681                 if (! frames)
7682                         break;
7683                 err = func(pcm, areas, offset, frames);
7684                 if (err < 0)
7685                         break;
7686                 frames = err;
7687                 offset += frames;
7688                 size -= frames;
7689                 xfer += frames;
7690         }
7691  _end:
7692         __snd_pcm_unlock(pcm->fast_op_arg);
7693         return xfer > 0 ? (snd_pcm_sframes_t) xfer : snd_pcm_check_error(pcm, err);
7694 }
7695
7696 snd_pcm_sframes_t snd_pcm_write_areas(snd_pcm_t *pcm, const snd_pcm_channel_area_t *areas,
7697                                       snd_pcm_uframes_t offset, snd_pcm_uframes_t size,
7698                                       snd_pcm_xfer_areas_func_t func)
7699 {
7700         snd_pcm_uframes_t xfer = 0;
7701         snd_pcm_sframes_t err = 0;
7702         snd_pcm_state_t state;
7703
7704         if (size == 0)
7705                 return 0;
7706
7707         __snd_pcm_lock(pcm->fast_op_arg); /* forced lock */
7708         while (size > 0) {
7709                 snd_pcm_uframes_t frames;
7710                 snd_pcm_sframes_t avail;
7711         _again:
7712                 state = __snd_pcm_state(pcm);
7713                 switch (state) {
7714                 case SND_PCM_STATE_PREPARED:
7715                 case SND_PCM_STATE_PAUSED:
7716                         break;
7717                 case SND_PCM_STATE_RUNNING:
7718                         err = __snd_pcm_hwsync(pcm);
7719                         if (err < 0)
7720                                 goto _end;
7721                         break;
7722                 default:
7723                         err = pcm_state_to_error(state);
7724                         if (!err)
7725                                 err = -EBADFD;
7726                         goto _end;
7727                 }
7728                 avail = __snd_pcm_avail_update(pcm);
7729                 if (avail < 0) {
7730                         err = avail;
7731                         goto _end;
7732                 }
7733                 if (state == SND_PCM_STATE_RUNNING &&
7734                     size > (snd_pcm_uframes_t)avail) {
7735                         if (snd_pcm_may_wait_for_avail_min(pcm, avail)) {
7736                                 if (pcm->mode & SND_PCM_NONBLOCK) {
7737                                         err = -EAGAIN;
7738                                         goto _end;
7739                                 }
7740
7741                                 err = snd_pcm_wait_nocheck(pcm, SND_PCM_WAIT_IO);
7742                                 if (err < 0)
7743                                         break;
7744                                 goto _again;
7745                         }
7746                         /* the snd_pcm_may_wait_for_avail_min may check against the
7747                          * updated hw.ptr (slaves), get the avail again here
7748                          */
7749                         avail = __snd_pcm_avail_update(pcm);
7750                         if (avail < 0) {
7751                                 err = avail;
7752                                 goto _end;
7753                         }
7754                 }
7755                 frames = size;
7756                 if (frames > (snd_pcm_uframes_t) avail)
7757                         frames = avail;
7758                 if (! frames)
7759                         break;
7760                 err = func(pcm, areas, offset, frames);
7761                 if (err < 0)
7762                         break;
7763                 frames = err;
7764                 if (state == SND_PCM_STATE_PREPARED) {
7765                         snd_pcm_sframes_t hw_avail = pcm->buffer_size - avail;
7766                         hw_avail += frames;
7767                         /* some plugins might automatically start the stream */
7768                         state = __snd_pcm_state(pcm);
7769                         if (state == SND_PCM_STATE_PREPARED &&
7770                             hw_avail >= 0 &&
7771                             (snd_pcm_uframes_t) hw_avail >= pcm->start_threshold) {
7772                                 err = __snd_pcm_start(pcm);
7773                                 if (err < 0)
7774                                         goto _end;
7775                         }
7776                 }
7777                 offset += frames;
7778                 size -= frames;
7779                 xfer += frames;
7780         }
7781  _end:
7782         __snd_pcm_unlock(pcm->fast_op_arg);
7783         return xfer > 0 ? (snd_pcm_sframes_t) xfer : snd_pcm_check_error(pcm, err);
7784 }
7785
7786 snd_pcm_uframes_t _snd_pcm_mmap_hw_ptr(snd_pcm_t *pcm)
7787 {
7788         return *pcm->hw.ptr;
7789 }
7790
7791 snd_pcm_uframes_t _snd_pcm_boundary(snd_pcm_t *pcm)
7792 {
7793         return pcm->boundary;
7794 }
7795
7796 #ifndef DOC_HIDDEN
7797 link_warning(_snd_pcm_mmap_hw_ptr, "Warning: _snd_pcm_mmap_hw_ptr() is deprecated, consider to not use this function");
7798 link_warning(_snd_pcm_boundary, "Warning: _snd_pcm_boundary() is deprecated, consider to use snd_pcm_sw_params_current()");
7799 #endif
7800
7801 static const char *const names[SND_PCM_HW_PARAM_LAST_INTERVAL + 1] = {
7802         [SND_PCM_HW_PARAM_FORMAT] = "format",
7803         [SND_PCM_HW_PARAM_CHANNELS] = "channels",
7804         [SND_PCM_HW_PARAM_RATE] = "rate",
7805         [SND_PCM_HW_PARAM_PERIOD_TIME] = "period_time",
7806         [SND_PCM_HW_PARAM_PERIOD_SIZE] = "period_size",
7807         [SND_PCM_HW_PARAM_BUFFER_TIME] = "buffer_time",
7808         [SND_PCM_HW_PARAM_BUFFER_SIZE] = "buffer_size",
7809         [SND_PCM_HW_PARAM_PERIODS] = "periods"
7810 };
7811
7812 int snd_pcm_slave_conf(snd_config_t *root, snd_config_t *conf,
7813                        snd_config_t **_pcm_conf, unsigned int count, ...)
7814 {
7815         snd_config_iterator_t i, next;
7816         const char *str;
7817         struct {
7818                 unsigned int index;
7819                 int flags;
7820                 void *ptr;
7821                 int present;
7822         } fields[count];
7823         unsigned int k;
7824         snd_config_t *pcm_conf = NULL;
7825         int err;
7826         int to_free = 0;
7827         va_list args;
7828         assert(root);
7829         assert(conf);
7830         assert(_pcm_conf);
7831         if (snd_config_get_string(conf, &str) >= 0) {
7832                 err = snd_config_search_definition(root, "pcm_slave", str, &conf);
7833                 if (err < 0) {
7834                         SNDERR("Invalid slave definition");
7835                         return -EINVAL;
7836                 }
7837                 to_free = 1;
7838         }
7839         if (snd_config_get_type(conf) != SND_CONFIG_TYPE_COMPOUND) {
7840                 SNDERR("Invalid slave definition");
7841                 err = -EINVAL;
7842                 goto _err;
7843         }
7844         va_start(args, count);
7845         for (k = 0; k < count; ++k) {
7846                 fields[k].index = va_arg(args, int);
7847                 fields[k].flags = va_arg(args, int);
7848                 fields[k].ptr = va_arg(args, void *);
7849                 fields[k].present = 0;
7850         }
7851         va_end(args);
7852         snd_config_for_each(i, next, conf) {
7853                 snd_config_t *n = snd_config_iterator_entry(i);
7854                 const char *id;
7855                 if (snd_config_get_id(n, &id) < 0)
7856                         continue;
7857                 if (strcmp(id, "comment") == 0)
7858                         continue;
7859                 if (strcmp(id, "pcm") == 0) {
7860                         if (pcm_conf != NULL)
7861                                 snd_config_delete(pcm_conf);
7862                         if ((err = snd_config_copy(&pcm_conf, n)) < 0)
7863                                 goto _err;
7864                         continue;
7865                 }
7866                 for (k = 0; k < count; ++k) {
7867                         unsigned int idx = fields[k].index;
7868                         long v;
7869                         assert(idx < SND_PCM_HW_PARAM_LAST_INTERVAL);
7870                         assert(names[idx]);
7871                         if (strcmp(id, names[idx]) != 0)
7872                                 continue;
7873                         switch (idx) {
7874                         case SND_PCM_HW_PARAM_FORMAT:
7875                         {
7876                                 snd_pcm_format_t f;
7877                                 err = snd_config_get_string(n, &str);
7878                                 if (err < 0) {
7879                                 _invalid:
7880                                         SNDERR("invalid type for %s", id);
7881                                         goto _err;
7882                                 }
7883                                 if ((fields[k].flags & SCONF_UNCHANGED) &&
7884                                     strcasecmp(str, "unchanged") == 0) {
7885                                         *(snd_pcm_format_t*)fields[k].ptr = (snd_pcm_format_t) -2;
7886                                         break;
7887                                 }
7888                                 f = snd_pcm_format_value(str);
7889                                 if (f == SND_PCM_FORMAT_UNKNOWN) {
7890                                         SNDERR("unknown format %s", str);
7891                                         err = -EINVAL;
7892                                         goto _err;
7893                                 }
7894                                 *(snd_pcm_format_t*)fields[k].ptr = f;
7895                                 break;
7896                         }
7897                         default:
7898                                 if ((fields[k].flags & SCONF_UNCHANGED)) {
7899                                         err = snd_config_get_string(n, &str);
7900                                         if (err >= 0 &&
7901                                             strcasecmp(str, "unchanged") == 0) {
7902                                                 *(int*)fields[k].ptr = -2;
7903                                                 break;
7904                                         }
7905                                 }
7906                                 err = snd_config_get_integer(n, &v);
7907                                 if (err < 0)
7908                                         goto _invalid;
7909                                 *(int*)fields[k].ptr = v;
7910                                 break;
7911                         }
7912                         fields[k].present = 1;
7913                         break;
7914                 }
7915                 if (k < count)
7916                         continue;
7917                 SNDERR("Unknown field %s", id);
7918                 err = -EINVAL;
7919                 goto _err;
7920         }
7921         if (!pcm_conf) {
7922                 SNDERR("missing field pcm");
7923                 err = -EINVAL;
7924                 goto _err;
7925         }
7926         for (k = 0; k < count; ++k) {
7927                 if ((fields[k].flags & SCONF_MANDATORY) && !fields[k].present) {
7928                         SNDERR("missing field %s", names[fields[k].index]);
7929                         err = -EINVAL;
7930                         goto _err;
7931                 }
7932         }
7933         *_pcm_conf = pcm_conf;
7934         pcm_conf = NULL;
7935         err = 0;
7936  _err:
7937         if (pcm_conf)
7938                 snd_config_delete(pcm_conf);
7939         if (to_free)
7940                 snd_config_delete(conf);
7941         return err;
7942 }
7943                 
7944 static void snd_pcm_set_ptr(snd_pcm_t *pcm, snd_pcm_rbptr_t *rbptr,
7945                             volatile snd_pcm_uframes_t *hw_ptr, int fd, off_t offset)
7946 {
7947         rbptr->master = NULL;   /* I'm master */
7948         rbptr->ptr = hw_ptr;
7949         rbptr->fd = fd;
7950         rbptr->offset = offset;
7951         if (rbptr->changed)
7952                 rbptr->changed(pcm, NULL);
7953 }
7954
7955 void snd_pcm_set_hw_ptr(snd_pcm_t *pcm, volatile snd_pcm_uframes_t *hw_ptr, int fd, off_t offset)
7956 {
7957         assert(pcm);
7958         assert(hw_ptr);
7959         snd_pcm_set_ptr(pcm, &pcm->hw, hw_ptr, fd, offset);
7960 }
7961
7962 void snd_pcm_set_appl_ptr(snd_pcm_t *pcm, volatile snd_pcm_uframes_t *appl_ptr, int fd, off_t offset)
7963 {
7964         assert(pcm);
7965         assert(appl_ptr);
7966         snd_pcm_set_ptr(pcm, &pcm->appl, appl_ptr, fd, offset);
7967 }
7968
7969 static void snd_pcm_link_ptr(snd_pcm_t *pcm, snd_pcm_rbptr_t *pcm_rbptr,
7970                              snd_pcm_t *slave, snd_pcm_rbptr_t *slave_rbptr)
7971 {
7972         snd_pcm_t **a;
7973         int idx;
7974         
7975         a = slave_rbptr->link_dst;
7976         for (idx = 0; idx < slave_rbptr->link_dst_count; idx++)
7977                 if (a[idx] == NULL) {
7978                         a[idx] = pcm;
7979                         goto __found_free_place;
7980                 }
7981         a = realloc(a, sizeof(snd_pcm_t *) * (slave_rbptr->link_dst_count + 1));
7982         if (a == NULL) {
7983                 pcm_rbptr->ptr = NULL;
7984                 pcm_rbptr->fd = -1;
7985                 pcm_rbptr->offset = 0UL;
7986                 return;
7987         }
7988         a[slave_rbptr->link_dst_count++] = pcm;
7989       __found_free_place:
7990         pcm_rbptr->master = slave_rbptr->master ? slave_rbptr->master : slave;
7991         pcm_rbptr->ptr = slave_rbptr->ptr;
7992         pcm_rbptr->fd = slave_rbptr->fd;
7993         pcm_rbptr->offset = slave_rbptr->offset;
7994         slave_rbptr->link_dst = a;
7995         if (pcm_rbptr->changed)
7996                 pcm_rbptr->changed(pcm, slave);
7997 }
7998
7999 static void snd_pcm_unlink_ptr(snd_pcm_t *pcm, snd_pcm_rbptr_t *pcm_rbptr,
8000                                snd_pcm_t *slave, snd_pcm_rbptr_t *slave_rbptr)
8001 {
8002         snd_pcm_t **a;
8003         int idx;
8004
8005         a = slave_rbptr->link_dst;
8006         for (idx = 0; idx < slave_rbptr->link_dst_count; idx++) {
8007                 if (a[idx] == pcm) {
8008                         a[idx] = NULL;
8009                         goto __found;
8010                 }
8011         }
8012         /* assert(0); */
8013         return;
8014
8015       __found:
8016         pcm_rbptr->master = NULL;
8017         pcm_rbptr->ptr = NULL;
8018         pcm_rbptr->fd = -1;
8019         pcm_rbptr->offset = 0UL;
8020         if (pcm_rbptr->changed)
8021                 pcm_rbptr->changed(pcm, slave);
8022 }
8023
8024 void snd_pcm_link_hw_ptr(snd_pcm_t *pcm, snd_pcm_t *slave)
8025 {
8026         assert(pcm);
8027         assert(slave);
8028         snd_pcm_link_ptr(pcm, &pcm->hw, slave, &slave->hw);
8029 }
8030
8031 void snd_pcm_link_appl_ptr(snd_pcm_t *pcm, snd_pcm_t *slave)
8032 {
8033         assert(pcm);
8034         assert(slave);
8035         snd_pcm_link_ptr(pcm, &pcm->appl, slave, &slave->appl);
8036 }
8037
8038 void snd_pcm_unlink_hw_ptr(snd_pcm_t *pcm, snd_pcm_t *slave)
8039 {
8040         assert(pcm);
8041         assert(slave);
8042         snd_pcm_unlink_ptr(pcm, &pcm->hw, slave, &slave->hw);
8043 }
8044
8045 void snd_pcm_unlink_appl_ptr(snd_pcm_t *pcm, snd_pcm_t *slave)
8046 {
8047         assert(pcm);
8048         assert(slave);
8049         snd_pcm_unlink_ptr(pcm, &pcm->appl, slave, &slave->appl);
8050 }
8051
8052 #endif /* DOC_HIDDEN */
8053
8054 /*
8055  *
8056  */
8057
8058 #ifndef DOC_HIDDEN
8059
8060 #ifdef USE_VERSIONED_SYMBOLS
8061
8062 #define OBSOLETE1(name, what, new) \
8063   default_symbol_version(__##name, name, new); \
8064   symbol_version(__old_##name, name, what);
8065
8066 #else
8067
8068 #define OBSOLETE1(name, what, new) \
8069   use_default_symbol_version(__##name, name, new);
8070
8071 #endif /* USE_VERSIONED_SYMBOLS */
8072
8073 #define __P_OLD_GET(pfx, name, val_type, ret_type) \
8074 EXPORT_SYMBOL ret_type pfx##name(const snd_pcm_hw_params_t *params) \
8075 { \
8076         val_type val; \
8077         if (INTERNAL(name)(params, &val) < 0) \
8078                 return 0; \
8079         return (ret_type)val; \
8080 }
8081
8082 #define __P_OLD_GET1(pfx, name, val_type, ret_type) \
8083 EXPORT_SYMBOL ret_type pfx##name(const snd_pcm_hw_params_t *params, int *dir) \
8084 { \
8085         val_type val; \
8086         if (INTERNAL(name)(params, &val, dir) < 0) \
8087                 return 0; \
8088         return (ret_type)val; \
8089 }
8090
8091 #define __OLD_GET(name, val_type, ret_type) __P_OLD_GET(__old_, name, val_type, ret_type)
8092 #define __OLD_GET1(name, val_type, ret_type) __P_OLD_GET1(__old_, name, val_type, ret_type)
8093
8094 __OLD_GET(snd_pcm_hw_params_get_access, snd_pcm_access_t, int);
8095 __OLD_GET(snd_pcm_hw_params_get_format, snd_pcm_format_t, int);
8096 __OLD_GET(snd_pcm_hw_params_get_subformat, snd_pcm_subformat_t, int);
8097 __OLD_GET(snd_pcm_hw_params_get_channels, unsigned int, int);
8098 __OLD_GET1(snd_pcm_hw_params_get_rate, unsigned int, int);
8099 __OLD_GET1(snd_pcm_hw_params_get_period_time, unsigned int, int);
8100 __OLD_GET1(snd_pcm_hw_params_get_period_size, snd_pcm_uframes_t, snd_pcm_sframes_t);
8101 __OLD_GET1(snd_pcm_hw_params_get_periods, unsigned int, int);
8102 __OLD_GET1(snd_pcm_hw_params_get_buffer_time, unsigned int, int);
8103 __OLD_GET(snd_pcm_hw_params_get_buffer_size, snd_pcm_uframes_t, snd_pcm_sframes_t);
8104 __OLD_GET1(snd_pcm_hw_params_get_tick_time, unsigned int, int);
8105
8106 __OLD_GET(snd_pcm_hw_params_get_channels_min, unsigned int, unsigned int);
8107 __OLD_GET1(snd_pcm_hw_params_get_rate_min, unsigned int, unsigned int);
8108 __OLD_GET1(snd_pcm_hw_params_get_period_time_min, unsigned int, unsigned int);
8109 __OLD_GET1(snd_pcm_hw_params_get_period_size_min, snd_pcm_uframes_t, snd_pcm_uframes_t);
8110 __OLD_GET1(snd_pcm_hw_params_get_periods_min, unsigned int, unsigned int);
8111 __OLD_GET1(snd_pcm_hw_params_get_buffer_time_min, unsigned int, unsigned int);
8112 __OLD_GET(snd_pcm_hw_params_get_buffer_size_min, snd_pcm_uframes_t, snd_pcm_uframes_t);
8113 __OLD_GET1(snd_pcm_hw_params_get_tick_time_min, unsigned int, unsigned int);
8114
8115 __OLD_GET(snd_pcm_hw_params_get_channels_max, unsigned int, unsigned int);
8116 __OLD_GET1(snd_pcm_hw_params_get_rate_max, unsigned int, unsigned int);
8117 __OLD_GET1(snd_pcm_hw_params_get_period_time_max, unsigned int, unsigned int);
8118 __OLD_GET1(snd_pcm_hw_params_get_period_size_max, snd_pcm_uframes_t, snd_pcm_uframes_t);
8119 __OLD_GET1(snd_pcm_hw_params_get_periods_max, unsigned int, unsigned int);
8120 __OLD_GET1(snd_pcm_hw_params_get_buffer_time_max, unsigned int, unsigned int);
8121 __OLD_GET(snd_pcm_hw_params_get_buffer_size_max, snd_pcm_uframes_t, snd_pcm_uframes_t);
8122 __OLD_GET1(snd_pcm_hw_params_get_tick_time_max, unsigned int, unsigned int);
8123
8124 #define __P_OLD_NEAR(pfx, name, ret_type) \
8125 EXPORT_SYMBOL ret_type pfx##name(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, ret_type val) \
8126 { \
8127         if (INTERNAL(name)(pcm, params, &val) < 0) \
8128                 return 0; \
8129         return (ret_type)val; \
8130 }
8131
8132 #define __P_OLD_NEAR1(pfx, name, ret_type) \
8133 EXPORT_SYMBOL ret_type pfx##name(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, ret_type val, int *dir) \
8134 { \
8135         if (INTERNAL(name)(pcm, params, &val, dir) < 0) \
8136                 return 0; \
8137         return (ret_type)val; \
8138 }
8139
8140 #define __OLD_NEAR(name, ret_type) __P_OLD_NEAR(__old_, name, ret_type)
8141 #define __OLD_NEAR1(name, ret_type) __P_OLD_NEAR1(__old_, name, ret_type)
8142
8143 __OLD_NEAR(snd_pcm_hw_params_set_channels_near, unsigned int);
8144 __OLD_NEAR1(snd_pcm_hw_params_set_rate_near, unsigned int);
8145 __OLD_NEAR1(snd_pcm_hw_params_set_period_time_near, unsigned int);
8146 __OLD_NEAR1(snd_pcm_hw_params_set_period_size_near, snd_pcm_uframes_t);
8147 __OLD_NEAR1(snd_pcm_hw_params_set_periods_near, unsigned int);
8148 __OLD_NEAR1(snd_pcm_hw_params_set_buffer_time_near, unsigned int);
8149 __OLD_NEAR(snd_pcm_hw_params_set_buffer_size_near, snd_pcm_uframes_t);
8150 __OLD_NEAR1(snd_pcm_hw_params_set_tick_time_near, unsigned int);
8151
8152 #define __P_OLD_SET_FL(pfx, name, ret_type) \
8153 EXPORT_SYMBOL ret_type pfx##name(snd_pcm_t *pcm, snd_pcm_hw_params_t *params) \
8154 { \
8155         ret_type val; \
8156         if (INTERNAL(name)(pcm, params, &val) < 0) \
8157                 return 0; \
8158         return (ret_type)val; \
8159 }
8160
8161 #define __P_OLD_SET_FL1(pfx, name, ret_type) \
8162 EXPORT_SYMBOL ret_type pfx##name(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, int *dir) \
8163 { \
8164         ret_type val; \
8165         if (INTERNAL(name)(pcm, params, &val, dir) < 0) \
8166                 return 0; \
8167         return (ret_type)val; \
8168 }
8169
8170 #define __OLD_SET_FL(name, ret_type) __P_OLD_SET_FL(__old_, name, ret_type)
8171 #define __OLD_SET_FL1(name, ret_type) __P_OLD_SET_FL1(__old_, name, ret_type)
8172
8173 __OLD_SET_FL(snd_pcm_hw_params_set_access_first, snd_pcm_access_t);
8174 __OLD_SET_FL(snd_pcm_hw_params_set_format_first, snd_pcm_format_t);
8175 __OLD_SET_FL(snd_pcm_hw_params_set_subformat_first, snd_pcm_subformat_t);
8176 __OLD_SET_FL(snd_pcm_hw_params_set_channels_first, unsigned int);
8177 __OLD_SET_FL1(snd_pcm_hw_params_set_rate_first, unsigned int);
8178 __OLD_SET_FL1(snd_pcm_hw_params_set_period_time_first, unsigned int);
8179 __OLD_SET_FL1(snd_pcm_hw_params_set_period_size_first, snd_pcm_uframes_t);
8180 __OLD_SET_FL1(snd_pcm_hw_params_set_periods_first, unsigned int);
8181 __OLD_SET_FL1(snd_pcm_hw_params_set_buffer_time_first, unsigned int);
8182 __OLD_SET_FL(snd_pcm_hw_params_set_buffer_size_first, snd_pcm_uframes_t);
8183 __OLD_SET_FL1(snd_pcm_hw_params_set_tick_time_first, unsigned int);
8184
8185 __OLD_SET_FL(snd_pcm_hw_params_set_access_last, snd_pcm_access_t);
8186 __OLD_SET_FL(snd_pcm_hw_params_set_format_last, snd_pcm_format_t);
8187 __OLD_SET_FL(snd_pcm_hw_params_set_subformat_last, snd_pcm_subformat_t);
8188 __OLD_SET_FL(snd_pcm_hw_params_set_channels_last, unsigned int);
8189 __OLD_SET_FL1(snd_pcm_hw_params_set_rate_last, unsigned int);
8190 __OLD_SET_FL1(snd_pcm_hw_params_set_period_time_last, unsigned int);
8191 __OLD_SET_FL1(snd_pcm_hw_params_set_period_size_last, snd_pcm_uframes_t);
8192 __OLD_SET_FL1(snd_pcm_hw_params_set_periods_last, unsigned int);
8193 __OLD_SET_FL1(snd_pcm_hw_params_set_buffer_time_last, unsigned int);
8194 __OLD_SET_FL(snd_pcm_hw_params_set_buffer_size_last, snd_pcm_uframes_t);
8195 __OLD_SET_FL1(snd_pcm_hw_params_set_tick_time_last, unsigned int);
8196
8197 #define __P_OLD_GET_SW(pfx, name, ret_type) \
8198 EXPORT_SYMBOL ret_type pfx##name(snd_pcm_sw_params_t *params) \
8199 { \
8200         ret_type val; \
8201         if (INTERNAL(name)(params, &val) < 0) \
8202                 return 0; \
8203         return (ret_type)val; \
8204 }
8205
8206 #define __OLD_GET_SW(name, ret_type) __P_OLD_GET_SW(__old_, name, ret_type)
8207
8208 __OLD_GET_SW(snd_pcm_sw_params_get_tstamp_mode, snd_pcm_tstamp_t);
8209 __OLD_GET_SW(snd_pcm_sw_params_get_sleep_min, unsigned int);
8210 __OLD_GET_SW(snd_pcm_sw_params_get_avail_min, snd_pcm_uframes_t);
8211 __OLD_GET_SW(snd_pcm_sw_params_get_xfer_align, snd_pcm_uframes_t);
8212 __OLD_GET_SW(snd_pcm_sw_params_get_start_threshold, snd_pcm_uframes_t);
8213 __OLD_GET_SW(snd_pcm_sw_params_get_stop_threshold, snd_pcm_uframes_t);
8214 __OLD_GET_SW(snd_pcm_sw_params_get_silence_threshold, snd_pcm_uframes_t);
8215 __OLD_GET_SW(snd_pcm_sw_params_get_silence_size, snd_pcm_uframes_t);
8216
8217 OBSOLETE1(snd_pcm_hw_params_get_access, ALSA_0.9, ALSA_0.9.0rc4);
8218 OBSOLETE1(snd_pcm_hw_params_set_access_first, ALSA_0.9, ALSA_0.9.0rc4);
8219 OBSOLETE1(snd_pcm_hw_params_set_access_last, ALSA_0.9, ALSA_0.9.0rc4);
8220
8221 OBSOLETE1(snd_pcm_hw_params_get_format, ALSA_0.9, ALSA_0.9.0rc4);
8222 OBSOLETE1(snd_pcm_hw_params_set_format_first, ALSA_0.9, ALSA_0.9.0rc4);
8223 OBSOLETE1(snd_pcm_hw_params_set_format_last, ALSA_0.9, ALSA_0.9.0rc4);
8224
8225 OBSOLETE1(snd_pcm_hw_params_get_subformat, ALSA_0.9, ALSA_0.9.0rc4);
8226 OBSOLETE1(snd_pcm_hw_params_set_subformat_first, ALSA_0.9, ALSA_0.9.0rc4);
8227 OBSOLETE1(snd_pcm_hw_params_set_subformat_last, ALSA_0.9, ALSA_0.9.0rc4);
8228
8229 OBSOLETE1(snd_pcm_hw_params_get_channels, ALSA_0.9, ALSA_0.9.0rc4);
8230 OBSOLETE1(snd_pcm_hw_params_get_channels_min, ALSA_0.9, ALSA_0.9.0rc4);
8231 OBSOLETE1(snd_pcm_hw_params_get_channels_max, ALSA_0.9, ALSA_0.9.0rc4);
8232 OBSOLETE1(snd_pcm_hw_params_set_channels_near, ALSA_0.9, ALSA_0.9.0rc4);
8233 OBSOLETE1(snd_pcm_hw_params_set_channels_first, ALSA_0.9, ALSA_0.9.0rc4);
8234 OBSOLETE1(snd_pcm_hw_params_set_channels_last, ALSA_0.9, ALSA_0.9.0rc4);
8235
8236 OBSOLETE1(snd_pcm_hw_params_get_rate, ALSA_0.9, ALSA_0.9.0rc4);
8237 OBSOLETE1(snd_pcm_hw_params_get_rate_min, ALSA_0.9, ALSA_0.9.0rc4);
8238 OBSOLETE1(snd_pcm_hw_params_get_rate_max, ALSA_0.9, ALSA_0.9.0rc4);
8239 OBSOLETE1(snd_pcm_hw_params_set_rate_near, ALSA_0.9, ALSA_0.9.0rc4);
8240 OBSOLETE1(snd_pcm_hw_params_set_rate_first, ALSA_0.9, ALSA_0.9.0rc4);
8241 OBSOLETE1(snd_pcm_hw_params_set_rate_last, ALSA_0.9, ALSA_0.9.0rc4);
8242
8243 OBSOLETE1(snd_pcm_hw_params_get_period_time, ALSA_0.9, ALSA_0.9.0rc4);
8244 OBSOLETE1(snd_pcm_hw_params_get_period_time_min, ALSA_0.9, ALSA_0.9.0rc4);
8245 OBSOLETE1(snd_pcm_hw_params_get_period_time_max, ALSA_0.9, ALSA_0.9.0rc4);
8246 OBSOLETE1(snd_pcm_hw_params_set_period_time_near, ALSA_0.9, ALSA_0.9.0rc4);
8247 OBSOLETE1(snd_pcm_hw_params_set_period_time_first, ALSA_0.9, ALSA_0.9.0rc4);
8248 OBSOLETE1(snd_pcm_hw_params_set_period_time_last, ALSA_0.9, ALSA_0.9.0rc4);
8249
8250 OBSOLETE1(snd_pcm_hw_params_get_period_size, ALSA_0.9, ALSA_0.9.0rc4);
8251 OBSOLETE1(snd_pcm_hw_params_get_period_size_min, ALSA_0.9, ALSA_0.9.0rc4);
8252 OBSOLETE1(snd_pcm_hw_params_get_period_size_max, ALSA_0.9, ALSA_0.9.0rc4);
8253 OBSOLETE1(snd_pcm_hw_params_set_period_size_near, ALSA_0.9, ALSA_0.9.0rc4);
8254 OBSOLETE1(snd_pcm_hw_params_set_period_size_first, ALSA_0.9, ALSA_0.9.0rc4);
8255 OBSOLETE1(snd_pcm_hw_params_set_period_size_last, ALSA_0.9, ALSA_0.9.0rc4);
8256
8257 OBSOLETE1(snd_pcm_hw_params_get_periods, ALSA_0.9, ALSA_0.9.0rc4);
8258 OBSOLETE1(snd_pcm_hw_params_get_periods_min, ALSA_0.9, ALSA_0.9.0rc4);
8259 OBSOLETE1(snd_pcm_hw_params_get_periods_max, ALSA_0.9, ALSA_0.9.0rc4);
8260 OBSOLETE1(snd_pcm_hw_params_set_periods_near, ALSA_0.9, ALSA_0.9.0rc4);
8261 OBSOLETE1(snd_pcm_hw_params_set_periods_first, ALSA_0.9, ALSA_0.9.0rc4);
8262 OBSOLETE1(snd_pcm_hw_params_set_periods_last, ALSA_0.9, ALSA_0.9.0rc4);
8263
8264 OBSOLETE1(snd_pcm_hw_params_get_buffer_time, ALSA_0.9, ALSA_0.9.0rc4);
8265 OBSOLETE1(snd_pcm_hw_params_get_buffer_time_min, ALSA_0.9, ALSA_0.9.0rc4);
8266 OBSOLETE1(snd_pcm_hw_params_get_buffer_time_max, ALSA_0.9, ALSA_0.9.0rc4);
8267 OBSOLETE1(snd_pcm_hw_params_set_buffer_time_near, ALSA_0.9, ALSA_0.9.0rc4);
8268 OBSOLETE1(snd_pcm_hw_params_set_buffer_time_first, ALSA_0.9, ALSA_0.9.0rc4);
8269 OBSOLETE1(snd_pcm_hw_params_set_buffer_time_last, ALSA_0.9, ALSA_0.9.0rc4);
8270
8271 OBSOLETE1(snd_pcm_hw_params_get_buffer_size, ALSA_0.9, ALSA_0.9.0rc4);
8272 OBSOLETE1(snd_pcm_hw_params_get_buffer_size_min, ALSA_0.9, ALSA_0.9.0rc4);
8273 OBSOLETE1(snd_pcm_hw_params_get_buffer_size_max, ALSA_0.9, ALSA_0.9.0rc4);
8274 OBSOLETE1(snd_pcm_hw_params_set_buffer_size_near, ALSA_0.9, ALSA_0.9.0rc4);
8275 OBSOLETE1(snd_pcm_hw_params_set_buffer_size_first, ALSA_0.9, ALSA_0.9.0rc4);
8276 OBSOLETE1(snd_pcm_hw_params_set_buffer_size_last, ALSA_0.9, ALSA_0.9.0rc4);
8277
8278 OBSOLETE1(snd_pcm_hw_params_get_tick_time, ALSA_0.9, ALSA_0.9.0rc4);
8279 OBSOLETE1(snd_pcm_hw_params_get_tick_time_min, ALSA_0.9, ALSA_0.9.0rc4);
8280 OBSOLETE1(snd_pcm_hw_params_get_tick_time_max, ALSA_0.9, ALSA_0.9.0rc4);
8281 OBSOLETE1(snd_pcm_hw_params_set_tick_time_near, ALSA_0.9, ALSA_0.9.0rc4);
8282 OBSOLETE1(snd_pcm_hw_params_set_tick_time_first, ALSA_0.9, ALSA_0.9.0rc4);
8283 OBSOLETE1(snd_pcm_hw_params_set_tick_time_last, ALSA_0.9, ALSA_0.9.0rc4);
8284
8285 OBSOLETE1(snd_pcm_sw_params_get_tstamp_mode, ALSA_0.9, ALSA_0.9.0rc4);
8286 OBSOLETE1(snd_pcm_sw_params_get_sleep_min, ALSA_0.9, ALSA_0.9.0rc4);
8287 OBSOLETE1(snd_pcm_sw_params_get_avail_min, ALSA_0.9, ALSA_0.9.0rc4);
8288 OBSOLETE1(snd_pcm_sw_params_get_xfer_align, ALSA_0.9, ALSA_0.9.0rc4);
8289 OBSOLETE1(snd_pcm_sw_params_get_start_threshold, ALSA_0.9, ALSA_0.9.0rc4);
8290 OBSOLETE1(snd_pcm_sw_params_get_stop_threshold, ALSA_0.9, ALSA_0.9.0rc4);
8291 OBSOLETE1(snd_pcm_sw_params_get_silence_threshold, ALSA_0.9, ALSA_0.9.0rc4);
8292 OBSOLETE1(snd_pcm_sw_params_get_silence_size, ALSA_0.9, ALSA_0.9.0rc4);
8293
8294 #endif /* DOC_HIDDEN */
8295
8296 static int chmap_equal(const snd_pcm_chmap_t *a, const snd_pcm_chmap_t *b)
8297 {
8298         if (a->channels != b->channels)
8299                 return 0;
8300         return !memcmp(a->pos, b->pos, a->channels * sizeof(a->pos[0]));
8301 }
8302
8303 /**
8304  * \!brief Query the available channel maps
8305  * \param pcm PCM handle to query
8306  * \return the NULL-terminated array of integer pointers, each of
8307  * which contains the channel map. A channel map is represented by an
8308  * integer array, beginning with the channel map type, followed by the
8309  * number of channels, and the position of each channel. Return NULL
8310  * in case of an error.
8311  *
8312  * Note: the caller is requested to release the returned value via
8313  * snd_pcm_free_chmaps().
8314  */
8315 snd_pcm_chmap_query_t **snd_pcm_query_chmaps(snd_pcm_t *pcm)
8316 {
8317         if (!pcm->ops->query_chmaps)
8318                 return NULL;
8319         return pcm->ops->query_chmaps(pcm);
8320 }
8321
8322 /**
8323  * \!brief Release the channel map array allocated via #snd_pcm_query_chmaps
8324  * \param maps the array pointer to release
8325  */
8326 void snd_pcm_free_chmaps(snd_pcm_chmap_query_t **maps)
8327 {
8328         snd_pcm_chmap_query_t **p;
8329         if (!maps)
8330                 return;
8331         for (p = maps; *p; p++)
8332                 free(*p);
8333         free(maps);
8334 }
8335
8336 /**
8337  * \!brief Get the current channel map
8338  * \param pcm PCM instance
8339  * \return the current channel map, or NULL if error
8340  *
8341  * Note: the caller is requested to release the returned value via free()
8342  */
8343 snd_pcm_chmap_t *snd_pcm_get_chmap(snd_pcm_t *pcm)
8344 {
8345         if (!pcm->ops->get_chmap)
8346                 return NULL;
8347         return pcm->ops->get_chmap(pcm);
8348 }
8349
8350 /**
8351  * \!brief Configure the current channel map
8352  * \param pcm PCM instance
8353  * \param map the channel map to write
8354  * \return zero if succeeded, or a negative error code
8355  */
8356 int snd_pcm_set_chmap(snd_pcm_t *pcm, const snd_pcm_chmap_t *map)
8357 {
8358         const snd_pcm_chmap_t *oldmap;
8359         int nochange;
8360
8361         oldmap = snd_pcm_get_chmap(pcm);
8362         nochange = (oldmap && chmap_equal(oldmap, map));
8363         free((void *)oldmap);
8364         if (nochange)
8365                 return 0;
8366
8367         if (!pcm->ops->set_chmap)
8368                 return -ENXIO;
8369         return pcm->ops->set_chmap(pcm, map);
8370 }
8371
8372 /*
8373  */
8374 #ifndef DOC_HIDDEN
8375 #define _NAME(n) [SND_CHMAP_TYPE_##n] = #n
8376 static const char *chmap_type_names[SND_CHMAP_TYPE_LAST + 1] = {
8377         _NAME(NONE), _NAME(FIXED), _NAME(VAR), _NAME(PAIRED),
8378 };
8379 #undef _NAME
8380 #endif
8381
8382 /**
8383  * \!brief Get a name string for a channel map type as query results
8384  * \param val Channel position
8385  * \return The string corresponding to the given type, or NULL
8386  */
8387 const char *snd_pcm_chmap_type_name(enum snd_pcm_chmap_type val)
8388 {
8389         if (val <= SND_CHMAP_TYPE_LAST)
8390                 return chmap_type_names[val];
8391         else
8392                 return NULL;
8393 }
8394
8395 #ifndef DOC_HIDDEN
8396 #define _NAME(n) [SND_CHMAP_##n] = #n
8397 static const char *chmap_names[SND_CHMAP_LAST + 1] = {
8398         _NAME(UNKNOWN), _NAME(NA), _NAME(MONO),
8399         _NAME(FL), _NAME(FR),
8400         _NAME(RL), _NAME(RR),
8401         _NAME(FC), _NAME(LFE),
8402         _NAME(SL), _NAME(SR),
8403         _NAME(RC), _NAME(FLC), _NAME(FRC), _NAME(RLC), _NAME(RRC),
8404         _NAME(FLW), _NAME(FRW),
8405         _NAME(FLH), _NAME(FCH), _NAME(FRH), _NAME(TC),
8406         _NAME(TFL), _NAME(TFR), _NAME(TFC),
8407         _NAME(TRL), _NAME(TRR), _NAME(TRC),
8408         _NAME(TFLC), _NAME(TFRC), _NAME(TSL), _NAME(TSR),
8409         _NAME(LLFE), _NAME(RLFE),
8410         _NAME(BC), _NAME(BLC), _NAME(BRC),
8411 };
8412 #undef _NAME
8413 #endif
8414
8415 /**
8416  * \!brief Get a name string for a standard channel map position
8417  * \param val Channel position
8418  * \return The string corresponding to the given position, or NULL
8419  */
8420 const char *snd_pcm_chmap_name(enum snd_pcm_chmap_position val)
8421 {
8422         if (val <= SND_CHMAP_LAST)
8423                 return chmap_names[val];
8424         else
8425                 return NULL;
8426 }
8427
8428 static const char *chmap_long_names[SND_CHMAP_LAST + 1] = {
8429         [SND_CHMAP_UNKNOWN] = "Unknown",
8430         [SND_CHMAP_NA] = "Unused",
8431         [SND_CHMAP_MONO] = "Mono",
8432         [SND_CHMAP_FL] = "Front Left",
8433         [SND_CHMAP_FR] = "Front Right",
8434         [SND_CHMAP_RL] = "Rear Left",
8435         [SND_CHMAP_RR] = "Rear Right",
8436         [SND_CHMAP_FC] = "Front Center",
8437         [SND_CHMAP_LFE] = "LFE",
8438         [SND_CHMAP_SL] = "Side Left",
8439         [SND_CHMAP_SR] = "Side Right",
8440         [SND_CHMAP_RC] = "Rear Center",
8441         [SND_CHMAP_FLC] = "Front Left Center",
8442         [SND_CHMAP_FRC] = "Front Right Center",
8443         [SND_CHMAP_RLC] = "Rear Left Center",
8444         [SND_CHMAP_RRC] = "Rear Right Center",
8445         [SND_CHMAP_FLW] = "Front Left Wide",
8446         [SND_CHMAP_FRW] = "Front Right Wide",
8447         [SND_CHMAP_FLH] = "Front Left High",
8448         [SND_CHMAP_FCH] = "Front Center High",
8449         [SND_CHMAP_FRH] = "Front Right High",
8450         [SND_CHMAP_TC] = "Top Center",
8451         [SND_CHMAP_TFL] = "Top Front Left",
8452         [SND_CHMAP_TFR] = "Top Front Right",
8453         [SND_CHMAP_TFC] = "Top Front Center",
8454         [SND_CHMAP_TRL] = "Top Rear Left",
8455         [SND_CHMAP_TRR] = "Top Rear Right",
8456         [SND_CHMAP_TRC] = "Top Rear Center",
8457         [SND_CHMAP_TFLC] = "Top Front Left Center",
8458         [SND_CHMAP_TFRC] = "Top Front Right Center",
8459         [SND_CHMAP_TSL] = "Top Side Left",
8460         [SND_CHMAP_TSR] = "Top Side Right",
8461         [SND_CHMAP_LLFE] = "Left LFE",
8462         [SND_CHMAP_RLFE] = "Right LFE",
8463         [SND_CHMAP_BC] = "Bottom Center",
8464         [SND_CHMAP_BLC] = "Bottom Left Center",
8465         [SND_CHMAP_BRC] = "Bottom Right Center",
8466 };
8467
8468 /**
8469  * \!brief Get a longer name string for a standard channel map position
8470  * \param val Channel position
8471  * \return The string corresponding to the given position, or NULL
8472  */
8473 const char *snd_pcm_chmap_long_name(enum snd_pcm_chmap_position val)
8474 {
8475         if (val <= SND_CHMAP_LAST)
8476                 return chmap_long_names[val];
8477         else
8478                 return NULL;
8479 }
8480
8481 /**
8482  * \!brief Print the channels in chmap on the buffer
8483  * \param map The channel map to print
8484  * \param maxlen The maximal length to write (including NUL letter)
8485  * \param buf The buffer to write
8486  * \return The actual string length or a negative error code
8487  */
8488 int snd_pcm_chmap_print(const snd_pcm_chmap_t *map, size_t maxlen, char *buf)
8489 {
8490         unsigned int i, len = 0;
8491
8492         for (i = 0; i < map->channels; i++) {
8493                 unsigned int p = map->pos[i] & SND_CHMAP_POSITION_MASK;
8494                 if (i > 0) {
8495                         len += snprintf(buf + len, maxlen - len, " ");
8496                         if (len >= maxlen)
8497                                 return -ENOMEM;
8498                 }
8499                 if (map->pos[i] & SND_CHMAP_DRIVER_SPEC)
8500                         len += snprintf(buf + len, maxlen - len, "%d", p);
8501                 else {
8502                         const char *name = chmap_names[p];
8503                         if (name)
8504                                 len += snprintf(buf + len, maxlen - len,
8505                                                 "%s", name);
8506                         else
8507                                 len += snprintf(buf + len, maxlen - len,
8508                                                 "Ch%d", p);
8509                 }
8510                 if (len >= maxlen)
8511                         return -ENOMEM;
8512                 if (map->pos[i] & SND_CHMAP_PHASE_INVERSE) {
8513                         len += snprintf(buf + len, maxlen - len, "[INV]");
8514                         if (len >= maxlen)
8515                                 return -ENOMEM;
8516                 }
8517         }
8518         return len;
8519 }
8520
8521 static int str_to_chmap(const char *str, int len)
8522 {
8523         int val;
8524         unsigned long v;
8525         char *p;
8526
8527         if (isdigit(*str)) {
8528                 v = strtoul(str, &p, 0);
8529                 if (v == ULONG_MAX)
8530                         return -1;
8531                 val = v;
8532                 val |= SND_CHMAP_DRIVER_SPEC;
8533                 str = p;
8534         } else if (!strncasecmp(str, "ch", 2)) {
8535                 v = strtoul(str + 2, &p, 0);
8536                 if (v == ULONG_MAX)
8537                         return -1;
8538                 val = v;
8539                 str = p;
8540         } else {
8541                 for (val = 0; val <= SND_CHMAP_LAST; val++) {
8542                         int slen;
8543                         assert(chmap_names[val]);
8544                         slen = strlen(chmap_names[val]);
8545                         if (slen > len)
8546                                 continue;
8547                         if (!strncasecmp(str, chmap_names[val], slen) &&
8548                             !isalpha(str[slen])) {
8549                                 str += slen;
8550                                 break;
8551                         }
8552                 }
8553                 if (val > SND_CHMAP_LAST)
8554                         return -1;
8555         }
8556         if (str && !strncasecmp(str, "[INV]", 5))
8557                 val |= SND_CHMAP_PHASE_INVERSE;
8558         return val;
8559 }
8560
8561 /**
8562  * \!brief Convert from string to channel position
8563  * \param str The string to parse
8564  * \return The channel position value or -1 as an error
8565  */
8566 unsigned int snd_pcm_chmap_from_string(const char *str)
8567 {
8568         return str_to_chmap(str, strlen(str));
8569 }
8570
8571 /**
8572  * \!brief Convert from string to channel map
8573  * \param str The string to parse
8574  * \return The channel map
8575  *
8576  * Note: the caller is requested to release the returned value via free()
8577  */
8578 snd_pcm_chmap_t *snd_pcm_chmap_parse_string(const char *str)
8579 {
8580         int i, ch = 0;
8581         int tmp_map[64];
8582         snd_pcm_chmap_t *map;
8583
8584         for (;;) {
8585                 const char *p;
8586                 int len, val;
8587
8588                 if (ch >= (int)(sizeof(tmp_map) / sizeof(tmp_map[0])))
8589                         return NULL;
8590                 for (p = str; *p && isalnum(*p); p++)
8591                         ;
8592                 len = p - str;
8593                 if (!len)
8594                         return NULL;
8595                 val = str_to_chmap(str, len);
8596                 if (val < 0)
8597                         return NULL;
8598                 str += len;
8599                 if (*str == '[') {
8600                         if (!strncmp(str, "[INV]", 5)) {
8601                                 val |= SND_CHMAP_PHASE_INVERSE;
8602                                 str += 5;
8603                         }
8604                 }
8605                 tmp_map[ch] = val;
8606                 ch++;
8607                 for (; *str && !isalnum(*str); str++)
8608                         ;
8609                 if (!*str)
8610                         break;
8611         }
8612         map = malloc(sizeof(*map) + ch * sizeof(int));
8613         if (!map)
8614                 return NULL;
8615         map->channels = ch;
8616         for (i = 0; i < ch; i++)
8617                 map->pos[i] = tmp_map[i];
8618         return map;
8619 }
8620
8621 /* copy a single channel map with the fixed type to chmap_query pointer */
8622 static int _copy_to_fixed_query_map(snd_pcm_chmap_query_t **dst,
8623                                     const snd_pcm_chmap_t *src)
8624 {
8625         *dst = malloc((src->channels + 2) * sizeof(int));
8626         if (!*dst)
8627                 return -ENOMEM;
8628         (*dst)->type = SND_CHMAP_TYPE_FIXED;
8629         memcpy(&(*dst)->map, src, (src->channels + 1) * sizeof(int));
8630         return 0;
8631 }
8632
8633 #ifndef DOC_HIDDEN
8634 /* make a chmap_query array from a single channel map */
8635 snd_pcm_chmap_query_t **
8636 _snd_pcm_make_single_query_chmaps(const snd_pcm_chmap_t *src)
8637 {
8638         snd_pcm_chmap_query_t **maps;
8639
8640         maps = calloc(2, sizeof(*maps));
8641         if (!maps)
8642                 return NULL;
8643         if (_copy_to_fixed_query_map(maps, src)) {
8644                 free(maps);
8645                 return NULL;
8646         }
8647         return maps;
8648 }
8649
8650 /* make a copy of chmap */
8651 snd_pcm_chmap_t *_snd_pcm_copy_chmap(const snd_pcm_chmap_t *src)
8652 {
8653         snd_pcm_chmap_t *map;
8654
8655         map = malloc((src->channels + 1) * sizeof(int));
8656         if (!map)
8657                 return NULL;
8658         memcpy(map, src, (src->channels + 1) * sizeof(int));
8659         return map;
8660 }
8661
8662 /* make a copy of channel maps */
8663 snd_pcm_chmap_query_t **
8664 _snd_pcm_copy_chmap_query(snd_pcm_chmap_query_t * const *src)
8665 {
8666         snd_pcm_chmap_query_t * const *p;
8667         snd_pcm_chmap_query_t **maps;
8668         int i, nums;
8669
8670         for (nums = 0, p = src; *p; p++)
8671                 nums++;
8672
8673         maps = calloc(nums + 1, sizeof(*maps));
8674         if (!maps)
8675                 return NULL;
8676         for (i = 0; i < nums; i++) {
8677                 maps[i] = malloc((src[i]->map.channels + 2) * sizeof(int));
8678                 if (!maps[i]) {
8679                         snd_pcm_free_chmaps(maps);
8680                         return NULL;
8681                 }
8682                 memcpy(maps[i], src[i], (src[i]->map.channels + 2) * sizeof(int));
8683         }
8684         return maps;
8685 }
8686
8687 /* select the channel map with the current PCM channels and make a copy */
8688 snd_pcm_chmap_t *
8689 _snd_pcm_choose_fixed_chmap(snd_pcm_t *pcm, snd_pcm_chmap_query_t * const *maps)
8690 {
8691         snd_pcm_chmap_query_t * const *p;
8692
8693         for (p = maps; *p; p++) {
8694                 if ((*p)->map.channels == pcm->channels)
8695                         return _snd_pcm_copy_chmap(&(*p)->map);
8696         }
8697         return NULL;
8698 }
8699
8700 /* make chmap_query array from the config tree;
8701  * conf must be a compound (array)
8702  */
8703 snd_pcm_chmap_query_t **
8704 _snd_pcm_parse_config_chmaps(snd_config_t *conf)
8705 {
8706         snd_pcm_chmap_t *chmap;
8707         snd_pcm_chmap_query_t **maps;
8708         snd_config_iterator_t i, next;
8709         const char *str;
8710         int nums, err;
8711
8712         if (snd_config_get_type(conf) != SND_CONFIG_TYPE_COMPOUND)
8713                 return NULL;
8714
8715         nums = 0;
8716         snd_config_for_each(i, next, conf) {
8717                 nums++;
8718         }
8719
8720         maps = calloc(nums + 1, sizeof(*maps));
8721         if (!maps)
8722                 return NULL;
8723
8724         nums = 0;
8725         snd_config_for_each(i, next, conf) {
8726                 snd_config_t *n = snd_config_iterator_entry(i);
8727                 err = snd_config_get_string(n, &str);
8728                 if (err < 0)
8729                         goto error;
8730                 chmap = snd_pcm_chmap_parse_string(str);
8731                 if (!chmap)
8732                         goto error;
8733                 if (_copy_to_fixed_query_map(maps + nums, chmap)) {
8734                         free(chmap);
8735                         goto error;
8736                 }
8737                 free(chmap);
8738                 nums++;
8739         }
8740         return maps;
8741
8742  error:
8743         snd_pcm_free_chmaps(maps);
8744         return NULL;
8745 }
8746 #endif /* DOC_HIDDEN */
8747
8748 /*
8749  * basic helpers
8750  */
8751  
8752  
8753 /**
8754  * \brief Recover the stream state from an error or suspend
8755  * \param pcm PCM handle
8756  * \param err error number
8757  * \param silent do not print error reason
8758  * \return 0 when error code was handled successfuly, otherwise a negative error code
8759  *
8760  * This a high-level helper function building on other functions.
8761  *
8762  * This functions handles -EINTR (interrupted system call),
8763  * -EPIPE (overrun or underrun) and -ESTRPIPE (stream is suspended)
8764  * error codes trying to prepare given stream for next I/O.
8765  *
8766  * Note that this function returns the original error code when it is not
8767  * handled inside this function (for example -EAGAIN is returned back).
8768  */
8769 int snd_pcm_recover(snd_pcm_t *pcm, int err, int silent)
8770 {
8771         if (err > 0)
8772                 err = -err;
8773         if (err == -EINTR)      /* nothing to do, continue */
8774                 return 0;
8775         if (err == -EPIPE) {
8776                 const char *s;
8777                 if (snd_pcm_stream(pcm) == SND_PCM_STREAM_PLAYBACK)
8778                         s = "underrun";
8779                 else
8780                         s = "overrun";
8781                 if (!silent)
8782                         SNDERR("%s occurred", s);
8783                 err = snd_pcm_prepare(pcm);
8784                 if (err < 0) {
8785                         SNDERR("cannot recovery from %s, prepare failed: %s", s, snd_strerror(err));
8786                         return err;
8787                 }
8788                 return 0;
8789         }
8790         if (err == -ESTRPIPE) {
8791                 while ((err = snd_pcm_resume(pcm)) == -EAGAIN)
8792                         /* wait until suspend flag is released */
8793                         poll(NULL, 0, 1000);
8794                 if (err < 0) {
8795                         err = snd_pcm_prepare(pcm);
8796                         if (err < 0) {
8797                                 SNDERR("cannot recovery from suspend, prepare failed: %s", snd_strerror(err));
8798                                 return err;
8799                         }
8800                 }
8801                 return 0;
8802         }
8803         return err;
8804 }
8805
8806 /**
8807  * \brief Set the hardware and software parameters in a simple way
8808  * \param pcm PCM handle
8809  * \param format required PCM format
8810  * \param access required PCM access
8811  * \param channels required PCM channels
8812  * \param rate required sample rate in Hz
8813  * \param soft_resample 0 = disallow alsa-lib resample stream, 1 = allow resampling
8814  * \param latency required overall latency in us
8815  * \return 0 on success otherwise a negative error code
8816  */
8817 int snd_pcm_set_params(snd_pcm_t *pcm,
8818                        snd_pcm_format_t format,
8819                        snd_pcm_access_t access,
8820                        unsigned int channels,
8821                        unsigned int rate,
8822                        int soft_resample,
8823                        unsigned int latency)
8824 {
8825         snd_pcm_hw_params_t params_saved, params = {0};
8826         snd_pcm_sw_params_t swparams = {0};
8827         const char *s = snd_pcm_stream_name(snd_pcm_stream(pcm));
8828         snd_pcm_uframes_t buffer_size, period_size;
8829         unsigned int rrate, period_time;
8830         int err;
8831
8832         assert(pcm);
8833         /* choose all parameters */
8834         err = snd_pcm_hw_params_any(pcm, &params);
8835         if (err < 0) {
8836                 SNDERR("Broken configuration for %s: no configurations available",
8837                        s);
8838                 return err;
8839         }
8840         /* set software resampling */
8841         err = snd_pcm_hw_params_set_rate_resample(pcm, &params, soft_resample);
8842         if (err < 0) {
8843                 SNDERR("Resampling setup failed for %s: %s",
8844                        s, snd_strerror(err));
8845                 return err;
8846         }
8847         /* set the selected read/write format */
8848         err = snd_pcm_hw_params_set_access(pcm, &params, access);
8849         if (err < 0) {
8850                 SNDERR("Access type not available for %s: %s",
8851                        s, snd_strerror(err));
8852                 return err;
8853         }
8854         /* set the sample format */
8855         err = snd_pcm_hw_params_set_format(pcm, &params, format);
8856         if (err < 0) {
8857                 SNDERR("Sample format not available for %s: %s",
8858                        s, snd_strerror(err));
8859                 return err;
8860         }
8861         /* set the count of channels */
8862         err = snd_pcm_hw_params_set_channels(pcm, &params, channels);
8863         if (err < 0) {
8864                 SNDERR("Channels count (%i) not available for %s: %s",
8865                        channels, s, snd_strerror(err));
8866                 return err;
8867         }
8868         /* set the stream rate */
8869         rrate = rate;
8870         err = INTERNAL(snd_pcm_hw_params_set_rate_near)(pcm, &params, &rrate,
8871                                                         0);
8872         if (err < 0) {
8873                 SNDERR("Rate %iHz not available for playback: %s",
8874                        rate, snd_strerror(err));
8875                 return err;
8876         }
8877         if (rrate != rate) {
8878                 SNDERR("Rate doesn't match (requested %iHz, get %iHz)",
8879                        rate, rrate);
8880                 return -EINVAL;
8881         }
8882         /* set the buffer time */
8883         params_saved = params;
8884         err = INTERNAL(snd_pcm_hw_params_set_buffer_time_near)(pcm, &params,
8885                                                         &latency, NULL);
8886         if (err < 0) {
8887                 /* error path -> set period size as first */
8888                 params = params_saved;
8889                 /* set the period time */
8890                 period_time = latency / 4;
8891                 err = INTERNAL(snd_pcm_hw_params_set_period_time_near)(pcm,
8892                                                 &params, &period_time, NULL);
8893                 if (err < 0) {
8894                         SNDERR("Unable to set period time %i for %s: %s",
8895                                period_time, s, snd_strerror(err));
8896                         return err;
8897                 }
8898                 err = INTERNAL(snd_pcm_hw_params_get_period_size)(&params,
8899                                                         &period_size, NULL);
8900                 if (err < 0) {
8901                         SNDERR("Unable to get period size for %s: %s",
8902                                                         s, snd_strerror(err));
8903                         return err;
8904                 }
8905                 buffer_size = period_size * 4;
8906                 err = INTERNAL(snd_pcm_hw_params_set_buffer_size_near)(pcm,
8907                                                         &params, &buffer_size);
8908                 if (err < 0) {
8909                         SNDERR("Unable to set buffer size %lu %s: %s",
8910                                         buffer_size, s, snd_strerror(err));
8911                         return err;
8912                 }
8913                 err = INTERNAL(snd_pcm_hw_params_get_buffer_size)(&params,
8914                                                                 &buffer_size);
8915                 if (err < 0) {
8916                         SNDERR("Unable to get buffer size for %s: %s",
8917                                s, snd_strerror(err));
8918                         return err;
8919                 }
8920         } else {
8921                 /* standard configuration buffer_time -> periods */
8922                 err = INTERNAL(snd_pcm_hw_params_get_buffer_size)(&params,
8923                                                                 &buffer_size);
8924                 if (err < 0) {
8925                         SNDERR("Unable to get buffer size for %s: %s",
8926                                                         s, snd_strerror(err));
8927                         return err;
8928                 }
8929                 err = INTERNAL(snd_pcm_hw_params_get_buffer_time)(&params,
8930                                                         &latency, NULL);
8931                 if (err < 0) {
8932                         SNDERR("Unable to get buffer time (latency) for %s: %s",
8933                                s, snd_strerror(err));
8934                         return err;
8935                 }
8936                 /* set the period time */
8937                 period_time = latency / 4;
8938                 err = INTERNAL(snd_pcm_hw_params_set_period_time_near)(pcm,
8939                                                 &params, &period_time, NULL);
8940                 if (err < 0) {
8941                         SNDERR("Unable to set period time %i for %s: %s",
8942                                period_time, s, snd_strerror(err));
8943                         return err;
8944                 }
8945                 err = INTERNAL(snd_pcm_hw_params_get_period_size)(&params,
8946                                                         &period_size, NULL);
8947                 if (err < 0) {
8948                         SNDERR("Unable to get period size for %s: %s",
8949                                s, snd_strerror(err));
8950                         return err;
8951                 }
8952         }
8953         /* write the parameters to device */
8954         err = snd_pcm_hw_params(pcm, &params);
8955         if (err < 0) {
8956                 SNDERR("Unable to set hw params for %s: %s",
8957                        s, snd_strerror(err));
8958                 return err;
8959         }
8960
8961         /* get the current swparams */
8962         err = snd_pcm_sw_params_current(pcm, &swparams);
8963         if (err < 0) {
8964                 SNDERR("Unable to determine current swparams for %s: %s",
8965                        s, snd_strerror(err));
8966                 return err;
8967         }
8968         /*
8969          * start the transfer when the buffer is almost full:
8970          * (buffer_size / avail_min) * avail_min
8971          */
8972         err = snd_pcm_sw_params_set_start_threshold(pcm, &swparams,
8973                                 (buffer_size / period_size) * period_size);
8974         if (err < 0) {
8975                 SNDERR("Unable to set start threshold mode for %s: %s",
8976                        s, snd_strerror(err));
8977                 return err;
8978         }
8979         /*
8980          * allow the transfer when at least period_size samples can be
8981          * processed
8982          */
8983         err = snd_pcm_sw_params_set_avail_min(pcm, &swparams, period_size);
8984         if (err < 0) {
8985                 SNDERR("Unable to set avail min for %s: %s",
8986                        s, snd_strerror(err));
8987                 return err;
8988         }
8989         /* write the parameters to the playback device */
8990         err = snd_pcm_sw_params(pcm, &swparams);
8991         if (err < 0) {
8992                 SNDERR("Unable to set sw params for %s: %s",
8993                        s, snd_strerror(err));
8994                 return err;
8995         }
8996         return 0;
8997 }
8998
8999 /**
9000  * \brief Get the transfer size parameters in a simple way
9001  * \param pcm PCM handle
9002  * \param buffer_size PCM ring buffer size in frames
9003  * \param period_size PCM period size in frames
9004  * \return 0 on success otherwise a negative error code
9005  */
9006 int snd_pcm_get_params(snd_pcm_t *pcm,
9007                        snd_pcm_uframes_t *buffer_size,
9008                        snd_pcm_uframes_t *period_size)
9009 {
9010         snd_pcm_hw_params_t params = {0};
9011         int err;
9012
9013         assert(pcm);
9014         err = snd_pcm_hw_params_current(pcm, &params);
9015         if (err < 0)
9016                 return err;
9017         err = INTERNAL(snd_pcm_hw_params_get_buffer_size)(&params, buffer_size);
9018         if (err < 0)
9019                 return err;
9020         return INTERNAL(snd_pcm_hw_params_get_period_size)(&params, period_size,
9021                                                            NULL);
9022 }