diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2014-06-24 08:13:32 +0200 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2014-06-24 08:13:32 +0200 |
commit | 0c835dce3bf01dc31b33311378043b3799150a64 (patch) | |
tree | 65197c9aa5eec4cedf9bbf9156b9165532d257df /src/test_audio.h |
Import from shared project.
Diffstat (limited to 'src/test_audio.h')
-rw-r--r-- | src/test_audio.h | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/src/test_audio.h b/src/test_audio.h new file mode 100644 index 0000000..68dac67 --- /dev/null +++ b/src/test_audio.h @@ -0,0 +1,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__*/ |