1 // Copyright (c) 2010, Niels Martin Hansen
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright notice,
8 // this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright notice,
10 // this list of conditions and the following disclaimer in the documentation
11 // and/or other materials provided with the distribution.
12 // * Neither the name of the Aegisub Group nor the names of its contributors
13 // may be used to endorse or promote products derived from this software
14 // without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 // POSSIBILITY OF SUCH DAMAGE.
27 //
28 // Aegisub Project http://www.aegisub.org/
29
30 #include "audio_renderer_waveform.h"
31
32 #include "audio_colorscheme.h"
33 #include "options.h"
34
35 #include <libaegisub/audio/provider.h>
36
37 #include <algorithm>
38 #include <wx/dcmemory.h>
39
40 enum {
41 /// Only render the peaks
42 Waveform_MaxOnly = 0,
43 /// Render the peaks and averages
44 Waveform_MaxAvg,
45 Waveform_Continuous
46 };
47
AudioWaveformRenderer(std::string const & color_scheme_name)48 AudioWaveformRenderer::AudioWaveformRenderer(std::string const& color_scheme_name)
49 : render_averages(OPT_GET("Audio/Display/Waveform Style")->GetInt() == Waveform_MaxAvg)
50 {
51 colors.reserve(AudioStyle_MAX);
52 for (int i = 0; i < AudioStyle_MAX; ++i)
53 colors.emplace_back(6, color_scheme_name, i);
54 }
55
~AudioWaveformRenderer()56 AudioWaveformRenderer::~AudioWaveformRenderer() { }
57
Render(wxBitmap & bmp,int start,AudioRenderingStyle style)58 void AudioWaveformRenderer::Render(wxBitmap &bmp, int start, AudioRenderingStyle style)
59 {
60 wxMemoryDC dc(bmp);
61 wxRect rect(wxPoint(0, 0), bmp.GetSize());
62 int midpoint = rect.height / 2;
63
64 const AudioColorScheme *pal = &colors[style];
65
66 double pixel_samples = pixel_ms * provider->GetSampleRate() / 1000.0;
67
68 // Fill the background
69 dc.SetBrush(wxBrush(pal->get(0.0f)));
70 dc.SetPen(*wxTRANSPARENT_PEN);
71 dc.DrawRectangle(rect);
72
73 // Make sure we've got a buffer to fill with audio data
74 if (!audio_buffer)
75 {
76 // Buffer for one pixel strip of audio
77 size_t buffer_needed = pixel_samples * provider->GetChannels() * provider->GetBytesPerSample();
78 audio_buffer.reset(new char[buffer_needed]);
79 }
80
81 double cur_sample = start * pixel_samples;
82
83 assert(provider->GetBytesPerSample() == 2);
84 assert(provider->GetChannels() == 1);
85
86 wxPen pen_peaks(wxPen(pal->get(0.4f)));
87 wxPen pen_avgs(wxPen(pal->get(0.7f)));
88
89 for (int x = 0; x < rect.width; ++x)
90 {
91 provider->GetAudio(audio_buffer.get(), (int64_t)cur_sample, (int64_t)pixel_samples);
92 cur_sample += pixel_samples;
93
94 int peak_min = 0, peak_max = 0;
95 int64_t avg_min_accum = 0, avg_max_accum = 0;
96 auto aud = reinterpret_cast<const int16_t *>(audio_buffer.get());
97 for (int si = pixel_samples; si > 0; --si, ++aud)
98 {
99 if (*aud > 0)
100 {
101 peak_max = std::max(peak_max, (int)*aud);
102 avg_max_accum += *aud;
103 }
104 else
105 {
106 peak_min = std::min(peak_min, (int)*aud);
107 avg_min_accum += *aud;
108 }
109 }
110
111 // midpoint is half height
112 peak_min = std::max((int)(peak_min * amplitude_scale * midpoint) / 0x8000, -midpoint);
113 peak_max = std::min((int)(peak_max * amplitude_scale * midpoint) / 0x8000, midpoint);
114 int avg_min = std::max((int)(avg_min_accum * amplitude_scale * midpoint / pixel_samples) / 0x8000, -midpoint);
115 int avg_max = std::min((int)(avg_max_accum * amplitude_scale * midpoint / pixel_samples) / 0x8000, midpoint);
116
117 dc.SetPen(pen_peaks);
118 dc.DrawLine(x, midpoint - peak_max, x, midpoint - peak_min);
119 if (render_averages) {
120 dc.SetPen(pen_avgs);
121 dc.DrawLine(x, midpoint - avg_max, x, midpoint - avg_min);
122 }
123 }
124
125 // Horizontal zero-point line
126 if (render_averages)
127 dc.SetPen(wxPen(pal->get(1.0f)));
128 else
129 dc.SetPen(pen_peaks);
130
131 dc.DrawLine(0, midpoint, rect.width, midpoint);
132 }
133
RenderBlank(wxDC & dc,const wxRect & rect,AudioRenderingStyle style)134 void AudioWaveformRenderer::RenderBlank(wxDC &dc, const wxRect &rect, AudioRenderingStyle style)
135 {
136 const AudioColorScheme *pal = &colors[style];
137 wxColor line(pal->get(1.0));
138 wxColor bg(pal->get(0.0));
139
140 // Draw the line as background above and below, and line in the middle, to avoid
141 // overdraw flicker (the common theme in all of audio display direct drawing).
142 int halfheight = rect.height / 2;
143
144 dc.SetBrush(wxBrush(bg));
145 dc.SetPen(*wxTRANSPARENT_PEN);
146 dc.DrawRectangle(rect.x, rect.y, rect.width, halfheight);
147 dc.DrawRectangle(rect.x, rect.y + halfheight + 1, rect.width, rect.height - halfheight - 1);
148
149 dc.SetPen(wxPen(line));
150 dc.DrawLine(rect.x, rect.y+halfheight, rect.x+rect.width, rect.y+halfheight);
151 }
152
GetWaveformStyles()153 wxArrayString AudioWaveformRenderer::GetWaveformStyles() {
154 wxArrayString ret;
155 ret.push_back(_("Maximum"));
156 ret.push_back(_("Maximum + Average"));
157 return ret;
158 }
159