* scream-alsa: add -v option to dump PCM setup info * scream-alsa: remove unnecessary `channels` argument in setup_alsa * scream-alsa: use snd_pcm_set_params to set up the PCM to get a reasonable buffer and start_threshold on a wider variety of hardware * scream-alsa: document -t target latency * scream-alsa: handle short writes by writing again * scream-alsa: add error handling to dump_alsa_info * scream-alsa: remove unused macro
Unified · Split
| @@ -29,10 +29,15 @@ $ scream-alsa -i eth0 · 28 unmodified lines | ||
| 29 | 29 | |
| 30 | 30 | If you experience excessive underruns under normal operating conditions, |
| 31 | 31 | lower the process niceness; if it still underruns, raise the default |
| 32 | −output start threshold (1960) with `-t`: | |
| 32 | +target latency (50 ms) with `-t`: | |
| 33 | 33 | |
| 34 | 34 | ```shell |
| 35 | −$ scream-alsa -t 7840 | |
| 35 | +$ scream-alsa -t 100 | |
| 36 | 36 | ``` |
| 37 | 37 | |
| 38 | +Note that audio hardware typically has small buffers that result in a | |
| 39 | +latency lower than the target latency. | |
| 40 | + | |
| 41 | +Run with `-v` to dump ALSA PCM setup information. | |
| 42 | + | |
| 38 | 43 | Run with `env LIBASOUND_DEBUG=1` to debug ALSA problems. |
| @@ -15,7 +15,6 @@ · 14 unmodified lines | ||
| 15 | 15 | #define MAX_SO_PACKETSIZE 1764 |
| 16 | 16 | #define BYTES_PER_SAMPLE 2 |
| 17 | 17 | #define CHANNELS 2 |
| 18 | −#define TYPICAL_SAMPLES_PER_PACKET (980 / (BYTES_PER_SAMPLE * CHANNELS)) | |
| 19 | 18 | |
| 20 | 19 | #define SNDCHK(call, ret) { \ |
| 21 | 20 | if (ret < 0) { \ |
| @@ -31,7 +30,7 @@ static void alsa_error(const char *msg, int r) · 9 unmodified lines | ||
| 31 | 30 | |
| 32 | 31 | static void show_usage(const char *arg0) |
| 33 | 32 | { |
| 34 | − fprintf(stderr, "Usage: %s [-i interface_name_or_address] [-t alsa_output_start_threshold]\n", | |
| 33 | + fprintf(stderr, "Usage: %s [-v] [-i interface_name_or_address] [-t target_latency_ms]\n", | |
| 35 | 34 | arg0); |
| 36 | 35 | exit(1); |
| 37 | 36 | } |
| @@ -74,68 +73,58 @@ error_exit: · 36 unmodified lines | ||
| 74 | 73 | exit(1); |
| 75 | 74 | } |
| 76 | 75 | |
| 77 | −static int setup_alsa(snd_pcm_t *pcm, unsigned int rate, unsigned int channels, int start_threshold) | |
| 76 | +static int dump_alsa_info(snd_pcm_t *pcm) | |
| 78 | 77 | { |
| 79 | 78 | int ret; |
| 80 | − snd_pcm_hw_params_t *hw; | |
| 81 | − snd_pcm_sw_params_t *sw; | |
| 79 | + snd_output_t *log; | |
| 82 | 80 | |
| 83 | − snd_pcm_hw_params_alloca(&hw); | |
| 81 | + ret = snd_output_stdio_attach(&log, stderr, 0); | |
| 82 | + SNDCHK("snd_output_stdio_attach", ret); | |
| 84 | 83 | |
| 85 | − ret = snd_pcm_hw_params_any(pcm, hw); | |
| 86 | − SNDCHK("snd_pcm_hw_params_any", ret); | |
| 84 | + ret = snd_pcm_dump(pcm, log); | |
| 85 | + SNDCHK("snd_pcm_dump", ret); | |
| 87 | 86 | |
| 88 | − ret = snd_pcm_hw_params_set_rate_resample(pcm, hw, 1); | |
| 89 | − SNDCHK("snd_pcm_hw_params_set_rate_resample", ret); | |
| 87 | + ret = snd_output_close(log); | |
| 88 | + SNDCHK("snd_output_close", ret); | |
| 90 | 89 | |
| 91 | − ret = snd_pcm_hw_params_set_access(pcm, hw, SND_PCM_ACCESS_RW_INTERLEAVED); | |
| 92 | − SNDCHK("snd_pcm_hw_params_set_access", ret); | |
| 93 | − | |
| 94 | − ret = snd_pcm_hw_params_set_format(pcm, hw, SND_PCM_FORMAT_S16_LE); | |
| 95 | − SNDCHK("snd_pcm_hw_params_set_format", ret); | |
| 96 | − | |
| 97 | − ret = snd_pcm_hw_params_set_rate_near(pcm, hw, &rate, 0); | |
| 98 | − SNDCHK("snd_pcm_hw_params_set_rate_near", ret); | |
| 99 | − | |
| 100 | − ret = snd_pcm_hw_params_set_channels(pcm, hw, channels); | |
| 101 | − SNDCHK("snd_pcm_hw_params_set_channels", ret); | |
| 102 | − | |
| 103 | − ret = snd_pcm_hw_params(pcm, hw); | |
| 104 | − SNDCHK("hw_params", ret); | |
| 105 | − | |
| 106 | − ret = snd_pcm_prepare(pcm); | |
| 107 | − SNDCHK("snd_pcm_prepare", ret); | |
| 108 | − | |
| 109 | − snd_pcm_sw_params_alloca(&sw); | |
| 90 | + return 0; | |
| 91 | +} | |
| 110 | 92 | |
| 111 | − ret = snd_pcm_sw_params_current(pcm, sw); | |
| 112 | − SNDCHK("snd_pcm_sw_params_current", ret); | |
| 93 | +static int setup_alsa(snd_pcm_t *pcm, unsigned int rate, int verbosity, unsigned int target_latency_ms) | |
| 94 | +{ | |
| 95 | + int ret; | |
| 96 | + int soft_resample = 1; | |
| 97 | + unsigned int latency = target_latency_ms * 1000; | |
| 113 | 98 | |
| 114 | − // Avoid underruns | |
| 115 | − ret = snd_pcm_sw_params_set_start_threshold(pcm, sw, start_threshold); | |
| 116 | − SNDCHK("snd_pcm_sw_params_set_start_threshold", ret); | |
| 99 | + ret = snd_pcm_set_params(pcm, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, | |
| 100 | + CHANNELS, rate, soft_resample, latency); | |
| 101 | + SNDCHK("snd_pcm_set_params", ret); | |
| 117 | 102 | |
| 118 | − ret = snd_pcm_sw_params(pcm, sw); | |
| 119 | − SNDCHK("snd_pcm_sw_params", ret); | |
| 103 | + if (verbosity > 0) { | |
| 104 | + return dump_alsa_info(pcm); | |
| 105 | + } | |
| 120 | 106 | |
| 121 | 107 | return 0; |
| 122 | 108 | } |
| 123 | 109 | |
| 124 | −static int write_frames(snd_pcm_t *snd, void *pcm, int num_frames) | |
| 110 | +static int write_frames(snd_pcm_t *snd, unsigned char buf[MAX_SO_PACKETSIZE], int total_frames) | |
| 125 | 111 | { |
| 112 | + int i = 0; | |
| 126 | 113 | int ret; |
| 127 | − snd_pcm_sframes_t f; | |
| 128 | − | |
| 129 | − f = snd_pcm_writei(snd, pcm, num_frames); | |
| 130 | − if (f < 0) { | |
| 131 | − ret = snd_pcm_recover(snd, f, 0); | |
| 132 | − SNDCHK("snd_pcm_recover", ret); | |
| 133 | − return 0; | |
| 134 | − } | |
| 135 | − if (f < num_frames) { | |
| 136 | − fprintf(stderr, "Short write %ld\n", f); | |
| 114 | + snd_pcm_sframes_t written; | |
| 115 | + | |
| 116 | + while (i < total_frames) { | |
| 117 | + written = snd_pcm_writei(snd, &buf[i * BYTES_PER_SAMPLE * CHANNELS], total_frames - i); | |
| 118 | + if (written < 0) { | |
| 119 | + ret = snd_pcm_recover(snd, written, 0); | |
| 120 | + SNDCHK("snd_pcm_recover", ret); | |
| 121 | + return 0; | |
| 122 | + } else if (written < total_frames - i) { | |
| 123 | + fprintf(stderr, "Writing again after short write %ld < %d\n", written, total_frames - i); | |
| 124 | + } | |
| 125 | + i += written; | |
| 137 | 126 | } |
| 138 | − return num_frames; | |
| 127 | + return 0; | |
| 139 | 128 | } |
| 140 | 129 | |
| 141 | 130 | int main(int argc, char *argv[]) |
| @@ -148,17 +137,21 @@ int main(int argc, char *argv[]) · 6 unmodified lines | ||
| 148 | 137 | unsigned char buf[MAX_SO_PACKETSIZE]; |
| 149 | 138 | in_addr_t interface = INADDR_ANY; |
| 150 | 139 | int opt; |
| 140 | + int verbosity = 0; | |
| 151 | 141 | unsigned int rate = 44100; |
| 152 | 142 | int samples; |
| 153 | − int start_threshold = TYPICAL_SAMPLES_PER_PACKET * 8; | |
| 143 | + int target_latency_ms = 50; | |
| 154 | 144 | |
| 155 | − while ((opt = getopt(argc, argv, "i:t:")) != -1) { | |
| 145 | + while ((opt = getopt(argc, argv, "i:t:v")) != -1) { | |
| 156 | 146 | switch (opt) { |
| 157 | 147 | case 'i': |
| 158 | 148 | interface = get_interface(optarg); |
| 159 | 149 | break; |
| 160 | 150 | case 't': |
| 161 | − start_threshold = atoi(optarg); | |
| 151 | + target_latency_ms = atoi(optarg); | |
| 152 | + break; | |
| 153 | + case 'v': | |
| 154 | + verbosity += 1; | |
| 162 | 155 | break; |
| 163 | 156 | default: |
| 164 | 157 | show_usage(argv[0]); |
| @@ -173,7 +166,7 @@ int main(int argc, char *argv[]) · 8 unmodified lines | ||
| 173 | 166 | ret = snd_pcm_open(&snd, device, SND_PCM_STREAM_PLAYBACK, 0); |
| 174 | 167 | SNDCHK("snd_pcm_open", ret); |
| 175 | 168 | |
| 176 | − if (setup_alsa(snd, rate, CHANNELS, start_threshold) == -1) { | |
| 169 | + if (setup_alsa(snd, rate, verbosity, target_latency_ms) == -1) { | |
| 177 | 170 | return -1; |
| 178 | 171 | } |
| 179 | 172 | |
| @@ -194,6 +187,6 @@ int main(int argc, char *argv[]) · 14 unmodified lines | ||
| 194 | 187 | for (;;) { |
| 195 | 188 | n = recvfrom(sockfd, &buf, MAX_SO_PACKETSIZE, 0, NULL, 0); |
| 196 | 189 | samples = n / (BYTES_PER_SAMPLE * CHANNELS); |
| 197 | − write_frames(snd, &buf, samples); | |
| 190 | + write_frames(snd, buf, samples); | |
| 198 | 191 | } |
| 199 | 192 | } |