added moodbar algorithm. about 80% complete.

git-svn-id: https://projectm.svn.sourceforge.net/svnroot/projectm/trunk@440 6778bc44-b910-0410-a7a0-be141de4315d
This commit is contained in:
w1z7ard
2007-09-17 04:59:11 +00:00
parent 645c9397c0
commit 0ff4d32b84
2 changed files with 135 additions and 0 deletions

View File

@ -0,0 +1,85 @@
//
// C++ Implementation: MoodBar
//
// Description:
//
//
// Author: Carmelo Piccione <carmelo.piccione@gmail.com>, (C) 2007
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "MoodBar.hpp"
#include <cmath>
extern "C" {
#include <malloc.h>
}
#include <iostream>
#include <cassert>
const unsigned int MoodBar::s_bark_bands[] =
{ 100, 200, 300, 400, 510, 630, 770, 920,
1080, 1270, 1480, 1720, 2000, 2320, 2700, 3150, 3700, 4400,
5300, 6400, 7700, 9500, 12000, 15500 };
void MoodBar::calculateMood
(const float * out, float * rgb) {
int i;
float real, imag;
for (i = 0; i < 24; ++i)
m_amplitudes[i] = 0.f;
for (i = 0; i < m_numFreqs; ++i)
{
real = out[2*i]; imag = out[2*i + 1];
m_amplitudes[m_barkband_table[i]] += sqrtf (real*real + imag*imag);
}
for (i = 0; i < 24; ++i)
rgb[i/8] += m_amplitudes[i] * m_amplitudes[i];
rgb[0] = sqrtf (rgb[0]);
rgb[1] = sqrtf (rgb[1]);
rgb[2] = sqrtf (rgb[2]);
/// @bug verify normalized values
std::cerr << "rgb: " << rgb[0] << "," << "," << rgb[1] << "," << rgb[2] << std::endl;
for (i = 0; i < 3;i++) {
assert(rgb[i] <= 1.0);
assert(rgb[i] >= 0.0);
}
}
/* This calculates a table that caches which bark band slot each
* incoming band is supposed to go in. */
void
MoodBar::calcBarkbandTable ()
{
unsigned int i;
unsigned int barkband = 0;
/* Avoid divide-by-zero */
if (!m_size || !m_rate)
return;
if (m_barkband_table)
free (m_barkband_table);
m_barkband_table = new unsigned int[(m_numFreqs * sizeof (unsigned int))];
for (i = 0; i < m_numFreqs; ++i)
{
if (barkband < 23 && 1)
//(unsigned int) GST_SPECTRUM_BAND_FREQ (i, m_size, m_rate)
// >= s_bark_bands[barkband])
barkband++;
m_barkband_table[i] = barkband;
}
}

View File

@ -0,0 +1,50 @@
/* projectM -- Milkdrop-esque visualisation SDK
Copyright (C)2003-2007 projectM Team
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* See 'LICENSE.txt' included within this release
*
*/
#ifndef _MOODBAR_HPP
#define _MOODBAR_HPP
class MoodBar {
public:
MoodBar(unsigned int numFreqs, int size, int rate) : m_numFreqs(numFreqs), m_size(size), m_rate(rate) {
calcBarkbandTable();
}
~MoodBar() { delete(m_barkband_table); }
/// Calculate rgb mood values. Out should be an array containing
/// numFreqs pairs of real/complex values.
void calculateMood(const float * out, float * rgb);
private:
unsigned int m_numFreqs;
int m_size;
int m_rate;
/* This calculates a table that caches which bark band slot each
* incoming band is supposed to go in. */
void calcBarkbandTable ();
float m_amplitudes[24];
static const unsigned int s_bark_bands[24];
unsigned int * m_barkband_table;
};
#endif