c53778ac0d33daeca392c85b6dbbe7cd46fd0b6f
[ardour.git] / libs / pbd / sndfile_manager.cc
1 /*
2     Copyright (C) 2010 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 /** @file libs/pbd/sndfile_manager.cc
21  *  @brief A FileDescriptor for files opened using libsndfile.
22  */
23
24 #include <sys/time.h>
25 #include <sys/resource.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <cassert>
30 #include "pbd/compose.h"
31 #include "pbd/sndfile_manager.h"
32 #include "pbd/debug.h"
33
34 /*
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.
38  */
39 #ifndef O_BINARY
40 #define O_BINARY 0
41 #endif
42
43
44 using namespace std;
45 using namespace PBD;
46
47 /** @param file_name Filename.
48  *  @param writeable true to open writeable, otherwise false.
49  *  @param info SF_INFO for the file.
50  */
51
52 SndFileDescriptor::SndFileDescriptor (string const & file_name, bool writeable, SF_INFO* info)
53         : FileDescriptor (file_name, writeable)
54         , _sndfile (0)
55         , _info (info)
56 {
57         manager()->add (this);
58 }
59
60 SndFileDescriptor::~SndFileDescriptor ()
61 {
62         manager()->remove (this);
63 }
64
65 /** @return SNDFILE*, or 0 on error */
66 SNDFILE*
67 SndFileDescriptor::allocate ()
68 {
69         bool const f = manager()->allocate (this);
70         if (f) {
71                 return 0;
72         }
73
74         /* this is ok thread-wise because allocate () has incremented
75            the Descriptor's refcount, so the file will not be closed
76         */
77         return _sndfile;
78 }
79
80 void
81 SndFileDescriptor::close ()
82 {
83         /* we must have a lock on the FileManager's mutex */
84
85         assert (_sndfile);
86         sf_close (_sndfile);
87         _sndfile = 0;
88 }
89
90 bool
91 SndFileDescriptor::is_open () const
92 {
93         /* we must have a lock on the FileManager's mutex */
94
95         return _sndfile != 0;
96 }
97
98 bool
99 SndFileDescriptor::open ()
100 {
101         /* we must have a lock on the FileManager's mutex */
102
103         int fd, oflag, mode ;
104
105         if (_writeable) {
106                 oflag = O_RDWR | O_CREAT | O_BINARY ;
107                 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ;
108         } else {
109                 oflag = O_RDONLY | O_BINARY ;
110                 mode = 0 ;
111         }
112
113         if (mode == 0) {
114                 fd = ::open (_path.c_str(), oflag) ;
115         } else {
116                 fd = ::open (_path.c_str(), oflag, mode) ;
117         }
118
119         if (fd == -1) return false;
120         fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
121
122         _sndfile = sf_open_fd (fd, _writeable ? SFM_RDWR : SFM_READ, _info, 1);
123
124         if (_sndfile == 0) {
125                 ::close(fd);
126         }
127
128         return (_sndfile == 0);
129 }
130