Use unique_ptr for AudioWaveformRenderer's decode buffer

This commit is contained in:
Thomas Goyne 2014-05-22 17:08:08 -07:00
parent 09e325a1c3
commit e0b8c21590
2 changed files with 8 additions and 23 deletions

View File

@ -34,7 +34,6 @@
#include "options.h" #include "options.h"
#include <algorithm> #include <algorithm>
#include <wx/dcmemory.h> #include <wx/dcmemory.h>
enum { enum {
@ -53,10 +52,7 @@ AudioWaveformRenderer::AudioWaveformRenderer(std::string const& color_scheme_nam
colors.emplace_back(6, color_scheme_name, i); colors.emplace_back(6, color_scheme_name, i);
} }
AudioWaveformRenderer::~AudioWaveformRenderer() AudioWaveformRenderer::~AudioWaveformRenderer() { }
{
delete[] audio_buffer;
}
void AudioWaveformRenderer::Render(wxBitmap &bmp, int start, AudioRenderingStyle style) void AudioWaveformRenderer::Render(wxBitmap &bmp, int start, AudioRenderingStyle style)
{ {
@ -78,7 +74,7 @@ void AudioWaveformRenderer::Render(wxBitmap &bmp, int start, AudioRenderingStyle
{ {
// Buffer for one pixel strip of audio // Buffer for one pixel strip of audio
size_t buffer_needed = pixel_samples * provider->GetChannels() * provider->GetBytesPerSample(); size_t buffer_needed = pixel_samples * provider->GetChannels() * provider->GetBytesPerSample();
audio_buffer = new char[buffer_needed]; audio_buffer.reset(new char[buffer_needed]);
} }
double cur_sample = start * pixel_samples; double cur_sample = start * pixel_samples;
@ -91,12 +87,12 @@ void AudioWaveformRenderer::Render(wxBitmap &bmp, int start, AudioRenderingStyle
for (int x = 0; x < rect.width; ++x) for (int x = 0; x < rect.width; ++x)
{ {
provider->GetAudio(audio_buffer, (int64_t)cur_sample, (int64_t)pixel_samples); provider->GetAudio(audio_buffer.get(), (int64_t)cur_sample, (int64_t)pixel_samples);
cur_sample += pixel_samples; cur_sample += pixel_samples;
int peak_min = 0, peak_max = 0; int peak_min = 0, peak_max = 0;
int64_t avg_min_accum = 0, avg_max_accum = 0; int64_t avg_min_accum = 0, avg_max_accum = 0;
const int16_t *aud = (const int16_t *)audio_buffer; auto aud = reinterpret_cast<const int16_t *>(audio_buffer.get());
for (int si = pixel_samples; si > 0; --si, ++aud) for (int si = pixel_samples; si > 0; --si, ++aud)
{ {
if (*aud > 0) if (*aud > 0)
@ -153,18 +149,6 @@ void AudioWaveformRenderer::RenderBlank(wxDC &dc, const wxRect &rect, AudioRende
dc.DrawLine(rect.x, rect.y+halfheight, rect.x+rect.width, rect.y+halfheight); dc.DrawLine(rect.x, rect.y+halfheight, rect.x+rect.width, rect.y+halfheight);
} }
void AudioWaveformRenderer::OnSetProvider()
{
delete[] audio_buffer;
audio_buffer = nullptr;
}
void AudioWaveformRenderer::OnSetMillisecondsPerPixel()
{
delete[] audio_buffer;
audio_buffer = nullptr;
}
wxArrayString AudioWaveformRenderer::GetWaveformStyles() { wxArrayString AudioWaveformRenderer::GetWaveformStyles() {
wxArrayString ret; wxArrayString ret;
ret.push_back(_("Maximum")); ret.push_back(_("Maximum"));

View File

@ -29,6 +29,7 @@
#include "audio_renderer.h" #include "audio_renderer.h"
#include <memory>
#include <vector> #include <vector>
class AudioColorScheme; class AudioColorScheme;
@ -40,13 +41,13 @@ class AudioWaveformRenderer final : public AudioRendererBitmapProvider {
std::vector<AudioColorScheme> colors; std::vector<AudioColorScheme> colors;
/// Pre-allocated buffer for audio fetched from provider /// Pre-allocated buffer for audio fetched from provider
char *audio_buffer = nullptr; std::unique_ptr<char[]> audio_buffer;
/// Whether to render max+avg or just max /// Whether to render max+avg or just max
bool render_averages; bool render_averages;
void OnSetProvider() override; void OnSetProvider() override { audio_buffer.reset(); }
void OnSetMillisecondsPerPixel() override; void OnSetMillisecondsPerPixel() override { audio_buffer.reset(); }
public: public:
/// @brief Constructor /// @brief Constructor