2010-12-08 04:36:10 +01:00
|
|
|
// Copyright (c) 2009, 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/
|
|
|
|
|
2009-08-13 19:28:12 +02:00
|
|
|
/// @file audio_renderer_spectrum.h
|
|
|
|
/// @see audio_renderer_spectrum.cpp
|
2009-07-29 07:43:02 +02:00
|
|
|
/// @ingroup audio_ui
|
|
|
|
///
|
2009-08-01 03:55:17 +02:00
|
|
|
/// Calculate and render a frequency-power spectrum for PCM audio data.
|
|
|
|
|
2012-09-25 02:09:42 +02:00
|
|
|
#include <cstdint>
|
2013-06-10 15:58:13 +02:00
|
|
|
#include <memory>
|
2011-11-18 23:58:02 +01:00
|
|
|
#include <vector>
|
2009-09-11 04:36:34 +02:00
|
|
|
|
2011-11-18 23:58:02 +01:00
|
|
|
#include "audio_renderer.h"
|
|
|
|
|
2011-12-22 22:25:49 +01:00
|
|
|
#ifdef WITH_FFTW3
|
2010-12-08 04:36:10 +01:00
|
|
|
#include <fftw3.h>
|
|
|
|
#endif
|
|
|
|
|
2011-11-18 23:58:02 +01:00
|
|
|
class AudioColorScheme;
|
2010-12-08 04:36:10 +01:00
|
|
|
class AudioSpectrumCache;
|
|
|
|
struct AudioSpectrumCacheBlockFactory;
|
2007-01-07 05:44:11 +01:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
/// @class AudioSpectrumRenderer
|
2009-08-01 03:55:17 +02:00
|
|
|
/// @brief Render frequency-power spectrum graphs for audio data.
|
|
|
|
///
|
2010-12-08 04:36:10 +01:00
|
|
|
/// Renders frequency-power spectrum graphs of PCM audio data using a derivation function
|
|
|
|
/// such as the fast fourier transform.
|
2014-03-13 02:39:07 +01:00
|
|
|
class AudioSpectrumRenderer final : public AudioRendererBitmapProvider {
|
2010-12-08 04:36:10 +01:00
|
|
|
friend struct AudioSpectrumCacheBlockFactory;
|
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
|
|
|
|
2009-08-01 03:55:17 +02:00
|
|
|
/// Internal cache management for the spectrum
|
2013-06-10 15:58:13 +02:00
|
|
|
std::unique_ptr<AudioSpectrumCache> cache;
|
2007-01-07 05:44:11 +01:00
|
|
|
|
2012-03-12 01:07:16 +01:00
|
|
|
/// Colour tables used for rendering
|
|
|
|
std::vector<AudioColorScheme> colors;
|
2007-01-07 05:44:11 +01:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
/// Binary logarithm of number of samples to use in deriving frequency-power data
|
2013-12-12 01:29:48 +01:00
|
|
|
size_t derivation_size = 0;
|
2007-01-07 05:44:11 +01:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
/// Binary logarithm of number of samples between the start of derivations
|
2013-12-12 01:29:48 +01:00
|
|
|
size_t derivation_dist = 0;
|
2010-12-08 04:36:10 +01:00
|
|
|
|
|
|
|
/// @brief Reset in response to changing audio provider
|
|
|
|
///
|
|
|
|
/// Overrides the OnSetProvider event handler in the base class, to reset things
|
|
|
|
/// when the audio provider is changed.
|
2013-11-21 18:13:36 +01:00
|
|
|
void OnSetProvider() override;
|
2010-12-08 04:36:10 +01:00
|
|
|
|
|
|
|
/// @brief Recreates the cache
|
|
|
|
///
|
|
|
|
/// To be called when the number of blocks in cache might have changed,
|
2011-11-18 23:58:02 +01:00
|
|
|
/// e.g. new audio provider or new resolution.
|
2010-12-08 04:36:10 +01:00
|
|
|
void RecreateCache();
|
|
|
|
|
|
|
|
/// @brief Fill a block with frequency-power data for a time range
|
|
|
|
/// @param block_index Index of the block to fill data for
|
|
|
|
/// @param[out] block Address to write the data to
|
|
|
|
void FillBlock(size_t block_index, float *block);
|
|
|
|
|
2011-11-18 23:58:02 +01:00
|
|
|
/// @brief Convert audio data to float range [-1;+1)
|
|
|
|
/// @param count Samples to convert
|
|
|
|
/// @param dest Buffer to fill
|
|
|
|
template<class T>
|
2011-12-06 20:58:54 +01:00
|
|
|
void ConvertToFloat(size_t count, T *dest);
|
2011-11-18 23:58:02 +01:00
|
|
|
|
2011-12-22 22:25:49 +01:00
|
|
|
#ifdef WITH_FFTW3
|
2010-12-08 04:36:10 +01:00
|
|
|
/// FFTW plan data
|
2013-12-12 01:29:48 +01:00
|
|
|
fftw_plan dft_plan = nullptr;
|
2010-12-08 04:36:10 +01:00
|
|
|
/// Pre-allocated input array for FFTW
|
2013-12-12 01:29:48 +01:00
|
|
|
double *dft_input = nullptr;
|
2010-12-08 04:36:10 +01:00
|
|
|
/// Pre-allocated output array for FFTW
|
2013-12-12 01:29:48 +01:00
|
|
|
fftw_complex *dft_output = nullptr;
|
2010-12-08 04:36:10 +01:00
|
|
|
#else
|
|
|
|
/// Pre-allocated scratch area for doing FFT derivations
|
2011-11-18 23:58:02 +01:00
|
|
|
std::vector<float> fft_scratch;
|
2010-12-08 04:36:10 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/// Pre-allocated scratch area for storing raw audio data
|
2011-11-18 23:58:02 +01:00
|
|
|
std::vector<int16_t> audio_scratch;
|
|
|
|
|
2007-01-07 05:44:11 +01:00
|
|
|
public:
|
2009-08-01 03:55:17 +02:00
|
|
|
/// @brief Constructor
|
2011-11-30 22:04:09 +01:00
|
|
|
/// @param color_scheme_name Name of the color scheme to use
|
|
|
|
AudioSpectrumRenderer(std::string const& color_scheme_name);
|
2010-12-08 04:36:10 +01:00
|
|
|
|
2009-08-01 03:55:17 +02:00
|
|
|
/// @brief Destructor
|
2011-11-18 23:58:02 +01:00
|
|
|
~AudioSpectrumRenderer();
|
2010-12-08 04:36:10 +01:00
|
|
|
|
|
|
|
/// @brief Render a range of audio spectrum
|
2011-11-18 23:58:02 +01:00
|
|
|
/// @param bmp [in,out] Bitmap to render into, also carries length information
|
2011-11-18 23:56:45 +01:00
|
|
|
/// @param start First column of pixel data in display to render
|
|
|
|
/// @param style Style to render audio in
|
2013-11-21 18:13:36 +01:00
|
|
|
void Render(wxBitmap &bmp, int start, AudioRenderingStyle style) override;
|
2010-12-08 04:36:10 +01:00
|
|
|
|
|
|
|
/// @brief Render blank area
|
2013-11-21 18:13:36 +01:00
|
|
|
void RenderBlank(wxDC &dc, const wxRect &rect, AudioRenderingStyle style) override;
|
2010-12-08 04:36:10 +01:00
|
|
|
|
|
|
|
/// @brief Set the derivation resolution
|
|
|
|
/// @param derivation_size Binary logarithm of number of samples to use in deriving frequency-power data
|
|
|
|
/// @param derivation_dist Binary logarithm of number of samples between the start of derivations
|
|
|
|
///
|
|
|
|
/// The derivations done will each use 2^derivation_size audio samples and at a distance
|
|
|
|
/// of 2^derivation_dist samples.
|
|
|
|
///
|
|
|
|
/// The derivation distance must be smaller than or equal to the size. If the distance
|
|
|
|
/// is specified too large, it will be clamped to the size.
|
|
|
|
void SetResolution(size_t derivation_size, size_t derivation_dist);
|
|
|
|
|
|
|
|
/// @brief Cleans up the cache
|
|
|
|
/// @param max_size Maximum size in bytes for the cache
|
2013-11-21 18:13:36 +01:00
|
|
|
void AgeCache(size_t max_size) override;
|
2007-01-07 05:44:11 +01:00
|
|
|
};
|