2010-12-08 04:36:10 +01:00
|
|
|
// Copyright (c) 2009-2010, Niels Martin Hansen
|
2009-08-13 01:42:53 +02: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.
|
|
|
|
//
|
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file audio_renderer.cpp
|
|
|
|
/// @brief Base classes for audio renderers (spectrum, waveform, ...)
|
|
|
|
/// @ingroup audio_ui
|
|
|
|
|
2011-11-20 04:30:00 +01:00
|
|
|
#include "audio_renderer.h"
|
|
|
|
|
2014-07-09 16:22:49 +02:00
|
|
|
#include <libaegisub/audio/provider.h>
|
2014-04-23 22:53:24 +02:00
|
|
|
#include <libaegisub/make_unique.h>
|
2013-10-27 15:15:39 +01:00
|
|
|
|
2011-11-20 04:30:00 +01:00
|
|
|
#include <algorithm>
|
2014-05-23 00:40:16 +02:00
|
|
|
#include <wx/dc.h>
|
2009-08-13 01:42:53 +02:00
|
|
|
|
2014-04-22 21:34:20 +02:00
|
|
|
namespace {
|
|
|
|
template<typename T>
|
2014-12-20 20:09:15 +01:00
|
|
|
bool compare_and_set(T &var, const T new_value)
|
2014-04-22 21:34:20 +02:00
|
|
|
{
|
|
|
|
if (var == new_value) return false;
|
|
|
|
var = new_value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-18 23:58:12 +01:00
|
|
|
AudioRendererBitmapCacheBitmapFactory::AudioRendererBitmapCacheBitmapFactory(AudioRenderer *renderer)
|
|
|
|
: renderer(renderer)
|
2009-08-13 01:42:53 +02:00
|
|
|
{
|
2011-11-18 23:58:12 +01:00
|
|
|
assert(renderer);
|
2009-08-13 01:42:53 +02:00
|
|
|
}
|
|
|
|
|
2013-10-27 15:15:39 +01:00
|
|
|
std::unique_ptr<wxBitmap> AudioRendererBitmapCacheBitmapFactory::ProduceBlock(int /* i */)
|
2009-08-13 01:42:53 +02:00
|
|
|
{
|
2014-04-23 22:53:24 +02:00
|
|
|
return agi::make_unique<wxBitmap>(renderer->cache_bitmap_width, renderer->pixel_height, 24);
|
2009-08-13 01:42:53 +02:00
|
|
|
}
|
|
|
|
|
2009-08-16 02:28:26 +02:00
|
|
|
size_t AudioRendererBitmapCacheBitmapFactory::GetBlockSize() const
|
2009-08-13 01:42:53 +02:00
|
|
|
{
|
2010-12-08 04:36:10 +01:00
|
|
|
return sizeof(wxBitmap) + renderer->cache_bitmap_width * renderer->pixel_height * 3;
|
2009-08-13 01:42:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioRenderer::AudioRenderer()
|
|
|
|
{
|
2013-10-27 15:15:39 +01:00
|
|
|
for (int i = 0; i < AudioStyle_MAX; ++i)
|
|
|
|
bitmaps.emplace_back(256, AudioRendererBitmapCacheBitmapFactory(this));
|
2011-11-18 23:56:45 +01:00
|
|
|
|
2009-08-13 01:42:53 +02:00
|
|
|
// Make sure there's *some* values for those fields, and in the caches
|
2012-02-02 00:58:58 +01:00
|
|
|
SetMillisecondsPerPixel(1);
|
2009-08-13 01:42:53 +02:00
|
|
|
SetHeight(1);
|
|
|
|
}
|
|
|
|
|
2014-12-20 20:09:15 +01:00
|
|
|
void AudioRenderer::SetMillisecondsPerPixel(const double new_pixel_ms)
|
2009-08-13 01:42:53 +02:00
|
|
|
{
|
2014-04-22 21:34:20 +02:00
|
|
|
if (compare_and_set(pixel_ms, new_pixel_ms))
|
|
|
|
{
|
|
|
|
if (renderer)
|
|
|
|
renderer->SetMillisecondsPerPixel(pixel_ms);
|
2009-08-16 02:28:26 +02:00
|
|
|
|
2014-04-22 21:34:20 +02:00
|
|
|
ResetBlockCount();
|
|
|
|
}
|
2009-08-13 01:42:53 +02:00
|
|
|
}
|
|
|
|
|
2014-12-20 20:09:15 +01:00
|
|
|
void AudioRenderer::SetHeight(const int _pixel_height)
|
2009-08-13 01:42:53 +02:00
|
|
|
{
|
2014-04-22 21:34:20 +02:00
|
|
|
if (compare_and_set(pixel_height, _pixel_height))
|
|
|
|
Invalidate();
|
2009-08-16 02:28:26 +02:00
|
|
|
}
|
|
|
|
|
2014-12-20 20:09:15 +01:00
|
|
|
void AudioRenderer::SetAmplitudeScale(const float _amplitude_scale)
|
2009-08-16 02:28:26 +02:00
|
|
|
{
|
2014-04-22 21:34:20 +02:00
|
|
|
if (compare_and_set(amplitude_scale, _amplitude_scale))
|
|
|
|
{
|
|
|
|
// A scaling of 0 or a negative scaling makes no sense
|
|
|
|
assert(amplitude_scale > 0);
|
|
|
|
if (renderer)
|
|
|
|
renderer->SetAmplitudeScale(amplitude_scale);
|
|
|
|
Invalidate();
|
|
|
|
}
|
2009-08-13 01:42:53 +02:00
|
|
|
}
|
|
|
|
|
2014-12-20 20:09:15 +01:00
|
|
|
void AudioRenderer::SetRenderer(AudioRendererBitmapProvider *const _renderer)
|
2009-08-13 01:42:53 +02:00
|
|
|
{
|
2014-04-22 21:34:20 +02:00
|
|
|
if (compare_and_set(renderer, _renderer))
|
2009-08-16 02:28:26 +02:00
|
|
|
{
|
2014-04-22 21:34:20 +02:00
|
|
|
Invalidate();
|
|
|
|
|
|
|
|
if (renderer)
|
|
|
|
{
|
|
|
|
renderer->SetProvider(provider);
|
|
|
|
renderer->SetAmplitudeScale(amplitude_scale);
|
|
|
|
renderer->SetMillisecondsPerPixel(pixel_ms);
|
|
|
|
}
|
2009-08-16 02:28:26 +02:00
|
|
|
}
|
2009-08-13 01:42:53 +02:00
|
|
|
}
|
|
|
|
|
2014-12-20 20:09:15 +01:00
|
|
|
void AudioRenderer::SetAudioProvider(agi::AudioProvider *const _provider)
|
2009-08-13 01:42:53 +02:00
|
|
|
{
|
2014-04-22 21:34:20 +02:00
|
|
|
if (compare_and_set(provider, _provider))
|
|
|
|
{
|
|
|
|
Invalidate();
|
2009-08-13 01:42:53 +02:00
|
|
|
|
2014-04-22 21:34:20 +02:00
|
|
|
if (renderer)
|
|
|
|
renderer->SetProvider(provider);
|
2009-08-16 02:28:26 +02:00
|
|
|
|
2014-04-22 21:34:20 +02:00
|
|
|
ResetBlockCount();
|
|
|
|
}
|
2009-08-16 02:28:26 +02:00
|
|
|
}
|
|
|
|
|
2014-12-20 20:09:15 +01:00
|
|
|
void AudioRenderer::SetCacheMaxSize(const size_t max_size)
|
2009-08-16 18:22:59 +02:00
|
|
|
{
|
2010-12-08 04:36:10 +01:00
|
|
|
// Limit the bitmap cache sizes to 16 MB hard, to avoid the risk of exhausting
|
|
|
|
// system bitmap object resources and similar. Experimenting shows that 16 MB
|
|
|
|
// bitmap cache should be plenty even if working with a one hour audio clip.
|
2011-11-18 23:58:12 +01:00
|
|
|
cache_bitmap_maxsize = std::min<size_t>(max_size/8, 0x1000000);
|
2010-12-08 04:36:10 +01:00
|
|
|
// The renderer gets whatever is left.
|
2012-03-12 01:07:16 +01:00
|
|
|
cache_renderer_maxsize = max_size - 4*cache_bitmap_maxsize;
|
2009-08-16 18:22:59 +02:00
|
|
|
}
|
|
|
|
|
2009-08-16 02:28:26 +02:00
|
|
|
void AudioRenderer::ResetBlockCount()
|
|
|
|
{
|
|
|
|
if (provider)
|
|
|
|
{
|
2014-04-22 21:34:20 +02:00
|
|
|
const size_t total_blocks = NumBlocks(provider->GetNumSamples());
|
|
|
|
for (auto& bmp : bitmaps) bmp.SetBlockCount(total_blocks);
|
2009-08-16 02:28:26 +02:00
|
|
|
}
|
2009-08-13 01:42:53 +02:00
|
|
|
}
|
|
|
|
|
2014-04-22 21:34:20 +02:00
|
|
|
size_t AudioRenderer::NumBlocks(const int64_t samples) const
|
|
|
|
{
|
|
|
|
const double duration = samples * 1000.0 / provider->GetSampleRate();
|
|
|
|
return static_cast<size_t>(duration / pixel_ms / cache_bitmap_width);
|
|
|
|
}
|
|
|
|
|
2014-12-20 20:09:15 +01:00
|
|
|
const wxBitmap *AudioRenderer::GetCachedBitmap(const int i, const AudioRenderingStyle style)
|
2009-08-13 01:42:53 +02:00
|
|
|
{
|
|
|
|
assert(provider);
|
|
|
|
assert(renderer);
|
|
|
|
|
2009-08-16 02:28:26 +02:00
|
|
|
bool created = false;
|
2011-11-18 23:56:45 +01:00
|
|
|
wxBitmap *bmp = bitmaps[style].Get(i, &created);
|
2009-08-16 02:28:26 +02:00
|
|
|
assert(bmp);
|
|
|
|
if (created)
|
2009-08-13 01:42:53 +02:00
|
|
|
{
|
2011-11-18 23:56:45 +01:00
|
|
|
renderer->Render(*bmp, i*cache_bitmap_width, style);
|
2012-02-02 23:58:15 +01:00
|
|
|
needs_age = true;
|
2009-08-13 01:42:53 +02:00
|
|
|
}
|
|
|
|
|
2009-08-16 02:28:26 +02:00
|
|
|
assert(bmp->IsOk());
|
2012-01-31 01:42:47 +01:00
|
|
|
return bmp;
|
2009-08-13 01:42:53 +02:00
|
|
|
}
|
|
|
|
|
2014-12-20 20:09:15 +01:00
|
|
|
void AudioRenderer::Render(wxDC &dc, wxPoint origin, const int start, const int length, const AudioRenderingStyle style)
|
2009-08-13 01:42:53 +02:00
|
|
|
{
|
|
|
|
assert(start >= 0);
|
|
|
|
|
|
|
|
if (!provider) return;
|
|
|
|
if (!renderer) return;
|
2010-12-08 04:36:10 +01:00
|
|
|
if (length <= 0) return;
|
2009-08-13 01:42:53 +02:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
// One past last absolute pixel strip to render
|
2014-12-20 20:09:15 +01:00
|
|
|
const int end = start + length;
|
2010-12-08 04:36:10 +01:00
|
|
|
// One past last X coordinate to render on
|
2014-12-20 20:09:15 +01:00
|
|
|
const int lastx = origin.x + length;
|
2009-08-13 01:42:53 +02:00
|
|
|
// Figure out which range of bitmaps are required
|
2014-12-20 20:09:15 +01:00
|
|
|
const int firstbitmap = start / cache_bitmap_width;
|
2009-08-13 01:42:53 +02:00
|
|
|
// And the offset in it to start its use at
|
2014-12-20 20:09:15 +01:00
|
|
|
const int firstbitmapoffset = start % cache_bitmap_width;
|
2009-08-13 01:42:53 +02:00
|
|
|
// The last bitmap required
|
2014-12-20 20:09:15 +01:00
|
|
|
const int lastbitmap = std::min<int>(end / cache_bitmap_width, NumBlocks(provider->GetDecodedSamples()) - 1);
|
2009-08-13 01:42:53 +02:00
|
|
|
|
2012-01-31 01:42:47 +01:00
|
|
|
// Set a clipping region so that the first and last bitmaps don't draw
|
|
|
|
// outside the requested range
|
2014-12-20 20:09:15 +01:00
|
|
|
const wxDCClipper clipper(dc, wxRect(origin, wxSize(length, pixel_height)));
|
2012-01-31 01:42:47 +01:00
|
|
|
origin.x -= firstbitmapoffset;
|
2009-08-13 01:42:53 +02:00
|
|
|
|
2012-01-31 01:42:47 +01:00
|
|
|
for (int i = firstbitmap; i <= lastbitmap; ++i)
|
2009-08-13 01:42:53 +02:00
|
|
|
{
|
2012-01-31 01:42:47 +01:00
|
|
|
dc.DrawBitmap(*GetCachedBitmap(i, style), origin);
|
|
|
|
origin.x += cache_bitmap_width;
|
2009-08-13 01:42:53 +02:00
|
|
|
}
|
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
// Now render blank audio from origin to end
|
|
|
|
if (origin.x < lastx)
|
2011-11-18 23:56:45 +01:00
|
|
|
renderer->RenderBlank(dc, wxRect(origin.x-1, origin.y, lastx-origin.x+1, pixel_height), style);
|
2010-12-08 04:36:10 +01:00
|
|
|
|
2012-02-02 23:58:15 +01:00
|
|
|
if (needs_age)
|
|
|
|
{
|
|
|
|
bitmaps[style].Age(cache_bitmap_maxsize);
|
|
|
|
renderer->AgeCache(cache_renderer_maxsize);
|
|
|
|
needs_age = false;
|
|
|
|
}
|
2009-08-16 02:28:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioRenderer::Invalidate()
|
|
|
|
{
|
2013-06-08 06:19:40 +02:00
|
|
|
for (auto& bmp : bitmaps) bmp.Age(0);
|
2012-02-02 23:58:15 +01:00
|
|
|
needs_age = false;
|
2009-08-13 01:42:53 +02:00
|
|
|
}
|
|
|
|
|
2014-12-20 20:09:15 +01:00
|
|
|
void AudioRendererBitmapProvider::SetProvider(agi::AudioProvider *const _provider)
|
2009-08-13 02:14:36 +02:00
|
|
|
{
|
2014-04-22 21:34:20 +02:00
|
|
|
if (compare_and_set(provider, _provider))
|
|
|
|
OnSetProvider();
|
2009-08-13 02:14:36 +02:00
|
|
|
}
|
|
|
|
|
2014-12-20 20:09:15 +01:00
|
|
|
void AudioRendererBitmapProvider::SetMillisecondsPerPixel(const double new_pixel_ms)
|
2009-08-13 02:14:36 +02:00
|
|
|
{
|
2014-04-22 21:34:20 +02:00
|
|
|
if (compare_and_set(pixel_ms, new_pixel_ms))
|
|
|
|
OnSetMillisecondsPerPixel();
|
2009-08-13 02:14:36 +02:00
|
|
|
}
|
|
|
|
|
2014-12-20 20:09:15 +01:00
|
|
|
void AudioRendererBitmapProvider::SetAmplitudeScale(const float _amplitude_scale)
|
2009-08-16 02:28:26 +02:00
|
|
|
{
|
2014-04-22 21:34:20 +02:00
|
|
|
if (compare_and_set(amplitude_scale, _amplitude_scale))
|
|
|
|
OnSetAmplitudeScale();
|
2009-08-16 02:28:26 +02:00
|
|
|
}
|