2 Copyright (C) 2010 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /** @file libs/pbd/sndfile_manager.cc
21 * @brief A FileDescriptor for files opened using libsndfile.
25 #include <sys/resource.h>
26 #include <sys/types.h>
30 #include "pbd/compose.h"
31 #include "pbd/sndfile_manager.h"
32 #include "pbd/debug.h"
37 /** @param file_name Filename.
38 * @param writeable true to open writeable, otherwise false.
39 * @param info SF_INFO for the file.
42 SndFileDescriptor::SndFileDescriptor (string const & file_name, bool writeable, SF_INFO* info)
43 : FileDescriptor (file_name, writeable)
47 manager()->add (this);
50 SndFileDescriptor::~SndFileDescriptor ()
52 manager()->remove (this);
55 /** @return SNDFILE*, or 0 on error */
57 SndFileDescriptor::allocate ()
59 bool const f = manager()->allocate (this);
64 /* this is ok thread-wise because allocate () has incremented
65 the Descriptor's refcount, so the file will not be closed
71 SndFileDescriptor::close ()
73 /* we must have a lock on the FileManager's mutex */
81 SndFileDescriptor::is_open () const
83 /* we must have a lock on the FileManager's mutex */
89 SndFileDescriptor::open ()
91 /* we must have a lock on the FileManager's mutex */
93 int fd = ::open(_path.c_str(), O_LARGEFILE | (_writeable ? (O_RDWR) : O_RDONLY));
94 if (fd == -1) return false;
95 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
97 _sndfile = sf_open_fd (fd, _writeable ? SFM_RDWR : SFM_READ, _info, 1);
99 return (_sndfile == 0);