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"
35 * Neat solution to the Win32/OS2 binary file flage requirement.
36 * If O_BINARY isn't already defined by the inclusion of the system
37 * headers, set it to zero.
47 /** @param file_name Filename.
48 * @param writeable true to open writeable, otherwise false.
49 * @param info SF_INFO for the file.
52 SndFileDescriptor::SndFileDescriptor (string const & file_name, bool writeable, SF_INFO* info)
53 : FileDescriptor (file_name, writeable)
57 manager()->add (this);
60 SndFileDescriptor::~SndFileDescriptor ()
62 manager()->remove (this);
65 /** @return SNDFILE*, or 0 on error */
67 SndFileDescriptor::allocate ()
69 bool const f = manager()->allocate (this);
74 /* this is ok thread-wise because allocate () has incremented
75 the Descriptor's refcount, so the file will not be closed
81 SndFileDescriptor::close ()
83 /* we must have a lock on the FileManager's mutex */
91 SndFileDescriptor::is_open () const
93 /* we must have a lock on the FileManager's mutex */
99 SndFileDescriptor::open ()
101 /* we must have a lock on the FileManager's mutex */
103 int fd, oflag, mode ;
106 oflag = O_RDWR | O_CREAT | O_BINARY ;
107 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ;
109 oflag = O_RDONLY | O_BINARY ;
114 fd = ::open (_path.c_str(), oflag) ;
116 fd = ::open (_path.c_str(), oflag, mode) ;
119 if (fd == -1) return false;
120 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
122 _sndfile = sf_open_fd (fd, _writeable ? SFM_RDWR : SFM_READ, _info, 1);
128 return (_sndfile == 0);