Das Prinzip Hoffnung hat diesmal gewirkt ... und eröffnet ein neues Spielfeld!
NeuerTagNeuesGlück-Grüsse
Simon
Hallo Simon,Daihedz hat geschrieben: ↑22.08.2020, 22:55
Messwerte:
Als Beispiel sei ein konkretes Setup gewählt, in welchem als Ausgangs-Soundkarte eine RME HDSP 9632 verbaut ist. Beispielhaft soll nun Playhrt mit folgender Befehlszeile gestartet werden:
$ playhrt …. -b0 -c256 -e0 -k8 -M -s96000 …
Mit -b0 errechnet playhrt autonom den möglichst kleinsten Eingangspuffer, -c256 setzt den HW-Puffer der Soundkarte auf 256Byte, -e0 ist gleichbedeutend mit --extra-bytes-per-second=0, -k8 definiert die Anzahl Audiokanäle und -s96000 definiert die Rate. Diese Befehlszeile mit b0, d.h. ohne extra-bytes, führt zum sofortigen Abbruch.
Das weitere Experimentieren mit den extra-bytes legt offen, dass auf diesem System zwischen -e-135 und -e136 der kritische Umschlagspunkt liegt:
$ playhrt …. -b-135 -c256 -e-135 -k8 -M -s96000 …
befüllt die Puffer wesentlich schneller, als sie verbraucht werden, und erwirkt aufrund der Überfüllung einen Abbruch der Wiedergabe schon nach 17 Sekunden.
$ playhrt …. -b-136 -c256 -e-136 -k8 -M -s96000 …
befüllt die Puffer bloss ein klein wenig zu langsam, als sie verbraucht werden, und erwirkt aufgrund der Entleerung einen Abbruch der Wiedergabe nach 758 Sekunden.
Diese Werte sind nicht genau reproduzierbar. Denn der playhrt taktende Quarz auf dem Motherboard und der Quarz auf der Soundkarte driften nach jeweils eigener Dynamik erwartungsgemäss hin- und her. Unter der Annahme, dass das System dennoch einigermassen stabil bleibt, wäre somit der Sweet-Spot für die extra-bytes mit -136 gefunden.
Die einstellbare HW-Puffergrösse dieser spezifischen Soundkarte liegt nun zwischen 256Bytes und 16384Bytes. Unter der Annahme einer linearen Beziehung zwischen der Funktionsdauer und der HW-Puffergrösse lässt sich nun die stabile Funktionsdauer entsprechend und approximativ wie folgt erhöhen:
256Bytes - 12 Minuten
1024Bytes - 48 Minuten
4096Bytes - 3 Stunden
16384Bytes - 12 Stunden
Pragmatisch wird nun in den meisten Fällen wohl eine Einstellung des HW-Puffers zwischen 2048Bytes und 8192 gewählt werden. Passt schon ... Aber ganz befriedigend ist diese Schicksalsergebenheit dennoch nicht, und insbesondere dann nicht, wenn, aus welchem Grund auch immer, kleine Puffer eingestellt werden sollten.
Ich teste gerne die Extremvarianten aus, um zu schauen, ob die Software unter allen Umständen korrekt funktioniert. In besagtem Beispiel sind die 256Bytes deshalb gewählt, da dies der minimal einstellbare Wert für die HDSP ist. Was ich nicht publiziert habe, ist der Umstand, dass playhrt in diesem Testlauf mit -n6000 aufgerufen wird. Dieses bewusst extreme Setup läuft nota bene mit meinem externen DDS-Servo prima und stabil. Ohne nicht so ganz ...frankl hat geschrieben: ↑24.08.2020, 01:37 256 Bytes wäre nicht ausreichend als Hardware-Puffer. Die Zahl im `--hw-buffer`/`-c` Argument zählt Frames (= 1 Sample pro Kanal, bei Dir also vermutlich 8*4 = 32 Bytes). In der genannten Einstellung schreibt `playhrt` 1000 Mal je 96 Frames (=3072 Bytes) pro Sekunde in den Hardware-Buffer, also jeweils 37.5% der Puffergröße. Das ist wirklich sehr knapp bemessen. Warum hast Du ein Problem damit, einen größeren Buffer zu wählen, etwa 4096 Frames?
Die Deklaration von --extra-bps mittels Dezimalzahlen wäre somit eine dritte, mir bislang unbekannte Option, um das Auftreten von xruns weiter hinauszuzögern. Das inhärente Problem des suzzessiv voll- oder lerrlaufenden Speichers mit konsekutivem Absturz im Fall von -M wird jedoch auch damit nicht grundsätzlich behoben.frankl hat geschrieben: ↑24.08.2020, 01:37 Deine Beispielzahlen klingen doch eigentlich nach sehr gleichmäßigen Clocks und einem sehr präzisen Timing. Die `--extra-bytes-per-second` sind übrigens eine reine Rechengröße, mit deren Hilfe das Zeitintervall zwischen den Schreibvorgängen berechnet wird. Das Argument muss keine ganze Zahl sein, Du könntest also auch mal `-e -135.9` probieren.
Selbstverständlich bin ich zunächst einmal der Dokumentation gefolgt. Aber die ausgegebenen Werte scheinen in Deinem System etwas konsistenter zu sein als in meinem, sonst hättest Du sicherlich längst etwas an der Implementation geändert: Auf meinem System geschehen Dinge wie z.B. je nach Parametrisierung eine Empfehlung, die --extra-bps auf 2147483648 (sic!) einzustellen ... Oder aber, ich starte mit 16kB HW-Puffer bei -n1500 und -e-100 (Umschlagpunkt wäre bei mir bei -135/-136) und erhalte die Empfehlung, für den nächsten Run den Wert auf 30 einzustellen. Was sicherlich in die Irre führt ... Oder wiederum Start mit denselben Parametern (16k/-n1500), ausser diesmal mit -e-50. Die Ausgabe zeigt während 23" eine stabile (!) Puffergrösse an, danach stürzt playhrt ohne weitere Empfehlung ab ... Solcherlei Erfahrungen erinnern an Blindflug mit defekter Instrumentation. Und deshalb habe ich ja erst angefangen, es lebe die Selbsthilfe, nach Alternativen zu suchen, um das Ding einigermassen stabil hinzukriegen. Und erst die händische Abfrage der /proc/asound/... - Dateien brachten etwas konzisere Hinweise, wie die --extra-bps einigermassen funktional einzustellen seien.frankl hat geschrieben: ↑24.08.2020, 01:37 Bist Du der Dokumentation gefolgt und hast `playhrt` zuerst mit doppeltem `--verbose` Argument gestartet? Wenn ein over- oder under-run drohen, dann ändert `playhrt` einmal selbst den Wert von `--extra-bytes-per-second` und schreibt einen Vorschlag raus, wie man den Wert in Zukunft setzen sollte.
Nach wenigen Iterationen sollte sich so meist ein gut passender Wert finden lassen.
Habe ich alles gemacht. Und es stürzt weiter munter ab. Deshalb sei die sarkastische Frage erlaubt: was sind "gute Parameter"? Wenn das Setup erst nach 3 Stunden, oder erst nach 24 Stunden abstürzt?
Ditto meine Replik in derselben Logik: Ja und wenn dann der freie Teil des Hardware-Puffers ganz gross geworden ist ... was passiert dann?
Code: Alles auswählen
/*
playhrt.c Copyright frankl 2013-2016
Copyright Andree Buschmann 2020
This file is part of frankl's stereo utilities and was reworked by Andree Buschmann.
See the file License.txt of the distribution and http://www.gnu.org/licenses/gpl.txt
for license details.
*/
#include "version.h"
#include "net.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <alsa/asoundlib.h>
#include "cprefresh.h"
/* help page */
/* vim hint to remove resp. add quotes:
s/^"\(.*\)\\n"$/\1/
s/.*$/"\0\\n"/
*/
void usage( ) {
fprintf(stderr,
"playhrt (version %s of frankl's stereo utilities", VERSION);
#ifdef ALSANC
fprintf(stderr, ", with alsa-lib patch");
#endif
fprintf(stderr, ", reworked by Andree Buschmann");
fprintf(stderr, ", with PI control for clock deviation");
fprintf(stderr, ")\nUSAGE:\n");
fprintf(stderr,
"\n"
" playhrt [options] \n"
"\n"
" This program reads raw(!) stereo audio data from stdin, a file or the \n"
" network and plays it on a local (ALSA) sound device. \n"
"\n"
" The program repeats in a given number of loops per second: reading\n"
" a chunk of input data, preparing data for the audio driver, then it\n"
" sleeps until a specific instant of time and after wakeup it hands data\n"
" to the audio driver. In contrast to other player programs this is done\n"
" with a very precise timing such that no buffers underrun or overrun and\n"
" no reading or writing of data is blocking. Furthermore, the data are\n"
" refreshed in RAM directly before copying them to the audio driver.\n"
"\n"
" The Linux kernel needs the highres-timer functionality enabled (on most\n"
" systems this is the case).\n"
"\n"
" This reworked version only allows writes input data directly to the\n"
" memory of the audio driver (mmap mode).\n"
"\n"
" USAGE HINTS\n"
" \n"
" It is recommended to give this program a high priority and not to run\n"
" too many other things on the same computer during playback. A high\n"
" priority can be specified with the 'chrt' command:\n"
"\n"
" chrt -f 70 playhrt .....\n"
"\n"
" (Depending on the configuration of your computer you may need root\n"
" privileges for this, in that case use 'sudo chrt -f 99 playhrt ....' \n"
" or give 'chrt' setuid permissions.)\n"
"\n"
" While running this program the computer should run as few other things\n"
" as possible. In particular we recommend to generate the input data\n"
" on a different computer and to send them via the network to 'playhrt'\n"
" using the program 'bufhrt' which is also contained in this package. \n"
" \n"
" OPTIONS\n"
"\n"
" --host=hostname, -r hostname\n"
" the host from which to receive the data , given by name or\n"
" ip-address.\n"
"\n"
" --port=portnumber, -p portnumber\n"
" the port number on the remote host from which to receive data.\n"
"\n"
" --stdin, -S\n"
" read data from stdin (instead of --host and --port).\n"
"\n"
" --device=alsaname, -d alsaname\n"
" the name of the sound device. A typical name is 'hw:0,0', maybe\n"
" use 'aplay -l' to find out the correct numbers. It is recommended\n"
" to use the hardware devices 'hw:...' if possible.\n"
"\n"
" --sample-rate=intval, -s intval\n"
" the sample rate of the audio data. Default is 44100 as on CDs.\n"
"\n"
" --sample-format=formatstring, -f formatstring\n"
" the format of the samples in the audio data. Currently recognised\n"
" are 'S16_LE' (the sample format on CDs), 'S24_LE' \n"
" (signed integer data with 24 bits packed into 32 bit words, used by\n"
" many DACs), 'S24_3LE' (also 24 bit integers but only using 3 bytes\n"
" per sample), 'S32_LE' (true 32 bit signed integer samples).\n"
" Default is 'S16_LE'.\n"
"\n"
" --number-channels=intval, -k intval\n"
" the number of channels in the (interleaved) audio stream. The \n"
" default is 2 (stereo).\n"
"\n"
" --loops-per-second=intval, -n intval\n"
" the number of loops per second in which 'playhrt' reads some\n"
" data from the network into a buffer, sleeps until a precise\n"
" moment and then writes a chunk of data to the sound device. \n"
" Typical values would be 1000 or 2000. Default is 1000.\n"
"\n"
" --non-blocking-write, -N\n"
" write data to sound device in a non-blocking fashion. This can\n"
" improve sound quality, but the timing must be very precise.\n"
"\n"
" --hw-buffer=intval, -c intval\n"
" the buffer size (number of frames) used on the sound device.\n"
" It may be worth to experiment a bit with this,\n"
" in particular to try some smaller values. When 'playhrt' is\n"
" called with --verbose it should report on the range allowed by\n"
" the device. Default is 16384 (but there are devices where this\n"
" is not valid).\n"
" \n"
" --in-net-buffer-size=intval, -K intval\n"
" when reading from the network this allows to set the buffer\n"
" size for the incoming data. This is for finetuning only, normally\n"
" the operating system chooses sizes to guarantee constant data\n"
" flow. The actual fill of the buffer during playback can be checked\n"
" with 'netstat -tpn', it can be up to twice as big as the given\n"
" intval.\n"
"\n"
" --sleep=intval, -D intval\n"
" causes playhrt to sleep for intval microseconds (1/1000000 sec)\n"
" after opening the sound device and before starting playback.\n"
" This may sometimes be useful to give other programs time to \n"
" fill the input buffer of playhrt. Default is no sleep.\n"
"\n"
" --verbose, -v\n"
" print some information during startup and operation.\n"
" This option can be given twice for more output about the auto-\n"
" matic speed control and availability of the audio buffer.\n"
"\n"
" --version, -V\n"
" print information about the version of the program and abort.\n"
"\n"
" --help, -h\n"
" print this help page and abort.\n"
"\n"
" EXAMPLES\n"
"\n"
" We read from myserver on port 5123 stereo data in 32-bit integer\n"
" format with a sample rate of 192000. We want to run 1000 loops per \n"
" second (this is in particular a good choice for USB devices), our sound\n"
" device is 'hw:0,0' and we want to write non-blocking to the device:\n"
"\n"
" playhrt --host=myserver --port=5123 \\\n"
" --loops-per-second=1000 \\\n"
" --device=hw:0,0 --sample-rate=192000 --sample-format=S32_LE \\\n"
" --non-blocking --verbose \n"
"\n"
" To play a local CD quality flac file 'music.flac' you need another \n"
" program to unpack the raw audio data. In this example we use 'sox':\n"
"\n"
" sox musik.flac -t raw - | playhrt --stdin \\\n"
" --loops-per-second=1000 --device=hw:0,0 --sample-rate=44100 \\\n"
" --sample-format=S16_LE --non-blocking --verbose \n"
"\n"
" ADJUSTING SPEED\n"
"\n"
" This version of playhrt is automatically adjusting the speed of\n"
" writing the data to the hardware buffer. This is done via measuring\n"
" the space left in the hardware buffer and tuning the interval time\n"
" until the next data write occurs. The targeted value is hw-buffer/2.\n"
" \n"
" The automatic adjustment is implemented as PI-control which allows\n"
" playhrt to adjust to fixed and variable deviation of the local clock\n"
" against the consuming clock (typically a DAC).\n"
"\n"
);
}
int main(int argc, char *argv[])
{
int sfd, readbytes, verbose, nrchannels, startcount, sumavg, innetbufsize;
long loopspersec, sleep, nsec, extransec, count, avgav;
long long bytecount;
void *iptr;
struct timespec mtime;
struct timespec mtimecheck;
snd_pcm_t *pcm_handle;
snd_pcm_hw_params_t *hwparams;
snd_pcm_sw_params_t *swparams;
snd_pcm_format_t format;
char *host, *port, *pcm_name;
int optc, nonblock, rate, bytespersample, bytesperframe;
snd_pcm_uframes_t hwbufsize, offset, frames;
snd_pcm_sframes_t avail;
const snd_pcm_channel_area_t *areas;
/* define variables and default for PI control */
#define LOOPS_AVG 16 /* amount of averaged buffer measurement */
#define LOOPS_CADENCE 4000 /* measure each LOOPS_CADENCE loops */
double bufavg = 0;
double buferr = 0;
double buferr_i = 0;
double Ta = 0.0; /* will be calculated later */
double Kp = 1.0; /* value based on tests */
double Ki = 0.05; /* value based on tests */
/**********************************************************************/
/* read and set parameters */
/**********************************************************************/
/* read command line options */
static struct option longoptions[] = {
{"host", required_argument, 0, 'r' },
{"port", required_argument, 0, 'p' },
{"stdin", no_argument, 0, 'S' },
{"loops-per-second", required_argument, 0, 'n' },
{"sample-rate", required_argument, 0, 's' },
{"sample-format", required_argument, 0, 'f' },
{"number-channels", required_argument, 0, 'k' },
{"hw-buffer", required_argument, 0, 'c' },
{"mmap", no_argument, 0, 'M' },
{"device", required_argument, 0, 'd' },
{"sleep", required_argument, 0, 'D' },
{"in-net-buffer-size", required_argument, 0, 'K' },
{"non-blocking-write", no_argument, 0, 'N' },
{"verbose", no_argument, 0, 'v' },
{"version", no_argument, 0, 'V' },
{"help", no_argument, 0, 'h' },
{0, 0, 0, 0 }
};
if (argc == 1) {
usage();
exit(0);
}
/* set defaults */
host = NULL;
port = NULL;
loopspersec = 1000;
rate = 44100;
format = SND_PCM_FORMAT_S16_LE;
bytespersample = 2;
hwbufsize = 16384;
pcm_name = NULL;
sfd = -1;
nrchannels = 2;
extransec = 0;
sleep = 0;
nonblock = 0;
innetbufsize = 0;
verbose = 0;
sumavg = 0;
buferr_i = 0;
bytecount = 0;
/* read parameters */
while ((optc = getopt_long(argc, argv, "r:p:Sn:s:f:k:c:Md:D:K:NvVh", longoptions, &optind)) != -1) {
switch (optc) {
case 'r':
host = optarg;
break;
case 'p':
port = optarg;
break;
case 'S':
sfd = 0;
break;
case 'n':
loopspersec = atoi(optarg);
break;
case 's':
rate = atoi(optarg);
break;
case 'f':
if (strcmp(optarg, "S16_LE" )==0) {
format = SND_PCM_FORMAT_S16_LE;
bytespersample = 2;
} else if (strcmp(optarg, "S24_LE" )==0) {
format = SND_PCM_FORMAT_S24_LE;
bytespersample = 4;
} else if (strcmp(optarg, "S24_3LE")==0) {
format = SND_PCM_FORMAT_S24_3LE;
bytespersample = 3;
} else if (strcmp(optarg, "S32_LE" )==0) {
format = SND_PCM_FORMAT_S32_LE;
bytespersample = 4;
} else {
fprintf(stderr, "playhrt: Error. Sample format %s not recognized.\n", optarg);
exit(1);
}
break;
case 'k':
nrchannels = atoi(optarg);
break;
case 'c':
hwbufsize = atoi(optarg);
break;
case 'M':
/* ignore, just kept for compatibility */
break;
case 'd':
pcm_name = optarg;
break;
case 'D':
sleep = atoi(optarg);
break;
case 'K':
innetbufsize = atoi(optarg);
if (innetbufsize != 0 && innetbufsize < 128)
innetbufsize = 128;
break;
case 'N':
nonblock = 1;
break;
case 'v':
verbose += 1;
break;
case 'V':
fprintf(stderr, "playhrt (version %s of frankl's stereo utilities", VERSION);
#ifdef ALSANC
fprintf(stderr, ", with alsa-lib patch");
#endif
fprintf(stderr, ", reworked by Andree Buschmann");
fprintf(stderr, ", with PI control for clock deviation)\n");
exit(0);
default:
usage();
exit(2);
}
}
/**********************************************************************/
/* calculate and check values from given parameters */
/**********************************************************************/
/* calculate some values from the parameters */
bytesperframe = bytespersample*nrchannels; /* bytes per frame */
frames = rate/loopspersec; /* frames per loop */
nsec = (int) (1000000000/loopspersec); /* compute nanoseconds per loop (wrt local clock) */
Ta = (1.0*LOOPS_CADENCE)/loopspersec; /* delta T seconds */
Ta = (Ki*Ta>0.2) ? 0.2/Ki : Ta; /* limit Ta to avoid oscallation */
/* set hwbuffer to a multiple of frames per loop (needed for mmap!) */
hwbufsize = hwbufsize - (hwbufsize % frames);
/* amount of loops to fill half buffer */
startcount = hwbufsize/(2*frames);
/* check some arguments and set some parameters */
if ((host == NULL || port == NULL) && sfd < 0) {
fprintf(stderr, "playhrt: Error. Must specify --host and --port or --stdin.\n");
exit(3);
}
/**********************************************************************/
/* show playhrt configuration */
/**********************************************************************/
/* show configuration */
if (verbose) {
fprintf(stderr, "playhrt: Version %s\n", VERSION);
fprintf(stderr, "playhrt: Using mmap access.\n");
fprintf(stderr, "playhrt: Step size is %ld nsec.\n", nsec);
fprintf(stderr, "playhrt: %d channels with %d bytes per sample at %d Hz\n", nrchannels, bytespersample, rate);
}
/**********************************************************************/
/* setup network connection */
/**********************************************************************/
/* setup network connection */
if (host != NULL && port != NULL) {
sfd = fd_net(host, port);
if (innetbufsize != 0) {
if (setsockopt(sfd, SOL_SOCKET, SO_RCVBUF, (void*)&innetbufsize, sizeof(int)) < 0) {
fprintf(stderr, "playhrt: Error setting buffer size for network socket to %d.\n", innetbufsize);
exit(4);
}
}
}
/**********************************************************************/
/* setup sound device */
/**********************************************************************/
/* setup sound device */
snd_pcm_hw_params_malloc(&hwparams);
if (snd_pcm_open(&pcm_handle, pcm_name, SND_PCM_STREAM_PLAYBACK, 0) < 0) {
fprintf(stderr, "playhrt: Error opening PCM device %s\n", pcm_name);
exit(5);
}
if (nonblock) {
if (snd_pcm_nonblock(pcm_handle, 1) < 0) {
fprintf(stderr, "playhrt: Error setting non-block mode.\n");
exit(6);
} else if (verbose) {
fprintf(stderr, "playhrt: Using card in non-block mode.\n");
}
}
if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) {
fprintf(stderr, "playhrt: Error configuring this PCM device.\n");
exit(7);
}
if (snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_MMAP_INTERLEAVED) < 0) {
fprintf(stderr, "playhrt: Error setting MMAP access.\n");
exit(8);
}
if (snd_pcm_hw_params_set_format(pcm_handle, hwparams, format) < 0) {
fprintf(stderr, "playhrt: Error setting format.\n");
exit(9);
}
if (snd_pcm_hw_params_set_rate(pcm_handle, hwparams, rate, 0) < 0) {
fprintf(stderr, "playhrt: Error setting rate.\n");
exit(10);
}
if (snd_pcm_hw_params_set_channels(pcm_handle, hwparams, nrchannels) < 0) {
fprintf(stderr, "playhrt: Error setting channels to %d.\n", nrchannels);
exit(11);
}
if (verbose) {
snd_pcm_uframes_t min=1, max=100000000;
snd_pcm_hw_params_set_buffer_size_minmax(pcm_handle, hwparams, &min, &max);
fprintf(stderr, "playhrt: Min and max buffer size of device %ld .. %ld - ", min, max);
}
if (snd_pcm_hw_params_set_buffer_size(pcm_handle, hwparams, hwbufsize) < 0) {
fprintf(stderr, "\nplayhrt: Error setting buffersize to %ld.\n", hwbufsize);
exit(12);
}
snd_pcm_hw_params_get_buffer_size(hwparams, &hwbufsize);
if (verbose) {
fprintf(stderr, "using %ld.\n", hwbufsize);
}
if (snd_pcm_hw_params(pcm_handle, hwparams) < 0) {
fprintf(stderr, "playhrt: Error setting HW params.\n");
exit(13);
}
snd_pcm_hw_params_free(hwparams);
if (snd_pcm_sw_params_malloc (&swparams) < 0) {
fprintf(stderr, "playhrt: Error allocating SW params.\n");
exit(14);
}
if (snd_pcm_sw_params_current(pcm_handle, swparams) < 0) {
fprintf(stderr, "playhrt: Error getting current SW params.\n");
exit(15);
}
if (snd_pcm_sw_params_set_start_threshold(pcm_handle, swparams, hwbufsize/2) < 0) {
fprintf(stderr, "playhrt: Error setting start threshold.\n");
exit(16);
}
if (snd_pcm_sw_params(pcm_handle, swparams) < 0) {
fprintf(stderr, "playhrt: Error applying SW params.\n");
exit(17);
}
snd_pcm_sw_params_free (swparams);
/**********************************************************************/
/* sleep for defined amount of time to allow source to fill buffer */
/**********************************************************************/
/* short sleep to allow input to fill buffer */
if (sleep > 0) {
mtime.tv_sec = sleep/1000000;
mtime.tv_nsec = 1000*(sleep - mtime.tv_sec*1000000);
nanosleep(&mtime, NULL);
}
/* get time */
if (clock_gettime(CLOCK_MONOTONIC, &mtime) < 0) {
fprintf(stderr, "playhrt: Error getting monotonic clock.\n");
exit(18);
}
if (verbose)
fprintf(stderr, "playhrt: Start process (%ld sec %ld nsec).\n", mtime.tv_sec, mtime.tv_nsec);
/**********************************************************************/
/* main loop */
/**********************************************************************/
for (count=1; 1; count++) {
/* start playing when half of hwbuffer is filled */
if (count == startcount) {
snd_pcm_start(pcm_handle);
if (verbose)
if (count == startcount) {
clock_gettime(CLOCK_MONOTONIC, &mtimecheck);
fprintf(stderr, "playhrt: Start playback (%ld sec %ld nsec).\n",
mtimecheck.tv_sec, mtimecheck.tv_nsec);
}
}
/* read amount of frames which can be written to hardware buffer */
avail = snd_pcm_avail(pcm_handle);
if (avail < 0) {
fprintf(stderr, "playhrt: Error on snd_pcm_avail(): %ld.\n", avail);
exit(19);
}
/* get address for mmap access */
if (snd_pcm_mmap_begin(pcm_handle, &areas, &offset, &frames) < 0) {
fprintf(stderr, "playhrt: Error getting mmap address.\n");
exit(20);
}
/**********************************************************************/
/* automatic rate adaption */
/**********************************************************************/
/* start measurement of buffer level when LOOPS_CADENCE loops were done */
if (count > startcount && (count+LOOPS_AVG) % LOOPS_CADENCE == 0) {
sumavg = LOOPS_AVG;
avgav = 0;
}
/* add up buffer level for an amount of LOOPS_AVG measurements */
if (sumavg) {
avgav += avail;
if (sumavg == 1) {
bufavg = (double)avgav/LOOPS_AVG; /* average buffer level */
buferr = bufavg - hwbufsize/2; /* error against target (hwbufsize/2) */
buferr_i = buferr_i + buferr; /* integrated error */
/* calculate amount of time to be added to default step time */
/* to overall match the local clock to the outgoing clock */
extransec = (long)(-(Kp * buferr + Ki * Ta * buferr_i) + 0.5);
nsec = (int)(1000000000/loopspersec + extransec);
if (verbose > 1) {
double deviation = nsec / (1000000000.0/loopspersec);
deviation = (deviation > 1) ? (deviation-1) : (deviation-1);
fprintf(stderr, "playhrt: (%ld sec) buf: %5.1f e: %4.1f ei: %4.1f dt: %3ld ns (%1.4f%%)\n",
mtime.tv_sec, bufavg, buferr, buferr_i, extransec, deviation*100);
}
}
sumavg--;
}
/**********************************************************************/
/* read data */
/**********************************************************************/
iptr = areas[0].addr + offset * bytesperframe;
/* memclean(iptr, frames * bytesperframe); commented out to save some CPU-time */
/* in --mmap mode we read directly into mmaped space without internal buffer */
readbytes = read(sfd, iptr, frames * bytesperframe);
/**********************************************************************/
/* calcute next wakeup */
/**********************************************************************/
/* compute time for next wakeup */
mtime.tv_nsec += nsec;
if (mtime.tv_nsec > 999999999) {
mtime.tv_nsec -= 1000000000;
mtime.tv_sec++;
}
/**********************************************************************/
/* refresh buffer, sleep until defined wakeup, read/write data */
/**********************************************************************/
refreshmem(iptr, readbytes);
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &mtime, NULL);
refreshmem(iptr, readbytes);
snd_pcm_mmap_commit(pcm_handle, offset, frames);
bytecount += readbytes;
if (readbytes == 0) /* done */
break;
}
/**********************************************************************/
/* playhrt end, cleanup */
/**********************************************************************/
/* cleanup network connection and sound device */
close(sfd);
snd_pcm_drain(pcm_handle);
snd_pcm_close(pcm_handle);
if (verbose) {
fprintf(stderr, "playhrt: Loops: %ld, bytes: %lld. \n", count, bytecount);
}
return 0;
}
Sehr schön, das ist damit die dritte Umgebung, in der die Regelung funktioniert.Pittiplatsch hat geschrieben: ↑30.08.2020, 22:19Nachdem ich mit Deiner Aenderung gebaut habe funktonierte es sofort ohne aussetzer!
Dann passt es ja vielleicht ganz gut, dass ich auch Frank´s resample_soxr ein wenig umgeräumt und entschlackt habe. Es wird nur noch stdin/stdout unterstützt, volrace und Frank´s spezielle memory refresh/clean Aufrufe sind entfernt. Durch das Entfernen der memrefresh() und cleanmem() Aufrufe ist der Code bei mir etwa 30-40% schneller geworden. Und dabei war der Code vorher schon wesentlich schneller als sox.Pittiplatsch hat geschrieben: ↑30.08.2020, 22:19Das ganze ist eine wirklich hervorragende Spielwiese um mit sample rate conversion herumzuspielen
Code: Alles auswählen
/*
resample_soxr.c Copyright frankl 2018
Copyright Andree Buschmann 2020
This file is part of frankl's stereo utilities.
See the file License.txt of the distribution and
http://www.gnu.org/licenses/gpl.txt for license details.
Compile with
gcc -o resample_soxr -O2 resample_soxr.c -lsoxr -lsndfile -lrt
*/
#include "version.h"
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <sys/types.h>
#include <soxr.h>
/* help page */
/* vim hint to remove resp. add quotes:
s/^"\(.*\)\\n"$/\1/
s/.*$/"\0\\n"/
*/
void usage( ) {
fprintf(stderr, "resample_soxr (version %s of frankl's stereo utilities", VERSION);
fprintf(stderr, ", reworked by Andree Buschmann");
fprintf(stderr, ")\n\nUSAGE:\n");
fprintf(stderr,
"\n"
" resample_soxr [options] \n"
"\n"
" By default this program works as a resampling filter for stereo audio \n"
" streams in raw double format (in some programs denoted FLOAT64_LE).\n"
" Here 'filter' means that input comes from stdin and output is\n"
" written to stdout. Use pipes and 'sox' or other programs to deal with \n"
" other stream formats. The main options are the input sample rate,\n"
" the output sample rate and parameters for the resmapler itself.\n"
"\n"
" This program uses the 'soxr' standalone resampling library (see\n"
" https://sourceforge.net/projects/soxr/) with the highest quality \n"
" settings, all computations are done with 64-bit floating point \n"
" numbers.\n"
"\n"
" The computation is similar to using 'sox' with effect 'rate -v'.\n"
" But 'sox' applies all effects internally to 32-bit signed integer\n"
" samples (that is, the 64-bit input precision is lost).\n"
"\n"
" OPTIONS\n"
" \n"
" --inrate=floatval, -i floatval\n"
" the input sample rate as floating point number (must not be an \n"
" integer). Default is 44100. In case of file input this value is\n"
" overwritten by the sampling rate specified in the file (so, this\n"
" option is not needed).\n"
"\n"
" --outrate=floatval, -o floatval\n"
" the output sample rate as floating point number (must not be an \n"
" integer). Default is 192000.\n"
"\n"
" --channels=intval, -c intval\n"
" number of interleaved channels in the input. Default is 2 (stereo).\n"
" In case of input from a file this number is overwritten by the \n"
" the number of channels in the file.\n"
"\n"
" --buffer-length=intval, -b intval\n"
" the size of the input buffer in number of frames. The default\n"
" (and minimal value) is 8192 and should usually be fine.\n"
"\n"
" --phase=floatval, -P floatval\n"
" the phase response of the filter used during resampling; see the \n"
" documentation of the 'rate' effect in 'sox' for more details. This\n"
" is a number from 0 (minimum phase) to 100 (maximal phase), with \n"
" 50 (linear phase) and 25 (intermediate phase). The default is 25,\n"
" and should usually be fine.\n"
"\n"
" --band-width=floatval, -B floatval\n"
" the band-width of the filter used during resampling; see the \n"
" documentation of the rate effect in 'sox' for more details. The value\n"
" is given as percentage (of the Nyquist frequency of the smaller \n"
" sampling rate). The allowed range is 74.0..99.7, the default is 91.09\n"
" (that is the filter is flat up to about 20kHz).\n"
"\n"
" --precision=floatval, -e floatval\n"
" the bit precision for resampling; higher values cause higher CPU usage.\n"
" The valid range is 16.0..33.0, the default is 33.0 and should usually\n"
" be fine (except lower CPU usage is essential).\n"
"\n"
" --help, -h\n"
" show this help.\n"
"\n"
" --verbose, -v\n"
" shows some information during startup and operation.\n"
"\n"
" --version, -V\n"
" show the version of this program and exit.\n"
"\n"
" EXAMPLES\n"
"\n"
" Convert a file 'musicfile' that can be read by 'sox' to a 96/32\n"
" wav-file using a pipe:\n"
" ORIGRATE=`sox --i musicfile | grep \"Sample Rate\" | \\\n"
" cut -d: -f2 | sed -e \"s/ //g\"`\n"
" sox musicfile -t raw -e float -b 64 - | \\\n"
" resample_soxr --inrate=$ORIGRATE --outrate=96000 --precision=28 | \\\n"
" sox -t raw -e float -b 64 -c 2 -r 96000 - -e signed -b 32 out.wav\n"
"\n"
);
}
int main(int argc, char *argv[])
{
/* variables for the resampler */
double inrate, outrate, phase, bwidth, prec;
double *iptr, *optr;
int verbose, optc;
long intotal, outtotal, blen, mlen, check, nch, olen;
size_t indone, outdone;
/* variables for soxr */
int qspecflags, qspecrecipe;
soxr_quality_spec_t q_spec;
soxr_io_spec_t io_spec;
soxr_runtime_spec_t runtime_spec;
soxr_t soxr;
soxr_error_t error;
/**********************************************************************/
/* read and set parameters */
/**********************************************************************/
/* no parameter given */
if (argc == 1) {
usage();
exit(1);
}
/* read command line options */
static struct option longoptions[] = {
{"inrate", required_argument, 0, 'i' },
{"outrate", required_argument, 0, 'o' },
{"phase", required_argument, 0, 'P' },
{"band-width", required_argument, 0, 'B' },
{"precision", required_argument, 0, 'e' },
{"channels", required_argument, 0, 'c' },
{"buffer-length", required_argument, 0, 'b' },
{"verbose", no_argument, 0, 'v' },
{"version", no_argument, 0, 'V' },
{"help", no_argument, 0, 'h' },
{0, 0, 0, 0 }
};
/* defaults */
inrate = 44100.0;
outrate = 192000.0;
phase = 25.0;
bwidth = 0.0;
prec = 33.0;
nch = 2;
blen = 8192;
verbose = 0;
intotal = 0;
outtotal = 0;
/* read parameters */
while ((optc = getopt_long(argc, argv, "i:o:P:B:e:c:b:vVh", longoptions, &optind)) != -1) {
switch (optc) {
case 'i':
inrate = atof(optarg);
break;
case 'o':
outrate = atof(optarg);
break;
case 'P':
phase = atof(optarg);
if (phase < 0.0 || phase > 100.0)
phase = 25.0;
break;
case 'B':
bwidth = atof(optarg);
if (bwidth < 74.0 || bwidth > 99.7)
bwidth = 0.0;
break;
case 'e':
prec = atof(optarg);
if (prec < 16.0 || prec > 33.0)
prec = 33.0;
break;
case 'c':
nch = atoi(optarg);
break;
case 'b':
blen = atoi(optarg);
if (blen < 1024)
blen = 8192;
break;
case 'v':
verbose = 1;
break;
case 'V':
fprintf(stderr, "resample_soxr (version %s of frankl's stereo utilities", VERSION);
fprintf(stderr, ", reworked by Andree Buschmann)\n");
exit(2);
default:
usage();
exit(3);
}
}
/**********************************************************************/
/* show playhrt configuration */
/**********************************************************************/
if (verbose) {
fprintf(stderr, "resample_soxr: Version %s\n", VERSION);
fprintf(stderr, "resample_soxr: input rate %.1f output rate %.1f\n", inrate, outrate);
fprintf(stderr, "resample_soxr: precision %.0f, phase %.0f\n", prec, phase);
fprintf(stderr, "resample_soxr: passband %.4f, stopband 1.0 (no aliasing)\n", bwidth/100);
fprintf(stderr, "resample_soxr: SOXR_ROLLOFF_SMALL, SOXR_HI_PREC_CLOCK\n");
}
/**********************************************************************/
/* allocate buffers */
/**********************************************************************/
/* allocate input buffer */
iptr = (double*) malloc(nch*blen*sizeof(double));
/* allocate output buffer */
olen = (long)(blen*(outrate/inrate+1.0));
optr = (double*) malloc(nch*olen*sizeof(double));
/**********************************************************************/
/* create soxr resampler, for parameters see */
/* https://sourceforge.net/p/soxr/code/ci/master/tree/src/soxr.h */
/**********************************************************************/
/* small rolloff <= 0.01 dB and high precision for irrational ratios */
qspecflags = SOXR_ROLLOFF_SMALL | SOXR_HI_PREC_CLOCK;
/* intermediate phase (will later be overwritten by chosen value for phase) */
/* precision 32 (will later be overwritten by chosen value for precision) */
qspecrecipe = SOXR_INTERMEDIATE_PHASE | SOXR_32_BITQ;
/* create qspec, overwrite phase/prec/bwidth with chosen values */
q_spec = soxr_quality_spec(qspecrecipe, qspecflags);
q_spec.phase_response = phase;
q_spec.precision = prec;
q_spec.passband_end = (bwidth != 0.0) ? bwidth/100.0 : q_spec.passband_end;
/* set io_spec for FLOAT64 precision */
io_spec = soxr_io_spec(SOXR_FLOAT64_I,SOXR_FLOAT64_I);
/* set runtime spec for 1 thread */
runtime_spec = soxr_runtime_spec(1);
/* now we can create the resampler */
soxr = soxr_create(inrate, outrate, nch, &error, &io_spec, &q_spec, &runtime_spec);
if (error) {
fprintf(stderr, "resample_soxr: Error initializing soxr resampler.\n");
fflush(stderr);
exit(4);
}
/**********************************************************************/
/* main loop */
/**********************************************************************/
/* we read from stdin until eof and write to stdout */
while (1) {
mlen = blen;
/* clean buffers */
memset(iptr, 0, nch*blen*sizeof(double));
memset(optr, 0, nch*olen*sizeof(double));
/**********************************************************************/
/* read data */
/**********************************************************************/
/* read input block */
mlen = fread((void*)iptr, nch*sizeof(double), mlen, stdin);
/**********************************************************************/
/* resample data */
/**********************************************************************/
/* call resampler */
error = soxr_process(soxr, iptr, mlen, &indone, optr, olen, &outdone);
if (mlen > indone) {
fprintf(stderr, "resample_soxr: only %ld/%ld processed.\n",(long)indone,(long)mlen);
fflush(stderr);
}
if (error) {
fprintf(stderr, "resample_soxr: Error calling soxr_process: (%s).\n", soxr_strerror(error));
fflush(stderr);
exit(5);
}
/**********************************************************************/
/* write data to output */
/**********************************************************************/
/* write output */
check = fwrite((void*)optr, nch*sizeof(double), outdone, stdout);
fflush(stdout);
/* this should not happen, the whole block should be written */
if (check < outdone) {
fprintf(stderr, "resample_soxr: Error writing to output..\n");
fflush(stderr);
exit(6);
}
intotal += mlen;
outtotal += outdone;
if (mlen == 0) /* done */
break;
}
/**********************************************************************/
/* resample_soxr end, cleanup */
/**********************************************************************/
soxr_delete(soxr);
free(iptr);
free(optr);
if (verbose) {
fprintf(stderr, "resample_soxr: %ld input and %ld output samples\n", (long)intotal, (long)outtotal);
}
return(0);
}
Wie hast Du die Abstände gemessen und warum sollte der Jitter mit Regelung geringer sein? Ich kann leider Deine playhrt-Version nicht testen, da bei mir die mmap-Option nicht funktioniert. Mit welcher Zeitkonstante läuft der PI-Regler?Wenn ich jetzt messe in welchen Abständen playhrt aufwacht und Daten an den Hardware Puffer sendet, sehe ich typische Abweichungen bis +/- 5.000 ns (5 µs)
Der Jitter ist mit der Regelung nicht geringer. Ich wollte nur Frank´s Aussage dahin gehend kommentieren, dass eine Änderung der Abtast-Intervalle durch die Regelung im Vergleich zum sowieso auftretenden Jitter vernachlässigbar ist. Ich habe übrigens noch einmal nachgemessen und muss meine Aussage korrigieren. Der Jitter ist etwas größer als zunächst von mir angenommen. Wenn ich folgenden Code zum Messen der Zeitintervalle benutze
Code: Alles auswählen
// sleep until wake time is reached */
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &mtime, NULL);
// AB: get time and compare against targeted step time of 1000000 ns
clock_gettime(CLOCK_MONOTONIC, &mtimecheck);
time_ist = 1000000000 * mtimecheck.tv_sec + mtimecheck.tv_nsec;
time_diff = (time_ist - time_alt - 1000000)/5000 + 7; /* 5k steps into array of length 15 */
time_diff = (time_diff>14) ? 14 : (time_diff);
time_diff = (time_diff< 0) ? 0 : (time_diff);
time_alt = time_ist;
diff[time_diff]++;
if (count % LOOPS_CADENCE == 0) {
fprintf(stderr, "playhrt: count %ld\n", count);
int i;
for (i=0; i<15; ++i)
fprintf(stderr, "%dk: %d (%.2f%%)\n", (i-7)*5, diff[i], (float)diff[i]/count*100);
fprintf(stderr, "\n");
}
Code: Alles auswählen
playhrt: count 100000
-35k: 740 (0.74%)
-30k: 500 (0.50%)
-25k: 1496 (1.50%)
-20k: 1432 (1.43%)
-15k: 2187 (2.19%)
-10k: 4052 (4.05%)
-5k: 5574 (5.57%)
0k: 66348 (66.35%)
5k: 7186 (7.19%)
10k: 4208 (4.21%)
15k: 3098 (3.10%)
20k: 1064 (1.06%)
25k: 1311 (1.31%)
30k: 278 (0.28%)
35k: 526 (0.53%)
Die Regelung sieht wie folgt aus:
Code: Alles auswählen
double Kp = 1.0; /* value based on tests */
double Ki = 0.05; /* value based on tests */
...
Ta = (1.0*LOOPS_CADENCE)/loopspersec; /* delta T seconds */
Ta = (Ki*Ta>0.2) ? 0.2/Ki : Ta; /* limit Ta to avoid oscallation */
...
/* calculate amount of time to be added to default step time */
/* to overall match the local clock to the outgoing clock */
extransec = (long)(-(Kp * buferr + Ki * Ta * buferr_i) + 0.5);
nsec = (int)(1000000000/loopspersec + extransec);
Code: Alles auswählen
playhrt: (91961 sec) buf: 8148.0 e: 0.0 ei: -7.7 dt: 1 ns (0.0000%)
playhrt: (91965 sec) buf: 8148.0 e: 0.0 ei: -7.7 dt: 1 ns (0.0000%)
playhrt: (91969 sec) buf: 8148.0 e: 0.0 ei: -7.7 dt: 1 ns (0.0000%)
playhrt: (91973 sec) buf: 8145.5 e: -2.5 ei: -10.2 dt: 4 ns (0.0003%)
playhrt: (91976 sec) buf: 8148.0 e: 0.0 ei: -10.2 dt: 2 ns (0.0001%)
playhrt: (91980 sec) buf: 8148.0 e: 0.0 ei: -10.2 dt: 2 ns (0.0001%)
playhrt: (91984 sec) buf: 8148.0 e: 0.0 ei: -10.2 dt: 2 ns (0.0001%)
playhrt: (91988 sec) buf: 8148.0 e: 0.0 ei: -10.2 dt: 2 ns (0.0001%)
playhrt: (91992 sec) buf: 8148.2 e: 0.2 ei: -9.9 dt: 2 ns (0.0001%)
playhrt: (91996 sec) buf: 8148.2 e: 0.2 ei: -9.7 dt: 2 ns (0.0001%)
playhrt: (91999 sec) buf: 8148.8 e: 0.8 ei: -8.9 dt: 1 ns (0.0000%)
playhrt: (92003 sec) buf: 8148.0 e: 0.0 ei: -8.9 dt: 2 ns (0.0001%)
playhrt: (92007 sec) buf: 8149.2 e: 1.2 ei: -7.7 dt: 0 ns (-0.0001%)
playhrt: (92011 sec) buf: 8148.2 e: 0.2 ei: -7.4 dt: 1 ns (0.0000%)
playhrt: (92015 sec) buf: 8148.0 e: 0.0 ei: -7.4 dt: 1 ns (0.0000%)
playhrt: (92018 sec) buf: 8148.2 e: 0.2 ei: -7.2 dt: 1 ns (0.0000%)
playhrt: (92022 sec) buf: 8148.0 e: 0.0 ei: -7.2 dt: 1 ns (0.0000%)
playhrt: (92026 sec) buf: 8148.0 e: 0.0 ei: -7.2 dt: 1 ns (0.0000%)
playhrt: (92030 sec) buf: 8148.0 e: 0.0 ei: -7.2 dt: 1 ns (0.0000%)
playhrt: (92034 sec) buf: 8148.0 e: 0.0 ei: -7.2 dt: 1 ns (0.0000%)
playhrt: (92037 sec) buf: 8148.0 e: 0.0 ei: -7.2 dt: 1 ns (0.0000%)
playhrt: (92041 sec) buf: 8146.6 e: -1.4 ei: -8.7 dt: 3 ns (0.0002%)
playhrt: (92045 sec) buf: 8148.0 e: 0.0 ei: -8.7 dt: 2 ns (0.0001%)
playhrt: (92049 sec) buf: 8148.0 e: 0.0 ei: -8.7 dt: 2 ns (0.0001%)
playhrt: (92053 sec) buf: 8148.1 e: 0.1 ei: -8.6 dt: 2 ns (0.0001%)
playhrt: (92056 sec) buf: 8148.2 e: 0.2 ei: -8.3 dt: 1 ns (0.0000%)
playhrt: (92060 sec) buf: 8148.0 e: 0.0 ei: -8.3 dt: 2 ns (0.0001%)
playhrt: (92064 sec) buf: 8148.0 e: 0.0 ei: -8.3 dt: 2 ns (0.0001%)
playhrt: (92068 sec) buf: 8148.2 e: 0.2 ei: -8.1 dt: 1 ns (0.0000%)
playhrt: (92072 sec) buf: 8148.2 e: 0.2 ei: -7.9 dt: 1 ns (0.0000%)
playhrt: (92076 sec) buf: 8148.0 e: 0.0 ei: -7.9 dt: 2 ns (0.0001%)
playhrt: (92079 sec) buf: 8148.2 e: 0.2 ei: -7.6 dt: 1 ns (0.0000%)
playhrt: (92083 sec) buf: 8148.2 e: 0.2 ei: -7.4 dt: 1 ns (0.0000%)
playhrt: (92087 sec) buf: 8148.0 e: 0.0 ei: -7.4 dt: 1 ns (0.0000%)
Code: Alles auswählen
Measuring actual precision of monotonic clock for 10 seconds ...
Min diff: -5298 ns, max diff: 5665 ns,
avg. diff: 0 ns
diff in ns count
< -10*500 10
-9*500 98
-8*500 642
-7*500 156
-6*500 0
-5*500 3
-4*500 6
-3*500 47
-2*500 43
-1*500 70
0*500 7986
1*500 30
2*500 42
3*500 47
4*500 4
5*500 5
6*500 4
7*500 88
8*500 650
9*500 154
> 10*500 14