newly created files for use in recording appear in a .stubs folder, and are moved...
[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 #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"
30
31 using namespace std;
32 using namespace PBD;
33
34 /** @param n Filename.
35  *  @param w true to open writeable, otherwise false.
36  *  @param i SF_INFO for the file.
37  */
38
39 SndFileDescriptor::SndFileDescriptor (string const & n, bool w, SF_INFO* i)
40         : FileDescriptor (n, w)
41         , _sndfile (0)
42         , _info (i)
43 {
44         manager()->add (this);
45 }
46
47 SndFileDescriptor::~SndFileDescriptor ()
48 {
49         manager()->remove (this);
50 }
51
52 /** @return SNDFILE*, or 0 on error */
53 SNDFILE*
54 SndFileDescriptor::allocate ()
55 {
56         bool const f = manager()->allocate (this);
57         if (f) {
58                 return 0;
59         }
60
61         /* this is ok thread-wise because allocate () has incremented
62            the Descriptor's refcount, so the file will not be closed
63         */
64         return _sndfile;
65 }
66
67 void
68 SndFileDescriptor::close ()
69 {
70         /* we must have a lock on the FileManager's mutex */
71
72         sf_close (_sndfile);
73         _sndfile = 0;
74 }
75
76 bool
77 SndFileDescriptor::is_open () const
78 {
79         /* we must have a lock on the FileManager's mutex */
80
81         return _sndfile != 0;
82 }
83
84 bool
85 SndFileDescriptor::open ()
86 {
87         /* we must have a lock on the FileManager's mutex */
88         
89         _sndfile = sf_open (_path.c_str(), _writeable ? SFM_RDWR : SFM_READ, _info);
90         return (_sndfile == 0);
91 }
92