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/
|
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/// @file audio_renderer.cpp
|
|
|
|
/// @brief Base classes for audio renderers (spectrum, waveform, ...)
|
|
|
|
/// @ingroup audio_ui
|
|
|
|
|
|
|
|
|
2009-09-10 15:06:40 +02:00
|
|
|
// Headers
|
2009-10-09 18:34:38 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
2011-11-20 04:30:00 +01:00
|
|
|
#include "audio_renderer.h"
|
|
|
|
|
2009-09-10 15:06:40 +02:00
|
|
|
#ifndef AGI_PRE
|
2011-11-20 04:30:00 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <tr1/functional>
|
|
|
|
|
2009-08-13 01:42:53 +02:00
|
|
|
#include <wx/bitmap.h>
|
2009-08-13 02:05:38 +02:00
|
|
|
#include <wx/dcmemory.h>
|
2009-09-10 15:06:40 +02:00
|
|
|
#endif
|
2009-08-13 01:42:53 +02:00
|
|
|
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "include/aegisub/audio_provider.h"
|
2009-08-13 01:42:53 +02:00
|
|
|
|
2011-11-18 23:56:45 +01:00
|
|
|
template<class C, class F> static void for_each(C &container, F const& func)
|
|
|
|
{
|
|
|
|
std::for_each(container.begin(), container.end(), func);
|
|
|
|
}
|
2010-12-08 04:36:10 +01:00
|
|
|
|
2011-11-18 23:56:45 +01:00
|
|
|
using std::tr1::placeholders::_1;
|
2009-08-16 02:28:26 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-11-18 23:58:12 +01:00
|
|
|
wxBitmap *AudioRendererBitmapCacheBitmapFactory::ProduceBlock(int /* i */)
|
2009-08-13 01:42:53 +02:00
|
|
|
{
|
2010-12-08 04:36:10 +01:00
|
|
|
return new 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
|
|
|
void AudioRendererBitmapCacheBitmapFactory::DisposeBlock(wxBitmap *bmp)
|
2009-08-13 01:42:53 +02:00
|
|
|
{
|
2009-08-16 02:28:26 +02:00
|
|
|
delete bmp;
|
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()
|
2012-01-18 21:08:06 +01:00
|
|
|
: pixel_samples(0)
|
|
|
|
, pixel_height(0)
|
|
|
|
, amplitude_scale(0)
|
|
|
|
, cache_bitmap_width(32) // arbitrary value for now
|
2010-12-08 04:36:10 +01:00
|
|
|
, cache_bitmap_maxsize(0)
|
|
|
|
, cache_renderer_maxsize(0)
|
2009-08-13 01:42:53 +02:00
|
|
|
, renderer(0)
|
|
|
|
, provider(0)
|
|
|
|
{
|
2011-11-18 23:56:45 +01:00
|
|
|
bitmaps.resize(AudioStyle_MAX, AudioRendererBitmapCache(256, AudioRendererBitmapCacheBitmapFactory(this)));
|
|
|
|
|
2009-08-13 01:42:53 +02:00
|
|
|
// Make sure there's *some* values for those fields, and in the caches
|
|
|
|
SetSamplesPerPixel(1);
|
|
|
|
SetHeight(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioRenderer::~AudioRenderer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioRenderer::SetSamplesPerPixel(int _pixel_samples)
|
|
|
|
{
|
2009-08-16 02:28:26 +02:00
|
|
|
if (pixel_samples == _pixel_samples) return;
|
|
|
|
|
2009-08-13 01:42:53 +02:00
|
|
|
pixel_samples = _pixel_samples;
|
2009-08-16 02:28:26 +02:00
|
|
|
|
|
|
|
if (renderer)
|
|
|
|
renderer->SetSamplesPerPixel(pixel_samples);
|
|
|
|
|
|
|
|
ResetBlockCount();
|
2009-08-13 01:42:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AudioRenderer::SetHeight(int _pixel_height)
|
|
|
|
{
|
|
|
|
if (pixel_height == _pixel_height) return;
|
|
|
|
|
|
|
|
pixel_height = _pixel_height;
|
2009-08-16 02:28:26 +02:00
|
|
|
Invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AudioRenderer::SetAmplitudeScale(float _amplitude_scale)
|
|
|
|
{
|
|
|
|
if (amplitude_scale == _amplitude_scale) return;
|
|
|
|
|
|
|
|
// A scaling of 0 or a negative scaling makes no sense
|
|
|
|
assert(_amplitude_scale > 0);
|
|
|
|
|
|
|
|
amplitude_scale = _amplitude_scale;
|
|
|
|
|
|
|
|
if (renderer)
|
|
|
|
renderer->SetAmplitudeScale(amplitude_scale);
|
|
|
|
Invalidate();
|
2009-08-13 01:42:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AudioRenderer::SetRenderer(AudioRendererBitmapProvider *_renderer)
|
|
|
|
{
|
|
|
|
if (renderer == _renderer) return;
|
|
|
|
|
|
|
|
renderer = _renderer;
|
2009-08-16 02:28:26 +02:00
|
|
|
Invalidate();
|
2009-08-13 01:42:53 +02:00
|
|
|
|
|
|
|
if (renderer)
|
2009-08-16 02:28:26 +02:00
|
|
|
{
|
2009-08-13 01:42:53 +02:00
|
|
|
renderer->SetProvider(provider);
|
2009-08-16 02:28:26 +02:00
|
|
|
renderer->SetAmplitudeScale(amplitude_scale);
|
|
|
|
renderer->SetSamplesPerPixel(pixel_samples);
|
|
|
|
}
|
2009-08-13 01:42:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AudioRenderer::SetAudioProvider(AudioProvider *_provider)
|
|
|
|
{
|
|
|
|
if (provider == _provider) return;
|
|
|
|
|
|
|
|
provider = _provider;
|
2009-08-16 02:28:26 +02:00
|
|
|
Invalidate();
|
2009-08-13 01:42:53 +02:00
|
|
|
|
|
|
|
if (renderer)
|
|
|
|
renderer->SetProvider(provider);
|
2009-08-16 02:28:26 +02:00
|
|
|
|
|
|
|
ResetBlockCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-16 18:22:59 +02:00
|
|
|
void AudioRenderer::SetCacheMaxSize(size_t max_size)
|
|
|
|
{
|
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.
|
|
|
|
cache_renderer_maxsize = max_size - 2*cache_bitmap_maxsize;
|
2009-08-16 18:22:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-16 02:28:26 +02:00
|
|
|
void AudioRenderer::ResetBlockCount()
|
|
|
|
{
|
|
|
|
if (provider)
|
|
|
|
{
|
2010-12-08 04:36:10 +01:00
|
|
|
size_t rendered_width = (size_t)((provider->GetNumSamples() + pixel_samples - 1) / pixel_samples);
|
|
|
|
cache_numblocks = rendered_width / cache_bitmap_width;
|
2011-11-18 23:56:45 +01:00
|
|
|
for_each(bitmaps, bind(&AudioRendererBitmapCache::SetBlockCount, _1, cache_numblocks));
|
2009-08-16 02:28:26 +02:00
|
|
|
}
|
2009-08-13 01:42:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-18 23:56:45 +01:00
|
|
|
wxBitmap AudioRenderer::GetCachedBitmap(int i, 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);
|
2009-08-13 01:42:53 +02:00
|
|
|
}
|
|
|
|
|
2009-08-16 02:28:26 +02:00
|
|
|
assert(bmp->IsOk());
|
|
|
|
return *bmp;
|
2009-08-13 01:42:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-18 23:56:45 +01:00
|
|
|
void AudioRenderer::Render(wxDC &dc, wxPoint origin, int start, int length, 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
|
|
|
|
int end = start + length;
|
|
|
|
// One past last X coordinate to render on
|
|
|
|
int lastx = origin.x + length;
|
2009-08-13 01:42:53 +02:00
|
|
|
// Figure out which range of bitmaps are required
|
|
|
|
int firstbitmap = start / cache_bitmap_width;
|
|
|
|
// And the offset in it to start its use at
|
|
|
|
int firstbitmapoffset = start % cache_bitmap_width;
|
|
|
|
// The last bitmap required
|
|
|
|
int lastbitmap = end / cache_bitmap_width;
|
|
|
|
// How many columns of the last bitmap to use
|
|
|
|
int lastbitmapoffset = end % cache_bitmap_width;
|
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
// Check if we need to render any blank audio past the last bitmap from cache,
|
|
|
|
// this happens if we're asked to render more audio than the provider has.
|
|
|
|
if (lastbitmap >= (int)cache_numblocks)
|
|
|
|
{
|
|
|
|
lastbitmap = cache_numblocks - 1;
|
|
|
|
lastbitmapoffset = cache_bitmap_width;
|
|
|
|
|
|
|
|
if (firstbitmap > lastbitmap)
|
|
|
|
firstbitmap = lastbitmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Three basic cases now:
|
|
|
|
// * Either we're just rendering blank audio,
|
|
|
|
// * Or there is exactly one bitmap to render,
|
|
|
|
// * Or there is more than one bitmap to render.
|
2009-08-13 01:42:53 +02:00
|
|
|
|
|
|
|
// origin is passed by value because we'll be using it as a local var to keep track
|
|
|
|
// of rendering progress!
|
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
if (start / cache_bitmap_width >= (int)cache_numblocks)
|
2009-08-13 01:42:53 +02:00
|
|
|
{
|
2010-12-08 04:36:10 +01:00
|
|
|
// Do nothing, the blank audio rendering will happen later
|
|
|
|
}
|
|
|
|
else if (firstbitmap == lastbitmap)
|
|
|
|
{
|
|
|
|
const int renderwidth = lastbitmapoffset - firstbitmapoffset;
|
2011-11-18 23:56:45 +01:00
|
|
|
wxBitmap bmp = GetCachedBitmap(firstbitmap, style);
|
2009-08-13 01:42:53 +02:00
|
|
|
wxMemoryDC bmpdc(bmp);
|
2010-12-08 04:36:10 +01:00
|
|
|
dc.Blit(origin, wxSize(renderwidth, pixel_height), &bmpdc, wxPoint(firstbitmapoffset, 0));
|
|
|
|
origin.x += renderwidth;
|
2009-08-13 01:42:53 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxBitmap bmp;
|
|
|
|
|
|
|
|
{
|
2011-11-18 23:56:45 +01:00
|
|
|
bmp = GetCachedBitmap(firstbitmap, style);
|
2010-12-08 04:36:10 +01:00
|
|
|
// Can't use dc.DrawBitmap here because we need to clip the bitmap
|
2009-08-13 01:42:53 +02:00
|
|
|
wxMemoryDC bmpdc(bmp);
|
|
|
|
dc.Blit(origin, wxSize(cache_bitmap_width-firstbitmapoffset, pixel_height),
|
|
|
|
&bmpdc, wxPoint(firstbitmapoffset, 0));
|
|
|
|
origin.x += cache_bitmap_width-firstbitmapoffset;
|
|
|
|
}
|
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
for (int i = firstbitmap+1; i < lastbitmap; ++i)
|
2009-08-13 01:42:53 +02:00
|
|
|
{
|
2011-11-18 23:56:45 +01:00
|
|
|
bmp = GetCachedBitmap(i, style);
|
2010-12-08 04:36:10 +01:00
|
|
|
dc.DrawBitmap(bmp, origin);
|
2009-08-13 01:42:53 +02:00
|
|
|
origin.x += cache_bitmap_width;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2011-11-18 23:56:45 +01:00
|
|
|
bmp = GetCachedBitmap(lastbitmap, style);
|
2010-12-08 04:36:10 +01:00
|
|
|
// We also need clipping here
|
2009-08-13 01:42:53 +02:00
|
|
|
wxMemoryDC bmpdc(bmp);
|
2010-12-08 04:36:10 +01:00
|
|
|
dc.Blit(origin, wxSize(lastbitmapoffset+1, pixel_height), &bmpdc, wxPoint(0, 0));
|
|
|
|
origin.x += lastbitmapoffset+1;
|
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
|
|
|
}
|
|
|
|
|
2011-11-18 23:56:45 +01:00
|
|
|
bitmaps[style].Age(cache_bitmap_maxsize);
|
2010-12-08 04:36:10 +01:00
|
|
|
renderer->AgeCache(cache_renderer_maxsize);
|
2009-08-16 02:28:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AudioRenderer::Invalidate()
|
|
|
|
{
|
2011-11-18 23:56:45 +01:00
|
|
|
for_each(bitmaps, bind(&AudioRendererBitmapCache::Age, _1, 0));
|
2009-08-13 01:42:53 +02:00
|
|
|
}
|
|
|
|
|
2009-08-13 02:14:36 +02:00
|
|
|
|
|
|
|
void AudioRendererBitmapProvider::SetProvider(AudioProvider *_provider)
|
|
|
|
{
|
|
|
|
if (provider == _provider) return;
|
|
|
|
|
|
|
|
provider = _provider;
|
|
|
|
|
|
|
|
OnSetProvider();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AudioRendererBitmapProvider::SetSamplesPerPixel(int _pixel_samples)
|
|
|
|
{
|
|
|
|
if (pixel_samples == _pixel_samples) return;
|
|
|
|
|
|
|
|
pixel_samples = _pixel_samples;
|
2010-12-08 04:36:10 +01:00
|
|
|
|
2009-08-13 02:14:36 +02:00
|
|
|
OnSetSamplesPerPixel();
|
|
|
|
}
|
|
|
|
|
2009-08-16 02:28:26 +02:00
|
|
|
|
|
|
|
void AudioRendererBitmapProvider::SetAmplitudeScale(float _amplitude_scale)
|
|
|
|
{
|
|
|
|
if (amplitude_scale == _amplitude_scale) return;
|
|
|
|
|
|
|
|
amplitude_scale = _amplitude_scale;
|
|
|
|
|
|
|
|
OnSetAmplitudeScale();
|
|
|
|
}
|