summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLuo Jinghua <sunmoon1997@gmail.com>2009-03-18 13:08:47 +0800
committerLuo Jinghua <sunmoon1997@gmail.com>2009-03-18 13:08:47 +0800
commit5f97c2f5485b43fe9cb9781ef88bbafa9556b045 (patch)
treed31eb6eaefac57392b1842d9522bfe73e1bad793 /src
parent7bc09fa7116f4f5d6c3043a93a6cc483e3398df7 (diff)
AlsaDevice: enlarge buffer size.
Diffstat (limited to 'src')
-rw-r--r--src/device_alsa.cpp42
1 files changed, 16 insertions, 26 deletions
diff --git a/src/device_alsa.cpp b/src/device_alsa.cpp
index d61bb53..2f748f8 100644
--- a/src/device_alsa.cpp
+++ b/src/device_alsa.cpp
@@ -11,8 +11,7 @@
namespace audiere {
static snd_pcm_t* openDevice(const std::string &name,
- unsigned int rate,
- unsigned int &chunk_bytes) {
+ unsigned int rate) {
int status = 0;
snd_pcm_t* pcm_handle = 0;
@@ -67,18 +66,18 @@ namespace audiere {
return 0;
}
- snd_pcm_uframes_t buffer = 1024;
- status = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams,
- &buffer);
+ snd_pcm_uframes_t frame_size = 1024;
+ status = snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams,
+ &frame_size, NULL);
if (status < 0) {
ADR_LOG("Coudn't set buffer size.");
snd_pcm_close(pcm_handle);
return 0;
}
- unsigned int period = 16;
+ unsigned frames = 4;
status = snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams,
- &period, NULL);
+ &frames, NULL);
if (status < 0) {
ADR_LOG("Coudn't set periods.");
snd_pcm_close(pcm_handle);
@@ -99,6 +98,7 @@ namespace audiere {
snd_pcm_close(pcm_handle);
return 0;
}
+
snd_pcm_uframes_t period_size;
status = snd_pcm_hw_params_get_period_size(hwparams, &period_size, NULL);
if (status < 0) {
@@ -157,11 +157,6 @@ namespace audiere {
return 0;
}
- unsigned int bits_per_sample =
- snd_pcm_format_physical_width(SND_PCM_FORMAT_S16_LE);
- unsigned int bits_per_frame = bits_per_sample * 2;
- chunk_bytes = period_size * bits_per_frame / 8;
-
return pcm_handle;
}
@@ -176,13 +171,13 @@ namespace audiere {
int i, rate;
snd_pcm_t* pcm_handle = 0;
- unsigned chunk_bytes;
+
for (i = 0; devices[i].length() > 0; i++) {
rate = 48000;
- pcm_handle = openDevice(devices[i], rate, chunk_bytes);
+ pcm_handle = openDevice(devices[i], rate);
if (!pcm_handle) {
rate = 44100;
- pcm_handle = openDevice(devices[i], rate, chunk_bytes);
+ pcm_handle = openDevice(devices[i], rate);
}
if (pcm_handle)
break;
@@ -192,7 +187,7 @@ namespace audiere {
return 0;
}
- return new ALSAAudioDevice(pcm_handle, devices[i], rate, chunk_bytes);
+ return new ALSAAudioDevice(pcm_handle, devices[i], rate, 4096);
}
@@ -232,20 +227,15 @@ namespace audiere {
sample_left = read(sample_len, sample_buf);
while (sample_left > 0 && m_pcm_handle) {
ret = snd_pcm_writei(m_pcm_handle, sample_buf, sample_left);
- if (ret == -EAGAIN || (ret > 0 && ret < sample_len)) {
- snd_pcm_wait(m_pcm_handle, 10);
+ if (ret == -EAGAIN) {
+ snd_pcm_wait(m_pcm_handle, 100);
if (++retry > 50) {
fprintf(stderr, "audiere: snd_pcm_writei doesn't work anymore, try to reopen a new pcm.\n");
snd_pcm_close(m_pcm_handle);
- unsigned chunk_bytes;
- m_pcm_handle = openDevice(m_pcm_name, m_rate, chunk_bytes);
- if (m_pcm_handle) {
- delete [] m_buffer;
- m_buffer_size = chunk_bytes;
- m_buffer = new char [m_buffer_size];
- }
- break;
+ m_pcm_handle = openDevice(m_pcm_name, m_rate);
+ if (!m_pcm_handle)
+ break;
}
} else if (ret == -ESTRPIPE) {
do {