summaryrefslogtreecommitdiff
path: root/dxr3audio-alsa.c
blob: 6129f581ebc97a1e42083a245a1d6cd3d39bc416 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 *
 * Copyright (C) 2009 Christian Gmeiner
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

#include <string>
using namespace std;

#include "dxr3audio-alsa.h"
#include "dxr3configdata.h"

void cAudioAlsa::openDevice()
{
    if (open)
        return;

    // generate alsa card name
    int card = cDxr3ConfigData::instance()->GetDxr3Card();
    string cardname = "EM8300";

    if (card > 0) {
        cardname.append("_" + card);
    }
    string device = "default:CARD=" + cardname;

    dsyslog("[dxr3-audio-alsa] opening device %s", device.c_str());
    int err = snd_pcm_open(&handle, device.c_str(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);

    if (err < 0) {
        esyslog("[dxr3-audio-alsa] Playback open error: %s", snd_strerror(err));
        exit(1);
    }

    open = true;
}

void cAudioAlsa::releaseDevice()
{
    if (!open)
        return;

    if (handle) {
        snd_pcm_drop(handle);
        snd_pcm_close(handle);
        handle = NULL;
    }

    open = false;
}

void cAudioAlsa::setup(const SampleContext& ctx)
{
    if (!open)
        return;

    // look if ctx is different
    if (curContext.channels == ctx.channels && curContext.samplerate == ctx.samplerate) {
        return;
    }

    dsyslog("[dxr3-audio-alsa] changing samplerate to %d (old %d) ", ctx.samplerate, curContext.samplerate);
    dsyslog("[dxr3-audio-alsa] changing num of channels to %d (old %d)", ctx.channels, curContext.channels);

    snd_pcm_hw_params_t* alsa_hwparams;
    snd_pcm_sw_params_t* alsa_swparams;

    snd_pcm_hw_params_alloca(&alsa_hwparams);
    snd_pcm_sw_params_alloca(&alsa_swparams);

    //
    // set hardware settings

    int err = snd_pcm_hw_params_any(handle, alsa_hwparams);
    if (err < 0) {
        esyslog("[dxr3-audio-alsa] Broken config for this PCM: no configurations available");
        exit(1);
    }

    // set access type
    err = snd_pcm_hw_params_set_access(handle, alsa_hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
    if (err < 0) {
        esyslog("[dxr3-audio-alsa] Unable to set access type: %s", snd_strerror(err));
    }

    // set format
    err = snd_pcm_hw_params_set_format(handle, alsa_hwparams, SND_PCM_FORMAT_S16_LE);
    if (err < 0) {
        esyslog("[dxr3-audio-alsa] Unable to set format: %s", snd_strerror(err));
    }

    // set channels
    err = snd_pcm_hw_params_set_channels(handle, alsa_hwparams, ctx.channels);
    if (err < 0) {
        esyslog("[dxr3-audio-alsa] Unable to set channels %d: %s", ctx.channels, snd_strerror(err));
    }

    unsigned int sr = ctx.samplerate;

    // set samplerate
    err = snd_pcm_hw_params_set_rate_near(handle, alsa_hwparams, &sr, NULL);
    if (err < 0) {
        esyslog("[dxr3-audio-alsa] Unable to set samplerate %d: %s", ctx.samplerate, snd_strerror(err));
    }

    if (snd_pcm_state(handle) == SND_PCM_STATE_RUNNING) {
        if ((err = snd_pcm_drain(handle)) < 0) {
            esyslog("[dxr3-audio-alsa] Cannot drain (%s); will try to set parameters anyway\n", snd_strerror(err));
        }
    }

    // set hardware pararmeters
    err = snd_pcm_hw_params(handle, alsa_hwparams);
    if (err < 0) {
        esyslog("[dxr3-audio-alsa] Unable to set hw pararmeters: %s", snd_strerror(err));
    }

    // prepare for playback
    err = snd_pcm_prepare(handle);
    if (err < 0) {
        esyslog("[dxr3-audio-alsa] Cannot prepare audio interface for use: %s", snd_strerror(err));
    }

    //
    // set software settings

    err = snd_pcm_sw_params_current(handle, alsa_swparams);
    if (err < 0) {
        esyslog("[dxr3-audio-alsa] Cannot get current sw params: %s", snd_strerror(err));
    }

    static snd_pcm_uframes_t chunk_size = 1024;

    // start playing when one period has been written
    err = snd_pcm_sw_params_set_start_threshold(handle, alsa_swparams, chunk_size);
    if (err < 0) {
        esyslog("[dxr3-audio-alsa] Failed to set chunk_size: %s", snd_strerror(err));
    }

    snd_pcm_uframes_t boundary;
#if SND_LIB_VERSION >= 0x000901
    err = snd_pcm_sw_params_get_boundary(alsa_swparams, &boundary);
    if (err < 0) {
        esyslog("[dxr3-audio-alsa] Failed to get boundary: %s", snd_strerror(err));
    }
#else
    boundary = 0x7fffffff;
#endif

    // disable underrun reporting
    err = snd_pcm_sw_params_set_stop_threshold(handle, alsa_swparams, boundary);
    if (err < 0) {
        esyslog("[dxr3-audio-alsa] Failed to disable underrun reporting: %s", snd_strerror(err));
    }

#if SND_LIB_VERSION >= 0x000901
    // play silence when there is an underrun
    err = snd_pcm_sw_params_set_silence_size(handle, alsa_swparams, boundary);
    if (err < 0) {
        esyslog("[dxr3-audio-alsa] Failed to enable silence mode: %s", snd_strerror(err));
    }
#endif

    err = snd_pcm_sw_params(handle, alsa_swparams);
    if (err < 0) {
        esyslog("[dxr3-audio-alsa] Failed to set sw params: %s", snd_strerror(err));
    }

    curContext.channels = ctx.channels;
    curContext.samplerate = ctx.samplerate;

    bytesFrame = snd_pcm_format_physical_width(SND_PCM_FORMAT_S16_LE) / 8;
    bytesFrame *= curContext.channels;
}

void cAudioAlsa::write(uchar* data, size_t size)
{
    if (!open)
        return;

    snd_pcm_uframes_t frames = size / bytesFrame;

    if (frames == 0) {
        dsyslog("[dxr3-audio-alsa] no frames");
        return;
    }

    uchar *output_samples = data;
    snd_pcm_sframes_t res = 0;

    while (frames > 0) {
        res = snd_pcm_writei(handle, output_samples, frames);

        if (res == -EAGAIN) {
            snd_pcm_wait(handle, 500);
        } else  if (res == -EINTR) {
            // nothing to do
            res = 0;
        } else  if (res == -EPIPE) {
            Xrun();
        } else if (res == -ESTRPIPE) {
            // suspend
            dsyslog("[dxr3-audio-alsa] pcm in suspend");
            while ((res = snd_pcm_resume(handle)) == -EAGAIN) {
                sleep(1);
            }
        }

        if (res > 0) {
            output_samples += res * bytesFrame;
            frames -= res;
        }
    }
}

void cAudioAlsa::Xrun()
{
    int res;
    snd_pcm_status_alloca(&status);

    dsyslog("[dxr3-audio-alsa] Xrun");

    res = snd_pcm_status(handle, status);
    if (res < 0) {
        esyslog("[dxr3-audio-alsa]: Xrun status error: %s FATAL exiting", snd_strerror(res));
        exit(EXIT_FAILURE);
    }

    if (snd_pcm_status_get_state(status) == SND_PCM_STATE_XRUN) {

        res = snd_pcm_prepare(handle);
        if (res < 0) {
            esyslog("[dxr3-audio-alsa]: Xrun prepare error: %s FATAL exiting", snd_strerror(res));
            exit(EXIT_FAILURE);
        }
        return;         // ok, data should be accepted again
    }

    esyslog("[dxr3-audio-alsa]: read/write error FATAL exiting");
    exit(-1);
}