2010-12-08 04:36:10 +01:00
|
|
|
// Copyright (c) 2010, Niels Martin Hansen
|
|
|
|
// 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_waveform.cpp
|
|
|
|
/// @ingroup audio_ui
|
|
|
|
///
|
|
|
|
/// Render a waveform display of PCM audio data
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2011-11-30 22:04:09 +01:00
|
|
|
#include "audio_renderer_waveform.h"
|
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include <wx/dcmemory.h>
|
|
|
|
|
|
|
|
#include "audio_colorscheme.h"
|
2011-11-30 22:04:09 +01:00
|
|
|
#include "block_cache.h"
|
2010-12-08 04:36:10 +01:00
|
|
|
#include "colorspace.h"
|
2011-11-30 22:04:09 +01:00
|
|
|
#include "include/aegisub/audio_provider.h"
|
2013-01-07 02:50:09 +01:00
|
|
|
#include "options.h"
|
2011-12-22 22:19:21 +01:00
|
|
|
|
|
|
|
enum {
|
|
|
|
/// Only render the peaks
|
|
|
|
Waveform_MaxOnly = 0,
|
|
|
|
/// Render the peaks and averages
|
|
|
|
Waveform_MaxAvg,
|
|
|
|
Waveform_Continuous
|
|
|
|
};
|
2010-12-08 04:36:10 +01:00
|
|
|
|
2011-11-30 22:04:09 +01:00
|
|
|
AudioWaveformRenderer::AudioWaveformRenderer(std::string const& color_scheme_name)
|
2013-12-12 01:29:48 +01:00
|
|
|
: render_averages(OPT_GET("Audio/Display/Waveform Style")->GetInt() == Waveform_MaxAvg)
|
2010-12-08 04:36:10 +01:00
|
|
|
{
|
2012-03-12 01:07:16 +01:00
|
|
|
colors.reserve(AudioStyle_MAX);
|
|
|
|
for (int i = 0; i < AudioStyle_MAX; ++i)
|
2012-11-28 16:35:26 +01:00
|
|
|
colors.emplace_back(6, color_scheme_name, i);
|
2010-12-08 04:36:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioWaveformRenderer::~AudioWaveformRenderer()
|
|
|
|
{
|
|
|
|
delete[] audio_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-18 23:56:45 +01:00
|
|
|
void AudioWaveformRenderer::Render(wxBitmap &bmp, int start, AudioRenderingStyle style)
|
2010-12-08 04:36:10 +01:00
|
|
|
{
|
|
|
|
wxMemoryDC dc(bmp);
|
|
|
|
wxRect rect(wxPoint(0, 0), bmp.GetSize());
|
|
|
|
int midpoint = rect.height / 2;
|
|
|
|
|
2012-03-12 01:07:16 +01:00
|
|
|
const AudioColorScheme *pal = &colors[style];
|
2010-12-08 04:36:10 +01:00
|
|
|
|
2012-03-18 19:41:48 +01:00
|
|
|
double pixel_samples = pixel_ms * provider->GetSampleRate() / 1000.0;
|
2012-02-02 00:58:58 +01:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
// Fill the background
|
|
|
|
dc.SetBrush(wxBrush(pal->get(0.0f)));
|
|
|
|
dc.SetPen(*wxTRANSPARENT_PEN);
|
|
|
|
dc.DrawRectangle(rect);
|
|
|
|
|
|
|
|
// Make sure we've got a buffer to fill with audio data
|
|
|
|
if (!audio_buffer)
|
|
|
|
{
|
|
|
|
// Buffer for one pixel strip of audio
|
2012-05-18 03:58:35 +02:00
|
|
|
size_t buffer_needed = pixel_samples * provider->GetChannels() * provider->GetBytesPerSample();
|
2010-12-08 04:36:10 +01:00
|
|
|
audio_buffer = new char[buffer_needed];
|
|
|
|
}
|
|
|
|
|
2012-03-18 19:41:48 +01:00
|
|
|
double cur_sample = start * pixel_samples;
|
2010-12-08 04:36:10 +01:00
|
|
|
|
|
|
|
assert(provider->GetBytesPerSample() == 2);
|
|
|
|
assert(provider->GetChannels() == 1);
|
|
|
|
|
|
|
|
wxPen pen_peaks(wxPen(pal->get(0.4f)));
|
|
|
|
wxPen pen_avgs(wxPen(pal->get(0.7f)));
|
|
|
|
|
|
|
|
for (int x = 0; x < rect.width; ++x)
|
|
|
|
{
|
2012-03-18 19:41:48 +01:00
|
|
|
provider->GetAudio(audio_buffer, (int64_t)cur_sample, (int64_t)pixel_samples);
|
2010-12-08 04:36:10 +01:00
|
|
|
cur_sample += pixel_samples;
|
2012-03-25 06:05:06 +02:00
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
int peak_min = 0, peak_max = 0;
|
|
|
|
int64_t avg_min_accum = 0, avg_max_accum = 0;
|
|
|
|
const int16_t *aud = (const int16_t *)audio_buffer;
|
|
|
|
for (int si = pixel_samples; si > 0; --si, ++aud)
|
|
|
|
{
|
|
|
|
if (*aud > 0)
|
|
|
|
{
|
|
|
|
peak_max = std::max(peak_max, (int)*aud);
|
|
|
|
avg_max_accum += *aud;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
peak_min = std::min(peak_min, (int)*aud);
|
|
|
|
avg_min_accum += *aud;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// midpoint is half height
|
|
|
|
peak_min = std::max((int)(peak_min * amplitude_scale * midpoint) / 0x8000, -midpoint);
|
|
|
|
peak_max = std::min((int)(peak_max * amplitude_scale * midpoint) / 0x8000, midpoint);
|
|
|
|
int avg_min = std::max((int)(avg_min_accum * amplitude_scale * midpoint / pixel_samples) / 0x8000, -midpoint);
|
|
|
|
int avg_max = std::min((int)(avg_max_accum * amplitude_scale * midpoint / pixel_samples) / 0x8000, midpoint);
|
|
|
|
|
|
|
|
dc.SetPen(pen_peaks);
|
|
|
|
dc.DrawLine(x, midpoint - peak_max, x, midpoint - peak_min);
|
2011-12-22 22:19:21 +01:00
|
|
|
if (render_averages) {
|
|
|
|
dc.SetPen(pen_avgs);
|
|
|
|
dc.DrawLine(x, midpoint - avg_max, x, midpoint - avg_min);
|
|
|
|
}
|
2010-12-08 04:36:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Horizontal zero-point line
|
2011-12-22 22:19:21 +01:00
|
|
|
if (render_averages)
|
|
|
|
dc.SetPen(wxPen(pal->get(1.0f)));
|
|
|
|
else
|
|
|
|
dc.SetPen(pen_peaks);
|
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
dc.DrawLine(0, midpoint, rect.width, midpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-18 23:56:45 +01:00
|
|
|
void AudioWaveformRenderer::RenderBlank(wxDC &dc, const wxRect &rect, AudioRenderingStyle style)
|
2010-12-08 04:36:10 +01:00
|
|
|
{
|
2012-03-12 01:07:16 +01:00
|
|
|
const AudioColorScheme *pal = &colors[style];
|
2010-12-08 04:36:10 +01:00
|
|
|
wxColor line(pal->get(1.0));
|
|
|
|
wxColor bg(pal->get(0.0));
|
|
|
|
|
|
|
|
// Draw the line as background above and below, and line in the middle, to avoid
|
|
|
|
// overdraw flicker (the common theme in all of audio display direct drawing).
|
|
|
|
int halfheight = rect.height / 2;
|
|
|
|
|
|
|
|
dc.SetBrush(wxBrush(bg));
|
|
|
|
dc.SetPen(*wxTRANSPARENT_PEN);
|
|
|
|
dc.DrawRectangle(rect.x, rect.y, rect.width, halfheight);
|
|
|
|
dc.DrawRectangle(rect.x, rect.y + halfheight + 1, rect.width, rect.height - halfheight - 1);
|
|
|
|
|
|
|
|
dc.SetPen(wxPen(line));
|
|
|
|
dc.DrawLine(rect.x, rect.y+halfheight, rect.x+rect.width, rect.y+halfheight);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioWaveformRenderer::OnSetProvider()
|
|
|
|
{
|
|
|
|
delete[] audio_buffer;
|
2013-11-21 18:13:36 +01:00
|
|
|
audio_buffer = nullptr;
|
2010-12-08 04:36:10 +01:00
|
|
|
}
|
|
|
|
|
2012-02-02 00:58:58 +01:00
|
|
|
void AudioWaveformRenderer::OnSetMillisecondsPerPixel()
|
2010-12-08 04:36:10 +01:00
|
|
|
{
|
|
|
|
delete[] audio_buffer;
|
2013-11-21 18:13:36 +01:00
|
|
|
audio_buffer = nullptr;
|
2010-12-08 04:36:10 +01:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:19:21 +01:00
|
|
|
wxArrayString AudioWaveformRenderer::GetWaveformStyles() {
|
|
|
|
wxArrayString ret;
|
|
|
|
ret.push_back(_("Maximum"));
|
|
|
|
ret.push_back(_("Maximum + Average"));
|
|
|
|
return ret;
|
|
|
|
}
|