]> git.sesse.net Git - nageru/blob - alsa_output.cpp
Fix an issue where the mixer lagging too much behind CEF would cause us to display...
[nageru] / alsa_output.cpp
1 #include "alsa_output.h"
2
3 #include <alsa/asoundlib.h>
4 #include <errno.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <vector>
8
9 using namespace std;
10
11 namespace {
12
13 void die_on_error(const char *func_name, int err)
14 {
15         if (err < 0) {
16                 fprintf(stderr, "%s: %s\n", func_name, snd_strerror(err));
17                 exit(1);
18         }
19 }
20
21 }  // namespace
22
23 ALSAOutput::ALSAOutput(int sample_rate, int num_channels)
24         : sample_rate(sample_rate), num_channels(num_channels)
25 {
26         die_on_error("snd_pcm_open()", snd_pcm_open(&pcm_handle, "default", SND_PCM_STREAM_PLAYBACK, 0));
27
28         // Set format.
29         snd_pcm_hw_params_t *hw_params;
30         snd_pcm_hw_params_alloca(&hw_params);
31         die_on_error("snd_pcm_hw_params_any()", snd_pcm_hw_params_any(pcm_handle, hw_params));
32         die_on_error("snd_pcm_hw_params_set_access()", snd_pcm_hw_params_set_access(pcm_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED));
33         die_on_error("snd_pcm_hw_params_set_format()", snd_pcm_hw_params_set_format(pcm_handle, hw_params, SND_PCM_FORMAT_FLOAT_LE));
34         die_on_error("snd_pcm_hw_params_set_rate()", snd_pcm_hw_params_set_rate(pcm_handle, hw_params, sample_rate, 0));
35         die_on_error("snd_pcm_hw_params_set_channels", snd_pcm_hw_params_set_channels(pcm_handle, hw_params, num_channels));
36
37         // Fragment size of 512 samples. (A frame at 60 fps/48 kHz is 800 samples.)
38         // We ask for 16 such periods (~170 ms buffer).
39         unsigned int num_periods = 16;
40         int dir = 0;
41         die_on_error("snd_pcm_hw_params_set_periods_near()", snd_pcm_hw_params_set_periods_near(pcm_handle, hw_params, &num_periods, &dir));
42         period_size = 512;
43         dir = 0;
44         die_on_error("snd_pcm_hw_params_set_period_size_near()", snd_pcm_hw_params_set_period_size_near(pcm_handle, hw_params, &period_size, &dir));
45         die_on_error("snd_pcm_hw_params()", snd_pcm_hw_params(pcm_handle, hw_params));
46         //snd_pcm_hw_params_free(hw_params);
47
48         snd_pcm_sw_params_t *sw_params;
49         snd_pcm_sw_params_alloca(&sw_params);
50         die_on_error("snd_pcm_sw_params_current()", snd_pcm_sw_params_current(pcm_handle, sw_params));
51         die_on_error("snd_pcm_sw_params_set_start_threshold", snd_pcm_sw_params_set_start_threshold(pcm_handle, sw_params, num_periods * period_size / 2));
52         die_on_error("snd_pcm_sw_params()", snd_pcm_sw_params(pcm_handle, sw_params));
53
54         die_on_error("snd_pcm_nonblock", snd_pcm_nonblock(pcm_handle, 1));
55         die_on_error("snd_pcm_prepare()", snd_pcm_prepare(pcm_handle));
56 }
57
58 void ALSAOutput::write(const vector<float> &samples)
59 {
60         buffer.insert(buffer.end(), samples.begin(), samples.end());
61
62 try_again:
63         int periods_to_write = buffer.size() / (period_size * num_channels);
64         if (periods_to_write == 0) {
65                 return;
66         }
67
68         int ret = snd_pcm_writei(pcm_handle, buffer.data(), periods_to_write * period_size);
69         if (ret == -EPIPE) {
70                 fprintf(stderr, "warning: snd_pcm_writei() reported underrun\n");
71                 snd_pcm_recover(pcm_handle, ret, 1);
72                 goto try_again;
73         } else if (ret == -EAGAIN) {
74                 ret = 0;
75         } else if (ret < 0) {
76                 fprintf(stderr, "error: snd_pcm_writei() returned '%s'\n", snd_strerror(ret));
77                 exit(1);
78         } else if (ret > 0) {
79                 buffer.erase(buffer.begin(), buffer.begin() + ret * num_channels);
80         }
81
82         if (buffer.size() >= period_size * num_channels) {  // Still more to write.
83                 if (ret == 0) {
84                         if (buffer.size() >= period_size * num_channels * 8) {
85                                 // OK, almost 100 ms. Giving up.
86                                 fprintf(stderr, "warning: ALSA overrun, dropping some audio (%d ms)\n",
87                                         int(buffer.size() * 1000 / (num_channels * sample_rate)));
88                                 buffer.clear();
89                         }
90                 } else if (ret > 0) {
91                         // Not a completely failure (effectively a short write),
92                         // possibly due to a signal.
93                         goto try_again;
94                 }
95         }
96 }