Use test utility function to find evoral test files
[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/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <cassert>
29 #include "pbd/compose.h"
30 #include "pbd/sndfile_manager.h"
31 #include "pbd/debug.h"
32
33 using namespace std;
34 using namespace PBD;
35
36 /** @param file_name Filename.
37  *  @param writeable true to open writeable, otherwise false.
38  *  @param info SF_INFO for the file.
39  */
40
41 SndFileDescriptor::SndFileDescriptor (string const & file_name, bool writeable, SF_INFO* info)
42         : FileDescriptor (file_name, writeable)
43         , _sndfile (0)
44         , _info (info)
45 {
46         manager()->add (this);
47 }
48
49 SndFileDescriptor::~SndFileDescriptor ()
50 {
51         manager()->remove (this);
52 }
53
54 /** @return SNDFILE*, or 0 on error */
55 SNDFILE*
56 SndFileDescriptor::allocate ()
57 {
58         bool const f = manager()->allocate (this);
59         if (f) {
60                 return 0;
61         }
62
63         /* this is ok thread-wise because allocate () has incremented
64            the Descriptor's refcount, so the file will not be closed
65         */
66         return _sndfile;
67 }
68
69 void
70 SndFileDescriptor::close ()
71 {
72         /* we must have a lock on the FileManager's mutex */
73
74         assert (_sndfile);
75         sf_close (_sndfile);
76         _sndfile = 0;
77 }
78
79 bool
80 SndFileDescriptor::is_open () const
81 {
82         /* we must have a lock on the FileManager's mutex */
83
84         return _sndfile != 0;
85 }
86
87 bool
88 SndFileDescriptor::open ()
89 {
90         /* we must have a lock on the FileManager's mutex */
91         
92         _sndfile = sf_open (_path.c_str(), _writeable ? SFM_RDWR : SFM_READ, _info);
93         return (_sndfile == 0);
94 }
95