* 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 | If you experience excessive underruns under normal operating conditions, | 30 | If you experience excessive underruns under normal operating conditions, |
| 31 | lower the process niceness; if it still underruns, raise the default | 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 | ```shell | 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 | Run with `env LIBASOUND_DEBUG=1` to debug ALSA problems. | 43 | Run with `env LIBASOUND_DEBUG=1` to debug ALSA problems. |
| @@ -15,7 +15,6 @@ · 14 unmodified lines | |||
| 15 | #define MAX_SO_PACKETSIZE 1764 | 15 | #define MAX_SO_PACKETSIZE 1764 |
| 16 | #define BYTES_PER_SAMPLE 2 | 16 | #define BYTES_PER_SAMPLE 2 |
| 17 | #define CHANNELS 2 | 17 | #define CHANNELS 2 |
| 18 | #define TYPICAL_SAMPLES_PER_PACKET (980 / (BYTES_PER_SAMPLE * CHANNELS)) | ||
| 19 | 18 | ||
| 20 | #define SNDCHK(call, ret) { \ | 19 | #define SNDCHK(call, ret) { \ |
| 21 | if (ret < 0) { \ | 20 | if (ret < 0) { \ |
| @@ -31,7 +30,7 @@ static void alsa_error(const char *msg, int r) · 9 unmodified lines | |||
| 31 | 30 | ||
| 32 | static void show_usage(const char *arg0) | 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 | arg0); | 34 | arg0); |
| 36 | exit(1); | 35 | exit(1); |
| 37 | } | 36 | } |
| @@ -74,68 +73,58 @@ error_exit: · 36 unmodified lines | |||
| 74 | exit(1); | 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 | int ret; | 78 | int ret; |
| 80 | snd_pcm_hw_params_t *hw; | 79 | snd_output_t *log; |
| 81 | snd_pcm_sw_params_t *sw; | ||
| 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); | 84 | ret = snd_pcm_dump(pcm, log); |
| 86 | SNDCHK("snd_pcm_hw_params_any", ret); | 85 | SNDCHK("snd_pcm_dump", ret); |
| 87 | 86 | ||
| 88 | ret = snd_pcm_hw_params_set_rate_resample(pcm, hw, 1); | 87 | ret = snd_output_close(log); |
| 89 | SNDCHK("snd_pcm_hw_params_set_rate_resample", ret); | 88 | SNDCHK("snd_output_close", ret); |
| 90 | 89 | ||
| 91 | ret = snd_pcm_hw_params_set_access(pcm, hw, SND_PCM_ACCESS_RW_INTERLEAVED); | 90 | return 0; |
| 92 | SNDCHK("snd_pcm_hw_params_set_access", ret); | 91 | } |
| 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); | ||
| 110 | 92 | ||
| 111 | ret = snd_pcm_sw_params_current(pcm, sw); | 93 | static int setup_alsa(snd_pcm_t *pcm, unsigned int rate, int verbosity, unsigned int target_latency_ms) |
| 112 | SNDCHK("snd_pcm_sw_params_current", ret); | 94 | { |
| 95 | int ret; | ||
| 96 | int soft_resample = 1; | ||
| 97 | unsigned int latency = target_latency_ms * 1000; | ||
| 113 | 98 | ||
| 114 | // Avoid underruns | 99 | ret = snd_pcm_set_params(pcm, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, |
| 115 | ret = snd_pcm_sw_params_set_start_threshold(pcm, sw, start_threshold); | 100 | CHANNELS, rate, soft_resample, latency); |
| 116 | SNDCHK("snd_pcm_sw_params_set_start_threshold", ret); | 101 | SNDCHK("snd_pcm_set_params", ret); |
| 117 | 102 | ||
| 118 | ret = snd_pcm_sw_params(pcm, sw); | 103 | if (verbosity > 0) { |
| 119 | SNDCHK("snd_pcm_sw_params", ret); | 104 | return dump_alsa_info(pcm); |
| 105 | } | ||
| 120 | 106 | ||
| 121 | return 0; | 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 | int ret; | 113 | int ret; |
| 127 | snd_pcm_sframes_t f; | 114 | snd_pcm_sframes_t written; |
| 128 | 115 | ||
| 129 | f = snd_pcm_writei(snd, pcm, num_frames); | 116 | while (i < total_frames) { |
| 130 | if (f < 0) { | 117 | written = snd_pcm_writei(snd, &buf[i * BYTES_PER_SAMPLE * CHANNELS], total_frames - i); |
| 131 | ret = snd_pcm_recover(snd, f, 0); | 118 | if (written < 0) { |
| 132 | SNDCHK("snd_pcm_recover", ret); | 119 | ret = snd_pcm_recover(snd, written, 0); |
| 133 | return 0; | 120 | SNDCHK("snd_pcm_recover", ret); |
| 134 | } | 121 | return 0; |
| 135 | if (f < num_frames) { | 122 | } else if (written < total_frames - i) { |
| 136 | fprintf(stderr, "Short write %ld\n", f); | 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 | int main(int argc, char *argv[]) | 130 | int main(int argc, char *argv[]) |
| @@ -148,17 +137,21 @@ int main(int argc, char *argv[]) · 6 unmodified lines | |||
| 148 | unsigned char buf[MAX_SO_PACKETSIZE]; | 137 | unsigned char buf[MAX_SO_PACKETSIZE]; |
| 149 | in_addr_t interface = INADDR_ANY; | 138 | in_addr_t interface = INADDR_ANY; |
| 150 | int opt; | 139 | int opt; |
| 140 | int verbosity = 0; | ||
| 151 | unsigned int rate = 44100; | 141 | unsigned int rate = 44100; |
| 152 | int samples; | 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 | switch (opt) { | 146 | switch (opt) { |
| 157 | case 'i': | 147 | case 'i': |
| 158 | interface = get_interface(optarg); | 148 | interface = get_interface(optarg); |
| 159 | break; | 149 | break; |
| 160 | case 't': | 150 | case 't': |
| 161 | start_threshold = atoi(optarg); | 151 | target_latency_ms = atoi(optarg); |
| 152 | break; | ||
| 153 | case 'v': | ||
| 154 | verbosity += 1; | ||
| 162 | break; | 155 | break; |
| 163 | default: | 156 | default: |
| 164 | show_usage(argv[0]); | 157 | show_usage(argv[0]); |
| @@ -173,7 +166,7 @@ int main(int argc, char *argv[]) · 8 unmodified lines | |||
| 173 | ret = snd_pcm_open(&snd, device, SND_PCM_STREAM_PLAYBACK, 0); | 166 | ret = snd_pcm_open(&snd, device, SND_PCM_STREAM_PLAYBACK, 0); |
| 174 | SNDCHK("snd_pcm_open", ret); | 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 | return -1; | 170 | return -1; |
| 178 | } | 171 | } |
| 179 | 172 | ||
| @@ -194,6 +187,6 @@ int main(int argc, char *argv[]) · 14 unmodified lines | |||
| 194 | for (;;) { | 187 | for (;;) { |
| 195 | n = recvfrom(sockfd, &buf, MAX_SO_PACKETSIZE, 0, NULL, 0); | 188 | n = recvfrom(sockfd, &buf, MAX_SO_PACKETSIZE, 0, NULL, 0); |
| 196 | samples = n / (BYTES_PER_SAMPLE * CHANNELS); | 189 | samples = n / (BYTES_PER_SAMPLE * CHANNELS); |
| 197 | write_frames(snd, &buf, samples); | 190 | write_frames(snd, buf, samples); |
| 198 | } | 191 | } |
| 199 | } | 192 | } |