Use unique_ptr in AlsaPlayer

This commit is contained in:
Thomas Goyne 2014-05-22 19:51:55 -07:00
parent b9a683eeb2
commit c2b8892b33

View file

@ -215,27 +215,27 @@ do_setup:
clock_gettime(CLOCK_REALTIME, &ps.last_position_time); clock_gettime(CLOCK_REALTIME, &ps.last_position_time);
// Initial buffer-fill // Initial buffer-fill
{
snd_pcm_sframes_t avail = std::min(snd_pcm_avail(pcm), (snd_pcm_sframes_t)(ps.end_position-position)); snd_pcm_sframes_t avail = std::min(snd_pcm_avail(pcm), (snd_pcm_sframes_t)(ps.end_position-position));
char *buf = new char[avail*framesize]; std::unique_ptr<char[]> buf{new char[avail*framesize]};
ps.provider->GetAudioWithVolume(buf, position, avail, ps.volume); ps.provider->GetAudioWithVolume(buf.get(), position, avail, ps.volume);
snd_pcm_sframes_t written = 0; snd_pcm_sframes_t written = 0;
while (written <= 0) while (written <= 0)
{ {
written = snd_pcm_writei(pcm, buf, avail); written = snd_pcm_writei(pcm, buf.get(), avail);
if (written == -ESTRPIPE) if (written == -ESTRPIPE)
{ {
snd_pcm_recover(pcm, written, 0); snd_pcm_recover(pcm, written, 0);
} }
else if (written <= 0) else if (written <= 0)
{ {
delete[] buf;
snd_pcm_close(pcm); snd_pcm_close(pcm);
LOG_D("audio/player/alsa") << "error filling buffer"; LOG_D("audio/player/alsa") << "error filling buffer";
return (void*)"snd_pcm_writei"; return (void*)"snd_pcm_writei";
} }
} }
delete[] buf;
position += written; position += written;
}
// Start playback // Start playback
LOG_D("audio/player/alsa") << "initial buffer filled, hitting start"; LOG_D("audio/player/alsa") << "initial buffer filled, hitting start";
@ -294,12 +294,14 @@ do_setup:
printf("---------\n\n"); printf("---------\n\n");
continue; continue;
} }
buf = new char[avail*framesize];
ps.provider->GetAudioWithVolume(buf, position, avail, ps.volume); {
written = 0; std::unique_ptr<char[]> buf{new char[avail*framesize]};
ps.provider->GetAudioWithVolume(buf.get(), position, avail, ps.volume);
snd_pcm_sframes_t written = 0;
while (written <= 0) while (written <= 0)
{ {
written = snd_pcm_writei(pcm, buf, avail); written = snd_pcm_writei(pcm, buf.get(), avail);
if (written == -ESTRPIPE || written == -EPIPE) if (written == -ESTRPIPE || written == -EPIPE)
{ {
snd_pcm_recover(pcm, written, 0); snd_pcm_recover(pcm, written, 0);
@ -310,14 +312,13 @@ do_setup:
} }
else if (written < 0) else if (written < 0)
{ {
delete[] buf;
snd_pcm_close(pcm); snd_pcm_close(pcm);
LOG_D("audio/player/alsa") << "error filling buffer, written=" << written; LOG_D("audio/player/alsa") << "error filling buffer, written=" << written;
return (void*)"snd_pcm_writei"; return (void*)"snd_pcm_writei";
} }
} }
delete[] buf;
position += written; position += written;
}
//LOG_D("audio/player/alsa") << "playback loop, filled buffer"; //LOG_D("audio/player/alsa") << "playback loop, filled buffer";
// Check for end of playback // Check for end of playback