2010-12-08 04:36:10 +01:00
|
|
|
// Copyright (c) 2005-2006, Rodrigo Braz Monteiro
|
|
|
|
// Copyright (c) 2006-2010, Niels Martin Hansen
|
2007-01-07 05:44:11 +01:00
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Aegisub Group nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
2007-01-07 05:44:11 +01:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// $Id$
|
|
|
|
|
2009-08-13 19:28:12 +02:00
|
|
|
/// @file audio_renderer_spectrum.cpp
|
2009-07-29 07:43:02 +02:00
|
|
|
/// @brief Caching frequency-power spectrum renderer for audio display
|
|
|
|
/// @ingroup audio_ui
|
2007-01-07 05:44:11 +01:00
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2011-11-18 23:58:02 +01:00
|
|
|
#include "audio_renderer_spectrum.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
|
2011-11-18 23:58:02 +01:00
|
|
|
#include "audio_colorscheme.h"
|
|
|
|
#ifndef WITH_FFTW
|
|
|
|
#include "fft.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
#endif
|
2011-11-18 23:58:02 +01:00
|
|
|
#include "include/aegisub/audio_provider.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "utils.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
|
2010-06-09 01:21:39 +02:00
|
|
|
#include <libaegisub/log.h>
|
|
|
|
|
2011-11-18 23:58:02 +01:00
|
|
|
#ifndef AGI_PRE
|
|
|
|
#include <algorithm>
|
2010-12-08 04:36:10 +01:00
|
|
|
|
2011-11-18 23:58:02 +01:00
|
|
|
#include <wx/image.h>
|
|
|
|
#include <wx/rawbmp.h>
|
|
|
|
#include <wx/dcmemory.h>
|
2010-12-08 04:36:10 +01:00
|
|
|
#endif
|
2007-01-07 05:44:11 +01:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
/// Allocates blocks of derived data for the audio spectrum
|
|
|
|
struct AudioSpectrumCacheBlockFactory {
|
|
|
|
/// Pointer back to the owning spectrum renderer
|
|
|
|
AudioSpectrumRenderer *spectrum;
|
2007-01-07 05:44:11 +01:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
/// @brief Constructor
|
|
|
|
/// @param s The owning spectrum renderer
|
|
|
|
AudioSpectrumCacheBlockFactory(AudioSpectrumRenderer *s) : spectrum(s) { }
|
2007-06-30 16:40:52 +02:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
/// @brief Allocate and fill a data block
|
|
|
|
/// @param i Index of the block to produce data for
|
|
|
|
/// @return Newly allocated and filled block
|
2009-08-01 03:55:17 +02:00
|
|
|
///
|
2010-12-08 04:36:10 +01:00
|
|
|
/// The filling is delegated to the spectrum renderer
|
|
|
|
float *ProduceBlock(size_t i)
|
2007-06-30 16:40:52 +02:00
|
|
|
{
|
2010-12-08 04:36:10 +01:00
|
|
|
float *res = new float[((size_t)1)<<spectrum->derivation_size];
|
|
|
|
spectrum->FillBlock(i, res);
|
|
|
|
return res;
|
2007-01-07 05:44:11 +01:00
|
|
|
}
|
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
/// @brief De-allocate a cache block
|
|
|
|
/// @param block The block to dispose of
|
|
|
|
void DisposeBlock(float *block)
|
2007-06-30 16:40:52 +02:00
|
|
|
{
|
2010-12-08 04:36:10 +01:00
|
|
|
delete[] block;
|
2007-06-30 16:40:52 +02:00
|
|
|
}
|
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
/// @brief Calculate the in-memory size of a spec
|
|
|
|
/// @return The size in bytes of a spectrum cache block
|
|
|
|
size_t GetBlockSize() const
|
2007-06-30 16:40:52 +02:00
|
|
|
{
|
2010-12-08 04:36:10 +01:00
|
|
|
return sizeof(float) << spectrum->derivation_size;
|
2007-06-30 16:40:52 +02:00
|
|
|
}
|
2010-12-08 04:36:10 +01:00
|
|
|
};
|
2007-06-30 16:40:52 +02:00
|
|
|
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
/// @brief Cache for audio spectrum frequency-power data
|
|
|
|
class AudioSpectrumCache
|
2011-11-18 23:58:02 +01:00
|
|
|
: public DataBlockCache<float, 10, AudioSpectrumCacheBlockFactory> {
|
2010-12-08 04:36:10 +01:00
|
|
|
public:
|
|
|
|
AudioSpectrumCache(size_t block_count, AudioSpectrumRenderer *renderer)
|
2011-11-18 23:58:02 +01:00
|
|
|
: DataBlockCache<float, 10, AudioSpectrumCacheBlockFactory>(
|
2010-12-08 04:36:10 +01:00
|
|
|
block_count, AudioSpectrumCacheBlockFactory(renderer))
|
2007-06-30 16:40:52 +02:00
|
|
|
{
|
|
|
|
}
|
2010-12-08 04:36:10 +01:00
|
|
|
};
|
2007-06-30 16:40:52 +02:00
|
|
|
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
AudioSpectrumRenderer::AudioSpectrumRenderer()
|
2011-11-18 23:58:02 +01:00
|
|
|
: colors_normal(new AudioColorScheme(12))
|
|
|
|
, colors_selected(new AudioColorScheme(12))
|
|
|
|
, colors_inactive(new AudioColorScheme(12))
|
2010-12-08 04:36:10 +01:00
|
|
|
, derivation_size(8)
|
|
|
|
, derivation_dist(8)
|
|
|
|
#ifdef WITH_FFTW
|
|
|
|
, dft_plan(0)
|
|
|
|
, dft_input(0)
|
|
|
|
, dft_output(0)
|
2008-01-15 23:18:54 +01:00
|
|
|
#endif
|
2010-12-08 04:36:10 +01:00
|
|
|
{
|
2011-11-18 23:58:02 +01:00
|
|
|
colors_normal->InitIcyBlue(AudioStyle_Normal);
|
|
|
|
colors_selected->InitIcyBlue(AudioStyle_Selected);
|
|
|
|
colors_inactive->InitIcyBlue(AudioStyle_Inactive);
|
2010-12-08 04:36:10 +01:00
|
|
|
}
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
AudioSpectrumRenderer::~AudioSpectrumRenderer()
|
|
|
|
{
|
|
|
|
// This sequence will clean up
|
|
|
|
provider = 0;
|
|
|
|
RecreateCache();
|
|
|
|
}
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
void AudioSpectrumRenderer::RecreateCache()
|
|
|
|
{
|
|
|
|
#ifdef WITH_FFTW
|
|
|
|
if (dft_plan)
|
2007-01-07 05:44:11 +01:00
|
|
|
{
|
2010-12-08 04:36:10 +01:00
|
|
|
fftw_destroy_plan(dft_plan);
|
|
|
|
fftw_free(dft_input);
|
|
|
|
fftw_free(dft_output);
|
|
|
|
dft_plan = 0;
|
|
|
|
dft_input = 0;
|
|
|
|
dft_output = 0;
|
2007-01-07 05:44:11 +01:00
|
|
|
}
|
2010-12-08 04:36:10 +01:00
|
|
|
#endif
|
2007-01-07 05:44:11 +01:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
if (provider)
|
2007-06-30 16:40:52 +02:00
|
|
|
{
|
2010-12-08 04:36:10 +01:00
|
|
|
size_t block_count = (size_t)((provider->GetNumSamples() + (size_t)(1<<derivation_dist) - 1) >> derivation_dist);
|
2011-11-18 23:58:02 +01:00
|
|
|
cache.reset(new AudioSpectrumCache(block_count, this));
|
2010-12-08 04:36:10 +01:00
|
|
|
|
|
|
|
#ifdef WITH_FFTW
|
|
|
|
dft_input = (double*)fftw_malloc(sizeof(double) * (2<<derivation_size));
|
|
|
|
dft_output = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * (2<<derivation_size));
|
|
|
|
dft_plan = fftw_plan_dft_r2c_1d(
|
|
|
|
2<<derivation_size,
|
|
|
|
dft_input,
|
|
|
|
dft_output,
|
|
|
|
FFTW_MEASURE);
|
|
|
|
#else
|
|
|
|
// Allocate scratch for 6x the derivation size:
|
|
|
|
// 2x for the input sample data
|
|
|
|
// 2x for the real part of the output
|
|
|
|
// 2x for the imaginary part of the output
|
2011-11-18 23:58:02 +01:00
|
|
|
fft_scratch.resize(6 << derivation_size);
|
2010-12-08 04:36:10 +01:00
|
|
|
#endif
|
2011-11-18 23:58:02 +01:00
|
|
|
audio_scratch.resize(2 << derivation_size);
|
2007-06-30 16:40:52 +02:00
|
|
|
}
|
2010-12-08 04:36:10 +01:00
|
|
|
}
|
2007-06-30 16:40:52 +02:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
void AudioSpectrumRenderer::OnSetProvider()
|
|
|
|
{
|
|
|
|
RecreateCache();
|
|
|
|
}
|
2007-06-30 16:40:52 +02:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
void AudioSpectrumRenderer::SetResolution(size_t _derivation_size, size_t _derivation_dist)
|
|
|
|
{
|
|
|
|
if (derivation_dist != _derivation_dist)
|
2007-06-30 16:40:52 +02:00
|
|
|
{
|
2010-12-08 04:36:10 +01:00
|
|
|
derivation_dist = _derivation_dist;
|
|
|
|
if (cache)
|
|
|
|
cache->Age(0);
|
2007-06-30 16:40:52 +02:00
|
|
|
}
|
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
if (derivation_size != _derivation_size)
|
2007-01-07 05:44:11 +01:00
|
|
|
{
|
2010-12-08 04:36:10 +01:00
|
|
|
derivation_size = _derivation_size;
|
|
|
|
RecreateCache();
|
|
|
|
}
|
|
|
|
}
|
2007-01-07 05:44:11 +01:00
|
|
|
|
2011-11-18 23:58:02 +01:00
|
|
|
template<class T>
|
|
|
|
void AudioSpectrumRenderer::ConvertToFloat(size_t count, T &dest) {
|
|
|
|
for (size_t si = 0; si < count; ++si)
|
|
|
|
{
|
|
|
|
dest[si] = (float)(audio_scratch[si]) / 32768.f;
|
|
|
|
}
|
|
|
|
}
|
2007-01-07 05:44:11 +01:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
void AudioSpectrumRenderer::FillBlock(size_t block_index, float *block)
|
|
|
|
{
|
|
|
|
assert(cache);
|
|
|
|
assert(block);
|
2007-01-07 05:44:11 +01:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
int64_t first_sample = ((int64_t)block_index) << derivation_dist;
|
2011-11-18 23:58:02 +01:00
|
|
|
provider->GetAudio(&audio_scratch[0], first_sample, 2 << derivation_size);
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
#ifdef WITH_FFTW
|
2011-11-18 23:58:02 +01:00
|
|
|
ConvertToFloat(2 << derivation_size, dft_input);
|
2007-01-07 05:44:11 +01:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
fftw_execute(dft_plan);
|
2007-06-30 16:40:52 +02:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
float scale_factor = 9 / sqrt(2 * (float)(2<<derivation_size));
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
fftw_complex *o = dft_output;
|
|
|
|
for (size_t si = 1<<derivation_size; si > 0; --si)
|
2007-06-30 16:40:52 +02:00
|
|
|
{
|
2010-12-08 04:36:10 +01:00
|
|
|
*block++ = log10( sqrt(o[0][0] * o[0][0] + o[0][1] * o[0][1]) * scale_factor + 1 );
|
|
|
|
o++;
|
2007-06-30 16:40:52 +02:00
|
|
|
}
|
2010-12-08 04:36:10 +01:00
|
|
|
#else
|
2011-11-18 23:58:02 +01:00
|
|
|
ConvertToFloat(2 << derivation_size, fft_scratch);
|
2007-06-30 16:40:52 +02:00
|
|
|
|
2011-11-18 23:58:02 +01:00
|
|
|
float *fft_input = &fft_scratch[0];
|
|
|
|
float *fft_real = &fft_scratch[0] + (2 << derivation_size);
|
|
|
|
float *fft_imag = &fft_scratch[0] + (4 << derivation_size);
|
2007-06-30 16:40:52 +02:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
FFT fft;
|
|
|
|
fft.Transform(2<<derivation_size, fft_input, fft_real, fft_imag);
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
float scale_factor = 9 / sqrt(2 * (float)(2<<derivation_size));
|
2007-06-30 16:40:52 +02:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
for (size_t si = 1<<derivation_size; si > 0; --si)
|
2007-06-30 16:40:52 +02:00
|
|
|
{
|
2010-12-08 04:36:10 +01:00
|
|
|
// With x in range [0;1], log10(x*9+1) will also be in range [0;1],
|
|
|
|
// although the FFT output can apparently get greater magnitudes than 1
|
|
|
|
// despite the input being limited to [-1;+1).
|
|
|
|
*block++ = log10( sqrt(*fft_real * *fft_real + *fft_imag * *fft_imag) * scale_factor + 1 );
|
|
|
|
fft_real++; fft_imag++;
|
2007-06-30 16:40:52 +02:00
|
|
|
}
|
2010-12-08 04:36:10 +01:00
|
|
|
#endif
|
|
|
|
}
|
2007-01-07 05:44:11 +01:00
|
|
|
|
2011-11-18 23:56:45 +01:00
|
|
|
void AudioSpectrumRenderer::Render(wxBitmap &bmp, int start, AudioRenderingStyle style)
|
2007-01-07 05:44:11 +01:00
|
|
|
{
|
2010-12-08 04:36:10 +01:00
|
|
|
if (!cache)
|
|
|
|
return;
|
2007-01-07 05:44:11 +01:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
assert(bmp.IsOk());
|
|
|
|
assert(bmp.GetDepth() == 24);
|
2007-01-07 05:44:11 +01:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
int end = start + bmp.GetWidth();
|
2007-01-07 05:44:11 +01:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
assert(start >= 0);
|
|
|
|
assert(end >= 0);
|
|
|
|
assert(end >= start);
|
2007-01-07 05:44:11 +01:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
// Prepare an image buffer to write
|
|
|
|
wxImage img(bmp.GetSize());
|
|
|
|
unsigned char *imgdata = img.GetData();
|
|
|
|
ptrdiff_t stride = img.GetWidth()*3;
|
|
|
|
int imgheight = img.GetHeight();
|
2007-01-07 05:44:11 +01:00
|
|
|
|
2011-11-18 23:58:02 +01:00
|
|
|
const AudioColorScheme *pal = GetColorScheme(style);
|
2007-01-07 05:44:11 +01:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
/// @todo Make minband and maxband configurable
|
|
|
|
int minband = 0;
|
|
|
|
int maxband = 1 << derivation_size;
|
2007-01-07 05:44:11 +01:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
// ax = absolute x, absolute to the virtual spectrum bitmap
|
|
|
|
for (int ax = start; ax < end; ++ax)
|
|
|
|
{
|
|
|
|
// Derived audio data
|
|
|
|
size_t block_index = (size_t)(ax * pixel_samples) >> derivation_dist;
|
|
|
|
float *power = cache->Get(block_index);
|
|
|
|
|
|
|
|
// Prepare bitmap writing
|
|
|
|
unsigned char *px = imgdata + (imgheight-1) * stride + (ax - start) * 3;
|
|
|
|
|
|
|
|
// Scale up or down vertically?
|
|
|
|
if (imgheight > 1<<derivation_size)
|
|
|
|
{
|
|
|
|
// Interpolate
|
|
|
|
for (int y = 0; y < imgheight; ++y)
|
|
|
|
{
|
|
|
|
assert(px >= imgdata);
|
|
|
|
assert(px < imgdata + imgheight*stride);
|
|
|
|
float ideal = (float)(y+1.)/imgheight * (maxband-minband) + minband;
|
|
|
|
float sample1 = power[(int)floor(ideal)+minband];
|
|
|
|
float sample2 = power[(int)ceil(ideal)+minband];
|
|
|
|
float frac = ideal - floor(ideal);
|
|
|
|
float val = (1-frac)*sample1 + frac*sample2;
|
|
|
|
pal->map(val*amplitude_scale, px);
|
|
|
|
px -= stride;
|
2007-01-07 05:44:11 +01:00
|
|
|
}
|
|
|
|
}
|
2010-12-08 04:36:10 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Pick greatest
|
|
|
|
for (int y = 0; y < imgheight; ++y)
|
|
|
|
{
|
|
|
|
assert(px >= imgdata);
|
|
|
|
assert(px < imgdata + imgheight*stride);
|
|
|
|
int sample1 = std::max(0, maxband * y/imgheight + minband);
|
|
|
|
int sample2 = std::min((1<<derivation_size)-1, maxband * (y+1)/imgheight + minband);
|
|
|
|
float maxval = 0;
|
|
|
|
for (int samp = sample1; samp <= sample2; samp++)
|
|
|
|
if (power[samp] > maxval) maxval = power[samp];
|
|
|
|
pal->map(maxval*amplitude_scale, px);
|
|
|
|
px -= stride;
|
2007-01-07 05:44:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
wxBitmap tmpbmp(img);
|
|
|
|
wxMemoryDC targetdc(bmp);
|
|
|
|
targetdc.DrawBitmap(tmpbmp, 0, 0);
|
2007-01-07 05:44:11 +01:00
|
|
|
}
|
|
|
|
|
2011-11-18 23:56:45 +01:00
|
|
|
void AudioSpectrumRenderer::RenderBlank(wxDC &dc, const wxRect &rect, AudioRenderingStyle style)
|
2007-01-07 05:44:11 +01:00
|
|
|
{
|
2010-12-08 04:36:10 +01:00
|
|
|
// Get the colour of silence
|
2011-11-18 23:58:02 +01:00
|
|
|
wxColour col = GetColorScheme(style)->get(0.0f);
|
2010-12-08 04:36:10 +01:00
|
|
|
dc.SetBrush(wxBrush(col));
|
|
|
|
dc.SetPen(wxPen(col));
|
|
|
|
dc.DrawRectangle(rect);
|
2007-01-07 05:44:11 +01:00
|
|
|
}
|
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
void AudioSpectrumRenderer::AgeCache(size_t max_size)
|
|
|
|
{
|
|
|
|
if (cache)
|
|
|
|
cache->Age(max_size);
|
|
|
|
}
|
2011-11-18 23:58:02 +01:00
|
|
|
const AudioColorScheme *AudioSpectrumRenderer::GetColorScheme(AudioRenderingStyle style) const
|
|
|
|
{
|
|
|
|
switch (style)
|
|
|
|
{
|
|
|
|
case AudioStyle_Selected: return colors_selected.get();
|
|
|
|
case AudioStyle_Inactive: return colors_inactive.get();
|
|
|
|
default: return colors_normal.get();
|
|
|
|
}
|
|
|
|
}
|