From 21af6f2f454b78159ba49f4d75dceadebe25b9bf Mon Sep 17 00:00:00 2001 From: John Glover Date: Fri, 19 Aug 2011 16:07:38 +0100 Subject: Add initial simpl module C++ files --- src/simpl/base.cpp | 72 ++++++++++++ src/simpl/base.h | 58 ++++++++++ src/simpl/exceptions.cpp | 61 ++++++++++ src/simpl/exceptions.h | 284 +++++++++++++++++++++++++++++++++++++++++++++++ src/simpl/simplloris.cpp | 0 src/simpl/simplloris.h | 35 ++++++ 6 files changed, 510 insertions(+) create mode 100644 src/simpl/base.cpp create mode 100644 src/simpl/base.h create mode 100644 src/simpl/exceptions.cpp create mode 100644 src/simpl/exceptions.h create mode 100644 src/simpl/simplloris.cpp create mode 100644 src/simpl/simplloris.h (limited to 'src') diff --git a/src/simpl/base.cpp b/src/simpl/base.cpp new file mode 100644 index 0000000..4e34dfd --- /dev/null +++ b/src/simpl/base.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2009-2011 John Glover, National University of Ireland, Maynooth + * + * This program 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. + * + * This program 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 more 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include "base.h" + +using namespace std; + +namespace Simpl { + +Peak::Peak() +{ + amplitude = 0.0; + frequency = 0.0; + phase = 0.0; + next_peak = NULL; + previous_peak = NULL; + partial_id = 0; + partial_position = 0; + frame_number = 0; +} + +Peak::~Peak() +{ +} + +// Returns true iff this peak is unmatched in the given direction, and has positive amplitude +bool Peak::is_free(const char* direction) +{ + if(amplitude <= 0.0) + { + return false; + } + + if(direction == "forwards") + { + if(next_peak != NULL) + { + return false; + } + } + else if(direction == "backwards") + { + if(previous_peak != NULL) + { + return false; + } + } + else + { + Throw(InvalidArgument, "Invalid direction"); + } + + return true; +} + +} // end of namespace Simpl diff --git a/src/simpl/base.h b/src/simpl/base.h new file mode 100644 index 0000000..88ff0e9 --- /dev/null +++ b/src/simpl/base.h @@ -0,0 +1,58 @@ +#ifndef BASE_H +#define BASE_H +/* + * Copyright (c) 2009-2011 John Glover, National University of Ireland, Maynooth + * + * This program 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. + * + * This program 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 more 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include + +#include "exceptions.h" + +namespace Simpl +{ + +typedef double number; + +// A Spectral Peak +class Peak +{ +public: + number amplitude; + number frequency; + number phase; + Peak* next_peak; + Peak* previous_peak; + int partial_id; + int partial_position; + int frame_number; + + Peak(); + ~Peak(); + + bool is_start_of_partial() + { + return previous_peak == NULL; + }; + bool is_free(const char* direction="forwards"); +}; + +typedef std::vector Peaks; + +} // end of namespace Simpl + +#endif diff --git a/src/simpl/exceptions.cpp b/src/simpl/exceptions.cpp new file mode 100644 index 0000000..d64ae6f --- /dev/null +++ b/src/simpl/exceptions.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2009-2011 John Glover, National University of Ireland, Maynooth + * + * This program 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. + * + * This program 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 more 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include "exceptions.h" +#include + +namespace Simpl { + +// --------------------------------------------------------------------------- +// Exception constructor +// --------------------------------------------------------------------------- +//! Construct a new instance with the specified description and, optionally +//! a string identifying the location at which the exception as thrown. The +//! Throw(Exception_Class, description_string) macro generates a location +//! string automatically using __FILE__ and __LINE__. +//! +//! \param str is a string describing the exceptional condition +//! \param where is an option string describing the location in +//! the source code from which the exception was thrown +//! (generated automatically byt he Throw macro). +// +Exception::Exception(const std::string & str, const std::string & where) : + _sbuf(str) +{ + _sbuf.append(where); + _sbuf.append(" "); +} + +// --------------------------------------------------------------------------- +// append +// --------------------------------------------------------------------------- +//! Append the specified string to this Exception's description, +//! and return a reference to this Exception. +//! +//! \param str is text to append to the exception description +//! \return a reference to this Exception. +// +Exception & +Exception::append(const std::string & str) +{ + _sbuf.append(str); + return *this; +} + +} // end of namespace Simpl diff --git a/src/simpl/exceptions.h b/src/simpl/exceptions.h new file mode 100644 index 0000000..f556b6a --- /dev/null +++ b/src/simpl/exceptions.h @@ -0,0 +1,284 @@ +#ifndef EXCEPTIONS_H +#define EXCEPTIONS_H +/* + * Copyright (c) 2009-2011 John Glover, National University of Ireland, Maynooth + * + * This program 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. + * + * This program 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 more 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +// Mostly taken from the LorisException.h file in Loris (http://www.cerlsoundgroup.org/loris) + +#include +#include + +namespace Simpl { + +// --------------------------------------------------------------------------- +// class Exception +// +//! Exception is a generic exception class for reporting exceptional +//! circumstances in Simpl. Exception is derived from std:exception, +//! and is the base for a hierarchy of derived exception classes +//! in Simpl. +//! +// +class Exception : public std::exception +{ +public: + //! Construct a new instance with the specified description and, optionally + //! a string identifying the location at which the exception as thrown. The + //! Throw(Exception_Class, description_string) macro generates a location + //! string automatically using __FILE__ and __LINE__. + //! + //! \param str is a string describing the exceptional condition + //! \param where is an option string describing the location in + //! the source code from which the exception was thrown + //! (generated automatically by the Throw macro). + Exception(const std::string & str, const std::string & where = ""); + + //! Destroy this Exception. + virtual ~Exception(void) throw() {} + + //! Return a description of this Exception in the form of a + //! C-style string (char pointer). Overrides std::exception::what. + //! + //! \return a C-style string describing the exceptional condition. + const char * what(void) const throw() + { + return _sbuf.c_str(); + } + + //! Append the specified string to this Exception's description, + //! and return a reference to this Exception. + //! + //! \param str is text to append to the exception description + //! \return a reference to this Exception. + Exception & append(const std::string & str); + + //! Return a read-only refernce to this Exception's + //! description string. + //! + //! \return a string describing the exceptional condition + const std::string & str(void) const + { + return _sbuf; + } + +protected: + //! string for storing the exception description + std::string _sbuf; + +}; + +// ---------------------------------------------------------------------------- +// class AssertionFailure +// +//! Class of exceptions thrown when an assertion (usually representing an +//! invariant condition, and usually detected by the Assert macro) is +//! violated. +// +class AssertionFailure : public Exception +{ +public: + + //! Construct a new instance with the specified description and, optionally + //! a string identifying the location at which the exception as thrown. The + //! Throw(Exception_Class, description_string) macro generates a location + //! string automatically using __FILE__ and __LINE__. + //! + //! \param str is a string describing the exceptional condition + //! \param where is an option string describing the location in + //! the source code from which the exception was thrown + //! (generated automatically by the Throw macro). + AssertionFailure(const std::string & str, const std::string & where = "") : + Exception(std::string("Assertion failed -- ").append(str), where) + { + } + +}; + +// --------------------------------------------------------------------------- +// class IndexOutOfBounds +// +//! Class of exceptions thrown when a subscriptable object is accessed +//! with an index that is out of range. +// +class IndexOutOfBounds : public Exception +{ +public: + + //! Construct a new instance with the specified description and, optionally + //! a string identifying the location at which the exception as thrown. The + //! Throw(Exception_Class, description_string) macro generates a location + //! string automatically using __FILE__ and __LINE__. + //! + //! \param str is a string describing the exceptional condition + //! \param where is an option string describing the location in + //! the source code from which the exception was thrown + //! (generated automatically by the Throw macro). + IndexOutOfBounds(const std::string & str, const std::string & where = "") : + Exception(std::string("Index out of bounds -- ").append(str), where) {} + +}; + + +// --------------------------------------------------------------------------- +// class InvalidObject +// +//! Class of exceptions thrown when an object is found to be badly configured +//! or otherwise invalid. +// +class InvalidObject : public Exception +{ +public: + + //! Construct a new instance with the specified description and, optionally + //! a string identifying the location at which the exception as thrown. The + //! Throw(Exception_Class, description_string) macro generates a location + //! string automatically using __FILE__ and __LINE__. + //! + //! \param str is a string describing the exceptional condition + //! \param where is an option string describing the location in + //! the source code from which the exception was thrown + //! (generated automatically by the Throw macro). + InvalidObject(const std::string & str, const std::string & where = "") : + Exception(std::string("Invalid configuration or object -- ").append(str), where) + { + } + +}; + +// --------------------------------------------------------------------------- +// class InvalidIterator +// +//! Class of exceptions thrown when an Iterator is found to be badly configured +//! or otherwise invalid. +// +class InvalidIterator : public InvalidObject +{ +public: + + //! Construct a new instance with the specified description and, optionally + //! a string identifying the location at which the exception as thrown. The + //! Throw(Exception_Class, description_string) macro generates a location + //! string automatically using __FILE__ and __LINE__. + //! + //! \param str is a string describing the exceptional condition + //! \param where is an option string describing the location in + //! the source code from which the exception was thrown + //! (generated automatically by the Throw macro). + InvalidIterator(const std::string & str, const std::string & where = "") : + InvalidObject(std::string("Invalid Iterator -- ").append(str), where) + { + } + +}; + +// --------------------------------------------------------------------------- +// class InvalidArgument +// +//! Class of exceptions thrown when a function argument is found to be invalid. +// +class InvalidArgument : public Exception +{ +public: + + //! Construct a new instance with the specified description and, optionally + //! a string identifying the location at which the exception as thrown. The + //! Throw(Exception_Class, description_string) macro generates a location + //! string automatically using __FILE__ and __LINE__. + //! + //! \param str is a string describing the exceptional condition + //! \param where is an option string describing the location in + //! the source code from which the exception was thrown + //! (generated automatically by the Throw macro). + InvalidArgument(const std::string & str, const std::string & where = "") : + Exception(std::string("Invalid Argument -- ").append(str), where) + { + } + +}; + +// --------------------------------------------------------------------------- +// class RuntimeError +// +//! Class of exceptions thrown when an unanticipated runtime error is +//! encountered. +// +class RuntimeError : public Exception +{ +public: + + //! Construct a new instance with the specified description and, optionally + //! a string identifying the location at which the exception as thrown. The + //! Throw(Exception_Class, description_string) macro generates a location + //! string automatically using __FILE__ and __LINE__. + //! + //! \param str is a string describing the exceptional condition + //! \param where is an option string describing the location in + //! the source code from which the exception was thrown + //! (generated automatically by the Throw macro). + RuntimeError(const std::string & str, const std::string & where = "") : + Exception(std::string("Runtime Error -- ").append(str), where) + { + } + +}; + +// --------------------------------------------------------------------------- +// class FileIOException +// +//! Class of exceptions thrown when file input or output fails. +// +class FileIOException : public RuntimeError +{ +public: + + //! Construct a new instance with the specified description and, optionally + //! a string identifying the location at which the exception as thrown. The + //! Throw(Exception_Class, description_string) macro generates a location + //! string automatically using __FILE__ and __LINE__. + //! + //! \param str is a string describing the exceptional condition + //! \param where is an option string describing the location in + //! the source code from which the exception was thrown + //! (generated automatically by the Throw macro). + FileIOException(const std::string & str, const std::string & where = "") : + RuntimeError(std::string("File i/o error -- ").append(str), where) + { + } + +}; + +// --------------------------------------------------------------------------- +// macros for throwing exceptions +// +// The compelling reason for using macros instead of inlines for all these +// things is that the __FILE__ and __LINE__ macros will be useful. +// +#define __STR(x) __VAL(x) +#define __VAL(x) #x +#define Throw(exType, report) \ + throw exType(report, " (" __FILE__ " line: " __STR(__LINE__)") ") + +#define Assert(test) \ + do { \ + if (!(test)) Throw(Simpl::AssertionFailure, #test); \ + } while (false) + +} // end of namespace Simpl + +#endif diff --git a/src/simpl/simplloris.cpp b/src/simpl/simplloris.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/simpl/simplloris.h b/src/simpl/simplloris.h new file mode 100644 index 0000000..fe2f9fc --- /dev/null +++ b/src/simpl/simplloris.h @@ -0,0 +1,35 @@ +#ifndef SIMPLLORIS_H +#define SIMPLLORIS_H +/* + * Copyright (c) 2009-2011 John Glover, National University of Ireland, Maynooth + * + * This program 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. + * + * This program 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 more 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include + +namespace Simpl +{ + +class LorisPeakDetection +{ +public: + LorisPeakDetection(){}; +}; + +} // end of namespace Simpl + +#endif -- cgit v1.2.3