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
|
#include <algorithm>
#include <string>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <alsa/asoundlib.h>
#include "device_alsa.h"
#include "debug.h"
namespace audiere {
ALSAAudioDevice*
ALSAAudioDevice::create(const ParameterList& parameters) {
std::string devices[] = {
"default", "plughw:0,0", "hw:0,0", ""
};
std::string default_device = parameters.getValue("device", devices[0].c_str());
if (default_device != devices[0])
devices[0] = default_device;
int status = 0;
snd_pcm_t* pcm_handle = 0;
for (int i = 0; devices[i].length() > 0; i++) {
status = snd_pcm_open(&pcm_handle, devices[i].c_str (),
SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
if (status >= 0)
break;
}
if (status < 0) {
ADR_LOG("Coudn't open haredware.");
return 0;
}
int rate = 48000;
status = snd_pcm_set_params(pcm_handle,
SND_PCM_FORMAT_S16_LE,
SND_PCM_ACCESS_RW_INTERLEAVED,
2, rate, 1, 0);
if (status < 0) {
rate = 441000;
status = snd_pcm_set_params(pcm_handle,
SND_PCM_FORMAT_S16_LE,
SND_PCM_ACCESS_RW_INTERLEAVED,
2, rate, 1, 0);
if (status < 0) {
ADR_LOG("Couldn't set audio device parameters.");
snd_pcm_close(pcm_handle);
return 0;
}
}
snd_pcm_uframes_t buffer_size;
snd_pcm_uframes_t period_size;
status = snd_pcm_get_params(pcm_handle, &buffer_size, &period_size);
if (status) {
ADR_LOG("Couldn't get audio device parameters.");
snd_pcm_close(pcm_handle);
return 0;
}
int chunk_size = period_size;
int bits_per_sample = snd_pcm_format_physical_width(SND_PCM_FORMAT_S16_LE);
int bits_per_frame = bits_per_sample * 2;
int chunk_bytes = chunk_size * bits_per_frame / 8;
return new ALSAAudioDevice(pcm_handle, rate, chunk_bytes);
}
ALSAAudioDevice::ALSAAudioDevice(snd_pcm_t* pcm_handle,
int rate,
int buffer_size)
: MixerDevice(rate)
{
m_pcm_handle = pcm_handle;
m_buffer_size = buffer_size;
m_buffer = new char [buffer_size];
}
ALSAAudioDevice::~ALSAAudioDevice() {
ADR_GUARD("ALSAAudioDevice::~ALSAAudioDevice");
snd_pcm_drain(m_pcm_handle);
snd_pcm_close(m_pcm_handle);
delete [] m_buffer;
}
void ADR_CALL
ALSAAudioDevice::update() {
int ret;
int sample_len;
int sample_left;
char* sample_buf;
sample_buf = m_buffer;
sample_len = m_buffer_size / 4;
sample_left = read(sample_len, sample_buf);
while (sample_len > 0) {
ret = snd_pcm_writei(m_pcm_handle, sample_buf, sample_len);
if (ret == -EAGAIN || (ret > 0 && ret < sample_len)) {
snd_pcm_wait(m_pcm_handle, 10);
} else if (ret == -ESTRPIPE) {
do {
snd_pcm_wait(m_pcm_handle, 10);
ret = snd_pcm_resume(m_pcm_handle);
} while (ret == -EAGAIN);
snd_pcm_prepare(m_pcm_handle);
} else if (ret == -EPIPE) {
snd_pcm_prepare(m_pcm_handle);
} else if (ret == -EINTR) {
// nothing to do.
} else if (ret < 0) {
fprintf(stderr, "alsa error: %s\n", snd_strerror(ret));
return;
}
if (ret > 0) {
sample_buf += ret * 4;
sample_len -= ret;
}
}
}
const char* ADR_CALL
ALSAAudioDevice::getName() {
return "alsa";
}
}
|