summaryrefslogtreecommitdiffstats
path: root/client/x11/playback.cpp
blob: 5e796fdbf60c9b03a0894833d28fb53766889afc (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
/*
   Copyright (C) 2009 Red Hat, Inc.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of
   the License, or (at your option) any later version.

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "playback.h"
#include "utils.h"
#include "debug.h"

#define REING_SIZE_MS 300

WavePlayer::WavePlayer(uint32_t sampels_per_sec, uint32_t bits_per_sample, uint32_t channels)
    : _pcm (NULL)
    , _hw_params (NULL)
    , _sw_params (NULL)
{
    if (!init(sampels_per_sec, bits_per_sample, channels)) {
        cleanup();
        THROW("failed");
    }
}

void WavePlayer::cleanup()
{
    if (_pcm) {
        snd_pcm_close(_pcm);
    }

    if (_hw_params) {
        snd_pcm_hw_params_free(_hw_params);
    }

    if (_sw_params) {
        snd_pcm_sw_params_free(_sw_params);
    }
}

WavePlayer::~WavePlayer()
{
    cleanup();
}

bool WavePlayer::init(uint32_t sampels_per_sec,
                      uint32_t bits_per_sample,
                      uint32_t channels)
{
    const int frame_size = WavePlaybackAbstract::FRAME_SIZE;
    const char* pcm_device = "default";
    snd_pcm_format_t format;
    int err;

    switch (bits_per_sample) {
    case 8:
        format = SND_PCM_FORMAT_S8;
        break;
    case 16:
        format = SND_PCM_FORMAT_S16_LE;
        break;
    default:
        return false;
    }
    _sampels_per_ms = sampels_per_sec / 1000;

    if ((err = snd_pcm_open(&_pcm, pcm_device, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)) < 0) {
        LOG_ERROR("cannot open audio playback device %s %s", pcm_device, snd_strerror(err));
        return false;
    }

    if ((err = snd_pcm_hw_params_malloc(&_hw_params)) < 0) {
        LOG_ERROR("cannot allocate hardware parameter structure %s", snd_strerror(err));
        return false;
    }

    if ((err = snd_pcm_sw_params_malloc(&_sw_params)) < 0) {
        LOG_ERROR("cannot allocate software parameter structure %s", snd_strerror(err));
        return false;
    }

    if ((err = snd_pcm_hw_params_any(_pcm, _hw_params)) < 0) {
        LOG_ERROR("cannot initialize hardware parameter structure %s", snd_strerror(err));
        return false;
    }

    if ((err = snd_pcm_hw_params_set_rate_resample(_pcm, _hw_params, 1)) < 0) {
        LOG_ERROR("cannot set rate resample %s", snd_strerror(err));
        return false;
    }

    if ((err = snd_pcm_hw_params_set_access(_pcm, _hw_params,
                                            SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
        LOG_ERROR("cannot set access type %s", snd_strerror(err));
        return false;
    }

    if ((err = snd_pcm_hw_params_set_rate(_pcm, _hw_params, sampels_per_sec, 0)) < 0) {
        LOG_ERROR("cannot set sample rate %s", snd_strerror(err));
        return false;
    }

    if ((err = snd_pcm_hw_params_set_channels(_pcm, _hw_params, channels)) < 0) {
        LOG_ERROR("cannot set channel count %s", snd_strerror(err));
        return false;
    }

    if ((err = snd_pcm_hw_params_set_format(_pcm, _hw_params, format)) < 0) {
        LOG_ERROR("cannot set sample format %s", snd_strerror(err));
        return false;
    }

    snd_pcm_uframes_t buffer_size;
    buffer_size = (sampels_per_sec * REING_SIZE_MS / 1000) / frame_size * frame_size;

    if ((err = snd_pcm_hw_params_set_buffer_size_near(_pcm, _hw_params, &buffer_size)) < 0) {
        LOG_ERROR("cannot set buffer size %s", snd_strerror(err));
        return false;
    }

    int direction = 1;
    snd_pcm_uframes_t period_size = (sampels_per_sec * 20 / 1000) / frame_size * frame_size;
    if ((err = snd_pcm_hw_params_set_period_size_near(_pcm, _hw_params, &period_size,
                                                      &direction)) < 0) {
        LOG_ERROR("cannot set period size %s", snd_strerror(err));
        return false;
    }

    if ((err = snd_pcm_hw_params(_pcm, _hw_params)) < 0) {
        LOG_ERROR("cannot set parameters %s", snd_strerror(err));
        return false;
    }

    if ((err = snd_pcm_sw_params_current(_pcm, _sw_params)) < 0) {
        LOG_ERROR("cannot obtain sw parameters %s", snd_strerror(err));
        return false;
    }

    err = snd_pcm_hw_params_get_buffer_size(_hw_params, &buffer_size);
    if (err < 0) {
        LOG_ERROR("unable to get buffer size for playback: %s", snd_strerror(err));
        return false;
    }

    direction = 0;
    err = snd_pcm_hw_params_get_period_size(_hw_params, &period_size, &direction);
    if (err < 0) {
        LOG_ERROR("unable to get period size for playback: %s", snd_strerror(err));
        return false;
    }

    err = snd_pcm_sw_params_set_start_threshold(_pcm, _sw_params,
                                                (buffer_size / period_size) * period_size);
    if (err < 0) {
        LOG_ERROR("unable to set start threshold mode for playback: %s", snd_strerror(err));
        return false;
    }

    if ((err = snd_pcm_sw_params(_pcm, _sw_params)) < 0) {
        LOG_ERROR("cannot set software parameters %s", snd_strerror(err));
        return false;
    }

    if ((err = snd_pcm_prepare(_pcm)) < 0) {
        LOG_ERROR("cannot prepare pcm device %s", snd_strerror(err));
        return false;
    }

    return true;
}

bool WavePlayer::write(uint8_t* frame)
{
    snd_pcm_sframes_t ret = snd_pcm_writei(_pcm, frame, WavePlaybackAbstract::FRAME_SIZE);
    if (ret < 0) {
        if (ret == -EAGAIN) {
            return false;
        }
        DBG(0, "err %s", snd_strerror(-ret));
        if (snd_pcm_recover(_pcm, ret, 1) == 0) {
            snd_pcm_writei(_pcm, frame, WavePlaybackAbstract::FRAME_SIZE);
        }
    }
    return true;
}

void WavePlayer::stop()
{
    snd_pcm_drain(_pcm);
    snd_pcm_prepare(_pcm);
}

bool WavePlayer::abort()
{
    return true;
}

uint32_t WavePlayer::get_delay_ms()
{
    ASSERT(_pcm);

    snd_pcm_sframes_t delay;

    if (snd_pcm_delay(_pcm, &delay) < 0) {
        return 0;
    }
    return delay / _sampels_per_ms;
}