~ubuntu-branches/ubuntu/saucy/ecasound2.2/saucy

« back to all changes in this revision

Viewing changes to libecasound/samplebuffer_functions.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Junichi Uekawa
  • Date: 2009-11-02 18:22:35 UTC
  • mfrom: (5.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20091102182235-4ngh7699dmkgonyu
Tags: 2.7.0-1
* New upstream release.
* Depend on libreadline-dev instead of libreadline5-dev by request of
  Mattias Klose. It's now libreadline6-dev. (closes: #553748)
* Update menu file to use section Applications/ instead of Apps/.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ------------------------------------------------------------------------
 
2
// samplebuffer_functions.cpp: Extra functions for SAMPLE_BUFFER class
 
3
// Copyright (C) 2000,2001,2009 Kai Vehmanen
 
4
//
 
5
// Attributes:
 
6
//     eca-style-version: 3
 
7
//
 
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.
 
12
// 
 
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.
 
17
// 
 
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
// ------------------------------------------------------------------------
 
22
 
 
23
#ifdef HAVE_CONFIG_H
 
24
#include <config.h>
 
25
#endif
 
26
 
 
27
#include <string>
 
28
#include <cassert>
 
29
#include <cstdio>
 
30
#include <cstdlib>
 
31
#include <cstring>
 
32
#include <ctime>
 
33
 
 
34
#include "kvu_dbc.h"
 
35
#include "kvu_inttypes.h"
 
36
 
 
37
#include "samplebuffer.h"
 
38
#include "samplebuffer_functions.h"
 
39
 
 
40
#undef NEVER_USED_CODE
 
41
 
 
42
void SAMPLE_BUFFER_FUNCTIONS::fill_with_random_samples(SAMPLE_BUFFER *sbuf)
 
43
{
 
44
  std::srand(std::time(0));
 
45
  int ch_count = sbuf->number_of_channels();
 
46
  int i_count = sbuf->length_in_samples();
 
47
 
 
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));
 
54
    }
 
55
  }
 
56
}
 
57
 
 
58
/**
 
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
 
63
 * a sample). 
 
64
 *
 
65
 * @param bitprec adjust precision (defaults to 24)
 
66
 * @param verbose_stderr whether to output comparison traces to
 
67
 *        stderr
 
68
 */
 
69
bool SAMPLE_BUFFER_FUNCTIONS::is_almost_equal(const SAMPLE_BUFFER& a, const SAMPLE_BUFFER& b, int bitprec, bool verbose_stderr)
 
70
{
 
71
  if (a.number_of_channels() !=
 
72
      b.number_of_channels())
 
73
    return false;
 
74
 
 
75
  if (a.length_in_samples() !=
 
76
      b.length_in_samples())
 
77
    return false;
 
78
  
 
79
  int ch_count = a.number_of_channels();
 
80
  int i_count = a.length_in_samples();
 
81
 
 
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]) {
 
85
 
 
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 */
 
89
 
 
90
        const SAMPLE_SPECS::sample_t diff_threshold = 1.0 / ((1 << bitprec) - 1);
 
91
 
 
92
        SAMPLE_SPECS::sample_t diff = 
 
93
          std::fabs(a.buffer[ch][i] - b.buffer[ch][i]);
 
94
 
 
95
        if (verbose_stderr == true) {   
 
96
          std::fprintf(stderr, 
 
97
                       "%s: diff for sample ch%d[%d], diff %.30f [%s], (a=%.30f to b=%.30f, thrshd %.30f)\n",
 
98
                       __FILE__, ch, i, 
 
99
                       diff,
 
100
                       diff > diff_threshold ? "MISMATCH" : "INRANGE",
 
101
                       a.buffer[ch][i], b.buffer[ch][i],
 
102
                       diff_threshold);
 
103
        }
 
104
 
 
105
        if (diff > diff_threshold) {
 
106
            return false;
 
107
        }
 
108
 
 
109
        {
 
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]);
 
116
          uint32_t diff 
 
117
            = std::labs(aint - bint);
 
118
          if (diff_total > diff_threshold_ints) {
 
119
            return false;
 
120
          }
 
121
#endif
 
122
        }
 
123
 
 
124
      }
 
125
    }
 
126
  }
 
127
 
 
128
  return true;
 
129
}