verbosity--
[ardour.git] / libs / evoral / src / LibSMF.cpp
index c0c59fa3f0191fcabb46a398ec54d3dfb910e6f4..832d5f322575a0eb6831214c5b11caf2ffe9b40f 100644 (file)
@@ -1,13 +1,41 @@
-#include "evoral/LibSMF.hpp"
-#include "evoral/Event.hpp"
-#include <cassert>
+/* This file is part of Evoral.
+ * Copyright (C) 2008 Dave Robillard <http://drobilla.net>
+ * Copyright (C) 2000-2008 Paul Davis
+ * Author: Hans Baier
+ * 
+ * Evoral is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation; either version 2 of the License, or (at your option) any later
+ * version.
+ * 
+ * Evoral is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for details.
+ * 
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
 
+#include <cassert>
 #include <iostream>
+#include "evoral/Event.hpp"
+#include "evoral/LibSMF.hpp"
+#include "libsmf/smf.h"
 
 using namespace std;
 
 namespace Evoral {
 
+template<typename Time>
+LibSMF<Time>::~LibSMF()
+{      
+       if (_smf) {
+               smf_delete(_smf);
+               _smf = 0;
+               _smf_track = 0;
+       } 
+}
 
 /** Attempt to open the SMF file for reading and writing.
  *
@@ -18,7 +46,7 @@ namespace Evoral {
  */
 template<typename Time>
 int
-LibSMF<Time>::open(const std::string& path)
+LibSMF<Time>::open(const std::string& path) THROW_FILE_ERROR
 {
        if (_smf) { 
                smf_delete(_smf);
@@ -27,7 +55,9 @@ LibSMF<Time>::open(const std::string& path)
        _smf = smf_load(path.c_str());
        if (!_smf) {
                _smf = smf_new();
-               smf_set_ppqn(_smf, _ppqn);
+               if (smf_set_ppqn(_smf, _ppqn) != 0) {
+                       throw typename MIDIFile<Time>::FileError();
+               }
                
                if(_smf == NULL) {
                        return -1;
@@ -46,11 +76,13 @@ LibSMF<Time>::open(const std::string& path)
 
 template<typename Time>
 void
-LibSMF<Time>::close()
+LibSMF<Time>::close() THROW_FILE_ERROR
 {
        assert(false);
        if (_smf) {
-               smf_save(_smf, _path.c_str());
+               if (smf_save(_smf, _path.c_str()) != 0) {
+                       throw typename MIDIFile<Time>::FileError();
+               }
                smf_delete(_smf);
                _smf = 0;
                _smf_track = 0;
@@ -112,7 +144,7 @@ LibSMF<Time>::read_event(uint32_t* delta_t, uint32_t* size, uint8_t** buf) const
 
 template<typename Time>
 void
-LibSMF<Time>::append_event_unlocked(uint32_t delta_t, const Event<Time>& ev)
+LibSMF<Time>::append_event_delta(uint32_t delta_t, const Event<Time>& ev)
 {
        assert(ev.size() > 0);
        
@@ -134,7 +166,7 @@ LibSMF<Time>::append_event_unlocked(uint32_t delta_t, const Event<Time>& ev)
 
 template<typename Time>
 void
-LibSMF<Time>::begin_write(FrameTime start_frame)
+LibSMF<Time>::begin_write()
 {
        assert(_smf_track);
        smf_track_delete(_smf_track);
@@ -150,9 +182,10 @@ LibSMF<Time>::begin_write(FrameTime start_frame)
 
 template<typename Time>
 void
-LibSMF<Time>::end_write()
+LibSMF<Time>::end_write() THROW_FILE_ERROR
 {
-       smf_save(_smf, _path.c_str());
+       if (smf_save(_smf, _path.c_str()) != 0)
+               throw typename MIDIFile<Time>::FileError();
 }
 
 template class LibSMF<double>;