CRAS: DSP - Make the conversion between int16_t and float better.

Reduce rounding error by adding 0.5 before truncating to integer.

Change the constant from 32767.5 to 32768, so the division can be
optimized into multiplication by the compiler.

BUG=none
TEST=make test

Change-Id: I32ca3dd439e106203b9695bc14ab7e2bb0b9ebe4
Reviewed-on: https://gerrit.chromium.org/gerrit/39588
Commit-Ready: Dylan Reid <dgreid@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Tested-by: Dylan Reid <dgreid@chromium.org>
CRAS: jack UT - stub out get_interface.

This slipped by and was calling the actual alsa version.  That didn't
work when running unit tests from the chroot.

BUG=none
TEST=FEATURES="test" emerge-link adhd

Change-Id: Ic31ba14579c3824b158130f68a5530d56f2c41f8
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/39487
Reviewed-by: Chih-Chung Chang <chihchung@chromium.org>
Reviewed-by: Hsinyu Chao <hychao@chromium.org>
CRAS: Fix 32bit unit tests.

Make sure to use 64 bit numbers for tracking nano-seconds when running
on 23 bit machines.

BUG=none
TEST=make check on 32bit machine.

Change-Id: If9ba0c13fdc7ab45bd4226fbb388bbdbd2ebfe90
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/39582
Reviewed-by: Chih-Chung Chang <chihchung@chromium.org>
CRAS: rclient - Check for frame rate of zero.

In case an iodev doesn't have any supported formats, or that a client
sends a bogus request, the frame rate could be set to zero.  Bail
early if that happens.

BUG=none
TEST=updated rclient unittest.

Change-Id: Ia84e5a6cef444e65394b0ceda642487e1d78e15e
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: iodev - track sleep correction in iodev.

Instead of tracking the sleep correction frame count in alsa io, move it
to iodev as more iodev implementations will need to use this.

BUG=chromium-os:35272
TEST=alsa_io_unittest, and manual: cras_test_client with differnt
latencies.

Change-Id: I84406f65f59922d74c053635a43a56c693f5d77c
Signed-off-by: Dylan Reid <dgreid@chromium.or
CRAS: alsa_io - Have open/close take an iodev.

Instead of passing an alsa_io pointer to open and close, pass an iodev
pointer.  This will allow these functions to be called from the iodev
core without caring what type of iodev is being used.

BUG=chromium-os:35272
TEST=alsa_io_unittest

Change-Id: I2133b4d2ecc0be1f6b87040c3dced47e38212a15
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: alsa_io - Add get/put buffer helpers.

Pull out getting and returning mmap buffers from alsa to helper
functions.  This will aid in refactoring the logic in the read/write
functions to be alsa-independent.  Once it is alsa independent it will
be able to be reused across other types of io devices.
Rework some types so that no alsa-specific types are used.

BUG=chromium-os:35272
TEST=alsa_io_unittest

Change-Id: I81306a8aa54c599885ce6cbb5e8c1e6f7b673ed5
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: alsa_io - Remove extra audio fetch.

During playback of two streams, if one is much higher latency than the
other, there was a slight optimization to fetch the audio early if
possible.  There was a bug in that this fetched audio from the low
latency stream the first time it ran, and since the fetches don't block
unless about to underrun, it didn't save anything.  Remove the extra
fetch.

BUG=chromium-os:35272
TEST=alsa_io_unittest, plus simultaneous playback of two streams one
with 10mS latency the other with 100mS latency, ensure no dropouts.

Change-Id: I2b5196babdcb68093d6383a0c80ce77c9ff74766
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: alsa_io - factor out more alsa specifics from read/write.

Pull out checking the level and delay into functions so that the code
using the return values can be re-used for other iodevs.

BUG=chromium-os:35272
TEST=alsa_io_unittest

Change-Id: I45eae3d15566dc9892364aa5318bbbb66dc6acd8
CRAS: iodev - common time to sleep calculations.

Make the calculation of time remaining to sleep the same between input
and ouput in alsa io.

BUG=chromium-os:35272
TEST=alsa_io_unittest

Change-Id: I30ca84775c29fbd099e98ebecf59b03f01321eac
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: alsa_io - Pass iodev instead of aio.

Pass around an iodev pointer instead of an alsa_iodev pointer.  This
will allow refactoring much of this code into the iodev implementation
so it can be reused for new iodevs, such as synchronous ones and
bluetooth.

BUG=chromium-os:35272
TEST=alsa_io_unittest

Change-Id: I1641f3007e1de6a4f2a164a73aa86e84ba25c1c0
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: alsa_io - Add callbacks to iodev struct.

Add callbacks for all alsa-specific ops used from the audio callback to
struct cras_iodev.  This will allow these device specific functions to
be set differently for each typeof iodev and for the calling functions
to be moved to a common place.

BUG=chromium-os:35272
TEST=alsa_io_unittest, manually play and record audio.

Change-Id: I7c50c0e2add0c591338dd807825a52397b8a0c77
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: alsa_io - Move open/close ops to iodev base.

Prepares for moving the functions calling these ops to a common place.

BUG=none
TEST=make check

Change-Id: Id9f166e227035262ec013f9d0d2138fc60312736
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: alsa_io - Pass iodev to terminate and message functions.

Instead of taking an alsa_io take an iodev as an argument so they can be
moved to a common place as they have nothing to do with alsa.

BUG=chromium-os:35272
TEST=make check

Change-Id: I7f3c9098d2de814ebf63c29e5c0e29bff0793a5e
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: iodev - add is_open to iodev.

Add io_open to iodev and set/use if from alsa_io.  This removes the need
to check the alsa handle directly from the audio callback.

BUG=chromium-os:35272
TEST=make check

Change-Id: I2f6d7f4e15b199a35f92e05e2bab2448cf13116d
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: iodev - Add generic audio callback.

Add a callback pointer to iodev for processing audio samples, remove the
alsa-specific callback from alsa_io and use the generic iodev instead.
This will allow for the common threading code to be re-used.

BUG=chromium-os:35272
TEST=make check

Change-Id: Ib011591edef2d6b8cf2383a9ac14b6efa89e57ee
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: alsa_io - convert audio thread to take iodev as arg.

Take an iodev as an argument to the audio thread function.  This
prepares for moving the audio thread to the generic iodev code.

BUG=chromium-os:35272
TEST=make check

Change-Id: I0813395a341b5b248880e3dab2ad241ba57dcdf5
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: alsa_io - remove unused define

Change-Id: I77e0e535472729ea2f0dd77a7c718fb46562bd20
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: alsa_io - Move device settings init to open.

Move device setting initialization call into the open function.  They are
dependent on being called sequentially, no need to burden the caller
with calling both.

BUG=chromium-os:35272
TEST=make check

Change-Id: If15f46f4a41ed0814348beae48797f60fc03e54f
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: iodev - Remove extra arguments to init.

Direction needs to be set before calling init, so no need to pass it in.

BUG=chromium-os:35272
TEST=updated unit tests, play and record audio with the test client.

Change-Id: I59d2796d271023fbc264cd3e4772c7581fd93b20
Signed-off-by: Dylan Reid <dgreid@chromium.org>