split out sndfile manager code into its own file; move a couple of utility functions...
[ardour2.git] / libs / pbd / sndfile_manager.cc
blob5637de26eeadf72f65bd7078e7c6d84950b605ac
1 /*
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 #include <sys/time.h>
21 #include <sys/resource.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <cassert>
26 #include <iostream>
27 #include "pbd/compose.h"
28 #include "pbd/sndfile_manager.h"
29 #include "pbd/debug.h"
31 using namespace std;
32 using namespace PBD;
34 /** @param n Filename.
35 * @param w true to open writeable, otherwise false.
36 * @param i SF_INFO for the file.
39 SndFileDescriptor::SndFileDescriptor (string const & n, bool w, SF_INFO* i)
40 : FileDescriptor (n, w)
41 , _sndfile (0)
42 , _info (i)
44 manager()->add (this);
47 SndFileDescriptor::~SndFileDescriptor ()
49 manager()->remove (this);
52 /** @return SNDFILE*, or 0 on error */
53 SNDFILE*
54 SndFileDescriptor::allocate ()
56 bool const f = manager()->allocate (this);
57 if (f) {
58 return 0;
61 /* this is ok thread-wise because allocate () has incremented
62 the Descriptor's refcount, so the file will not be closed
64 return _sndfile;
67 void
68 SndFileDescriptor::close ()
70 /* we must have a lock on the FileManager's mutex */
72 sf_close (_sndfile);
73 _sndfile = 0;
76 bool
77 SndFileDescriptor::is_open () const
79 /* we must have a lock on the FileManager's mutex */
81 return _sndfile != 0;
84 bool
85 SndFileDescriptor::open ()
87 /* we must have a lock on the FileManager's mutex */
89 _sndfile = sf_open (_name.c_str(), _writeable ? SFM_RDWR : SFM_READ, _info);
90 return (_sndfile == 0);