Chris@1051: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@1051: Chris@1051: /* Chris@1051: Sonic Visualiser Chris@1051: An audio file viewer and annotation editor. Chris@1051: Centre for Digital Music, Queen Mary, University of London. Chris@1051: Chris@1051: This program is free software; you can redistribute it and/or Chris@1051: modify it under the terms of the GNU General Public License as Chris@1051: published by the Free Software Foundation; either version 2 of the Chris@1051: License, or (at your option) any later version. See the file Chris@1051: COPYING included with this distribution for more information. Chris@1051: */ Chris@1049: Chris@1528: #ifndef SV_BASE_TYPES_H Chris@1528: #define SV_BASE_TYPES_H Chris@1049: Chris@1049: #include Chris@1326: #include Chris@1326: #include Chris@1326: Chris@1326: #include Chris@1049: Chris@1051: /** Frame index, the unit of our time axis. This is signed because the Chris@1051: axis conceptually extends below zero: zero represents the start of Chris@1051: the main loaded audio model, not the start of time; a windowed Chris@1051: transform could legitimately produce results before then. We also Chris@1051: use this for frame counts, simply to avoid error-prone arithmetic Chris@1051: between signed and unsigned types. Chris@1051: */ Chris@1049: typedef int64_t sv_frame_t; Chris@1049: Chris@1051: /** Check whether an integer index is in range for a container, Chris@1051: avoiding overflows and signed/unsigned comparison warnings. Chris@1051: */ Chris@1049: template Chris@1049: bool in_range_for(const C &container, T i) Chris@1049: { Chris@1049: if (i < 0) return false; Chris@1049: if (sizeof(T) > sizeof(typename C::size_type)) { Chris@1429: return i < static_cast(container.size()); Chris@1049: } else { Chris@1429: return static_cast(i) < container.size(); Chris@1049: } Chris@1049: } Chris@1049: Chris@1051: /** Sample rate. We have to deal with sample rates provided as float Chris@1051: or (unsigned) int types, so we might as well have a type that can Chris@1051: represent both. Storage size isn't an issue anyway. Chris@1051: */ Chris@1051: typedef double sv_samplerate_t; Chris@1051: Chris@1326: typedef std::vector> floatvec_t; Chris@1326: Chris@1326: typedef std::vector, Chris@1326: breakfastquay::StlAllocator>> complexvec_t; Chris@1326: Chris@1324: /** Display zoom level. Can be an integer number of samples per pixel, Chris@1324: * or an integer number of pixels per sample. Chris@1324: */ Chris@1324: struct ZoomLevel { Chris@1324: Chris@1324: enum Zone { Chris@1324: FramesPerPixel, // zoomed out (as in classic SV) Chris@1324: PixelsPerFrame // zoomed in beyond 1-1 (interpolating the waveform) Chris@1324: }; Chris@1324: Zone zone; Chris@1324: int level; Chris@1324: Chris@1324: bool operator<(const ZoomLevel &other) const { Chris@1324: if (zone == FramesPerPixel) { Chris@1324: if (other.zone == zone) { Chris@1324: return level < other.level; Chris@1324: } else { Chris@1324: return false; Chris@1324: } Chris@1324: } else { Chris@1324: if (other.zone == zone) { Chris@1324: return level > other.level; Chris@1324: } else { Chris@1324: return false; Chris@1324: } Chris@1324: } Chris@1324: } Chris@1324: Chris@1324: ZoomLevel incremented() const { Chris@1324: if (zone == FramesPerPixel) { Chris@1324: return { zone, level + 1 }; Chris@1324: } else if (level == 1) { Chris@1324: return { FramesPerPixel, 2 }; Chris@1324: } else if (level == 2) { Chris@1324: return { FramesPerPixel, 1 }; Chris@1324: } else { Chris@1324: return { zone, level - 1 }; Chris@1324: } Chris@1324: } Chris@1324: Chris@1324: ZoomLevel decremented() const { Chris@1324: if (zone == PixelsPerFrame) { Chris@1324: return { zone, level + 1 }; Chris@1324: } else if (level == 1) { Chris@1324: return { PixelsPerFrame, 2 }; Chris@1324: } else { Chris@1324: return { zone, level - 1 }; Chris@1324: } Chris@1324: } Chris@1324: }; Chris@1324: Chris@1049: #endif Chris@1051: