summaryrefslogtreecommitdiff
path: root/libs/ardour/sndfilesource.cc
diff options
context:
space:
mode:
authorPaul Davis <paul@linuxaudiosystems.com>2014-12-10 18:28:55 -0500
committerPaul Davis <paul@linuxaudiosystems.com>2014-12-10 18:28:55 -0500
commit294b99aabf3eb96323a3159b7a5e1b4bfc1ff04a (patch)
treedbb6bfba564d2979c8cebbfebe162a9659dff5aa /libs/ardour/sndfilesource.cc
parent17707b9674958391949e59a724a264cdcc2d65ff (diff)
remove file manager LRU cache from code.
This was a very clever attempt to fix a non-problem. If the platform doesn't have enough file descriptors available then the platform is broken and we're not going to hack around trying to fix it.
Diffstat (limited to 'libs/ardour/sndfilesource.cc')
-rw-r--r--libs/ardour/sndfilesource.cc110
1 files changed, 35 insertions, 75 deletions
diff --git a/libs/ardour/sndfilesource.cc b/libs/ardour/sndfilesource.cc
index 9af2b56056..27c91c8c27 100644
--- a/libs/ardour/sndfilesource.cc
+++ b/libs/ardour/sndfilesource.cc
@@ -58,7 +58,7 @@ const Source::Flag SndFileSource::default_writable_flags = Source::Flag (
SndFileSource::SndFileSource (Session& s, const XMLNode& node)
: Source(s, node)
, AudioFileSource (s, node)
- , _descriptor (0)
+ , _sndfile (0)
, _broadcast_info (0)
, _capture_start (false)
, _capture_end (false)
@@ -82,7 +82,7 @@ SndFileSource::SndFileSource (Session& s, const string& path, int chn, Flag flag
: Source(s, DataType::AUDIO, path, flags)
/* note that the origin of an external file is itself */
, AudioFileSource (s, path, Flag (flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy)))
- , _descriptor (0)
+ , _sndfile (0)
, _broadcast_info (0)
, _capture_start (false)
, _capture_end (false)
@@ -108,7 +108,7 @@ SndFileSource::SndFileSource (Session& s, const string& path, const string& orig
SampleFormat sfmt, HeaderFormat hf, framecnt_t rate, Flag flags)
: Source(s, DataType::AUDIO, path, flags)
, AudioFileSource (s, path, origin, flags, sfmt, hf)
- , _descriptor (0)
+ , _sndfile (0)
, _broadcast_info (0)
, _capture_start (false)
, _capture_end (false)
@@ -194,7 +194,7 @@ SndFileSource::SndFileSource (Session& s, const string& path, int chn)
: Source (s, DataType::AUDIO, path, Flag (0))
/* the final boolean argument is not used, its value is irrelevant. see audiofilesource.h for explanation */
, AudioFileSource (s, path, Flag (0))
- , _descriptor (0)
+ , _sndfile (0)
, _broadcast_info (0)
, _capture_start (false)
, _capture_end (false)
@@ -235,17 +235,19 @@ SndFileSource::open ()
{
string path_to_open;
+ if (_sndfile) {
+ return 0;
+ }
+
#ifdef PLATFORM_WINDOWS
path_to_open = Glib::locale_from_utf8(_path);
#else
path_to_open = _path;
#endif
- _descriptor = new SndFileDescriptor (path_to_open.c_str(), writable(), &_info);
- _descriptor->Closed.connect_same_thread (file_manager_connection, boost::bind (&SndFileSource::file_closed, this));
- SNDFILE* sf = _descriptor->allocate ();
+ _sndfile = sf_open (path_to_open.c_str(), writable() ? SFM_RDWR : SFM_READ, &_info);
- if (sf == 0) {
+ if (_sndfile == 0) {
char errbuf[1024];
sf_error_str (0, errbuf, sizeof (errbuf) - 1);
#ifndef HAVE_COREAUDIO
@@ -265,8 +267,8 @@ SndFileSource::open ()
#ifndef HAVE_COREAUDIO
error << string_compose(_("SndFileSource: file only contains %1 channels; %2 is invalid as a channel number"), _info.channels, _channel) << endmsg;
#endif
- delete _descriptor;
- _descriptor = 0;
+ sf_close (_sndfile);
+ _sndfile = 0;
return -1;
}
@@ -276,7 +278,7 @@ SndFileSource::open ()
_broadcast_info = new BroadcastInfo;
}
- bool bwf_info_exists = _broadcast_info->load_from_file (sf);
+ bool bwf_info_exists = _broadcast_info->load_from_file (_sndfile);
if (_file_is_new && _length == 0 && writable() && !bwf_info_exists) {
/* newly created files will not have a BWF header at this point in time.
@@ -305,7 +307,7 @@ SndFileSource::open ()
}
if (writable()) {
- sf_command (sf, SFC_SET_UPDATE_HEADER_AUTO, 0, SF_FALSE);
+ sf_command (_sndfile, SFC_SET_UPDATE_HEADER_AUTO, 0, SF_FALSE);
if (_flags & Broadcast) {
@@ -316,7 +318,7 @@ SndFileSource::open ()
_broadcast_info->set_from_session (_session, header_position_offset);
_broadcast_info->set_description (string_compose ("BWF %1", _name));
- if (!_broadcast_info->write_to_file (sf)) {
+ if (!_broadcast_info->write_to_file (_sndfile)) {
error << string_compose (_("cannot set broadcast info for audio file %1 (%2); dropping broadcast info for this file"),
path_to_open, _broadcast_info->get_error())
<< endmsg;
@@ -326,15 +328,16 @@ SndFileSource::open ()
}
}
}
-
- _descriptor->release ();
- _open = true;
+
return 0;
}
SndFileSource::~SndFileSource ()
{
- delete _descriptor;
+ if (_sndfile) {
+ sf_close (_sndfile);
+ _sndfile = 0;
+ }
delete _broadcast_info;
delete [] xfade_buf;
}
@@ -355,15 +358,13 @@ SndFileSource::read_unlocked (Sample *dst, framepos_t start, framecnt_t cnt) con
uint32_t real_cnt;
framepos_t file_cnt;
- if (writable() && !_open) {
+ if (writable() && !_sndfile) {
/* file has not been opened yet - nothing written to it */
memset (dst, 0, sizeof (Sample) * cnt);
return cnt;
}
- SNDFILE* sf = _descriptor->allocate ();
-
- if (sf == 0) {
+ if (_sndfile == 0) {
error << string_compose (_("could not allocate file %1 for reading."), _path) << endmsg;
return 0;
}
@@ -396,22 +397,20 @@ SndFileSource::read_unlocked (Sample *dst, framepos_t start, framecnt_t cnt) con
if (file_cnt) {
- if (sf_seek (sf, (sf_count_t) start, SEEK_SET|SFM_READ) != (sf_count_t) start) {
+ if (sf_seek (_sndfile, (sf_count_t) start, SEEK_SET|SFM_READ) != (sf_count_t) start) {
char errbuf[256];
sf_error_str (0, errbuf, sizeof (errbuf) - 1);
error << string_compose(_("SndFileSource: could not seek to frame %1 within %2 (%3)"), start, _name.val().substr (1), errbuf) << endmsg;
- _descriptor->release ();
return 0;
}
if (_info.channels == 1) {
- framecnt_t ret = sf_read_float (sf, dst, file_cnt);
+ framecnt_t ret = sf_read_float (_sndfile, dst, file_cnt);
if (ret != file_cnt) {
char errbuf[256];
sf_error_str (0, errbuf, sizeof (errbuf) - 1);
error << string_compose(_("SndFileSource: @ %1 could not read %2 within %3 (%4) (len = %5, ret was %6)"), start, file_cnt, _name.val().substr (1), errbuf, _length, ret) << endl;
}
- _descriptor->release ();
return ret;
}
}
@@ -420,7 +419,7 @@ SndFileSource::read_unlocked (Sample *dst, framepos_t start, framecnt_t cnt) con
Sample* interleave_buf = get_interleave_buffer (real_cnt);
- nread = sf_read_float (sf, interleave_buf, real_cnt);
+ nread = sf_read_float (_sndfile, interleave_buf, real_cnt);
ptr = interleave_buf + _channel;
nread /= _info.channels;
@@ -431,14 +430,13 @@ SndFileSource::read_unlocked (Sample *dst, framepos_t start, framecnt_t cnt) con
ptr += _info.channels;
}
- _descriptor->release ();
return nread;
}
framecnt_t
SndFileSource::write_unlocked (Sample *data, framecnt_t cnt)
{
- if (!_open && open()) {
+ if (open()) {
return 0; // failure
}
@@ -589,19 +587,12 @@ SndFileSource::flush_header ()
return -1;
}
- if (!_open) {
- warning << string_compose (_("attempt to flush an un-opened audio file source (%1)"), _path) << endmsg;
- return -1;
- }
-
- SNDFILE* sf = _descriptor->allocate ();
- if (sf == 0) {
+ if (_sndfile == 0) {
error << string_compose (_("could not allocate file %1 to write header"), _path) << endmsg;
return -1;
}
- int const r = sf_command (sf, SFC_UPDATE_HEADER_NOW, 0, 0) != SF_TRUE;
- _descriptor->release ();
+ int const r = sf_command (_sndfile, SFC_UPDATE_HEADER_NOW, 0, 0) != SF_TRUE;
return r;
}
@@ -609,25 +600,18 @@ SndFileSource::flush_header ()
void
SndFileSource::flush ()
{
- if (!_open) {
- warning << string_compose (_("attempt to flush an un-opened audio file source (%1)"), _path) << endmsg;
- return;
- }
-
if (!writable()) {
warning << string_compose (_("attempt to flush a non-writable audio file source (%1)"), _path) << endmsg;
return;
}
- SNDFILE* sf = _descriptor->allocate ();
- if (sf == 0) {
+ if (_sndfile == 0) {
error << string_compose (_("could not allocate file %1 to flush contents"), _path) << endmsg;
return;
}
// Hopefully everything OK
- sf_write_sync (sf);
- _descriptor->release ();
+ sf_write_sync (_sndfile);
}
int
@@ -638,7 +622,7 @@ SndFileSource::setup_broadcast_info (framepos_t /*when*/, struct tm& now, time_t
return -1;
}
- if (!_open) {
+ if (!_sndfile) {
warning << string_compose (_("attempt to set BWF info for an un-opened audio file source (%1)"), _path) << endmsg;
return -1;
}
@@ -654,9 +638,7 @@ SndFileSource::setup_broadcast_info (framepos_t /*when*/, struct tm& now, time_t
set_header_timeline_position ();
- SNDFILE* sf = _descriptor->allocate ();
-
- if (sf == 0 || !_broadcast_info->write_to_file (sf)) {
+ if (!_broadcast_info->write_to_file (_sndfile)) {
error << string_compose (_("cannot set broadcast info for audio file %1 (%2); dropping broadcast info for this file"),
_path, _broadcast_info->get_error())
<< endmsg;
@@ -665,7 +647,6 @@ SndFileSource::setup_broadcast_info (framepos_t /*when*/, struct tm& now, time_t
_broadcast_info = 0;
}
- _descriptor->release ();
return 0;
}
@@ -678,9 +659,7 @@ SndFileSource::set_header_timeline_position ()
_broadcast_info->set_time_reference (_timeline_position);
- SNDFILE* sf = _descriptor->allocate ();
-
- if (sf == 0 || !_broadcast_info->write_to_file (sf)) {
+ if (_sndfile == 0 || !_broadcast_info->write_to_file (_sndfile)) {
error << string_compose (_("cannot set broadcast info for audio file %1 (%2); dropping broadcast info for this file"),
_path, _broadcast_info->get_error())
<< endmsg;
@@ -688,29 +667,22 @@ SndFileSource::set_header_timeline_position ()
delete _broadcast_info;
_broadcast_info = 0;
}
-
- _descriptor->release ();
}
framecnt_t
SndFileSource::write_float (Sample* data, framepos_t frame_pos, framecnt_t cnt)
{
- SNDFILE* sf = _descriptor->allocate ();
-
- if (sf == 0 || sf_seek (sf, frame_pos, SEEK_SET|SFM_WRITE) < 0) {
+ if (_sndfile == 0 || sf_seek (_sndfile, frame_pos, SEEK_SET|SFM_WRITE) < 0) {
char errbuf[256];
sf_error_str (0, errbuf, sizeof (errbuf) - 1);
error << string_compose (_("%1: cannot seek to %2 (libsndfile error: %3)"), _path, frame_pos, errbuf) << endmsg;
- _descriptor->release ();
return 0;
}
- if (sf_writef_float (sf, data, cnt) != (ssize_t) cnt) {
- _descriptor->release ();
+ if (sf_writef_float (_sndfile, data, cnt) != (ssize_t) cnt) {
return 0;
}
- _descriptor->release ();
return cnt;
}
@@ -1011,17 +983,5 @@ void
SndFileSource::set_path (const string& p)
{
FileSource::set_path (p);
-
- if (_descriptor) {
- _descriptor->set_path (_path);
- }
}
-void
-SndFileSource::release_descriptor ()
-{
- if (_descriptor) {
- _descriptor->release ();
- _descriptor = 0;
- }
-}