Fix for undefined references to fftw lib when using gcc/mingw
[ardour.git] / libs / audiographer / private / sndfile.hh
index 91e99875acfa5bb01611bd563e0b47f3fe763cf3..8b7e1928e43ff31241901f1a67a6cde07e248d13 100644 (file)
 #ifndef SNDFILE_HH
 #define SNDFILE_HH
 
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include <glib.h>
+#include "pbd/gstdio_compat.h"
+
 #include <sndfile.h>
 
 #include <string>
@@ -87,6 +94,8 @@ class SndfileHandle
                        SndfileHandle (const SndfileHandle &orig) ;
                        SndfileHandle & operator = (const SndfileHandle &rhs) ;
 
+                       void close (void) ;
+
                /* Mainly for debugging/testing. */
                int refCount (void) const { return (p == NULL) ? 0 : p->ref ; }
 
@@ -150,7 +159,19 @@ SndfileHandle::SNDFILE_ref::SNDFILE_ref (void)
 
 inline
 SndfileHandle::SNDFILE_ref::~SNDFILE_ref (void)
-{      if (sf != NULL) sf_close (sf) ; }
+{      if (sf != NULL) { sf_close (sf) ; } }
+
+
+void
+SndfileHandle::close (void)
+{
+       if (p != NULL && --p->ref == 0)
+       {
+               delete p ;
+               p = NULL;
+       }
+}
+
 
 inline
 SndfileHandle::SndfileHandle (const char *path, int mode, int fmt, int chans, int srate)
@@ -168,7 +189,17 @@ SndfileHandle::SndfileHandle (const char *path, int mode, int fmt, int chans, in
                p->sfinfo.sections = 0 ;
                p->sfinfo.seekable = 0 ;
 
-               p->sf = sf_open (path, mode, &p->sfinfo) ;
+               bool writable = false;
+               if (mode & SFM_WRITE) {
+                       writable = true;
+               }
+#ifdef PLATFORM_WINDOWS
+               int fd = g_open (path, writable ? O_CREAT | O_RDWR : O_RDONLY, writable ? 0644 : 0444);
+#else
+               int fd = ::open (path, writable ? O_CREAT | O_RDWR : O_RDONLY, writable ? 0644 : 0444);
+#endif
+
+               p->sf = sf_open_fd (fd, mode, &p->sfinfo, true) ;
                } ;
 
        return ;
@@ -190,7 +221,17 @@ SndfileHandle::SndfileHandle (std::string const & path, int mode, int fmt, int c
                p->sfinfo.sections = 0 ;
                p->sfinfo.seekable = 0 ;
 
-               p->sf = sf_open (path.c_str (), mode, &p->sfinfo) ;
+               bool writable = false;
+               if (mode & SFM_WRITE) {
+                       writable = true;
+               }
+#ifdef PLATFORM_WINDOWS
+               int fd = g_open (path.c_str(), writable ? O_CREAT | O_RDWR : O_RDONLY, writable ? 0644 : 0444);
+#else
+               int fd = ::open (path.c_str(), writable ? O_CREAT | O_RDWR : O_RDONLY, writable ? 0644 : 0444);
+#endif
+
+               p->sf = sf_open_fd (fd, mode, &p->sfinfo, true) ;
                } ;
 
        return ;