2005-04-17 02:20:36 +04:00
/*
* PCM Interface - misc routines
2007-10-15 11:50:19 +04:00
* Copyright ( c ) 1998 by Jaroslav Kysela < perex @ perex . cz >
2005-04-17 02:20:36 +04:00
*
*
* This library is free software ; you can redistribute it and / or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation ; either version 2 of
* the License , or ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU Library General Public License for more details .
*
* You should have received a copy of the GNU Library General Public
* License along with this library ; if not , write to the Free Software
* Foundation , Inc . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307 USA
*
*/
# include <linux/time.h>
2011-09-22 17:34:58 +04:00
# include <linux/export.h>
2005-04-17 02:20:36 +04:00
# include <sound/core.h>
# include <sound/pcm.h>
2017-05-26 03:30:46 +03:00
# include "pcm_local.h"
2005-04-17 02:20:36 +04:00
# define SND_PCM_FORMAT_UNKNOWN (-1)
/* NOTE: "signed" prefix must be given below since the default char is
* unsigned on some architectures !
*/
struct pcm_format_data {
unsigned char width ; /* bit width */
unsigned char phys ; /* physical bit width */
signed char le ; /* 0 = big-endian, 1 = little-endian, -1 = others */
signed char signd ; /* 0 = unsigned, 1 = signed, -1 = others */
unsigned char silence [ 8 ] ; /* silence data to fill */
} ;
2011-02-14 13:00:47 +03:00
/* we do lots of calculations on snd_pcm_format_t; shut up sparse */
# define INT __force int
static struct pcm_format_data pcm_formats [ ( INT ) SNDRV_PCM_FORMAT_LAST + 1 ] = {
2005-04-17 02:20:36 +04:00
[ SNDRV_PCM_FORMAT_S8 ] = {
. width = 8 , . phys = 8 , . le = - 1 , . signd = 1 ,
. silence = { } ,
} ,
[ SNDRV_PCM_FORMAT_U8 ] = {
. width = 8 , . phys = 8 , . le = - 1 , . signd = 0 ,
. silence = { 0x80 } ,
} ,
[ SNDRV_PCM_FORMAT_S16_LE ] = {
. width = 16 , . phys = 16 , . le = 1 , . signd = 1 ,
. silence = { } ,
} ,
[ SNDRV_PCM_FORMAT_S16_BE ] = {
. width = 16 , . phys = 16 , . le = 0 , . signd = 1 ,
. silence = { } ,
} ,
[ SNDRV_PCM_FORMAT_U16_LE ] = {
. width = 16 , . phys = 16 , . le = 1 , . signd = 0 ,
. silence = { 0x00 , 0x80 } ,
} ,
[ SNDRV_PCM_FORMAT_U16_BE ] = {
. width = 16 , . phys = 16 , . le = 0 , . signd = 0 ,
. silence = { 0x80 , 0x00 } ,
} ,
[ SNDRV_PCM_FORMAT_S24_LE ] = {
. width = 24 , . phys = 32 , . le = 1 , . signd = 1 ,
. silence = { } ,
} ,
[ SNDRV_PCM_FORMAT_S24_BE ] = {
. width = 24 , . phys = 32 , . le = 0 , . signd = 1 ,
. silence = { } ,
} ,
[ SNDRV_PCM_FORMAT_U24_LE ] = {
. width = 24 , . phys = 32 , . le = 1 , . signd = 0 ,
. silence = { 0x00 , 0x00 , 0x80 } ,
} ,
[ SNDRV_PCM_FORMAT_U24_BE ] = {
. width = 24 , . phys = 32 , . le = 0 , . signd = 0 ,
2007-12-13 12:36:03 +03:00
. silence = { 0x00 , 0x80 , 0x00 , 0x00 } ,
2005-04-17 02:20:36 +04:00
} ,
[ SNDRV_PCM_FORMAT_S32_LE ] = {
. width = 32 , . phys = 32 , . le = 1 , . signd = 1 ,
. silence = { } ,
} ,
[ SNDRV_PCM_FORMAT_S32_BE ] = {
. width = 32 , . phys = 32 , . le = 0 , . signd = 1 ,
. silence = { } ,
} ,
[ SNDRV_PCM_FORMAT_U32_LE ] = {
. width = 32 , . phys = 32 , . le = 1 , . signd = 0 ,
. silence = { 0x00 , 0x00 , 0x00 , 0x80 } ,
} ,
[ SNDRV_PCM_FORMAT_U32_BE ] = {
. width = 32 , . phys = 32 , . le = 0 , . signd = 0 ,
. silence = { 0x80 , 0x00 , 0x00 , 0x00 } ,
} ,
[ SNDRV_PCM_FORMAT_FLOAT_LE ] = {
. width = 32 , . phys = 32 , . le = 1 , . signd = - 1 ,
. silence = { } ,
} ,
[ SNDRV_PCM_FORMAT_FLOAT_BE ] = {
. width = 32 , . phys = 32 , . le = 0 , . signd = - 1 ,
. silence = { } ,
} ,
[ SNDRV_PCM_FORMAT_FLOAT64_LE ] = {
. width = 64 , . phys = 64 , . le = 1 , . signd = - 1 ,
. silence = { } ,
} ,
[ SNDRV_PCM_FORMAT_FLOAT64_BE ] = {
. width = 64 , . phys = 64 , . le = 0 , . signd = - 1 ,
. silence = { } ,
} ,
[ SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE ] = {
. width = 32 , . phys = 32 , . le = 1 , . signd = - 1 ,
. silence = { } ,
} ,
[ SNDRV_PCM_FORMAT_IEC958_SUBFRAME_BE ] = {
. width = 32 , . phys = 32 , . le = 0 , . signd = - 1 ,
. silence = { } ,
} ,
[ SNDRV_PCM_FORMAT_MU_LAW ] = {
. width = 8 , . phys = 8 , . le = - 1 , . signd = - 1 ,
. silence = { 0x7f } ,
} ,
[ SNDRV_PCM_FORMAT_A_LAW ] = {
. width = 8 , . phys = 8 , . le = - 1 , . signd = - 1 ,
. silence = { 0x55 } ,
} ,
[ SNDRV_PCM_FORMAT_IMA_ADPCM ] = {
. width = 4 , . phys = 4 , . le = - 1 , . signd = - 1 ,
. silence = { } ,
} ,
2010-05-28 19:43:45 +04:00
[ SNDRV_PCM_FORMAT_G723_24 ] = {
. width = 3 , . phys = 3 , . le = - 1 , . signd = - 1 ,
. silence = { } ,
} ,
[ SNDRV_PCM_FORMAT_G723_40 ] = {
. width = 5 , . phys = 5 , . le = - 1 , . signd = - 1 ,
. silence = { } ,
} ,
2013-04-16 20:01:36 +04:00
[ SNDRV_PCM_FORMAT_DSD_U8 ] = {
. width = 8 , . phys = 8 , . le = 1 , . signd = 0 ,
2014-08-22 13:18:48 +04:00
. silence = { 0x69 } ,
2013-04-16 20:01:36 +04:00
} ,
[ SNDRV_PCM_FORMAT_DSD_U16_LE ] = {
. width = 16 , . phys = 16 , . le = 1 , . signd = 0 ,
2014-08-22 13:18:48 +04:00
. silence = { 0x69 , 0x69 } ,
2013-04-16 20:01:36 +04:00
} ,
2014-09-05 12:47:56 +04:00
[ SNDRV_PCM_FORMAT_DSD_U32_LE ] = {
. width = 32 , . phys = 32 , . le = 1 , . signd = 0 ,
. silence = { 0x69 , 0x69 , 0x69 , 0x69 } ,
} ,
2014-11-21 17:04:46 +03:00
[ SNDRV_PCM_FORMAT_DSD_U16_BE ] = {
. width = 16 , . phys = 16 , . le = 0 , . signd = 0 ,
. silence = { 0x69 , 0x69 } ,
} ,
[ SNDRV_PCM_FORMAT_DSD_U32_BE ] = {
. width = 32 , . phys = 32 , . le = 0 , . signd = 0 ,
. silence = { 0x69 , 0x69 , 0x69 , 0x69 } ,
} ,
2005-04-17 02:20:36 +04:00
/* FIXME: the following three formats are not defined properly yet */
[ SNDRV_PCM_FORMAT_MPEG ] = {
. le = - 1 , . signd = - 1 ,
} ,
[ SNDRV_PCM_FORMAT_GSM ] = {
. le = - 1 , . signd = - 1 ,
} ,
[ SNDRV_PCM_FORMAT_SPECIAL ] = {
. le = - 1 , . signd = - 1 ,
} ,
[ SNDRV_PCM_FORMAT_S24_3LE ] = {
. width = 24 , . phys = 24 , . le = 1 , . signd = 1 ,
. silence = { } ,
} ,
[ SNDRV_PCM_FORMAT_S24_3BE ] = {
. width = 24 , . phys = 24 , . le = 0 , . signd = 1 ,
. silence = { } ,
} ,
[ SNDRV_PCM_FORMAT_U24_3LE ] = {
. width = 24 , . phys = 24 , . le = 1 , . signd = 0 ,
. silence = { 0x00 , 0x00 , 0x80 } ,
} ,
[ SNDRV_PCM_FORMAT_U24_3BE ] = {
. width = 24 , . phys = 24 , . le = 0 , . signd = 0 ,
. silence = { 0x80 , 0x00 , 0x00 } ,
} ,
[ SNDRV_PCM_FORMAT_S20_3LE ] = {
. width = 20 , . phys = 24 , . le = 1 , . signd = 1 ,
. silence = { } ,
} ,
[ SNDRV_PCM_FORMAT_S20_3BE ] = {
. width = 20 , . phys = 24 , . le = 0 , . signd = 1 ,
. silence = { } ,
} ,
[ SNDRV_PCM_FORMAT_U20_3LE ] = {
. width = 20 , . phys = 24 , . le = 1 , . signd = 0 ,
. silence = { 0x00 , 0x00 , 0x08 } ,
} ,
[ SNDRV_PCM_FORMAT_U20_3BE ] = {
. width = 20 , . phys = 24 , . le = 0 , . signd = 0 ,
. silence = { 0x08 , 0x00 , 0x00 } ,
} ,
[ SNDRV_PCM_FORMAT_S18_3LE ] = {
. width = 18 , . phys = 24 , . le = 1 , . signd = 1 ,
. silence = { } ,
} ,
[ SNDRV_PCM_FORMAT_S18_3BE ] = {
. width = 18 , . phys = 24 , . le = 0 , . signd = 1 ,
. silence = { } ,
} ,
[ SNDRV_PCM_FORMAT_U18_3LE ] = {
. width = 18 , . phys = 24 , . le = 1 , . signd = 0 ,
. silence = { 0x00 , 0x00 , 0x02 } ,
} ,
[ SNDRV_PCM_FORMAT_U18_3BE ] = {
. width = 18 , . phys = 24 , . le = 0 , . signd = 0 ,
. silence = { 0x02 , 0x00 , 0x00 } ,
} ,
2010-05-28 19:43:45 +04:00
[ SNDRV_PCM_FORMAT_G723_24_1B ] = {
. width = 3 , . phys = 8 , . le = - 1 , . signd = - 1 ,
. silence = { } ,
} ,
[ SNDRV_PCM_FORMAT_G723_40_1B ] = {
. width = 5 , . phys = 8 , . le = - 1 , . signd = - 1 ,
. silence = { } ,
} ,
2005-04-17 02:20:36 +04:00
} ;
/**
* snd_pcm_format_signed - Check the PCM format is signed linear
* @ format : the format to check
*
2013-03-12 01:05:14 +04:00
* Return : 1 if the given PCM format is signed linear , 0 if unsigned
2005-04-17 02:20:36 +04:00
* linear , and a negative error code for non - linear formats .
*/
int snd_pcm_format_signed ( snd_pcm_format_t format )
{
int val ;
2011-02-14 13:00:47 +03:00
if ( ( INT ) format < 0 | | ( INT ) format > ( INT ) SNDRV_PCM_FORMAT_LAST )
2005-04-17 02:20:36 +04:00
return - EINVAL ;
2011-02-14 13:00:47 +03:00
if ( ( val = pcm_formats [ ( INT ) format ] . signd ) < 0 )
2005-04-17 02:20:36 +04:00
return - EINVAL ;
return val ;
}
2006-04-28 17:13:40 +04:00
EXPORT_SYMBOL ( snd_pcm_format_signed ) ;
2005-04-17 02:20:36 +04:00
/**
* snd_pcm_format_unsigned - Check the PCM format is unsigned linear
* @ format : the format to check
*
2013-03-12 01:05:14 +04:00
* Return : 1 if the given PCM format is unsigned linear , 0 if signed
2005-04-17 02:20:36 +04:00
* linear , and a negative error code for non - linear formats .
*/
int snd_pcm_format_unsigned ( snd_pcm_format_t format )
{
int val ;
val = snd_pcm_format_signed ( format ) ;
if ( val < 0 )
return val ;
return ! val ;
}
2006-04-28 17:13:40 +04:00
EXPORT_SYMBOL ( snd_pcm_format_unsigned ) ;
2005-04-17 02:20:36 +04:00
/**
* snd_pcm_format_linear - Check the PCM format is linear
* @ format : the format to check
*
2013-03-12 01:05:14 +04:00
* Return : 1 if the given PCM format is linear , 0 if not .
2005-04-17 02:20:36 +04:00
*/
int snd_pcm_format_linear ( snd_pcm_format_t format )
{
return snd_pcm_format_signed ( format ) > = 0 ;
}
2006-04-28 17:13:40 +04:00
EXPORT_SYMBOL ( snd_pcm_format_linear ) ;
2005-04-17 02:20:36 +04:00
/**
* snd_pcm_format_little_endian - Check the PCM format is little - endian
* @ format : the format to check
*
2013-03-12 01:05:14 +04:00
* Return : 1 if the given PCM format is little - endian , 0 if
2005-04-17 02:20:36 +04:00
* big - endian , or a negative error code if endian not specified .
*/
int snd_pcm_format_little_endian ( snd_pcm_format_t format )
{
int val ;
2011-02-14 13:00:47 +03:00
if ( ( INT ) format < 0 | | ( INT ) format > ( INT ) SNDRV_PCM_FORMAT_LAST )
2005-04-17 02:20:36 +04:00
return - EINVAL ;
2011-02-14 13:00:47 +03:00
if ( ( val = pcm_formats [ ( INT ) format ] . le ) < 0 )
2005-04-17 02:20:36 +04:00
return - EINVAL ;
return val ;
}
2006-04-28 17:13:40 +04:00
EXPORT_SYMBOL ( snd_pcm_format_little_endian ) ;
2005-04-17 02:20:36 +04:00
/**
* snd_pcm_format_big_endian - Check the PCM format is big - endian
* @ format : the format to check
*
2013-03-12 01:05:14 +04:00
* Return : 1 if the given PCM format is big - endian , 0 if
2005-04-17 02:20:36 +04:00
* little - endian , or a negative error code if endian not specified .
*/
int snd_pcm_format_big_endian ( snd_pcm_format_t format )
{
int val ;
val = snd_pcm_format_little_endian ( format ) ;
if ( val < 0 )
return val ;
return ! val ;
}
2006-04-28 17:13:40 +04:00
EXPORT_SYMBOL ( snd_pcm_format_big_endian ) ;
2005-04-17 02:20:36 +04:00
/**
* snd_pcm_format_width - return the bit - width of the format
* @ format : the format to check
*
2013-03-12 01:05:14 +04:00
* Return : The bit - width of the format , or a negative error code
2005-04-17 02:20:36 +04:00
* if unknown format .
*/
int snd_pcm_format_width ( snd_pcm_format_t format )
{
int val ;
2011-02-14 13:00:47 +03:00
if ( ( INT ) format < 0 | | ( INT ) format > ( INT ) SNDRV_PCM_FORMAT_LAST )
2005-04-17 02:20:36 +04:00
return - EINVAL ;
2011-02-14 13:00:47 +03:00
if ( ( val = pcm_formats [ ( INT ) format ] . width ) = = 0 )
2005-04-17 02:20:36 +04:00
return - EINVAL ;
return val ;
}
2006-04-28 17:13:40 +04:00
EXPORT_SYMBOL ( snd_pcm_format_width ) ;
2005-04-17 02:20:36 +04:00
/**
* snd_pcm_format_physical_width - return the physical bit - width of the format
* @ format : the format to check
*
2013-03-12 01:05:14 +04:00
* Return : The physical bit - width of the format , or a negative error code
2005-04-17 02:20:36 +04:00
* if unknown format .
*/
int snd_pcm_format_physical_width ( snd_pcm_format_t format )
{
int val ;
2011-02-14 13:00:47 +03:00
if ( ( INT ) format < 0 | | ( INT ) format > ( INT ) SNDRV_PCM_FORMAT_LAST )
2005-04-17 02:20:36 +04:00
return - EINVAL ;
2011-02-14 13:00:47 +03:00
if ( ( val = pcm_formats [ ( INT ) format ] . phys ) = = 0 )
2005-04-17 02:20:36 +04:00
return - EINVAL ;
return val ;
}
2006-04-28 17:13:40 +04:00
EXPORT_SYMBOL ( snd_pcm_format_physical_width ) ;
2005-04-17 02:20:36 +04:00
/**
* snd_pcm_format_size - return the byte size of samples on the given format
* @ format : the format to check
2008-10-17 22:28:11 +04:00
* @ samples : sampling rate
2005-04-17 02:20:36 +04:00
*
2013-03-12 01:05:14 +04:00
* Return : The byte size of the given samples for the format , or a
2005-04-17 02:20:36 +04:00
* negative error code if unknown format .
*/
ssize_t snd_pcm_format_size ( snd_pcm_format_t format , size_t samples )
{
int phys_width = snd_pcm_format_physical_width ( format ) ;
if ( phys_width < 0 )
return - EINVAL ;
return samples * phys_width / 8 ;
}
2006-04-28 17:13:40 +04:00
EXPORT_SYMBOL ( snd_pcm_format_size ) ;
2005-04-17 02:20:36 +04:00
/**
* snd_pcm_format_silence_64 - return the silent data in 8 bytes array
* @ format : the format to check
*
2013-03-12 01:05:14 +04:00
* Return : The format pattern to fill or % NULL if error .
2005-04-17 02:20:36 +04:00
*/
const unsigned char * snd_pcm_format_silence_64 ( snd_pcm_format_t format )
{
2011-02-14 13:00:47 +03:00
if ( ( INT ) format < 0 | | ( INT ) format > ( INT ) SNDRV_PCM_FORMAT_LAST )
2005-04-17 02:20:36 +04:00
return NULL ;
2011-02-14 13:00:47 +03:00
if ( ! pcm_formats [ ( INT ) format ] . phys )
2005-04-17 02:20:36 +04:00
return NULL ;
2011-02-14 13:00:47 +03:00
return pcm_formats [ ( INT ) format ] . silence ;
2005-04-17 02:20:36 +04:00
}
2006-04-28 17:13:40 +04:00
EXPORT_SYMBOL ( snd_pcm_format_silence_64 ) ;
2005-04-17 02:20:36 +04:00
/**
* snd_pcm_format_set_silence - set the silence data on the buffer
* @ format : the PCM format
* @ data : the buffer pointer
* @ samples : the number of samples to set silence
*
* Sets the silence data on the buffer for the given samples .
*
2013-03-12 01:05:14 +04:00
* Return : Zero if successful , or a negative error code on failure .
2005-04-17 02:20:36 +04:00
*/
int snd_pcm_format_set_silence ( snd_pcm_format_t format , void * data , unsigned int samples )
{
int width ;
unsigned char * dst , * pat ;
2011-02-14 13:00:47 +03:00
if ( ( INT ) format < 0 | | ( INT ) format > ( INT ) SNDRV_PCM_FORMAT_LAST )
2005-04-17 02:20:36 +04:00
return - EINVAL ;
if ( samples = = 0 )
return 0 ;
2011-02-14 13:00:47 +03:00
width = pcm_formats [ ( INT ) format ] . phys ; /* physical width */
pat = pcm_formats [ ( INT ) format ] . silence ;
2005-04-17 02:20:36 +04:00
if ( ! width )
return - EINVAL ;
/* signed or 1 byte data */
2011-02-14 13:00:47 +03:00
if ( pcm_formats [ ( INT ) format ] . signd = = 1 | | width < = 8 ) {
2005-04-17 02:20:36 +04:00
unsigned int bytes = samples * width / 8 ;
memset ( data , * pat , bytes ) ;
return 0 ;
}
/* non-zero samples, fill using a loop */
width / = 8 ;
dst = data ;
#if 0
while ( samples - - ) {
memcpy ( dst , pat , width ) ;
dst + = width ;
}
# else
/* a bit optimization for constant width */
switch ( width ) {
case 2 :
while ( samples - - ) {
memcpy ( dst , pat , 2 ) ;
dst + = 2 ;
}
break ;
case 3 :
while ( samples - - ) {
memcpy ( dst , pat , 3 ) ;
dst + = 3 ;
}
break ;
case 4 :
while ( samples - - ) {
memcpy ( dst , pat , 4 ) ;
dst + = 4 ;
}
break ;
case 8 :
while ( samples - - ) {
memcpy ( dst , pat , 8 ) ;
dst + = 8 ;
}
break ;
}
# endif
return 0 ;
}
2006-04-28 17:13:40 +04:00
EXPORT_SYMBOL ( snd_pcm_format_set_silence ) ;
2005-04-17 02:20:36 +04:00
/**
* snd_pcm_limit_hw_rates - determine rate_min / rate_max fields
* @ runtime : the runtime instance
*
* Determines the rate_min and rate_max fields from the rates bits of
* the given runtime - > hw .
*
2013-03-12 01:05:14 +04:00
* Return : Zero if successful .
2005-04-17 02:20:36 +04:00
*/
2005-11-17 15:59:38 +03:00
int snd_pcm_limit_hw_rates ( struct snd_pcm_runtime * runtime )
2005-04-17 02:20:36 +04:00
{
int i ;
2007-08-13 19:38:54 +04:00
for ( i = 0 ; i < ( int ) snd_pcm_known_rates . count ; i + + ) {
2005-04-17 02:20:36 +04:00
if ( runtime - > hw . rates & ( 1 < < i ) ) {
2007-08-13 19:38:54 +04:00
runtime - > hw . rate_min = snd_pcm_known_rates . list [ i ] ;
2005-04-17 02:20:36 +04:00
break ;
}
}
2007-08-13 19:38:54 +04:00
for ( i = ( int ) snd_pcm_known_rates . count - 1 ; i > = 0 ; i - - ) {
2005-04-17 02:20:36 +04:00
if ( runtime - > hw . rates & ( 1 < < i ) ) {
2007-08-13 19:38:54 +04:00
runtime - > hw . rate_max = snd_pcm_known_rates . list [ i ] ;
2005-04-17 02:20:36 +04:00
break ;
}
}
return 0 ;
}
2006-04-28 17:13:40 +04:00
EXPORT_SYMBOL ( snd_pcm_limit_hw_rates ) ;
2007-08-13 19:40:54 +04:00
/**
* snd_pcm_rate_to_rate_bit - converts sample rate to SNDRV_PCM_RATE_xxx bit
* @ rate : the sample rate to convert
*
2013-03-12 01:05:14 +04:00
* Return : The SNDRV_PCM_RATE_xxx flag that corresponds to the given rate , or
2007-08-13 19:40:54 +04:00
* SNDRV_PCM_RATE_KNOT for an unknown rate .
*/
unsigned int snd_pcm_rate_to_rate_bit ( unsigned int rate )
{
unsigned int i ;
for ( i = 0 ; i < snd_pcm_known_rates . count ; i + + )
if ( snd_pcm_known_rates . list [ i ] = = rate )
return 1u < < i ;
return SNDRV_PCM_RATE_KNOT ;
}
EXPORT_SYMBOL ( snd_pcm_rate_to_rate_bit ) ;
2012-06-15 19:35:28 +04:00
/**
* snd_pcm_rate_bit_to_rate - converts SNDRV_PCM_RATE_xxx bit to sample rate
* @ rate_bit : the rate bit to convert
*
2013-03-12 01:05:14 +04:00
* Return : The sample rate that corresponds to the given SNDRV_PCM_RATE_xxx flag
* or 0 for an unknown rate bit .
2012-06-15 19:35:28 +04:00
*/
unsigned int snd_pcm_rate_bit_to_rate ( unsigned int rate_bit )
{
unsigned int i ;
for ( i = 0 ; i < snd_pcm_known_rates . count ; i + + )
if ( ( 1u < < i ) = = rate_bit )
return snd_pcm_known_rates . list [ i ] ;
return 0 ;
}
EXPORT_SYMBOL ( snd_pcm_rate_bit_to_rate ) ;
2014-01-11 13:24:43 +04:00
static unsigned int snd_pcm_rate_mask_sanitize ( unsigned int rates )
{
if ( rates & SNDRV_PCM_RATE_CONTINUOUS )
return SNDRV_PCM_RATE_CONTINUOUS ;
else if ( rates & SNDRV_PCM_RATE_KNOT )
return SNDRV_PCM_RATE_KNOT ;
return rates ;
}
/**
* snd_pcm_rate_mask_intersect - computes the intersection between two rate masks
* @ rates_a : The first rate mask
* @ rates_b : The second rate mask
*
* This function computes the rates that are supported by both rate masks passed
* to the function . It will take care of the special handling of
* SNDRV_PCM_RATE_CONTINUOUS and SNDRV_PCM_RATE_KNOT .
*
* Return : A rate mask containing the rates that are supported by both rates_a
* and rates_b .
*/
unsigned int snd_pcm_rate_mask_intersect ( unsigned int rates_a ,
unsigned int rates_b )
{
rates_a = snd_pcm_rate_mask_sanitize ( rates_a ) ;
rates_b = snd_pcm_rate_mask_sanitize ( rates_b ) ;
if ( rates_a & SNDRV_PCM_RATE_CONTINUOUS )
return rates_b ;
else if ( rates_b & SNDRV_PCM_RATE_CONTINUOUS )
return rates_a ;
else if ( rates_a & SNDRV_PCM_RATE_KNOT )
return rates_b ;
else if ( rates_b & SNDRV_PCM_RATE_KNOT )
return rates_a ;
return rates_a & rates_b ;
}
EXPORT_SYMBOL_GPL ( snd_pcm_rate_mask_intersect ) ;
2016-01-15 11:13:10 +03:00
/**
* snd_pcm_rate_range_to_bits - converts rate range to SNDRV_PCM_RATE_xxx bit
* @ rate_min : the minimum sample rate
* @ rate_max : the maximum sample rate
*
* This function has an implicit assumption : the rates in the given range have
* only the pre - defined rates like 44100 or 16000.
*
* Return : The SNDRV_PCM_RATE_xxx flag that corresponds to the given rate range ,
* or SNDRV_PCM_RATE_KNOT for an unknown range .
*/
unsigned int snd_pcm_rate_range_to_bits ( unsigned int rate_min ,
unsigned int rate_max )
{
unsigned int rates = 0 ;
int i ;
for ( i = 0 ; i < snd_pcm_known_rates . count ; i + + ) {
if ( snd_pcm_known_rates . list [ i ] > = rate_min
& & snd_pcm_known_rates . list [ i ] < = rate_max )
rates | = 1 < < i ;
}
if ( ! rates )
rates = SNDRV_PCM_RATE_KNOT ;
return rates ;
}
EXPORT_SYMBOL_GPL ( snd_pcm_rate_range_to_bits ) ;