alsa-lib/src/pcm/interval_inline.h

160 lines
3.7 KiB
C
Raw Normal View History

2000-12-21 20:44:10 +00:00
/*
* Interval inlines
* Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
*
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
2000-12-21 20:44:10 +00:00
* 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 Lesser General Public License for more details.
2000-12-21 20:44:10 +00:00
*
* You should have received a copy of the GNU Lesser General Public
2000-12-21 20:44:10 +00:00
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2000-12-21 20:44:10 +00:00
*
*/
2001-03-29 17:50:28 +00:00
#define INTERVAL_INLINE static inline
2000-12-21 20:44:10 +00:00
INTERVAL_INLINE void snd_interval_any(snd_interval_t *i)
2000-12-21 20:44:10 +00:00
{
i->min = 0;
i->openmin = 0;
2000-12-21 20:44:10 +00:00
i->max = UINT_MAX;
i->openmax = 0;
i->integer = 0;
i->empty = 0;
}
INTERVAL_INLINE void snd_interval_none(snd_interval_t *i)
{
i->empty = 1;
2000-12-21 20:44:10 +00:00
}
INTERVAL_INLINE int snd_interval_checkempty(const snd_interval_t *i)
2000-12-23 10:14:15 +00:00
{
return (i->min > i->max ||
(i->min == i->max && (i->openmin || i->openmax)));
}
INTERVAL_INLINE int snd_interval_empty(const snd_interval_t *i)
2000-12-21 20:44:10 +00:00
{
return i->empty;
}
INTERVAL_INLINE int snd_interval_single(const snd_interval_t *i)
2000-12-21 20:44:10 +00:00
{
assert(!snd_interval_empty(i));
2000-12-21 20:44:10 +00:00
return (i->min == i->max ||
pcm: interval: Interpret (x x+1] correctly and return x+1 Without this change an interval of (x x+1] will be interpreted as an empty interval but the right value would be x+1. This leads to a failing snd_pcm_hw_params() call which returns -EINVAL. An example issue log is given in the following: snd_pcm_hw_params failed with err -22 (Invalid argument) ACCESS: MMAP_NONINTERLEAVED FORMAT: S16_LE SUBFORMAT: STD SAMPLE_BITS: 16 FRAME_BITS: 16 CHANNELS: 1 RATE: 16000 PERIOD_TIME: (15999 16000] PERIOD_SIZE: (255 256] PERIOD_BYTES: (510 512] PERIODS: [2 3) BUFFER_TIME: 32000 BUFFER_SIZE: 512 BUFFER_BYTES: 1024 In case of (x x+1) we have to interpret it anyway as a single value of x to compensate rounding issues. For example the period size will result in an interval of (352 353) when the period time is 16ms and the sample rate 22050 Hz (16ms * 22,05 kHz = 352,8 frames). But 352 has to be chosen to allow a buffer size of 705 (32ms * 22,05 kHz = 705,6 frames) which has to be >= 2x period size to avoid Xruns. The buffer size will not end up with an interval of (705 706) simular to the period size because snd_pcm_rate_hw_refine_cchange() calls snd_interval_floor() for the buffer size. Therefore this value will be interpreted as an integer interval instead of a real interval further on. This issue seems to exist since the change of 9bb985c38 ("pcm: snd_interval_refine_first/last: exclude value only if also excluded before") Signed-off-by: Timo Wischer <twischer@de.adit-jv.com> Signed-off-by: Jaroslav Kysela <perex@perex.cz>
2018-10-18 13:33:24 +02:00
(i->min + 1 == i->max && (i->openmin || i->openmax)));
2000-12-21 20:44:10 +00:00
}
INTERVAL_INLINE int snd_interval_value(const snd_interval_t *i)
2000-12-21 20:44:10 +00:00
{
assert(snd_interval_single(i));
pcm: interval: Interpret (x x+1] correctly and return x+1 Without this change an interval of (x x+1] will be interpreted as an empty interval but the right value would be x+1. This leads to a failing snd_pcm_hw_params() call which returns -EINVAL. An example issue log is given in the following: snd_pcm_hw_params failed with err -22 (Invalid argument) ACCESS: MMAP_NONINTERLEAVED FORMAT: S16_LE SUBFORMAT: STD SAMPLE_BITS: 16 FRAME_BITS: 16 CHANNELS: 1 RATE: 16000 PERIOD_TIME: (15999 16000] PERIOD_SIZE: (255 256] PERIOD_BYTES: (510 512] PERIODS: [2 3) BUFFER_TIME: 32000 BUFFER_SIZE: 512 BUFFER_BYTES: 1024 In case of (x x+1) we have to interpret it anyway as a single value of x to compensate rounding issues. For example the period size will result in an interval of (352 353) when the period time is 16ms and the sample rate 22050 Hz (16ms * 22,05 kHz = 352,8 frames). But 352 has to be chosen to allow a buffer size of 705 (32ms * 22,05 kHz = 705,6 frames) which has to be >= 2x period size to avoid Xruns. The buffer size will not end up with an interval of (705 706) simular to the period size because snd_pcm_rate_hw_refine_cchange() calls snd_interval_floor() for the buffer size. Therefore this value will be interpreted as an integer interval instead of a real interval further on. This issue seems to exist since the change of 9bb985c38 ("pcm: snd_interval_refine_first/last: exclude value only if also excluded before") Signed-off-by: Timo Wischer <twischer@de.adit-jv.com> Signed-off-by: Jaroslav Kysela <perex@perex.cz>
2018-10-18 13:33:24 +02:00
if (i->openmin && !i->openmax)
return i->max;
2000-12-21 20:44:10 +00:00
return i->min;
}
INTERVAL_INLINE void snd_interval_set_value(snd_interval_t *i, unsigned int val)
{
i->openmax = i->openmin = 0;
i->min = i->max = val;
i->integer = 0;
i->empty = 0;
}
INTERVAL_INLINE int snd_interval_min(const snd_interval_t *i)
2000-12-21 20:44:10 +00:00
{
assert(!snd_interval_empty(i));
2000-12-21 20:44:10 +00:00
return i->min;
}
INTERVAL_INLINE int snd_interval_max(const snd_interval_t *i)
2000-12-21 20:44:10 +00:00
{
assert(!snd_interval_empty(i));
return i->max;
2000-12-21 20:44:10 +00:00
}
2003-02-17 10:21:24 +00:00
INTERVAL_INLINE void snd_interval_set_minmax(snd_interval_t *i, unsigned int min, unsigned int max)
{
i->openmax = i->openmin = 0;
i->min = min;
i->max = max;
i->integer = 0;
i->empty = 0;
}
INTERVAL_INLINE int snd_interval_test(const snd_interval_t *i, unsigned int val)
2000-12-21 20:44:10 +00:00
{
return !((i->min > val || (i->min == val && i->openmin) ||
i->max < val || (i->max == val && i->openmax)));
}
INTERVAL_INLINE void snd_interval_copy(snd_interval_t *d, const snd_interval_t *s)
2000-12-21 20:44:10 +00:00
{
*d = *s;
}
INTERVAL_INLINE int snd_interval_setinteger(snd_interval_t *i)
{
if (i->integer)
return 0;
if (i->openmin && i->openmax && i->min == i->max)
return -EINVAL;
i->integer = 1;
return 1;
}
INTERVAL_INLINE void snd_interval_floor(snd_interval_t *i)
2000-12-21 20:44:10 +00:00
{
if (i->integer || snd_interval_empty(i))
return;
i->openmin = 0;
2001-01-19 18:28:50 +00:00
if (i->openmax) {
i->max--;
i->openmax = 0;
}
i->integer = 1;
}
INTERVAL_INLINE void snd_interval_unfloor(snd_interval_t *i)
2001-01-19 18:28:50 +00:00
{
if (snd_interval_empty(i))
2001-01-19 18:28:50 +00:00
return;
if (i->max == UINT_MAX)
return;
if (i->openmax)
return;
i->max++;
i->openmax = 1;
i->integer = 0;
}
INTERVAL_INLINE int snd_interval_always_eq(const snd_interval_t *i1, const snd_interval_t *i2)
{
return snd_interval_single(i1) && snd_interval_single(i2) &&
snd_interval_value(i1) == snd_interval_value(i2);
2000-12-21 20:44:10 +00:00
}
INTERVAL_INLINE int snd_interval_never_eq(const snd_interval_t *i1, const snd_interval_t *i2)
2000-12-21 20:44:10 +00:00
{
return (i1->max < i2->min ||
(i1->max == i2->min &&
(i1->openmax || i1->openmin)) ||
i1->min > i2->max ||
(i1->min == i2->max &&
(i1->openmin || i2->openmax)));
2000-12-21 20:44:10 +00:00
}