annotate base/BaseTypes.h @ 1529:c1c45c5146bb zoom

Merge from default branch
author Chris Cannam
date Tue, 18 Sep 2018 15:06:58 +0100
parents 710e6250a401 a7485c1bdba5
children bf32b26d1dad
rev   line source
Chris@1051 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1051 2
Chris@1051 3 /*
Chris@1051 4 Sonic Visualiser
Chris@1051 5 An audio file viewer and annotation editor.
Chris@1051 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1051 7
Chris@1051 8 This program is free software; you can redistribute it and/or
Chris@1051 9 modify it under the terms of the GNU General Public License as
Chris@1051 10 published by the Free Software Foundation; either version 2 of the
Chris@1051 11 License, or (at your option) any later version. See the file
Chris@1051 12 COPYING included with this distribution for more information.
Chris@1051 13 */
Chris@1049 14
Chris@1528 15 #ifndef SV_BASE_TYPES_H
Chris@1528 16 #define SV_BASE_TYPES_H
Chris@1049 17
Chris@1049 18 #include <cstdint>
Chris@1326 19 #include <complex>
Chris@1326 20 #include <vector>
Chris@1326 21
Chris@1326 22 #include <bqvec/Allocators.h>
Chris@1049 23
Chris@1051 24 /** Frame index, the unit of our time axis. This is signed because the
Chris@1051 25 axis conceptually extends below zero: zero represents the start of
Chris@1051 26 the main loaded audio model, not the start of time; a windowed
Chris@1051 27 transform could legitimately produce results before then. We also
Chris@1051 28 use this for frame counts, simply to avoid error-prone arithmetic
Chris@1051 29 between signed and unsigned types.
Chris@1051 30 */
Chris@1049 31 typedef int64_t sv_frame_t;
Chris@1049 32
Chris@1051 33 /** Check whether an integer index is in range for a container,
Chris@1051 34 avoiding overflows and signed/unsigned comparison warnings.
Chris@1051 35 */
Chris@1049 36 template<typename T, typename C>
Chris@1049 37 bool in_range_for(const C &container, T i)
Chris@1049 38 {
Chris@1049 39 if (i < 0) return false;
Chris@1049 40 if (sizeof(T) > sizeof(typename C::size_type)) {
Chris@1429 41 return i < static_cast<T>(container.size());
Chris@1049 42 } else {
Chris@1429 43 return static_cast<typename C::size_type>(i) < container.size();
Chris@1049 44 }
Chris@1049 45 }
Chris@1049 46
Chris@1051 47 /** Sample rate. We have to deal with sample rates provided as float
Chris@1051 48 or (unsigned) int types, so we might as well have a type that can
Chris@1051 49 represent both. Storage size isn't an issue anyway.
Chris@1051 50 */
Chris@1051 51 typedef double sv_samplerate_t;
Chris@1051 52
Chris@1326 53 typedef std::vector<float, breakfastquay::StlAllocator<float>> floatvec_t;
Chris@1326 54
Chris@1326 55 typedef std::vector<std::complex<float>,
Chris@1326 56 breakfastquay::StlAllocator<std::complex<float>>> complexvec_t;
Chris@1326 57
Chris@1324 58 /** Display zoom level. Can be an integer number of samples per pixel,
Chris@1324 59 * or an integer number of pixels per sample.
Chris@1324 60 */
Chris@1324 61 struct ZoomLevel {
Chris@1324 62
Chris@1324 63 enum Zone {
Chris@1324 64 FramesPerPixel, // zoomed out (as in classic SV)
Chris@1324 65 PixelsPerFrame // zoomed in beyond 1-1 (interpolating the waveform)
Chris@1324 66 };
Chris@1324 67 Zone zone;
Chris@1324 68 int level;
Chris@1324 69
Chris@1324 70 bool operator<(const ZoomLevel &other) const {
Chris@1324 71 if (zone == FramesPerPixel) {
Chris@1324 72 if (other.zone == zone) {
Chris@1324 73 return level < other.level;
Chris@1324 74 } else {
Chris@1324 75 return false;
Chris@1324 76 }
Chris@1324 77 } else {
Chris@1324 78 if (other.zone == zone) {
Chris@1324 79 return level > other.level;
Chris@1324 80 } else {
Chris@1324 81 return false;
Chris@1324 82 }
Chris@1324 83 }
Chris@1324 84 }
Chris@1324 85
Chris@1324 86 ZoomLevel incremented() const {
Chris@1324 87 if (zone == FramesPerPixel) {
Chris@1324 88 return { zone, level + 1 };
Chris@1324 89 } else if (level == 1) {
Chris@1324 90 return { FramesPerPixel, 2 };
Chris@1324 91 } else if (level == 2) {
Chris@1324 92 return { FramesPerPixel, 1 };
Chris@1324 93 } else {
Chris@1324 94 return { zone, level - 1 };
Chris@1324 95 }
Chris@1324 96 }
Chris@1324 97
Chris@1324 98 ZoomLevel decremented() const {
Chris@1324 99 if (zone == PixelsPerFrame) {
Chris@1324 100 return { zone, level + 1 };
Chris@1324 101 } else if (level == 1) {
Chris@1324 102 return { PixelsPerFrame, 2 };
Chris@1324 103 } else {
Chris@1324 104 return { zone, level - 1 };
Chris@1324 105 }
Chris@1324 106 }
Chris@1324 107 };
Chris@1324 108
Chris@1049 109 #endif
Chris@1051 110