/* -*- 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 #include #include #ifdef VERBOSE #include #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__*/