-
Notifications
You must be signed in to change notification settings - Fork 1
/
ALSAPlayer.cpp
165 lines (128 loc) · 4.08 KB
/
ALSAPlayer.cpp
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
/*
* ALSAPlayer.cpp
*
* Created on: Sep 13, 2017
* Author: jussi
*/
#include "ALSAPlayer.hpp"
#include <iostream>
/* dtor */
ALSAPlayer::~ALSAPlayer()
{
if(pcm_handle)
{
snd_pcm_drop(pcm_handle);
snd_pcm_drain(pcm_handle);
snd_pcm_close(pcm_handle);
snd_config_update_free_global();
}
}
int32_t ALSAPlayer::initPlayer(ALSAConfig cfg)
{
std::cout << "=== INITIALIZING ALSA ===" << std::endl;
if(!cfg.channels || !cfg.rate)
{
std::cout << "ERROR: player config was bad" << std::endl;
return -1;
}
m_channels = cfg.channels;
m_rate = cfg.rate;
m_frames = 1024;
uint32_t tmp;
int dir = 0;
/* Open the PCM device in playback mode */
pcm = snd_pcm_open(&pcm_handle, PCM_DEVICE, SND_PCM_STREAM_PLAYBACK, 0);
if (pcm < 0)
{
pcm_handle = nullptr;
printf("ERROR: Can't open \"%s\" PCM device. %s\n", PCM_DEVICE, snd_strerror(pcm));
}
snd_pcm_hw_params_alloca(¶ms);
if ((pcm = snd_pcm_hw_params_any(pcm_handle, params)) < 0)
{
printf("ERROR: Param setting fails %s\n", snd_strerror(pcm));
}
if ((pcm = snd_pcm_hw_params_set_access(pcm_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
{
printf("ERROR: Can't set interleaved mode. %s\n", snd_strerror(pcm));
}
if ((pcm = snd_pcm_hw_params_set_format(pcm_handle, params, SND_PCM_FORMAT_U8)) < 0)
{
printf("ERROR: Can't set format. %s\n", snd_strerror(pcm));
}
if ((pcm = snd_pcm_hw_params_set_channels(pcm_handle, params, m_channels)) < 0)
{
printf("ERROR: Can't set channels number. %s\n", snd_strerror(pcm));
}
if ((pcm = snd_pcm_hw_params_set_rate_near(pcm_handle, params, &m_rate, &dir)) < 0)
{
printf("ERROR: Can't set rate. %s\n", snd_strerror(pcm));
}
pcm = snd_pcm_hw_params_set_periods(pcm_handle, params, 4, 0);
if(pcm != 0)
{
printf("ERROR: Can't set the number of periods. Error message: %s\n", snd_strerror(pcm));
}
// force the ALSA interface to use exactly *m_frames* number of frames
pcm = snd_pcm_hw_params_set_period_size(pcm_handle, params, m_frames, dir);
if(pcm < 0)
{
printf("ERROR: Can't set period size. %s\n", snd_strerror(pcm));
}
/* Write parameters */
if ((pcm = snd_pcm_hw_params(pcm_handle, params)) < 0)
{
printf("ERROR: Can't set harware parameters. %s\n", snd_strerror(pcm));
}
std::cout << "ALSA output device name: " << snd_pcm_name(pcm_handle) << std::endl;
std::cout << "ALSA output device state: " << snd_pcm_state_name(snd_pcm_state(pcm_handle)) << std::endl;
snd_pcm_hw_params_get_channels(params, &tmp);
std::cout << "ALSA output device channels: " << tmp << std::endl;
snd_pcm_format_t format;
snd_pcm_hw_params_get_format(params, &format);
std::cout << "ALSA output device format: " << format << std::endl;
snd_pcm_hw_params_get_rate(params, &tmp, 0);
std::cout << "ALSA output device rate: " << tmp << std::endl;
snd_pcm_hw_params_get_period_size(params, &m_frames, &dir);
m_buffSize = m_frames * m_channels;
std::cout << "ALSA output device frames size: " << m_frames << std::endl;
std::cout << "ALSA output device buffer size: " << m_buffSize << "(should be 1024)" << std::endl;
m_init = true;
return 0;
}
int ALSAPlayer::writeAudio(byte* buffer, uint32_t buffSize)
{
if(!m_init)
{
return -2;
}
int pcmRetVal;
if(buffSize == 0)
{
snd_pcm_drain(pcm_handle);
snd_pcm_close(pcm_handle);
return -1;
}
if((pcmRetVal = snd_pcm_writei(pcm_handle, buffer, buffSize)) == -EPIPE)
{
snd_pcm_prepare(pcm_handle);
}
else if(pcm < 0)
{
std::cout << "ERROR: could not write to audio interface" << std::endl;
}
return 0;
}
int ALSAPlayer::closePlayer()
{
/* Do some extra cleanup here if needed, maybe a dummy function? */
return 0;
}
uint32_t ALSAPlayer::getChannelCount()
{
return m_channels;
}
uint32_t ALSAPlayer::getBuffSize()
{
return m_buffSize;
}