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
|
/* -*- mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
* File: test_audio.h
* This file belongs to the Bifrost project.
* [FILL IN DESCRIPTION HERE]
* Date: Wed Feb 3 08:04:33 CET 2010
* Author: Bent Bisballe Nyeng
* Copyright: 2010
* Email: deva@aasimon.org
****************************************************************************/
#ifndef __BIFROST_TEST_AUDIO_H__
#define __BIFROST_TEST_AUDIO_H__
#include <math.h>
#include <time.h>
#include <stdlib.h>
#ifdef VERBOSE
#include <stdio.h>
#endif
static inline size_t getBufferSize(size_t samples, size_t channels)
{
return samples * channels * sizeof(short);
}
static inline char *getBuffer(size_t samples, size_t channels)
{
return (char*)calloc(getBufferSize(samples, channels), 1);
}
static inline char *getSineBuffer(size_t samples, size_t channels,
size_t srate, size_t freq)
{
short *s = (short*)getBuffer(samples, channels);
for(size_t i = 0; i < samples; i++) {
double x = (double)i / (double)srate;
double val = sin(x * 2 * M_PI * freq);
for(size_t c = 0; c < channels; c++) {
s[(i * channels) + c] = val * 32500;
}
}
return (char*)s;
}
static inline char *getNoiseBuffer(size_t samples, size_t channels)
{
srand(time(NULL));
short *s = (short*)getBuffer(samples, channels);
for(size_t i = 0; i < samples; i++) {
for(size_t c = 0; c < channels; c++) {
s[(i * channels) + c] = (rand() % 65000) - 32500;
}
}
return (char*)s;
}
static inline void normaliseBuffer(size_t samples, size_t channels, char *buf)
{
int max = 0;
short *s = (short*)buf;
for(size_t i = 0; i < samples; i++) {
for(size_t c = 0; c < channels; c++) {
if(max < abs(s[(i * channels) + c])) max = abs(s[(i * channels) + c]);
}
}
for(size_t i = 0; i < samples; i++) {
for(size_t c = 0; c < channels; c++) {
s[(i * channels) + c] *= 32500/max;
}
}
}
static inline double compareBuffers(size_t samples, size_t channels, char *buf1, char *buf2)
{
double diff = 0.0;
#ifdef NORM
normaliseBuffer(samples, channels, buf1);
normaliseBuffer(samples, channels, buf2);
#endif
short *s1 = (short*)buf1;
short *s2 = (short*)buf2;
for(size_t i = 0; i < samples; i++) {
for(size_t c = 0; c < channels; c++) {
diff += (double)abs(s1[(i * channels) + c] - s2[(i * channels) + c]) / (double)(samples*channels);
#ifdef VERBOSE
if(c == 0) fprintf(stderr, "%d\t%d\t~%d\t: %f\n",
s1[(i * channels) + c], s2[(i * channels) + c],
s1[(i * channels) + c] - s2[(i * channels) + c],
diff);
#endif
}
}
#ifdef VERBOSE
fprintf(stderr, "Diff: %f\n", diff);
#endif
return diff;
}
#endif/*__BIFROST_TEST_AUDIO_H__*/
|