File: utils.h

package info (click to toggle)
chromaprint 0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,312 kB
  • sloc: cpp: 5,218; ansic: 632; python: 164; sh: 79; makefile: 14
file content (154 lines) | stat: -rw-r--r-- 3,667 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * Chromaprint -- Audio fingerprinting toolkit
 * Copyright (C) 2010  Lukas Lalinsky <lalinsky@gmail.com>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

#ifndef CHROMAPRINT_UTILS_H_
#define CHROMAPRINT_UTILS_H_

#if defined(HAVE_CONFIG_H)
#include <config.h>
#endif

#include <math.h>
#include <stddef.h>
#include <stdint.h>
#include <limits>
#include <iterator>

#ifndef HAVE_ROUND
static inline double round(double x)
{
   if (x >= 0.0)
      return floor(x + 0.5);
   else
      return ceil(x - 0.5);
}
#endif

#define CHROMAPRINT_DISABLE_COPY(ClassName) \
	ClassName(const ClassName &);			\
	void operator=(const ClassName &);

//#include <iostream>

namespace Chromaprint
{

	template<class RandomAccessIterator>
	void PrepareHammingWindow(RandomAccessIterator first, RandomAccessIterator last)
	{
		size_t i = 0, max_i = last - first - 1;
		double scale = 2. * M_PI / max_i;
		while (first != last) {
			*first++ = 0.54 - 0.46 * cos(scale * i++);
		}
	}

	template<class InputIterator1, class InputIterator2, class OutputIterator>
	void ApplyWindow(InputIterator1 input, InputIterator2 window, OutputIterator output, int size, double scale)
	{
		while (size--) {
			*output = *input * *window * scale;
			++input;
			++window;
			++output;
		}
	}

	template<class Iterator>
	typename std::iterator_traits<Iterator>::value_type Sum(Iterator first, Iterator last)
	{
		typename std::iterator_traits<Iterator>::value_type sum = 0;
		while (first != last) {
			sum += *first;
			++first;
		}
		return sum;
	}

	template<class Iterator>
	typename std::iterator_traits<Iterator>::value_type EuclideanNorm(Iterator first, Iterator last)
	{
		typename std::iterator_traits<Iterator>::value_type squares = 0;
		while (first != last) {
			squares += *first * *first;
			++first;
		}
		return (squares > 0) ? sqrt(squares) : 0;
	}

	template<class Iterator, class Func>
	void NormalizeVector(Iterator first, Iterator last, Func func, double threshold = 0.01)
	{
		double norm = func(first, last);
		if (norm < threshold) {
			std::fill(first, last, 0.0);
		}
		else {
			while (first != last) {
				*first /= norm;
				++first;
			}
		}
	}

	inline int GrayCode(int i)
	{
		static const unsigned char CODES[] = { 0, 1, 3, 2 };
		return CODES[i];
	}

	inline double IndexToFreq(int i, int frame_size, int sample_rate)
	{
		return double(i) * sample_rate / frame_size;
	}

	inline int FreqToIndex(double freq, int frame_size, int sample_rate)
	{
		return (int)round(frame_size * freq / sample_rate);
	}

    inline int32_t UnsignedToSigned(uint32_t x)
    {
        return *((int32_t *)&x);
        //return x & 0x80000000 ? x & 0x7FFFFFFF - 0x80000000 : x;
    }

	template<class T>
	inline bool IsNaN(T value)
	{
		return value != value;
	}

	inline double FreqToBark(double f)
	{
		double z = (26.81 * f) / (1960.0 + f) - 0.53;

		if (z < 2.0) {
			z = z + 0.15 * (2.0 - z);
		} else if (z > 20.1) {
			z = z + 0.22 * (z - 20.1);
		}

		return z;
	}

};

#endif