1
// ------------------------------------------------------------------------
2
// samplebuffer_functions.cpp: Extra functions for SAMPLE_BUFFER class
3
// Copyright (C) 2000,2001,2009 Kai Vehmanen
6
// eca-style-version: 3
8
// This program is free software; you can redistribute it and/or modify
9
// it under the terms of the GNU General Public License as published by
10
// the Free Software Foundation; either version 2 of the License, or
11
// (at your option) any later version.
13
// This program is distributed in the hope that it will be useful,
14
// but WITHOUT ANY WARRANTY; without even the implied warranty of
15
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
// GNU General Public License for more details.
18
// You should have received a copy of the GNU General Public License
19
// along with this program; if not, write to the Free Software
20
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
// ------------------------------------------------------------------------
35
#include "kvu_inttypes.h"
37
#include "samplebuffer.h"
38
#include "samplebuffer_functions.h"
40
#undef NEVER_USED_CODE
42
void SAMPLE_BUFFER_FUNCTIONS::fill_with_random_samples(SAMPLE_BUFFER *sbuf)
44
std::srand(std::time(0));
45
int ch_count = sbuf->number_of_channels();
46
int i_count = sbuf->length_in_samples();
48
for (int ch = 0; ch < ch_count; ch++) {
49
SAMPLE_BUFFER::sample_t *buf = sbuf->buffer[ch];
50
for (int i = 0; i < i_count; i++) {
51
int foo = std::rand();
52
assert(sizeof(SAMPLE_BUFFER::sample_t) <= sizeof(foo));
53
std::memcpy(buf, &foo, sizeof(SAMPLE_BUFFER::sample_t));
59
* Returns true if 'a' and 'b' have the same exact signal content,
60
* or if the two signals are sufficiently close to each
61
* other considering the limits imposed by implementation (e.g.
62
* precision of the floating point type used to represent
65
* @param bitprec adjust precision (defaults to 24)
66
* @param verbose_stderr whether to output comparison traces to
69
bool SAMPLE_BUFFER_FUNCTIONS::is_almost_equal(const SAMPLE_BUFFER& a, const SAMPLE_BUFFER& b, int bitprec, bool verbose_stderr)
71
if (a.number_of_channels() !=
72
b.number_of_channels())
75
if (a.length_in_samples() !=
76
b.length_in_samples())
79
int ch_count = a.number_of_channels();
80
int i_count = a.length_in_samples();
82
for (int ch = 0; ch < ch_count; ch++) {
83
for (int i = 0; i < i_count; i++) {
84
if (a.buffer[ch][i] != b.buffer[ch][i]) {
86
/* note: the following is intended only for comparing
87
* audio signals with a nominal range of [-1,1]
88
* and precision of 'bitprec' bits */
90
const SAMPLE_SPECS::sample_t diff_threshold = 1.0 / ((1 << bitprec) - 1);
92
SAMPLE_SPECS::sample_t diff =
93
std::fabs(a.buffer[ch][i] - b.buffer[ch][i]);
95
if (verbose_stderr == true) {
97
"%s: diff for sample ch%d[%d], diff %.30f [%s], (a=%.30f to b=%.30f, thrshd %.30f)\n",
100
diff > diff_threshold ? "MISMATCH" : "INRANGE",
101
a.buffer[ch][i], b.buffer[ch][i],
105
if (diff > diff_threshold) {
110
#if NEVER_USED_CODE /* integer-based comparison */
111
assert(sizeof(SAMPLE_BUFFER::sample_t) == sizeof(uint32_t));
112
/* allow diff of one in binary representation */
113
const int diff_threshold_ints = 1;
114
uint32_t aint = *reinterpret_cast<uint32_t*>(&a.buffer[ch][i]);
115
uint32_t bint = *reinterpret_cast<uint32_t*>(&b.buffer[ch][i]);
117
= std::labs(aint - bint);
118
if (diff_total > diff_threshold_ints) {