2021-12-24 07:43:20 +00:00
|
|
|
/**
|
|
|
|
* Looking Glass
|
2022-01-05 08:42:46 +00:00
|
|
|
* Copyright © 2017-2022 The Looking Glass Authors
|
2021-12-24 07:43:20 +00:00
|
|
|
* https://looking-glass.io
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU 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 General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., 59
|
|
|
|
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _H_I_AUDIODEV_
|
|
|
|
#define _H_I_AUDIODEV_
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
2022-01-06 12:49:20 +00:00
|
|
|
#include <stddef.h>
|
2021-12-24 07:43:20 +00:00
|
|
|
|
2022-01-18 23:29:49 +00:00
|
|
|
typedef int (*LG_AudioPullFn)(uint8_t * dst, int frames);
|
|
|
|
typedef void (*LG_AudioPushFn)(uint8_t * src, int frames);
|
2022-01-17 22:02:44 +00:00
|
|
|
|
2021-12-24 07:43:20 +00:00
|
|
|
struct LG_AudioDevOps
|
|
|
|
{
|
|
|
|
/* internal name of the audio for debugging */
|
|
|
|
const char * name;
|
|
|
|
|
|
|
|
/* called very early to allow for option registration, optional */
|
|
|
|
void (*earlyInit)(void);
|
|
|
|
|
|
|
|
/* called to initialize the audio backend */
|
|
|
|
bool (*init)(void);
|
|
|
|
|
|
|
|
/* final free */
|
|
|
|
void (*free)(void);
|
|
|
|
|
2022-01-06 11:47:22 +00:00
|
|
|
struct
|
|
|
|
{
|
2022-01-17 22:02:44 +00:00
|
|
|
/* setup the stream for playback but don't start it yet
|
[client] audio: adjust playback speed to match audio device clock
This change is based on the techniques described in [1] and [2].
The input audio stream from Spice is not synchronised to the audio playback
device. While the input and output may be both nominally running at 48 kHz,
when compared against each other, they will differ by a tiny fraction of a
percent. Given enough time (typically on the order of a few hours), this
will result in the ring buffer becoming completely full or completely
empty. It will stay in this state permanently, periodically resulting in
glitches as the buffer repeatedly underruns or overruns.
To address this, adjust the speed of the received data to match the rate at
which it is being consumed by the audio device. This will result in a
slight pitch shift, but the changes should be small and smooth enough that
this is unnoticeable to the user.
The process works roughly as follows:
1. Every time audio data is received from Spice, or consumed by the audio
device, sample the current time. These are fed into a pair of delay
locked loops to produce smoothed approximations of the two clocks.
2. Compute the difference between the two clocks and compare this against
the target latency to produce an error value. This error value will be
quite stable during normal operation, but can change quite rapidly due
to external factors, particularly at the start of playback. To smooth
out any sudden changes in playback speed, which would be noticeable to
the user, this value is also filtered through another delay locked loop.
3. Feed this error value into a PI controller to produce a ratio value.
This is the target playback speed in order to bring the error value
towards zero.
4. Resample the input audio using the computed ratio to apply the speed
change. The output of the resampler is what is ultimately inserted into
the ring buffer for consumption by the audio device.
Since this process targets a specific latency value, rather than simply
trying to rate match the input and output, it also has the effect of
'correcting' latency issues. If a high latency application (such as a media
player) is already running, the time between requesting the start of
playback and the audio device actually starting to consume samples can be
very high, easily in the hundreds of milliseconds. The changes here will
automatically adjust the playback speed over the course of a few minutes to
bring the latency back down to the target value.
[1] https://kokkinizita.linuxaudio.org/papers/adapt-resamp.pdf
[2] https://kokkinizita.linuxaudio.org/papers/usingdll.pdf
2022-01-26 20:55:24 +00:00
|
|
|
* Note: the pull function returns f32 samples
|
2022-01-06 11:47:22 +00:00
|
|
|
*/
|
2022-02-20 21:27:22 +00:00
|
|
|
void (*setup)(int channels, int sampleRate, int requestedPeriodFrames,
|
|
|
|
int * maxPeriodFrames, int * startFrames, LG_AudioPullFn pullFn);
|
2021-12-24 07:43:20 +00:00
|
|
|
|
2022-02-05 09:53:06 +00:00
|
|
|
/* called when there is data available to start playback */
|
|
|
|
void (*start)(void);
|
2021-12-24 07:43:20 +00:00
|
|
|
|
2022-01-06 11:47:22 +00:00
|
|
|
/* called when SPICE reports the audio stream has stopped */
|
|
|
|
void (*stop)(void);
|
2021-12-25 07:55:09 +00:00
|
|
|
|
2022-01-06 11:47:22 +00:00
|
|
|
/* [optional] called to set the volume of the channels */
|
|
|
|
void (*volume)(int channels, const uint16_t volume[]);
|
2021-12-25 07:55:09 +00:00
|
|
|
|
2022-01-06 11:47:22 +00:00
|
|
|
/* [optional] called to set muting of the output */
|
|
|
|
void (*mute)(bool mute);
|
2022-01-17 11:13:41 +00:00
|
|
|
|
|
|
|
/* return the current total playback latency in microseconds */
|
|
|
|
uint64_t (*latency)(void);
|
2022-01-06 11:47:22 +00:00
|
|
|
}
|
|
|
|
playback;
|
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
/* start the record stream
|
|
|
|
* Note: currently SPICE only supports S16 samples so always assume so
|
|
|
|
*/
|
2022-01-17 22:02:44 +00:00
|
|
|
void (*start)(int channels, int sampleRate, LG_AudioPushFn pushFn);
|
2022-01-06 11:47:22 +00:00
|
|
|
|
|
|
|
/* called when SPICE reports the audio stream has stopped */
|
|
|
|
void (*stop)(void);
|
2021-12-24 07:43:20 +00:00
|
|
|
|
2022-01-06 11:47:22 +00:00
|
|
|
/* [optional] called to set the volume of the channels */
|
|
|
|
void (*volume)(int channels, const uint16_t volume[]);
|
|
|
|
|
|
|
|
/* [optional] called to set muting of the input */
|
|
|
|
void (*mute)(bool mute);
|
|
|
|
}
|
|
|
|
record;
|
|
|
|
};
|
2021-12-24 07:43:20 +00:00
|
|
|
|
|
|
|
#endif
|